summaryrefslogtreecommitdiff
path: root/src/mongo
diff options
context:
space:
mode:
authorBenety Goh <benety@mongodb.com>2020-10-27 13:26:56 -0400
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2020-10-27 17:43:31 +0000
commit6419fd81bdefa91390303fdda48457c8a485399b (patch)
tree2b4037d799726873daec53eda9b47f0c601ffc90 /src/mongo
parent010abc222b8dbb0504c26a3a1cc38509e9d7c068 (diff)
downloadmongo-6419fd81bdefa91390303fdda48457c8a485399b.tar.gz
SERVER-51733 ShardLocal::createIndexOnConfig() falls back on hybrid index build if collection is not empty
Diffstat (limited to 'src/mongo')
-rw-r--r--src/mongo/db/s/shard_local.cpp34
-rw-r--r--src/mongo/db/s/shard_local_test.cpp8
2 files changed, 31 insertions, 11 deletions
diff --git a/src/mongo/db/s/shard_local.cpp b/src/mongo/db/s/shard_local.cpp
index 9d6c9a2d4df..fd48ebdad78 100644
--- a/src/mongo/db/s/shard_local.cpp
+++ b/src/mongo/db/s/shard_local.cpp
@@ -27,6 +27,8 @@
* it in the license file.
*/
+#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kSharding
+
#include "mongo/platform/basic.h"
#include "mongo/db/s/shard_local.h"
@@ -41,6 +43,7 @@
#include "mongo/db/repl/repl_set_config.h"
#include "mongo/db/repl/replication_coordinator.h"
#include "mongo/db/server_options.h"
+#include "mongo/logv2/log.h"
#include "mongo/util/assert_util.h"
#include "mongo/util/scopeguard.h"
@@ -161,14 +164,29 @@ Status ShardLocal::createIndexOnConfig(OperationContext* opCtx,
return Status::OK();
}
- writeConflictRetry(opCtx, "ShardLocal::createIndexOnConfig", ns.ns(), [&] {
- WriteUnitOfWork wunit(opCtx);
- auto fromMigrate = true;
- CollectionWriter collWriter(opCtx, collection->uuid());
- IndexBuildsCoordinator::get(opCtx)->createIndexesOnEmptyCollection(
- opCtx, collWriter, indexSpecs, fromMigrate);
- wunit.commit();
- });
+ auto fromMigrate = true;
+ if (!collection->isEmpty(opCtx)) {
+ // We typically create indexes on config/admin collections for sharding while setting up
+ // a sharded cluster, so we do not expect to see data in the collection.
+ // Therefore, it is ok to log this index build.
+ const auto& indexSpec = indexSpecs[0];
+ LOGV2(5173300,
+ "Creating index on sharding collection with existing data",
+ "ns"_attr = ns,
+ "uuid"_attr = collection->uuid(),
+ "index"_attr = indexSpec);
+ auto indexConstraints = IndexBuildsManager::IndexConstraints::kEnforce;
+ IndexBuildsCoordinator::get(opCtx)->createIndex(
+ opCtx, collection->uuid(), indexSpec, indexConstraints, fromMigrate);
+ } else {
+ writeConflictRetry(opCtx, "ShardLocal::createIndexOnConfig", ns.ns(), [&] {
+ WriteUnitOfWork wunit(opCtx);
+ CollectionWriter collWriter(opCtx, collection->uuid());
+ IndexBuildsCoordinator::get(opCtx)->createIndexesOnEmptyCollection(
+ opCtx, collWriter, indexSpecs, fromMigrate);
+ wunit.commit();
+ });
+ }
} catch (const DBException& e) {
return e.toStatus();
}
diff --git a/src/mongo/db/s/shard_local_test.cpp b/src/mongo/db/s/shard_local_test.cpp
index c551347b2bf..200043c20df 100644
--- a/src/mongo/db/s/shard_local_test.cpp
+++ b/src/mongo/db/s/shard_local_test.cpp
@@ -41,7 +41,6 @@
#include "mongo/db/service_context_d_test_fixture.h"
#include "mongo/db/write_concern_options.h"
#include "mongo/s/client/shard_registry.h"
-#include "mongo/unittest/death_test.h"
namespace mongo {
namespace {
@@ -272,7 +271,7 @@ TEST_F(ShardLocalTest, CreateIndex) {
ASSERT_EQ(2U, indexes.size());
}
-DEATH_TEST_REGEX_F(ShardLocalTest, CreateIndexNonEmptyCollection, "Invariant failure.*isEmpty") {
+TEST_F(ShardLocalTest, CreateIndexNonEmptyCollection) {
NamespaceString nss("config.foo");
ASSERT_EQUALS(ErrorCodes::NamespaceNotFound, getIndexes(nss).getStatus());
@@ -281,7 +280,10 @@ DEATH_TEST_REGEX_F(ShardLocalTest, CreateIndexNonEmptyCollection, "Invariant fai
DBDirectClient dbDirectClient(_opCtx.get());
dbDirectClient.insert(nss.toString(), BSON("_id" << 1 << "a" << 1));
- _shardLocal->createIndexOnConfig(_opCtx.get(), nss, BSON("a" << 1), false).ignore();
+ auto status = _shardLocal->createIndexOnConfig(_opCtx.get(), nss, BSON("a" << 1), false);
+ ASSERT_OK(status);
+ auto indexes = unittest::assertGet(getIndexes(nss));
+ ASSERT_EQ(2U, indexes.size()) << BSON("indexes" << indexes);
}
} // namespace