Skip to content

Commit 31a5161

Browse files
craig[bot]kev-cao
andcommitted
Merge #147239
147239: roachtest: refactor backup restore test driver r=msbutler a=kev-cao This commit refactors the `BackupRestoreTestDriver` functions and splits up code into several more helper function to improve readability and composition. Many functions that were originally added as receiver functions to `backupCollection` have been moved under the driver more accordingly. Epic: CRDB-37550 Release note: None Co-authored-by: Kevin Cao <[email protected]>
2 parents 18194f2 + 81688c9 commit 31a5161

File tree

2 files changed

+256
-141
lines changed

2 files changed

+256
-141
lines changed

pkg/cmd/roachtest/tests/backup_restore_roundtrip.go

Lines changed: 73 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -114,40 +114,23 @@ func backupRestoreRoundTrip(
114114
m := c.NewMonitor(ctx, c.CRDBNodes())
115115

116116
m.Go(func(ctx context.Context) error {
117-
connectFunc := func(node int) (*gosql.DB, error) {
118-
conn, err := c.ConnE(ctx, t.L(), node)
119-
if err != nil {
120-
return nil, fmt.Errorf("failed to connect to node %d: %w", node, err)
121-
}
122-
123-
return conn, err
124-
}
125-
// TODO (msbutler): enable compaction for online restore test once inc layer limit is increased.
126-
testUtils, err := newCommonTestUtils(ctx, t, c, connectFunc, c.CRDBNodes(), withMock(sp.mock), withOnlineRestore(sp.onlineRestore), withCompaction(!sp.onlineRestore))
117+
testUtils, err := setupBackupRestoreTestUtils(
118+
ctx, t, c, m, testRNG,
119+
withMock(sp.mock), withOnlineRestore(sp.onlineRestore), withCompaction(!sp.onlineRestore),
120+
)
127121
if err != nil {
128122
return err
129123
}
130124
defer testUtils.CloseConnections()
131125

132126
dbs := []string{"bank", "tpcc", schemaChangeDB}
133-
runBackgroundWorkload, err := startBackgroundWorkloads(ctx, t.L(), c, m, testRNG, c.CRDBNodes(), c.WorkloadNode(), testUtils, dbs)
134-
if err != nil {
135-
return err
136-
}
137-
tables, err := testUtils.loadTablesForDBs(ctx, t.L(), testRNG, dbs...)
138-
if err != nil {
139-
return err
140-
}
141-
d, err := newBackupRestoreTestDriver(ctx, t, c, testUtils, c.CRDBNodes(), dbs, tables)
127+
d, runBackgroundWorkload, _, err := createDriversForBackupRestore(
128+
ctx, t, c, m, testRNG, testUtils, dbs,
129+
)
142130
if err != nil {
143131
return err
144132
}
145-
if err := testUtils.setShortJobIntervals(ctx, testRNG); err != nil {
146-
return err
147-
}
148-
if err := testUtils.setClusterSettings(ctx, t.L(), c, testRNG); err != nil {
149-
return err
150-
}
133+
151134
if sp.metamorphicRangeSize {
152135
if err := testUtils.setMaxRangeSizeAndDependentSettings(ctx, t, testRNG, dbs); err != nil {
153136
return err
@@ -199,7 +182,7 @@ func backupRestoreRoundTrip(
199182

200183
t.L().Printf("verifying backup %d", i+1)
201184
// Verify content in backups.
202-
err = collection.verifyBackupCollection(ctx, t.L(), testRNG, d, true /* checkFiles */, true /* internalSystemJobs */)
185+
err = d.verifyBackupCollection(ctx, t.L(), testRNG, collection, true /* checkFiles */, true /* internalSystemJobs */)
203186
if err != nil {
204187
return err
205188
}
@@ -220,8 +203,8 @@ func backupRestoreRoundTrip(
220203
m.Wait()
221204
}
222205

223-
// startBackgroundWorkloads starts a TPCC, bank, and a system table workload in
224-
// the background.
206+
// startBackgroundWorkloads returns a function that starts a TPCC, bank, and a
207+
// system table workload in the background.
225208
func startBackgroundWorkloads(
226209
ctx context.Context,
227210
l *logger.Logger,
@@ -361,7 +344,6 @@ func (u *CommonTestUtils) CloseConnections() {
361344
}
362345

363346
func workloadWithCancel(m cluster.Monitor, fn func(ctx context.Context) error) func() {
364-
365347
cancelWorkload := m.GoWithCancel(func(ctx context.Context) error {
366348
err := fn(ctx)
367349
if ctx.Err() != nil {
@@ -372,3 +354,65 @@ func workloadWithCancel(m cluster.Monitor, fn func(ctx context.Context) error) f
372354
})
373355
return cancelWorkload
374356
}
357+
358+
// setupBackupRestoreTestUtils sets up a CommonTestUtils instance for backup and
359+
// restore tests and initializes some useful settings.
360+
func setupBackupRestoreTestUtils(
361+
ctx context.Context,
362+
t test.Test,
363+
c cluster.Cluster,
364+
m cluster.Monitor,
365+
rng *rand.Rand,
366+
testOpts ...commonTestOption,
367+
) (*CommonTestUtils, error) {
368+
connectFunc := func(node int) (*gosql.DB, error) {
369+
conn, err := c.ConnE(ctx, t.L(), node)
370+
if err != nil {
371+
return nil, fmt.Errorf("failed to connect to node %d: %w", node, err)
372+
}
373+
374+
return conn, err
375+
}
376+
// TODO (msbutler): enable compaction for online restore test once inc layer limit is increased.
377+
testUtils, err := newCommonTestUtils(ctx, t, c, connectFunc, c.CRDBNodes(), testOpts...)
378+
if err != nil {
379+
return nil, err
380+
}
381+
if err := testUtils.setShortJobIntervals(ctx, rng); err != nil {
382+
return nil, err
383+
}
384+
if err := testUtils.setClusterSettings(ctx, t.L(), c, rng); err != nil {
385+
return nil, err
386+
}
387+
return testUtils, err
388+
}
389+
390+
// createDriversForBackupRestore creates a BackupRestoreTestDriver for backup
391+
// and restore tests, a handler to trigger background workloads, and the tables
392+
// that are used in the test. The tables are mapped to the databases that were
393+
// passed in.
394+
func createDriversForBackupRestore(
395+
ctx context.Context,
396+
t test.Test,
397+
c cluster.Cluster,
398+
m cluster.Monitor,
399+
rng *rand.Rand,
400+
testUtils *CommonTestUtils,
401+
dbs []string,
402+
) (*BackupRestoreTestDriver, func() (func(), error), [][]string, error) {
403+
runBackgroundWorkload, err := startBackgroundWorkloads(
404+
ctx, t.L(), c, m, rng, c.CRDBNodes(), c.WorkloadNode(), testUtils, dbs,
405+
)
406+
if err != nil {
407+
return nil, nil, nil, err
408+
}
409+
tables, err := testUtils.loadTablesForDBs(ctx, t.L(), rng, dbs...)
410+
if err != nil {
411+
return nil, nil, nil, err
412+
}
413+
d, err := newBackupRestoreTestDriver(ctx, t, c, testUtils, c.CRDBNodes(), dbs, tables)
414+
if err != nil {
415+
return nil, nil, nil, err
416+
}
417+
return d, runBackgroundWorkload, tables, nil
418+
}

0 commit comments

Comments
 (0)