diff options
-rw-r--r-- | src/mongo/db/s/collection_metadata.cpp | 5 | ||||
-rw-r--r-- | src/mongo/db/s/collection_metadata.h | 18 | ||||
-rw-r--r-- | src/mongo/db/s/metadata_manager.cpp | 16 | ||||
-rw-r--r-- | src/mongo/db/s/metadata_manager.h | 3 | ||||
-rw-r--r-- | src/mongo/db/s/metadata_manager_test.cpp | 10 |
5 files changed, 36 insertions, 16 deletions
diff --git a/src/mongo/db/s/collection_metadata.cpp b/src/mongo/db/s/collection_metadata.cpp index 5c16ace947b..8b713dd0cbd 100644 --- a/src/mongo/db/s/collection_metadata.cpp +++ b/src/mongo/db/s/collection_metadata.cpp @@ -48,6 +48,11 @@ using str::stream; CollectionMetadata::CollectionMetadata() = default; +CollectionMetadata::CollectionMetadata(const BSONObj& keyPattern, ChunkVersion collectionVersion) + : _collVersion(collectionVersion), + _shardVersion(ChunkVersion(0, 0, collectionVersion.epoch())), + _keyPattern(keyPattern.getOwned()) {} + CollectionMetadata::~CollectionMetadata() = default; std::unique_ptr<CollectionMetadata> CollectionMetadata::cloneMigrate( diff --git a/src/mongo/db/s/collection_metadata.h b/src/mongo/db/s/collection_metadata.h index a84eeb61849..df0212ca6a4 100644 --- a/src/mongo/db/s/collection_metadata.h +++ b/src/mongo/db/s/collection_metadata.h @@ -57,13 +57,13 @@ class CollectionMetadata { public: /** - * Use the MetadataLoader to fill the empty metadata from the config server, or use - * clone*() methods to use existing metadatas to build new ones. + * The main way to construct CollectionMetadata is through MetadataLoader or the clone*() + * methods. * - * Unless you are the MetadataLoader or a test you should probably not be using this - * directly. + * The constructors should not be used directly outside of tests. */ CollectionMetadata(); + CollectionMetadata(const BSONObj& keyPattern, ChunkVersion collectionVersion); ~CollectionMetadata(); /** @@ -230,6 +230,11 @@ public: const BSONObj& maxKey, const ChunkVersion& newShardVersion) const; + /** + * Returns true if this metadata was loaded with all necessary information. + */ + bool isValid() const; + private: // Effectively, the MetadataLoader is this class's builder. So we open an exception and grant it // friendship. @@ -269,11 +274,6 @@ private: RangeMap _rangesMap; /** - * Returns true if this metadata was loaded with all necessary information. - */ - bool isValid() const; - - /** * Try to find chunks that are adjacent and record these intervals in the _rangesMap */ void fillRanges(); diff --git a/src/mongo/db/s/metadata_manager.cpp b/src/mongo/db/s/metadata_manager.cpp index 457d1e9ae29..e815e2ef50d 100644 --- a/src/mongo/db/s/metadata_manager.cpp +++ b/src/mongo/db/s/metadata_manager.cpp @@ -34,7 +34,8 @@ namespace mongo { -MetadataManager::MetadataManager() = default; +MetadataManager::MetadataManager() + : _activeMetadataTracker(stdx::make_unique<CollectionMetadataTracker>(nullptr)) {} MetadataManager::~MetadataManager() { stdx::lock_guard<stdx::mutex> scopedLock(_managerLock); @@ -46,27 +47,34 @@ ScopedCollectionMetadata MetadataManager::getActiveMetadata() { if (!_activeMetadataTracker) { return ScopedCollectionMetadata(); } + return ScopedCollectionMetadata(this, _activeMetadataTracker.get()); } void MetadataManager::setActiveMetadata(std::unique_ptr<CollectionMetadata> newMetadata) { + invariant(!newMetadata || newMetadata->isValid()); + stdx::lock_guard<stdx::mutex> scopedLock(_managerLock); - if (_activeMetadataTracker && _activeMetadataTracker->usageCounter > 0) { + + if (_activeMetadataTracker->usageCounter > 0) { _metadataInUse.push_front(std::move(_activeMetadataTracker)); } + _activeMetadataTracker = stdx::make_unique<CollectionMetadataTracker>(std::move(newMetadata)); } void MetadataManager::_removeMetadata_inlock(CollectionMetadataTracker* metadataTracker) { invariant(metadataTracker->usageCounter == 0); + auto i = _metadataInUse.begin(); - auto e = _metadataInUse.end(); + const auto e = _metadataInUse.end(); while (i != e) { if (metadataTracker == i->get()) { _metadataInUse.erase(i); return; } - i++; + + ++i; } } diff --git a/src/mongo/db/s/metadata_manager.h b/src/mongo/db/s/metadata_manager.h index 04135cef840..5953ccb9fdf 100644 --- a/src/mongo/db/s/metadata_manager.h +++ b/src/mongo/db/s/metadata_manager.h @@ -106,8 +106,11 @@ private: void _addRangeToClean_inlock(const ChunkRange& range); void _removeRangeToClean_inlock(const ChunkRange& range); + // Holds the collection metadata, which is currently active std::unique_ptr<CollectionMetadataTracker> _activeMetadataTracker; + // Holds collection metadata instances, which have previously been active, but are still in use + // by still active server operations or cursors std::list<std::unique_ptr<CollectionMetadataTracker>> _metadataInUse; // Contains the information of which ranges of sharding keys need to diff --git a/src/mongo/db/s/metadata_manager_test.cpp b/src/mongo/db/s/metadata_manager_test.cpp index 355c4c020d4..45cff494785 100644 --- a/src/mongo/db/s/metadata_manager_test.cpp +++ b/src/mongo/db/s/metadata_manager_test.cpp @@ -44,10 +44,14 @@ using unittest::assertGet; namespace { +std::unique_ptr<CollectionMetadata> makeMetadata() { + return stdx::make_unique<CollectionMetadata>(BSON("key" << 1), ChunkVersion(1, 0, OID::gen())); +} + TEST(MetadataManager, SetAndGetActiveMetadata) { MetadataManager manager; - std::unique_ptr<CollectionMetadata> cm = stdx::make_unique<CollectionMetadata>(); + std::unique_ptr<CollectionMetadata> cm = makeMetadata(); auto cmPtr = cm.get(); manager.setActiveMetadata(std::move(cm)); @@ -58,11 +62,11 @@ TEST(MetadataManager, SetAndGetActiveMetadata) { TEST(MetadataManager, ResetActiveMetadata) { MetadataManager manager; - manager.setActiveMetadata(stdx::make_unique<CollectionMetadata>()); + manager.setActiveMetadata(makeMetadata()); ScopedCollectionMetadata scopedMetadata1 = manager.getActiveMetadata(); - std::unique_ptr<CollectionMetadata> cm2 = stdx::make_unique<CollectionMetadata>(); + std::unique_ptr<CollectionMetadata> cm2 = makeMetadata(); auto cm2Ptr = cm2.get(); manager.setActiveMetadata(std::move(cm2)); |