diff options
| author | Lucas de Castro Borges <lucas@gnuabordo.com.br> | 2025-02-14 14:26:38 -0300 |
|---|---|---|
| committer | Lucas de Castro Borges <lucas@gnuabordo.com.br> | 2025-02-14 14:26:38 -0300 |
| commit | 294bc6ecabf14c09c9bc8644704921dcf97cb44e (patch) | |
| tree | 279b1e0bab53901a1647ac63c1c724f0f789a663 /src/mongo/dbtests | |
| parent | 70be7c27a251621187a1de533462ae2bb1e3bd39 (diff) | |
| parent | 1e917fd798aa25b7066d4b414b51184f13d5a092 (diff) | |
Update upstream source from tag 'upstream/6.0.10'debian/6.0.10-1
Update to upstream version '6.0.10'
with Debian dir 2d176fa254eee97b139f712fec5709641335a8c3
Diffstat (limited to 'src/mongo/dbtests')
29 files changed, 244 insertions, 347 deletions
diff --git a/src/mongo/dbtests/SConscript b/src/mongo/dbtests/SConscript index 9aea0a64976..418d4893655 100644 --- a/src/mongo/dbtests/SConscript +++ b/src/mongo/dbtests/SConscript @@ -32,8 +32,7 @@ env.Library( LIBDEPS=[ '$BUILD_DIR/mongo/db/catalog/catalog_impl', '$BUILD_DIR/mongo/db/dbdirectclient', - '$BUILD_DIR/mongo/db/index/index_access_method_factory', - '$BUILD_DIR/mongo/db/index/index_access_methods', + '$BUILD_DIR/mongo/db/index/index_access_method', '$BUILD_DIR/mongo/db/index_builds_coordinator_mongod', '$BUILD_DIR/mongo/db/op_observer', '$BUILD_DIR/mongo/db/service_context_d', @@ -96,7 +95,6 @@ env.Program( 'mock_dbclient_conn_test.cpp', 'mock_replica_set_test.cpp', 'multikey_paths_test.cpp', - 'shared_buffer_test.cpp', 'pdfiletests.cpp', 'plan_executor_invalidation_test.cpp', 'plan_ranking.cpp', @@ -149,7 +147,7 @@ env.Program( "$BUILD_DIR/mongo/db/commands/test_commands_enabled", "$BUILD_DIR/mongo/db/concurrency/deferred_writer", "$BUILD_DIR/mongo/db/exec/document_value/document_value_test_util", - "$BUILD_DIR/mongo/db/index/index_access_methods", + "$BUILD_DIR/mongo/db/index/index_access_method", "$BUILD_DIR/mongo/db/logical_time_metadata_hook", "$BUILD_DIR/mongo/db/mongohasher", "$BUILD_DIR/mongo/db/multitenancy", diff --git a/src/mongo/dbtests/cursor_manager_test.cpp b/src/mongo/dbtests/cursor_manager_test.cpp index 1cc8ee1f535..f71e45fe2ec 100644 --- a/src/mongo/dbtests/cursor_manager_test.cpp +++ b/src/mongo/dbtests/cursor_manager_test.cpp @@ -753,5 +753,44 @@ TEST_F(CursorManagerTestCustomOpCtx, CursorsMarkedAsKilledAreReturnedForOpKeyLoo auto cursors = useCursorManager()->getCursorsForOpKeys({opKey}); ASSERT_EQ(cursors.size(), size_t(1)); } + +TEST_F(CursorManagerTestCustomOpCtx, + GetCursorIdsForNamespaceReturnsSingleEntryForMatchingNamespace) { + auto opCtx = _queryServiceContext->makeOperationContext(); + auto pinned = makeCursor(opCtx.get()); + auto cursorId = pinned.getCursor()->cursorid(); + auto cursorsForNamespace = useCursorManager()->getCursorIdsForNamespace(kTestNss); + ASSERT_EQUALS(cursorsForNamespace.size(), 1ull); + ASSERT_EQUALS(cursorsForNamespace[0], cursorId); +} + +TEST_F(CursorManagerTestCustomOpCtx, + GetCursorIdsForNamespaceReturnsMultipleEntriesForMatchingNamespace) { + auto opCtx = _queryServiceContext->makeOperationContext(); + auto pinned1 = makeCursor(opCtx.get()); + auto pinned2 = makeCursor(opCtx.get()); + auto cursorId1 = pinned1.getCursor()->cursorid(); + auto cursorId2 = pinned2.getCursor()->cursorid(); + auto cursorsForNamespace = useCursorManager()->getCursorIdsForNamespace(kTestNss); + ASSERT_EQUALS(cursorsForNamespace.size(), 2ull); + // The results for cursorsForNamespace won't necessarily be the same as the order of insertion. + std::set<CursorId> cursorsForNamespaceSet(cursorsForNamespace.begin(), + cursorsForNamespace.end()); + + ASSERT_EQUALS(cursorsForNamespaceSet.count(cursorId1), 1ull); + ASSERT_EQUALS(cursorsForNamespaceSet.count(cursorId2), 1ull); +} + +TEST_F(CursorManagerTestCustomOpCtx, + GetCursorIdsForNamespaceDoesNotReturnEntriesForNonMatchingNamespace) { + auto opCtx = _queryServiceContext->makeOperationContext(); + // Add a cursor for kTestNss. + auto pinned = makeCursor(opCtx.get()); + // Get cursors for a different NamespaceString. + auto cursorsForNamespace = + useCursorManager()->getCursorIdsForNamespace(NamespaceString("somerandom.nss")); + ASSERT_EQUALS(cursorsForNamespace.size(), 0ull); +} + } // namespace } // namespace mongo diff --git a/src/mongo/dbtests/framework.cpp b/src/mongo/dbtests/framework.cpp index 330d796e9f3..27068cc571e 100644 --- a/src/mongo/dbtests/framework.cpp +++ b/src/mongo/dbtests/framework.cpp @@ -43,7 +43,6 @@ #include "mongo/db/client.h" #include "mongo/db/concurrency/lock_state.h" #include "mongo/db/dbdirectclient.h" -#include "mongo/db/index/index_access_method_factory_impl.h" #include "mongo/db/index_builds_coordinator_mongod.h" #include "mongo/db/op_observer_registry.h" #include "mongo/db/s/collection_sharding_state_factory_shard.h" @@ -118,8 +117,6 @@ int runDbTests(int argc, char** argv) { StorageControl::startStorageControls(globalServiceContext, true /*forTestOnly*/); DatabaseHolder::set(globalServiceContext, std::make_unique<DatabaseHolderImpl>()); - IndexAccessMethodFactory::set(globalServiceContext, - std::make_unique<IndexAccessMethodFactoryImpl>()); Collection::Factory::set(globalServiceContext, std::make_unique<CollectionImpl::FactoryImpl>()); IndexBuildsCoordinator::set(globalServiceContext, std::make_unique<IndexBuildsCoordinatorMongod>()); diff --git a/src/mongo/dbtests/indexcatalogtests.cpp b/src/mongo/dbtests/indexcatalogtests.cpp index 6e6da7e8c7a..7a648f654f0 100644 --- a/src/mongo/dbtests/indexcatalogtests.cpp +++ b/src/mongo/dbtests/indexcatalogtests.cpp @@ -90,8 +90,8 @@ public: ASSERT_TRUE(indexCatalog(&opCtx)->numIndexesReady(&opCtx) == numFinishedIndexesStart + 2); - std::unique_ptr<IndexCatalog::IndexIterator> ii = - indexCatalog(&opCtx)->getIndexIterator(&opCtx, false); + auto ii = + indexCatalog(&opCtx)->getIndexIterator(&opCtx, IndexCatalog::InclusionPolicy::kReady); int indexesIterated = 0; bool foundIndex = false; while (ii->more()) { diff --git a/src/mongo/dbtests/indexupdatetests.cpp b/src/mongo/dbtests/indexupdatetests.cpp index e38d3663bc2..2988f9f658a 100644 --- a/src/mongo/dbtests/indexupdatetests.cpp +++ b/src/mongo/dbtests/indexupdatetests.cpp @@ -222,8 +222,10 @@ public: .getStatus()); auto& coll = collection(); - auto desc = - coll->getIndexCatalog()->findIndexByName(_opCtx, "a", true /* includeUnfinished */); + auto desc = coll->getIndexCatalog()->findIndexByName( + _opCtx, + "a", + IndexCatalog::InclusionPolicy::kReady | IndexCatalog::InclusionPolicy::kUnfinished); ASSERT(desc); // Hybrid index builds check duplicates explicitly. diff --git a/src/mongo/dbtests/mock/mock_replica_set.cpp b/src/mongo/dbtests/mock/mock_replica_set.cpp index b03ffd477ed..3871e1ef191 100644 --- a/src/mongo/dbtests/mock/mock_replica_set.cpp +++ b/src/mongo/dbtests/mock/mock_replica_set.cpp @@ -149,7 +149,7 @@ void MockReplicaSet::setPrimary(const string& hostAndPort) { _primaryHost = hostAndPort; - mockIsMasterCmd(); + mockHelloCmd(); mockReplSetGetStatusCmd(); } @@ -183,7 +183,7 @@ repl::ReplSetConfig MockReplicaSet::getReplConfig() const { void MockReplicaSet::setConfig(const repl::ReplSetConfig& newConfig) { _replConfig = newConfig; - mockIsMasterCmd(); + mockHelloCmd(); mockReplSetGetStatusCmd(); } @@ -211,14 +211,14 @@ BSONObj MockReplicaSet::mockHelloResponseFor(const MockRemoteDBServer& server) c const MemberConfig* member = _replConfig.findMemberByHostAndPort(hostAndPort); if (!member) { - builder.append("ismaster", false); + builder.append("isWritablePrimary", false); builder.append("secondary", false); vector<string> hostList; builder.append("hosts", hostList); } else { const bool isPrimary = hostAndPort.toString() == getPrimary(); - builder.append("ismaster", isPrimary); + builder.append("isWritablePrimary", isPrimary); builder.append("secondary", !isPrimary); { @@ -285,14 +285,12 @@ BSONObj MockReplicaSet::mockHelloResponseFor(const MockRemoteDBServer& server) c return builder.obj(); } -void MockReplicaSet::mockIsMasterCmd() { +void MockReplicaSet::mockHelloCmd() { for (ReplNodeMap::iterator nodeIter = _nodeMap.begin(); nodeIter != _nodeMap.end(); ++nodeIter) { - auto isMaster = mockHelloResponseFor(*nodeIter->second); + auto helloReply = mockHelloResponseFor(*nodeIter->second); - // DBClientBase::isMaster() sends "ismaster", but ReplicaSetMonitor sends "isMaster". - nodeIter->second->setCommandReply("ismaster", isMaster); - nodeIter->second->setCommandReply("isMaster", isMaster); + nodeIter->second->setCommandReply("hello", helloReply); } } diff --git a/src/mongo/dbtests/mock/mock_replica_set.h b/src/mongo/dbtests/mock/mock_replica_set.h index 677d0b822a1..d93ba956ee8 100644 --- a/src/mongo/dbtests/mock/mock_replica_set.h +++ b/src/mongo/dbtests/mock/mock_replica_set.h @@ -57,7 +57,7 @@ class ClockSource; class MockReplicaSet { public: /** - * Creates a mock replica set and automatically mocks the isMaster and replSetGetStatus commands + * Creates a mock replica set and automatically mocks the hello and replSetGetStatus commands * based on the default replica set configuration. Either the first node is primary and the * others are secondaries, or all are secondaries. By default, hostnames begin with "$", which * signals to ReplicaSetMonitor and to ConnectionString::connect that these are mocked hosts. @@ -87,12 +87,11 @@ public: std::vector<std::string> getSecondaries() const; /** - * Sets the configuration for this replica sets. This also has a side effect - * of mocking the ismaster and replSetGetStatus command responses based on - * the new config. + * Sets the configuration for this replica sets. This also has a side effect of mocking the + * hello and replSetGetStatus command responses based on the new config. * - * Note: does not automatically select a new primary. Can be done manually by - * calling setPrimary. + * Note: does not automatically select a new primary. Can be done manually by calling + * setPrimary. */ void setConfig(const repl::ReplSetConfig& newConfig); @@ -138,10 +137,9 @@ private: typedef std::map<std::string, MockRemoteDBServer*> ReplNodeMap; /** - * Mocks the ismaster command based on the information on the current - * replica set configuration. + * Mocks the "hello" command based on the information on the current replica set configuration. */ - void mockIsMasterCmd(); + void mockHelloCmd(); /** * Mock the hello response for the given server. diff --git a/src/mongo/dbtests/mock_dbclient_conn_test.cpp b/src/mongo/dbtests/mock_dbclient_conn_test.cpp index a0da2e717ea..19ca674c84c 100644 --- a/src/mongo/dbtests/mock_dbclient_conn_test.cpp +++ b/src/mongo/dbtests/mock_dbclient_conn_test.cpp @@ -507,23 +507,23 @@ TEST(MockDBClientConnTest, CyclingCmd) { MockRemoteDBServer server("test"); { - vector<mongo::StatusWith<BSONObj>> isMasterSequence; - isMasterSequence.push_back(BSON("set" - << "a" - << "isMaster" << true << "ok" << 1)); - isMasterSequence.push_back(BSON("set" - << "a" - << "isMaster" << false << "ok" << 1)); - server.setCommandReply("isMaster", isMasterSequence); + vector<mongo::StatusWith<BSONObj>> helloReplySequence; + helloReplySequence.push_back(BSON("set" + << "a" + << "isWritablePrimary" << true << "ok" << 1)); + helloReplySequence.push_back(BSON("set" + << "a" + << "isWritablePrimary" << false << "ok" << 1)); + server.setCommandReply("hello", helloReplySequence); } { MockDBClientConnection conn(&server); BSONObj response; - ASSERT(conn.runCommand("foo.baz", BSON("isMaster" << 1), response)); + ASSERT(conn.runCommand("foo.baz", BSON("hello" << 1), response)); ASSERT_EQUALS(1, response["ok"].numberInt()); ASSERT_EQUALS("a", response["set"].str()); - ASSERT(response["isMaster"].trueValue()); + ASSERT(response["isWritablePrimary"].trueValue()); ASSERT_EQUALS(1U, server.getCmdCount()); } @@ -531,10 +531,10 @@ TEST(MockDBClientConnTest, CyclingCmd) { { MockDBClientConnection conn(&server); BSONObj response; - ASSERT(conn.runCommand("foo.baz", BSON("isMaster" << 1), response)); + ASSERT(conn.runCommand("foo.baz", BSON("hello" << 1), response)); ASSERT_EQUALS(1, response["ok"].numberInt()); ASSERT_EQUALS("a", response["set"].str()); - ASSERT(!response["isMaster"].trueValue()); + ASSERT(!response["isWritablePrimary"].trueValue()); ASSERT_EQUALS(2U, server.getCmdCount()); } @@ -542,10 +542,10 @@ TEST(MockDBClientConnTest, CyclingCmd) { { MockDBClientConnection conn(&server); BSONObj response; - ASSERT(conn.runCommand("foo.baz", BSON("isMaster" << 1), response)); + ASSERT(conn.runCommand("foo.baz", BSON("hello" << 1), response)); ASSERT_EQUALS(1, response["ok"].numberInt()); ASSERT_EQUALS("a", response["set"].str()); - ASSERT(response["isMaster"].trueValue()); + ASSERT(response["isWritablePrimary"].trueValue()); ASSERT_EQUALS(3U, server.getCmdCount()); } @@ -554,13 +554,13 @@ TEST(MockDBClientConnTest, CyclingCmd) { TEST(MockDBClientConnTest, MultipleStoredResponse) { MockRemoteDBServer server("test"); server.setCommandReply("serverStatus", BSON("ok" << 0)); - server.setCommandReply("isMaster", BSON("ok" << 1 << "secondary" << false)); + server.setCommandReply("hello", BSON("ok" << 1 << "secondary" << false)); MockDBClientConnection conn(&server); { BSONObj response; ASSERT(conn.runCommand("foo.baz", - BSON("isMaster" + BSON("hello" << "abc"), response)); ASSERT(!response["secondary"].trueValue()); diff --git a/src/mongo/dbtests/mock_replica_set_test.cpp b/src/mongo/dbtests/mock_replica_set_test.cpp index e3461193747..3b87b3b20e5 100644 --- a/src/mongo/dbtests/mock_replica_set_test.cpp +++ b/src/mongo/dbtests/mock_replica_set_test.cpp @@ -71,7 +71,7 @@ TEST(MockReplicaSetTest, GetNode) { ASSERT(replSet.getNode("$n3:27017") == nullptr); } -TEST(MockReplicaSetTest, IsMasterNode0) { +TEST(MockReplicaSetTest, HelloNode0) { MockReplicaSet replSet("n", 3); set<string> expectedHosts; expectedHosts.insert("$n0:27017"); @@ -80,11 +80,10 @@ TEST(MockReplicaSetTest, IsMasterNode0) { BSONObj cmdResponse; MockRemoteDBServer* node = replSet.getNode("$n0:27017"); - bool ok = - MockDBClientConnection(node).runCommand("foo.bar", BSON("ismaster" << 1), cmdResponse); + bool ok = MockDBClientConnection(node).runCommand("foo.bar", BSON("hello" << 1), cmdResponse); ASSERT(ok); - ASSERT(cmdResponse["ismaster"].trueValue()); + ASSERT(cmdResponse["isWritablePrimary"].trueValue()); ASSERT(!cmdResponse["secondary"].trueValue()); ASSERT_EQUALS("$n0:27017", cmdResponse["me"].str()); ASSERT_EQUALS("$n0:27017", cmdResponse["primary"].str()); @@ -99,7 +98,7 @@ TEST(MockReplicaSetTest, IsMasterNode0) { ASSERT(expectedHosts == hostList); } -TEST(MockReplicaSetTest, IsMasterNode1) { +TEST(MockReplicaSetTest, HelloNode1) { MockReplicaSet replSet("n", 3); set<string> expectedHosts; expectedHosts.insert("$n0:27017"); @@ -108,11 +107,10 @@ TEST(MockReplicaSetTest, IsMasterNode1) { BSONObj cmdResponse; MockRemoteDBServer* node = replSet.getNode("$n1:27017"); - bool ok = - MockDBClientConnection(node).runCommand("foo.bar", BSON("ismaster" << 1), cmdResponse); + bool ok = MockDBClientConnection(node).runCommand("foo.bar", BSON("hello" << 1), cmdResponse); ASSERT(ok); - ASSERT(!cmdResponse["ismaster"].trueValue()); + ASSERT(!cmdResponse["isWritablePrimary"].trueValue()); ASSERT(cmdResponse["secondary"].trueValue()); ASSERT_EQUALS("$n1:27017", cmdResponse["me"].str()); ASSERT_EQUALS("$n0:27017", cmdResponse["primary"].str()); @@ -127,7 +125,7 @@ TEST(MockReplicaSetTest, IsMasterNode1) { ASSERT(expectedHosts == hostList); } -TEST(MockReplicaSetTest, IsMasterNode2) { +TEST(MockReplicaSetTest, HelloNode2) { MockReplicaSet replSet("n", 3); set<string> expectedHosts; expectedHosts.insert("$n0:27017"); @@ -136,11 +134,10 @@ TEST(MockReplicaSetTest, IsMasterNode2) { BSONObj cmdResponse; MockRemoteDBServer* node = replSet.getNode("$n2:27017"); - bool ok = - MockDBClientConnection(node).runCommand("foo.bar", BSON("ismaster" << 1), cmdResponse); + bool ok = MockDBClientConnection(node).runCommand("foo.bar", BSON("hello" << 1), cmdResponse); ASSERT(ok); - ASSERT(!cmdResponse["ismaster"].trueValue()); + ASSERT(!cmdResponse["isWritablePrimary"].trueValue()); ASSERT(cmdResponse["secondary"].trueValue()); ASSERT_EQUALS("$n2:27017", cmdResponse["me"].str()); ASSERT_EQUALS("$n0:27017", cmdResponse["primary"].str()); @@ -290,7 +287,7 @@ ReplSetConfig _getConfigWithMemberRemoved(const ReplSetConfig& oldConfig, } } // namespace -TEST(MockReplicaSetTest, IsMasterReconfigNodeRemoved) { +TEST(MockReplicaSetTest, HelloReconfigNodeRemoved) { MockReplicaSet replSet("n", 3); ReplSetConfig oldConfig = replSet.getReplConfig(); @@ -299,14 +296,14 @@ TEST(MockReplicaSetTest, IsMasterReconfigNodeRemoved) { replSet.setConfig(newConfig); { - // Check isMaster for node still in set + // Check that node is still a writable primary. BSONObj cmdResponse; MockRemoteDBServer* node = replSet.getNode("$n0:27017"); bool ok = - MockDBClientConnection(node).runCommand("foo.bar", BSON("ismaster" << 1), cmdResponse); + MockDBClientConnection(node).runCommand("foo.bar", BSON("hello" << 1), cmdResponse); ASSERT(ok); - ASSERT(cmdResponse["ismaster"].trueValue()); + ASSERT(cmdResponse["isWritablePrimary"].trueValue()); ASSERT(!cmdResponse["secondary"].trueValue()); ASSERT_EQUALS("$n0:27017", cmdResponse["me"].str()); ASSERT_EQUALS("$n0:27017", cmdResponse["primary"].str()); @@ -327,14 +324,14 @@ TEST(MockReplicaSetTest, IsMasterReconfigNodeRemoved) { } { - // Check isMaster for node still not in set anymore + // Check node is no longer a writable primary. BSONObj cmdResponse; MockRemoteDBServer* node = replSet.getNode(hostToRemove); bool ok = - MockDBClientConnection(node).runCommand("foo.bar", BSON("ismaster" << 1), cmdResponse); + MockDBClientConnection(node).runCommand("foo.bar", BSON("hello" << 1), cmdResponse); ASSERT(ok); - ASSERT(!cmdResponse["ismaster"].trueValue()); + ASSERT(!cmdResponse["isWritablePrimary"].trueValue()); ASSERT(!cmdResponse["secondary"].trueValue()); ASSERT_EQUALS(hostToRemove, cmdResponse["me"].str()); ASSERT_EQUALS("n", cmdResponse["setName"].str()); diff --git a/src/mongo/dbtests/multikey_paths_test.cpp b/src/mongo/dbtests/multikey_paths_test.cpp index c7d8e1acb28..34afd00fa0e 100644 --- a/src/mongo/dbtests/multikey_paths_test.cpp +++ b/src/mongo/dbtests/multikey_paths_test.cpp @@ -95,7 +95,8 @@ public: const MultikeyPaths& expectedMultikeyPaths) { const IndexCatalog* indexCatalog = collection->getIndexCatalog(); std::vector<const IndexDescriptor*> indexes; - indexCatalog->findIndexesByKeyPattern(_opCtx.get(), keyPattern, false, &indexes); + indexCatalog->findIndexesByKeyPattern( + _opCtx.get(), keyPattern, IndexCatalog::InclusionPolicy::kReady, &indexes); ASSERT_EQ(indexes.size(), 1U); auto desc = indexes[0]; const IndexCatalogEntry* ice = indexCatalog->getEntry(desc); diff --git a/src/mongo/dbtests/query_plan_executor.cpp b/src/mongo/dbtests/query_plan_executor.cpp index b97dae14aa5..7b3249b313d 100644 --- a/src/mongo/dbtests/query_plan_executor.cpp +++ b/src/mongo/dbtests/query_plan_executor.cpp @@ -188,7 +188,8 @@ private: CollectionPtr collection = CollectionCatalog::get(&_opCtx)->lookupCollectionByNamespace(&_opCtx, nss); std::vector<const IndexDescriptor*> indexes; - collection->getIndexCatalog()->findIndexesByKeyPattern(&_opCtx, obj, false, &indexes); + collection->getIndexCatalog()->findIndexesByKeyPattern( + &_opCtx, obj, IndexCatalog::InclusionPolicy::kReady, &indexes); ASSERT_LTE(indexes.size(), 1U); return indexes.size() == 0 ? nullptr : indexes[0]; } diff --git a/src/mongo/dbtests/query_stage_and.cpp b/src/mongo/dbtests/query_stage_and.cpp index 269682199c2..8773f170ca3 100644 --- a/src/mongo/dbtests/query_stage_and.cpp +++ b/src/mongo/dbtests/query_stage_and.cpp @@ -75,7 +75,8 @@ public: const IndexDescriptor* getIndex(const BSONObj& obj, const CollectionPtr& coll) { std::vector<const IndexDescriptor*> indexes; - coll->getIndexCatalog()->findIndexesByKeyPattern(&_opCtx, obj, false, &indexes); + coll->getIndexCatalog()->findIndexesByKeyPattern( + &_opCtx, obj, IndexCatalog::InclusionPolicy::kReady, &indexes); if (indexes.empty()) { FAIL(str::stream() << "Unable to find index with key pattern " << obj); } diff --git a/src/mongo/dbtests/query_stage_batched_delete.cpp b/src/mongo/dbtests/query_stage_batched_delete.cpp index fdf55709d99..a6d3f6f9d7f 100644 --- a/src/mongo/dbtests/query_stage_batched_delete.cpp +++ b/src/mongo/dbtests/query_stage_batched_delete.cpp @@ -32,7 +32,6 @@ #include "mongo/db/catalog/collection.h" #include "mongo/db/catalog/database.h" #include "mongo/db/client.h" -#include "mongo/db/concurrency/write_conflict_exception.h" #include "mongo/db/db_raii.h" #include "mongo/db/dbdirectclient.h" #include "mongo/db/exec/batched_delete_stage.h" @@ -84,10 +83,14 @@ public: class QueryStageBatchedDeleteTest : public unittest::Test { public: QueryStageBatchedDeleteTest() : _client(&_opCtx) { - auto tickSource = std::make_unique<TickSourceMock<Milliseconds>>(); - tickSource->reset(1); - _tickSource = tickSource.get(); - _opCtx.getServiceContext()->setTickSource(std::move(tickSource)); + // Avoid churning the ticket source on the service context which is shared + // with background jobs such as the checkpoint thread. + if (!_tickSource) { + auto tickSource = std::make_unique<TickSourceMock<Milliseconds>>(); + _tickSource = tickSource.get(); + _opCtx.getServiceContext()->setTickSource(std::move(tickSource)); + } + _tickSource->reset(1); std::unique_ptr<ClockAdvancingOpObserver> opObserverUniquePtr = std::make_unique<ClockAdvancingOpObserver>(); opObserverUniquePtr->tickSource = _tickSource; @@ -210,12 +213,15 @@ protected: boost::intrusive_ptr<ExpressionContext> _expCtx = make_intrusive<ExpressionContext>(&_opCtx, nullptr, nss); ClockAdvancingOpObserver* _opObserver; - TickSourceMock<Milliseconds>* _tickSource; + static TickSourceMock<Milliseconds>* _tickSource; private: DBDirectClient _client; }; +// static +TickSourceMock<Milliseconds>* QueryStageBatchedDeleteTest::_tickSource = nullptr; + // Confirms batched deletes wait until a batch meets the targetBatchDocs before deleting documents. TEST_F(QueryStageBatchedDeleteTest, BatchedDeleteTargetBatchDocsBasic) { dbtests::WriteContextForTests ctx(&_opCtx, nss.ns()); @@ -236,9 +242,9 @@ TEST_F(QueryStageBatchedDeleteTest, BatchedDeleteTargetBatchDocsBasic) { ASSERT_EQUALS(state, PlanStage::NEED_TIME); // Only delete documents once the current batch reaches targetBatchDocs. + nIterations++; int batch = nIterations / (int)targetBatchDocs; ASSERT_EQUALS(stats->docsDeleted, targetBatchDocs * batch); - nIterations++; } // There should be 2 more docs deleted by the time the command returns EOF. @@ -525,7 +531,7 @@ TEST_F(QueryStageBatchedDeleteTest, BatchedDeleteTargetBatchTimeMSBasic) { // targetBatchDocs. { ASSERT_LTE(nDocs, targetBatchDocs); - for (auto i = 0; i <= nDocs; i++) { + for (auto i = 0; i < nDocs; i++) { state = deleteStage->work(&id); ASSERT_EQ(stats->docsDeleted, 0); ASSERT_EQ(state, PlanStage::NEED_TIME); @@ -607,7 +613,7 @@ TEST_F(QueryStageBatchedDeleteTest, BatchedDeleteTargetBatchTimeMSWithTargetBatc // Stages up to targetBatchDocs - 1 documents in the buffer. { - for (auto i = 0; i < targetBatchDocs; i++) { + for (auto i = 0; i < targetBatchDocs - 1; i++) { state = deleteStage->work(&id); ASSERT_EQ(stats->docsDeleted, 0); ASSERT_EQ(state, PlanStage::NEED_TIME); diff --git a/src/mongo/dbtests/query_stage_collscan.cpp b/src/mongo/dbtests/query_stage_collscan.cpp index fafbd58c0ef..6f21a6437b8 100644 --- a/src/mongo/dbtests/query_stage_collscan.cpp +++ b/src/mongo/dbtests/query_stage_collscan.cpp @@ -339,6 +339,38 @@ TEST_F(QueryStageCollectionScanTest, QueryStageCollscanBasicBackwardWithMatch) { ASSERT_EQUALS(25, countResults(CollectionScanParams::BACKWARD, obj)); } +TEST_F(QueryStageCollectionScanTest, + QueryTestCollscanStopsScanningOnFilterFailureInClusteredCollectionIfSpecified) { + auto ns = NamespaceString("a.b"); + auto collDeleter = createClusteredCollection(ns, false /* prePopulate */); + for (int i = 1; i <= numObj(); ++i) { + insertDocument(ns, BSON("_id" << i << "foo" << i)); + } + + AutoGetCollectionForRead autoColl(&_opCtx, ns); + const CollectionPtr& coll = autoColl.getCollection(); + ASSERT(coll->isClustered()); + + // Configure the threshold and the expected number of scanned documents. + const int threshold = numObj() / 2; + const int expectedNumberOfScannedDocuments = threshold + 1; + + // Configure the scan. + CollectionScanParams params; + params.shouldReturnEofOnFilterMismatch = true; + WorkingSet ws; + LTEMatchExpression filter{"foo"_sd, Value(threshold)}; + auto scan = std::make_unique<CollectionScan>(_expCtx.get(), coll, params, &ws, &filter); + + // Scan all matching documents. + WorkingSetID id = WorkingSet::INVALID_ID; + while (!scan->isEOF()) { + scan->work(&id); + } + auto collScanStats = static_cast<const CollectionScanStats*>(scan->getSpecificStats()); + ASSERT_EQUALS(expectedNumberOfScannedDocuments, collScanStats->docsTested); +} + // Get objects in the order we inserted them. TEST_F(QueryStageCollectionScanTest, QueryStageCollscanObjectsInOrderForward) { AutoGetCollectionForReadCommand collection(&_opCtx, nss); @@ -537,11 +569,6 @@ TEST_F(QueryStageCollectionScanTest, QueryTestCollscanResumeAfterRecordIdSeekSuc unique_ptr<PlanStage> ps = std::make_unique<CollectionScan>( _expCtx.get(), collection.getCollection(), params, ws.get(), nullptr); - WorkingSetID id = WorkingSet::INVALID_ID; - - // Check that the resume succeeds in making the cursor. - ASSERT_EQUALS(PlanStage::NEED_TIME, ps->work(&id)); - // Run the rest of the scan and verify the results. auto statusWithPlanExecutor = plan_executor_factory::make(_expCtx, diff --git a/src/mongo/dbtests/query_stage_count.cpp b/src/mongo/dbtests/query_stage_count.cpp index 956c1f463b7..89487c2bca7 100644 --- a/src/mongo/dbtests/query_stage_count.cpp +++ b/src/mongo/dbtests/query_stage_count.cpp @@ -203,7 +203,8 @@ public: IndexScan* createIndexScan(MatchExpression* expr, WorkingSet* ws) { const IndexCatalog* catalog = _coll->getIndexCatalog(); std::vector<const IndexDescriptor*> indexes; - catalog->findIndexesByKeyPattern(&_opCtx, BSON("x" << 1), false, &indexes); + catalog->findIndexesByKeyPattern( + &_opCtx, BSON("x" << 1), IndexCatalog::InclusionPolicy::kReady, &indexes); ASSERT_EQ(indexes.size(), 1U); auto descriptor = indexes[0]; @@ -326,12 +327,15 @@ public: class QueryStageCountUpdateDuringYield : public CountStageTest { public: void run() { - // expected count would be kDocuments-2 but we update the first and second records - // after doing the first unit of work so they wind up getting counted later on CountCommandRequest request((NamespaceString(ns()))); request.setQuery(BSON("x" << GTE << 2)); - testCount(request, kDocuments); + // We call 'interject' after first unit of work that skips the first document, so it is + // not counted. + testCount(request, kDocuments - 1); + + // We call 'interject' after first unit of work and even if some documents are skipped, + // they are added to the end of the index on x so they are counted later. testCount(request, kDocuments, true); } diff --git a/src/mongo/dbtests/query_stage_count_scan.cpp b/src/mongo/dbtests/query_stage_count_scan.cpp index 7485a938c01..b03ed4a49a9 100644 --- a/src/mongo/dbtests/query_stage_count_scan.cpp +++ b/src/mongo/dbtests/query_stage_count_scan.cpp @@ -95,7 +95,8 @@ public: const IndexDescriptor* getIndex(Database* db, const BSONObj& obj) { std::vector<const IndexDescriptor*> indexes; - getCollection()->getIndexCatalog()->findIndexesByKeyPattern(&_opCtx, obj, false, &indexes); + getCollection()->getIndexCatalog()->findIndexesByKeyPattern( + &_opCtx, obj, IndexCatalog::InclusionPolicy::kReady, &indexes); return indexes.empty() ? nullptr : indexes[0]; } diff --git a/src/mongo/dbtests/query_stage_delete.cpp b/src/mongo/dbtests/query_stage_delete.cpp index 5c07aab93cf..d8725ddbc24 100644 --- a/src/mongo/dbtests/query_stage_delete.cpp +++ b/src/mongo/dbtests/query_stage_delete.cpp @@ -32,7 +32,6 @@ #include "mongo/db/catalog/collection.h" #include "mongo/db/catalog/database.h" #include "mongo/db/client.h" -#include "mongo/db/concurrency/write_conflict_exception.h" #include "mongo/db/db_raii.h" #include "mongo/db/dbdirectclient.h" #include "mongo/db/exec/collection_scan.h" diff --git a/src/mongo/dbtests/query_stage_distinct.cpp b/src/mongo/dbtests/query_stage_distinct.cpp index 57552d1f18e..bea39ab55f1 100644 --- a/src/mongo/dbtests/query_stage_distinct.cpp +++ b/src/mongo/dbtests/query_stage_distinct.cpp @@ -130,7 +130,8 @@ public: // Set up the distinct stage. std::vector<const IndexDescriptor*> indexes; - coll->getIndexCatalog()->findIndexesByKeyPattern(&_opCtx, BSON("a" << 1), false, &indexes); + coll->getIndexCatalog()->findIndexesByKeyPattern( + &_opCtx, BSON("a" << 1), IndexCatalog::InclusionPolicy::kReady, &indexes); ASSERT_EQ(indexes.size(), 1U); DistinctParams params{&_opCtx, coll, indexes[0]}; @@ -196,7 +197,8 @@ public: // Set up the distinct stage. std::vector<const IndexDescriptor*> indexes; - coll->getIndexCatalog()->findIndexesByKeyPattern(&_opCtx, BSON("a" << 1), false, &indexes); + coll->getIndexCatalog()->findIndexesByKeyPattern( + &_opCtx, BSON("a" << 1), IndexCatalog::InclusionPolicy::kReady, &indexes); verify(indexes.size() == 1); DistinctParams params{&_opCtx, coll, indexes[0]}; @@ -263,7 +265,7 @@ public: std::vector<const IndexDescriptor*> indices; coll->getIndexCatalog()->findIndexesByKeyPattern( - &_opCtx, BSON("a" << 1 << "b" << 1), false, &indices); + &_opCtx, BSON("a" << 1 << "b" << 1), IndexCatalog::InclusionPolicy::kReady, &indices); ASSERT_EQ(1U, indices.size()); DistinctParams params{&_opCtx, coll, indices[0]}; diff --git a/src/mongo/dbtests/query_stage_ixscan.cpp b/src/mongo/dbtests/query_stage_ixscan.cpp index 30ebc65e983..d8a2454b259 100644 --- a/src/mongo/dbtests/query_stage_ixscan.cpp +++ b/src/mongo/dbtests/query_stage_ixscan.cpp @@ -97,7 +97,8 @@ public: IndexScan* createIndexScanSimpleRange(BSONObj startKey, BSONObj endKey) { IndexCatalog* catalog = _coll->getIndexCatalog(); std::vector<const IndexDescriptor*> indexes; - catalog->findIndexesByKeyPattern(&_opCtx, BSON("x" << 1), false, &indexes); + catalog->findIndexesByKeyPattern( + &_opCtx, BSON("x" << 1), IndexCatalog::InclusionPolicy::kReady, &indexes); ASSERT_EQ(indexes.size(), 1U); // We are not testing indexing here so use maximal bounds @@ -120,7 +121,8 @@ public: int direction = 1) { IndexCatalog* catalog = _coll->getIndexCatalog(); std::vector<const IndexDescriptor*> indexes; - catalog->findIndexesByKeyPattern(&_opCtx, BSON("x" << 1), false, &indexes); + catalog->findIndexesByKeyPattern( + &_opCtx, BSON("x" << 1), IndexCatalog::InclusionPolicy::kReady, &indexes); ASSERT_EQ(indexes.size(), 1U); IndexScanParams params(&_opCtx, _collPtr, indexes[0]); diff --git a/src/mongo/dbtests/query_stage_merge_sort.cpp b/src/mongo/dbtests/query_stage_merge_sort.cpp index f3f9f48139d..4b25e270647 100644 --- a/src/mongo/dbtests/query_stage_merge_sort.cpp +++ b/src/mongo/dbtests/query_stage_merge_sort.cpp @@ -75,7 +75,8 @@ public: const IndexDescriptor* getIndex(const BSONObj& obj, const CollectionPtr& coll) { std::vector<const IndexDescriptor*> indexes; - coll->getIndexCatalog()->findIndexesByKeyPattern(&_opCtx, obj, false, &indexes); + coll->getIndexCatalog()->findIndexesByKeyPattern( + &_opCtx, obj, IndexCatalog::InclusionPolicy::kReady, &indexes); return indexes.empty() ? nullptr : indexes[0]; } diff --git a/src/mongo/dbtests/query_stage_multiplan.cpp b/src/mongo/dbtests/query_stage_multiplan.cpp index 3552cf095fd..9defb8663e9 100644 --- a/src/mongo/dbtests/query_stage_multiplan.cpp +++ b/src/mongo/dbtests/query_stage_multiplan.cpp @@ -146,7 +146,7 @@ unique_ptr<PlanStage> getIxScanPlan(ExpressionContext* expCtx, int desiredFooValue) { std::vector<const IndexDescriptor*> indexes; coll->getIndexCatalog()->findIndexesByKeyPattern( - expCtx->opCtx, BSON("foo" << 1), false, &indexes); + expCtx->opCtx, BSON("foo" << 1), IndexCatalog::InclusionPolicy::kReady, &indexes); ASSERT_EQ(indexes.size(), 1U); IndexScanParams ixparams(expCtx->opCtx, coll, indexes[0]); @@ -182,6 +182,11 @@ unique_ptr<PlanStage> getCollScanPlan(ExpressionContext* expCtx, return root; } +const PlanStage* getBestPlanRoot(const MultiPlanStage* mps) { + auto bestPlanIdx = mps->bestPlanIdx(); + return bestPlanIdx ? mps->getChildren()[bestPlanIdx.get()].get() : nullptr; +} + std::unique_ptr<MultiPlanStage> runMultiPlanner(ExpressionContext* expCtx, const NamespaceString& nss, const CollectionPtr& coll, @@ -191,6 +196,7 @@ std::unique_ptr<MultiPlanStage> runMultiPlanner(ExpressionContext* expCtx, // at least). unique_ptr<WorkingSet> sharedWs(new WorkingSet()); unique_ptr<PlanStage> ixScanRoot = getIxScanPlan(expCtx, coll, sharedWs.get(), desiredFooValue); + const auto* ixScanRootPtr = ixScanRoot.get(); // Plan 1: CollScan. BSONObj filterObj = BSON("foo" << desiredFooValue); @@ -209,7 +215,7 @@ std::unique_ptr<MultiPlanStage> runMultiPlanner(ExpressionContext* expCtx, NoopYieldPolicy yieldPolicy(expCtx->opCtx->getServiceContext()->getFastClockSource()); ASSERT_OK(mps->pickBestPlan(&yieldPolicy)); ASSERT(mps->bestPlanChosen()); - ASSERT_EQUALS(0, *mps->bestPlanIdx()); + ASSERT_EQUALS(getBestPlanRoot(mps.get()), ixScanRootPtr); return mps; } @@ -242,6 +248,8 @@ TEST_F(QueryStageMultiPlanTest, MPSCollectionScanVsHighlySelectiveIXScan) { unique_ptr<WorkingSet> sharedWs(new WorkingSet()); unique_ptr<PlanStage> ixScanRoot = getIxScanPlan(_expCtx.get(), coll, sharedWs.get(), 7); + const auto* ixScanRootPtr = ixScanRoot.get(); + // Plan 1: CollScan with matcher. BSONObj filterObj = BSON("foo" << 7); unique_ptr<MatchExpression> filter = makeMatchExpressionFromFilter(_expCtx.get(), filterObj); @@ -256,11 +264,7 @@ TEST_F(QueryStageMultiPlanTest, MPSCollectionScanVsHighlySelectiveIXScan) { mps->addPlan(createQuerySolution(), std::move(ixScanRoot), sharedWs.get()); mps->addPlan(createQuerySolution(), std::move(collScanRoot), sharedWs.get()); - // Plan 0 aka the first plan aka the index scan should be the best. - NoopYieldPolicy yieldPolicy(_clock); - ASSERT_OK(mps->pickBestPlan(&yieldPolicy)); - ASSERT(mps->bestPlanChosen()); - ASSERT_EQUALS(0, *mps->bestPlanIdx()); + const auto* mpsPtr = mps.get(); // Takes ownership of arguments other than 'collection'. auto statusWithPlanExecutor = @@ -273,6 +277,9 @@ TEST_F(QueryStageMultiPlanTest, MPSCollectionScanVsHighlySelectiveIXScan) { ASSERT_OK(statusWithPlanExecutor.getStatus()); auto exec = std::move(statusWithPlanExecutor.getValue()); + ASSERT_TRUE(mpsPtr->bestPlanChosen()); + ASSERT_EQUALS(mpsPtr->getChildren()[mpsPtr->bestPlanIdx().get()].get(), ixScanRootPtr); + // Get all our results out. int results = 0; BSONObj obj; @@ -484,6 +491,8 @@ TEST_F(QueryStageMultiPlanTest, MPSExplainAllPlans) { auto ws = std::make_unique<WorkingSet>(); auto firstPlan = std::make_unique<MockStage>(_expCtx.get(), ws.get()); + const auto* firstPlanPtr = firstPlan.get(); + auto secondPlan = std::make_unique<MockStage>(_expCtx.get(), ws.get()); for (int i = 0; i < nDocs; ++i) { @@ -519,7 +528,7 @@ TEST_F(QueryStageMultiPlanTest, MPSExplainAllPlans) { auto root = static_cast<MultiPlanStage*>(execImpl->getRootStage()); ASSERT_TRUE(root->bestPlanChosen()); // The first candidate plan should have won. - ASSERT_EQ(*root->bestPlanIdx(), 0); + ASSERT_EQ(getBestPlanRoot(root), firstPlanPtr); BSONObjBuilder bob; Explain::explainStages(exec.get(), diff --git a/src/mongo/dbtests/query_stage_tests.cpp b/src/mongo/dbtests/query_stage_tests.cpp index e5aa014b78c..a3a46a047cd 100644 --- a/src/mongo/dbtests/query_stage_tests.cpp +++ b/src/mongo/dbtests/query_stage_tests.cpp @@ -127,7 +127,8 @@ public: const IndexDescriptor* getIndex(const BSONObj& obj) { AutoGetCollectionForReadCommand collection(&_opCtx, NamespaceString(ns())); std::vector<const IndexDescriptor*> indexes; - collection->getIndexCatalog()->findIndexesByKeyPattern(&_opCtx, obj, false, &indexes); + collection->getIndexCatalog()->findIndexesByKeyPattern( + &_opCtx, obj, IndexCatalog::InclusionPolicy::kReady, &indexes); return indexes.empty() ? nullptr : indexes[0]; } diff --git a/src/mongo/dbtests/query_stage_update.cpp b/src/mongo/dbtests/query_stage_update.cpp index 64db23ea494..2ae8011e9c8 100644 --- a/src/mongo/dbtests/query_stage_update.cpp +++ b/src/mongo/dbtests/query_stage_update.cpp @@ -38,7 +38,6 @@ #include "mongo/db/catalog/collection.h" #include "mongo/db/catalog/database.h" #include "mongo/db/client.h" -#include "mongo/db/concurrency/write_conflict_exception.h" #include "mongo/db/db_raii.h" #include "mongo/db/dbdirectclient.h" #include "mongo/db/exec/collection_scan.h" diff --git a/src/mongo/dbtests/repltests.cpp b/src/mongo/dbtests/repltests.cpp index dde6c946431..99386593fce 100644 --- a/src/mongo/dbtests/repltests.cpp +++ b/src/mongo/dbtests/repltests.cpp @@ -36,7 +36,7 @@ #include "mongo/db/catalog/collection.h" #include "mongo/db/catalog/index_catalog.h" #include "mongo/db/client.h" -#include "mongo/db/concurrency/write_conflict_exception.h" +#include "mongo/db/concurrency/exception_util.h" #include "mongo/db/db_raii.h" #include "mongo/db/dbdirectclient.h" #include "mongo/db/json.h" @@ -374,12 +374,12 @@ protected: virtual void reset() const = 0; }; -// Some operations are only idempotent when in RECOVERING, not in SECONDARY. This includes -// duplicate inserts and deletes. +// Some operations are only idempotent when in RECOVERING from unstable checkpoint, not in +// SECONDARY. This includes duplicate inserts and deletes. class Recovering : public Base { protected: virtual OplogApplication::Mode getOplogApplicationMode() { - return OplogApplication::Mode::kRecovering; + return OplogApplication::Mode::kUnstableRecovering; } }; diff --git a/src/mongo/dbtests/rollbacktests.cpp b/src/mongo/dbtests/rollbacktests.cpp index 75ce5b01d6e..2f9c12a0577 100644 --- a/src/mongo/dbtests/rollbacktests.cpp +++ b/src/mongo/dbtests/rollbacktests.cpp @@ -110,11 +110,16 @@ void assertEmpty(OperationContext* opCtx, const NamespaceString& nss) { } bool indexExists(OperationContext* opCtx, const NamespaceString& nss, const string& idxName) { auto coll = CollectionCatalog::get(opCtx)->lookupCollectionByNamespace(opCtx, nss); - return coll->getIndexCatalog()->findIndexByName(opCtx, idxName, true) != nullptr; + return coll->getIndexCatalog()->findIndexByName( + opCtx, + idxName, + IndexCatalog::InclusionPolicy::kReady | + IndexCatalog::InclusionPolicy::kUnfinished) != nullptr; } bool indexReady(OperationContext* opCtx, const NamespaceString& nss, const string& idxName) { auto coll = CollectionCatalog::get(opCtx)->lookupCollectionByNamespace(opCtx, nss); - return coll->getIndexCatalog()->findIndexByName(opCtx, idxName, false) != nullptr; + return coll->getIndexCatalog()->findIndexByName( + opCtx, idxName, IndexCatalog::InclusionPolicy::kReady) != nullptr; } size_t getNumIndexEntries(OperationContext* opCtx, const NamespaceString& nss, @@ -123,7 +128,7 @@ size_t getNumIndexEntries(OperationContext* opCtx, auto coll = CollectionCatalog::get(opCtx)->lookupCollectionByNamespace(opCtx, nss); const IndexCatalog* catalog = coll->getIndexCatalog(); - auto desc = catalog->findIndexByName(opCtx, idxName, false); + auto desc = catalog->findIndexByName(opCtx, idxName, IndexCatalog::InclusionPolicy::kReady); if (desc) { auto iam = catalog->getEntry(desc)->accessMethod()->asSortedData(); diff --git a/src/mongo/dbtests/shared_buffer_test.cpp b/src/mongo/dbtests/shared_buffer_test.cpp deleted file mode 100644 index a1c62eef885..00000000000 --- a/src/mongo/dbtests/shared_buffer_test.cpp +++ /dev/null @@ -1,191 +0,0 @@ -/** - * Copyright (C) 2019-present MongoDB, Inc. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the Server Side Public License, version 1, - * as published by MongoDB, Inc. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Server Side Public License for more details. - * - * You should have received a copy of the Server Side Public License - * along with this program. If not, see - * <http://www.mongodb.com/licensing/server-side-public-license>. - * - * As a special exception, the copyright holders give permission to link the - * code of portions of this program with the OpenSSL library under certain - * conditions as described in each individual source file and distribute - * linked combinations including the program with the OpenSSL library. You - * must comply with the Server Side Public License in all respects for - * all of the code used other than as permitted herein. If you modify file(s) - * with this exception, you may extend this exception to your version of the - * file(s), but you are not obligated to do so. If you do not wish to do so, - * delete this exception statement from your version. If you delete this - * exception statement from all source files in the program, then also delete - * it in the license file. - */ - -#include "mongo/platform/basic.h" - -#include "mongo/base/string_data.h" -#include "mongo/util/shared_buffer.h" -#include "mongo/util/shared_buffer_fragment.h" - -#include "mongo/unittest/unittest.h" - -namespace mongo { -namespace { - -using SharedBufferTest = unittest::Test; - -TEST_F(SharedBufferTest, ReallocOrCopyNull) { - SharedBuffer buf; - ASSERT_EQ(buf.capacity(), 0u); - ASSERT(!buf); - ASSERT(!buf.isShared()); - buf.reallocOrCopy(10); - ASSERT(buf); - ASSERT(!buf.isShared()); - ASSERT_EQ(buf.capacity(), 10u); -} - -TEST_F(SharedBufferTest, ReallocOrCopyNullShared) { - // null SharedBuffers are never considered "shared", even when copied. - SharedBuffer buf; - const SharedBuffer sharer = buf; - ASSERT_EQ(buf.capacity(), 0u); - ASSERT(!buf); - ASSERT(!buf.isShared()); - buf.reallocOrCopy(10); - ASSERT(buf); - ASSERT(!buf.isShared()); - ASSERT_EQ(buf.capacity(), 10u); - ASSERT_EQ(sharer.capacity(), 0u); -} - -SharedBuffer makeBuffer() { - SharedBuffer buf = SharedBuffer::allocate(4); - memcpy(buf.get(), "foo", 4); - return buf; -} - -TEST_F(SharedBufferTest, ReallocOrCopyGrow) { - SharedBuffer buf = makeBuffer(); - ASSERT_EQ(buf.capacity(), 4u); - ASSERT(buf); - ASSERT(!buf.isShared()); - buf.reallocOrCopy(10); - ASSERT(buf); - ASSERT(!buf.isShared()); - ASSERT_EQ(buf.capacity(), 10u); - ASSERT_EQ("foo"_sd, buf.get()); -} - -TEST_F(SharedBufferTest, ReallocOrCopyGrowShared) { - SharedBuffer buf = makeBuffer(); - const SharedBuffer sharer = buf; - ASSERT_EQ(buf.capacity(), 4u); - ASSERT(buf); - ASSERT(buf.isShared()); - buf.reallocOrCopy(10); - ASSERT(buf); - ASSERT(!buf.isShared()); - ASSERT_EQ(buf.capacity(), 10u); - ASSERT_EQ(sharer.capacity(), 4u); - ASSERT_EQ("foo"_sd, buf.get()); - ASSERT_EQ("foo"_sd, sharer.get()); - ASSERT_NE(buf.get(), sharer.get()); -} - -TEST_F(SharedBufferTest, ReallocOrCopyShrink) { - SharedBuffer buf = makeBuffer(); - ASSERT_EQ(buf.capacity(), 4u); - ASSERT(buf); - ASSERT(!buf.isShared()); - // The buffer is already at least 1 byte. - buf.reallocOrCopy(1); - ASSERT(buf); - // We copy it anyway. - ASSERT(!buf.isShared()); - ASSERT_EQ(buf.capacity(), 1u); - ASSERT_EQ('f', buf.get()[0]); -} - -TEST_F(SharedBufferTest, ReallocOrCopyShrinkShared) { - SharedBuffer buf = makeBuffer(); - const SharedBuffer sharer = buf; - ASSERT_EQ(buf.capacity(), 4u); - ASSERT(buf); - ASSERT(buf.isShared()); - // The buffer is already at least 1 byte. - buf.reallocOrCopy(1); - ASSERT(buf); - // We copy it anyway. - ASSERT(!buf.isShared()); - ASSERT_EQ(buf.capacity(), 1u); - ASSERT_EQ(sharer.capacity(), 4u); - ASSERT_EQ('f', buf.get()[0]); - ASSERT_EQ("foo"_sd, sharer.get()); - ASSERT_NE(buf.get(), sharer.get()); -} - -TEST_F(SharedBufferTest, SharedBufferFragmentBuilder) { - constexpr size_t kBlockSize = 16; - SharedBufferFragmentBuilder builder(kBlockSize); - - auto verifyFragment = [](const SharedBufferFragment& fragment, uint8_t expected) { - for (size_t i = 0; i < fragment.size(); ++i) - ASSERT(memcmp(fragment.get() + i, &expected, 1) == 0); - }; - - builder.start(kBlockSize / 2); - ASSERT_EQ(builder.capacity(), kBlockSize); - uint8_t one = 1; - memset(builder.get(), one, kBlockSize / 2); - auto fragment1 = builder.finish(kBlockSize / 2); - ASSERT_EQ(fragment1.size(), kBlockSize / 2); - verifyFragment(fragment1, one); - - builder.start(kBlockSize / 2); - ASSERT_EQ(builder.capacity(), kBlockSize / 2); - // We can use less than we ask for - uint8_t two = 2; - memset(builder.get(), two, kBlockSize / 4); - auto fragment2 = builder.finish(kBlockSize / 4); - ASSERT_EQ(fragment2.size(), kBlockSize / 4); - // Buffers should not overlap and be next to each other - ASSERT_EQ(fragment1.get() + fragment1.size(), fragment2.get()); - verifyFragment(fragment2, two); - - // Verify that anything written is transfered when we grow - builder.start(builder.capacity()); - ASSERT_EQ(builder.capacity(), kBlockSize / 4); - uint8_t three = 3; - size_t written = kBlockSize / 4; - // Write current capacity - memset(builder.get(), three, written); - builder.grow(kBlockSize); - // Write the rest - memset(builder.get() + written, three, builder.capacity() - written); - auto fragment3 = builder.finish(kBlockSize); - for (size_t i = 0; i < (kBlockSize / 4); ++i) - ASSERT(memcmp(fragment3.get() + i, &three, 1) == 0); - ASSERT_GTE(builder.capacity(), kBlockSize); - verifyFragment(fragment3, three); - - builder.start(builder.capacity()); - auto ptr = builder.get(); - builder.discard(); - builder.start(builder.capacity()); - ASSERT_EQ(builder.get(), ptr); - - // No buffers should have been overwritten by others - verifyFragment(fragment1, one); - verifyFragment(fragment2, two); - verifyFragment(fragment3, three); -} - -} // namespace -} // namespace mongo diff --git a/src/mongo/dbtests/storage_debug_util.cpp b/src/mongo/dbtests/storage_debug_util.cpp index 29266bc6ddd..408a9c76b6c 100644 --- a/src/mongo/dbtests/storage_debug_util.cpp +++ b/src/mongo/dbtests/storage_debug_util.cpp @@ -64,7 +64,7 @@ void printCollectionAndIndexTableEntries(OperationContext* opCtx, const Namespac // Iterate and print each index's table of documents. const auto indexCatalog = coll->getIndexCatalog(); - const auto it = indexCatalog->getIndexIterator(opCtx, /*includeUnfinished*/ false); + const auto it = indexCatalog->getIndexIterator(opCtx, IndexCatalog::InclusionPolicy::kReady); while (it->more()) { const auto indexCatalogEntry = it->next(); const auto indexDescriptor = indexCatalogEntry->descriptor(); diff --git a/src/mongo/dbtests/validate_tests.cpp b/src/mongo/dbtests/validate_tests.cpp index 031057cb7cf..0b031447a0e 100644 --- a/src/mongo/dbtests/validate_tests.cpp +++ b/src/mongo/dbtests/validate_tests.cpp @@ -54,7 +54,7 @@ using std::unique_ptr; namespace { const auto kIndexVersion = IndexDescriptor::IndexVersion::kV2; -const bool kTurnOnExtraLoggingForTest = true; +const bool kLogDiagnostics = true; std::size_t omitTransientWarningsFromCount(const ValidateResults& results) { return std::count_if( @@ -147,7 +147,7 @@ protected: BSONObjBuilder output; ASSERT_OK(CollectionValidation::validate( - &_opCtx, _nss, mode, repairMode, &results, &output, kTurnOnExtraLoggingForTest)); + &_opCtx, _nss, mode, repairMode, &results, &output, kLogDiagnostics)); // Check if errors are reported if and only if valid is set to false. ASSERT_EQ(results.valid, results.errors.empty()); @@ -1197,7 +1197,7 @@ public: CollectionValidation::RepairMode::kNone, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -1315,7 +1315,7 @@ public: CollectionValidation::RepairMode::kNone, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -1404,7 +1404,7 @@ public: CollectionValidation::RepairMode::kNone, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -1516,7 +1516,7 @@ public: CollectionValidation::RepairMode::kNone, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -1545,7 +1545,7 @@ public: CollectionValidation::RepairMode::kFixErrors, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -1581,7 +1581,7 @@ public: CollectionValidation::RepairMode::kFixErrors, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -1700,7 +1700,7 @@ public: CollectionValidation::RepairMode::kNone, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -1731,7 +1731,7 @@ public: CollectionValidation::RepairMode::kFixErrors, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -1763,7 +1763,7 @@ public: CollectionValidation::RepairMode::kNone, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -1853,7 +1853,7 @@ public: CollectionValidation::RepairMode::kNone, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -1884,7 +1884,7 @@ public: CollectionValidation::RepairMode::kFixErrors, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -1915,7 +1915,7 @@ public: CollectionValidation::RepairMode::kNone, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -2067,7 +2067,7 @@ public: CollectionValidation::RepairMode::kNone, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -2097,7 +2097,7 @@ public: CollectionValidation::RepairMode::kFixErrors, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -2132,7 +2132,7 @@ public: CollectionValidation::RepairMode::kNone, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -2303,7 +2303,7 @@ public: CollectionValidation::RepairMode::kNone, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -2333,7 +2333,7 @@ public: CollectionValidation::RepairMode::kFixErrors, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -2369,7 +2369,7 @@ public: CollectionValidation::RepairMode::kNone, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -2629,7 +2629,7 @@ public: CollectionValidation::RepairMode::kNone, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -2662,7 +2662,7 @@ public: CollectionValidation::RepairMode::kFixErrors, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -2698,7 +2698,7 @@ public: CollectionValidation::RepairMode::kNone, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -2823,7 +2823,7 @@ public: CollectionValidation::RepairMode::kNone, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -2854,7 +2854,7 @@ public: CollectionValidation::RepairMode::kFixErrors, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -2886,7 +2886,7 @@ public: CollectionValidation::RepairMode::kFixErrors, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -3286,7 +3286,7 @@ public: CollectionValidation::RepairMode::kNone, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -3354,7 +3354,7 @@ public: CollectionValidation::RepairMode::kNone, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -3385,7 +3385,7 @@ public: CollectionValidation::RepairMode::kFixErrors, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -3420,7 +3420,7 @@ public: CollectionValidation::RepairMode::kFixErrors, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -3451,7 +3451,7 @@ public: CollectionValidation::RepairMode::kNone, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -3617,7 +3617,7 @@ public: CollectionValidation::RepairMode::kNone, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -3646,7 +3646,7 @@ public: CollectionValidation::RepairMode::kFixErrors, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -3676,7 +3676,7 @@ public: CollectionValidation::RepairMode::kFixErrors, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -3823,7 +3823,7 @@ public: CollectionValidation::RepairMode::kNone, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -3852,7 +3852,7 @@ public: CollectionValidation::RepairMode::kFixErrors, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -3881,7 +3881,7 @@ public: CollectionValidation::RepairMode::kNone, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -3987,7 +3987,7 @@ public: CollectionValidation::RepairMode::kAdjustMultikey, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -4020,7 +4020,7 @@ public: CollectionValidation::RepairMode::kAdjustMultikey, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -4082,7 +4082,7 @@ public: CollectionValidation::RepairMode::kNone, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -4180,7 +4180,7 @@ public: CollectionValidation::RepairMode::kNone, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -4275,7 +4275,7 @@ public: CollectionValidation::RepairMode::kNone, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -4306,7 +4306,7 @@ public: CollectionValidation::RepairMode::kFixErrors, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); @@ -4426,7 +4426,7 @@ public: CollectionValidation::RepairMode::kNone, &results, &output, - kTurnOnExtraLoggingForTest)); + kLogDiagnostics)); ScopeGuard dumpOnErrorGuard([&] { StorageDebugUtil::printValidateResults(results); diff --git a/src/mongo/dbtests/wildcard_multikey_persistence_test.cpp b/src/mongo/dbtests/wildcard_multikey_persistence_test.cpp index c57eb9ac4b8..cc4fd043dd2 100644 --- a/src/mongo/dbtests/wildcard_multikey_persistence_test.cpp +++ b/src/mongo/dbtests/wildcard_multikey_persistence_test.cpp @@ -137,7 +137,7 @@ protected: * Verifes that the index access method associated with 'indexName' in the collection identified * by 'nss' reports 'expectedPaths' as the set of multikey paths. */ - void assertMultikeyPathSetEquals(const std::set<std::string>& expectedPaths, + void assertMultikeyPathSetEquals(const OrderedPathSet& expectedPaths, const NamespaceString& nss = kDefaultNSS, const std::string& indexName = kDefaultIndexName) { // Convert the set of std::string to a set of FieldRef. |
