summaryrefslogtreecommitdiff
path: root/src/mongo/rpc
diff options
context:
space:
mode:
authorSpencer T Brody <spencer@mongodb.com>2015-09-18 15:53:41 -0400
committerSpencer T Brody <spencer@mongodb.com>2015-09-24 13:36:59 -0400
commit03c414e87e5c3bc34230421163cdd06c9451389d (patch)
tree1fc24482ada7905ee380a2c0fd1c58a7fb89ed78 /src/mongo/rpc
parent3a7b1a9800f75706f35a290a8dee198bb29e3366 (diff)
downloadmongo-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/rpc')
-rw-r--r--src/mongo/rpc/SConscript7
-rw-r--r--src/mongo/rpc/metadata.cpp7
-rw-r--r--src/mongo/rpc/metadata/config_server_metadata.cpp (renamed from src/mongo/rpc/metadata/config_server_response_metadata.cpp)20
-rw-r--r--src/mongo/rpc/metadata/config_server_metadata.h (renamed from src/mongo/rpc/metadata/config_server_response_metadata.h)22
-rw-r--r--src/mongo/rpc/metadata/config_server_metadata_test.cpp (renamed from src/mongo/rpc/metadata/config_server_response_metadata_test.cpp)6
-rw-r--r--src/mongo/rpc/metadata/config_server_request_metadata.cpp65
-rw-r--r--src/mongo/rpc/metadata/config_server_request_metadata.h86
7 files changed, 37 insertions, 176 deletions
diff --git a/src/mongo/rpc/SConscript b/src/mongo/rpc/SConscript
index c15c4c69a68..2b171fe0ef9 100644
--- a/src/mongo/rpc/SConscript
+++ b/src/mongo/rpc/SConscript
@@ -139,8 +139,7 @@ env.Library(
source=[
'metadata.cpp',
'metadata/audit_metadata.cpp',
- 'metadata/config_server_request_metadata.cpp',
- 'metadata/config_server_response_metadata.cpp',
+ 'metadata/config_server_metadata.cpp',
'metadata/server_selection_metadata.cpp',
'metadata/sharding_metadata.cpp',
'metadata/repl_set_metadata.cpp',
@@ -195,9 +194,9 @@ env.CppUnitTest(
)
env.CppUnitTest(
- target='config_server_response_metadata_test',
+ target='config_server_metadata_test',
source=[
- 'metadata/config_server_response_metadata_test.cpp',
+ 'metadata/config_server_metadata_test.cpp',
],
LIBDEPS=['metadata']
)
diff --git a/src/mongo/rpc/metadata.cpp b/src/mongo/rpc/metadata.cpp
index 30b435e2872..f934e8afa2f 100644
--- a/src/mongo/rpc/metadata.cpp
+++ b/src/mongo/rpc/metadata.cpp
@@ -33,6 +33,7 @@
#include "mongo/client/dbclientinterface.h"
#include "mongo/db/jsobj.h"
#include "mongo/rpc/metadata/audit_metadata.h"
+#include "mongo/rpc/metadata/config_server_metadata.h"
#include "mongo/rpc/metadata/sharding_metadata.h"
#include "mongo/rpc/metadata/server_selection_metadata.h"
@@ -56,6 +57,12 @@ Status readRequestMetadata(OperationContext* txn, const BSONObj& metadataObj) {
}
AuditMetadata::get(txn) = std::move(swAuditMetadata.getValue());
+ auto configServerMetadata = ConfigServerMetadata::readFromMetadata(metadataObj);
+ if (!configServerMetadata.isOK()) {
+ return configServerMetadata.getStatus();
+ }
+ ConfigServerMetadata::get(txn) = std::move(configServerMetadata.getValue());
+
return Status::OK();
}
diff --git a/src/mongo/rpc/metadata/config_server_response_metadata.cpp b/src/mongo/rpc/metadata/config_server_metadata.cpp
index 14608f8d915..5119ed05c20 100644
--- a/src/mongo/rpc/metadata/config_server_response_metadata.cpp
+++ b/src/mongo/rpc/metadata/config_server_metadata.cpp
@@ -26,7 +26,7 @@
* it in the license file.
*/
-#include "mongo/rpc/metadata/config_server_response_metadata.h"
+#include "mongo/rpc/metadata/config_server_metadata.h"
#include "mongo/bson/util/bson_check.h"
#include "mongo/bson/util/bson_extract.h"
@@ -45,17 +45,19 @@ const char kOpTimeFieldName[] = "opTime";
} // unnamed namespace
-ConfigServerResponseMetadata::ConfigServerResponseMetadata(OpTime opTime)
- : _opTime(std::move(opTime)) {}
+const OperationContext::Decoration<ConfigServerMetadata> ConfigServerMetadata::get =
+ OperationContext::declareDecoration<ConfigServerMetadata>();
-StatusWith<ConfigServerResponseMetadata> ConfigServerResponseMetadata::readFromMetadata(
+ConfigServerMetadata::ConfigServerMetadata(OpTime opTime) : _opTime(std::move(opTime)) {}
+
+StatusWith<ConfigServerMetadata> ConfigServerMetadata::readFromMetadata(
const BSONObj& metadataObj) {
BSONElement configMetadataElement;
Status status =
bsonExtractTypedField(metadataObj, kRootFieldName, Object, &configMetadataElement);
if (status == ErrorCodes::NoSuchKey) {
- return ConfigServerResponseMetadata{};
+ return ConfigServerMetadata{};
} else if (!status.isOK()) {
return status;
}
@@ -68,13 +70,13 @@ StatusWith<ConfigServerResponseMetadata> ConfigServerResponseMetadata::readFromM
return status;
}
- return ConfigServerResponseMetadata(std::move(opTime));
+ return ConfigServerMetadata(std::move(opTime));
}
-void ConfigServerResponseMetadata::writeToMetadata(BSONObjBuilder* builder) const {
- invariant(_opTime.is_initialized());
+void ConfigServerMetadata::writeToMetadata(BSONObjBuilder* builder) const {
+ invariant(_opTime);
BSONObjBuilder configMetadataBuilder(builder->subobjStart(kRootFieldName));
- _opTime.get().append(&configMetadataBuilder, kOpTimeFieldName);
+ _opTime->append(&configMetadataBuilder, kOpTimeFieldName);
}
} // namespace rpc
diff --git a/src/mongo/rpc/metadata/config_server_response_metadata.h b/src/mongo/rpc/metadata/config_server_metadata.h
index 7d96fdc663d..86eabfddf9b 100644
--- a/src/mongo/rpc/metadata/config_server_response_metadata.h
+++ b/src/mongo/rpc/metadata/config_server_metadata.h
@@ -28,6 +28,7 @@
#pragma once
+#include "mongo/db/operation_context.h"
#include "mongo/db/repl/optime.h"
namespace mongo {
@@ -38,28 +39,31 @@ class BSONObjBuilder;
namespace rpc {
/**
- * This class encapsulates the response that mongod will return to mongos on every
- * command, containing metadata information about the config servers.
+ * This class encapsulates the metadata sent between shard mongods and mongos on every command
+ * request and response, containing metadata information about the config servers.
*
* format:
* configsvr: {
* opTime: {ts: Timestamp(0, 0), t: 0}
* }
*/
-class ConfigServerResponseMetadata {
+class ConfigServerMetadata {
public:
- ConfigServerResponseMetadata() = default;
- explicit ConfigServerResponseMetadata(repl::OpTime opTime);
+ static const OperationContext::Decoration<ConfigServerMetadata> get;
+
+ ConfigServerMetadata() = default;
+ explicit ConfigServerMetadata(repl::OpTime opTime);
/**
- * Parses the response metadata from the given metadata object.
+ * Parses the metadata from the given metadata object.
* Returns a non-ok status on parse error.
- * If no metadata is found, returns a default-constructed ConfigServerResponseMetadata.
+ * If no metadata is found, returns a default-constructed ConfigServerMetadata.
*/
- static StatusWith<ConfigServerResponseMetadata> readFromMetadata(const BSONObj& doc);
+ static StatusWith<ConfigServerMetadata> readFromMetadata(const BSONObj& doc);
/**
- * Writes the request metadata to the given BSONObjBuilder for building a command request.
+ * Writes the metadata to the given BSONObjBuilder for building a command request or response
+ * metadata.
* Only valid to call if _opTime is initialized.
*/
void writeToMetadata(BSONObjBuilder* builder) const;
diff --git a/src/mongo/rpc/metadata/config_server_response_metadata_test.cpp b/src/mongo/rpc/metadata/config_server_metadata_test.cpp
index 7e70749dc25..7f0a66a12d7 100644
--- a/src/mongo/rpc/metadata/config_server_response_metadata_test.cpp
+++ b/src/mongo/rpc/metadata/config_server_metadata_test.cpp
@@ -27,7 +27,7 @@
*/
#include "mongo/db/jsobj.h"
-#include "mongo/rpc/metadata/config_server_response_metadata.h"
+#include "mongo/rpc/metadata/config_server_metadata.h"
#include "mongo/unittest/unittest.h"
namespace mongo {
@@ -38,7 +38,7 @@ using repl::OpTime;
TEST(ConfigSvrMetadataTest, Roundtrip) {
OpTime opTime(Timestamp(1234, 100), 5);
- ConfigServerResponseMetadata metadata(opTime);
+ ConfigServerMetadata metadata(opTime);
ASSERT_EQ(opTime, metadata.getOpTime().get());
@@ -52,7 +52,7 @@ TEST(ConfigSvrMetadataTest, Roundtrip) {
BSONObj serializedObj = builder.obj();
ASSERT_EQ(expectedObj, serializedObj);
- auto cloneStatus = ConfigServerResponseMetadata::readFromMetadata(serializedObj);
+ auto cloneStatus = ConfigServerMetadata::readFromMetadata(serializedObj);
ASSERT_OK(cloneStatus.getStatus());
const auto& clonedMetadata = cloneStatus.getValue();
diff --git a/src/mongo/rpc/metadata/config_server_request_metadata.cpp b/src/mongo/rpc/metadata/config_server_request_metadata.cpp
deleted file mode 100644
index 0bdad35037f..00000000000
--- a/src/mongo/rpc/metadata/config_server_request_metadata.cpp
+++ /dev/null
@@ -1,65 +0,0 @@
-/**
- * Copyright (C) 2015 MongoDB Inc.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- * As a special exception, the copyright holders give permission to link the
- * code of portions of this program with the OpenSSL library under certain
- * conditions as described in each individual source file and distribute
- * linked combinations including the program with the OpenSSL library. You
- * must comply with the GNU Affero General Public License in all respects for
- * all of the code used other than as permitted herein. If you modify file(s)
- * with this exception, you may extend this exception to your version of the
- * file(s), but you are not obligated to do so. If you do not wish to do so,
- * delete this exception statement from your version. If you delete this
- * exception statement from all source files in the program, then also delete
- * it in the license file.
- */
-
-#include "mongo/rpc/metadata/config_server_request_metadata.h"
-
-#include "mongo/bson/util/bson_extract.h"
-#include "mongo/db/jsobj.h"
-#include "mongo/rpc/metadata.h"
-
-namespace mongo {
-namespace rpc {
-
-using repl::OpTime;
-
-namespace {
-const char kConfigsvrOpTimeFieldName[] = "configsvrOpTime";
-} // namespace
-
-ConfigServerRequestMetadata::ConfigServerRequestMetadata(OpTime opTime)
- : _opTime(std::move(opTime)) {}
-
-StatusWith<ConfigServerRequestMetadata> ConfigServerRequestMetadata::readFromCommand(
- const BSONObj& cmdObj) {
- repl::OpTime opTime;
- Status status = bsonExtractOpTimeField(cmdObj, kConfigsvrOpTimeFieldName, &opTime);
- if (status == ErrorCodes::NoSuchKey) {
- return ConfigServerRequestMetadata{};
- } else if (!status.isOK()) {
- return status;
- }
- return ConfigServerRequestMetadata(opTime);
-}
-
-void ConfigServerRequestMetadata::writeToCommand(BSONObjBuilder* builder) const {
- invariant(_opTime.is_initialized());
- _opTime->append(builder, kConfigsvrOpTimeFieldName);
-}
-
-} // namespace rpc
-} // namespace mongo
diff --git a/src/mongo/rpc/metadata/config_server_request_metadata.h b/src/mongo/rpc/metadata/config_server_request_metadata.h
deleted file mode 100644
index 33d74d7f64d..00000000000
--- a/src/mongo/rpc/metadata/config_server_request_metadata.h
+++ /dev/null
@@ -1,86 +0,0 @@
-/**
- * Copyright (C) 2015 MongoDB Inc.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- * As a special exception, the copyright holders give permission to link the
- * code of portions of this program with the OpenSSL library under certain
- * conditions as described in each individual source file and distribute
- * linked combinations including the program with the OpenSSL library. You
- * must comply with the GNU Affero General Public License in all respects for
- * all of the code used other than as permitted herein. If you modify file(s)
- * with this exception, you may extend this exception to your version of the
- * file(s), but you are not obligated to do so. If you do not wish to do so,
- * delete this exception statement from your version. If you delete this
- * exception statement from all source files in the program, then also delete
- * it in the license file.
- */
-
-#pragma once
-
-#include <boost/optional.hpp>
-
-#include "mongo/db/repl/optime.h"
-
-namespace mongo {
-
-class BSONObj;
-template <typename T>
-class StatusWith;
-
-namespace rpc {
-
-/**
- * This class encapsulates the extra information that mongos may attach to commands it sends to
- * mongods, containing metadata information about the config servers.
- *
- * format:
- * configsvrOpTime: {ts: Timestamp(0, 0), t: 0}
- *
- * TODO(SERVER-20442): Currently this extracts the config server information from the main command
- * description rather than the actual OP_COMMAND metadata section. Ideally this information
- * should be in the metadata, but we currently have no good way to add metadata to all commands
- * being *sent* to another server.
- */
-class ConfigServerRequestMetadata {
-public:
- ConfigServerRequestMetadata() = default;
- explicit ConfigServerRequestMetadata(repl::OpTime opTime);
-
- /**
- * Parses the request metadata from the given command object.
- * Returns a non-ok status on parse error.
- * If no metadata is found, returns a default-constructed ConfigServerRequestMetadata.
- */
- static StatusWith<ConfigServerRequestMetadata> readFromCommand(const BSONObj& doc);
-
- /**
- * Writes the request metadata to the given BSONObjBuilder for building a command request.
- * Only valid to call if _opTime is initialized.
- */
- void writeToCommand(BSONObjBuilder* builder) const;
-
- /**
- * Returns the OpTime of the most recent operation on the config servers that this
- * shard has seen.
- */
- boost::optional<repl::OpTime> getOpTime() const {
- return _opTime;
- }
-
-private:
- const boost::optional<repl::OpTime> _opTime = boost::none;
-};
-
-} // namespace rpc
-} // namespace mongo