summaryrefslogtreecommitdiff
path: root/src/mongo/db/s/set_shard_version_command.cpp
diff options
context:
space:
mode:
authorKaloian Manassiev <kaloian.manassiev@mongodb.com>2017-08-11 16:43:18 -0400
committerKaloian Manassiev <kaloian.manassiev@mongodb.com>2017-08-14 15:29:38 -0400
commit58b36b8bbc6ca7159e100ada6dc15852af0a8661 (patch)
tree16934881e75c10af862aa3f5f7d280c21928246b /src/mongo/db/s/set_shard_version_command.cpp
parent2176ab64295b23e8c67f7799d1310791f14db147 (diff)
downloadmongo-58b36b8bbc6ca7159e100ada6dc15852af0a8661.tar.gz
SERVER-30424 Remove opCtx/serviceContext null checks from db.cpp since ServiceContext must always exist
Diffstat (limited to 'src/mongo/db/s/set_shard_version_command.cpp')
-rw-r--r--src/mongo/db/s/set_shard_version_command.cpp101
1 files changed, 43 insertions, 58 deletions
diff --git a/src/mongo/db/s/set_shard_version_command.cpp b/src/mongo/db/s/set_shard_version_command.cpp
index 929332c29d9..561b13b335d 100644
--- a/src/mongo/db/s/set_shard_version_command.cpp
+++ b/src/mongo/db/s/set_shard_version_command.cpp
@@ -97,7 +97,7 @@ public:
"can't issue setShardVersion from 'eval'",
!opCtx->getClient()->isInDirectClient());
- auto shardingState = ShardingState::get(opCtx);
+ auto const shardingState = ShardingState::get(opCtx);
uassertStatusOK(shardingState->canAcceptShardedCommands());
// Steps
@@ -158,8 +158,8 @@ public:
// Step 3
// Validate shardName parameter.
- string shardName = cmdObj["shard"].str();
- auto storedShardName = ShardingState::get(opCtx)->getShardName();
+ const auto shardName = cmdObj["shard"].str();
+ const auto storedShardName = ShardingState::get(opCtx)->getShardName();
uassert(ErrorCodes::BadValue,
str::stream() << "received shardName " << shardName
<< " which differs from stored shardName "
@@ -167,45 +167,29 @@ public:
storedShardName == shardName);
// Validate config connection string parameter.
+ const auto configdb = cmdObj["configdb"].String();
+ uassert(ErrorCodes::BadValue,
+ "Config server connection string cannot be empty",
+ !configdb.empty());
- const auto configdb = cmdObj["configdb"].str();
- if (configdb.size() == 0) {
- errmsg = "no configdb";
- return false;
- }
-
- auto givenConnStrStatus = ConnectionString::parse(configdb);
- uassertStatusOK(givenConnStrStatus);
-
- const auto& givenConnStr = givenConnStrStatus.getValue();
- if (givenConnStr.type() != ConnectionString::SET) {
- errmsg = str::stream() << "given config server string is not of type SET";
- return false;
- }
-
- ConnectionString storedConnStr = ShardingState::get(opCtx)->getConfigServer(opCtx);
- if (givenConnStr.getSetName() != storedConnStr.getSetName()) {
- errmsg = str::stream()
- << "given config server set name: " << givenConnStr.getSetName()
- << " differs from known set name: " << storedConnStr.getSetName();
+ const auto givenConnStr = uassertStatusOK(ConnectionString::parse(configdb));
+ uassert(ErrorCodes::InvalidOptions,
+ str::stream() << "Given config server string " << givenConnStr.toString()
+ << " is not of type SET",
+ givenConnStr.type() == ConnectionString::SET);
- return false;
- }
+ const auto storedConnStr = ShardingState::get(opCtx)->getConfigServer(opCtx);
+ uassert(ErrorCodes::IllegalOperation,
+ str::stream() << "Given config server set name: " << givenConnStr.getSetName()
+ << " differs from known set name: "
+ << storedConnStr.getSetName(),
+ givenConnStr.getSetName() == storedConnStr.getSetName());
// Validate namespace parameter.
-
- const string ns = cmdObj["setShardVersion"].valuestrsafe();
- if (ns.size() == 0) {
- errmsg = "need to specify namespace";
- return false;
- }
-
- // Backwards compatibility for SERVER-23119
- const NamespaceString nss(ns);
- if (!nss.isValid()) {
- warning() << "Invalid namespace used for setShardVersion: " << ns;
- return true;
- }
+ const NamespaceString nss(cmdObj["setShardVersion"].String());
+ uassert(ErrorCodes::InvalidNamespace,
+ str::stream() << "Invalid namespace " << nss.ns(),
+ nss.isValid());
// Validate chunk version parameter.
const ChunkVersion requestedVersion =
@@ -213,7 +197,7 @@ public:
// Step 4
- const ChunkVersion connectionVersion = info->getVersion(ns);
+ const ChunkVersion connectionVersion = info->getVersion(nss.ns());
connectionVersion.addToBSON(result, "oldVersion");
{
@@ -252,15 +236,15 @@ public:
// A migration occurred.
if (connectionVersion < collectionShardVersion &&
connectionVersion.epoch() == collectionShardVersion.epoch()) {
- info->setVersion(ns, requestedVersion);
+ info->setVersion(nss.ns(), requestedVersion);
}
// The collection was dropped and recreated.
else if (authoritative) {
- info->setVersion(ns, requestedVersion);
+ info->setVersion(nss.ns(), requestedVersion);
} else {
- result.append("ns", ns);
+ result.append("ns", nss.ns());
result.appendBool("need_authoritative", true);
- errmsg = "verifying drop on '" + ns + "'";
+ errmsg = str::stream() << "verifying drop on '" << nss.ns() << "'";
return false;
}
}
@@ -276,7 +260,7 @@ public:
if (isDropRequested) {
if (!authoritative) {
result.appendBool("need_authoritative", true);
- result.append("ns", ns);
+ result.append("ns", nss.ns());
collectionShardVersion.addToBSON(result, "globalVersion");
errmsg = "dropping needs to be authoritative";
return false;
@@ -292,8 +276,8 @@ public:
if (requestedVersion < connectionVersion &&
requestedVersion.epoch() == connectionVersion.epoch()) {
errmsg = str::stream() << "this connection already had a newer version "
- << "of collection '" << ns << "'";
- result.append("ns", ns);
+ << "of collection '" << nss.ns() << "'";
+ result.append("ns", nss.ns());
requestedVersion.addToBSON(result, "newVersion");
collectionShardVersion.addToBSON(result, "globalVersion");
return false;
@@ -315,8 +299,8 @@ public:
}
errmsg = str::stream() << "shard global version for collection is higher "
- << "than trying to set to '" << ns << "'";
- result.append("ns", ns);
+ << "than trying to set to '" << nss.ns() << "'";
+ result.append("ns", nss.ns());
requestedVersion.addToBSON(result, "version");
collectionShardVersion.addToBSON(result, "globalVersion");
result.appendBool("reloadConfig", true);
@@ -339,9 +323,9 @@ public:
}
// need authoritative for first look
- result.append("ns", ns);
+ result.append("ns", nss.ns());
result.appendBool("need_authoritative", true);
- errmsg = "first time for collection '" + ns + "'";
+ errmsg = str::stream() << "first time for collection '" << nss.ns() << "'";
return false;
}
@@ -365,14 +349,15 @@ public:
if (!status.isOK()) {
// The reload itself was interrupted or confused here
- errmsg = str::stream()
- << "could not refresh metadata for " << ns << " with requested shard version "
- << requestedVersion.toString() << ", stored shard version is "
- << currVersion.toString() << causedBy(redact(status));
+ errmsg = str::stream() << "could not refresh metadata for " << nss.ns()
+ << " with requested shard version "
+ << requestedVersion.toString()
+ << ", stored shard version is " << currVersion.toString()
+ << causedBy(redact(status));
warning() << errmsg;
- result.append("ns", ns);
+ result.append("ns", nss.ns());
requestedVersion.addToBSON(result, "version");
currVersion.addToBSON(result, "globalVersion");
result.appendBool("reloadConfig", true);
@@ -382,7 +367,7 @@ public:
// We reloaded a version that doesn't match the version mongos was trying to
// set.
errmsg = str::stream() << "requested shard version differs from"
- << " config shard version for " << ns
+ << " config shard version for " << nss.ns()
<< ", requested version is " << requestedVersion.toString()
<< " but found version " << currVersion.toString();
@@ -391,7 +376,7 @@ public:
// WARNING: the exact fields below are important for compatibility with mongos
// version reload.
- result.append("ns", ns);
+ result.append("ns", nss.ns());
currVersion.addToBSON(result, "globalVersion");
// If this was a reset of a collection or the last chunk moved out, inform mongos to
@@ -411,7 +396,7 @@ public:
}
}
- info->setVersion(ns, requestedVersion);
+ info->setVersion(nss.ns(), requestedVersion);
return true;
}