Skip to content

Commit bcf17ec

Browse files
committed
scplan: make schedule ID output deterministic in tests
Release note: None
1 parent 50b8899 commit bcf17ec

File tree

6 files changed

+53
-153
lines changed

6 files changed

+53
-153
lines changed

pkg/sql/GEMINI.md

Lines changed: 0 additions & 147 deletions
This file was deleted.

pkg/sql/schemachanger/scpb/uml/table.puml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ object RowLevelTTL
365365
RowLevelTTL : TableID
366366
RowLevelTTL : RowLevelTTL
367367
RowLevelTTL : TTLExpr
368+
RowLevelTTL : SeqNum
368369

369370
object Schema
370371

pkg/sql/schemachanger/scplan/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ go_test(
5050
":scplan",
5151
"//pkg/base",
5252
"//pkg/ccl",
53+
"//pkg/jobs/jobspb",
5354
"//pkg/security/securityassets",
5455
"//pkg/security/securitytest",
5556
"//pkg/server",

pkg/sql/schemachanger/scplan/plan_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"github.com/cockroachdb/cockroach/pkg/base"
1717
"github.com/cockroachdb/cockroach/pkg/ccl"
18+
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
1819
"github.com/cockroachdb/cockroach/pkg/sql"
1920
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
2021
"github.com/cockroachdb/cockroach/pkg/sql/parser"
@@ -200,9 +201,28 @@ func marshalDeps(t *testing.T, plan *scplan.Plan) string {
200201
return stages.String()
201202
}
202203

204+
// normalizeScheduleID maps non-deterministic schedule IDs to deterministic
205+
// sequential values for test output stability. Each unique non-zero schedule
206+
// ID gets mapped to the next available placeholder value (1, 2, 3, ...).
207+
func normalizeScheduleID(
208+
scheduleIDMap map[jobspb.ScheduleID]jobspb.ScheduleID, id jobspb.ScheduleID,
209+
) jobspb.ScheduleID {
210+
if id == 0 {
211+
return 0
212+
}
213+
if mapped, ok := scheduleIDMap[id]; ok {
214+
return mapped
215+
}
216+
nextID := jobspb.ScheduleID(len(scheduleIDMap) + 1)
217+
scheduleIDMap[id] = nextID
218+
return nextID
219+
}
220+
203221
// marshalOps marshals operations in scplan.Plan to a string.
204222
func marshalOps(t *testing.T, ts scpb.TargetState, stages []scstage.Stage) string {
205223
var sb strings.Builder
224+
// Map non-deterministic schedule IDs to deterministic sequential values.
225+
scheduleIDMap := make(map[jobspb.ScheduleID]jobspb.ScheduleID)
206226
for _, stage := range stages {
207227
sb.WriteString(stage.String())
208228
sb.WriteString("\n transitions:\n")
@@ -235,6 +255,18 @@ func marshalOps(t *testing.T, ts scpb.TargetState, stages []scstage.Stage) strin
235255
clone.State = scpb.DescriptorState{}
236256
op = &clone
237257
}
258+
if upsertTTLOp, ok := op.(*scop.UpsertRowLevelTTL); ok {
259+
// Normalize the ScheduleID since it is non-deterministic.
260+
clone := *upsertTTLOp
261+
clone.RowLevelTTL.ScheduleID = normalizeScheduleID(scheduleIDMap, clone.RowLevelTTL.ScheduleID)
262+
op = &clone
263+
}
264+
if updateCronOp, ok := op.(*scop.UpdateTTLScheduleCron); ok {
265+
// Normalize the ScheduleID since it is non-deterministic.
266+
clone := *updateCronOp
267+
clone.ScheduleID = normalizeScheduleID(scheduleIDMap, clone.ScheduleID)
268+
op = &clone
269+
}
238270
opMap, err := scgraphviz.ToMap(op)
239271
require.NoError(t, err)
240272
data, err := yaml.Marshal(opMap)

pkg/sql/schemachanger/scplan/testdata/alter_table_set_storage_param

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ StatementPhase stage 1 of 1 with 1 MutationType op
232232
*scop.UpsertRowLevelTTL
233233
RowLevelTTL:
234234
ExpirationExpr: ts
235-
ScheduleID: 1131801715215400961
235+
ScheduleID: 1
236236
SelectRateLimit: 10
237237
TTLExpr:
238238
expr: ts
@@ -254,7 +254,7 @@ PreCommitPhase stage 2 of 2 with 1 MutationType op
254254
*scop.UpsertRowLevelTTL
255255
RowLevelTTL:
256256
ExpirationExpr: ts
257-
ScheduleID: 1131801715215400961
257+
ScheduleID: 1
258258
SelectRateLimit: 10
259259
TTLExpr:
260260
expr: ts
@@ -263,7 +263,7 @@ PreCommitPhase stage 2 of 2 with 1 MutationType op
263263
TableID: 107
264264

265265
ops
266-
ALTER TABLE ttl_table SET (ttl_job_cron = '0 0 * * *');
266+
ALTER TABLE ttl_table SET (ttl_select_rate_limit = 10, ttl_job_cron = '0 0 * * *');
267267
----
268268
StatementPhase stage 1 of 1 with 1 MutationType op
269269
transitions:
@@ -274,7 +274,8 @@ StatementPhase stage 1 of 1 with 1 MutationType op
274274
RowLevelTTL:
275275
DeletionCron: 0 0 * * *
276276
ExpirationExpr: ts
277-
ScheduleID: 1131801715215400961
277+
ScheduleID: 1
278+
SelectRateLimit: 10
278279
TTLExpr:
279280
expr: ts
280281
referencedColumnIds:
@@ -296,13 +297,14 @@ PreCommitPhase stage 2 of 2 with 2 MutationType ops
296297
RowLevelTTL:
297298
DeletionCron: 0 0 * * *
298299
ExpirationExpr: ts
299-
ScheduleID: 1131801715215400961
300+
ScheduleID: 1
301+
SelectRateLimit: 10
300302
TTLExpr:
301303
expr: ts
302304
referencedColumnIds:
303305
- 2
304306
TableID: 107
305307
*scop.UpdateTTLScheduleCron
306308
NewCronExpr: 0 0 * * *
307-
ScheduleID: 1131801715215400961
309+
ScheduleID: 1
308310
TableID: 107

pkg/sql/schemachanger/sctest/end_to_end.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ func checkExplainDiagrams(
254254
require.NoError(t, err)
255255
out, err := fn()
256256
require.NoError(t, err)
257+
// Normalize non-deterministic values in the explain output.
258+
out = replaceNonDeterministicOutput(out)
257259
_, err = io.WriteString(file, out)
258260
require.NoError(t, err)
259261
}
@@ -296,12 +298,21 @@ func checkExplainDiagrams(
296298
// scheduleIDRegexp captures either `scheduleId: 384784` or `scheduleId: "374764"`.
297299
var scheduleIDRegexp = regexp.MustCompile(`scheduleId: "?[0-9]+"?`)
298300

301+
// scheduleIDJSONRegexp captures `"ScheduleID":1234567890` in JSON output.
302+
var scheduleIDJSONRegexp = regexp.MustCompile(`"ScheduleID":[0-9]+`)
303+
304+
// scheduleIDLogRegexp captures `#1234567890` schedule IDs in log output like
305+
// "update ttl schedule cron #1234567890 to ...".
306+
var scheduleIDLogRegexp = regexp.MustCompile(`(update ttl schedule cron )#[0-9]+`)
307+
299308
// dropTimeRegexp captures either `dropTime: \"time\"`.
300309
var dropTimeRegexp = regexp.MustCompile("dropTime: \"[0-9]+")
301310

302311
func replaceNonDeterministicOutput(text string) string {
303312
// scheduleIDs change based on execution time, so redact the output.
304313
nextString := scheduleIDRegexp.ReplaceAllString(text, "scheduleId: <redacted>")
314+
nextString = scheduleIDJSONRegexp.ReplaceAllString(nextString, `"ScheduleID":<redacted>`)
315+
nextString = scheduleIDLogRegexp.ReplaceAllString(nextString, "${1}#<redacted>")
305316
return dropTimeRegexp.ReplaceAllString(nextString, "dropTime: <redacted>")
306317
}
307318

0 commit comments

Comments
 (0)