summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAntonio Fuschetto <antonio.fuschetto@mongodb.com>2022-11-09 14:54:35 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-11-09 15:25:40 +0000
commit78d555f1c06d73df273fc0b47ac4d2b69e7a618e (patch)
tree9bb0248ef2394c1a0c004258cfa3136e3a399870
parent7c0dec929a7ed5d98c7cec4983baee9a5eb4a998 (diff)
downloadmongo-78d555f1c06d73df273fc0b47ac4d2b69e7a618e.tar.gz
SERVER-70793 Make database metadata refresh first check new metadata under the IS lock before taking X lock
-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);