4141#include " mongo/db/jsobj.h"
4242#include " mongo/db/namespace_string.h"
4343#include " mongo/db/range_deleter_service.h"
44+ #include " mongo/db/repl/repl_coordinator_global.h"
4445#include " mongo/s/collection_metadata.h"
4546#include " mongo/s/d_logic.h"
4647#include " mongo/s/range_arithmetic.h"
48+ #include " mongo/s/type_settings.h"
4749#include " mongo/util/log.h"
4850
51+ namespace {
52+ using mongo::WriteConcernOptions;
53+
54+ const int kDefaultWTimeoutMs = 60 * 1000 ;
55+ const WriteConcernOptions DefaultWriteConcern (" majority" ,
56+ WriteConcernOptions::NONE,
57+ kDefaultWTimeoutMs );
58+ }
59+
4960namespace mongo {
5061
5162 MONGO_LOG_DEFAULT_COMPONENT_FILE (::mongo::logger::LogComponent::kCommands );
@@ -69,7 +80,7 @@ namespace mongo {
6980 CleanupResult cleanupOrphanedData ( OperationContext* txn,
7081 const NamespaceString& ns,
7182 const BSONObj& startingFromKeyConst,
72- bool secondaryThrottle,
83+ const WriteConcernOptions& secondaryThrottle,
7384 BSONObj* stoppedAtKey,
7485 string* errMsg ) {
7586
@@ -153,6 +164,17 @@ namespace mongo {
153164 * the balancer is off.
154165 *
155166 * Safe to call with the balancer on.
167+ *
168+ * Format:
169+ *
170+ * {
171+ * cleanupOrphaned: <ns>,
172+ * // optional parameters:
173+ * startingAtKey: { <shardKeyValue> }, // defaults to lowest value
174+ * secondaryThrottle: <bool>, // defaults to true
175+ * // defaults to { w: "majority", wtimeout: 60000 }. Applies to individual writes.
176+ * writeConcern: { <writeConcern options> }
177+ * }
156178 */
157179 class CleanupOrphanedCommand : public Command {
158180 public:
@@ -179,7 +201,6 @@ namespace mongo {
179201 // Input
180202 static BSONField<string> nsField;
181203 static BSONField<BSONObj> startingFromKeyField;
182- static BSONField<bool > secondaryThrottleField;
183204
184205 // Output
185206 static BSONField<BSONObj> stoppedAtKeyField;
@@ -210,12 +231,29 @@ namespace mongo {
210231 return false ;
211232 }
212233
213- bool secondaryThrottle = true ;
214- if ( !FieldParser::extract ( cmdObj,
215- secondaryThrottleField,
216- &secondaryThrottle,
217- &errmsg ) ) {
218- return false ;
234+ WriteConcernOptions writeConcern;
235+ Status status = writeConcern.parseSecondaryThrottle (cmdObj, NULL );
236+
237+ if (!status.isOK ()){
238+ if (status.code () != ErrorCodes::WriteConcernNotDefined) {
239+ return appendCommandStatus (result, status);
240+ }
241+
242+ writeConcern = DefaultWriteConcern;
243+ }
244+ else {
245+ repl::ReplicationCoordinator* replCoordinator =
246+ repl::getGlobalReplicationCoordinator ();
247+ Status status = replCoordinator->checkIfWriteConcernCanBeSatisfied (writeConcern);
248+ if (!status.isOK ()) {
249+ return appendCommandStatus (result, status);
250+ }
251+ }
252+
253+ if (writeConcern.shouldWaitForOtherNodes () &&
254+ writeConcern.wTimeout == WriteConcernOptions::kNoTimeout ) {
255+ // Don't allow no timeout.
256+ writeConcern.wTimeout = kDefaultWTimeoutMs ;
219257 }
220258
221259 if (!shardingState.enabled ()) {
@@ -225,7 +263,7 @@ namespace mongo {
225263 }
226264
227265 ChunkVersion shardVersion;
228- Status status = shardingState.refreshMetadataNow ( ns, &shardVersion );
266+ status = shardingState.refreshMetadataNow ( ns, &shardVersion );
229267 if ( !status.isOK () ) {
230268 if ( status.code () == ErrorCodes::RemoteChangeDetected ) {
231269 warning () << " Shard version in transition detected while refreshing "
@@ -242,7 +280,7 @@ namespace mongo {
242280 CleanupResult cleanupResult = cleanupOrphanedData ( txn,
243281 NamespaceString ( ns ),
244282 startingFromKey,
245- secondaryThrottle ,
283+ writeConcern ,
246284 &stoppedAtKey,
247285 &errmsg );
248286
@@ -263,7 +301,6 @@ namespace mongo {
263301
264302 BSONField<string> CleanupOrphanedCommand::nsField ( " cleanupOrphaned" );
265303 BSONField<BSONObj> CleanupOrphanedCommand::startingFromKeyField ( " startingFromKey" );
266- BSONField<bool > CleanupOrphanedCommand::secondaryThrottleField ( " secondaryThrottle" );
267304 BSONField<BSONObj> CleanupOrphanedCommand::stoppedAtKeyField ( " stoppedAtKey" );
268305
269306 MONGO_INITIALIZER (RegisterCleanupOrphanedCommand)(InitializerContext* context) {
0 commit comments