Skip to content

Commit c47ef3a

Browse files
committed
sql: deflake TestDropDatabaseDeleteData
This commit deflakes the test by blocking the full reconciliation from starting before we drop the database. This is used to avoid a race condition where the full reconciliation starts after we drop the database, it will ignore the dropped database. Then, there is a race between the SQLWatcher and the GC job where the SQLWatcher might write a zone config of table1 before table2, while the GC queue might not know that this range needs to be split because it has different span config. Release note: None Fixes: #138185
1 parent 5a18d88 commit c47ef3a

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

pkg/spanconfig/spanconfigreconciler/reconciler.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ type fullReconciler struct {
217217
func (f *fullReconciler) reconcile(
218218
ctx context.Context,
219219
) (storeWithLatestSpanConfigs *spanconfigstore.Store, _ hlc.Timestamp, _ error) {
220+
if f.knobs != nil && f.knobs.OnFullReconcilerStart != nil {
221+
f.knobs.OnFullReconcilerStart()
222+
}
223+
220224
storeWithExistingSpanConfigs, err := f.fetchExistingSpanConfigs(ctx)
221225
if err != nil {
222226
return nil, hlc.Timestamp{}, err

pkg/spanconfig/testing_knobs.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ type TestingKnobs struct {
127127
// fallback config that will be applied to the span.
128128
OverrideFallbackConf func(roachpb.SpanConfig) roachpb.SpanConfig
129129

130+
// OnFullReconcilerStart is invoked when full reconciliation starts.
131+
OnFullReconcilerStart func()
132+
130133
// OnWatchForZoneConfigUpdatesEstablished is invoked when the RangeFeed over
131134
// system.zones starts.
132135
OnWatchForZoneConfigUpdatesEstablished func()

pkg/sql/drop_test.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -267,17 +267,26 @@ func TestDropDatabaseDeleteData(t *testing.T) {
267267
// Speed up mvcc queue scan.
268268
params.ScanMaxIdleTime = time.Millisecond
269269

270+
// fullReconcilierStarted is used to block the full reconciliation from
271+
// starting before we drop the database. This is used to avoid a race
272+
// condition where the full reconciliation starts after we drop the database,
273+
// it will ignore the dropped database. Then, there is a race between the
274+
// SQLWatcher and the GC job where the SQLWatcher might write a zone config
275+
// of table1 before table2, while the GC queue might not know that this range
276+
// needs to be split because it has different span config.
277+
//
270278
// zoneCfgRangeFeedStarted is used to signal that the zone config range feed
271-
// started. This is used to avoid a race where we update `system.zones` before
272-
// the full reconciliation of zone configs has started. The full
273-
// reconciliation ignores dropped databases, and if we write to
274-
// `system.zones` before that, our write will not be reconciled. By delaying
275-
// the test until the zone config range feed starts, we ensure that the full
276-
// reconciliation has finished, and the range feed is ready to stream our
277-
// changes.
278-
var zoneCfgRangeFeedStarted sync.WaitGroup
279+
// started and that the full reconciliation has finished. This is used to
280+
// avoid a race where we update `system.zones` before the full reconciliation
281+
// of zone configs has finished. If we write to `system.zones` before that,
282+
// our write will not be reconciled.
283+
var fullReconcilierStarted, zoneCfgRangeFeedStarted sync.WaitGroup
284+
fullReconcilierStarted.Add(1)
279285
zoneCfgRangeFeedStarted.Add(1)
280286
params.Knobs.SpanConfig = &spanconfig.TestingKnobs{
287+
OnFullReconcilerStart: func() {
288+
fullReconcilierStarted.Wait()
289+
},
281290
OnWatchForZoneConfigUpdatesEstablished: func() {
282291
zoneCfgRangeFeedStarted.Done()
283292
},
@@ -306,9 +315,6 @@ func TestDropDatabaseDeleteData(t *testing.T) {
306315
_, err = systemDB.Exec(`SET CLUSTER SETTING spanconfig.tenant_coalesce_adjacent.enabled = 'false';`)
307316
require.NoError(t, err)
308317

309-
// Wait for the zone config range feed to start.
310-
zoneCfgRangeFeedStarted.Wait()
311-
312318
// Disable strict GC TTL enforcement because we're going to shove a zero-value
313319
// TTL into the system with AddImmediateGCZoneConfig.
314320
defer sqltestutils.DisableGCTTLStrictEnforcement(t, systemDB)()
@@ -347,6 +353,12 @@ INSERT INTO t.kv2 VALUES ('c', 'd'), ('a', 'b'), ('e', 'a');
347353
t.Fatal(err)
348354
}
349355

356+
// Unblock the full reconciliation.
357+
fullReconcilierStarted.Done()
358+
359+
// Wait for the zone config range feed to start.
360+
zoneCfgRangeFeedStarted.Wait()
361+
350362
if _, err := sqlDB.Exec(`DROP DATABASE t CASCADE`); err != nil {
351363
t.Fatal(err)
352364
}

0 commit comments

Comments
 (0)