diff options
author | Matthew Saltz <matthew.saltz@mongodb.com> | 2019-12-06 20:28:41 +0000 |
---|---|---|
committer | evergreen <evergreen@mongodb.com> | 2019-12-06 20:28:41 +0000 |
commit | 38ed2fce5eee9821bcc52b1c6af83235008b756e (patch) | |
tree | 54cce318e251d56843255a6b1908554d25a9ab7b | |
parent | 5215876fa41427f074ac1d43ded07c474bf0f903 (diff) | |
download | mongo-38ed2fce5eee9821bcc52b1c6af83235008b756e.tar.gz |
SERVER-44159 Make CollectionRangeDeleter::_doDeletion an anonymous helper
-rw-r--r-- | src/mongo/db/s/collection_range_deleter.cpp | 213 | ||||
-rw-r--r-- | src/mongo/db/s/collection_range_deleter.h | 13 |
2 files changed, 113 insertions, 113 deletions
diff --git a/src/mongo/db/s/collection_range_deleter.cpp b/src/mongo/db/s/collection_range_deleter.cpp index c13a3722a02..3a636b5bcbf 100644 --- a/src/mongo/db/s/collection_range_deleter.cpp +++ b/src/mongo/db/s/collection_range_deleter.cpp @@ -89,6 +89,111 @@ boost::optional<DeleteNotification> checkOverlap(std::list<Deletion> const& dele return boost::none; } +/** + * Performs the deletion of up to maxToDelete entries within the range in progress. Must be + * called under the collection lock. + * + * Returns the number of documents deleted, 0 if done with the range, or bad status if deleting + * the range failed. + */ +StatusWith<int> doDeletion(OperationContext* opCtx, + Collection* collection, + BSONObj const& keyPattern, + ChunkRange const& range, + int maxToDelete, + bool throwWriteConflictForTest) { + invariant(collection != nullptr); + + auto const& nss = collection->ns(); + + // The IndexChunk has a keyPattern that may apply to more than one index - we need to + // select the index and get the full index keyPattern here. + auto catalog = collection->getIndexCatalog(); + const IndexDescriptor* idx = catalog->findShardKeyPrefixedIndex(opCtx, keyPattern, false); + if (!idx) { + std::string msg = str::stream() + << "Unable to find shard key index for " << keyPattern.toString() << " in " << nss.ns(); + LOG(0) << msg; + return {ErrorCodes::InternalError, msg}; + } + + // Extend bounds to match the index we found + const KeyPattern indexKeyPattern(idx->keyPattern()); + const auto extend = [&](const auto& key) { + return Helpers::toKeyFormat(indexKeyPattern.extendRangeBound(key, false)); + }; + + const auto min = extend(range.getMin()); + const auto max = extend(range.getMax()); + + LOG(1) << "begin removal of " << min << " to " << max << " in " << nss.ns(); + + const auto indexName = idx->indexName(); + const IndexDescriptor* descriptor = + collection->getIndexCatalog()->findIndexByName(opCtx, indexName); + if (!descriptor) { + std::string msg = str::stream() + << "shard key index with name " << indexName << " on '" << nss.ns() << "' was dropped"; + LOG(0) << msg; + return {ErrorCodes::InternalError, msg}; + } + + auto deleteStageParams = std::make_unique<DeleteStageParams>(); + deleteStageParams->fromMigrate = true; + deleteStageParams->isMulti = true; + deleteStageParams->returnDeleted = true; + + if (serverGlobalParams.moveParanoia) { + deleteStageParams->removeSaver = + std::make_unique<RemoveSaver>("moveChunk", nss.ns(), "cleaning"); + } + + auto exec = InternalPlanner::deleteWithIndexScan(opCtx, + collection, + std::move(deleteStageParams), + descriptor, + min, + max, + BoundInclusion::kIncludeStartKeyOnly, + PlanExecutor::YIELD_MANUAL, + InternalPlanner::FORWARD); + + if (MONGO_unlikely(hangBeforeDoingDeletion.shouldFail())) { + LOG(0) << "Hit hangBeforeDoingDeletion failpoint"; + hangBeforeDoingDeletion.pauseWhileSet(opCtx); + } + + PlanYieldPolicy planYieldPolicy(exec.get(), PlanExecutor::YIELD_MANUAL); + + int numDeleted = 0; + do { + BSONObj deletedObj; + + // TODO SERVER-41606: Remove this function when we refactor CollectionRangeDeleter. + if (throwWriteConflictForTest) + throw WriteConflictException(); + + PlanExecutor::ExecState state = exec->getNext(&deletedObj, nullptr); + + if (state == PlanExecutor::IS_EOF) { + break; + } + + if (state == PlanExecutor::FAILURE) { + warning() << PlanExecutor::statestr(state) << " - cursor error while trying to delete " + << redact(min) << " to " << redact(max) << " in " << nss + << ": FAILURE, stats: " << Explain::getWinningPlanStats(exec.get()); + break; + } + + invariant(PlanExecutor::ADVANCED == state); + ShardingStatistics::get(opCtx).countDocsDeletedOnDonor.addAndFetch(1); + + } while (++numDeleted < maxToDelete); + + return numDeleted; +} + } // namespace CollectionRangeDeleter::CollectionRangeDeleter() = default; @@ -195,8 +300,14 @@ boost::optional<Date_t> CollectionRangeDeleter::cleanUpNextRange( const auto& metadata = *scopedCollectionMetadata; try { - swNumDeleted = self->_doDeletion( - opCtx, collection, metadata->getKeyPattern(), *range, maxToDelete); + swNumDeleted = doDeletion(opCtx, + collection, + metadata->getKeyPattern(), + *range, + maxToDelete, + // _throwWriteConflictForTest is only used in unit tests, so + // taking the MetadataManager lock is not required. + self->_throwWriteConflictForTest); if (swNumDeleted.isOK()) { LOG(0) << "Deleted " << swNumDeleted.getValue() << " documents in pass."; } @@ -337,104 +448,6 @@ bool CollectionRangeDeleter::_checkCollectionMetadataStillValid( return true; } -StatusWith<int> CollectionRangeDeleter::_doDeletion(OperationContext* opCtx, - Collection* collection, - BSONObj const& keyPattern, - ChunkRange const& range, - int maxToDelete) { - invariant(collection != nullptr); - invariant(!isEmpty()); - - auto const& nss = collection->ns(); - - // The IndexChunk has a keyPattern that may apply to more than one index - we need to - // select the index and get the full index keyPattern here. - auto catalog = collection->getIndexCatalog(); - const IndexDescriptor* idx = catalog->findShardKeyPrefixedIndex(opCtx, keyPattern, false); - if (!idx) { - std::string msg = str::stream() - << "Unable to find shard key index for " << keyPattern.toString() << " in " << nss.ns(); - LOG(0) << msg; - return {ErrorCodes::InternalError, msg}; - } - - // Extend bounds to match the index we found - const KeyPattern indexKeyPattern(idx->keyPattern()); - const auto extend = [&](const auto& key) { - return Helpers::toKeyFormat(indexKeyPattern.extendRangeBound(key, false)); - }; - - const auto min = extend(range.getMin()); - const auto max = extend(range.getMax()); - - LOG(1) << "begin removal of " << min << " to " << max << " in " << nss.ns(); - - const auto indexName = idx->indexName(); - const IndexDescriptor* descriptor = - collection->getIndexCatalog()->findIndexByName(opCtx, indexName); - if (!descriptor) { - std::string msg = str::stream() - << "shard key index with name " << indexName << " on '" << nss.ns() << "' was dropped"; - LOG(0) << msg; - return {ErrorCodes::InternalError, msg}; - } - - auto deleteStageParams = std::make_unique<DeleteStageParams>(); - deleteStageParams->fromMigrate = true; - deleteStageParams->isMulti = true; - deleteStageParams->returnDeleted = true; - - if (serverGlobalParams.moveParanoia) { - deleteStageParams->removeSaver = - std::make_unique<RemoveSaver>("moveChunk", nss.ns(), "cleaning"); - } - - auto exec = InternalPlanner::deleteWithIndexScan(opCtx, - collection, - std::move(deleteStageParams), - descriptor, - min, - max, - BoundInclusion::kIncludeStartKeyOnly, - PlanExecutor::YIELD_MANUAL, - InternalPlanner::FORWARD); - - if (MONGO_unlikely(hangBeforeDoingDeletion.shouldFail())) { - LOG(0) << "Hit hangBeforeDoingDeletion failpoint"; - hangBeforeDoingDeletion.pauseWhileSet(opCtx); - } - - PlanYieldPolicy planYieldPolicy(exec.get(), PlanExecutor::YIELD_MANUAL); - - int numDeleted = 0; - do { - BSONObj deletedObj; - - // TODO SERVER-41606: Remove this function when we refactor CollectionRangeDeleter. - if (_throwWriteConflictForTest) - throw WriteConflictException(); - - PlanExecutor::ExecState state = exec->getNext(&deletedObj, nullptr); - - if (state == PlanExecutor::IS_EOF) { - break; - } - - if (state == PlanExecutor::FAILURE) { - warning() << PlanExecutor::statestr(state) << " - cursor error while trying to delete " - << redact(min) << " to " << redact(max) << " in " << nss - << ": FAILURE, stats: " << Explain::getWinningPlanStats(exec.get()); - break; - } - - invariant(PlanExecutor::ADVANCED == state); - ShardingStatistics::get(opCtx).countDocsDeletedOnDonor.addAndFetch(1); - - } while (++numDeleted < maxToDelete); - - return numDeleted; -} - auto CollectionRangeDeleter::overlaps(ChunkRange const& range) const -> boost::optional<DeleteNotification> { auto result = checkOverlap(_orphans, range); diff --git a/src/mongo/db/s/collection_range_deleter.h b/src/mongo/db/s/collection_range_deleter.h index f37bdff0524..f6e8d1b136f 100644 --- a/src/mongo/db/s/collection_range_deleter.h +++ b/src/mongo/db/s/collection_range_deleter.h @@ -201,19 +201,6 @@ private: std::shared_ptr<MetadataManager> metadataManager); /** - * Performs the deletion of up to maxToDelete entries within the range in progress. Must be - * called under the collection lock. - * - * Returns the number of documents deleted, 0 if done with the range, or bad status if deleting - * the range failed. - */ - StatusWith<int> _doDeletion(OperationContext* opCtx, - Collection* collection, - const BSONObj& keyPattern, - ChunkRange const& range, - int maxToDelete); - - /** * Removes the latest-scheduled range from the ranges to be cleaned up, and notifies any * interested callers of this->overlaps(range) with specified status. */ |