diff options
| author | Randolph Tan <randolph@10gen.com> | 2022-09-20 13:44:07 +0000 |
|---|---|---|
| committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2022-09-20 14:49:09 +0000 |
| commit | 2725f810fb72b14c094bd77a8c9cfcca95a0b452 (patch) | |
| tree | 07f2017c6b9f452b313ef2063db6c814c813f81b /src | |
| parent | 48fd6095abaf8d688c1e3e216eadc96262025e17 (diff) | |
| download | mongo-2725f810fb72b14c094bd77a8c9cfcca95a0b452.tar.gz | |
SERVER-69194 Update global index cloner to use _shardsvrInsertGlobalIndexKey
Diffstat (limited to 'src')
| -rw-r--r-- | src/mongo/db/s/SConscript | 1 | ||||
| -rw-r--r-- | src/mongo/db/s/global_index/global_index_cloning_service_test.cpp | 26 | ||||
| -rw-r--r-- | src/mongo/db/s/global_index/global_index_entry.idl | 48 | ||||
| -rw-r--r-- | src/mongo/db/s/global_index/global_index_inserter.cpp | 93 | ||||
| -rw-r--r-- | src/mongo/db/s/global_index/global_index_inserter.h | 1 | ||||
| -rw-r--r-- | src/mongo/db/s/global_index/global_index_inserter_test.cpp | 51 | ||||
| -rw-r--r-- | src/mongo/db/s/global_index/global_index_util.cpp | 4 | ||||
| -rw-r--r-- | src/mongo/db/s/global_index/global_index_util.h | 6 | ||||
| -rw-r--r-- | src/mongo/db/transaction/transaction_api.cpp | 1 |
9 files changed, 75 insertions, 156 deletions
diff --git a/src/mongo/db/s/SConscript b/src/mongo/db/s/SConscript index b67524f1834..3ed98826eee 100644 --- a/src/mongo/db/s/SConscript +++ b/src/mongo/db/s/SConscript @@ -82,7 +82,6 @@ env.Library( 'global_index/global_index_cloner.idl', 'global_index/global_index_cloning_external_state.cpp', 'global_index/global_index_cloning_service.cpp', - 'global_index/global_index_entry.idl', 'global_index/global_index_inserter.cpp', 'global_index/global_index_server_parameters.idl', 'global_index/global_index_util.cpp', diff --git a/src/mongo/db/s/global_index/global_index_cloning_service_test.cpp b/src/mongo/db/s/global_index/global_index_cloning_service_test.cpp index ea56451168c..0eb66087f6f 100644 --- a/src/mongo/db/s/global_index/global_index_cloning_service_test.cpp +++ b/src/mongo/db/s/global_index/global_index_cloning_service_test.cpp @@ -45,6 +45,7 @@ #include "mongo/db/transaction/session_catalog_mongod_transaction_interface_impl.h" #include "mongo/idl/server_parameter_test_util.h" #include "mongo/logv2/log.h" +#include "mongo/s/request_types/sharded_ddl_commands_gen.h" #define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kTest @@ -228,13 +229,11 @@ public: {BSON("_id" << 10 << kSourceShardKey << 20), BSON(_indexKey << 30)}); replaceFetcherResultList(std::move(fetcherResults)); - const auto& indexNs = globalIndexNss(kSourceNss, _indexName); - client.createCollection(indexNs.ns()); - auto all = client.getCollectionInfos(indexNs.db().toString(), - BSON("name" << indexNs.coll().toString())); - - ASSERT_EQ(1, all.size()); - _indexCollectionUUID.emplace(uassertStatusOK(UUID::parse(all.front()["info"]["uuid"]))); + CreateGlobalIndex createGlobalIndex(_indexCollectionUUID); + createGlobalIndex.setDbName({boost::none, "admin"}); + BSONObj cmdResult; + auto success = client.runCommand("admin", createGlobalIndex.toBSON({}), cmdResult); + ASSERT(success) << "createGlobalIndex cmd failed with result: " << cmdResult; } void checkIndexCollection(OperationContext* opCtx) { @@ -242,18 +241,18 @@ public: MockGlobalIndexClonerFetcher fetcherCopy(_fetcherCopyForVerification); while (auto next = fetcherCopy.getNext(opCtx)) { - FindCommandRequest query(globalIndexNss(kSourceNss, _indexName)); - query.setFilter(BSON("_id" << next->indexKeyValues)); + FindCommandRequest query(NamespaceString::makeGlobalIndexNSS(_indexCollectionUUID)); + query.setFilter(BSON("_id" << next->documentKey)); auto doc = client.findOne(query); ASSERT_TRUE(!doc.isEmpty()) - << "doc with index key: " << next->indexKeyValues + << "doc with document key: " << next->documentKey << " missing in global index output collection: " << dumpOutputColl(opCtx); } } GlobalIndexClonerDoc makeStateDocument() { - return GlobalIndexClonerDoc(*_indexCollectionUUID, + return GlobalIndexClonerDoc(_indexCollectionUUID, kSourceNss, _collectionUUID, _indexName, @@ -296,7 +295,7 @@ public: private: std::string dumpOutputColl(OperationContext* opCtx) { DBDirectClient client(opCtx); - FindCommandRequest query(globalIndexNss(kSourceNss, _indexName)); + FindCommandRequest query(NamespaceString::makeGlobalIndexNSS(_indexCollectionUUID)); std::ostringstream outputStr; auto res = client.find(query); @@ -314,11 +313,12 @@ private: return outputStr.str(); } - boost::optional<UUID> _indexCollectionUUID; + const UUID _indexCollectionUUID{UUID::gen()}; const UUID _collectionUUID{UUID::gen()}; const std::string _indexName{"global_x_1"}; const StringData _indexKey{"x"}; const BSONObj _indexSpec{BSON("key" << BSON(_indexKey << 1) << "unique" << true)}; + const RAIIServerParameterControllerForTest _enableFeature{"featureFlagGlobalIndexes", true}; ReadWriteConcernDefaultsLookupMock _lookupMock; std::shared_ptr<StateTransitionController> _stateTransitionController; diff --git a/src/mongo/db/s/global_index/global_index_entry.idl b/src/mongo/db/s/global_index/global_index_entry.idl deleted file mode 100644 index 0334c46577b..00000000000 --- a/src/mongo/db/s/global_index/global_index_entry.idl +++ /dev/null @@ -1,48 +0,0 @@ -# Copyright (C) 2022-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. -# - -# This file contains the schema for the document stored in the global index collection. - -global: - cpp_namespace: "mongo::global_index" - -imports: - - "mongo/db/basic_types.idl" - -structs: - GlobalIndexEntry: - description: "The document describing the index entry for global indexes." - strict: false - fields: - _id: - type: object - description: "The index representation of the original document." - cpp_name: "indexKeyValue" - documentKey: - type: object - description: "The union of _id and shard key values of the original document." diff --git a/src/mongo/db/s/global_index/global_index_inserter.cpp b/src/mongo/db/s/global_index/global_index_inserter.cpp index ac1a82a8cf4..8db830be1ad 100644 --- a/src/mongo/db/s/global_index/global_index_inserter.cpp +++ b/src/mongo/db/s/global_index/global_index_inserter.cpp @@ -31,8 +31,8 @@ #include <fmt/format.h> -#include "mongo/db/s/global_index/global_index_entry_gen.h" #include "mongo/db/s/global_index/global_index_util.h" +#include "mongo/db/s/global_index_crud_commands_gen.h" #include "mongo/db/transaction/transaction_api.h" #include "mongo/logv2/log.h" #include "mongo/util/fail_point.h" @@ -60,57 +60,52 @@ NamespaceString GlobalIndexInserter::_skipIdNss() { return skipIdNss(_nss, _indexName); } -NamespaceString GlobalIndexInserter::_globalIndexNss() { - return globalIndexNss(_nss, _indexName); -} - void GlobalIndexInserter::processDoc(OperationContext* opCtx, const BSONObj& indexKeyValues, const BSONObj& documentKey) { - auto insertToGlobalIndexFn = [this, - service = opCtx->getServiceContext(), - indexKeyValues, - documentKey](const txn_api::TransactionClient& txnClient, - ExecutorPtr txnExec) { - FindCommandRequest skipIdQuery(_skipIdNss()); - skipIdQuery.setFilter(BSON("_id" << documentKey)); - skipIdQuery.setLimit(1); - - return txnClient.exhaustiveFind(skipIdQuery) - .thenRunOn(txnExec) - .then([this, service, indexKeyValues, documentKey, &txnClient, txnExec]( - const auto& skipIdDocResults) { - auto client = service->makeClient("globalIndexInserter"); - auto opCtx = service->makeOperationContext(client.get()); - - { - stdx::lock_guard<Client> lk(*client); - client->setSystemOperationKillableByStepdown(lk); - } - - globalIndexInserterPauseAfterReadingSkipCollection.pauseWhileSet(opCtx.get()); - - if (!skipIdDocResults.empty()) { - return SemiFuture<void>::makeReady(); - } - - write_ops::InsertCommandRequest globalIndexEntryInsert(_globalIndexNss()); - globalIndexEntryInsert.getWriteCommandRequestBase().setCollectionUUID(_indexUUID); - GlobalIndexEntry indexEntry(indexKeyValues, documentKey); - globalIndexEntryInsert.setDocuments({indexEntry.toBSON()}); - - return txnClient.runCRUDOp({globalIndexEntryInsert}, {}) - .thenRunOn(txnExec) - .then([this, documentKey, &txnClient](const auto& commandResponse) { - write_ops::InsertCommandRequest skipIdInsert(_skipIdNss()); - - skipIdInsert.setDocuments({BSON("_id" << documentKey)}); - return txnClient.runCRUDOp({skipIdInsert}, {}).ignoreValue(); - }) - .semi(); - }) - .semi(); - }; + auto insertToGlobalIndexFn = + [this, service = opCtx->getServiceContext(), indexKeyValues, documentKey]( + const txn_api::TransactionClient& txnClient, ExecutorPtr txnExec) { + FindCommandRequest skipIdQuery(_skipIdNss()); + skipIdQuery.setFilter(BSON("_id" << documentKey)); + skipIdQuery.setLimit(1); + + return txnClient.exhaustiveFind(skipIdQuery) + .thenRunOn(txnExec) + .then([this, service, indexKeyValues, documentKey, &txnClient, txnExec]( + const auto& skipIdDocResults) { + auto client = service->makeClient("globalIndexInserter"); + auto opCtx = service->makeOperationContext(client.get()); + + { + stdx::lock_guard<Client> lk(*client); + client->setSystemOperationKillableByStepdown(lk); + } + + globalIndexInserterPauseAfterReadingSkipCollection.pauseWhileSet(opCtx.get()); + + if (!skipIdDocResults.empty()) { + return SemiFuture<void>::makeReady(); + } + + InsertGlobalIndexKey globalIndexEntryInsert(_indexUUID); + // Note: dbName is unused by command but required by idl. + globalIndexEntryInsert.setDbName({boost::none, "admin"}); + globalIndexEntryInsert.setDocKey(documentKey); + globalIndexEntryInsert.setKey(indexKeyValues); + + return txnClient.runCommand(_nss.db(), globalIndexEntryInsert.toBSON({})) + .thenRunOn(txnExec) + .then([this, documentKey, &txnClient](const auto& commandResponse) { + write_ops::InsertCommandRequest skipIdInsert(_skipIdNss()); + + skipIdInsert.setDocuments({BSON("_id" << documentKey)}); + return txnClient.runCRUDOp({skipIdInsert}, {}).ignoreValue(); + }) + .semi(); + }) + .semi(); + }; txn_api::SyncTransactionWithRetries txn(opCtx, _executor, nullptr); txn.run(opCtx, insertToGlobalIndexFn); diff --git a/src/mongo/db/s/global_index/global_index_inserter.h b/src/mongo/db/s/global_index/global_index_inserter.h index f818715532c..a22f9da0eab 100644 --- a/src/mongo/db/s/global_index/global_index_inserter.h +++ b/src/mongo/db/s/global_index/global_index_inserter.h @@ -57,7 +57,6 @@ public: private: NamespaceString _skipIdNss(); - NamespaceString _globalIndexNss(); const NamespaceString _nss; const std::string _indexName; diff --git a/src/mongo/db/s/global_index/global_index_inserter_test.cpp b/src/mongo/db/s/global_index/global_index_inserter_test.cpp index 51cff39ec44..eb6c6563ca2 100644 --- a/src/mongo/db/s/global_index/global_index_inserter_test.cpp +++ b/src/mongo/db/s/global_index/global_index_inserter_test.cpp @@ -32,7 +32,6 @@ #include "mongo/db/auth/authorization_session.h" #include "mongo/db/dbdirectclient.h" #include "mongo/db/repl/replication_coordinator_mock.h" -#include "mongo/db/s/global_index/global_index_entry_gen.h" #include "mongo/db/s/global_index/global_index_util.h" #include "mongo/db/s/shard_server_test_fixture.h" #include "mongo/db/s/transaction_coordinator_service.h" @@ -40,7 +39,9 @@ #include "mongo/db/session/session_catalog_mongod.h" #include "mongo/executor/network_interface_factory.h" #include "mongo/executor/thread_pool_task_executor.h" +#include "mongo/idl/server_parameter_test_util.h" #include "mongo/logv2/log.h" +#include "mongo/s/request_types/sharded_ddl_commands_gen.h" #include "mongo/util/concurrency/thread_pool.h" #include "mongo/util/fail_point.h" @@ -74,15 +75,11 @@ public: // future will not run unless the clock is advanced manually. _executor = makeTaskExecutorForCloner(); - const auto& indexNs = globalIndexNss(); - client.createCollection(indexNs.ns()); - auto all = client.getCollectionInfos(indexNs.db().toString(), - BSON("name" << indexNs.coll().toString())); - - ASSERT_EQ(1, all.size()); - _indexUUID.emplace(uassertStatusOK(UUID::parse(all.front()["info"]["uuid"]))); - - client.createCollection(skipIdNss().ns()); + CreateGlobalIndex createGlobalIndex(_indexUUID); + createGlobalIndex.setDbName({boost::none, "admin"}); + BSONObj cmdResult; + auto success = client.runCommand("admin", createGlobalIndex.toBSON({}), cmdResult); + ASSERT(success) << "createGlobalIndex cmd failed with result: " << cmdResult; } void tearDown() override { @@ -99,7 +96,7 @@ public: } const UUID& indexUUID() const { - return *_indexUUID; + return _indexUUID; } NamespaceString skipIdNss() const { @@ -107,7 +104,7 @@ public: } NamespaceString globalIndexNss() const { - return NamespaceString(_nss.db(), "{}.globalIndex.{}"_format(_nss.coll(), _indexName)); + return NamespaceString::makeGlobalIndexNSS(indexUUID()); } std::shared_ptr<executor::ThreadPoolTaskExecutor> getExecutor() { @@ -131,8 +128,9 @@ private: const NamespaceString _nss{"test", "user"}; const std::string _indexName{"global_x"}; + const UUID _indexUUID{UUID::gen()}; + const RAIIServerParameterControllerForTest _enableFeature{"featureFlagGlobalIndexes", true}; - boost::optional<UUID> _indexUUID; std::shared_ptr<executor::ThreadPoolTaskExecutor> _executor; }; @@ -144,13 +142,7 @@ TEST_F(GlobalIndexInserterTest, ClonerUpdatesIndexEntryAndSkipIdCollection) { cloner.processDoc(operationContext(), indexKeyValues, documentKey); DBDirectClient client(operationContext()); - - FindCommandRequest indexEntryQuery(globalIndexNss()); - auto indexEntryDoc = client.findOne(indexEntryQuery); - ASSERT_BSONOBJ_EQ(BSON(GlobalIndexEntry::kIndexKeyValueFieldName - << indexKeyValues << GlobalIndexEntry::kDocumentKeyFieldName - << documentKey), - indexEntryDoc); + ASSERT_EQ(1, client.count(globalIndexNss())); FindCommandRequest skipIdQuery(skipIdNss()); auto skipIdDoc = client.findOne(skipIdQuery); @@ -170,9 +162,7 @@ TEST_F(GlobalIndexInserterTest, ClonerSkipsDocumentIfInSkipCollection) { cloner.processDoc(operationContext(), indexKeyValues, documentKey); - FindCommandRequest indexEntryQuery(globalIndexNss()); - auto indexEntryDoc = client.findOne(indexEntryQuery); - ASSERT_TRUE(indexEntryDoc.isEmpty()); + ASSERT_EQ(0, client.count(globalIndexNss())); } TEST_F(GlobalIndexInserterTest, ClonerRetriesWhenItEncountersWCE) { @@ -193,7 +183,6 @@ TEST_F(GlobalIndexInserterTest, ClonerRetriesWhenItEncountersWCE) { fp->waitForTimesEntered(1); write_ops::InsertCommandRequest skipIdInsert(skipIdNss()); - GlobalIndexEntry indexEntry(indexKeyValues, documentKey); skipIdInsert.setDocuments({BSON("_id" << documentKey)}); client.insert(skipIdInsert); @@ -202,9 +191,7 @@ TEST_F(GlobalIndexInserterTest, ClonerRetriesWhenItEncountersWCE) { clonerThread.get(); - FindCommandRequest indexEntryQuery(globalIndexNss()); - auto indexEntryDoc = client.findOne(indexEntryQuery); - ASSERT_TRUE(indexEntryDoc.isEmpty()); + ASSERT_EQ(0, client.count(globalIndexNss())); } TEST_F(GlobalIndexInserterTest, ClonerThrowsIfIndexEntryAlreadyExists) { @@ -212,14 +199,10 @@ TEST_F(GlobalIndexInserterTest, ClonerThrowsIfIndexEntryAlreadyExists) { const auto indexKeyValues = BSON("x" << 34); const auto documentKey = BSON("_id" << 12 << "x" << 34); + const auto documentKey2 = BSON("_id" << 25 << "x" << 34); - DBDirectClient client(operationContext()); - write_ops::InsertCommandRequest globalIndexInsert(globalIndexNss()); - GlobalIndexEntry indexEntry(indexKeyValues, documentKey); - globalIndexInsert.setDocuments({indexEntry.toBSON()}); - client.insert(globalIndexInsert); - - ASSERT_THROWS_CODE(cloner.processDoc(operationContext(), indexKeyValues, documentKey), + cloner.processDoc(operationContext(), indexKeyValues, documentKey); + ASSERT_THROWS_CODE(cloner.processDoc(operationContext(), indexKeyValues, documentKey2), DBException, ErrorCodes::DuplicateKey); } diff --git a/src/mongo/db/s/global_index/global_index_util.cpp b/src/mongo/db/s/global_index/global_index_util.cpp index 306b02ba071..db2a136d3d1 100644 --- a/src/mongo/db/s/global_index/global_index_util.cpp +++ b/src/mongo/db/s/global_index/global_index_util.cpp @@ -41,9 +41,5 @@ NamespaceString skipIdNss(const NamespaceString& nss, StringData indexName) { "{}.globalIndex.{}.skipList"_format(nss.coll(), indexName.toString())); } -NamespaceString globalIndexNss(const NamespaceString& nss, StringData indexName) { - return NamespaceString(nss.db(), "{}.globalIndex.{}"_format(nss.coll(), indexName)); -} - } // namespace global_index } // namespace mongo diff --git a/src/mongo/db/s/global_index/global_index_util.h b/src/mongo/db/s/global_index/global_index_util.h index f3fbc62b000..657520558af 100644 --- a/src/mongo/db/s/global_index/global_index_util.h +++ b/src/mongo/db/s/global_index/global_index_util.h @@ -40,11 +40,5 @@ namespace global_index { */ NamespaceString skipIdNss(const NamespaceString& nss, StringData indexName); -/** - * Returns the namespace of global index collection. This should be removed once the global index - * catalog api is available. - */ -NamespaceString globalIndexNss(const NamespaceString& nss, StringData indexName); - } // namespace global_index } // namespace mongo diff --git a/src/mongo/db/transaction/transaction_api.cpp b/src/mongo/db/transaction/transaction_api.cpp index 38f7fb86eec..36635d16694 100644 --- a/src/mongo/db/transaction/transaction_api.cpp +++ b/src/mongo/db/transaction/transaction_api.cpp @@ -316,6 +316,7 @@ SemiFuture<BSONObj> SEPTransactionClient::runCommand(StringData dbName, BSONObj .then([this](DbResponse dbResponse) { auto reply = rpc::makeReply(&dbResponse.response)->getCommandReply().getOwned(); _hooks->runReplyHook(reply); + uassertStatusOK(getStatusFromCommandResult(reply)); return reply; }) .semi(); |
