Skip to content

Commit 50b8899

Browse files
committed
schemachanger: support ttl_job_cron parameter in declarative schema changer
Previously, modifying the ttl_job_cron storage parameter required falling back to the legacy schema changer because the declarative schema changer had no mechanism to update the TTL scheduled job's cron expression. This commit adds support for ttl_job_cron by: 1. Adding UpdateTTLScheduleCron as a new deferred mutation operation that updates the cron expression on the existing TTL scheduled job. 2. Implementing findOldTTLCron in opgen to detect when the cron expression has changed by comparing the new RowLevelTTL element against the one being dropped, emitting UpdateTTLScheduleCron only when necessary. 3. Adding UpdateTTLScheduleCron to metadataUpdater which loads the scheduled job, updates its cron expression, and persists the change. Release note: None
1 parent bf2f15b commit 50b8899

File tree

13 files changed

+161
-4
lines changed

13 files changed

+161
-4
lines changed

pkg/sql/descmetadata/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ go_library(
66
importpath = "github.com/cockroachdb/cockroach/pkg/sql/descmetadata",
77
visibility = ["//visibility:public"],
88
deps = [
9+
"//pkg/jobs",
910
"//pkg/jobs/jobspb",
1011
"//pkg/keys",
12+
"//pkg/scheduledjobs",
1113
"//pkg/settings",
1214
"//pkg/sql/catalog",
1315
"//pkg/sql/catalog/descpb",

pkg/sql/descmetadata/metadata_updater.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import (
99
"context"
1010
"fmt"
1111

12+
"github.com/cockroachdb/cockroach/pkg/jobs"
1213
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
1314
"github.com/cockroachdb/cockroach/pkg/keys"
15+
"github.com/cockroachdb/cockroach/pkg/scheduledjobs"
1416
"github.com/cockroachdb/cockroach/pkg/settings"
1517
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
1618
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
@@ -113,3 +115,19 @@ func (mu metadataUpdater) UpdateTTLScheduleLabel(
113115
)
114116
return err
115117
}
118+
119+
// UpdateTTLScheduleCron implement scexec.DescriptorMetadataUpdater.
120+
func (mu metadataUpdater) UpdateTTLScheduleCron(
121+
ctx context.Context, scheduleID jobspb.ScheduleID, cronExpr string,
122+
) error {
123+
env := scheduledjobs.ProdJobSchedulerEnv
124+
schedules := jobs.ScheduledJobTxn(mu.txn)
125+
s, err := schedules.Load(ctx, env, scheduleID)
126+
if err != nil {
127+
return err
128+
}
129+
if err := s.SetScheduleAndNextRun(cronExpr); err != nil {
130+
return err
131+
}
132+
return schedules.Update(ctx, s)
133+
}

pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_set_storage_param.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ import (
2727
func validateStorageParamKey(t tree.NodeFormatter, key string) {
2828
loweredKey := strings.ToLower(key)
2929
// These TTL params still require legacy schema changer because they
30-
// affect column management, column dependencies, or schedule management.
30+
// affect column management or column dependencies.
3131
switch loweredKey {
32-
case "ttl", "ttl_expire_after", "ttl_expiration_expression", "ttl_job_cron":
32+
case "ttl", "ttl_expire_after", "ttl_expiration_expression":
3333
panic(scerrors.NotImplementedErrorf(t, redact.Sprintf("%s not implemented yet", redact.SafeString(key))))
3434
}
3535
if loweredKey == catpb.RBRUsingConstraintTableSettingName {

pkg/sql/schemachanger/scdeps/sctestdeps/test_deps.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,14 @@ func (s *TestState) UpdateTTLScheduleLabel(ctx context.Context, tbl catalog.Tabl
13411341
return nil
13421342
}
13431343

1344+
// UpdateTTLScheduleCron implements scexec.DescriptorMetadataUpdater
1345+
func (s *TestState) UpdateTTLScheduleCron(
1346+
ctx context.Context, scheduleID jobspb.ScheduleID, cronExpr string,
1347+
) error {
1348+
s.LogSideEffectf("update ttl schedule cron #%d to %q", scheduleID, cronExpr)
1349+
return nil
1350+
}
1351+
13441352
// DescriptorMetadataUpdater implement scexec.Dependencies.
13451353
func (s *TestState) DescriptorMetadataUpdater(
13461354
ctx context.Context,

pkg/sql/schemachanger/scexec/dependencies.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,9 @@ type DescriptorMetadataUpdater interface {
386386
// UpdateTTLScheduleLabel updates the schedule_name for the TTL Scheduled Job
387387
// of the given table.
388388
UpdateTTLScheduleLabel(ctx context.Context, tbl catalog.TableDescriptor) error
389+
390+
// UpdateTTLScheduleCron updates the cron schedule for a TTL job.
391+
UpdateTTLScheduleCron(ctx context.Context, scheduleID jobspb.ScheduleID, cronExpr string) error
389392
}
390393

391394
type TemporarySchemaCreator interface {

pkg/sql/schemachanger/scexec/exec_deferred_mutation.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type deferredState struct {
2929
statsToRefresh catalog.DescriptorIDSet
3030
indexesToSplitAndScatter []indexesToSplitAndScatter
3131
ttlScheduleMetadataUpdates []ttlScheduleMetadataUpdate
32+
ttlScheduleCronUpdates []ttlScheduleCronUpdate
3233
gcJobs
3334
}
3435

@@ -47,6 +48,11 @@ type ttlScheduleMetadataUpdate struct {
4748
newName string
4849
}
4950

51+
type ttlScheduleCronUpdate struct {
52+
scheduleID jobspb.ScheduleID
53+
newCronExpr string
54+
}
55+
5056
type schemaChangerJobUpdate struct {
5157
isNonCancelable bool
5258
runningStatus redact.RedactableString
@@ -92,6 +98,16 @@ func (s *deferredState) UpdateTTLScheduleMetadata(
9298
return nil
9399
}
94100

101+
func (s *deferredState) UpdateTTLScheduleCron(
102+
ctx context.Context, scheduleID jobspb.ScheduleID, cronExpr string,
103+
) error {
104+
s.ttlScheduleCronUpdates = append(s.ttlScheduleCronUpdates, ttlScheduleCronUpdate{
105+
scheduleID: scheduleID,
106+
newCronExpr: cronExpr,
107+
})
108+
return nil
109+
}
110+
95111
func (s *deferredState) AddNewSchemaChangerJob(
96112
jobID jobspb.JobID,
97113
stmts []scpb.Statement,
@@ -226,6 +242,11 @@ func (s *deferredState) exec(
226242
return err
227243
}
228244
}
245+
for _, cronUpdate := range s.ttlScheduleCronUpdates {
246+
if err := m.UpdateTTLScheduleCron(ctx, cronUpdate.scheduleID, cronUpdate.newCronExpr); err != nil {
247+
return err
248+
}
249+
}
229250
for _, idx := range s.indexesToSplitAndScatter {
230251
descs, err := c.MustReadImmutableDescriptors(ctx, idx.tableID)
231252
if err != nil {

pkg/sql/schemachanger/scexec/executor_external_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,12 @@ func (noopMetadataUpdater) UpdateTTLScheduleLabel(
530530
return nil
531531
}
532532

533+
func (u noopMetadataUpdater) UpdateTTLScheduleCron(
534+
ctx context.Context, scheduleID jobspb.ScheduleID, cronExpr string,
535+
) error {
536+
return nil
537+
}
538+
533539
type noopTemporarySchemaCreator struct{}
534540

535541
var _ scexec.TemporarySchemaCreator = noopTemporarySchemaCreator{}

pkg/sql/schemachanger/scexec/scmutationexec/dependencies.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,7 @@ type DeferredMutationStateUpdater interface {
159159

160160
// UpdateTTLScheduleMetadata updates the TTL schedule metadata for a table.
161161
UpdateTTLScheduleMetadata(ctx context.Context, tableID descpb.ID, newName string) error
162+
163+
// UpdateTTLScheduleCron updates the cron expression for a TTL schedule.
164+
UpdateTTLScheduleCron(ctx context.Context, scheduleID jobspb.ScheduleID, cronExpr string) error
162165
}

pkg/sql/schemachanger/scexec/scmutationexec/table.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,9 @@ func (d *deferredVisitor) UpdateTTLScheduleMetadata(
7777
) error {
7878
return d.DeferredMutationStateUpdater.UpdateTTLScheduleMetadata(ctx, op.TableID, op.NewName)
7979
}
80+
81+
func (d *deferredVisitor) UpdateTTLScheduleCron(
82+
ctx context.Context, op scop.UpdateTTLScheduleCron,
83+
) error {
84+
return d.DeferredMutationStateUpdater.UpdateTTLScheduleCron(ctx, op.ScheduleID, op.NewCronExpr)
85+
}

pkg/sql/schemachanger/scop/deferred_mutation.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,11 @@ type UpdateTTLScheduleMetadata struct {
107107
TableID descpb.ID
108108
NewName string
109109
}
110+
111+
// UpdateTTLScheduleCron updates the cron schedule expression for a TTL job.
112+
type UpdateTTLScheduleCron struct {
113+
deferredMutationOp
114+
TableID descpb.ID
115+
ScheduleID jobspb.ScheduleID
116+
NewCronExpr string
117+
}

0 commit comments

Comments
 (0)