diff options
author | Henrik Edin <henrik.edin@mongodb.com> | 2018-05-18 17:29:14 -0400 |
---|---|---|
committer | Henrik Edin <henrik.edin@mongodb.com> | 2018-09-18 16:34:02 -0400 |
commit | 27e0275301eed05bea3d65c766dbe76ee1da9b8a (patch) | |
tree | c744bad9984e66cbce25c1f907f3e203db78fad5 /src/mongo/db/s/collection_sharding_runtime.h | |
parent | df38daf87387969413c66df415601663e63cbb34 (diff) | |
download | mongo-27e0275301eed05bea3d65c766dbe76ee1da9b8a.tar.gz |
SERVER-32198 Get rid of CollectionShardingState::collectionIsSharded
(cherry picked from commit 80de0da37b00dbeed576b28a842cb172b6714358)
SERVER-35773 Remove references to the CatalogCache from MetadataManager
(cherry picked from commit 2aa65a86193e5d38934a4f2d6b0a8298e2432485)
SERVER-32198 Add support for an optional `vWanted` to StaleConfigInfo
(cherry picked from commit 60559a00b81293184922b3418a8e56610edf8dd9)
SERVER-36054 Remove ScopedCollectionMetadata's operator bool
(cherry picked from commit c9c340ad6e9e1f33cb001a8375c62d6b16138c74)
SERVER-36054 Remove more unused methods from CSS/MetadataManager
(cherry picked from commit ca04f5bcf9bfa73c9162b3a77225c997c6deec8a)
SERVER-36116 Get rid of CollectionShardingState::resetAll
(cherry picked from commit db1cc80d13d203b6351f5510f7756cc1c7bfc0ea)
SERVER-36054 Get rid of unused methods from CollectionShardingState
(cherry picked from commit 884d232473dca72e0872f0e540d4c3108c1e0b3d)
SERVER-36164 Decouple ScopedCollectionMetadata from MetadataManager
(cherry picked from commit d91262c4a2ed7d94923c3b1c5ff5d208aa981c73)
SERVER-29908 Move CollectionShardingState under sharding_api_d
(cherry picked from commit e491e284e8066929c8272c96a3128241ab481be8)
SERVER-29908 Remove ShardingState::appendInfo
Expose the ShardingState properties and move the appendInfo logic to be
entirely inside the 'getShardingState' function, which is its only
consumer.
(cherry picked from commit 24e411d5cd7f64c5b4da25a351529cd1873284b8)
SERVER-29908 Move 'updateConfigServerOpTimeFromMetadata' out of ShardingState
(cherry picked from commit 7a97557ce5bf74dc2b663762b7a5ffb9c958d580)
SERVER-29908 Move all runtime logic out of ShardingState
... and move it into a ShardingInitializationMongoD class, which is
responsible for driving the sharding-awareness of the node and setting
it onto ShardingState.
Also gets rid of the 'sharding' library, so there is no more library
dependency cycle.
(cherry picked from commit 200c3dc58410d8b3287a2075cc9b2ad085100e83)
SERVER-29908 Fold the 'sharding_connection_hook' library into 'sharding_initialization'
... and also remove dependency of MongoS on the replication coordinator
(cherry picked from commit fab6864f4edcae7bb304f79e601f1f62cc376a77)
Diffstat (limited to 'src/mongo/db/s/collection_sharding_runtime.h')
-rw-r--r-- | src/mongo/db/s/collection_sharding_runtime.h | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/src/mongo/db/s/collection_sharding_runtime.h b/src/mongo/db/s/collection_sharding_runtime.h new file mode 100644 index 00000000000..0515fde5ab0 --- /dev/null +++ b/src/mongo/db/s/collection_sharding_runtime.h @@ -0,0 +1,171 @@ +/** + * Copyright (C) 2018 MongoDB Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * As a special exception, the copyright holders give permission to link the + * code of portions of this program with the OpenSSL library under certain + * conditions as described in each individual source file and distribute + * linked combinations including the program with the OpenSSL library. You + * must comply with the GNU Affero General Public License in all respects + * for all of the code used other than as permitted herein. If you modify + * file(s) with this exception, you may extend this exception to your + * version of the file(s), but you are not obligated to do so. If you do not + * wish to do so, delete this exception statement from your version. If you + * delete this exception statement from all source files in the program, + * then also delete it in the license file. + */ + +#pragma once + +#include "mongo/base/disallow_copying.h" +#include "mongo/db/namespace_string.h" +#include "mongo/db/s/collection_sharding_state.h" +#include "mongo/db/s/metadata_manager.h" +#include "mongo/util/decorable.h" + +namespace mongo { + +/** + * See the comments for CollectionShardingState for more information on how this class fits in the + * sharding architecture. + */ +class CollectionShardingRuntime final : public CollectionShardingState, + public Decorable<CollectionShardingRuntime> { + MONGO_DISALLOW_COPYING(CollectionShardingRuntime); + +public: + CollectionShardingRuntime(ServiceContext* sc, + NamespaceString nss, + executor::TaskExecutor* rangeDeleterExecutor); + + /** + * Obtains the sharding state for the specified collection. If it does not exist, it will be + * created and will remain active until the collection is dropped or unsharded. + * + * Must be called with some lock held on the specific collection being looked up and the + * returned pointer should never be stored. + */ + static CollectionShardingRuntime* get(OperationContext* opCtx, const NamespaceString& nss); + + /** + * Updates the metadata based on changes received from the config server and also resolves the + * pending receives map in case some of these pending receives have completed or have been + * abandoned. If newMetadata is null, unshard the collection. + * + * Must always be called with an exclusive collection lock. + */ + void refreshMetadata(OperationContext* opCtx, std::unique_ptr<CollectionMetadata> newMetadata); + + /** + * Marks the collection as not sharded at stepdown time so that no filtering will occur for + * slaveOk queries. + */ + void markNotShardedAtStepdown(); + + /** + * Schedules any documents in `range` for immediate cleanup iff no running queries can depend + * on them, and adds the range to the list of pending ranges. Otherwise, returns a notification + * that yields bad status immediately. Does not block. Call waitStatus(opCtx) on the result + * to wait for the deletion to complete or fail. After that, call waitForClean to ensure no + * other deletions are pending for the range. + */ + using CleanupNotification = CollectionRangeDeleter::DeleteNotification; + CleanupNotification beginReceive(ChunkRange const& range); + + /* + * Removes `range` from the list of pending ranges, and schedules any documents in the range for + * immediate cleanup. Does not block. + */ + void forgetReceive(const ChunkRange& range); + + /** + * Schedules documents in `range` for cleanup after any running queries that may depend on them + * have terminated. Does not block. Fails if range overlaps any current local shard chunk. + * Passed kDelayed, an additional delay (configured via server parameter orphanCleanupDelaySecs) + * is added to permit (most) dependent queries on secondaries to complete, too. + * + * Call result.waitStatus(opCtx) to wait for the deletion to complete or fail. If that succeeds, + * waitForClean can be called to ensure no other deletions are pending for the range. Call + * result.abandon(), instead of waitStatus, to ignore the outcome. + */ + enum CleanWhen { kNow, kDelayed }; + CleanupNotification cleanUpRange(ChunkRange const& range, CleanWhen when); + + /** + * Tracks deletion of any documents within the range, returning when deletion is complete. + * Throws if the collection is dropped while it sleeps. + */ + static Status waitForClean(OperationContext* opCtx, + const NamespaceString& nss, + OID const& epoch, + ChunkRange orphanRange); + + /** + * Reports whether any range still scheduled for deletion overlaps the argument range. If so, + * it returns a notification n such that n->get(opCtx) will wake when the newest overlapping + * range's deletion (possibly the one of interest) completes or fails. This should be called + * again after each wakeup until it returns boost::none, because there can be more than one + * range scheduled for deletion that overlaps its argument. + */ + auto trackOrphanedDataCleanup(ChunkRange const& range) -> boost::optional<CleanupNotification>; + + /** + * Returns a range _not_ owned by this shard that starts no lower than the specified + * startingFrom key value, if any, or boost::none if there is no such range. + */ + boost::optional<ChunkRange> getNextOrphanRange(BSONObj const& startingFrom); + + /** + * BSON output of the pending metadata into a BSONArray + */ + void toBSONPending(BSONArrayBuilder& bb) const { + _metadataManager->toBSONPending(bb); + } + + +private: + friend boost::optional<Date_t> CollectionRangeDeleter::cleanUpNextRange( + OperationContext*, NamespaceString const&, OID const&, int, CollectionRangeDeleter*); + + // Namespace this state belongs to. + const NamespaceString _nss; + + // Contains all the metadata associated with this collection. + std::shared_ptr<MetadataManager> _metadataManager; + + ScopedCollectionMetadata _getMetadata(OperationContext* opCtx) override; +}; + +/** + * RAII-style class, which obtains a reference to the critical section for the specified collection. + */ +class CollectionCriticalSection { + MONGO_DISALLOW_COPYING(CollectionCriticalSection); + +public: + CollectionCriticalSection(OperationContext* opCtx, NamespaceString ns); + ~CollectionCriticalSection(); + + /** + * Enters the commit phase of the critical section and blocks reads. + */ + void enterCommitPhase(); + +private: + NamespaceString _nss; + + OperationContext* _opCtx; +}; + +} // namespace mongo |