-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
region_test.go
397 lines (345 loc) · 11.5 KB
/
region_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
// Copyright 2021 The Cockroach Authors.
//
// Licensed as a CockroachDB Enterprise file under the Cockroach Community
// License (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt
package multiregionccl_test
import (
"testing"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/ccl/multiregionccl/multiregionccltestutils"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/sqltestutils"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/testutils/skip"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/errors"
"github.com/stretchr/testify/require"
)
// TestConcurrentAddDropRegions tests all combinations of add/drop as if they
// were executed by two concurrent sessions. The general sketch of the test is
// as follows:
// - First operation is executed and blocks before the enum members are promoted.
// - The second operation starts once the first operation has reached the type
// schema changer. It continues to completion.
// - The first operation is resumed and allowed to complete.
// - Verify database regions are as expected.
// Operations act on a multi-region database that contains a REGIONAL BY ROW
// table, so as to exercise the repartitioning semantics.
func TestConcurrentAddDropRegions(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
skip.UnderRace(t, "times out under race")
testCases := []struct {
name string
firstOp string
secondOp string
expectedRegions []string
}{
{
"concurrent-drops",
`ALTER DATABASE db DROP REGION "us-east2"`,
`ALTER DATABASE db DROP REGION "us-east3"`,
[]string{"us-east1"},
},
{
"concurrent-adds",
`ALTER DATABASE db ADD REGION "us-east4"`,
`ALTER DATABASE db ADD REGION "us-east5"`,
[]string{"us-east1", "us-east2", "us-east3", "us-east4", "us-east5"},
},
{
"concurrent-add-drop",
`ALTER DATABASE db ADD REGION "us-east4"`,
`ALTER DATABASE db DROP REGION "us-east3"`,
[]string{"us-east1", "us-east2", "us-east4"},
},
{
"concurrent-drop-add",
`ALTER DATABASE db DROP REGION "us-east2"`,
`ALTER DATABASE db ADD REGION "us-east5"`,
[]string{"us-east1", "us-east3", "us-east5"},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
firstOp := true
firstOpStarted := make(chan struct{})
secondOpFinished := make(chan struct{})
firstOpFinished := make(chan struct{})
var mu syncutil.Mutex
knobs := base.TestingKnobs{
SQLTypeSchemaChanger: &sql.TypeSchemaChangerTestingKnobs{
RunBeforeEnumMemberPromotion: func() {
mu.Lock()
if firstOp {
firstOp = false
close(firstOpStarted)
mu.Unlock()
// Don't promote any members before the second operation reaches
// the schema changer as well.
<-secondOpFinished
} else {
mu.Unlock()
}
},
},
}
_, sqlDB, cleanup := multiregionccltestutils.TestingCreateMultiRegionCluster(
t, 5 /* numServers */, knobs, nil, /* baseDir */
)
defer cleanup()
// Create a multi-region database with a REGIONAL BY ROW table inside of it
// which needs to be re-partitioned on add/drop operations.
_, err := sqlDB.Exec(`
CREATE DATABASE db WITH PRIMARY REGION "us-east1" REGIONS "us-east2", "us-east3";
CREATE TABLE db.rbr () LOCALITY REGIONAL BY ROW`)
require.NoError(t, err)
go func() {
if _, err := sqlDB.Exec(tc.firstOp); err != nil {
t.Error(err)
}
close(firstOpFinished)
}()
// Wait for the first operation to reach the type schema changer.
<-firstOpStarted
// Start with the second operation.
_, err = sqlDB.Exec(tc.secondOp)
require.NoError(t, err)
close(secondOpFinished)
<-firstOpFinished
rows, err := sqlDB.Query("SELECT count(*) FROM [SHOW REGIONS FROM DATABASE db]")
require.NoError(t, err)
rows.Next()
var numRegions int
err = rows.Scan(&numRegions)
require.NoError(t, err)
if numRegions != len(tc.expectedRegions) {
t.Fatalf("unexpected number of regions, expected: %d found %d",
len(tc.expectedRegions),
numRegions,
)
}
rows, err = sqlDB.Query("SELECT region FROM [SHOW REGIONS FROM DATABASE db]")
require.NoError(t, err)
for _, expectedRegion := range tc.expectedRegions {
rows.Next()
var region string
err := rows.Scan(®ion)
require.NoError(t, err)
if expectedRegion != region {
t.Fatalf("unexpected region, expected: %q found %q", expectedRegion, region)
}
}
})
}
}
func TestSettingPrimaryRegionAmidstDrop(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
var mu syncutil.Mutex
dropRegionStarted := make(chan struct{})
waitForPrimaryRegionSwitch := make(chan struct{})
dropRegionFinished := make(chan struct{})
knobs := base.TestingKnobs{
SQLTypeSchemaChanger: &sql.TypeSchemaChangerTestingKnobs{
RunBeforeEnumMemberPromotion: func() {
mu.Lock()
defer mu.Unlock()
if dropRegionStarted != nil {
close(dropRegionStarted)
<-waitForPrimaryRegionSwitch
dropRegionStarted = nil
}
},
},
}
_, sqlDB, cleanup := multiregionccltestutils.TestingCreateMultiRegionCluster(
t, 2 /* numServers */, knobs, nil, /* baseDir */
)
defer cleanup()
// Setup the test.
_, err := sqlDB.Exec(`CREATE DATABASE db WITH PRIMARY REGION "us-east1" REGIONS "us-east2"`)
if err != nil {
t.Fatal(err)
}
go func() {
if _, err := sqlDB.Exec(`ALTER DATABASE db DROP REGION "us-east2"`); err != nil {
t.Error(err)
}
close(dropRegionFinished)
}()
// Wait for the drop region to start and move the enum member "us-east2" in
// read-only state.
<-dropRegionStarted
_, err = sqlDB.Exec(`
ALTER DATABASE db PRIMARY REGION "us-east2";
`)
if err == nil {
t.Fatalf("expected error, found nil")
}
if !testutils.IsError(err, `"us-east2" has not been added to the database`) {
t.Fatalf(`expected err, got %v`, err)
}
close(waitForPrimaryRegionSwitch)
<-dropRegionFinished
// We expect the async region drop job to succeed soon.
testutils.SucceedsSoon(t, func() error {
rows, err := sqlDB.Query("SELECT region FROM [SHOW REGIONS FROM DATABASE db]")
if err != nil {
return err
}
defer rows.Close()
const expectedRegion = "us-east1"
var region string
rows.Next()
if err := rows.Scan(®ion); err != nil {
return err
}
if region != expectedRegion {
return errors.Newf("expected region to be: %q, got %q", expectedRegion, region)
}
if rows.Next() {
return errors.New("unexpected number of rows returned")
}
return nil
})
}
// TestDroppingPrimaryRegionAsyncJobFailure drops the primary region of the
// database, which results in dropping the multi-region type descriptor. Then,
// it errors out the async job associated with the type descriptor cleanup and
// ensures the namespace entry is reclaimed back despite the injected error.
// We rely on this behavior to be able to add multi-region capability in the
// future.
func TestDroppingPrimaryRegionAsyncJobFailure(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
// Decrease the adopt loop interval so that retries happen quickly.
defer sqltestutils.SetTestJobsAdoptInterval()()
// Protects expectedCleanupRuns
var mu syncutil.Mutex
// We need to cleanup 2 times, once for the multi-region type descriptor and
// once for the array alias of the multi-region type descriptor.
haveWePerformedFirstRoundOfCleanup := false
cleanupFinished := make(chan struct{})
knobs := base.TestingKnobs{
SQLTypeSchemaChanger: &sql.TypeSchemaChangerTestingKnobs{
RunBeforeExec: func() error {
return errors.New("yikes")
},
RunAfterOnFailOrCancel: func() error {
mu.Lock()
defer mu.Unlock()
if haveWePerformedFirstRoundOfCleanup {
close(cleanupFinished)
}
haveWePerformedFirstRoundOfCleanup = true
return nil
},
},
}
_, sqlDB, cleanup := multiregionccltestutils.TestingCreateMultiRegionCluster(
t, 1 /* numServers */, knobs, nil, /* baseDir */
)
defer cleanup()
// Setup the test.
_, err := sqlDB.Exec(`
CREATE DATABASE db WITH PRIMARY REGION "us-east1";
CREATE TABLE db.t(k INT) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION;
`)
require.NoError(t, err)
_, err = sqlDB.Exec(`ALTER DATABASE db DROP REGION "us-east1"`)
testutils.IsError(err, "yikes")
<-cleanupFinished
rows := sqlDB.QueryRow(`SELECT count(*) FROM system.namespace WHERE name = 'crdb_internal_region'`)
var count int
err = rows.Scan(&count)
require.NoError(t, err)
if count != 0 {
t.Fatal("expected crdb_internal_region not to be present in system.namespace")
}
_, err = sqlDB.Exec(`ALTER DATABASE db PRIMARY REGION "us-east1"`)
require.NoError(t, err)
rows = sqlDB.QueryRow(`SELECT count(*) FROM system.namespace WHERE name = 'crdb_internal_region'`)
err = rows.Scan(&count)
require.NoError(t, err)
if count != 1 {
t.Fatal("expected crdb_internal_region to be present in system.namespace")
}
}
// TestRollbackDuringAddDropRegionAsyncJobFailure ensures that rollback when
// an ADD REGION/DROP REGION fails asynchronously is handled appropriately.
// This also ensures that zone configuration changes on the database are
// transactional.
func TestRollbackDuringAddDropRegionAsyncJobFailure(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
skip.UnderRace(t, "times out under race")
// Decrease the adopt loop interval so that retries happen quickly.
defer sqltestutils.SetTestJobsAdoptInterval()()
knobs := base.TestingKnobs{
SQLTypeSchemaChanger: &sql.TypeSchemaChangerTestingKnobs{
RunBeforeMultiRegionUpdates: func() error {
return errors.New("boom")
},
},
}
// Setup.
_, sqlDB, cleanup := multiregionccltestutils.TestingCreateMultiRegionCluster(
t, 3 /* numServers */, knobs, nil, /* baseDir */
)
defer cleanup()
_, err := sqlDB.Exec(`CREATE DATABASE db WITH PRIMARY REGION "us-east1" REGIONS "us-east2"`)
require.NoError(t, err)
testCases := []struct {
name string
query string
}{
{
"add-region",
`ALTER DATABASE db ADD REGION "us-east3"`,
},
{
"drop-region",
`ALTER DATABASE db DROP REGION "us-east2"`,
},
{
"add-drop-region-in-txn",
`BEGIN;
ALTER DATABASE db DROP REGION "us-east2";
ALTER DATABASE db ADD REGION "us-east3";
COMMIT`,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var originalZoneConfig string
res := sqlDB.QueryRow(`SELECT raw_config_sql FROM [SHOW ZONE CONFIGURATION FOR DATABASE db]`)
err = res.Scan(&originalZoneConfig)
require.NoError(t, err)
_, err = sqlDB.Exec(tc.query)
testutils.IsError(err, "boom")
var jobStatus string
var jobErr string
row := sqlDB.QueryRow("SELECT status, error FROM [SHOW JOBS] WHERE job_type = 'TYPEDESC SCHEMA CHANGE'")
require.NoError(t, row.Scan(&jobStatus, &jobErr))
require.Contains(t, "boom", jobErr)
require.Contains(t, "failed", jobStatus)
// Ensure the zone configuration didn't change.
var newZoneConfig string
res = sqlDB.QueryRow(`SELECT raw_config_sql FROM [SHOW ZONE CONFIGURATION FOR DATABASE db]`)
err = res.Scan(&newZoneConfig)
require.NoError(t, err)
if newZoneConfig != originalZoneConfig {
t.Fatalf("expected zone config to not have changed, expected %q found %q",
originalZoneConfig,
newZoneConfig,
)
}
})
}
}