summaryrefslogtreecommitdiff
path: root/src/mongo/s/commands
diff options
context:
space:
mode:
authorTommaso Tocci <tommaso.tocci@mongodb.com>2023-03-29 13:28:18 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2023-03-29 14:52:41 +0000
commit581335ec61978fd60d09dd6dbcbc04e4b4e12ee3 (patch)
tree80dde062422010e62eb19c98ef52e5573614f60d /src/mongo/s/commands
parenta302ec402d05a9f381a05f9c4cdcf02ae7dbc16c (diff)
downloadmongo-581335ec61978fd60d09dd6dbcbc04e4b4e12ee3.tar.gz
SERVER-73978 Optimize shards retrieval in CheckMetadataConstistency command
Diffstat (limited to 'src/mongo/s/commands')
-rw-r--r--src/mongo/s/commands/cluster_check_metadata_consistency_cmd.cpp40
1 files changed, 24 insertions, 16 deletions
diff --git a/src/mongo/s/commands/cluster_check_metadata_consistency_cmd.cpp b/src/mongo/s/commands/cluster_check_metadata_consistency_cmd.cpp
index 6babb16601c..4ecf7d49f78 100644
--- a/src/mongo/s/commands/cluster_check_metadata_consistency_cmd.cpp
+++ b/src/mongo/s/commands/cluster_check_metadata_consistency_cmd.cpp
@@ -42,7 +42,6 @@
#include "mongo/s/request_types/sharded_ddl_commands_gen.h"
#include "mongo/s/sharding_feature_flags_gen.h"
-
namespace mongo {
namespace {
@@ -52,6 +51,27 @@ namespace {
// more than 8 decimal digits since the response is at most 16MB, and 16 * 1024 * 1024 < 1 * 10^8.
static const int kPerDocumentOverheadBytesUpperBound = 10;
+/*
+ * Return the set of shards that are primaries for at least one database
+ */
+stdx::unordered_set<ShardId> getAllDbPrimaryShards(OperationContext* opCtx) {
+ static const std::vector<BSONObj> rawPipeline{fromjson(R"({
+ $group: {
+ _id: '$primary'
+ }
+ })")};
+ AggregateCommandRequest aggRequest{NamespaceString::kConfigDatabasesNamespace, rawPipeline};
+ auto aggResponse = Grid::get(opCtx)->catalogClient()->runCatalogAggregation(
+ opCtx, aggRequest, {repl::ReadConcernLevel::kMajorityReadConcern});
+
+ stdx::unordered_set<ShardId> shardIds;
+ shardIds.reserve(aggResponse.size());
+ for (auto&& responseEntry : aggResponse) {
+ shardIds.insert(responseEntry.firstElement().str());
+ }
+ return shardIds;
+}
+
MetadataConsistencyCommandLevelEnum getCommandLevel(const NamespaceString& nss) {
if (nss.isAdminDB()) {
return MetadataConsistencyCommandLevelEnum::kClusterLevel;
@@ -102,25 +122,13 @@ public:
"cluster level mode must be run against the 'admin' database with {aggregate: 1}",
nss.isCollectionlessCursorNamespace());
- const auto catalogClient = Grid::get(opCtx)->catalogClient();
- // TODO: SERVER-73978: Retrieve directly from configsvr a list of shards with its
- // corresponding primary databases.
- const auto databases =
- catalogClient->getAllDBs(opCtx, repl::ReadConcernLevel::kMajorityReadConcern);
-
std::vector<std::pair<ShardId, BSONObj>> requests;
ShardsvrCheckMetadataConsistency shardsvrRequest{nss};
- shardsvrRequest.setDbName(nss.db());
shardsvrRequest.setCursor(request().getCursor());
- // Send a unique request per shard that is a primary shard for, at least, one database.
- std::set<ShardId> shardIds;
- for (const auto& db : databases) {
- const auto insertionRes = shardIds.insert(db.getPrimary());
- if (insertionRes.second) {
- // The shard was not in the set, so we need to send a request to it.
- requests.emplace_back(db.getPrimary(), shardsvrRequest.toBSON({}));
- }
+ // Send a request to all shards that are primaries for at least one database
+ for (auto&& shardId : getAllDbPrimaryShards(opCtx)) {
+ requests.emplace_back(std::move(shardId), shardsvrRequest.toBSON({}));
}
// Send a request to the configsvr to check cluster metadata consistency.