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

Fix connect to mongodb during restore procedure #1428

Merged
merged 1 commit into from
Jan 30, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 34 additions & 12 deletions internal/databases/mongo/binary/mongod.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"time"

"github.com/cenkalti/backoff"
"github.com/pkg/errors"
"github.com/wal-g/tracelog"
"go.mongodb.org/mongo-driver/bson"
Expand All @@ -18,26 +19,47 @@ import (
const adminDB = "admin"

const cursorCreateRetries = 10
const mongoConnectRetries = 3

type MongodService struct {
Context context.Context
MongoClient *mongo.Client
}

func CreateMongodService(ctx context.Context, appName, mongodbURI string) (*MongodService, error) {
mongoClient, err := mongo.Connect(ctx,
options.Client().ApplyURI(mongodbURI).
SetConnectTimeout(10*time.Minute).
SetSocketTimeout(time.Minute).
SetAppName(appName).
SetDirect(true).
SetRetryReads(false))
var repeatOptions backoff.BackOff
repeatOptions = backoff.NewExponentialBackOff()
repeatOptions = backoff.WithMaxRetries(repeatOptions, mongoConnectRetries)
repeatOptions = backoff.WithContext(repeatOptions, ctx)

var mongoClient *mongo.Client
var err error
err = backoff.RetryNotify(
func() error {
mongoClient, err = mongo.Connect(ctx,
options.Client().ApplyURI(mongodbURI).
SetServerSelectionTimeout(10*time.Minute).
SetConnectTimeout(10*time.Minute).
SetSocketTimeout(time.Minute).
SetAppName(appName).
SetDirect(true).
SetRetryReads(false))
if err != nil {
return errors.Wrap(err, "unable to connect to mongod")
}
err = mongoClient.Ping(ctx, nil)
if err != nil {
return errors.Wrap(err, "ping to mongod is failed")
}
return nil
},
repeatOptions,
func(err error, duration time.Duration) {
tracelog.InfoLogger.Printf("Unable to connect due '%+v', next retry: %v", err, duration)
},
)
if err != nil {
return nil, errors.Wrap(err, "unable to connect to mongod")
}
err = mongoClient.Ping(ctx, nil)
if err != nil {
return nil, errors.Wrap(err, "ping to mongod is failed")
return nil, err
}

return &MongodService{
Expand Down
5 changes: 4 additions & 1 deletion internal/databases/mongo/binary/mongod_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@ func (mongodProcess *MongodProcess) Wait() error {
} else {
tracelog.InfoLogger.Printf("Mongod %v stopped successfully!", mongodProcess.GetURI())
}
mongodProcess.cancel()
return err
}

func (mongodProcess *MongodProcess) Close() {
mongodProcess.cancel()
}

func (mongodProcess *MongodProcess) start() (err error) {
mongodProcess.port, err = randomUnusedPort()
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions internal/databases/mongo/binary/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ func (restoreService *RestoreService) fixSystemData(rsConfig RsConfig) error {
return errors.Wrap(err, "unable to start mongod in special mode")
}

defer mongodProcess.Close()

mongodService, err := CreateMongodService(restoreService.Context, "wal-g restore", mongodProcess.GetURI())
if err != nil {
return errors.Wrap(err, "unable to create mongod service")
Expand All @@ -99,6 +101,8 @@ func (restoreService *RestoreService) recoverFromOplogAsStandalone() error {
return errors.Wrap(err, "unable to start mongod in special mode")
}

defer mongodProcess.Close()

mongodService, err := CreateMongodService(restoreService.Context, "wal-g restore", mongodProcess.GetURI())
if err != nil {
return errors.Wrap(err, "unable to create mongod service")
Expand Down