diff options
author | Kaloian Manassiev <kaloian.manassiev@mongodb.com> | 2017-03-12 17:27:43 -0400 |
---|---|---|
committer | Kaloian Manassiev <kaloian.manassiev@mongodb.com> | 2017-03-12 17:28:36 -0400 |
commit | 39e06c9ef8c797ad626956b564ac9ebe295cbaf3 (patch) | |
tree | bfa2742fe1a814980def015b29dc8d5bfaf4bad3 /src/mongo/s/chunk_manager.h | |
parent | 8125c55a251805899552d0af4776930216223703 (diff) | |
download | mongo-39e06c9ef8c797ad626956b564ac9ebe295cbaf3.tar.gz |
SERVER-22611 Sharding catalog cache refactor
Diffstat (limited to 'src/mongo/s/chunk_manager.h')
-rw-r--r-- | src/mongo/s/chunk_manager.h | 103 |
1 files changed, 40 insertions, 63 deletions
diff --git a/src/mongo/s/chunk_manager.h b/src/mongo/s/chunk_manager.h index 365d4d5df62..f1edeefc668 100644 --- a/src/mongo/s/chunk_manager.h +++ b/src/mongo/s/chunk_manager.h @@ -35,8 +35,6 @@ #include "mongo/base/disallow_copying.h" #include "mongo/db/namespace_string.h" #include "mongo/db/query/collation/collator_interface.h" -#include "mongo/db/repl/optime.h" -#include "mongo/s/catalog/type_chunk.h" #include "mongo/s/chunk.h" #include "mongo/s/chunk_version.h" #include "mongo/s/client/shard.h" @@ -60,10 +58,11 @@ class ChunkManager { public: ChunkManager(NamespaceString nss, - const OID& epoch, - const ShardKeyPattern& shardKeyPattern, + KeyPattern shardKeyPattern, std::unique_ptr<CollatorInterface> defaultCollator, - bool unique); + bool unique, + ChunkMap chunkMap, + ChunkVersion collectionVersion); ~ChunkManager(); @@ -79,7 +78,7 @@ public: } const ShardKeyPattern& getShardKeyPattern() const { - return _keyPattern; + return _shardKeyPattern; } const CollatorInterface* getDefaultCollator() const { @@ -91,10 +90,12 @@ public: } ChunkVersion getVersion() const { - return _version; + return _collectionVersion; } - const ChunkMap& getChunkMap() const { + ChunkVersion getVersion(const ShardId& shardId) const; + + const ChunkMap& chunkMap() const { return _chunkMap; } @@ -102,12 +103,9 @@ public: return _chunkMap.size(); } - // Loads existing ranges based on info in chunk manager - void loadExistingRanges(OperationContext* opCtx, const ChunkManager* oldManager); - - // - // Methods to use once loaded / created - // + const ShardVersionMap& shardVersions() const { + return _chunkMapViews.shardVersions; + } /** * Given a shard key (or a prefix) that has been extracted from a document, returns the chunk @@ -177,57 +175,46 @@ public: std::string toString() const; - ChunkVersion getVersion(const ShardId& shardName) const; - - /** - * Returns the opTime of config server the last time chunks were loaded. - */ - repl::OpTime getConfigOpTime() const; - private: + friend class CollectionRoutingDataLoader; + /** * Represents a range of chunk keys [getMin(), getMax()) and the id of the shard on which they * reside according to the metadata. */ - class ShardAndChunkRange { - public: - ShardAndChunkRange(const BSONObj& min, const BSONObj& max, ShardId inShardId) - : _range(min, max), _shardId(std::move(inShardId)) {} - - const BSONObj& getMin() const { - return _range.getMin(); - } - - const BSONObj& getMax() const { - return _range.getMax(); + struct ShardAndChunkRange { + const BSONObj& min() const { + return range.getMin(); } - const ShardId& getShardId() const { - return _shardId; + const BSONObj& max() const { + return range.getMax(); } - private: - ChunkRange _range; - ShardId _shardId; + ChunkRange range; + ShardId shardId; }; using ChunkRangeMap = BSONObjIndexedMap<ShardAndChunkRange>; /** - * If load was successful, returns true and it is guaranteed that the _chunkMap and - * _chunkRangeMap are consistent with each other. If false is returned, it is not safe to use - * the chunk manager anymore. + * Contains different transformations of the chunk map for efficient querying */ - bool _load(OperationContext* opCtx, - ChunkMap& chunks, - std::set<ShardId>& shardIds, - ShardVersionMap* shardVersions, - const ChunkManager* oldManager); + struct ChunkMapViews { + // Transformation of the chunk map containing what range of keys reside on which shard. The + // index is the max key of the respective range and the union of all ranges in a such + // constructed map must cover the complete space from [MinKey, MaxKey). + const ChunkRangeMap chunkRangeMap; + + // Map from shard id to the maximum chunk version for that shard. If a shard contains no + // chunks, it won't be present in this map. + const ShardVersionMap shardVersions; + }; /** - * Merges consecutive chunks, which reside on the same shard into a single range. + * Does a single pass over the chunkMap and constructs the ChunkMapViews object. */ - static ChunkRangeMap _constructRanges(const ChunkMap& chunkMap); + static ChunkMapViews _constructChunkMapViews(const OID& epoch, const ChunkMap& chunkMap); // The shard versioning mechanism hinges on keeping track of the number of times we reload // ChunkManagers. @@ -237,7 +224,7 @@ private: const NamespaceString _nss; // The key pattern used to shard the collection - const ShardKeyPattern _keyPattern; + const ShardKeyPattern _shardKeyPattern; // Default collation to use for routing data queries for this collection const std::unique_ptr<CollatorInterface> _defaultCollator; @@ -247,23 +234,15 @@ private: // Map from the max for each chunk to an entry describing the chunk. The union of all chunks' // ranges must cover the complete space from [MinKey, MaxKey). - ChunkMap _chunkMap; - - // Transformation of the chunk map containing what range of keys reside on which shard. The - // index is the max key of the respective range and the union of all ranges in a such - // constructed map must cover the complete space from [MinKey, MaxKey). - ChunkRangeMap _chunkRangeMap; + const ChunkMap _chunkMap; - // Max known version per shard - ShardVersionMap _shardVersions; + // Different transformations of the chunk map for efficient querying + const ChunkMapViews _chunkMapViews; // Max version across all chunks - ChunkVersion _version; + const ChunkVersion _collectionVersion; - // OpTime of config server the last time chunks were loaded. - repl::OpTime _configOpTime; - - // Auto-split throttling state + // Auto-split throttling state (state mutable by write commands) struct AutoSplitThrottle { public: AutoSplitThrottle() : _splitTickets(maxParallelSplits) {} @@ -280,8 +259,6 @@ private: ChunkManager*, Chunk*, long); - - friend class TestableChunkManager; }; } // namespace mongo |