@@ -222,7 +222,9 @@ func (c *Catalog) doCreateCollection(
222222 cmd = append (cmd , bson.E {"indexOptionDefaults" , opts .IndexOptionDefaults })
223223 }
224224
225- err := c .target .Database (db ).RunCommand (ctx , cmd ).Err ()
225+ err := runWithRetry (ctx , func (ctx context.Context ) error {
226+ return c .target .Database (db ).RunCommand (ctx , cmd ).Err ()
227+ })
226228 if err != nil {
227229 return errors .Wrap (err , "create collection" )
228230 }
@@ -250,7 +252,9 @@ func (c *Catalog) doCreateView(
250252 cmd = append (cmd , bson.E {"collation" , opts .Collation })
251253 }
252254
253- err := c .target .Database (db ).RunCommand (ctx , cmd ).Err ()
255+ err := runWithRetry (ctx , func (ctx context.Context ) error {
256+ return c .target .Database (db ).RunCommand (ctx , cmd ).Err ()
257+ })
254258 if err != nil {
255259 return errors .Wrap (err , "create view" )
256260 }
@@ -265,7 +269,9 @@ func (c *Catalog) DropCollection(ctx context.Context, db, coll string) error {
265269 c .lock .Lock ()
266270 defer c .lock .Unlock ()
267271
268- err := c .target .Database (db ).Collection (coll ).Drop (ctx )
272+ err := runWithRetry (ctx , func (ctx context.Context ) error {
273+ return c .target .Database (db ).Collection (coll ).Drop (ctx )
274+ })
269275 if err != nil {
270276 return err //nolint:wrapcheck
271277 }
@@ -292,7 +298,9 @@ func (c *Catalog) DropDatabase(ctx context.Context, db string) error {
292298
293299 for _ , coll := range colls {
294300 eg .Go (func () error {
295- err := c .target .Database (db ).Collection (coll ).Drop (grpCtx )
301+ err := runWithRetry (grpCtx , func (ctx context.Context ) error {
302+ return c .target .Database (db ).Collection (coll ).Drop (ctx )
303+ })
296304 if err != nil {
297305 return errors .Wrapf (err , "drop namespace %s.%s" , db , coll )
298306 }
@@ -376,12 +384,13 @@ func (c *Catalog) CreateIndexes(
376384 // NOTE: [mongo.IndexView.CreateMany] uses [mongo.IndexModel]
377385 // which does not support `prepareUnique`.
378386 for _ , index := range idxs {
379- res := c .target .Database (db ).RunCommand (ctx , bson.D {
380- {"createIndexes" , coll },
381- {"indexes" , bson.A {index }},
387+ err := runWithRetry (ctx , func (ctx context.Context ) error {
388+ return c .target .Database (db ).RunCommand (ctx , bson.D {
389+ {"createIndexes" , coll },
390+ {"indexes" , bson.A {index }},
391+ }).Err ()
382392 })
383-
384- if err := res .Err (); err != nil {
393+ if err != nil {
385394 processedIdxs [index .Name ] = err
386395
387396 continue
@@ -468,9 +477,9 @@ func (c *Catalog) ModifyCappedCollection(
468477 cmd = append (cmd , bson.E {"cappedMax" , maxDocs })
469478 }
470479
471- err := c . target . Database ( db ). RunCommand ( ctx , cmd ). Err ()
472-
473- return err //nolint:wrapcheck
480+ return runWithRetry ( ctx , func ( ctx context. Context ) error {
481+ return c . target . Database ( db ). RunCommand ( ctx , cmd ). Err ()
482+ }) //nolint:wrapcheck
474483}
475484
476485// ModifyView modifies a view in the target MongoDB.
@@ -483,9 +492,10 @@ func (c *Catalog) ModifyView(ctx context.Context, db, view, viewOn string, pipel
483492 {"viewOn" , viewOn },
484493 {"pipeline" , pipeline },
485494 }
486- err := c .target .Database (db ).RunCommand (ctx , cmd ).Err ()
487495
488- return err //nolint:wrapcheck
496+ return runWithRetry (ctx , func (ctx context.Context ) error {
497+ return c .target .Database (db ).RunCommand (ctx , cmd ).Err ()
498+ }) //nolint:wrapcheck
489499}
490500
491501func (c * Catalog ) ModifyChangeStreamPreAndPostImages (
@@ -501,9 +511,10 @@ func (c *Catalog) ModifyChangeStreamPreAndPostImages(
501511 {"collMod" , coll },
502512 {"changeStreamPreAndPostImages" , bson.D {{"enabled" , enabled }}},
503513 }
504- err := c .target .Database (db ).RunCommand (ctx , cmd ).Err ()
505514
506- return err //nolint:wrapcheck
515+ return runWithRetry (ctx , func (ctx context.Context ) error {
516+ return c .target .Database (db ).RunCommand (ctx , cmd ).Err ()
517+ }) //nolint:wrapcheck
507518}
508519
509520// ModifyCappedCollection modifies a capped collection in the target MongoDB.
@@ -529,9 +540,9 @@ func (c *Catalog) ModifyValidation(
529540 cmd = append (cmd , bson.E {"validationAction" , validationAction })
530541 }
531542
532- err := c . target . Database ( db ). RunCommand ( ctx , cmd ). Err ()
533-
534- return err //nolint:wrapcheck
543+ return runWithRetry ( ctx , func ( ctx context. Context ) error {
544+ return c . target . Database ( db ). RunCommand ( ctx , cmd ). Err ()
545+ }) //nolint:wrapcheck
535546}
536547
537548// ModifyIndex modifies an index in the target MongoDB.
@@ -548,7 +559,9 @@ func (c *Catalog) ModifyIndex(ctx context.Context, db, coll string, mods *Modify
548559 }},
549560 }
550561
551- err := c .target .Database (db ).RunCommand (ctx , cmd ).Err ()
562+ err := runWithRetry (ctx , func (ctx context.Context ) error {
563+ return c .target .Database (db ).RunCommand (ctx , cmd ).Err ()
564+ })
552565 if err != nil {
553566 return errors .Wrap (err , "modify index: " + mods .Name )
554567 }
@@ -594,7 +607,9 @@ func (c *Catalog) Rename(ctx context.Context, db, coll, targetDB, targetColl str
594607 {"dropTarget" , true },
595608 }
596609
597- err := c .target .Database ("admin" ).RunCommand (ctx , opts ).Err ()
610+ err := runWithRetry (ctx , func (ctx context.Context ) error {
611+ return c .target .Database ("admin" ).RunCommand (ctx , opts ).Err ()
612+ })
598613 if err != nil {
599614 if topo .IsNamespaceNotFound (err ) {
600615 lg .Errorf (err , "" )
@@ -618,7 +633,9 @@ func (c *Catalog) DropIndex(ctx context.Context, db, coll, index string) error {
618633
619634 lg := log .Ctx (ctx )
620635
621- err := c .target .Database (db ).Collection (coll ).Indexes ().DropOne (ctx , index )
636+ err := runWithRetry (ctx , func (ctx context.Context ) error {
637+ return c .target .Database (db ).Collection (coll ).Indexes ().DropOne (ctx , index )
638+ })
622639 if err != nil {
623640 if ! topo .IsNamespaceNotFound (err ) && ! topo .IsIndexNotFound (err ) {
624641 return err //nolint:wrapcheck
@@ -803,15 +820,15 @@ func (c *Catalog) doModifyIndexOption(
803820 propName string ,
804821 value any ,
805822) error {
806- res := c . target . Database ( db ). RunCommand ( ctx , bson. D {
807- { "collMod" , coll },
808- { "index " , bson. D {
809- {"name " , index },
810- { propName , value },
811- } },
812- })
813-
814- return res . Err ( ) //nolint:wrapcheck
823+ return runWithRetry ( ctx , func ( ctx context. Context ) error {
824+ return c . target . Database ( db ). RunCommand ( ctx , bson. D {
825+ { "collMod " , coll },
826+ {"index " , bson. D {
827+ { "name" , index },
828+ { propName , value },
829+ }},
830+ }). Err ()
831+ } ) //nolint:wrapcheck
815832}
816833
817834// getIndexFromCatalog gets an index spec from the catalog.
@@ -1009,3 +1026,10 @@ func (c *Catalog) renameCollectionInCatalog(
10091026 c .deleteCollectionFromCatalog (ctx , db , coll )
10101027 lg .Debugf ("Collection renamed in catalog %s.%s to %s.%s" , db , coll , targetDB , targetColl )
10111028}
1029+
1030+ func runWithRetry (
1031+ ctx context.Context ,
1032+ fn func (context.Context ) error ,
1033+ ) error {
1034+ return topo .RunWithRetry (ctx , fn , topo .DefaultRetryInterval , topo .DefaultMaxRetries ) //nolint:wrapcheck
1035+ }
0 commit comments