-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathschemachanger_test.go
516 lines (459 loc) · 16.8 KB
/
schemachanger_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
// Copyright 2021 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package schemachanger_test
import (
"context"
gosql "database/sql"
"fmt"
"sync"
"testing"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/jobs"
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkv"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scexec"
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scop"
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scplan"
"github.com/cockroachdb/cockroach/pkg/sql/tests"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
"github.com/cockroachdb/cockroach/pkg/util/ctxgroup"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSchemaChangeWaitsForOtherSchemaChanges(t *testing.T) {
defer leaktest.AfterTest(t)()
t.Run("wait for old-style schema changes", func(t *testing.T) {
// This test starts an old-style schema change job (job 1), and then starts
// another old-style schema change job (job 2) and a new-style schema change
// job (job 3) while job 1 is backfilling. Job 1 is resumed after job 2
// has started running.
ctx := context.Background()
var job1Backfill sync.Once
var job2Resume sync.Once
var job3Wait sync.Once
// Closed when we enter the RunBeforeBackfill knob of job 1.
job1BackfillNotification := make(chan struct{})
// Closed when we're ready to continue with job 1.
job1ContinueNotification := make(chan struct{})
// Closed when job 2 starts.
job2ResumeNotification := make(chan struct{})
// Closed when job 3 starts waiting for concurrent schema changes to finish.
job3WaitNotification := make(chan struct{})
var job1ID jobspb.JobID
var s serverutils.TestServerInterface
var kvDB *kv.DB
params, _ := tests.CreateTestServerParams()
params.Knobs = base.TestingKnobs{
SQLSchemaChanger: &sql.SchemaChangerTestingKnobs{
RunBeforeResume: func(jobID jobspb.JobID) error {
// Only block in job 2.
if job1ID == 0 || jobID == job1ID {
job1ID = jobID
return nil
}
job2Resume.Do(func() {
close(job2ResumeNotification)
})
return nil
},
RunBeforeBackfill: func() error {
job1Backfill.Do(func() {
close(job1BackfillNotification)
<-job1ContinueNotification
})
return nil
},
},
SQLNewSchemaChanger: &scexec.NewSchemaChangerTestingKnobs{
BeforeStage: func(_ scop.Ops, m scexec.TestingKnobMetadata) error {
// Assert that when job 3 is running, there are no mutations other
// than the ones associated with this schema change.
if m.Phase != scplan.PostCommitPhase {
return nil
}
table := catalogkv.TestingGetTableDescriptorFromSchema(
kvDB, keys.SystemSQLCodec, "db", "public", "t")
// There are 2 schema changes that should precede job 3. Note that the
// new-style schema change itself uses multiple mutation IDs.
for _, m := range table.AllMutations() {
assert.Greater(t, int(m.MutationID()), 2)
}
return nil
},
BeforeWaitingForConcurrentSchemaChanges: func(_ []string) {
job3Wait.Do(func() {
close(job3WaitNotification)
})
},
},
JobsTestingKnobs: jobs.NewTestingKnobsWithShortIntervals(),
}
var sqlDB *gosql.DB
s, sqlDB, kvDB = serverutils.StartServer(t, params)
defer s.Stopper().Stop(ctx)
tdb := sqlutils.MakeSQLRunner(sqlDB)
tdb.Exec(t, `CREATE DATABASE db`)
tdb.Exec(t, `CREATE TABLE db.t (a INT PRIMARY KEY)`)
g := ctxgroup.WithContext(ctx)
// Start job 1: An index schema change, which does not use the new schema
// changer.
g.GoCtx(func(ctx context.Context) error {
_, err := sqlDB.ExecContext(ctx, `CREATE INDEX idx ON db.t(a)`)
assert.NoError(t, err)
return nil
})
<-job1BackfillNotification
// Start job 3: A column schema change which uses the new schema changer.
// The transaction will not actually commit until job 1 has finished.
g.GoCtx(func(ctx context.Context) error {
conn, err := sqlDB.Conn(ctx)
if err != nil {
return err
}
_, err = conn.ExecContext(ctx, `SET experimental_use_new_schema_changer = 'on'`)
assert.NoError(t, err)
_, err = conn.ExecContext(ctx, `ALTER TABLE db.t ADD COLUMN b INT DEFAULT 1`)
assert.NoError(t, err)
return nil
})
<-job3WaitNotification
// Start job 2: Another index schema change which does not use the new
// schema changer.
g.GoCtx(func(ctx context.Context) error {
_, err := sqlDB.ExecContext(ctx, `CREATE INDEX idx2 ON db.t(a)`)
assert.NoError(t, err)
return nil
})
// Wait for job 2 to start.
<-job2ResumeNotification
// Finally, let job 1 finish, which will unblock the
// others.
close(job1ContinueNotification)
require.NoError(t, g.Wait())
// Check that job 3 was created last.
tdb.CheckQueryResults(t,
fmt.Sprintf(`SELECT job_type, status, description FROM crdb_internal.jobs WHERE job_type = '%s' OR job_type = '%s' ORDER BY created`,
jobspb.TypeSchemaChange.String(), jobspb.TypeNewSchemaChange.String(),
),
[][]string{
{jobspb.TypeSchemaChange.String(), string(jobs.StatusSucceeded), `CREATE INDEX idx ON db.public.t (a)`},
{jobspb.TypeSchemaChange.String(), string(jobs.StatusSucceeded), `CREATE INDEX idx2 ON db.public.t (a)`},
{jobspb.TypeNewSchemaChange.String(), string(jobs.StatusSucceeded), `Schema change job`},
},
)
})
t.Run("wait for new-style schema changes", func(t *testing.T) {
// This test starts a new-style schema change job (job 1), and then starts
// another new-style schema change job (job 2) while job 1 is backfilling.
ctx := context.Background()
var job1Backfill sync.Once
var job2Wait sync.Once
// Closed when we enter the RunBeforeBackfill knob of job 1.
job1BackfillNotification := make(chan struct{})
// Closed when we're ready to continue with job 1.
job1ContinueNotification := make(chan struct{})
// Closed when job 2 starts waiting for concurrent schema changes to finish.
job2WaitNotification := make(chan struct{})
stmt1 := `ALTER TABLE db.t ADD COLUMN b INT DEFAULT 1`
stmt2 := `ALTER TABLE db.t ADD COLUMN c INT DEFAULT 2`
var kvDB *kv.DB
params, _ := tests.CreateTestServerParams()
params.Knobs = base.TestingKnobs{
SQLNewSchemaChanger: &scexec.NewSchemaChangerTestingKnobs{
BeforeStage: func(ops scop.Ops, m scexec.TestingKnobMetadata) error {
// Verify that we never queue mutations for job 2 before finishing job
// 1.
if m.Phase != scplan.PostCommitPhase {
return nil
}
table := catalogkv.TestingGetTableDescriptorFromSchema(
kvDB, keys.SystemSQLCodec, "db", "public", "t")
mutations := table.AllMutations()
if len(mutations) == 0 {
t.Errorf("unexpected empty mutations")
return errors.Errorf("test failure")
}
var idsSeen []descpb.MutationID
for _, m := range mutations {
if len(idsSeen) == 0 || m.MutationID() > idsSeen[len(idsSeen)-1] {
idsSeen = append(idsSeen, m.MutationID())
}
}
// Each schema change involving an index backfill has 2 consecutive
// mutation IDs, so the first schema change is expected to have
// mutation IDs 1 and 2, and the second one 3 and 4.
lowestID, highestID := idsSeen[0], idsSeen[len(idsSeen)-1]
if m.Statements[0] == stmt1 {
assert.Truef(t, highestID <= 2, "unexpected mutation IDs %v", idsSeen)
} else if m.Statements[0] == stmt2 {
assert.Truef(t, lowestID >= 3 && highestID <= 4, "unexpected mutation IDs %v", idsSeen)
} else {
t.Errorf("unexpected statements %v", m.Statements)
return errors.Errorf("test failure")
}
// Block job 1 during the backfill.
if m.Statements[0] != stmt1 || ops.Type() != scop.BackfillType {
return nil
}
for _, op := range ops.Slice() {
if backfillOp, ok := op.(scop.BackfillIndex); ok && backfillOp.IndexID == descpb.IndexID(2) {
job1Backfill.Do(func() {
close(job1BackfillNotification)
<-job1ContinueNotification
})
}
}
return nil
},
BeforeWaitingForConcurrentSchemaChanges: func(stmts []string) {
if stmts[0] != stmt2 {
return
}
job2Wait.Do(func() {
close(job2WaitNotification)
})
},
},
}
var s serverutils.TestServerInterface
var sqlDB *gosql.DB
s, sqlDB, kvDB = serverutils.StartServer(t, params)
defer s.Stopper().Stop(ctx)
tdb := sqlutils.MakeSQLRunner(sqlDB)
tdb.Exec(t, `CREATE DATABASE db`)
tdb.Exec(t, `CREATE TABLE db.t (a INT PRIMARY KEY)`)
g := ctxgroup.WithContext(ctx)
g.GoCtx(func(ctx context.Context) error {
conn, err := sqlDB.Conn(ctx)
if err != nil {
return err
}
_, err = conn.ExecContext(ctx, `SET experimental_use_new_schema_changer = 'on'`)
assert.NoError(t, err)
_, err = conn.ExecContext(ctx, stmt1)
assert.NoError(t, err)
return nil
})
<-job1BackfillNotification
g.GoCtx(func(ctx context.Context) error {
conn, err := sqlDB.Conn(ctx)
if err != nil {
return err
}
_, err = conn.ExecContext(ctx, `SET experimental_use_new_schema_changer = 'on'`)
assert.NoError(t, err)
_, err = conn.ExecContext(ctx, stmt2)
assert.NoError(t, err)
return nil
})
<-job2WaitNotification
close(job1ContinueNotification)
require.NoError(t, g.Wait())
tdb.CheckQueryResults(t,
fmt.Sprintf(`SELECT job_type, status FROM crdb_internal.jobs WHERE job_type = '%s' OR job_type = '%s' ORDER BY created`,
jobspb.TypeSchemaChange.String(), jobspb.TypeNewSchemaChange.String(),
),
[][]string{
{jobspb.TypeNewSchemaChange.String(), string(jobs.StatusSucceeded)},
{jobspb.TypeNewSchemaChange.String(), string(jobs.StatusSucceeded)},
},
)
})
}
func TestConcurrentOldSchemaChangesCannotStart(t *testing.T) {
defer leaktest.AfterTest(t)()
ctx := context.Background()
var doOnce sync.Once
// Closed when we enter the RunBeforeBackfill knob.
beforeBackfillNotification := make(chan struct{})
// Closed when we're ready to continue with the schema change.
continueNotification := make(chan struct{})
var kvDB *kv.DB
params, _ := tests.CreateTestServerParams()
params.Knobs = base.TestingKnobs{
SQLSchemaChanger: &sql.SchemaChangerTestingKnobs{
RunBeforeResume: func(jobID jobspb.JobID) error {
// Assert that old schema change jobs never run in this test.
t.Errorf("unexpected old schema change job %d", jobID)
return nil
},
},
SQLNewSchemaChanger: &scexec.NewSchemaChangerTestingKnobs{
BeforeStage: func(ops scop.Ops, m scexec.TestingKnobMetadata) error {
// Verify that we never get a mutation ID not associated with the schema
// change that is running.
if m.Phase != scplan.PostCommitPhase {
return nil
}
table := catalogkv.TestingGetTableDescriptorFromSchema(
kvDB, keys.SystemSQLCodec, "db", "public", "t")
for _, m := range table.AllMutations() {
assert.LessOrEqual(t, int(m.MutationID()), 2)
}
if ops.Type() != scop.BackfillType {
return nil
}
for _, op := range ops.Slice() {
if _, ok := op.(scop.BackfillIndex); ok {
doOnce.Do(func() {
close(beforeBackfillNotification)
<-continueNotification
})
}
}
return nil
},
},
JobsTestingKnobs: jobs.NewTestingKnobsWithShortIntervals(),
}
var s serverutils.TestServerInterface
var sqlDB *gosql.DB
s, sqlDB, kvDB = serverutils.StartServer(t, params)
defer s.Stopper().Stop(ctx)
tdb := sqlutils.MakeSQLRunner(sqlDB)
tdb.Exec(t, `CREATE DATABASE db`)
tdb.Exec(t, `CREATE TABLE db.t (a INT PRIMARY KEY)`)
g := ctxgroup.WithContext(ctx)
g.GoCtx(func(ctx context.Context) error {
conn, err := sqlDB.Conn(ctx)
if err != nil {
return err
}
_, err = conn.ExecContext(ctx, `SET experimental_use_new_schema_changer = 'on'`)
assert.NoError(t, err)
_, err = conn.ExecContext(ctx, `ALTER TABLE db.t ADD COLUMN b INT DEFAULT 1`)
assert.NoError(t, err)
return nil
})
<-beforeBackfillNotification
{
conn, err := sqlDB.Conn(ctx)
require.NoError(t, err)
_, err = conn.ExecContext(ctx, `SET experimental_use_new_schema_changer = 'off'`)
require.NoError(t, err)
for _, stmt := range []string{
`ALTER TABLE db.t ADD COLUMN c INT DEFAULT 2`,
`CREATE INDEX ON db.t(a)`,
`ALTER TABLE db.t RENAME COLUMN a TO c`,
`CREATE TABLE db.t2 (i INT PRIMARY KEY, a INT REFERENCES db.t)`,
`CREATE VIEW db.v AS SELECT a FROM db.t`,
`ALTER TABLE db.t RENAME TO db.new`,
`GRANT ALL ON db.t TO root`,
`TRUNCATE TABLE db.t`,
`DROP TABLE db.t`,
} {
_, err = conn.ExecContext(ctx, stmt)
assert.Truef(t,
testutils.IsError(err, `cannot perform a schema change on table "t"`),
"statement: %s, error: %s", stmt, err,
)
}
}
close(continueNotification)
require.NoError(t, g.Wait())
}
func TestInsertDuringAddColumnNotWritingToCurrentPrimaryIndex(t *testing.T) {
defer leaktest.AfterTest(t)()
defer sqltestutils.SetTestJobsAdoptInterval()()
ctx := context.Background()
var doOnce sync.Once
// Closed when we enter the RunBeforeBackfill knob.
beforeBackfillNotification := make(chan struct{})
// Closed when we're ready to continue with the schema change.
continueNotification := make(chan struct{})
var kvDB *kv.DB
params, _ := tests.CreateTestServerParams()
params.Knobs = base.TestingKnobs{
SQLSchemaChanger: &sql.SchemaChangerTestingKnobs{
RunBeforeResume: func(jobID jobspb.JobID) error {
// Assert that old schema change jobs never run in this test.
t.Errorf("unexpected old schema change job %d", jobID)
return nil
},
},
SQLNewSchemaChanger: &scexec.NewSchemaChangerTestingKnobs{
BeforeStage: func(ops scop.Ops, m scexec.TestingKnobMetadata) error {
// Verify that we never get a mutation ID not associated with the schema
// change that is running.
if m.Phase != scplan.PostCommitPhase {
return nil
}
table := catalogkv.TestingGetTableDescriptorFromSchema(
kvDB, keys.SystemSQLCodec, "db", "public", "t")
for _, m := range table.AllMutations() {
assert.LessOrEqual(t, int(m.MutationID()), 2)
}
if ops.Type() != scop.BackfillType {
return nil
}
for _, op := range ops.Slice() {
if _, ok := op.(scop.BackfillIndex); ok {
doOnce.Do(func() {
close(beforeBackfillNotification)
<-continueNotification
})
}
}
return nil
},
},
}
var s serverutils.TestServerInterface
var sqlDB *gosql.DB
s, sqlDB, kvDB = serverutils.StartServer(t, params)
defer s.Stopper().Stop(ctx)
tdb := sqlutils.MakeSQLRunner(sqlDB)
tdb.Exec(t, `CREATE DATABASE db`)
tdb.Exec(t, `CREATE TABLE db.t (a INT PRIMARY KEY)`)
desc := catalogkv.TestingGetImmutableTableDescriptor(kvDB, keys.SystemSQLCodec, "db", "t")
g := ctxgroup.WithContext(ctx)
g.GoCtx(func(ctx context.Context) error {
conn, err := sqlDB.Conn(ctx)
if err != nil {
return err
}
_, err = conn.ExecContext(ctx, `SET experimental_use_new_schema_changer = 'on'`)
assert.NoError(t, err)
_, err = conn.ExecContext(ctx, `ALTER TABLE db.t ADD COLUMN b INT DEFAULT 100`)
assert.NoError(t, err)
return nil
})
<-beforeBackfillNotification
// At this point the backfill operation is paused as it's about to begin.
// The new column `b` is not yet public, so a concurrent insert should:
// - in the current primary index, only insert a value for `a`,
// - in the new secondary index, which will be the future primary index,
// insert a value both for `a` and the default value for `b`, because that
// new index is delete-and-write-only as it is being backfilled.
tdb.Exec(t, `
SET tracing = on,kv;
INSERT INTO db.t (a) VALUES (10);
SET tracing = off;`)
// Trigger the resumption and conclusion of the backfill,
// and hence of the ADD COLUMN transaction.
close(continueNotification)
require.NoError(t, g.Wait())
// Check that the expectations set out above are verified.
results := tdb.QueryStr(t, `
SELECT message
FROM [SHOW KV TRACE FOR SESSION]
WHERE message LIKE 'CPut %' OR message LIKE 'InitPut %'`)
require.GreaterOrEqual(t, len(results), 2)
require.Equal(t, fmt.Sprintf("CPut /Table/%d/1/10/0 -> /TUPLE/", desc.GetID()), results[0][0])
require.Equal(t, fmt.Sprintf("InitPut /Table/%d/2/10/0 -> /TUPLE/2:2:Int/100", desc.GetID()), results[1][0])
}