@@ -36,6 +36,8 @@ import (
3636 "github.com/percona/percona-backup-mongodb/pbm/version"
3737)
3838
39+ const mongoErrUnsatisfiableCommitQuorum = 278
40+
3941// mDBCl represents mDB client iterface for the DB related ops.
4042type mDBCl interface {
4143 runCmdShardsvrDropDatabase (ctx context.Context , db string , configDBDoc * configDatabasesDoc ) error
@@ -1316,16 +1318,22 @@ func (r *Restore) restoreIndexes(ctx context.Context, nss []string) error {
13161318 delete (index .Options , "v" )
13171319 }
13181320
1319- rawCommand := bson.D {
1320- {"createIndexes" , ns .Collection },
1321- {"indexes" , indexes },
1322- {"ignoreUnknownIndexOptions" , true },
1323- {"commitQuorum" , r .indexCommitQuorum .CommandValue ()},
1324- }
1321+ commitQuorum := r .indexCommitQuorum .CommandValue ()
1322+ rawCommand := createIndexesCommand (ns .Collection , indexes , commitQuorum )
13251323
13261324 r .log .Info ("restoring indexes for %s.%s: %s" ,
13271325 ns .DB , ns .Collection , strings .Join (indexNames , ", " ))
13281326 err := r .nodeConn .Database (ns .DB ).RunCommand (ctx , rawCommand ).Err ()
1327+ if shouldRetryWithDefaultIndexCommitQuorum (err , commitQuorum ) {
1328+ commitQuorum = config .DefaultRestoreIndexCommitQuorum .CommandValue ()
1329+ r .log .Debug ("createIndexes for %s.%s failed with MongoDB error: %v" , ns .DB , ns .Collection , err )
1330+ r .log .Warning (
1331+ "index commit quorum cannot be satisfied for %s.%s, retrying with %s" ,
1332+ ns .DB , ns .Collection , commitQuorum ,
1333+ )
1334+ rawCommand = createIndexesCommand (ns .Collection , indexes , commitQuorum )
1335+ err = r .nodeConn .Database (ns .DB ).RunCommand (ctx , rawCommand ).Err ()
1336+ }
13291337 if err != nil {
13301338 return errors .Wrapf (err , "createIndexes for %s.%s" , ns .DB , ns .Collection )
13311339 }
@@ -1334,6 +1342,27 @@ func (r *Restore) restoreIndexes(ctx context.Context, nss []string) error {
13341342 return nil
13351343}
13361344
1345+ func createIndexesCommand (collection string , indexes []* idx.IndexDocument , commitQuorum any ) bson.D {
1346+ return bson.D {
1347+ {"createIndexes" , collection },
1348+ {"indexes" , indexes },
1349+ {"ignoreUnknownIndexOptions" , true },
1350+ {"commitQuorum" , commitQuorum },
1351+ }
1352+ }
1353+
1354+ func shouldRetryWithDefaultIndexCommitQuorum (err error , commitQuorum any ) bool {
1355+ if err == nil {
1356+ return false
1357+ }
1358+ if _ , ok := commitQuorum .(int32 ); ! ok {
1359+ return false
1360+ }
1361+
1362+ var cmdErr mongo.CommandError
1363+ return errors .As (err , & cmdErr ) && cmdErr .HasErrorCode (mongoErrUnsatisfiableCommitQuorum )
1364+ }
1365+
13371366func (r * Restore ) updateRouterConfig (ctx context.Context ) error {
13381367 if len (r .sMap ) == 0 || ! r .nodeInfo .IsSharded () {
13391368 return nil
0 commit comments