diff options
author | Sara Golemon <sara.golemon@mongodb.com> | 2019-01-12 16:49:00 +0000 |
---|---|---|
committer | Sara Golemon <sara.golemon@mongodb.com> | 2019-01-15 20:11:17 +0000 |
commit | 7822bac17b8f166af4f02a339ed81a7ebb19060f (patch) | |
tree | c0ba78b3827fdaeeae8517b7e990dbfc378f2f08 /src/mongo/util | |
parent | 861f1f607c03ca78939ecc82afa91f9ec21b2bf4 (diff) | |
download | mongo-7822bac17b8f166af4f02a339ed81a7ebb19060f.tar.gz |
SERVER-38966 Convert Failpoint SCPs to use IDL
Diffstat (limited to 'src/mongo/util')
-rw-r--r-- | src/mongo/util/SConscript | 4 | ||||
-rw-r--r-- | src/mongo/util/fail_point_registry.cpp | 58 | ||||
-rw-r--r-- | src/mongo/util/fail_point_registry.h | 2 | ||||
-rw-r--r-- | src/mongo/util/fail_point_server_parameter.cpp | 94 | ||||
-rw-r--r-- | src/mongo/util/fail_point_server_parameter.h | 56 | ||||
-rw-r--r-- | src/mongo/util/fail_point_server_parameter.idl | 46 |
6 files changed, 96 insertions, 164 deletions
diff --git a/src/mongo/util/SConscript b/src/mongo/util/SConscript index 11bfa6313de..6c218980a0e 100644 --- a/src/mongo/util/SConscript +++ b/src/mongo/util/SConscript @@ -288,13 +288,13 @@ env.Library( source=[ "fail_point.cpp", "fail_point_registry.cpp", - "fail_point_server_parameter.cpp", "fail_point_service.cpp", + env.Idlc('fail_point_server_parameter.idl')[0], ], LIBDEPS=[ "$BUILD_DIR/mongo/base", "$BUILD_DIR/mongo/bson/util/bson_extract", - "$BUILD_DIR/mongo/db/server_parameters", + "$BUILD_DIR/mongo/idl/server_parameter", ], ) diff --git a/src/mongo/util/fail_point_registry.cpp b/src/mongo/util/fail_point_registry.cpp index 0a71e4a5f82..dcecebebdef 100644 --- a/src/mongo/util/fail_point_registry.cpp +++ b/src/mongo/util/fail_point_registry.cpp @@ -30,33 +30,33 @@ #include "mongo/util/fail_point_registry.h" -#include "mongo/util/fail_point_server_parameter.h" +#include "mongo/bson/json.h" +#include "mongo/util/fail_point_server_parameter_gen.h" +#include "mongo/util/fail_point_service.h" #include "mongo/util/map_util.h" #include "mongo/util/mongoutils/str.h" -using mongoutils::str::stream; - namespace mongo { -using std::string; +constexpr auto kFailPointServerParameterPrefix = "failpoint."_sd; FailPointRegistry::FailPointRegistry() : _frozen(false) {} -Status FailPointRegistry::addFailPoint(const string& name, FailPoint* failPoint) { +Status FailPointRegistry::addFailPoint(const std::string& name, FailPoint* failPoint) { if (_frozen) { - return Status(ErrorCodes::CannotMutateObject, "Registry is already frozen"); + return {ErrorCodes::CannotMutateObject, "Registry is already frozen"}; } if (_fpMap.count(name) > 0) { - return Status(ErrorCodes::Error(51006), - stream() << "Fail point already registered: " << name); + return {ErrorCodes::Error(51006), + str::stream() << "Fail point already registered: " << name}; } _fpMap.insert(make_pair(name, failPoint)); return Status::OK(); } -FailPoint* FailPointRegistry::getFailPoint(const string& name) const { +FailPoint* FailPointRegistry::getFailPoint(const std::string& name) const { return mapFindWithDefault(_fpMap, name, static_cast<FailPoint*>(nullptr)); } @@ -65,9 +65,45 @@ void FailPointRegistry::freeze() { } void FailPointRegistry::registerAllFailPointsAsServerParameters() { - for (auto it = _fpMap.begin(); it != _fpMap.end(); ++it) { + for (const auto& it : _fpMap) { // Intentionally leaked. - new FailPointServerParameter(it->first, it->second); + new FailPointServerParameter(it.first, ServerParameterType::kStartupOnly); } } + +FailPointServerParameter::FailPointServerParameter(StringData name, ServerParameterType spt) + : ServerParameter(kFailPointServerParameterPrefix.toString() + name.toString(), spt), + _data(getGlobalFailPointRegistry()->getFailPoint(name.toString())) { + invariant(name != "failpoint.*", "Failpoint prototype was auto-registered from IDL"); + invariant(_data != nullptr, str::stream() << "Unknown failpoint: " << name); +} + +void FailPointServerParameter::append(OperationContext* opCtx, + BSONObjBuilder& b, + const std::string& name) { + b << name << _data->toBSON(); +} + +Status FailPointServerParameter::setFromString(const std::string& str) { + BSONObj failPointOptions; + try { + failPointOptions = fromjson(str); + } catch (DBException& ex) { + return ex.toStatus(); + } + + auto swParsedOptions = FailPoint::parseBSON(failPointOptions); + if (!swParsedOptions.isOK()) { + return swParsedOptions.getStatus(); + } + + FailPoint::Mode mode; + FailPoint::ValType val; + BSONObj data; + std::tie(mode, val, data) = std::move(swParsedOptions.getValue()); + + _data->setMode(mode, val, data); + + return Status::OK(); } +} // namespace mongo diff --git a/src/mongo/util/fail_point_registry.h b/src/mongo/util/fail_point_registry.h index ea6e7629e9b..4b7a7a10f98 100644 --- a/src/mongo/util/fail_point_registry.h +++ b/src/mongo/util/fail_point_registry.h @@ -51,7 +51,7 @@ public: * * @return the status code under these circumstances: * OK - if successful. - * DuplicateKey - if the given name already exists in this registry. + * 51006 - if the given name already exists in this registry. * CannotMutateObject - if this registry is already frozen. */ Status addFailPoint(const std::string& name, FailPoint* failPoint); diff --git a/src/mongo/util/fail_point_server_parameter.cpp b/src/mongo/util/fail_point_server_parameter.cpp deleted file mode 100644 index f455565b48d..00000000000 --- a/src/mongo/util/fail_point_server_parameter.cpp +++ /dev/null @@ -1,94 +0,0 @@ - -/** - * Copyright (C) 2018-present MongoDB, Inc. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the Server Side Public License, version 1, - * as published by MongoDB, Inc. - * - * 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 - * Server Side Public License for more details. - * - * You should have received a copy of the Server Side Public License - * along with this program. If not, see - * <http://www.mongodb.com/licensing/server-side-public-license>. - * - * 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 Server Side 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/platform/basic.h" - -#include "mongo/util/fail_point_server_parameter.h" - -#include "mongo/base/status.h" -#include "mongo/bson/json.h" -#include "mongo/util/fail_point.h" -#include "mongo/util/fail_point_registry.h" -#include "mongo/util/fail_point_service.h" - -namespace mongo { - -const std::string FailPointServerParameter::failPointPrefix = "failpoint."; - -FailPointServerParameter::FailPointServerParameter(std::string name, FailPoint* failpoint) - : ServerParameter(ServerParameterSet::getGlobal(), - failPointPrefix + name, - true /* allowedToChangeAtStartup */, - false /* allowedToChangeAtRuntime */), - _failpoint(failpoint), - _failPointName(name) {} - -void FailPointServerParameter::append(OperationContext* opCtx, - BSONObjBuilder& b, - const std::string& name) { - b << name << _failpoint->toBSON(); -} - -Status FailPointServerParameter::set(const BSONElement& newValueElement) { - return {ErrorCodes::InternalError, - "FailPointServerParameter::setFromString() should be used instead of " - "FailPointServerParameter::set()"}; -} - -Status FailPointServerParameter::setFromString(const std::string& str) { - FailPointRegistry* registry = getGlobalFailPointRegistry(); - FailPoint* failPoint = registry->getFailPoint(_failPointName); - if (failPoint == NULL) { - return {ErrorCodes::BadValue, - str::stream() << _failPointName << " not found in fail point registry"}; - } - - BSONObj failPointOptions; - try { - failPointOptions = fromjson(str); - } catch (DBException& ex) { - return ex.toStatus(); - } - - FailPoint::Mode mode; - FailPoint::ValType val; - BSONObj data; - auto swParsedOptions = FailPoint::parseBSON(failPointOptions); - if (!swParsedOptions.isOK()) { - return swParsedOptions.getStatus(); - } - std::tie(mode, val, data) = std::move(swParsedOptions.getValue()); - - failPoint->setMode(mode, val, data); - - return Status::OK(); -} - -} // namespace mongo diff --git a/src/mongo/util/fail_point_server_parameter.h b/src/mongo/util/fail_point_server_parameter.h deleted file mode 100644 index 742a549b029..00000000000 --- a/src/mongo/util/fail_point_server_parameter.h +++ /dev/null @@ -1,56 +0,0 @@ - -/** - * Copyright (C) 2018-present MongoDB, Inc. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the Server Side Public License, version 1, - * as published by MongoDB, Inc. - * - * 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 - * Server Side Public License for more details. - * - * You should have received a copy of the Server Side Public License - * along with this program. If not, see - * <http://www.mongodb.com/licensing/server-side-public-license>. - * - * 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 Server Side 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/server_parameters.h" - -#include "mongo/base/status.h" -#include "mongo/util/fail_point.h" - -namespace mongo { - -class FailPointServerParameter : public ServerParameter { -public: - // When set via --setParameter on the command line, failpoint names must include this prefix. - static const std::string failPointPrefix; - - FailPointServerParameter(std::string name, FailPoint* failpoint); - - void append(OperationContext* opCtx, BSONObjBuilder& b, const std::string& name) override; - Status set(const BSONElement& newValueElement) override; - Status setFromString(const std::string& str) override; - -private: - FailPoint* _failpoint; // not owned here - std::string _failPointName; -}; - -} // namespace mongo diff --git a/src/mongo/util/fail_point_server_parameter.idl b/src/mongo/util/fail_point_server_parameter.idl new file mode 100644 index 00000000000..69741c971ef --- /dev/null +++ b/src/mongo/util/fail_point_server_parameter.idl @@ -0,0 +1,46 @@ +# Copyright (C) 2018-present MongoDB, Inc. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the Server Side Public License, version 1, +# as published by MongoDB, Inc. +# +# 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 +# Server Side Public License for more details. +# +# You should have received a copy of the Server Side Public License +# along with this program. If not, see +# <http://www.mongodb.com/licensing/server-side-public-license>. +# +# 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 Server Side 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. +# + +global: + cpp_namespace: "mongo" + cpp_includes: + - "mongo/util/fail_point.h" + +server_parameters: + # This represents the prototype for Failpoint ServerParameters. + # It contains condition: { expr: false } to avoid auto-instantiation. + # Instead, the FP registry will create N instances. + 'failpoint.*': + description: '[internal] Failpoint ServerParameter prototype' + set_at: startup + cpp_class: + name: FailPointServerParameter + data: 'FailPoint*' + override_ctor: true + condition: + expr: false |