Skip to content

Commit 56e7858

Browse files
committed
TOOLS-4703 Simplify MongoRestore.ApplyOps method
This method was accepting ops as a `[]any` slice, even though it was only ever called with a single value, and that single value can always be a `db.Oplog` struct. I changed the code to work this way, which simplifies adding some new logging in this method.
1 parent 3724b0e commit 56e7858

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

mongorestore/metadata.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -339,19 +339,14 @@ func (restore *MongoRestore) createCollectionWithApplyOps(
339339
return fmt.Errorf("Couldn't restore UUID because UUID was invalid: %s", err)
340340
}
341341

342-
createOp := struct {
343-
Operation string `bson:"op"`
344-
Namespace string `bson:"ns"`
345-
Object bson.D `bson:"o"`
346-
UI *primitive.Binary `bson:"ui,omitempty"`
347-
}{
342+
createOp := db.Oplog{
348343
Operation: "c",
349344
Namespace: intent.DB + ".$cmd",
350345
Object: command,
351346
UI: &primitive.Binary{Subtype: 0x04, Data: uuid},
352347
}
353348

354-
return restore.ApplyOps(session, []interface{}{createOp})
349+
return restore.ApplyOp(session, createOp)
355350
}
356351

357352
func createCollectionCommand(intent *intents.Intent, options bson.D) bson.D {

mongorestore/oplog.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ func (restore *MongoRestore) HandleNonTxnOp(oplogCtx *oplogContext, op db.Oplog)
362362
}
363363
}
364364

365-
return restore.ApplyOps(oplogCtx.session, []interface{}{op})
365+
return restore.ApplyOp(oplogCtx.session, op)
366366
}
367367

368368
func (restore *MongoRestore) HandleTxnOp(oplogCtx *oplogContext, meta txn.Meta, op db.Oplog) error {
@@ -414,10 +414,27 @@ Loop:
414414
return nil
415415
}
416416

417-
// ApplyOps is a wrapper for the applyOps database command, we pass in
418-
// a session to avoid opening a new connection for a few inserts at a time.
419-
func (restore *MongoRestore) ApplyOps(session *mongo.Client, entries []interface{}) error {
420-
singleRes := session.Database("admin").RunCommand(context.TODO(), bson.D{{"applyOps", entries}})
417+
// ApplyOp is a wrapper for the applyOps database command, we pass in
418+
// a session to avoid opening a new connection for each op applied.
419+
func (restore *MongoRestore) ApplyOp(session *mongo.Client, op db.Oplog) error {
420+
if log.IsInVerbosity(log.DebugLow) {
421+
opDetails := fmt.Sprintf("%#q to %#q", op.Operation, op.Namespace)
422+
if op.Operation == "c" {
423+
var cmd string
424+
// This should be impossible but we don't want to crash when logging.
425+
if len(op.Object) == 0 {
426+
cmd = "op.Object is empty"
427+
} else {
428+
cmd = op.Object[0].Key
429+
}
430+
431+
opDetails = fmt.Sprintf("%s with command %#q", opDetails, cmd)
432+
}
433+
log.Logv(log.DebugLow, opDetails)
434+
}
435+
436+
singleRes := session.Database("admin").
437+
RunCommand(context.TODO(), bson.D{{"applyOps", []db.Oplog{op}}})
421438
if err := singleRes.Err(); err != nil {
422439
return fmt.Errorf("applyOps: %v", err)
423440
}

0 commit comments

Comments
 (0)