3636#include " mongo/db/catalog/catalog_test_fixture.h"
3737#include " mongo/db/catalog/collection.h"
3838#include " mongo/db/dbdirectclient.h"
39+ #include " mongo/db/op_observer_impl.h"
40+ #include " mongo/db/op_observer_registry.h"
41+ #include " mongo/db/repl/replication_coordinator.h"
3942#include " mongo/db/storage/durable_catalog.h"
43+ #include " mongo/db/storage/snapshot_manager.h"
4044#include " mongo/db/storage/wiredtiger/wiredtiger_global_options.h"
4145#include " mongo/util/fail_point.h"
4246
@@ -59,6 +63,9 @@ class ValidateStateTest : public CatalogTestFixture {
5963 * Create collection 'nss' and insert some documents. It will possess a default _id index.
6064 */
6165 Collection* createCollectionAndPopulateIt (OperationContext* opCtx, const NamespaceString& nss);
66+
67+ private:
68+ void setUp () override ;
6269};
6370
6471void ValidateStateTest::createCollection (OperationContext* opCtx, const NamespaceString& nss) {
@@ -88,6 +95,29 @@ Collection* ValidateStateTest::createCollectionAndPopulateIt(OperationContext* o
8895 return collection;
8996}
9097
98+ void ValidateStateTest::setUp () {
99+ CatalogTestFixture::setUp ();
100+
101+ auto service = getServiceContext ();
102+
103+ // Set up OpObserver so that we will append actual oplog entries to the oplog using
104+ // repl::logOp(). This supports index builds that have to look up the last oplog entry.
105+ auto opObserverRegistry = dynamic_cast <OpObserverRegistry*>(service->getOpObserver ());
106+ opObserverRegistry->addObserver (std::make_unique<OpObserverImpl>());
107+
108+ // Index builds expect a non-empty oplog and a valid committed snapshot.
109+ auto opCtx = operationContext ();
110+ Lock::GlobalLock lk (opCtx, MODE_IX);
111+ WriteUnitOfWork wuow (opCtx);
112+ service->getOpObserver ()->onOpMessage (opCtx, BSONObj ());
113+ wuow.commit ();
114+
115+ // Provide an initial committed snapshot so that index build can begin the collection scan.
116+ auto snapshotManager = service->getStorageEngine ()->getSnapshotManager ();
117+ auto lastAppliedOpTime = repl::ReplicationCoordinator::get (service)->getMyLastAppliedOpTime ();
118+ snapshotManager->setCommittedSnapshot (lastAppliedOpTime.getTimestamp ());
119+ }
120+
91121/* *
92122 * Builds an index on the given 'nss'. 'indexKey' specifies the index key, e.g. {'a': 1};
93123 */
0 commit comments