diff options
author | Randolph Tan <randolph@10gen.com> | 2015-09-16 18:36:14 -0400 |
---|---|---|
committer | Randolph Tan <randolph@10gen.com> | 2015-09-17 15:59:59 -0400 |
commit | 85cfca58ffe522f13118d715532f7eb8a45ad0c1 (patch) | |
tree | 4ec4c2fbd23fe8d40caa01adb677614c6cb32f12 /src/mongo/rpc | |
parent | 815f7e0f2ebe698061cbe494ed2184c2ab35eedc (diff) | |
download | mongo-85cfca58ffe522f13118d715532f7eb8a45ad0c1.tar.gz |
SERVER-19855 Make shards append last known config opTime on command metadata response
Diffstat (limited to 'src/mongo/rpc')
4 files changed, 227 insertions, 0 deletions
diff --git a/src/mongo/rpc/SConscript b/src/mongo/rpc/SConscript index 075fa5a3eca..2c7617943a6 100644 --- a/src/mongo/rpc/SConscript +++ b/src/mongo/rpc/SConscript @@ -139,6 +139,7 @@ env.Library( source=[ 'metadata.cpp', 'metadata/audit_metadata.cpp', + 'metadata/config_server_response_metadata.cpp', 'metadata/server_selection_metadata.cpp', 'metadata/sharding_metadata.cpp', 'metadata/repl_set_metadata.cpp', @@ -192,3 +193,11 @@ env.CppUnitTest( LIBDEPS=['metadata'] ) +env.CppUnitTest( + target='config_server_response_metadata_test', + source=[ + 'metadata/config_server_response_metadata_test.cpp', + ], + LIBDEPS=['metadata'] +) + diff --git a/src/mongo/rpc/metadata/config_server_response_metadata.cpp b/src/mongo/rpc/metadata/config_server_response_metadata.cpp new file mode 100644 index 00000000000..e47b62d8598 --- /dev/null +++ b/src/mongo/rpc/metadata/config_server_response_metadata.cpp @@ -0,0 +1,78 @@ +/** + * 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_response_metadata.h" + +#include "mongo/bson/util/bson_check.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 kRootFieldName[] = "configsvr"; +const char kOpTimeFieldName[] = "opTime"; + +} // unnamed namespace + +ConfigServerResponseMetadata::ConfigServerResponseMetadata(OpTime opTime) + : _opTime(std::move(opTime)) {} + +StatusWith<ConfigServerResponseMetadata> ConfigServerResponseMetadata::readFromMetadata( + const BSONObj& metadataObj) { + BSONElement configMetadataElement; + + Status status = + bsonExtractTypedField(metadataObj, kRootFieldName, Object, &configMetadataElement); + if (!status.isOK()) { + return status; + } + + BSONObj configMetadataObj = configMetadataElement.Obj(); + + repl::OpTime opTime; + status = bsonExtractOpTimeField(configMetadataObj, kOpTimeFieldName, &opTime); + if (!status.isOK()) { + return status; + } + + return ConfigServerResponseMetadata(std::move(opTime)); +} + +void ConfigServerResponseMetadata::writeToMetadata(BSONObjBuilder* builder) const { + BSONObjBuilder configMetadataBuilder(builder->subobjStart(kRootFieldName)); + _opTime.append(&configMetadataBuilder, kOpTimeFieldName); +} + +} // namespace rpc +} // namespace mongo diff --git a/src/mongo/rpc/metadata/config_server_response_metadata.h b/src/mongo/rpc/metadata/config_server_response_metadata.h new file mode 100644 index 00000000000..25e052c5c4b --- /dev/null +++ b/src/mongo/rpc/metadata/config_server_response_metadata.h @@ -0,0 +1,70 @@ +/** + * 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 "mongo/db/repl/optime.h" + +namespace mongo { + +class BSONObj; +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. + */ +class ConfigServerResponseMetadata { +public: + explicit ConfigServerResponseMetadata(repl::OpTime opTime); + + /** + * format: + * configsvr: { + * opTime: {ts: Timestamp(0, 0), t: 0} + * } + */ + static StatusWith<ConfigServerResponseMetadata> readFromMetadata(const BSONObj& doc); + void writeToMetadata(BSONObjBuilder* builder) const; + + /** + * Returns the OpTime of the most recent operation on the config servers that this + * shard has seen. + */ + repl::OpTime getOpTime() const { + return _opTime; + } + +private: + repl::OpTime _opTime; +}; + +} // namespace rpc +} // namespace mongo diff --git a/src/mongo/rpc/metadata/config_server_response_metadata_test.cpp b/src/mongo/rpc/metadata/config_server_response_metadata_test.cpp new file mode 100644 index 00000000000..fcfc206b839 --- /dev/null +++ b/src/mongo/rpc/metadata/config_server_response_metadata_test.cpp @@ -0,0 +1,70 @@ +/** + * 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/db/jsobj.h" +#include "mongo/rpc/metadata/config_server_response_metadata.h" +#include "mongo/unittest/unittest.h" + +namespace mongo { +namespace rpc { +namespace { + +using repl::OpTime; + +TEST(ConfigSvrMetadataTest, Roundtrip) { + OpTime opTime(Timestamp(1234, 100), 5); + ConfigServerResponseMetadata metadata(opTime); + + ASSERT_EQ(opTime, metadata.getOpTime()); + + BSONObjBuilder builder; + metadata.writeToMetadata(&builder); + + BSONObj expectedObj( + BSON("configsvr" << BSON( + "opTime" << BSON("ts" << opTime.getTimestamp() << "t" << opTime.getTerm())))); + + BSONObj serializedObj = builder.obj(); + ASSERT_EQ(expectedObj, serializedObj); + + auto cloneStatus = ConfigServerResponseMetadata::readFromMetadata(serializedObj); + ASSERT_OK(cloneStatus.getStatus()); + + const auto& clonedMetadata = cloneStatus.getValue(); + ASSERT_EQ(opTime, clonedMetadata.getOpTime()); + + BSONObjBuilder clonedBuilder; + clonedMetadata.writeToMetadata(&clonedBuilder); + + BSONObj clonedSerializedObj = clonedBuilder.obj(); + ASSERT_EQ(expectedObj, clonedSerializedObj); +} + +} // unnamed namespace +} // namespace rpc +} // namespace mongo |