diff options
-rw-r--r-- | jstests/sharding/sharding_statistics_server_status.js | 18 | ||||
-rw-r--r-- | src/mongo/db/s/sharding_server_status.cpp | 27 |
2 files changed, 36 insertions, 9 deletions
diff --git a/jstests/sharding/sharding_statistics_server_status.js b/jstests/sharding/sharding_statistics_server_status.js index acb2c8a18f0..a87fcdeba39 100644 --- a/jstests/sharding/sharding_statistics_server_status.js +++ b/jstests/sharding/sharding_statistics_server_status.js @@ -64,6 +64,13 @@ function checkServerStatusAbortedMigrationCount(shardConn, count) { assert.eq(count, shardStats.countDonorMoveChunkAbortConflictingIndexOperation); } +function checkServerStatusNumShardedCollections(conn, count) { + const shardStats = + assert.commandWorked(conn.adminCommand({serverStatus: 1})).shardingStatistics; + assert(shardStats.hasOwnProperty("numShardedCollections")); + assert.eq(count, shardStats.numShardedCollections); +} + function runConcurrentMoveChunk(host, ns, toShard) { const mongos = new Mongo(host); let result = mongos.adminCommand({moveChunk: ns, find: {_id: 1}, to: toShard}); @@ -100,6 +107,17 @@ st.ensurePrimaryShard(coll.getDB() + "", st.shard0.shardName); assert.commandWorked(admin.runCommand({shardCollection: coll + "", key: {_id: 1}})); assert.commandWorked(admin.runCommand({split: coll + "", middle: {_id: 0}})); +// Check the number of sharded collections. +const testDB = st.rs0.getPrimary().getDB(dbName); +const fcvDoc = testDB.adminCommand({getParameter: 1, featureCompatibilityVersion: 1}); +if (MongoRunner.compareBinVersions(fcvDoc.featureCompatibilityVersion.version, '4.4') >= 0) { + st.shardColl(dbName + ".coll2", {_id: 1}, false); + st.shardColl(dbName + ".coll3", {_id: 1}, false); + const configCollections = mongos.getCollection("config.collections"); + checkServerStatusNumShardedCollections(st.configRS.getPrimary(), + configCollections.countDocuments({})); +} + // Move chunk from shard0 to shard1 without docs. assert.commandWorked( mongos.adminCommand({moveChunk: coll + '', find: {_id: 1}, to: st.shard1.shardName})); diff --git a/src/mongo/db/s/sharding_server_status.cpp b/src/mongo/db/s/sharding_server_status.cpp index ad6b17b25c5..e17d02dac5c 100644 --- a/src/mongo/db/s/sharding_server_status.cpp +++ b/src/mongo/db/s/sharding_server_status.cpp @@ -31,6 +31,7 @@ #include "mongo/bson/bsonobjbuilder.h" #include "mongo/db/commands/server_status.h" +#include "mongo/db/db_raii.h" #include "mongo/db/s/active_migrations_registry.h" #include "mongo/db/s/collection_sharding_state.h" #include "mongo/db/s/sharding_state.h" @@ -102,17 +103,25 @@ public: if (!isClusterNode()) return {}; - auto const shardingState = ShardingState::get(opCtx); - if (!shardingState->enabled()) - return {}; + BSONObjBuilder result; + if (auto const shardingState = ShardingState::get(opCtx); shardingState->enabled()) { + auto const grid = Grid::get(opCtx); + auto const catalogCache = grid->catalogCache(); - auto const grid = Grid::get(opCtx); - auto const catalogCache = grid->catalogCache(); + ShardingStatistics::get(opCtx).report(&result); + catalogCache->report(&result); + CollectionShardingState::appendInfoForServerStatus(opCtx, &result); + } - BSONObjBuilder result; - ShardingStatistics::get(opCtx).report(&result); - catalogCache->report(&result); - CollectionShardingState::appendInfoForServerStatus(opCtx, &result); + // To calculate the number of sharded collection we simply get the number of records from + // `config.collections` collection. This count must only be appended when serverStatus is + // invoked on the config server. + if (serverGlobalParams.clusterRole == ClusterRole::ConfigServer) { + AutoGetCollectionForRead autoColl(opCtx, CollectionType::ConfigNS); + const auto& collection = autoColl.getCollection(); + const auto numShardedCollections = collection ? collection->numRecords(opCtx) : 0; + result.appendNumber("numShardedCollections", numShardedCollections); + } return result.obj(); } |