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

wait for etcd to be ready before continuing with etcd api call #628

Merged
merged 2 commits into from
May 24, 2023
Merged
Changes from 1 commit
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
62 changes: 46 additions & 16 deletions pkg/server/backuprestoreserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,50 @@ func (b *BackupRestoreServer) startHTTPServer(initializer initializer.Initialize
return handler
}

func waitUntilEtcdRunning(ctx context.Context, etcdConnectionConfig *brtypes.EtcdConnectionConfig, logger *logrus.Logger) error {
ticker := time.NewTicker(4 * time.Second)
defer ticker.Stop()
logger.Info("Checking if etcd is running")
for !isEtcdRunning(ctx, 2*time.Second, etcdConnectionConfig, logger) {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
}
}
logger.Info("Etcd is now running. Continuing br startup")
return nil
}

func isEtcdRunning(ctx context.Context, timeout time.Duration, etcdConnectionConfig *brtypes.EtcdConnectionConfig, logger *logrus.Logger) bool {
var endPoint string
aaronfern marked this conversation as resolved.
Show resolved Hide resolved

factory := etcdutil.NewFactory(*etcdConnectionConfig)
client, err := factory.NewMaintenance()
if err != nil {
logger.Errorf("failed to create etcd maintenance client: %v", err)
return false
}
defer client.Close()

if len(etcdConnectionConfig.Endpoints) > 0 {
endPoint = etcdConnectionConfig.Endpoints[0]
} else {
logger.Errorf("etcd endpoints are not passed correctly")
return false
}

ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

_, err = client.Status(ctx, endPoint)
if err != nil {
logger.Errorf("failed to get status of etcd endPoint: %v with error: %v", endPoint, err)
aaronfern marked this conversation as resolved.
Show resolved Hide resolved
return false
}
return true
}

// runServer runs the etcd-backup-restore server according to snapstore provider configuration.
func (b *BackupRestoreServer) runServer(ctx context.Context, restoreOpts *brtypes.RestoreOptions) error {
var (
Expand All @@ -173,23 +217,9 @@ func (b *BackupRestoreServer) runServer(ctx context.Context, restoreOpts *brtype
defer handler.Stop()

metrics.CurrentClusterSize.With(prometheus.Labels{}).Set(float64(restoreOpts.OriginalClusterSize))
// Promotes member if it is a learner

if restoreOpts.OriginalClusterSize > 1 {
for {
select {
case <-ctx.Done():
b.logger.Info("Context cancelled. Stopping retry promoting member")
return ctx.Err()
default:
}
m := member.NewMemberControl(b.config.EtcdConnectionConfig)
err := m.PromoteMember(ctx)
if err == nil {
break
}
_ = miscellaneous.SleepWithContext(ctx, retryTimeout)
}
if err := waitUntilEtcdRunning(ctx, b.config.EtcdConnectionConfig, b.logger.Logger); err != nil {
return err
}

m := member.NewMemberControl(b.config.EtcdConnectionConfig)
Expand Down