summaryrefslogtreecommitdiff
path: root/src/mongo/db/s/shard_local.cpp
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/db/s/shard_local.cpp
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/db/s/shard_local.cpp')
-rw-r--r--src/mongo/db/s/shard_local.cpp34
1 files changed, 26 insertions, 8 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();
}