Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ttl: Schedule TTL job when ttl_expiration_expression is set after table creation #88260

Merged
merged 1 commit into from
Sep 25, 2022
Merged

ttl: Schedule TTL job when ttl_expiration_expression is set after table creation #88260

merged 1 commit into from
Sep 25, 2022

Conversation

ecwall
Copy link
Contributor

@ecwall ecwall commented Sep 20, 2022

fixes #88254
refs #88560

Previously a TTL mutation was added for the schema changer only when
ttl_expire_after was set. Now a TTL mutation is also added when
ttl_expiration_expression is set.

Release note: None

@ecwall ecwall requested a review from a team September 20, 2022 16:46
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@ecwall ecwall changed the title ttl: Schedule TTL job when ttl_expiration_expression is set after table ttl: Schedule TTL job when ttl_expiration_expression is set after table creation Sep 20, 2022
@ecwall ecwall requested review from rafiss, otan and ajwerner September 20, 2022 16:46

subtest end

subtest create_table_no_ttl_set_ttl_expire_after
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the existing test to a subtest so I can run it individually.


query TTT
SELECT schedule_status, recurrence, owner FROM [SHOW SCHEDULES]
WHERE label = 'row-level-ttl-$table_id'
----
ACTIVE @hourly root

subtest end

subtest create_table_no_ttl_set_ttl_expiration_expression
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the existing test but uses ttl_expiration_expression instead of ttl_expire_after.

SELECT schedule_status, recurrence, owner FROM [SHOW SCHEDULES]
WHERE label = 'row-level-ttl-$table_id'
----
ACTIVE @hourly root
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This query returns 0 results without the alter_table.go change.

@otan
Copy link
Contributor

otan commented Sep 20, 2022

going to defer to schema to take a look at this one!

@otan
Copy link
Contributor

otan commented Sep 20, 2022

couple of notes in my head

  • adding a mutation seems like overkill in this case - if that fails, the TTL remains on the table but has no job in that case. it may be better to just add the schedule.
  • make sure dropping the ttl_expiration_expression also drops the scheduled job

@ecwall
Copy link
Contributor Author

ecwall commented Sep 20, 2022

couple of notes in my head

  • adding a mutation seems like overkill in this case - if that fails, the TTL remains on the table but has no job in that case. it may be better to just add the schedule.
  • make sure dropping the ttl_expiration_expression also drops the scheduled job

Can we simplify the process and make maybeUpdateScheduledJobsForRowLevelTTL always schedule or unschedule the job then and never use a mutation?

@otan
Copy link
Contributor

otan commented Sep 20, 2022

as long as cancelling the job adding or dropping the TTL column also correctly accounts for the schedule creation

@ecwall
Copy link
Contributor Author

ecwall commented Sep 20, 2022

as long as cancelling the job adding or dropping the TTL column also correctly accounts for the schedule creation

Can TableDescriptor.RowLevelTTL.ScheduleID be moved to TableDescriptor.TTLScheduleID since it looks like it is not modified with TableDescriptor.RowLevelTTL in the same transaction?

The previous ScheduleID needs to be maintained in the table descriptor until the job is unscheduled if TTL is unset on the table.

@ajwerner
Copy link
Contributor

My problem, from looking at this, is that I don't have a well-formed mental model on how this stuff is supposed to work. @otan I guess I wonder why you think a mutation is in any way wrong here? It seems somewhat important to me.

Consider:

CREATE TABLE t (
   id INT PRIMARY KEY,
   text STRING
   expire_at TIMESTAMPTZ,
   FAMILY (id, text, expire_at)
);

INSERT INTO t VALUES (1, 'a', NULL), (2, 'a', NULL);
BEGIN;
CREATE UNIQUE INDEX idx ON t(text);
ALTER TABLE create_table_no_ttl_set_ttl_expiration_expression SET (ttl_expiration_expression = 'expire_at');
COMMIT;

What should happen? I sincerely hope we'd roll back the ttl change.

@otan
Copy link
Contributor

otan commented Sep 20, 2022

ah i think the code as is immediately sets TTLExpirationExpression on the TableDescriptor. if we moved the TTLExpirationExpression to be in mutation and only apply the TableDescriptor change on the schema changer when processing the mutation then i think we're in the clear

i am totally rusty after 6 months 😬

Copy link
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's an edge case but what happens if you have multiple ALTER TABLE commands which muck with row-level TTL settings in the same transaction? My reading is that it's just going to add more mutations. Is that the right thing?

Generally this seems sane to me. Thanks for tracking it down and picking up all the context you did on this code.

statement error "ttl_expire_after" and/or "ttl_expiration_expression" must be set
CREATE TABLE tbl (id INT PRIMARY KEY, text TEXT) WITH (ttl = 'on')

subtest end

subtest todo_add_subtests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this about?

Copy link
Contributor Author

@ecwall ecwall Sep 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was isolating the failing tests to debug by putting them in subtests, but I didn't do all of them so I just moved subtest todo_add_subtests from above.

@@ -1847,7 +1844,7 @@ func dropColumnImpl(

func handleTTLStorageParamChange(
params runParams, tn *tree.TableName, tableDesc *tabledesc.Mutable, after *catpb.RowLevelTTL,
) error {
) (bool, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: name the return bool

@ecwall
Copy link
Contributor Author

ecwall commented Sep 23, 2022

it's an edge case but what happens if you have multiple ALTER TABLE commands which muck with row-level TTL settings in the same transaction? My reading is that it's just going to add more mutations. Is that the right thing?

Generally this seems sane to me. Thanks for tracking it down and picking up all the context you did on this code.

Right now it adds mutations if the job is scheduled/unscheduled and if the column is added/dropped, otherwise it does the change immediately. I have another followup PR to add tests for the immediate vs mutation changes and I can add tests for this also.

@ecwall ecwall requested a review from ajwerner September 23, 2022 18:37
creation

fixes #88254
refs #88560

Previously a TTL mutation was added for the schema changer only when
ttl_expire_after was set. Now a TTL mutation is also added when
ttl_expiration_expression is set.

Release note: None
Copy link
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ecwall
Copy link
Contributor Author

ecwall commented Sep 25, 2022

bors r=ajwerner

@craig
Copy link
Contributor

craig bot commented Sep 25, 2022

Build succeeded:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ALTER TABLE ... SET (ttl_expiration_expression=...) does not schedule TTL job
4 participants