Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: retry pitr cutover for PostgreSQL (#2384)

Yilong Li716106ec1bc0
diff --git a/server/task_executor_pitr_cutover.go b/server/task_executor_pitr_cutover.go
index 525e5f9b13..d1f0779063 100644
--- a/server/task_executor_pitr_cutover.go
+++ b/server/task_executor_pitr_cutover.go
@@ -7,6 +7,7 @@ import (
"fmt"
"strconv"
"sync/atomic"
+ "time"
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
@@ -96,17 +97,8 @@ func (exec *PITRCutoverTaskExecutor) pitrCutover(ctx context.Context, task *api.
}
defer driver.Close(ctx)
- switch task.Instance.Engine {
- case db.Postgres:
- if err := exec.pitrCutoverPostgres(ctx, driver, task, issue); err != nil {
- return true, nil, errors.Wrap(err, "failed to do cutover for PostgreSQL")
- }
- case db.MySQL:
- if err := exec.pitrCutoverMySQL(ctx, driver, server, task, issue); err != nil {
- return true, nil, errors.Wrap(err, "failed to do cutover for MySQL")
- }
- default:
- return true, nil, errors.Errorf("invalid database type %q for cutover task", task.Instance.Engine)
+ if err := exec.doCutover(ctx, driver, server, task, issue); err != nil {
+ return true, nil, err
}
log.Debug("Appending new migration history record")
@@ -142,6 +134,40 @@ func (exec *PITRCutoverTaskExecutor) pitrCutover(ctx context.Context, task *api.
}, nil
}
+func (exec *PITRCutoverTaskExecutor) doCutover(ctx context.Context, driver db.Driver, server *Server, task *api.Task, issue *api.Issue) error {
+ switch task.Instance.Engine {
+ case db.Postgres:
+ // Retry so that if there are clients reconnecting to the related databases, we can potentially kill the connections and do the cutover successfully.
+ ticker := time.NewTicker(100 * time.Millisecond)
+ defer ticker.Stop()
+ maxRetry := 3
+ retry := 0
+ for {
+ select {
+ case <-ticker.C:
+ retry++
+ if err := exec.pitrCutoverPostgres(ctx, driver, task, issue); err != nil {
+ if retry == maxRetry {
+ return errors.Wrapf(err, "failed to do cutover for PostgreSQL after retried for %d times", maxRetry)
+ }
+ log.Debug("Failed to do cutover for PostgreSQL. Retry later.", zap.Error(err))
+ } else {
+ return nil
+ }
+ case <-ctx.Done():
+ return errors.Errorf("context is canceled when doing cutover for PostgreSQL")
+ }
+ }
+ case db.MySQL:
+ if err := exec.pitrCutoverMySQL(ctx, driver, server, task, issue); err != nil {
+ return errors.Wrap(err, "failed to do cutover for MySQL")
+ }
+ return nil
+ default:
+ return errors.Errorf("invalid database type %q for cutover task", task.Instance.Engine)
+ }
+}
+
func (*PITRCutoverTaskExecutor) pitrCutoverMySQL(ctx context.Context, driver db.Driver, server *Server, task *api.Task, issue *api.Issue) error {
driverDB, err := driver.GetDBConnection(ctx, "")
if err != nil {