diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/mongo/db/commands/get_last_error.cpp | 4 | ||||
-rw-r--r-- | src/mongo/db/pipeline/aggregation_request.cpp | 3 | ||||
-rw-r--r-- | src/mongo/db/query/find_and_modify_request.cpp | 7 | ||||
-rw-r--r-- | src/mongo/db/repl/repl_set_config.cpp | 8 | ||||
-rw-r--r-- | src/mongo/db/write_concern_options.cpp | 47 | ||||
-rw-r--r-- | src/mongo/db/write_concern_options.h | 2 | ||||
-rw-r--r-- | src/mongo/db/write_concern_options_test.cpp | 60 | ||||
-rw-r--r-- | src/mongo/s/catalog/sharding_catalog_client_impl.cpp | 7 | ||||
-rw-r--r-- | src/mongo/s/request_types/migration_secondary_throttle_options.cpp | 22 |
9 files changed, 86 insertions, 74 deletions
diff --git a/src/mongo/db/commands/get_last_error.cpp b/src/mongo/db/commands/get_last_error.cpp index 2de7f05f169..1b674dd6ef7 100644 --- a/src/mongo/db/commands/get_last_error.cpp +++ b/src/mongo/db/commands/get_last_error.cpp @@ -224,12 +224,14 @@ public: writeConcern = repl::ReplicationCoordinator::get(opCtx)->getGetLastErrorDefault(); } - Status status = writeConcern.parse(writeConcernDoc); + auto sw = WriteConcernOptions::parse(writeConcernDoc); + Status status = sw.getStatus(); // // Validate write concern no matter what, this matches 2.4 behavior // if (status.isOK()) { + writeConcern = sw.getValue(); status = validateWriteConcern(opCtx, writeConcern); } diff --git a/src/mongo/db/pipeline/aggregation_request.cpp b/src/mongo/db/pipeline/aggregation_request.cpp index bea15219efe..5964e3ef76a 100644 --- a/src/mongo/db/pipeline/aggregation_request.cpp +++ b/src/mongo/db/pipeline/aggregation_request.cpp @@ -186,8 +186,7 @@ StatusWith<AggregationRequest> AggregationRequest::parseFromBSON( << fieldName << " must be an object, not a " << typeName(elem.type())}; } - WriteConcernOptions writeConcern; - uassertStatusOK(writeConcern.parse(elem.embeddedObject())); + auto writeConcern = uassertStatusOK(WriteConcernOptions::parse(elem.embeddedObject())); request.setWriteConcern(writeConcern); } else if (kRuntimeConstants == fieldName) { try { diff --git a/src/mongo/db/query/find_and_modify_request.cpp b/src/mongo/db/query/find_and_modify_request.cpp index cdc136a39f7..c86edcbde5c 100644 --- a/src/mongo/db/query/find_and_modify_request.cpp +++ b/src/mongo/db/query/find_and_modify_request.cpp @@ -254,11 +254,12 @@ StatusWith<FindAndModifyRequest> FindAndModifyRequest::parseFromBSON(NamespaceSt if (!writeConcernEltStatus.isOK()) { return writeConcernEltStatus; } - auto status = writeConcernOptions.parse(writeConcernElt.embeddedObject()); - if (!status.isOK()) { - return status; + auto sw = WriteConcernOptions::parse(writeConcernElt.embeddedObject()); + if (!sw.isOK()) { + return sw.getStatus(); } else { writeConcernOptionsSet = true; + writeConcernOptions = sw.getValue(); } } else if (!isGenericArgument(field) && !std::count(_knownFields.begin(), _knownFields.end(), field)) { diff --git a/src/mongo/db/repl/repl_set_config.cpp b/src/mongo/db/repl/repl_set_config.cpp index 6fa81e2aeb4..2084c400343 100644 --- a/src/mongo/db/repl/repl_set_config.cpp +++ b/src/mongo/db/repl/repl_set_config.cpp @@ -320,9 +320,11 @@ Status ReplSetConfig::_parseSettingsSubdocument(const BSONObj& settings) { status = bsonExtractTypedField( settings, kGetLastErrorDefaultsFieldName, Object, &gleDefaultsElement); if (status.isOK()) { - status = _defaultWriteConcern.parse(gleDefaultsElement.Obj()); - if (!status.isOK()) - return status; + auto sw = WriteConcernOptions::parse(gleDefaultsElement.Obj()); + if (!sw.isOK()) { + return sw.getStatus(); + } + _defaultWriteConcern = sw.getValue(); } else if (status == ErrorCodes::NoSuchKey) { // Default write concern is w: 1. _defaultWriteConcern = WriteConcernOptions(); diff --git a/src/mongo/db/write_concern_options.cpp b/src/mongo/db/write_concern_options.cpp index 22bb5b49790..cb4a2b13cf1 100644 --- a/src/mongo/db/write_concern_options.cpp +++ b/src/mongo/db/write_concern_options.cpp @@ -91,8 +91,7 @@ WriteConcernOptions::WriteConcernOptions(const std::string& mode, Milliseconds timeout) : syncMode(sync), wNumNodes(0), wMode(mode), wTimeout(durationCount<Milliseconds>(timeout)) {} -Status WriteConcernOptions::parse(const BSONObj& obj) { - *this = WriteConcernOptions(); +StatusWith<WriteConcernOptions> WriteConcernOptions::parse(const BSONObj& obj) { if (obj.isEmpty()) { return Status(ErrorCodes::FailedToParse, "write concern object cannot be empty"); } @@ -100,6 +99,7 @@ Status WriteConcernOptions::parse(const BSONObj& obj) { BSONElement jEl; BSONElement fsyncEl; BSONElement wEl; + int wTimeout = 0; for (auto e : obj) { const auto fieldName = e.fieldNameStringData(); @@ -132,40 +132,43 @@ Status WriteConcernOptions::parse(const BSONObj& obj) { const bool j = jEl.trueValue(); const bool fsync = fsyncEl.trueValue(); - - if (j && fsync) + if (j && fsync) { return Status(ErrorCodes::FailedToParse, "fsync and j options cannot be used together"); + } + + WriteConcernOptions writeConcern; if (j) { - syncMode = SyncMode::JOURNAL; + writeConcern.syncMode = SyncMode::JOURNAL; } else if (fsync) { - syncMode = SyncMode::FSYNC; + writeConcern.syncMode = SyncMode::FSYNC; } else if (!jEl.eoo()) { - syncMode = SyncMode::NONE; + writeConcern.syncMode = SyncMode::NONE; } if (wEl.isNumber()) { - wNumNodes = wEl.numberInt(); - usedDefaultW = false; + writeConcern.wNumNodes = wEl.numberInt(); + writeConcern.usedDefaultW = false; } else if (wEl.type() == String) { - wNumNodes = 0; - wMode = wEl.valuestrsafe(); - usedDefaultW = false; + writeConcern.wNumNodes = 0; + writeConcern.wMode = wEl.valuestrsafe(); + writeConcern.usedDefaultW = false; } else if (wEl.eoo() || wEl.type() == jstNULL || wEl.type() == Undefined) { - wNumNodes = 1; + writeConcern.wNumNodes = 1; } else { return Status(ErrorCodes::FailedToParse, "w has to be a number or a string"); } - return Status::OK(); + writeConcern.wTimeout = wTimeout; + + return writeConcern; } WriteConcernOptions WriteConcernOptions::deserializerForIDL(const BSONObj& obj) { - WriteConcernOptions writeConcern; if (!obj.isEmpty()) { - uassertStatusOK(writeConcern.parse(obj)); + return uassertStatusOK(parse(obj)); } - return writeConcern; + return WriteConcernOptions(); } StatusWith<WriteConcernOptions> WriteConcernOptions::extractWCFromCommand(const BSONObj& cmdObj) { @@ -191,12 +194,12 @@ StatusWith<WriteConcernOptions> WriteConcernOptions::extractWCFromCommand(const return writeConcern; } - wcStatus = writeConcern.parse(writeConcernObj); - writeConcern.usedDefault = false; - if (!wcStatus.isOK()) { - return wcStatus; + auto sw = parse(writeConcernObj); + if (!sw.isOK()) { + return sw.getStatus(); } - + writeConcern = sw.getValue(); + writeConcern.usedDefault = false; return writeConcern; } diff --git a/src/mongo/db/write_concern_options.h b/src/mongo/db/write_concern_options.h index c20ba6287fa..cca80457e4e 100644 --- a/src/mongo/db/write_concern_options.h +++ b/src/mongo/db/write_concern_options.h @@ -76,7 +76,7 @@ public: WriteConcernOptions(const std::string& mode, SyncMode sync, Milliseconds timeout); - Status parse(const BSONObj& obj); + static StatusWith<WriteConcernOptions> parse(const BSONObj& obj); /** * Returns an instance of WriteConcernOptions from a BSONObj. diff --git a/src/mongo/db/write_concern_options_test.cpp b/src/mongo/db/write_concern_options_test.cpp index 315608a9584..79c7c618a91 100644 --- a/src/mongo/db/write_concern_options_test.cpp +++ b/src/mongo/db/write_concern_options_test.cpp @@ -37,34 +37,37 @@ namespace mongo { namespace { TEST(WriteConcernOptionsTest, ParseReturnsFailedToParseOnEmptyDocument) { - auto status = WriteConcernOptions().parse({}); + auto status = WriteConcernOptions::parse({}).getStatus(); ASSERT_EQUALS(ErrorCodes::FailedToParse, status); ASSERT_EQUALS("write concern object cannot be empty", status.reason()); } TEST(WriteConcernOptionsTest, ParseReturnsFailedToParseOnInvalidJValue) { - auto status = WriteConcernOptions().parse(BSON("j" - << "abc")); + auto status = WriteConcernOptions::parse(BSON("j" + << "abc")) + .getStatus(); ASSERT_EQUALS(ErrorCodes::FailedToParse, status); ASSERT_EQUALS("j must be numeric or a boolean value", status.reason()); } TEST(WriteConcernOptionsTest, ParseReturnsFailedToParseOnInvalidFSyncValue) { - auto status = WriteConcernOptions().parse(BSON("fsync" - << "abc")); + auto status = WriteConcernOptions::parse(BSON("fsync" + << "abc")) + .getStatus(); ASSERT_EQUALS(ErrorCodes::FailedToParse, status); ASSERT_EQUALS("fsync must be numeric or a boolean value", status.reason()); } TEST(WriteConcernOptionsTest, ParseReturnsFailedToParseIfBothJAndFSyncAreTrue) { - auto status = WriteConcernOptions().parse(BSON("j" << true << "fsync" << true)); + auto status = WriteConcernOptions::parse(BSON("j" << true << "fsync" << true)).getStatus(); ASSERT_EQUALS(ErrorCodes::FailedToParse, status); ASSERT_EQUALS("fsync and j options cannot be used together", status.reason()); } TEST(WriteConcernOptionsTest, ParseSetsSyncModeToJournelIfJIsTrue) { - WriteConcernOptions options; - ASSERT_OK(options.parse(BSON("j" << true))); + auto sw = WriteConcernOptions::parse(BSON("j" << true)); + ASSERT_OK(sw.getStatus()); + WriteConcernOptions options = sw.getValue(); ASSERT_TRUE(WriteConcernOptions::SyncMode::JOURNAL == options.syncMode); ASSERT_EQUALS(1, options.wNumNodes); ASSERT_EQUALS("", options.wMode); @@ -72,8 +75,9 @@ TEST(WriteConcernOptionsTest, ParseSetsSyncModeToJournelIfJIsTrue) { } TEST(WriteConcernOptionsTest, ParseSetsSyncModeToFSyncIfFSyncIsTrue) { - WriteConcernOptions options; - ASSERT_OK(options.parse(BSON("fsync" << true))); + auto sw = WriteConcernOptions::parse(BSON("fsync" << true)); + ASSERT_OK(sw.getStatus()); + WriteConcernOptions options = sw.getValue(); ASSERT_TRUE(WriteConcernOptions::SyncMode::FSYNC == options.syncMode); ASSERT_EQUALS(1, options.wNumNodes); ASSERT_EQUALS("", options.wMode); @@ -81,8 +85,9 @@ TEST(WriteConcernOptionsTest, ParseSetsSyncModeToFSyncIfFSyncIsTrue) { } TEST(WriteConcernOptionsTest, ParseSetsSyncModeToNoneIfJIsFalse) { - WriteConcernOptions options; - ASSERT_OK(options.parse(BSON("j" << false))); + auto sw = WriteConcernOptions::parse(BSON("j" << false)); + ASSERT_OK(sw.getStatus()); + WriteConcernOptions options = sw.getValue(); ASSERT_TRUE(WriteConcernOptions::SyncMode::NONE == options.syncMode); ASSERT_EQUALS(1, options.wNumNodes); ASSERT_EQUALS("", options.wMode); @@ -90,8 +95,9 @@ TEST(WriteConcernOptionsTest, ParseSetsSyncModeToNoneIfJIsFalse) { } TEST(WriteConcernOptionsTest, ParseLeavesSyncModeAsUnsetIfFSyncIsFalse) { - WriteConcernOptions options; - ASSERT_OK(options.parse(BSON("fsync" << false))); + auto sw = WriteConcernOptions::parse(BSON("fsync" << false)); + ASSERT_OK(sw.getStatus()); + WriteConcernOptions options = sw.getValue(); ASSERT_TRUE(WriteConcernOptions::SyncMode::UNSET == options.syncMode); ASSERT_EQUALS(1, options.wNumNodes); ASSERT_EQUALS("", options.wMode); @@ -99,14 +105,15 @@ TEST(WriteConcernOptionsTest, ParseLeavesSyncModeAsUnsetIfFSyncIsFalse) { } TEST(WriteConcernOptionsTest, ParseReturnsFailedToParseIfWIsNotNumberOrString) { - auto status = WriteConcernOptions().parse(BSON("w" << BSONObj())); + auto status = WriteConcernOptions::parse(BSON("w" << BSONObj())).getStatus(); ASSERT_EQUALS(ErrorCodes::FailedToParse, status); ASSERT_EQUALS("w has to be a number or a string", status.reason()); } TEST(WriteConcernOptionsTest, ParseSetsWNumNodesIfWIsANumber) { - WriteConcernOptions options; - ASSERT_OK(options.parse(BSON("w" << 3))); + auto sw = WriteConcernOptions::parse(BSON("w" << 3)); + ASSERT_OK(sw.getStatus()); + WriteConcernOptions options = sw.getValue(); ASSERT_TRUE(WriteConcernOptions::SyncMode::UNSET == options.syncMode); ASSERT_EQUALS(3, options.wNumNodes); ASSERT_EQUALS("", options.wMode); @@ -114,9 +121,10 @@ TEST(WriteConcernOptionsTest, ParseSetsWNumNodesIfWIsANumber) { } TEST(WriteConcernOptionsTest, ParseSetsWTimeoutToZeroIfWTimeoutIsNotANumber) { - WriteConcernOptions options; - ASSERT_OK(options.parse(BSON("wtimeout" - << "abc"))); + auto sw = WriteConcernOptions::parse(BSON("wtimeout" + << "abc")); + ASSERT_OK(sw.getStatus()); + WriteConcernOptions options = sw.getValue(); ASSERT_TRUE(WriteConcernOptions::SyncMode::UNSET == options.syncMode); ASSERT_EQUALS("", options.wMode); ASSERT_EQUALS(1, options.wNumNodes); @@ -124,8 +132,9 @@ TEST(WriteConcernOptionsTest, ParseSetsWTimeoutToZeroIfWTimeoutIsNotANumber) { } TEST(WriteConcernOptionsTest, ParseWTimeoutAsNumber) { - WriteConcernOptions options; - ASSERT_OK(options.parse(BSON("wtimeout" << 123))); + auto sw = WriteConcernOptions::parse(BSON("wtimeout" << 123)); + ASSERT_OK(sw.getStatus()); + WriteConcernOptions options = sw.getValue(); ASSERT_TRUE(WriteConcernOptions::SyncMode::UNSET == options.syncMode); ASSERT_EQUALS("", options.wMode); ASSERT_EQUALS(1, options.wNumNodes); @@ -133,14 +142,15 @@ TEST(WriteConcernOptionsTest, ParseWTimeoutAsNumber) { } TEST(WriteConcernOptionsTest, ParseReturnsFailedToParseOnUnknownField) { - auto status = WriteConcernOptions().parse(BSON("x" << 123)); + auto status = WriteConcernOptions::parse(BSON("x" << 123)).getStatus(); ASSERT_EQUALS(ErrorCodes::FailedToParse, status); ASSERT_EQUALS("unrecognized write concern field: x", status.reason()); } void _testIgnoreWriteConcernField(const char* fieldName) { - WriteConcernOptions options; - ASSERT_OK(options.parse(BSON(fieldName << 1))); + auto sw = WriteConcernOptions::parse(BSON(fieldName << 1)); + ASSERT_OK(sw.getStatus()); + WriteConcernOptions options = sw.getValue(); ASSERT_TRUE(WriteConcernOptions::SyncMode::UNSET == options.syncMode); ASSERT_EQUALS("", options.wMode); ASSERT_EQUALS(1, options.wNumNodes); diff --git a/src/mongo/s/catalog/sharding_catalog_client_impl.cpp b/src/mongo/s/catalog/sharding_catalog_client_impl.cpp index 4ced819df8a..04d88a11834 100644 --- a/src/mongo/s/catalog/sharding_catalog_client_impl.cpp +++ b/src/mongo/s/catalog/sharding_catalog_client_impl.cpp @@ -594,10 +594,11 @@ bool ShardingCatalogClientImpl::runUserManagementWriteCommand(OperationContext* BSONElement writeConcernElement = cmdObj[WriteConcernOptions::kWriteConcernField]; bool initialCmdHadWriteConcern = !writeConcernElement.eoo(); if (initialCmdHadWriteConcern) { - Status status = writeConcern.parse(writeConcernElement.Obj()); - if (!status.isOK()) { - return CommandHelpers::appendCommandStatusNoThrow(*result, status); + auto sw = WriteConcernOptions::parse(writeConcernElement.Obj()); + if (!sw.isOK()) { + return CommandHelpers::appendCommandStatusNoThrow(*result, sw.getStatus()); } + writeConcern = sw.getValue(); if (!(writeConcern.wNumNodes == 1 || writeConcern.wMode == WriteConcernOptions::kMajority)) { diff --git a/src/mongo/s/request_types/migration_secondary_throttle_options.cpp b/src/mongo/s/request_types/migration_secondary_throttle_options.cpp index a6540d47ac7..d739671b1e3 100644 --- a/src/mongo/s/request_types/migration_secondary_throttle_options.cpp +++ b/src/mongo/s/request_types/migration_secondary_throttle_options.cpp @@ -108,10 +108,9 @@ StatusWith<MigrationSecondaryThrottleOptions> MigrationSecondaryThrottleOptions: invariant(writeConcernBSON.is_initialized()); // Make sure the write concern parses correctly - WriteConcernOptions writeConcern; - Status status = writeConcern.parse(*writeConcernBSON); - if (!status.isOK()) { - return status; + auto sw = WriteConcernOptions::parse(*writeConcernBSON); + if (!sw.isOK()) { + return sw.getStatus(); } return MigrationSecondaryThrottleOptions(secondaryThrottle, std::move(writeConcernBSON)); @@ -138,23 +137,18 @@ MigrationSecondaryThrottleOptions::createFromBalancerConfig(const BSONObj& obj) if (!status.isOK()) return status; - WriteConcernOptions writeConcern; - Status writeConcernParseStatus = writeConcern.parse(elem.Obj()); - if (!writeConcernParseStatus.isOK()) { - return writeConcernParseStatus; + auto sw = WriteConcernOptions::parse(elem.Obj()); + if (!sw.isOK()) { + return sw.getStatus(); } - - return MigrationSecondaryThrottleOptions::createWithWriteConcern(writeConcern); + return MigrationSecondaryThrottleOptions::createWithWriteConcern(sw.getValue()); } WriteConcernOptions MigrationSecondaryThrottleOptions::getWriteConcern() const { invariant(_secondaryThrottle != kOff); invariant(_writeConcernBSON); - WriteConcernOptions writeConcern; - fassert(34414, writeConcern.parse(*_writeConcernBSON)); - - return writeConcern; + return fassert(34414, WriteConcernOptions::parse(*_writeConcernBSON)); } void MigrationSecondaryThrottleOptions::append(BSONObjBuilder* builder) const { |