Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

br: create database by db pool (#50771) #51027

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions br/pkg/restore/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,13 +775,38 @@
return info.SchemaByName(dbName)
}

// CreateDatabase creates a database.
func (rc *Client) CreateDatabase(ctx context.Context, db *model.DBInfo) error {
// CreateDatabases creates databases. If the client has the db pool, it would create it.
func (rc *Client) CreateDatabases(ctx context.Context, dbs []*utils.Database) error {
if rc.IsSkipCreateSQL() {
log.Info("skip create database", zap.Stringer("name", db.Name))
log.Info("skip create database")

Check warning on line 781 in br/pkg/restore/client.go

View check run for this annotation

Codecov / codecov/patch

br/pkg/restore/client.go#L781

Added line #L781 was not covered by tests
return nil
}

if len(rc.dbPool) == 0 {
log.Info("create databases sequentially")
for _, db := range dbs {
err := rc.createDatabaseWithDBConn(ctx, db.Info, rc.db)
if err != nil {
return errors.Trace(err)
}

Check warning on line 791 in br/pkg/restore/client.go

View check run for this annotation

Codecov / codecov/patch

br/pkg/restore/client.go#L786-L791

Added lines #L786 - L791 were not covered by tests
}
return nil

Check warning on line 793 in br/pkg/restore/client.go

View check run for this annotation

Codecov / codecov/patch

br/pkg/restore/client.go#L793

Added line #L793 was not covered by tests
}

log.Info("create databases in db pool", zap.Int("pool size", len(rc.dbPool)))
eg, ectx := errgroup.WithContext(ctx)
workers := utils.NewWorkerPool(uint(len(rc.dbPool)), "DB DDL workers")
for _, db_ := range dbs {
db := db_
workers.ApplyWithIDInErrorGroup(eg, func(id uint64) error {
conn := rc.dbPool[id%uint64(len(rc.dbPool))]
return rc.createDatabaseWithDBConn(ectx, db.Info, conn)
})
}
return eg.Wait()
}

func (rc *Client) createDatabaseWithDBConn(ctx context.Context, db *model.DBInfo, conn *DB) error {
log.Info("create database", zap.Stringer("name", db.Name))

if !rc.supportPolicy {
Expand All @@ -791,12 +816,12 @@
}

if db.PlacementPolicyRef != nil {
if err := rc.db.ensurePlacementPolicy(ctx, db.PlacementPolicyRef.Name, rc.policyMap); err != nil {
if err := conn.ensurePlacementPolicy(ctx, db.PlacementPolicyRef.Name, rc.policyMap); err != nil {

Check warning on line 819 in br/pkg/restore/client.go

View check run for this annotation

Codecov / codecov/patch

br/pkg/restore/client.go#L819

Added line #L819 was not covered by tests
return errors.Trace(err)
}
}

return rc.db.CreateDatabase(ctx, db)
return conn.CreateDatabase(ctx, db)
}

// CreateTables creates multiple tables, and returns their rewrite rules.
Expand Down
2 changes: 1 addition & 1 deletion br/pkg/restore/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestCheckTargetClusterFresh(t *testing.T) {
ctx := context.Background()
require.NoError(t, client.CheckTargetClusterFresh(ctx))

require.NoError(t, client.CreateDatabase(ctx, &model.DBInfo{Name: model.NewCIStr("user_db")}))
require.NoError(t, client.CreateDatabases(ctx, []*utils.Database{{Info: &model.DBInfo{Name: model.NewCIStr("user_db")}}}))
require.True(t, berrors.ErrRestoreNotFreshCluster.Equal(client.CheckTargetClusterFresh(ctx)))
}

Expand Down
7 changes: 2 additions & 5 deletions br/pkg/task/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -878,11 +878,8 @@ func runRestore(c context.Context, g glue.Glue, cmdName string, cfg *RestoreConf
return nil
}

for _, db := range dbs {
err = client.CreateDatabase(ctx, db.Info)
if err != nil {
return errors.Trace(err)
}
if err = client.CreateDatabases(ctx, dbs); err != nil {
return errors.Trace(err)
}

// We make bigger errCh so we won't block on multi-part failed.
Expand Down