diff options
author | Mark Benvenuto <mark.benvenuto@mongodb.com> | 2015-06-20 00:22:50 -0400 |
---|---|---|
committer | Mark Benvenuto <mark.benvenuto@mongodb.com> | 2015-06-20 10:56:02 -0400 |
commit | 9c2ed42daa8fbbef4a919c21ec564e2db55e8d60 (patch) | |
tree | 3814f79c10d7b490948d8cb7b112ac1dd41ceff1 /src/mongo/db/commands/copydb.cpp | |
parent | 01965cf52bce6976637ecb8f4a622aeb05ab256a (diff) | |
download | mongo-9c2ed42daa8fbbef4a919c21ec564e2db55e8d60.tar.gz |
SERVER-18579: Clang-Format - reformat code, no comment reflow
Diffstat (limited to 'src/mongo/db/commands/copydb.cpp')
-rw-r--r-- | src/mongo/db/commands/copydb.cpp | 326 |
1 files changed, 163 insertions, 163 deletions
diff --git a/src/mongo/db/commands/copydb.cpp b/src/mongo/db/commands/copydb.cpp index 328c24111f8..d41c3f50657 100644 --- a/src/mongo/db/commands/copydb.cpp +++ b/src/mongo/db/commands/copydb.cpp @@ -43,190 +43,190 @@ namespace { - using namespace mongo; - - using std::string; - using std::stringstream; - - /* Usage: - * admindb.$cmd.findOne( { copydb: 1, fromhost: <connection string>, fromdb: <db>, - * todb: <db>[, username: <username>, nonce: <nonce>, key: <key>] } ); - * - * The "copydb" command is used to copy a database. Note that this is a very broad definition. - * This means that the "copydb" command can be used in the following ways: - * - * 1. To copy a database within a single node - * 2. To copy a database within a sharded cluster, possibly to another shard - * 3. To copy a database from one cluster to another - * - * Note that in all cases both the target and source database must be unsharded. - * - * The "copydb" command gets sent by the client or the mongos to the destination of the copy - * operation. The node, cluster, or shard that recieves the "copydb" command must then query - * the source of the database to be copied for all the contents and metadata of the database. - * - * - * - * When used with auth, there are two different considerations. - * - * The first is authentication with the target. The only entity that needs to authenticate with - * the target node is the client, so authentication works there the same as it would with any - * other command. - * - * The second is the authentication of the target with the source, which is needed because the - * target must query the source directly for the contents of the database. To do this, the - * client must use the "copydbgetnonce" command, in which the target will get a nonce from the - * source and send it back to the client. The client can then hash its password with the nonce, - * send it to the target when it runs the "copydb" command, which can then use that information - * to authenticate with the source. - * - * NOTE: mongos doesn't know how to call or handle the "copydbgetnonce" command. See - * SERVER-6427. - * - * NOTE: Since internal cluster auth works differently, "copydb" currently doesn't work between - * shards in a cluster when auth is enabled. See SERVER-13080. - */ - class CmdCopyDb : public Command { - public: - CmdCopyDb() : Command("copydb") { } - - virtual bool adminOnly() const { - return true; +using namespace mongo; + +using std::string; +using std::stringstream; + +/* Usage: + * admindb.$cmd.findOne( { copydb: 1, fromhost: <connection string>, fromdb: <db>, + * todb: <db>[, username: <username>, nonce: <nonce>, key: <key>] } ); + * + * The "copydb" command is used to copy a database. Note that this is a very broad definition. + * This means that the "copydb" command can be used in the following ways: + * + * 1. To copy a database within a single node + * 2. To copy a database within a sharded cluster, possibly to another shard + * 3. To copy a database from one cluster to another + * + * Note that in all cases both the target and source database must be unsharded. + * + * The "copydb" command gets sent by the client or the mongos to the destination of the copy + * operation. The node, cluster, or shard that recieves the "copydb" command must then query + * the source of the database to be copied for all the contents and metadata of the database. + * + * + * + * When used with auth, there are two different considerations. + * + * The first is authentication with the target. The only entity that needs to authenticate with + * the target node is the client, so authentication works there the same as it would with any + * other command. + * + * The second is the authentication of the target with the source, which is needed because the + * target must query the source directly for the contents of the database. To do this, the + * client must use the "copydbgetnonce" command, in which the target will get a nonce from the + * source and send it back to the client. The client can then hash its password with the nonce, + * send it to the target when it runs the "copydb" command, which can then use that information + * to authenticate with the source. + * + * NOTE: mongos doesn't know how to call or handle the "copydbgetnonce" command. See + * SERVER-6427. + * + * NOTE: Since internal cluster auth works differently, "copydb" currently doesn't work between + * shards in a cluster when auth is enabled. See SERVER-13080. + */ +class CmdCopyDb : public Command { +public: + CmdCopyDb() : Command("copydb") {} + + virtual bool adminOnly() const { + return true; + } + + virtual bool slaveOk() const { + return false; + } + + virtual bool isWriteCommandForConfigServer() const { + return false; + } + + virtual Status checkAuthForCommand(ClientBasic* client, + const std::string& dbname, + const BSONObj& cmdObj) { + return copydb::checkAuthForCopydbCommand(client, dbname, cmdObj); + } + + virtual void help(stringstream& help) const { + help << "copy a database from another host to this host\n"; + help << "usage: {copydb: 1, fromhost: <connection string>, fromdb: <db>, todb: <db>" + << "[, slaveOk: <bool>, username: <username>, nonce: <nonce>, key: <key>]}"; + } + + virtual bool run(OperationContext* txn, + const string& dbname, + BSONObj& cmdObj, + int, + string& errmsg, + BSONObjBuilder& result) { + boost::optional<DisableDocumentValidation> maybeDisableValidation; + if (shouldBypassDocumentValidationForCommand(cmdObj)) + maybeDisableValidation.emplace(txn); + + string fromhost = cmdObj.getStringField("fromhost"); + bool fromSelf = fromhost.empty(); + if (fromSelf) { + /* copy from self */ + stringstream ss; + ss << "localhost:" << serverGlobalParams.port; + fromhost = ss.str(); } - virtual bool slaveOk() const { + CloneOptions cloneOptions; + cloneOptions.fromDB = cmdObj.getStringField("fromdb"); + cloneOptions.slaveOk = cmdObj["slaveOk"].trueValue(); + cloneOptions.useReplAuth = false; + cloneOptions.snapshot = true; + cloneOptions.mayYield = true; + cloneOptions.mayBeInterrupted = false; + + string todb = cmdObj.getStringField("todb"); + if (fromhost.empty() || todb.empty() || cloneOptions.fromDB.empty()) { + errmsg = + "params missing - {copydb: 1, fromhost: <connection string>, " + "fromdb: <db>, todb: <db>}"; return false; } - virtual bool isWriteCommandForConfigServer() const { return false; } - - virtual Status checkAuthForCommand(ClientBasic* client, - const std::string& dbname, - const BSONObj& cmdObj) { - return copydb::checkAuthForCopydbCommand(client, dbname, cmdObj); + if (!NamespaceString::validDBName(todb)) { + errmsg = "invalid todb name: " + todb; + return false; } - virtual void help( stringstream &help ) const { - help << "copy a database from another host to this host\n"; - help << "usage: {copydb: 1, fromhost: <connection string>, fromdb: <db>, todb: <db>" - << "[, slaveOk: <bool>, username: <username>, nonce: <nonce>, key: <key>]}"; - } + Cloner cloner; - virtual bool run(OperationContext* txn, - const string& dbname, - BSONObj& cmdObj, - int, - string& errmsg, - BSONObjBuilder& result) { - - boost::optional<DisableDocumentValidation> maybeDisableValidation; - if (shouldBypassDocumentValidationForCommand(cmdObj)) - maybeDisableValidation.emplace(txn); - - string fromhost = cmdObj.getStringField("fromhost"); - bool fromSelf = fromhost.empty(); - if ( fromSelf ) { - /* copy from self */ - stringstream ss; - ss << "localhost:" << serverGlobalParams.port; - fromhost = ss.str(); - } + // Get MONGODB-CR parameters + string username = cmdObj.getStringField("username"); + string nonce = cmdObj.getStringField("nonce"); + string key = cmdObj.getStringField("key"); - CloneOptions cloneOptions; - cloneOptions.fromDB = cmdObj.getStringField("fromdb"); - cloneOptions.slaveOk = cmdObj["slaveOk"].trueValue(); - cloneOptions.useReplAuth = false; - cloneOptions.snapshot = true; - cloneOptions.mayYield = true; - cloneOptions.mayBeInterrupted = false; - - string todb = cmdObj.getStringField("todb"); - if ( fromhost.empty() || todb.empty() || cloneOptions.fromDB.empty() ) { - errmsg = "params missing - {copydb: 1, fromhost: <connection string>, " - "fromdb: <db>, todb: <db>}"; - return false; - } + auto& authConn = CopyDbAuthConnection::forClient(txn->getClient()); - if ( !NamespaceString::validDBName( todb ) ) { - errmsg = "invalid todb name: " + todb; - return false; - } - - Cloner cloner; - - // Get MONGODB-CR parameters - string username = cmdObj.getStringField( "username" ); - string nonce = cmdObj.getStringField( "nonce" ); - string key = cmdObj.getStringField( "key" ); - - auto& authConn = CopyDbAuthConnection::forClient(txn->getClient()); - - if ( !username.empty() && !nonce.empty() && !key.empty() ) { - uassert( 13008, "must call copydbgetnonce first", authConn.get() ); - BSONObj ret; - { - if ( !authConn->runCommand( cloneOptions.fromDB, - BSON( "authenticate" << 1 << "user" << username - << "nonce" << nonce << "key" << key ), ret ) ) { - errmsg = "unable to login " + ret.toString(); - authConn.reset(); - return false; - } - } - cloner.setConnection( authConn.release() ); - } - else if (cmdObj.hasField(saslCommandConversationIdFieldName) && - cmdObj.hasField(saslCommandPayloadFieldName)) { - uassert( 25487, "must call copydbsaslstart first", authConn.get() ); - BSONObj ret; - if ( !authConn->runCommand( cloneOptions.fromDB, - BSON( "saslContinue" << 1 << - cmdObj[saslCommandConversationIdFieldName] << - cmdObj[saslCommandPayloadFieldName] ), - ret ) ) { + if (!username.empty() && !nonce.empty() && !key.empty()) { + uassert(13008, "must call copydbgetnonce first", authConn.get()); + BSONObj ret; + { + if (!authConn->runCommand(cloneOptions.fromDB, + BSON("authenticate" << 1 << "user" << username << "nonce" + << nonce << "key" << key), + ret)) { errmsg = "unable to login " + ret.toString(); authConn.reset(); return false; } - - if (!ret["done"].Bool()) { - result.appendElements( ret ); - return true; - } - - result.append("done", true); - cloner.setConnection( authConn.release() ); } - else if (!fromSelf) { - // If fromSelf leave the cloner's conn empty, it will use a DBDirectClient instead. - const ConnectionString cs(uassertStatusOK(ConnectionString::parse(fromhost))); + cloner.setConnection(authConn.release()); + } else if (cmdObj.hasField(saslCommandConversationIdFieldName) && + cmdObj.hasField(saslCommandPayloadFieldName)) { + uassert(25487, "must call copydbsaslstart first", authConn.get()); + BSONObj ret; + if (!authConn->runCommand(cloneOptions.fromDB, + BSON("saslContinue" + << 1 << cmdObj[saslCommandConversationIdFieldName] + << cmdObj[saslCommandPayloadFieldName]), + ret)) { + errmsg = "unable to login " + ret.toString(); + authConn.reset(); + return false; + } - DBClientBase* conn = cs.connect(errmsg); - if (!conn) { - return false; - } - cloner.setConnection(conn); + if (!ret["done"].Bool()) { + result.appendElements(ret); + return true; } - // Either we didn't need the authConn (if we even had one), or we already moved it - // into the cloner so just make sure we don't keep it around if we don't need it. - authConn.reset(); + result.append("done", true); + cloner.setConnection(authConn.release()); + } else if (!fromSelf) { + // If fromSelf leave the cloner's conn empty, it will use a DBDirectClient instead. + const ConnectionString cs(uassertStatusOK(ConnectionString::parse(fromhost))); - if (fromSelf) { - // SERVER-4328 todo lock just the two db's not everything for the fromself case - ScopedTransaction transaction(txn, MODE_X); - Lock::GlobalWrite lk(txn->lockState()); - uassertStatusOK(cloner.copyDb(txn, todb, fromhost, cloneOptions, NULL)); - } - else { - ScopedTransaction transaction(txn, MODE_IX); - Lock::DBLock lk(txn->lockState(), todb, MODE_X); - uassertStatusOK(cloner.copyDb(txn, todb, fromhost, cloneOptions, NULL)); + DBClientBase* conn = cs.connect(errmsg); + if (!conn) { + return false; } + cloner.setConnection(conn); + } - return true; + // Either we didn't need the authConn (if we even had one), or we already moved it + // into the cloner so just make sure we don't keep it around if we don't need it. + authConn.reset(); + + if (fromSelf) { + // SERVER-4328 todo lock just the two db's not everything for the fromself case + ScopedTransaction transaction(txn, MODE_X); + Lock::GlobalWrite lk(txn->lockState()); + uassertStatusOK(cloner.copyDb(txn, todb, fromhost, cloneOptions, NULL)); + } else { + ScopedTransaction transaction(txn, MODE_IX); + Lock::DBLock lk(txn->lockState(), todb, MODE_X); + uassertStatusOK(cloner.copyDb(txn, todb, fromhost, cloneOptions, NULL)); } - } cmdCopyDB; + return true; + } + +} cmdCopyDB; -} // namespace +} // namespace |