diff options
author | Sara Golemon <sara.golemon@mongodb.com> | 2018-10-23 23:19:17 +0000 |
---|---|---|
committer | Sara Golemon <sara.golemon@mongodb.com> | 2018-11-02 03:03:00 +0000 |
commit | 9ebba27faca2cd535ecde117a3d6f17e9ec787f6 (patch) | |
tree | d2818a84f3ce92d42e82e94a8ce10c2315b58ac8 /src/mongo/idl | |
parent | 5a8cfa984ff672502448544262263f216f1998ef (diff) | |
download | mongo-9ebba27faca2cd535ecde117a3d6f17e9ec787f6.tar.gz |
SERVER-37086 Create IDLServerParameter class
Diffstat (limited to 'src/mongo/idl')
-rw-r--r-- | src/mongo/idl/SConscript | 21 | ||||
-rw-r--r-- | src/mongo/idl/server_parameter.cpp | 95 | ||||
-rw-r--r-- | src/mongo/idl/server_parameter.h | 123 | ||||
-rw-r--r-- | src/mongo/idl/server_parameter_test.cpp | 89 |
4 files changed, 328 insertions, 0 deletions
diff --git a/src/mongo/idl/SConscript b/src/mongo/idl/SConscript index b7fdf916c29..5908d8f9d3d 100644 --- a/src/mongo/idl/SConscript +++ b/src/mongo/idl/SConscript @@ -27,3 +27,24 @@ env.CppUnitTest( '$BUILD_DIR/mongo/idl/idl_parser', ], ) + +env.Library( + target='server_parameter', + source=[ + 'server_parameter.cpp', + ], + LIBDEPS=[ + '$BUILD_DIR/mongo/base', + '$BUILD_DIR/mongo/db/server_parameters', + ], +) + +env.CppUnitTest( + target='idl_server_parameter_test', + source=[ + 'server_parameter_test.cpp', + ], + LIBDEPS=[ + 'server_parameter', + ], +) diff --git a/src/mongo/idl/server_parameter.cpp b/src/mongo/idl/server_parameter.cpp new file mode 100644 index 00000000000..8555b3a39ca --- /dev/null +++ b/src/mongo/idl/server_parameter.cpp @@ -0,0 +1,95 @@ +/** + * 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. + */ + +#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kControl + +#include "mongo/idl/server_parameter.h" + +#include "mongo/util/log.h" + +namespace mongo { +using SPT = ServerParameterType; + +IDLServerParameter::IDLServerParameter(StringData name, ServerParameterType paramType) + : ServerParameter(ServerParameterSet::getGlobal(), + name, + paramType == SPT::kStartupOnly || paramType == SPT::kStartupAndRuntime, + paramType == SPT::kRuntimeOnly || paramType == SPT::kStartupAndRuntime) {} + +void IDLServerParameter::append(OperationContext* opCtx, + BSONObjBuilder& b, + const std::string& name) { + invariant(_appendBSON, + "append() called on IDLServerParamter with no appendBSON implementation"); + _appendBSON(opCtx, &b, name); +} + +IDLServerParameterDeprecatedAlias::IDLServerParameterDeprecatedAlias(StringData name, + ServerParameter* sp) + : ServerParameter(ServerParameterSet::getGlobal(), + name, + sp->allowedToChangeAtStartup(), + sp->allowedToChangeAtRuntime()), + _sp(sp) {} + +Status IDLServerParameter::set(const BSONElement& newValueElement) { + invariant(_fromBSON, "set() called on IDLServerParamter with no fromBSON implementation"); + if (!_fromBSON) { + return {ErrorCodes::BadValue, "Unable to set parameter with no setter callback"}; + } + return _fromBSON(newValueElement); +} + +Status IDLServerParameter::setFromString(const std::string& str) { + invariant(_fromString, + "setFromString() called on IDLServerParamter with no setFromString implementation"); + return _fromString(str); +} + +void IDLServerParameterDeprecatedAlias::append(OperationContext* opCtx, + BSONObjBuilder& b, + const std::string& fieldName) { + warning() << "Use of deprecated server parameter '" << name() << "', please use '" + << _sp->name() << "' instead."; + _sp->append(opCtx, b, fieldName); +} + +Status IDLServerParameterDeprecatedAlias::set(const BSONElement& newValueElement) { + warning() << "Use of deprecared server parameter '" << name() << "', please use '" + << _sp->name() << "' instead."; + return _sp->set(newValueElement); +} + +Status IDLServerParameterDeprecatedAlias::setFromString(const std::string& str) { + warning() << "Use of deprecared server parameter '" << name() << "', please use '" + << _sp->name() << "' instead."; + return _sp->setFromString(str); +} + +} // namespace mongo diff --git a/src/mongo/idl/server_parameter.h b/src/mongo/idl/server_parameter.h new file mode 100644 index 00000000000..5022bb00327 --- /dev/null +++ b/src/mongo/idl/server_parameter.h @@ -0,0 +1,123 @@ +/** + * 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 +/* The contents of this file are meant to be used by + * code generated from idlc.py. + * + * It should not be instantiated directly from mongo code, + * rather parameters should be defined in .idl files. + */ + +#include <functional> +#include <string> + +#include "mongo/base/status.h" +#include "mongo/bson/bsonelement.h" +#include "mongo/bson/bsonobjbuilder.h" +#include "mongo/db/server_parameters.h" + +namespace mongo { + +/** + * Specialization of ServerParameter used by IDL generator. + */ +class IDLServerParameter : public ServerParameter { +public: + IDLServerParameter(StringData name, ServerParameterType paramType); + + /** + * Define a callback for populating a BSONObj with the current setting. + */ + using appendBSON_t = void(OperationContext*, BSONObjBuilder*, StringData); + void setAppendBSON(std::function<appendBSON_t> appendBSON) { + _appendBSON = std::move(appendBSON); + } + + /** + * Encode the setting into BSON object. + * + * Typically invoked by {getParameter:...} to produce a dictionary + * of SCP settings. + */ + void append(OperationContext* opCtx, BSONObjBuilder& b, const std::string& name) final; + + /** + * Define a callback for setting the value from a BSONElement. + */ + using fromBSON_t = Status(const BSONElement&); + void setFromBSON(std::function<fromBSON_t> fromBSON) { + _fromBSON = std::move(fromBSON); + } + + /** + * Update the underlying value using a BSONElement. + * + * Allows setting non-basic values (e.g. vector<string>) + * via the {setParameter: ...} call. + */ + Status set(const BSONElement& newValueElement) final; + + /** + * Define a callback for setting the value from a string. + */ + using fromString_t = Status(StringData); + void setFromString(std::function<fromString_t> fromString) { + _fromString = std::move(fromString); + } + + /** + * Update the underlying value from a string. + * + * Typically invoked from commandline --setParameter usage. + */ + Status setFromString(const std::string& str) final; + +protected: + std::function<appendBSON_t> _appendBSON; + std::function<fromBSON_t> _fromBSON; + std::function<fromString_t> _fromString; +}; + +/** + * Proxy instance for deprecated aliases of set parameters. + */ +class IDLServerParameterDeprecatedAlias : ServerParameter { +public: + IDLServerParameterDeprecatedAlias(StringData name, ServerParameter* sp); + + void append(OperationContext* opCtx, BSONObjBuilder& b, const std::string& name) final; + Status set(const BSONElement& newValueElement) final; + Status setFromString(const std::string& str) final; + +private: + ServerParameter* _sp; +}; + +} // namespace mongo diff --git a/src/mongo/idl/server_parameter_test.cpp b/src/mongo/idl/server_parameter_test.cpp new file mode 100644 index 00000000000..37ea4312577 --- /dev/null +++ b/src/mongo/idl/server_parameter_test.cpp @@ -0,0 +1,89 @@ +/** + * 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/idl/server_parameter.h" +#include "mongo/unittest/unittest.h" + +namespace mongo { +namespace { + +TEST(ServerParameter, setAppendBSON) { + IDLServerParameter param("setAppendBSON"_sd, ServerParameterType::kStartupOnly); + + param.setAppendBSON([](OperationContext*, BSONObjBuilder* builder, StringData name) { + builder->append(name, 42); + }); + BSONObjBuilder builder; + param.append(nullptr, builder, param.name()); + auto obj = builder.obj(); + ASSERT_EQ(obj.nFields(), 1); + ASSERT_EQ(obj[param.name()].Int(), 42); +} + +TEST(ServerParameter, setFromString) { + IDLServerParameter param("setFromString"_sd, ServerParameterType::kStartupOnly); + + param.setFromString([](StringData) { return Status::OK(); }); + ASSERT_OK(param.setFromString("A value")); + + param.setFromString([](StringData) { return Status(ErrorCodes::BadValue, "Can't set me."); }); + ASSERT_NOT_OK(param.setFromString("A value")); +} + +TEST(ServerParameter, setFromBSON) { + IDLServerParameter param("setFromBSON"_sd, ServerParameterType::kStartupOnly); + BSONElement elem; + + param.setFromBSON([](const BSONElement&) { return Status::OK(); }); + ASSERT_OK(param.set(elem)); + + param.setFromBSON( + [](const BSONElement&) { return Status(ErrorCodes::BadValue, "Can't set me."); }); + ASSERT_NOT_OK(param.set(elem)); +} + +TEST(ServerParameter, deprecatedAlias) { + IDLServerParameter param("basename"_sd, ServerParameterType::kStartupOnly); + IDLServerParameterDeprecatedAlias alias("aliasname"_sd, ¶m); + std::string value; + param.setFromString([&value](StringData str) { + value = str.toString(); + return Status::OK(); + }); + ASSERT_OK(param.setFromString("alpha")); + ASSERT_EQ("alpha", value); + + ASSERT_OK(alias.setFromString("bravo")); + ASSERT_EQ("bravo", value); +} + +} // namespace +} // namespace mongo |