summaryrefslogtreecommitdiff
path: root/src/mongo/db/s/cleanup_orphaned_cmd.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/db/s/cleanup_orphaned_cmd.cpp')
-rw-r--r--src/mongo/db/s/cleanup_orphaned_cmd.cpp86
1 files changed, 40 insertions, 46 deletions
diff --git a/src/mongo/db/s/cleanup_orphaned_cmd.cpp b/src/mongo/db/s/cleanup_orphaned_cmd.cpp
index d0e58779a3b..bb0d58273f3 100644
--- a/src/mongo/db/s/cleanup_orphaned_cmd.cpp
+++ b/src/mongo/db/s/cleanup_orphaned_cmd.cpp
@@ -78,65 +78,59 @@ CleanupResult cleanupOrphanedData(OperationContext* opCtx,
const WriteConcernOptions& secondaryThrottle,
BSONObj* stoppedAtKey,
string* errMsg) {
- BSONObj startingFromKey = startingFromKeyConst;
- ScopedCollectionMetadata metadata;
+ BSONObj startingFromKey = startingFromKeyConst;
+ boost::optional<ChunkRange> targetRange;
{
AutoGetCollection autoColl(opCtx, ns, MODE_IS);
- metadata = CollectionShardingState::get(opCtx, ns.toString())->getMetadata();
- }
+ auto css = CollectionShardingState::get(opCtx, ns.toString());
+ auto metadata = css->getMetadata();
- if (!metadata) {
- warning() << "skipping orphaned data cleanup for " << ns.toString()
+ if (!metadata) {
+ log() << "skipping orphaned data cleanup for " << ns.toString()
<< ", collection is not sharded";
- return CleanupResult_Done;
- }
+ return CleanupResult_Done;
+ }
- BSONObj keyPattern = metadata->getKeyPattern();
- if (!startingFromKey.isEmpty()) {
- if (!metadata->isValidKey(startingFromKey)) {
- *errMsg = stream() << "could not cleanup orphaned data, start key "
- << redact(startingFromKey) << " does not match shard key pattern "
- << keyPattern;
+ BSONObj keyPattern = metadata->getKeyPattern();
+ if (!startingFromKey.isEmpty()) {
+ if (!metadata->isValidKey(startingFromKey)) {
+ *errMsg = stream() << "could not cleanup orphaned data, start key "
+ << redact(startingFromKey)
+ << " does not match shard key pattern " << keyPattern;
- warning() << *errMsg;
- return CleanupResult_Error;
+ log() << *errMsg;
+ return CleanupResult_Error;
+ }
+ } else {
+ startingFromKey = metadata->getMinKey();
}
- } else {
- startingFromKey = metadata->getMinKey();
- }
- KeyRange orphanRange;
- if (!metadata->getNextOrphanRange(startingFromKey, &orphanRange)) {
- LOG(1) << "cleanupOrphaned requested for " << ns.toString() << " starting from "
- << redact(startingFromKey) << ", no orphan ranges remain";
+ KeyRange orphanRange;
+ if (!metadata->getNextOrphanRange(css->getReceiveMap(), startingFromKey, &orphanRange)) {
+ LOG(1) << "cleanupOrphaned requested for " << ns.toString() << " starting from "
+ << redact(startingFromKey) << ", no orphan ranges remain";
+
+ return CleanupResult_Done;
+ }
+ orphanRange.ns = ns.ns();
+ *stoppedAtKey = orphanRange.maxKey;
+
+ LOG(0) << "cleanupOrphaned requested for " << ns.toString() << " starting from "
+ << redact(startingFromKey) << ", removing next orphan range"
+ << " [" << redact(orphanRange.minKey) << "," << redact(orphanRange.maxKey) << ")";
- return CleanupResult_Done;
+ targetRange.emplace(ChunkRange(orphanRange.minKey, orphanRange.maxKey));
+ css->cleanUpRange(*targetRange);
}
- orphanRange.ns = ns.ns();
- *stoppedAtKey = orphanRange.maxKey;
-
- LOG(0) << "cleanupOrphaned requested for " << ns.toString() << " starting from "
- << redact(startingFromKey) << ", removing next orphan range"
- << " [" << redact(orphanRange.minKey) << "," << redact(orphanRange.maxKey) << ")";
-
- // Metadata snapshot may be stale now, but deleter checks metadata again in write lock
- // before delete.
- RangeDeleterOptions deleterOptions(orphanRange);
- deleterOptions.writeConcern = secondaryThrottle;
- deleterOptions.onlyRemoveOrphanedDocs = true;
- deleterOptions.fromMigrate = true;
- // Must wait for cursors since there can be existing cursors with an older
- // CollectionMetadata.
- deleterOptions.waitForOpenCursors = true;
- deleterOptions.removeSaverReason = "cleanup-cmd";
-
- if (!getDeleter()->deleteNow(opCtx, deleterOptions, errMsg)) {
- warning() << redact(*errMsg);
- return CleanupResult_Error;
+ if (targetRange) {
+ auto result = CollectionShardingState::waitForClean(opCtx, ns, *targetRange);
+ if (!result.isOK()) {
+ warning() << redact(result.reason());
+ return CleanupResult_Error;
+ }
}
-
return CleanupResult_Continue;
}