summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Pulo <kevin.pulo@mongodb.com>2017-10-18 03:50:44 +0000
committerKevin Pulo <kevin.pulo@mongodb.com>2017-10-20 15:40:17 +1100
commitf26be76fa76156fa13878c7eedb1cc53e79fd029 (patch)
treeeb544618a58c3e491867e049ecc43251f5284b45
parentd8f0d6292967c7f9bdba8890f58acee3a97c247d (diff)
downloadmongo-f26be76fa76156fa13878c7eedb1cc53e79fd029.tar.gz
SERVER-31428 avoid redundant concurrent generation of new chunkMaps
-rw-r--r--src/mongo/db/s/sharding_state.cpp57
1 files changed, 42 insertions, 15 deletions
diff --git a/src/mongo/db/s/sharding_state.cpp b/src/mongo/db/s/sharding_state.cpp
index ed44a6990eb..5a54ef969c9 100644
--- a/src/mongo/db/s/sharding_state.cpp
+++ b/src/mongo/db/s/sharding_state.cpp
@@ -500,30 +500,57 @@ ChunkVersion ShardingState::_refreshMetadata(OperationContext* opCtx, const Name
<< " before shard name has been set",
shardId.isValid());
- auto newCollectionMetadata = [&]() -> std::unique_ptr<CollectionMetadata> {
- auto const catalogCache = Grid::get(opCtx)->catalogCache();
- catalogCache->invalidateShardedCollection(nss);
-
- const auto routingInfo =
- uassertStatusOK(catalogCache->getCollectionRoutingInfo(opCtx, nss));
- const auto cm = routingInfo.cm();
- if (!cm) {
- return nullptr;
- }
+ auto const catalogCache = Grid::get(opCtx)->catalogCache();
+ catalogCache->invalidateShardedCollection(nss);
+
+ const auto routingInfo = uassertStatusOK(catalogCache->getCollectionRoutingInfo(opCtx, nss));
+ const auto cm = routingInfo.cm();
+
+ if (!cm) {
+ // No chunk manager, so unsharded.
- return stdx::make_unique<CollectionMetadata>(cm, shardId);
- }();
+ // Exclusive collection lock needed since we're now changing the metadata
+ AutoGetCollection autoColl(opCtx, nss, MODE_IX, MODE_X);
+
+ auto css = CollectionShardingState::get(opCtx, nss);
+ css->refreshMetadata(opCtx, nullptr);
+
+ return ChunkVersion::UNSHARDED();
+ }
+
+ {
+ AutoGetCollection autoColl(opCtx, nss, MODE_IS);
+ auto css = CollectionShardingState::get(opCtx, nss);
+
+ // We already have newer version
+ if (css->getMetadata() &&
+ css->getMetadata()->getCollVersion().epoch() == cm->getVersion().epoch() &&
+ css->getMetadata()->getCollVersion() >= cm->getVersion()) {
+ LOG(1) << "Skipping refresh of metadata for " << nss << " "
+ << css->getMetadata()->getCollVersion() << " with an older " << cm->getVersion();
+ return css->getMetadata()->getShardVersion();
+ }
+ }
// Exclusive collection lock needed since we're now changing the metadata
AutoGetCollection autoColl(opCtx, nss, MODE_IX, MODE_X);
auto css = CollectionShardingState::get(opCtx, nss);
- css->refreshMetadata(opCtx, std::move(newCollectionMetadata));
- if (!css->getMetadata()) {
- return ChunkVersion::UNSHARDED();
+ // We already have newer version
+ if (css->getMetadata() &&
+ css->getMetadata()->getCollVersion().epoch() == cm->getVersion().epoch() &&
+ css->getMetadata()->getCollVersion() >= cm->getVersion()) {
+ LOG(1) << "Skipping refresh of metadata for " << nss << " "
+ << css->getMetadata()->getCollVersion() << " with an older " << cm->getVersion();
+ return css->getMetadata()->getShardVersion();
}
+ std::unique_ptr<CollectionMetadata> newCollectionMetadata =
+ stdx::make_unique<CollectionMetadata>(cm, shardId);
+
+ css->refreshMetadata(opCtx, std::move(newCollectionMetadata));
+
return css->getMetadata()->getShardVersion();
}