summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/mongo/db/op_observer.h25
-rw-r--r--src/mongo/db/op_observer_impl.cpp22
-rw-r--r--src/mongo/db/op_observer_impl.h16
-rw-r--r--src/mongo/db/op_observer_impl_test.cpp35
-rw-r--r--src/mongo/db/op_observer_noop.cpp18
-rw-r--r--src/mongo/db/op_observer_noop.h16
6 files changed, 90 insertions, 42 deletions
diff --git a/src/mongo/db/op_observer.h b/src/mongo/db/op_observer.h
index f6f1408f585..9ccafadc298 100644
--- a/src/mongo/db/op_observer.h
+++ b/src/mongo/db/op_observer.h
@@ -161,7 +161,7 @@ public:
/**
* This function logs an oplog entry when a 'drop' command on a collection is executed.
* Returns the optime of the oplog entry successfully written to the oplog.
- * Returns a null optime if an oplog entry should not be written for this operation.
+ * Returns a null optime if an oplog entry was not written for this operation.
*/
virtual repl::OpTime onDropCollection(OperationContext* opCtx,
const NamespaceString& collectionName,
@@ -181,14 +181,21 @@ public:
OptionalCollectionUUID uuid,
const std::string& indexName,
const BSONObj& indexInfo) = 0;
- virtual void onRenameCollection(OperationContext* opCtx,
- const NamespaceString& fromCollection,
- const NamespaceString& toCollection,
- OptionalCollectionUUID uuid,
- bool dropTarget,
- OptionalCollectionUUID dropTargetUUID,
- OptionalCollectionUUID dropSourceUUID,
- bool stayTemp) = 0;
+
+ /**
+ * This function logs an oplog entry when a 'renameCollection' command on a collection is
+ * executed.
+ * Returns the optime of the oplog entry successfully written to the oplog.
+ * Returns a null optime if an oplog entry was not written for this operation.
+ */
+ virtual repl::OpTime onRenameCollection(OperationContext* opCtx,
+ const NamespaceString& fromCollection,
+ const NamespaceString& toCollection,
+ OptionalCollectionUUID uuid,
+ bool dropTarget,
+ OptionalCollectionUUID dropTargetUUID,
+ OptionalCollectionUUID dropSourceUUID,
+ bool stayTemp) = 0;
virtual void onApplyOps(OperationContext* opCtx,
const std::string& dbName,
const BSONObj& applyOpCmd) = 0;
diff --git a/src/mongo/db/op_observer_impl.cpp b/src/mongo/db/op_observer_impl.cpp
index 8cdad0d7b2a..c857ef9b1aa 100644
--- a/src/mongo/db/op_observer_impl.cpp
+++ b/src/mongo/db/op_observer_impl.cpp
@@ -415,14 +415,14 @@ void OpObserverImpl::onDropIndex(OperationContext* opCtx,
getGlobalAuthorizationManager()->logOp(opCtx, "c", commandNS, cmdObj, &indexInfo);
}
-void OpObserverImpl::onRenameCollection(OperationContext* opCtx,
- const NamespaceString& fromCollection,
- const NamespaceString& toCollection,
- OptionalCollectionUUID uuid,
- bool dropTarget,
- OptionalCollectionUUID dropTargetUUID,
- OptionalCollectionUUID dropSourceUUID,
- bool stayTemp) {
+repl::OpTime OpObserverImpl::onRenameCollection(OperationContext* opCtx,
+ const NamespaceString& fromCollection,
+ const NamespaceString& toCollection,
+ OptionalCollectionUUID uuid,
+ bool dropTarget,
+ OptionalCollectionUUID dropTargetUUID,
+ OptionalCollectionUUID dropSourceUUID,
+ bool stayTemp) {
const NamespaceString cmdNss = fromCollection.getCommandNS();
BSONObjBuilder builder;
builder.append("renameCollection", fromCollection.ns());
@@ -438,7 +438,9 @@ void OpObserverImpl::onRenameCollection(OperationContext* opCtx,
}
BSONObj cmdObj = builder.done();
- repl::logOp(opCtx, "c", cmdNss, uuid, cmdObj, nullptr, false, kUninitializedStmtId);
+ auto renameOpTime =
+ repl::logOp(opCtx, "c", cmdNss, uuid, cmdObj, nullptr, false, kUninitializedStmtId);
+
if (fromCollection.isSystemDotViews())
DurableViewCatalog::onExternalChange(opCtx, fromCollection);
if (toCollection.isSystemDotViews())
@@ -462,6 +464,8 @@ void OpObserverImpl::onRenameCollection(OperationContext* opCtx,
UUIDCatalog& catalog = UUIDCatalog::get(opCtx);
catalog.onRenameCollection(opCtx, newColl, uuid.get());
}
+
+ return renameOpTime;
}
void OpObserverImpl::onApplyOps(OperationContext* opCtx,
diff --git a/src/mongo/db/op_observer_impl.h b/src/mongo/db/op_observer_impl.h
index bc701e1688b..25f1e28523f 100644
--- a/src/mongo/db/op_observer_impl.h
+++ b/src/mongo/db/op_observer_impl.h
@@ -81,14 +81,14 @@ public:
OptionalCollectionUUID uuid,
const std::string& indexName,
const BSONObj& indexInfo) override;
- void onRenameCollection(OperationContext* opCtx,
- const NamespaceString& fromCollection,
- const NamespaceString& toCollection,
- OptionalCollectionUUID uuid,
- bool dropTarget,
- OptionalCollectionUUID dropTargetUUID,
- OptionalCollectionUUID dropSourceUUID,
- bool stayTemp) override;
+ repl::OpTime onRenameCollection(OperationContext* opCtx,
+ const NamespaceString& fromCollection,
+ const NamespaceString& toCollection,
+ OptionalCollectionUUID uuid,
+ bool dropTarget,
+ OptionalCollectionUUID dropTargetUUID,
+ OptionalCollectionUUID dropSourceUUID,
+ bool stayTemp) override;
void onApplyOps(OperationContext* opCtx,
const std::string& dbName,
const BSONObj& applyOpCmd) override;
diff --git a/src/mongo/db/op_observer_impl_test.cpp b/src/mongo/db/op_observer_impl_test.cpp
index 0c4b752f575..4a91a3e8502 100644
--- a/src/mongo/db/op_observer_impl_test.cpp
+++ b/src/mongo/db/op_observer_impl_test.cpp
@@ -203,5 +203,40 @@ TEST_F(OpObserverTest, OnDropCollectionReturnsDropOpTime) {
ASSERT_EQUALS(repl::ReplClientInfo::forClient(&cc()).getLastOp(), dropOpTime);
}
+TEST_F(OpObserverTest, OnRenameCollectionReturnsRenameOpTime) {
+ OpObserverImpl opObserver;
+ auto opCtx = cc().makeOperationContext();
+
+ // Create 'renameCollection' command.
+ auto dropTarget = false;
+ auto stayTemp = false;
+ NamespaceString sourceNss("test.foo");
+ NamespaceString targetNss("test.bar");
+ auto renameCmd = BSON(
+ "renameCollection" << sourceNss.ns() << "to" << targetNss.ns() << "stayTemp" << stayTemp
+ << "dropTarget"
+ << dropTarget);
+
+ // Write to the oplog.
+ repl::OpTime renameOpTime;
+ {
+ AutoGetDb autoDb(opCtx.get(), sourceNss.db(), MODE_X);
+ WriteUnitOfWork wunit(opCtx.get());
+ renameOpTime = opObserver.onRenameCollection(
+ opCtx.get(), sourceNss, targetNss, {}, dropTarget, {}, {}, stayTemp);
+ wunit.commit();
+ }
+
+ auto oplogEntry = getSingleOplogEntry(opCtx.get());
+
+ // Ensure that renameCollection fields were properly added to oplog entry.
+ auto o = oplogEntry.getObjectField("o");
+ auto oExpected = renameCmd;
+ ASSERT_BSONOBJ_EQ(oExpected, o);
+
+ // Ensure that the rename optime returned is the same as the last optime in the ReplClientInfo.
+ ASSERT_EQUALS(repl::ReplClientInfo::forClient(&cc()).getLastOp(), renameOpTime);
+}
+
} // namespace
} // namespace mongo
diff --git a/src/mongo/db/op_observer_noop.cpp b/src/mongo/db/op_observer_noop.cpp
index fe863c705f5..648f14b6a99 100644
--- a/src/mongo/db/op_observer_noop.cpp
+++ b/src/mongo/db/op_observer_noop.cpp
@@ -109,14 +109,14 @@ void OpObserverNoop::onDropIndex(OperationContext*,
const std::string&,
const BSONObj&) {}
-void OpObserverNoop::onRenameCollection(OperationContext* opCtx,
- const NamespaceString& fromCollection,
- const NamespaceString& toCollection,
- OptionalCollectionUUID uuid,
- bool dropTarget,
- OptionalCollectionUUID dropTargetUUID,
- OptionalCollectionUUID dropSourceUUID,
- bool stayTemp) {
+repl::OpTime OpObserverNoop::onRenameCollection(OperationContext* opCtx,
+ const NamespaceString& fromCollection,
+ const NamespaceString& toCollection,
+ OptionalCollectionUUID uuid,
+ bool dropTarget,
+ OptionalCollectionUUID dropTargetUUID,
+ OptionalCollectionUUID dropSourceUUID,
+ bool stayTemp) {
// Evict namespace entry from the namespace/uuid cache if it exists.
NamespaceUUIDCache& cache = NamespaceUUIDCache::get(opCtx);
cache.evictNamespace(fromCollection);
@@ -132,6 +132,8 @@ void OpObserverNoop::onRenameCollection(OperationContext* opCtx,
UUIDCatalog& catalog = UUIDCatalog::get(opCtx);
catalog.onRenameCollection(opCtx, newColl, uuid.get());
}
+
+ return {};
}
void OpObserverNoop::onApplyOps(OperationContext*, const std::string&, const BSONObj&) {}
diff --git a/src/mongo/db/op_observer_noop.h b/src/mongo/db/op_observer_noop.h
index 9abcebd9d4e..7e51f18177b 100644
--- a/src/mongo/db/op_observer_noop.h
+++ b/src/mongo/db/op_observer_noop.h
@@ -81,14 +81,14 @@ public:
OptionalCollectionUUID uuid,
const std::string& indexName,
const BSONObj& idxDescriptor) override;
- void onRenameCollection(OperationContext* opCtx,
- const NamespaceString& fromCollection,
- const NamespaceString& toCollection,
- OptionalCollectionUUID uuid,
- bool dropTarget,
- OptionalCollectionUUID dropTargetUUID,
- OptionalCollectionUUID dropSourceUUID,
- bool stayTemp) override;
+ repl::OpTime onRenameCollection(OperationContext* opCtx,
+ const NamespaceString& fromCollection,
+ const NamespaceString& toCollection,
+ OptionalCollectionUUID uuid,
+ bool dropTarget,
+ OptionalCollectionUUID dropTargetUUID,
+ OptionalCollectionUUID dropSourceUUID,
+ bool stayTemp) override;
void onApplyOps(OperationContext* opCtx,
const std::string& dbName,
const BSONObj& applyOpCmd) override;