diff options
author | Jamie Heppenstall <jamie.heppenstall@mongodb.com> | 2019-06-06 15:00:55 -0400 |
---|---|---|
committer | Jamie Heppenstall <jamie.heppenstall@mongodb.com> | 2019-06-07 11:25:56 -0400 |
commit | 0863aca0c6ad060ad241b7fa75fb03db06beccb0 (patch) | |
tree | f13e72e24d9ea1ec68f6ef3b70f07254583664d7 /src/mongo | |
parent | 8b2879c30bcb8d8f5163ecf3050d8bdc421786c7 (diff) | |
download | mongo-0863aca0c6ad060ad241b7fa75fb03db06beccb0.tar.gz |
SERVER-39659 Reformat createShardDatabase to return void and call uasserts on all relevant statuses
Diffstat (limited to 'src/mongo')
-rw-r--r-- | src/mongo/s/cluster_commands_helpers.cpp | 36 | ||||
-rw-r--r-- | src/mongo/s/cluster_commands_helpers.h | 6 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_create_cmd.cpp | 2 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_create_indexes_cmd.cpp | 2 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_find_and_modify_cmd.cpp | 2 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_map_reduce_cmd.cpp | 2 | ||||
-rw-r--r-- | src/mongo/s/write_ops/chunk_manager_targeter.cpp | 7 |
7 files changed, 28 insertions, 29 deletions
diff --git a/src/mongo/s/cluster_commands_helpers.cpp b/src/mongo/s/cluster_commands_helpers.cpp index 3d6cf6ed18b..d411f79f991 100644 --- a/src/mongo/s/cluster_commands_helpers.cpp +++ b/src/mongo/s/cluster_commands_helpers.cpp @@ -507,36 +507,34 @@ bool appendEmptyResultSet(OperationContext* opCtx, return true; } -StatusWith<CachedDatabaseInfo> createShardDatabase(OperationContext* opCtx, StringData dbName) { +void createShardDatabase(OperationContext* opCtx, StringData dbName) { auto dbStatus = Grid::get(opCtx)->catalogCache()->getDatabase(opCtx, dbName); + if (dbStatus == ErrorCodes::NamespaceNotFound) { ConfigsvrCreateDatabase configCreateDatabaseRequest(dbName.toString()); configCreateDatabaseRequest.setDbName(NamespaceString::kAdminDb); auto configShard = Grid::get(opCtx)->shardRegistry()->getConfigShard(); - auto createDbStatus = - uassertStatusOK(configShard->runCommandWithFixedRetryAttempts( - opCtx, - ReadPreferenceSetting(ReadPreference::PrimaryOnly), - "admin", - CommandHelpers::appendMajorityWriteConcern( - configCreateDatabaseRequest.toBSON({})), - Shard::RetryPolicy::kIdempotent)) - .commandStatus; - - if (createDbStatus.isOK() || createDbStatus == ErrorCodes::NamespaceExists) { - dbStatus = Grid::get(opCtx)->catalogCache()->getDatabase(opCtx, dbName); - } else { - dbStatus = createDbStatus; + auto createDbResponse = uassertStatusOK(configShard->runCommandWithFixedRetryAttempts( + opCtx, + ReadPreferenceSetting(ReadPreference::PrimaryOnly), + "admin", + CommandHelpers::appendMajorityWriteConcern(configCreateDatabaseRequest.toBSON({})), + Shard::RetryPolicy::kIdempotent)); + + uassertStatusOK(createDbResponse.writeConcernStatus); + + if (createDbResponse.commandStatus != ErrorCodes::NamespaceExists) { + uassertStatusOKWithContext(createDbResponse.commandStatus, + str::stream() << "Database " << dbName + << " could not be created"); } - } - if (dbStatus.isOK()) { - return dbStatus; + dbStatus = Grid::get(opCtx)->catalogCache()->getDatabase(opCtx, dbName); } - return dbStatus.getStatus().withContext(str::stream() << "Database " << dbName << " not found"); + uassertStatusOKWithContext(dbStatus, str::stream() << "Database " << dbName << " not found"); } std::set<ShardId> getTargetedShardsForQuery(OperationContext* opCtx, diff --git a/src/mongo/s/cluster_commands_helpers.h b/src/mongo/s/cluster_commands_helpers.h index c1dcc2f1050..433013c76b6 100644 --- a/src/mongo/s/cluster_commands_helpers.h +++ b/src/mongo/s/cluster_commands_helpers.h @@ -198,10 +198,10 @@ bool appendEmptyResultSet(OperationContext* opCtx, const std::string& ns); /** - * If the specified database exists already, loads it in the cache (if not already there) and - * returns it. Otherwise, if it does not exist, this call will implicitly create it as non-sharded. + * If the specified database exists already, loads it in the cache (if not already there). + * Otherwise, if it does not exist, this call will implicitly create it as non-sharded. */ -StatusWith<CachedDatabaseInfo> createShardDatabase(OperationContext* opCtx, StringData dbName); +void createShardDatabase(OperationContext* opCtx, StringData dbName); /** * Returns the shards that would be targeted for the given query according to the given routing diff --git a/src/mongo/s/commands/cluster_create_cmd.cpp b/src/mongo/s/commands/cluster_create_cmd.cpp index a5c71dcb1e0..9b9ebab29e3 100644 --- a/src/mongo/s/commands/cluster_create_cmd.cpp +++ b/src/mongo/s/commands/cluster_create_cmd.cpp @@ -73,7 +73,7 @@ public: BSONObjBuilder& result) override { const NamespaceString nss(parseNs(dbName, cmdObj)); - uassertStatusOK(createShardDatabase(opCtx, dbName)); + createShardDatabase(opCtx, dbName); uassert(ErrorCodes::InvalidOptions, "specify size:<n> when capped is true", diff --git a/src/mongo/s/commands/cluster_create_indexes_cmd.cpp b/src/mongo/s/commands/cluster_create_indexes_cmd.cpp index 65ac1d1abcc..31e0d36625a 100644 --- a/src/mongo/s/commands/cluster_create_indexes_cmd.cpp +++ b/src/mongo/s/commands/cluster_create_indexes_cmd.cpp @@ -71,7 +71,7 @@ public: const NamespaceString nss(CommandHelpers::parseNsCollectionRequired(dbName, cmdObj)); LOG(1) << "createIndexes: " << nss << " cmd:" << redact(cmdObj); - uassertStatusOK(createShardDatabase(opCtx, dbName)); + createShardDatabase(opCtx, dbName); auto shardResponses = scatterGatherOnlyVersionIfUnsharded( opCtx, diff --git a/src/mongo/s/commands/cluster_find_and_modify_cmd.cpp b/src/mongo/s/commands/cluster_find_and_modify_cmd.cpp index 940c9299aaf..8d71f9c08d8 100644 --- a/src/mongo/s/commands/cluster_find_and_modify_cmd.cpp +++ b/src/mongo/s/commands/cluster_find_and_modify_cmd.cpp @@ -228,7 +228,7 @@ public: // findAndModify should only be creating database if upsert is true, but this would require // that the parsing be pulled into this function. - uassertStatusOK(createShardDatabase(opCtx, nss.db())); + createShardDatabase(opCtx, nss.db()); // Append mongoS' runtime constants to the command object before forwarding it to the shard. auto cmdObjForShard = appendRuntimeConstantsToCommandObject(opCtx, cmdObj); diff --git a/src/mongo/s/commands/cluster_map_reduce_cmd.cpp b/src/mongo/s/commands/cluster_map_reduce_cmd.cpp index 3a9cfddcdf6..6f169e55140 100644 --- a/src/mongo/s/commands/cluster_map_reduce_cmd.cpp +++ b/src/mongo/s/commands/cluster_map_reduce_cmd.cpp @@ -244,7 +244,7 @@ public: // Create the output database implicitly if we have a custom output requested if (customOutDB) { - uassertStatusOK(createShardDatabase(opCtx, outDB)); + createShardDatabase(opCtx, outDB); } // Ensure that the output database doesn't reside on the config server diff --git a/src/mongo/s/write_ops/chunk_manager_targeter.cpp b/src/mongo/s/write_ops/chunk_manager_targeter.cpp index 25fd831460b..91f0d94505d 100644 --- a/src/mongo/s/write_ops/chunk_manager_targeter.cpp +++ b/src/mongo/s/write_ops/chunk_manager_targeter.cpp @@ -340,9 +340,10 @@ ChunkManagerTargeter::ChunkManagerTargeter(const NamespaceString& nss, Status ChunkManagerTargeter::init(OperationContext* opCtx) { - auto shardDbStatus = createShardDatabase(opCtx, _nss.db()); - if (!shardDbStatus.isOK()) { - return shardDbStatus.getStatus(); + try { + createShardDatabase(opCtx, _nss.db()); + } catch (const DBException& e) { + return e.toStatus(); } const auto routingInfoStatus = getCollectionRoutingInfoForTxnCmd(opCtx, _nss); |