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 | |
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')
-rw-r--r-- | src/mongo/db/dbcommands.cpp | 10 | ||||
-rw-r--r-- | src/mongo/rpc/SConscript | 9 | ||||
-rw-r--r-- | src/mongo/rpc/metadata/config_server_response_metadata.cpp | 78 | ||||
-rw-r--r-- | src/mongo/rpc/metadata/config_server_response_metadata.h | 70 | ||||
-rw-r--r-- | src/mongo/rpc/metadata/config_server_response_metadata_test.cpp | 70 | ||||
-rw-r--r-- | src/mongo/s/catalog/catalog_manager.h | 6 | ||||
-rw-r--r-- | src/mongo/s/catalog/catalog_manager_mock.cpp | 5 | ||||
-rw-r--r-- | src/mongo/s/catalog/catalog_manager_mock.h | 2 | ||||
-rw-r--r-- | src/mongo/s/catalog/forwarding_catalog_manager.cpp | 4 | ||||
-rw-r--r-- | src/mongo/s/catalog/forwarding_catalog_manager.h | 2 | ||||
-rw-r--r-- | src/mongo/s/catalog/legacy/catalog_manager_legacy.cpp | 5 | ||||
-rw-r--r-- | src/mongo/s/catalog/legacy/catalog_manager_legacy.h | 2 | ||||
-rw-r--r-- | src/mongo/s/catalog/replset/catalog_manager_replica_set.cpp | 5 | ||||
-rw-r--r-- | src/mongo/s/catalog/replset/catalog_manager_replica_set.h | 2 |
14 files changed, 269 insertions, 1 deletions
diff --git a/src/mongo/db/dbcommands.cpp b/src/mongo/db/dbcommands.cpp index d419627b40d..defa9c02d0c 100644 --- a/src/mongo/db/dbcommands.cpp +++ b/src/mongo/db/dbcommands.cpp @@ -90,8 +90,10 @@ #include "mongo/rpc/request_interface.h" #include "mongo/rpc/reply_builder_interface.h" #include "mongo/rpc/metadata.h" +#include "mongo/rpc/metadata/config_server_response_metadata.h" #include "mongo/rpc/metadata/server_selection_metadata.h" #include "mongo/rpc/metadata/sharding_metadata.h" +#include "mongo/s/grid.h" #include "mongo/s/stale_exception.h" // for SendStaleConfigException #include "mongo/scripting/engine.h" #include "mongo/util/fail_point_service.h" @@ -1391,6 +1393,7 @@ bool Command::run(OperationContext* txn, BSONObjBuilder metadataBob; + const bool isShardingAware = ShardingState::get(txn)->enabled(); bool isReplSet = replCoord->getReplicationMode() == repl::ReplicationCoordinator::modeReplSet; if (isReplSet) { repl::OpTime lastOpTimeFromClient = @@ -1400,12 +1403,17 @@ bool Command::run(OperationContext* txn, // For commands from mongos, append some info to help getLastError(w) work. // TODO: refactor out of here as part of SERVER-18326 - if (ShardingState::get(txn)->enabled()) { + if (isShardingAware) { rpc::ShardingMetadata(lastOpTimeFromClient.getTimestamp(), replCoord->getElectionId()) .writeToMetadata(&metadataBob); } } + if (isShardingAware) { + auto opTime = grid.catalogManager(txn)->getConfigOpTime(txn); + rpc::ConfigServerResponseMetadata(opTime).writeToMetadata(&metadataBob); + } + auto cmdResponse = replyBuilderBob.done(); replyBuilder->setMetadata(metadataBob.done()); 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 diff --git a/src/mongo/s/catalog/catalog_manager.h b/src/mongo/s/catalog/catalog_manager.h index 8b796582cb6..8dd15cc3d1d 100644 --- a/src/mongo/s/catalog/catalog_manager.h +++ b/src/mongo/s/catalog/catalog_manager.h @@ -114,6 +114,12 @@ public: virtual void advanceConfigOpTime(OperationContext* txn, repl::OpTime opTime) = 0; /** + * Returns the last known OpTime of the config server that supports replica sets. + * Returns the smallest possible OpTime if the catalog does not support it. + */ + virtual repl::OpTime getConfigOpTime(OperationContext* txn) = 0; + + /** * Returns what type of catalog manager this is - CSRS for the CatalogManagerReplicaSet and * SCCC for the CatalogManagerLegacy. */ diff --git a/src/mongo/s/catalog/catalog_manager_mock.cpp b/src/mongo/s/catalog/catalog_manager_mock.cpp index f8de3dd049f..532c137d48f 100644 --- a/src/mongo/s/catalog/catalog_manager_mock.cpp +++ b/src/mongo/s/catalog/catalog_manager_mock.cpp @@ -31,6 +31,7 @@ #include "mongo/s/catalog/catalog_manager_mock.h" #include "mongo/base/status.h" +#include "mongo/db/repl/optime.h" #include "mongo/s/catalog/type_collection.h" #include "mongo/s/catalog/type_database.h" #include "mongo/s/catalog/type_settings.h" @@ -201,4 +202,8 @@ Status CatalogManagerMock::initConfigVersion(OperationContext* txn) { return Status::OK(); } +repl::OpTime CatalogManagerMock::getConfigOpTime(OperationContext* txn) { + return repl::OpTime(); +} + } // namespace mongo diff --git a/src/mongo/s/catalog/catalog_manager_mock.h b/src/mongo/s/catalog/catalog_manager_mock.h index b8939ed764b..293477ed669 100644 --- a/src/mongo/s/catalog/catalog_manager_mock.h +++ b/src/mongo/s/catalog/catalog_manager_mock.h @@ -147,6 +147,8 @@ public: Status initConfigVersion(OperationContext* txn) override; + repl::OpTime getConfigOpTime(OperationContext* txn) override; + private: Status _checkDbDoesNotExist(OperationContext* txn, const std::string& dbName, diff --git a/src/mongo/s/catalog/forwarding_catalog_manager.cpp b/src/mongo/s/catalog/forwarding_catalog_manager.cpp index a382b5eac52..8b6ffc22b37 100644 --- a/src/mongo/s/catalog/forwarding_catalog_manager.cpp +++ b/src/mongo/s/catalog/forwarding_catalog_manager.cpp @@ -587,4 +587,8 @@ Status ForwardingCatalogManager::initConfigVersion(OperationContext* txn) { return retry(txn, [&] { return _actual->initConfigVersion(txn); }); } +repl::OpTime ForwardingCatalogManager::getConfigOpTime(OperationContext* txn) { + return retry(txn, [&] { return _actual->getConfigOpTime(txn); }); +} + } // namespace mongo diff --git a/src/mongo/s/catalog/forwarding_catalog_manager.h b/src/mongo/s/catalog/forwarding_catalog_manager.h index 47286688e62..e39866d6c48 100644 --- a/src/mongo/s/catalog/forwarding_catalog_manager.h +++ b/src/mongo/s/catalog/forwarding_catalog_manager.h @@ -219,6 +219,8 @@ private: Status initConfigVersion(OperationContext* txn) override; + repl::OpTime getConfigOpTime(OperationContext* txn) override; + template <typename Callable> auto retry(OperationContext* txn, Callable&& c) -> decltype(std::forward<Callable>(c)()); diff --git a/src/mongo/s/catalog/legacy/catalog_manager_legacy.cpp b/src/mongo/s/catalog/legacy/catalog_manager_legacy.cpp index 28bc0e4c3db..9738d7a456c 100644 --- a/src/mongo/s/catalog/legacy/catalog_manager_legacy.cpp +++ b/src/mongo/s/catalog/legacy/catalog_manager_legacy.cpp @@ -43,6 +43,7 @@ #include "mongo/db/client.h" #include "mongo/db/commands.h" #include "mongo/db/operation_context.h" +#include "mongo/db/repl/optime.h" #include "mongo/db/server_options.h" #include "mongo/executor/network_interface.h" #include "mongo/rpc/get_status_from_command_result.h" @@ -1343,4 +1344,8 @@ bool CatalogManagerLegacy::_isConsistentFromLastCheck() { return _consistentFromLastCheck; } +repl::OpTime CatalogManagerLegacy::getConfigOpTime(OperationContext* txn) { + return repl::OpTime(); +} + } // namespace mongo diff --git a/src/mongo/s/catalog/legacy/catalog_manager_legacy.h b/src/mongo/s/catalog/legacy/catalog_manager_legacy.h index 2e543212f67..12531629785 100644 --- a/src/mongo/s/catalog/legacy/catalog_manager_legacy.h +++ b/src/mongo/s/catalog/legacy/catalog_manager_legacy.h @@ -150,6 +150,8 @@ public: Status initConfigVersion(OperationContext* txn) override; + repl::OpTime getConfigOpTime(OperationContext* txn) override; + private: Status _checkDbDoesNotExist(OperationContext* txn, const std::string& dbName, diff --git a/src/mongo/s/catalog/replset/catalog_manager_replica_set.cpp b/src/mongo/s/catalog/replset/catalog_manager_replica_set.cpp index 3420ab45505..c2ed1bbb1be 100644 --- a/src/mongo/s/catalog/replset/catalog_manager_replica_set.cpp +++ b/src/mongo/s/catalog/replset/catalog_manager_replica_set.cpp @@ -1309,4 +1309,9 @@ bool CatalogManagerReplicaSet::_runReadCommand(OperationContext* txn, return Command::getStatusFromCommandResult(resultStatus.getValue()).isOK(); } +repl::OpTime CatalogManagerReplicaSet::getConfigOpTime(OperationContext* txn) { + stdx::lock_guard<stdx::mutex> lk(_mutex); + return _configOpTime; +} + } // namespace mongo diff --git a/src/mongo/s/catalog/replset/catalog_manager_replica_set.h b/src/mongo/s/catalog/replset/catalog_manager_replica_set.h index e41dfc5128c..3bb6d0383a4 100644 --- a/src/mongo/s/catalog/replset/catalog_manager_replica_set.h +++ b/src/mongo/s/catalog/replset/catalog_manager_replica_set.h @@ -144,6 +144,8 @@ public: Status initConfigVersion(OperationContext* txn) override; + repl::OpTime getConfigOpTime(OperationContext* txn) override; + private: Status _checkDbDoesNotExist(OperationContext* txn, const std::string& dbName, |