summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/mongo/db/s/shard_filtering_metadata_refresh.cpp18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/mongo/db/s/shard_filtering_metadata_refresh.cpp b/src/mongo/db/s/shard_filtering_metadata_refresh.cpp
index 67aac77d941..c9270415e48 100644
--- a/src/mongo/db/s/shard_filtering_metadata_refresh.cpp
+++ b/src/mongo/db/s/shard_filtering_metadata_refresh.cpp
@@ -133,6 +133,24 @@ Status refreshDbMetadata(OperationContext* opCtx,
const auto swDbMetadata =
Grid::get(opCtx)->catalogCache()->getDatabaseWithRefresh(opCtx, dbName.db());
+ // Before setting the database metadata, exit early if the database version received by the
+ // config server is not newer than the cached one. This is a best-effort optimization to reduce
+ // the number of possible threads convoying on the exclusive lock below.
+ {
+ Lock::DBLock dbLock(opCtx, dbName, MODE_IS);
+ const auto cachedDbVersion = DatabaseHolder::get(opCtx)->getDbVersion(opCtx, dbName);
+ if (swDbMetadata.isOK() && swDbMetadata.getValue()->getVersion() <= cachedDbVersion) {
+ LOGV2_DEBUG(7079300,
+ 2,
+ "Skip setting cached database metadata as there are no updates",
+ "db"_attr = dbName,
+ "cachedDbVersion"_attr = *cachedDbVersion,
+ "refreshedDbVersion"_attr = swDbMetadata.getValue()->getVersion());
+
+ return Status::OK();
+ }
+ }
+
Lock::DBLock dbLock(opCtx, dbName, MODE_X);
auto* dss = DatabaseShardingState::get(opCtx, dbName.db());
const auto dssLock = DatabaseShardingState::DSSLock::lockExclusive(opCtx, dss);