diff options
author | Andy Schwerin <schwerin@mongodb.com> | 2017-06-28 16:52:56 -0400 |
---|---|---|
committer | Andy Schwerin <schwerin@mongodb.com> | 2017-07-12 15:25:22 -0400 |
commit | 4b9d69eb00361083ce835d42c4107a4caa52f6fc (patch) | |
tree | 4cbdb8f69dbe7b8333f6c5ddf05afe662dcae413 /src/mongo/db/s | |
parent | 4c15828d7bd7222fbcb5dc5b3c2060ea2c136dc7 (diff) | |
download | mongo-4b9d69eb00361083ce835d42c4107a4caa52f6fc.tar.gz |
SERVER-29817 Move construction of CollectionMetadata data structures out of ShardingState.
Diffstat (limited to 'src/mongo/db/s')
-rw-r--r-- | src/mongo/db/s/SConscript | 1 | ||||
-rw-r--r-- | src/mongo/db/s/collection_metadata.cpp | 43 | ||||
-rw-r--r-- | src/mongo/db/s/collection_metadata.h | 28 | ||||
-rw-r--r-- | src/mongo/db/s/collection_metadata_test.cpp | 116 | ||||
-rw-r--r-- | src/mongo/db/s/collection_range_deleter_test.cpp | 18 | ||||
-rw-r--r-- | src/mongo/db/s/metadata_manager.h | 7 | ||||
-rw-r--r-- | src/mongo/db/s/metadata_manager_test.cpp | 104 | ||||
-rw-r--r-- | src/mongo/db/s/sharding_state.cpp | 16 |
8 files changed, 152 insertions, 181 deletions
diff --git a/src/mongo/db/s/SConscript b/src/mongo/db/s/SConscript index bfb7f1f0325..947488ae3e0 100644 --- a/src/mongo/db/s/SConscript +++ b/src/mongo/db/s/SConscript @@ -15,6 +15,7 @@ env.Library( '$BUILD_DIR/mongo/db/range_arithmetic', '$BUILD_DIR/mongo/db/service_context', '$BUILD_DIR/mongo/s/common', + '$BUILD_DIR/mongo/s/routing_table', ], ) diff --git a/src/mongo/db/s/collection_metadata.cpp b/src/mongo/db/s/collection_metadata.cpp index 9813a45275b..6c05e401661 100644 --- a/src/mongo/db/s/collection_metadata.cpp +++ b/src/mongo/db/s/collection_metadata.cpp @@ -41,20 +41,22 @@ namespace mongo { -CollectionMetadata::CollectionMetadata(const BSONObj& keyPattern, - ChunkVersion collectionVersion, - ChunkVersion shardVersion, - RangeMap shardChunksMap) - : _shardKeyPattern(keyPattern), - _collVersion(collectionVersion), - _shardVersion(shardVersion), - _chunksMap(std::move(shardChunksMap)), +CollectionMetadata::CollectionMetadata(std::shared_ptr<ChunkManager> cm, const ShardId& thisShardId) + : _cm(cm), + _thisShardId(thisShardId), + _shardVersion(_cm->getVersion(thisShardId)), + _chunksMap(SimpleBSONObjComparator::kInstance.makeBSONObjIndexedMap<CachedChunkInfo>()), _rangesMap(SimpleBSONObjComparator::kInstance.makeBSONObjIndexedMap<CachedChunkInfo>()) { - invariant(_shardKeyPattern.isValid()); - invariant(_collVersion.epoch() == _shardVersion.epoch()); - invariant(_collVersion.isSet()); - invariant(_collVersion >= _shardVersion); + invariant(_cm->getVersion().isSet()); + invariant(_cm->getVersion() >= _shardVersion); + + for (const auto& chunk : _cm->chunks()) { + if (chunk->getShardId() != _thisShardId) + continue; + + _chunksMap.emplace(chunk->getMin(), CachedChunkInfo(chunk->getMax(), chunk->getLastmod())); + } if (_chunksMap.empty()) { invariant(!_shardVersion.isSet()); @@ -104,8 +106,7 @@ void CollectionMetadata::_buildRangesMap() { } std::unique_ptr<CollectionMetadata> CollectionMetadata::clone() const { - return stdx::make_unique<CollectionMetadata>( - _shardKeyPattern.toBSON(), getCollVersion(), getShardVersion(), getChunks()); + return stdx::make_unique<CollectionMetadata>(_cm, _thisShardId); } bool CollectionMetadata::keyBelongsToMe(const BSONObj& key) const { @@ -193,9 +194,9 @@ bool CollectionMetadata::rangeOverlapsChunk(ChunkRange const& range) { } void CollectionMetadata::toBSONBasic(BSONObjBuilder& bb) const { - _collVersion.addToBSON(bb, "collVersion"); + _cm->getVersion().addToBSON(bb, "collVersion"); _shardVersion.addToBSON(bb, "shardVersion"); - bb.append("keyPattern", _shardKeyPattern.toBSON()); + bb.append("keyPattern", _cm->getShardKeyPattern().toBSON()); } void CollectionMetadata::toBSONChunks(BSONArrayBuilder& bb) const { @@ -211,7 +212,7 @@ void CollectionMetadata::toBSONChunks(BSONArrayBuilder& bb) const { } std::string CollectionMetadata::toStringBasic() const { - return str::stream() << "collection version: " << _collVersion.toString() + return str::stream() << "collection version: " << _cm->getVersion().toString() << ", shard version: " << _shardVersion.toString(); } @@ -253,7 +254,7 @@ boost::optional<KeyRange> CollectionMetadata::getNextOrphanRange( } boost::optional<KeyRange> range = - KeyRange("", getMinKey(), maxKey, _shardKeyPattern.toBSON()); + KeyRange("", getMinKey(), maxKey, _cm->getShardKeyPattern().toBSON()); auto patchArgRange = [&range](RangeMap const& map, Its its) { // We know that the lookup key is not covered by a chunk or pending range, and where the @@ -278,15 +279,15 @@ boost::optional<KeyRange> CollectionMetadata::getNextOrphanRange( } BSONObj CollectionMetadata::getMinKey() const { - return _shardKeyPattern.getKeyPattern().globalMin(); + return _cm->getShardKeyPattern().getKeyPattern().globalMin(); } BSONObj CollectionMetadata::getMaxKey() const { - return _shardKeyPattern.getKeyPattern().globalMax(); + return _cm->getShardKeyPattern().getKeyPattern().globalMax(); } bool CollectionMetadata::isValidKey(const BSONObj& key) const { - return _shardKeyPattern.isShardKey(key); + return _cm->getShardKeyPattern().isShardKey(key); } } // namespace mongo diff --git a/src/mongo/db/s/collection_metadata.h b/src/mongo/db/s/collection_metadata.h index a290172362d..ae301d90212 100644 --- a/src/mongo/db/s/collection_metadata.h +++ b/src/mongo/db/s/collection_metadata.h @@ -31,6 +31,7 @@ #include "mongo/db/range_arithmetic.h" #include "mongo/db/s/collection_range_deleter.h" #include "mongo/s/catalog/type_chunk.h" +#include "mongo/s/chunk_manager.h" #include "mongo/s/chunk_version.h" #include "mongo/s/shard_key_pattern.h" @@ -55,11 +56,11 @@ class CollectionMetadata { public: /** * The main way to construct CollectionMetadata is through MetadataLoader or clone() methods. + * + * "thisShardId" is the shard identity of this shard for purposes of answering questions like + * "does this key belong to this shard"? */ - CollectionMetadata(const BSONObj& keyPattern, - ChunkVersion collectionVersion, - ChunkVersion shardVersion, - RangeMap shardChunksMap); + CollectionMetadata(std::shared_ptr<ChunkManager> cm, const ShardId& thisShardId); ~CollectionMetadata(); @@ -125,7 +126,7 @@ public: BSONObj const& lookupKey) const; ChunkVersion getCollVersion() const { - return _collVersion; + return _cm->getVersion(); } ChunkVersion getShardVersion() const { @@ -137,11 +138,11 @@ public: } const BSONObj& getKeyPattern() const { - return _shardKeyPattern.toBSON(); + return _cm->getShardKeyPattern().toBSON(); } const std::vector<std::unique_ptr<FieldRef>>& getKeyPatternFields() const { - return _shardKeyPattern.getKeyPatternFields(); + return _cm->getShardKeyPattern().getKeyPatternFields(); } BSONObj getMinKey() const; @@ -167,6 +168,10 @@ public: */ std::string toStringBasic() const; + std::shared_ptr<ChunkManager> getChunkManager() const { + return _cm; + } + private: struct Tracker { uint32_t usageCounter{0}; @@ -179,12 +184,11 @@ private: */ void _buildRangesMap(); - // Shard key pattern for the collection - ShardKeyPattern _shardKeyPattern; + // The full routing table for the collection. + std::shared_ptr<ChunkManager> _cm; - // a version for this collection that identifies the collection incarnation (ie, a - // dropped and recreated collection with the same name would have a different version) - ChunkVersion _collVersion; + // The identity of this shard, for the purpose of answering "key belongs to me" queries. + ShardId _thisShardId; // highest ChunkVersion for which this metadata's information is accurate ChunkVersion _shardVersion; diff --git a/src/mongo/db/s/collection_metadata_test.cpp b/src/mongo/db/s/collection_metadata_test.cpp index 999e5282490..cf5bea78a00 100644 --- a/src/mongo/db/s/collection_metadata_test.cpp +++ b/src/mongo/db/s/collection_metadata_test.cpp @@ -41,16 +41,43 @@ namespace { using unittest::assertGet; +std::unique_ptr<CollectionMetadata> makeCollectionMetadataImpl( + const KeyPattern& shardKeyPattern, + const std::vector<std::pair<BSONObj, BSONObj>>& thisShardsChunks) { + + const OID epoch = OID::gen(); + const NamespaceString kNss("test.foo"); + const ShardId kThisShard("thisShard"); + const ShardId kOtherShard("otherShard"); + + std::vector<ChunkType> allChunks; + auto nextMinKey = shardKeyPattern.globalMin(); + ChunkVersion version{1, 0, epoch}; + for (const auto& myNextChunk : thisShardsChunks) { + if (SimpleBSONObjComparator::kInstance.evaluate(nextMinKey < myNextChunk.first)) { + // Need to add a chunk to the other shard from nextMinKey to myNextChunk.first. + allChunks.emplace_back( + kNss, ChunkRange{nextMinKey, myNextChunk.first}, version, kOtherShard); + version.incMajor(); + } + allChunks.emplace_back( + kNss, ChunkRange{myNextChunk.first, myNextChunk.second}, version, kThisShard); + version.incMajor(); + nextMinKey = myNextChunk.second; + } + if (SimpleBSONObjComparator::kInstance.evaluate(nextMinKey < shardKeyPattern.globalMax())) { + allChunks.emplace_back( + kNss, ChunkRange{nextMinKey, shardKeyPattern.globalMax()}, version, kOtherShard); + } + + auto cm = ChunkManager::makeNew(kNss, shardKeyPattern, nullptr, false, epoch, allChunks); + return stdx::make_unique<CollectionMetadata>(cm, kThisShard); +} + class NoChunkFixture : public unittest::Test { protected: std::unique_ptr<CollectionMetadata> makeCollectionMetadata() const { - const OID epoch = OID::gen(); - - return stdx::make_unique<CollectionMetadata>( - BSON("a" << 1), - ChunkVersion(1, 0, epoch), - ChunkVersion(0, 0, epoch), - SimpleBSONObjComparator::kInstance.makeBSONObjIndexedMap<CachedChunkInfo>()); + return makeCollectionMetadataImpl(KeyPattern(BSON("a" << 1)), {}); } }; @@ -137,17 +164,8 @@ TEST_F(NoChunkFixture, OrphanedDataRangeEnd) { class SingleChunkFixture : public unittest::Test { protected: std::unique_ptr<CollectionMetadata> makeCollectionMetadata() const { - const OID epoch = OID::gen(); - - auto shardChunksMap = - SimpleBSONObjComparator::kInstance.makeBSONObjIndexedMap<CachedChunkInfo>(); - shardChunksMap.emplace(BSON("a" << 10), - CachedChunkInfo(BSON("a" << 20), ChunkVersion(1, 0, epoch))); - - return stdx::make_unique<CollectionMetadata>(BSON("a" << 1), - ChunkVersion(1, 0, epoch), - ChunkVersion(1, 0, epoch), - std::move(shardChunksMap)); + return makeCollectionMetadataImpl(KeyPattern(BSON("a" << 1)), + {std::make_pair(BSON("a" << 10), BSON("a" << 20))}); } }; @@ -215,18 +233,10 @@ TEST_F(SingleChunkFixture, ChunkOrphanedDataRanges) { class SingleChunkMinMaxCompoundKeyFixture : public unittest::Test { protected: std::unique_ptr<CollectionMetadata> makeCollectionMetadata() const { - const OID epoch = OID::gen(); - - auto shardChunksMap = - SimpleBSONObjComparator::kInstance.makeBSONObjIndexedMap<CachedChunkInfo>(); - shardChunksMap.emplace( - BSON("a" << MINKEY << "b" << MINKEY), - CachedChunkInfo(BSON("a" << MAXKEY << "b" << MAXKEY), ChunkVersion(1, 0, epoch))); - - return stdx::make_unique<CollectionMetadata>(BSON("a" << 1 << "b" << 1), - ChunkVersion(1, 0, epoch), - ChunkVersion(1, 0, epoch), - std::move(shardChunksMap)); + const KeyPattern shardKeyPattern(BSON("a" << 1 << "b" << 1)); + return makeCollectionMetadataImpl( + shardKeyPattern, + {std::make_pair(shardKeyPattern.globalMin(), shardKeyPattern.globalMax())}); } }; @@ -247,21 +257,10 @@ TEST_F(SingleChunkMinMaxCompoundKeyFixture, CompoundKeyBelongsToMe) { class TwoChunksWithGapCompoundKeyFixture : public unittest::Test { protected: std::unique_ptr<CollectionMetadata> makeCollectionMetadata() const { - const OID epoch = OID::gen(); - - auto shardChunksMap = - SimpleBSONObjComparator::kInstance.makeBSONObjIndexedMap<CachedChunkInfo>(); - shardChunksMap.emplace( - BSON("a" << 10 << "b" << 0), - CachedChunkInfo(BSON("a" << 20 << "b" << 0), ChunkVersion(1, 0, epoch))); - shardChunksMap.emplace( - BSON("a" << 30 << "b" << 0), - CachedChunkInfo(BSON("a" << 40 << "b" << 0), ChunkVersion(1, 1, epoch))); - - return stdx::make_unique<CollectionMetadata>(BSON("a" << 1 << "b" << 1), - ChunkVersion(1, 1, epoch), - ChunkVersion(1, 1, epoch), - std::move(shardChunksMap)); + return makeCollectionMetadataImpl( + KeyPattern(BSON("a" << 1 << "b" << 1)), + {std::make_pair(BSON("a" << 10 << "b" << 0), BSON("a" << 20 << "b" << 0)), + std::make_pair(BSON("a" << 30 << "b" << 0), BSON("a" << 40 << "b" << 0))}); } }; @@ -296,21 +295,11 @@ TEST_F(TwoChunksWithGapCompoundKeyFixture, ChunkGapOrphanedDataRanges) { class ThreeChunkWithRangeGapFixture : public unittest::Test { protected: std::unique_ptr<CollectionMetadata> makeCollectionMetadata() const { - const OID epoch = OID::gen(); - - auto shardChunksMap = - SimpleBSONObjComparator::kInstance.makeBSONObjIndexedMap<CachedChunkInfo>(); - shardChunksMap.emplace(BSON("a" << MINKEY), - CachedChunkInfo(BSON("a" << 10), ChunkVersion(1, 1, epoch))); - shardChunksMap.emplace(BSON("a" << 10), - CachedChunkInfo(BSON("a" << 20), ChunkVersion(1, 3, epoch))); - shardChunksMap.emplace(BSON("a" << 30), - CachedChunkInfo(BSON("a" << MAXKEY), ChunkVersion(1, 2, epoch))); - - return stdx::make_unique<CollectionMetadata>(BSON("a" << 1), - ChunkVersion(1, 3, epoch), - ChunkVersion(1, 3, epoch), - std::move(shardChunksMap)); + + return makeCollectionMetadataImpl(KeyPattern(BSON("a" << 1)), + {std::make_pair(BSON("a" << MINKEY), BSON("a" << 10)), + std::make_pair(BSON("a" << 10), BSON("a" << 20)), + std::make_pair(BSON("a" << 30), BSON("a" << MAXKEY))}); } }; @@ -320,14 +309,14 @@ TEST_F(ThreeChunkWithRangeGapFixture, ChunkVersionsMatch) { ChunkType chunk; ASSERT(metadata->getNextChunk(BSON("a" << MINKEY), &chunk)); - ASSERT_EQ(ChunkVersion(1, 1, metadata->getCollVersion().epoch()), chunk.getVersion()); + ASSERT_EQ(ChunkVersion(1, 0, metadata->getCollVersion().epoch()), chunk.getVersion()); ASSERT_BSONOBJ_EQ(metadata->getMinKey(), chunk.getMin()); ASSERT(metadata->getNextChunk(BSON("a" << 10), &chunk)); - ASSERT_EQ(ChunkVersion(1, 3, metadata->getCollVersion().epoch()), chunk.getVersion()); + ASSERT_EQ(ChunkVersion(2, 0, metadata->getCollVersion().epoch()), chunk.getVersion()); ASSERT(metadata->getNextChunk(BSON("a" << 30), &chunk)); - ASSERT_EQ(ChunkVersion(1, 2, metadata->getCollVersion().epoch()), chunk.getVersion()); + ASSERT_EQ(ChunkVersion(4, 0, metadata->getCollVersion().epoch()), chunk.getVersion()); ASSERT_BSONOBJ_EQ(metadata->getMaxKey(), chunk.getMax()); } @@ -339,6 +328,7 @@ TEST_F(ThreeChunkWithRangeGapFixture, ShardOwnsDoc) { } TEST_F(ThreeChunkWithRangeGapFixture, ShardDoesntOwnDoc) { + ASSERT_FALSE(makeCollectionMetadata()->keyBelongsToMe(BSON("a" << 20))); ASSERT_FALSE(makeCollectionMetadata()->keyBelongsToMe(BSON("a" << 25))); ASSERT_FALSE(makeCollectionMetadata()->keyBelongsToMe(BSON("a" << MAXKEY))); } diff --git a/src/mongo/db/s/collection_range_deleter_test.cpp b/src/mongo/db/s/collection_range_deleter_test.cpp index 943a120f11b..90c91ac1418 100644 --- a/src/mongo/db/s/collection_range_deleter_test.cpp +++ b/src/mongo/db/s/collection_range_deleter_test.cpp @@ -118,13 +118,19 @@ void CollectionRangeDeleterTest::setUp() { { AutoGetCollection autoColl(operationContext(), kNss, MODE_IX); auto collectionShardingState = CollectionShardingState::get(operationContext(), kNss); + const KeyPattern skPattern(kKeyPattern); + auto cm = ChunkManager::makeNew( + kNss, + kKeyPattern, + nullptr, + false, + epoch(), + {ChunkType(kNss, + ChunkRange{skPattern.globalMin(), skPattern.globalMax()}, + ChunkVersion(1, 0, epoch()), + ShardId("otherShard"))}); collectionShardingState->refreshMetadata( - operationContext(), - stdx::make_unique<CollectionMetadata>( - kKeyPattern, - ChunkVersion(1, 0, epoch()), - ChunkVersion(0, 0, epoch()), - SimpleBSONObjComparator::kInstance.makeBSONObjIndexedMap<CachedChunkInfo>())); + operationContext(), stdx::make_unique<CollectionMetadata>(cm, ShardId("thisShard"))); } } diff --git a/src/mongo/db/s/metadata_manager.h b/src/mongo/db/s/metadata_manager.h index 61ad837e9f5..7c22ed83ba6 100644 --- a/src/mongo/db/s/metadata_manager.h +++ b/src/mongo/db/s/metadata_manager.h @@ -83,12 +83,6 @@ public: */ void append(BSONObjBuilder* builder); - /** - * Returns a map to the set of chunks being migrated in. - */ - RangeMap const& getReceiveMap() const { - return _receivingChunks; - } /** * Schedules any documents in `range` for immediate cleanup iff no running queries can depend @@ -211,6 +205,7 @@ private: // data members const NamespaceString _nss; + // ServiceContext from which to obtain instances of global support objects. ServiceContext* const _serviceContext; diff --git a/src/mongo/db/s/metadata_manager_test.cpp b/src/mongo/db/s/metadata_manager_test.cpp index 698d8a05ba7..ee35fe24cce 100644 --- a/src/mongo/db/s/metadata_manager_test.cpp +++ b/src/mongo/db/s/metadata_manager_test.cpp @@ -66,9 +66,11 @@ namespace { using unittest::assertGet; const NamespaceString kNss("TestDB", "TestColl"); -const std::string kPattern = "X"; -const BSONObj kShardKeyPattern{BSON(kPattern << 1)}; -const std::string kShardName{"a"}; +const std::string kPattern = "key"; +const BSONObj kShardKeyPatternBSON{BSON(kPattern << 1)}; +const KeyPattern kShardKeyPattern{kShardKeyPatternBSON}; +const std::string kThisShard{"thisShard"}; +const std::string kOtherShard{"otherShard"}; const HostAndPort dummyHost("dummy", 123); class MetadataManagerTest : public ShardingMongodTestFixture { @@ -107,11 +109,17 @@ protected: static std::unique_ptr<CollectionMetadata> makeEmptyMetadata() { const OID epoch = OID::gen(); - return stdx::make_unique<CollectionMetadata>( - BSON("key" << 1), - ChunkVersion(1, 0, epoch), - ChunkVersion(0, 0, epoch), - SimpleBSONObjComparator::kInstance.makeBSONObjIndexedMap<CachedChunkInfo>()); + auto cm = ChunkManager::makeNew( + kNss, + kShardKeyPattern, + nullptr, + false, + epoch, + {ChunkType{kNss, + ChunkRange{BSON(kPattern << MINKEY), BSON(kPattern << MAXKEY)}, + ChunkVersion(1, 0, epoch), + kOtherShard}}); + return stdx::make_unique<CollectionMetadata>(cm, kThisShard); } /** @@ -123,30 +131,33 @@ protected: * chunk version is lower than the maximum one. */ static std::unique_ptr<CollectionMetadata> cloneMetadataPlusChunk( - const CollectionMetadata& metadata, - const BSONObj& minKey, - const BSONObj& maxKey, - const ChunkVersion& chunkVersion) { - invariant(chunkVersion.isSet()); - invariant(chunkVersion > metadata.getCollVersion()); + const CollectionMetadata& metadata, const BSONObj& minKey, const BSONObj& maxKey) { invariant(minKey.woCompare(maxKey) < 0); invariant(!rangeMapOverlaps(metadata.getChunks(), minKey, maxKey)); - auto chunksMap = metadata.getChunks(); - chunksMap.insert( - std::make_pair(minKey.getOwned(), CachedChunkInfo(maxKey.getOwned(), chunkVersion))); - - return stdx::make_unique<CollectionMetadata>( - metadata.getKeyPattern(), chunkVersion, chunkVersion, std::move(chunksMap)); + auto cm = metadata.getChunkManager(); + auto chunkToSplit = cm->findIntersectingChunkWithSimpleCollation(minKey); + ASSERT(SimpleBSONObjComparator::kInstance.evaluate(maxKey <= chunkToSplit->getMax())) + << "maxKey == " << maxKey + << " and chunkToSplit->getMax() == " << chunkToSplit->getMax(); + auto v1 = cm->getVersion(); + v1.incMajor(); + auto v2 = v1; + v2.incMajor(); + auto v3 = v2; + v3.incMajor(); + cm = cm->makeUpdated( + {ChunkType{kNss, ChunkRange{chunkToSplit->getMin(), minKey}, v1, kOtherShard}, + ChunkType{kNss, ChunkRange{minKey, maxKey}, v2, kThisShard}, + ChunkType{kNss, ChunkRange{maxKey, chunkToSplit->getMax()}, v3, kOtherShard}}); + return stdx::make_unique<CollectionMetadata>(cm, kThisShard); } CollectionMetadata* addChunk(std::shared_ptr<MetadataManager>& manager) { ScopedCollectionMetadata scopedMetadata1 = manager->getActiveMetadata(manager); - ChunkVersion newVersion = scopedMetadata1->getCollVersion(); - newVersion.incMajor(); std::unique_ptr<CollectionMetadata> cm2 = cloneMetadataPlusChunk( - *scopedMetadata1.getMetadata(), BSON("key" << 0), BSON("key" << 20), newVersion); + *scopedMetadata1.getMetadata(), BSON("key" << 0), BSON("key" << 20)); auto cm2Ptr = cm2.get(); manager->refreshActiveMetadata(std::move(cm2)); @@ -263,11 +274,8 @@ TEST_F(MetadataManagerTest, RefreshAfterSuccessfulMigrationSinglePending) { const ChunkRange cr1(BSON("key" << 0), BSON("key" << 10)); ASSERT_EQ(_manager->getActiveMetadata(_manager)->getChunks().size(), 0UL); - ChunkVersion version = _manager->getActiveMetadata(_manager)->getCollVersion(); - version.incMajor(); - _manager->refreshActiveMetadata(cloneMetadataPlusChunk( - *_manager->getActiveMetadata(_manager).getMetadata(), cr1.getMin(), cr1.getMax(), version)); + *_manager->getActiveMetadata(_manager).getMetadata(), cr1.getMin(), cr1.getMax())); ASSERT_EQ(_manager->getActiveMetadata(_manager)->getChunks().size(), 1UL); } @@ -280,27 +288,15 @@ TEST_F(MetadataManagerTest, RefreshAfterSuccessfulMigrationMultiplePending) { ASSERT_EQ(_manager->getActiveMetadata(_manager)->getChunks().size(), 0UL); { - ChunkVersion version = _manager->getActiveMetadata(_manager)->getCollVersion(); - version.incMajor(); - - _manager->refreshActiveMetadata( - cloneMetadataPlusChunk(*_manager->getActiveMetadata(_manager).getMetadata(), - cr1.getMin(), - cr1.getMax(), - version)); + _manager->refreshActiveMetadata(cloneMetadataPlusChunk( + *_manager->getActiveMetadata(_manager).getMetadata(), cr1.getMin(), cr1.getMax())); ASSERT_EQ(_manager->numberOfRangesToClean(), 0UL); ASSERT_EQ(_manager->getActiveMetadata(_manager)->getChunks().size(), 1UL); } { - ChunkVersion version = _manager->getActiveMetadata(_manager)->getCollVersion(); - version.incMajor(); - - _manager->refreshActiveMetadata( - cloneMetadataPlusChunk(*_manager->getActiveMetadata(_manager).getMetadata(), - cr2.getMin(), - cr2.getMax(), - version)); + _manager->refreshActiveMetadata(cloneMetadataPlusChunk( + *_manager->getActiveMetadata(_manager).getMetadata(), cr2.getMin(), cr2.getMax())); ASSERT_EQ(_manager->getActiveMetadata(_manager)->getChunks().size(), 2UL); } } @@ -312,14 +308,10 @@ TEST_F(MetadataManagerTest, RefreshAfterNotYetCompletedMigrationMultiplePending) const ChunkRange cr2(BSON("key" << 30), BSON("key" << 40)); ASSERT_EQ(_manager->getActiveMetadata(_manager)->getChunks().size(), 0UL); - ChunkVersion version = _manager->getActiveMetadata(_manager)->getCollVersion(); - version.incMajor(); - _manager->refreshActiveMetadata( cloneMetadataPlusChunk(*_manager->getActiveMetadata(_manager).getMetadata(), BSON("key" << 50), - BSON("key" << 60), - version)); + BSON("key" << 60))); ASSERT_EQ(_manager->getActiveMetadata(_manager)->getChunks().size(), 1UL); } @@ -338,25 +330,21 @@ TEST_F(MetadataManagerTest, RefreshMetadataAfterDropAndRecreate) { { auto metadata = _manager->getActiveMetadata(_manager); - ChunkVersion newVersion = metadata->getCollVersion(); - newVersion.incMajor(); - - _manager->refreshActiveMetadata(cloneMetadataPlusChunk( - *metadata.getMetadata(), BSON("key" << 0), BSON("key" << 10), newVersion)); + _manager->refreshActiveMetadata( + cloneMetadataPlusChunk(*metadata.getMetadata(), BSON("key" << 0), BSON("key" << 10))); } // Now, pretend that the collection was dropped and recreated auto recreateMetadata = makeEmptyMetadata(); - ChunkVersion newVersion = _manager->getActiveMetadata(_manager)->getCollVersion(); - newVersion.incMajor(); - _manager->refreshActiveMetadata(cloneMetadataPlusChunk( - *recreateMetadata, BSON("key" << 20), BSON("key" << 30), newVersion)); + _manager->refreshActiveMetadata( + cloneMetadataPlusChunk(*recreateMetadata, BSON("key" << 20), BSON("key" << 30))); + ChunkVersion newVersion = _manager->getActiveMetadata(_manager)->getShardVersion(); ASSERT_EQ(_manager->getActiveMetadata(_manager)->getChunks().size(), 1UL); const auto chunkEntry = _manager->getActiveMetadata(_manager)->getChunks().begin(); ASSERT_BSONOBJ_EQ(BSON("key" << 20), chunkEntry->first); ASSERT_BSONOBJ_EQ(BSON("key" << 30), chunkEntry->second.getMaxKey()); - ASSERT_EQ(newVersion, chunkEntry->second.getVersion()); + ASSERT_EQ(newVersion.epoch(), chunkEntry->second.getVersion().epoch()); } // Tests membership functions for _rangesToClean diff --git a/src/mongo/db/s/sharding_state.cpp b/src/mongo/db/s/sharding_state.cpp index 12925694b6c..91a2fc6f9eb 100644 --- a/src/mongo/db/s/sharding_state.cpp +++ b/src/mongo/db/s/sharding_state.cpp @@ -490,21 +490,7 @@ ChunkVersion ShardingState::_refreshMetadata(OperationContext* opCtx, const Name return nullptr; } - RangeMap shardChunksMap = - SimpleBSONObjComparator::kInstance.makeBSONObjIndexedMap<CachedChunkInfo>(); - - for (const auto& chunk : cm->chunks()) { - if (chunk->getShardId() != shardId) - continue; - - shardChunksMap.emplace(chunk->getMin(), - CachedChunkInfo(chunk->getMax(), chunk->getLastmod())); - } - - return stdx::make_unique<CollectionMetadata>(cm->getShardKeyPattern().toBSON(), - cm->getVersion(), - cm->getVersion(shardId), - std::move(shardChunksMap)); + return stdx::make_unique<CollectionMetadata>(cm, shardId); }(); // Exclusive collection lock needed since we're now changing the metadata |