Skip to content

Commit 509f8c8

Browse files
craig[bot]chrissetojeffswensonerikgrinakerRenato Costa
committed
107294: ttl: show table name for jobs in SHOW SCHEDULES r=chrisseto a=chrisseto Previously, `SHOW SCHEDULES` would only show a table's ID for row level TTL jobs. For the convenience of debugging, this commit upgrades the `label` column to contain the table's name instead of its ID. Rather than performing table name resolution at query time, the job name is set at creation time and updated by the schema change during table renames. This minimizes the likelihood of `SHOW SCHEDULES` being rendered unusable when a cluster is in an unstable state. Epic: CRDB-18322 Fixes: cockroachdb#93774 107316: sqlproxyccl: simplify NewSubStopper r=JeffSwenson a=JeffSwenson The lock in NewSubStopper caused lock ordering warnings when run under deadlock detection. The justification given for the lock's existence is wrong. If a closer is added to an already stopped stopper, the closer is called immediately. Release note: None Part of: cockroachdb#106571 107941: storage: don't import `testing` for `DisableMetamorphicSimpleValueEncoding` r=erikgrinaker a=erikgrinaker We shouldn't link `testing` in binaries. Epic: none Release note: None 107950: testutils: fix determinism in random predecessor history tests r=jayshrivastava a=renatolabs Previously, the `rng` instance used by the tests was initialized once in the `var` block. That meant that every assertion that relied on that rng was order-dependent: running a single test in that file in isolation would lead to failed assertions. This commit ensures that every test resets the rng before making any assertions, guaranteeing we get the same values regardless of execution order. Epic: none Release note: None Co-authored-by: Chris Seto <[email protected]> Co-authored-by: Jeff <[email protected]> Co-authored-by: Erik Grinaker <[email protected]> Co-authored-by: Renato Costa <[email protected]>
5 parents f6c4d9d + fc59204 + 6d94042 + b67f136 + 1395ba8 commit 509f8c8

File tree

22 files changed

+260
-111
lines changed

22 files changed

+260
-111
lines changed

pkg/ccl/backupccl/restore_job.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,8 +2239,7 @@ func (r *restoreResumer) publishDescriptors(
22392239
jobsKnobs,
22402240
jobs.ScheduledJobTxn(txn),
22412241
user,
2242-
mutTable.GetID(),
2243-
mutTable.GetRowLevelTTL(),
2242+
mutTable,
22442243
)
22452244
if err != nil {
22462245
return err

pkg/ccl/backupccl/testdata/backup-restore/restore-on-fail-or-cancel-ttl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ job cancel=a
3434
----
3535

3636
query-sql
37-
SELECT count(1) FROM [SHOW SCHEDULES] WHERE label LIKE 'row-level-ttl-%';
37+
SELECT count(1) FROM [SHOW SCHEDULES] WHERE label LIKE 'row-level-ttl%';
3838
----
3939
0
4040

@@ -75,7 +75,7 @@ job cancel=b
7575
----
7676

7777
query-sql
78-
SELECT count(1) FROM [SHOW SCHEDULES] WHERE label LIKE 'row-level-ttl-%';
78+
SELECT count(1) FROM [SHOW SCHEDULES] WHERE label LIKE 'row-level-ttl%';
7979
----
8080
0
8181

pkg/ccl/backupccl/testdata/backup-restore/row_level_ttl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ new-cluster name=s2 share-io-dir=s1 allow-implicit-access
2020

2121
query-sql
2222
SELECT count(1) FROM [SHOW SCHEDULES]
23-
WHERE label LIKE 'row-level-ttl-%'
23+
WHERE label LIKE 'row-level-ttl%'
2424
----
2525
0
2626

@@ -43,7 +43,7 @@ CREATE TABLE public.t (
4343

4444
query-sql
4545
SELECT count(1) FROM [SHOW SCHEDULES]
46-
WHERE label LIKE 'row-level-ttl-%'
46+
WHERE label LIKE 'row-level-ttl%'
4747
----
4848
1
4949

@@ -53,7 +53,7 @@ DROP DATABASE d
5353

5454
query-sql
5555
SELECT count(1) FROM [SHOW SCHEDULES]
56-
WHERE label LIKE 'row-level-ttl-%'
56+
WHERE label LIKE 'row-level-ttl%'
5757
----
5858
0
5959

@@ -76,7 +76,7 @@ CREATE TABLE public.t (
7676

7777
query-sql
7878
SELECT count(1) FROM [SHOW SCHEDULES]
79-
WHERE label LIKE 'row-level-ttl-%'
79+
WHERE label LIKE 'row-level-ttl%'
8080
----
8181
1
8282

@@ -86,7 +86,7 @@ DROP DATABASE d
8686

8787
query-sql
8888
SELECT count(1) FROM [SHOW SCHEDULES]
89-
WHERE label LIKE 'row-level-ttl-%'
89+
WHERE label LIKE 'row-level-ttl%'
9090
----
9191
0
9292

@@ -113,7 +113,7 @@ CREATE TABLE public.t (
113113

114114
query-sql
115115
SELECT count(1) FROM [SHOW SCHEDULES]
116-
WHERE label LIKE 'row-level-ttl-%'
116+
WHERE label LIKE 'row-level-ttl%'
117117
----
118118
1
119119

@@ -123,6 +123,6 @@ DROP TABLE d.public.t
123123

124124
query-sql
125125
SELECT count(1) FROM [SHOW SCHEDULES]
126-
WHERE label LIKE 'row-level-ttl-%'
126+
WHERE label LIKE 'row-level-ttl%'
127127
----
128128
0

pkg/ccl/sqlproxyccl/tenantdirsvr/test_directory_svr.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,12 @@ import (
2929
var _ tenant.DirectoryServer = (*TestDirectoryServer)(nil)
3030

3131
// NewSubStopper creates a new stopper that will be stopped when either the
32-
// parent is stopped or its own Stop is called. The code is slightly more
33-
// complicated that simply calling NewStopper followed by AddCloser since there
34-
// is a possibility that between the two calls, the parent stopper completes a
35-
// stop and then the leak detection may find a leaked stopper.
32+
// parent is stopped or its own Stop is called.
3633
func NewSubStopper(parentStopper *stop.Stopper) *stop.Stopper {
37-
var mu syncutil.Mutex
38-
var subStopper *stop.Stopper
34+
subStopper := stop.NewStopper(stop.WithTracer(parentStopper.Tracer()))
3935
parentStopper.AddCloser(stop.CloserFn(func() {
40-
mu.Lock()
41-
defer mu.Unlock()
42-
if subStopper == nil {
43-
subStopper = stop.NewStopper(stop.WithTracer(parentStopper.Tracer()))
44-
}
4536
subStopper.Stop(context.Background())
4637
}))
47-
mu.Lock()
48-
defer mu.Unlock()
49-
if subStopper == nil {
50-
subStopper = stop.NewStopper(stop.WithTracer(parentStopper.Tracer()))
51-
}
5238
return subStopper
5339
}
5440

pkg/sql/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ go_library(
508508
"//pkg/sql/storageparam/tablestorageparam",
509509
"//pkg/sql/syntheticprivilege",
510510
"//pkg/sql/syntheticprivilegecache",
511+
"//pkg/sql/ttl/ttlbase",
511512
"//pkg/sql/types",
512513
"//pkg/sql/vtable",
513514
"//pkg/storage",

pkg/sql/check.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,8 +804,7 @@ func (p *planner) RepairTTLScheduledJobForTable(ctx context.Context, tableID int
804804
p.ExecCfg().JobsKnobs(),
805805
jobs.ScheduledJobTxn(p.InternalSQLTxn()),
806806
p.User(),
807-
tableDesc.GetID(),
808-
tableDesc.GetRowLevelTTL(),
807+
tableDesc,
809808
)
810809
if err != nil {
811810
return err

pkg/sql/create_table.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import (
5959
"github.com/cockroachdb/cockroach/pkg/sql/storageparam"
6060
"github.com/cockroachdb/cockroach/pkg/sql/storageparam/indexstorageparam"
6161
"github.com/cockroachdb/cockroach/pkg/sql/storageparam/tablestorageparam"
62+
"github.com/cockroachdb/cockroach/pkg/sql/ttl/ttlbase"
6263
"github.com/cockroachdb/cockroach/pkg/sql/types"
6364
"github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented"
6465
"github.com/cockroachdb/cockroach/pkg/util/hlc"
@@ -2416,8 +2417,7 @@ func newTableDesc(
24162417
params.ExecCfg().JobsKnobs(),
24172418
jobs.ScheduledJobTxn(params.p.InternalSQLTxn()),
24182419
params.p.User(),
2419-
ret.GetID(),
2420-
ttl,
2420+
ret,
24212421
)
24222422
if err != nil {
24232423
return nil, err
@@ -2428,27 +2428,25 @@ func newTableDesc(
24282428
}
24292429

24302430
// newRowLevelTTLScheduledJob returns a *jobs.ScheduledJob for row level TTL
2431-
// for a given table.
2431+
// for a given table. newRowLevelTTLScheduledJob assumes that
2432+
// tblDesc.RowLevelTTL is not nil.
24322433
func newRowLevelTTLScheduledJob(
2433-
env scheduledjobs.JobSchedulerEnv,
2434-
owner username.SQLUsername,
2435-
tblID descpb.ID,
2436-
ttl *catpb.RowLevelTTL,
2434+
env scheduledjobs.JobSchedulerEnv, owner username.SQLUsername, tblDesc *tabledesc.Mutable,
24372435
) (*jobs.ScheduledJob, error) {
24382436
sj := jobs.NewScheduledJob(env)
2439-
sj.SetScheduleLabel(fmt.Sprintf("row-level-ttl-%d", tblID))
2437+
sj.SetScheduleLabel(ttlbase.BuildScheduleLabel(tblDesc))
24402438
sj.SetOwner(owner)
24412439
sj.SetScheduleDetails(jobspb.ScheduleDetails{
24422440
Wait: jobspb.ScheduleDetails_WAIT,
24432441
// If a job fails, try again at the allocated cron time.
24442442
OnError: jobspb.ScheduleDetails_RETRY_SCHED,
24452443
})
24462444

2447-
if err := sj.SetSchedule(ttl.DeletionCronOrDefault()); err != nil {
2445+
if err := sj.SetSchedule(tblDesc.RowLevelTTL.DeletionCronOrDefault()); err != nil {
24482446
return nil, err
24492447
}
24502448
args := &catpb.ScheduledRowLevelTTLArgs{
2451-
TableID: tblID,
2449+
TableID: tblDesc.GetID(),
24522450
}
24532451
any, err := pbtypes.MarshalAny(args)
24542452
if err != nil {
@@ -2467,12 +2465,15 @@ func CreateRowLevelTTLScheduledJob(
24672465
knobs *jobs.TestingKnobs,
24682466
s jobs.ScheduledJobStorage,
24692467
owner username.SQLUsername,
2470-
tblID descpb.ID,
2471-
ttl *catpb.RowLevelTTL,
2468+
tblDesc *tabledesc.Mutable,
24722469
) (*jobs.ScheduledJob, error) {
2470+
if !tblDesc.HasRowLevelTTL() {
2471+
return nil, errors.AssertionFailedf("CreateRowLevelTTLScheduledJob called with no .RowLevelTTL: %#v", tblDesc)
2472+
}
2473+
24732474
telemetry.Inc(sqltelemetry.RowLevelTTLCreated)
24742475
env := JobSchedulerEnv(knobs)
2475-
j, err := newRowLevelTTLScheduledJob(env, owner, tblID, ttl)
2476+
j, err := newRowLevelTTLScheduledJob(env, owner, tblDesc)
24762477
if err != nil {
24772478
return nil, err
24782479
}

pkg/sql/descmetadata/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ go_library(
1010
"//pkg/settings",
1111
"//pkg/sql/catalog/descpb",
1212
"//pkg/sql/catalog/descs",
13+
"//pkg/sql/catalog/tabledesc",
1314
"//pkg/sql/isql",
1415
"//pkg/sql/schemachanger/scexec",
1516
"//pkg/sql/sessiondata",
1617
"//pkg/sql/sessioninit",
18+
"//pkg/sql/ttl/ttlbase",
1719
],
1820
)
1921

pkg/sql/descmetadata/metadata_updater.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ import (
1818
"github.com/cockroachdb/cockroach/pkg/settings"
1919
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
2020
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descs"
21+
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
2122
"github.com/cockroachdb/cockroach/pkg/sql/isql"
2223
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scexec"
2324
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
2425
"github.com/cockroachdb/cockroach/pkg/sql/sessioninit"
26+
"github.com/cockroachdb/cockroach/pkg/sql/ttl/ttlbase"
2527
)
2628

2729
// metadataUpdater which implements scexec.MetaDataUpdater that is used to update
@@ -94,3 +96,23 @@ func (mu metadataUpdater) DeleteSchedule(ctx context.Context, scheduleID int64)
9496
)
9597
return err
9698
}
99+
100+
// UpdateTTLScheduleLabel implement scexec.DescriptorMetadataUpdater.
101+
func (mu metadataUpdater) UpdateTTLScheduleLabel(
102+
ctx context.Context, tbl *tabledesc.Mutable,
103+
) error {
104+
if !tbl.HasRowLevelTTL() {
105+
return nil
106+
}
107+
108+
_, err := mu.txn.ExecEx(
109+
ctx,
110+
"update-ttl-schedule-label",
111+
mu.txn.KV(),
112+
sessiondata.RootUserSessionDataOverride,
113+
"UPDATE system.scheduled_jobs SET schedule_name = $1 WHERE schedule_id = $2",
114+
ttlbase.BuildScheduleLabel(tbl),
115+
tbl.RowLevelTTL.ScheduleID,
116+
)
117+
return err
118+
}

0 commit comments

Comments
 (0)