diff options
author | Spencer T Brody <spencer@mongodb.com> | 2015-09-18 15:53:41 -0400 |
---|---|---|
committer | Spencer T Brody <spencer@mongodb.com> | 2015-09-24 13:36:59 -0400 |
commit | 03c414e87e5c3bc34230421163cdd06c9451389d (patch) | |
tree | 1fc24482ada7905ee380a2c0fd1c58a7fb89ed78 /src/mongo/s/client/sharding_connection_hook.cpp | |
parent | 3a7b1a9800f75706f35a290a8dee198bb29e3366 (diff) | |
download | mongo-03c414e87e5c3bc34230421163cdd06c9451389d.tar.gz |
SERVER-20498 Send config server optime to shards automatically on all commands via the OP_COMMAND metadata
Diffstat (limited to 'src/mongo/s/client/sharding_connection_hook.cpp')
-rw-r--r-- | src/mongo/s/client/sharding_connection_hook.cpp | 50 |
1 files changed, 38 insertions, 12 deletions
diff --git a/src/mongo/s/client/sharding_connection_hook.cpp b/src/mongo/s/client/sharding_connection_hook.cpp index 7a178055353..24cd459db27 100644 --- a/src/mongo/s/client/sharding_connection_hook.cpp +++ b/src/mongo/s/client/sharding_connection_hook.cpp @@ -42,7 +42,7 @@ #include "mongo/db/client.h" #include "mongo/rpc/get_status_from_command_result.h" #include "mongo/rpc/metadata/audit_metadata.h" -#include "mongo/rpc/metadata/config_server_response_metadata.h" +#include "mongo/rpc/metadata/config_server_metadata.h" #include "mongo/s/client/scc_fast_query_handler.h" #include "mongo/s/client/shard_registry.h" #include "mongo/s/cluster_last_error_info.h" @@ -55,6 +55,12 @@ namespace mongo { using std::string; namespace { + +// A hook that parses the reply metadata from every response to a command sent from a DBClient +// created by mongos or a sharding aware mongod and being used for sharded operations. +// Used by mongos to capture the GLE stats so that we can target the correct node when subsequent +// getLastError calls are made, as well as by both mongod and mongos to update the stored config +// server optime. Status _shardingReplyMetadataReader(const BSONObj& metadataObj, StringData hostString) { saveGLEStats(metadataObj, hostString); @@ -64,7 +70,7 @@ Status _shardingReplyMetadataReader(const BSONObj& metadataObj, StringData hostS } // If this host is a known shard of ours, look for a config server optime in the response // metadata to use to update our notion of the current config server optime. - auto responseStatus = rpc::ConfigServerResponseMetadata::readFromMetadata(metadataObj); + auto responseStatus = rpc::ConfigServerMetadata::readFromMetadata(metadataObj); if (!responseStatus.isOK()) { return responseStatus.getStatus(); } @@ -74,6 +80,32 @@ Status _shardingReplyMetadataReader(const BSONObj& metadataObj, StringData hostS } return Status::OK(); } + +// A hook that will append impersonated users to the metadata of every runCommand run by a DBClient +// created by mongos or a sharding aware mongod. mongos uses this information to send information +// to mongod so that the mongod can produce auditing records attributed to the proper authenticated +// user(s). +// Additionally, if the connection is sharding-aware, also appends the stored config server optime. +Status _shardingRequestMetadataWriter(bool shardedConn, + BSONObjBuilder* metadataBob, + StringData hostStringData) { + audit::writeImpersonatedUsersToMetadata(metadataBob); + if (!shardedConn) { + return Status::OK(); + } + + // Add config server optime to metadata sent to shards. + std::string hostString = hostStringData.toString(); + auto shard = grid.shardRegistry()->getShardNoReload(hostString); + invariant(shard); + if (shard->isConfig()) { + return Status::OK(); + } + rpc::ConfigServerMetadata(grid.shardRegistry()->getConfigOpTime()).writeToMetadata(metadataBob); + + return Status::OK(); +} + } // namespace ShardingConnectionHook::ShardingConnectionHook(bool shardedConnections) @@ -93,19 +125,13 @@ void ShardingConnectionHook::onCreate(DBClientBase* conn) { } if (_shardedConnections) { - // For every DBClient created by mongos, add a hook that will capture the response from - // commands we pass along from the client, so that we can target the correct node when - // subsequent getLastError calls are made by mongos. conn->setReplyMetadataReader(_shardingReplyMetadataReader); } - // For every DBClient created by mongos, add a hook that will append impersonated users - // to the end of every runCommand. mongod uses this information to produce auditing - // records attributed to the proper authenticated user(s). - conn->setRequestMetadataWriter([](BSONObjBuilder* metadataBob, StringData) -> Status { - audit::writeImpersonatedUsersToMetadata(metadataBob); - return Status::OK(); - }); + conn->setRequestMetadataWriter( + [this](BSONObjBuilder* metadataBob, StringData hostStringData) -> Status { + return _shardingRequestMetadataWriter(_shardedConnections, metadataBob, hostStringData); + }); // For every SCC created, add a hook that will allow fastest-config-first config reads if // the appropriate server options are set. |