summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Benvenuto <mark.benvenuto@mongodb.com>2020-09-02 20:34:40 -0400
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2020-09-03 02:44:31 +0000
commitc6b83aca3aa431aff366c27d29eccac850758cef (patch)
treead1d32c0f69526b07272561eb92f5f89baae922b
parent8e64b07e3bb363347ee2c11a56aba873365ed74a (diff)
downloadmongo-c6b83aca3aa431aff366c27d29eccac850758cef.tar.gz
SERVER-50367 Add IDL support for feature flags
-rw-r--r--buildscripts/idl/idl/binder.py19
-rw-r--r--buildscripts/idl/idl/parser.py21
-rw-r--r--buildscripts/idl/idl/syntax.py17
-rw-r--r--src/mongo/idl/SConscript2
-rw-r--r--src/mongo/idl/feature_flag_test.cpp60
-rw-r--r--src/mongo/idl/feature_flag_test.idl36
6 files changed, 155 insertions, 0 deletions
diff --git a/buildscripts/idl/idl/binder.py b/buildscripts/idl/idl/binder.py
index 4d9bd3ff79c..8e01fe83b02 100644
--- a/buildscripts/idl/idl/binder.py
+++ b/buildscripts/idl/idl/binder.py
@@ -1005,6 +1005,22 @@ def _bind_server_parameter(ctxt, param):
return None
+def _bind_feature_flags(param):
+ # type: (syntax.FeatureFlag) -> ast.ServerParameter
+ """Bind a FeatureFlag as a serverParameter setting."""
+ ast_param = ast.ServerParameter(param.file_name, param.line, param.column)
+ ast_param.name = param.name
+ ast_param.description = param.description
+
+ ast_param.set_at = "ServerParameterType::kStartupOnly"
+
+ ast_param.cpp_vartype = "bool"
+ ast_param.default = _bind_expression(param.default)
+ ast_param.cpp_varname = param.cpp_varname
+
+ return ast_param
+
+
def _is_invalid_config_short_name(name):
# type: (str) -> bool
"""Check if a given name is valid as a short name."""
@@ -1168,6 +1184,9 @@ def bind(parsed_spec):
if not struct.imported:
bound_spec.structs.append(_bind_struct(ctxt, parsed_spec, struct))
+ for feature_flag in parsed_spec.feature_flags:
+ bound_spec.server_parameters.append(_bind_feature_flags(feature_flag))
+
for server_parameter in parsed_spec.server_parameters:
bound_spec.server_parameters.append(_bind_server_parameter(ctxt, server_parameter))
diff --git a/buildscripts/idl/idl/parser.py b/buildscripts/idl/idl/parser.py
index 7a3a1e376d2..08f76bf0273 100644
--- a/buildscripts/idl/idl/parser.py
+++ b/buildscripts/idl/idl/parser.py
@@ -608,6 +608,25 @@ def _parse_server_parameter(ctxt, spec, name, node):
spec.server_parameters.append(param)
+def _parse_feature_flag(ctxt, spec, name, node):
+ # type: (errors.ParserContext, syntax.IDLSpec, str, Union[yaml.nodes.MappingNode, yaml.nodes.ScalarNode, yaml.nodes.SequenceNode]) -> None
+ """Parse a feature_flags section in the IDL file."""
+ if not ctxt.is_mapping_node(node, "feature_flags"):
+ return
+
+ param = syntax.FeatureFlag(ctxt.file_name, node.start_mark.line, node.start_mark.column)
+ param.name = name
+
+ _generic_parser(
+ ctxt, node, "feature_flags", param, {
+ "description": _RuleDesc('scalar', _RuleDesc.REQUIRED),
+ "cpp_varname": _RuleDesc('scalar'),
+ "default": _RuleDesc('scalar_or_mapping', mapping_parser_func=_parse_expression),
+ })
+
+ spec.feature_flags.append(param)
+
+
def _parse_config_option(ctxt, spec, name, node):
# type: (errors.ParserContext, syntax.IDLSpec, str, Union[yaml.nodes.MappingNode, yaml.nodes.ScalarNode, yaml.nodes.SequenceNode]) -> None
"""Parse a configs section in the IDL file."""
@@ -727,6 +746,8 @@ def _parse(stream, error_file_name):
_parse_mapping(ctxt, spec, second_node, "server_parameters", _parse_server_parameter)
elif first_name == "configs":
_parse_mapping(ctxt, spec, second_node, "configs", _parse_config_option)
+ elif first_name == "feature_flags":
+ _parse_mapping(ctxt, spec, second_node, "feature_flags", _parse_feature_flag)
else:
ctxt.add_unknown_root_node_error(first_node)
diff --git a/buildscripts/idl/idl/syntax.py b/buildscripts/idl/idl/syntax.py
index 3c9bb7bedf1..9b3530a7db7 100644
--- a/buildscripts/idl/idl/syntax.py
+++ b/buildscripts/idl/idl/syntax.py
@@ -67,6 +67,7 @@ class IDLSpec(object):
self.imports = None # type: Optional[Import]
self.server_parameters = [] # type: List[ServerParameter]
self.configs = [] # type: List[ConfigOption]
+ self.feature_flags = [] # type: List[FeatureFlag]
def parse_array_type(name):
@@ -527,6 +528,22 @@ class ServerParameter(common.SourceLocation):
super(ServerParameter, self).__init__(file_name, line, column)
+class FeatureFlag(common.SourceLocation):
+ """IDL FeatureFlag information."""
+
+ # pylint: disable=too-many-instance-attributes
+
+ def __init__(self, file_name, line, column):
+ # type: (str, int, int) -> None
+ """Construct a FeatureFlag."""
+ self.name = None # type: str
+ self.description = None # type: str
+ self.cpp_varname = None # type: str
+ self.default = None # type: Expression
+
+ super(FeatureFlag, self).__init__(file_name, line, column)
+
+
class GlobalInitializer(common.SourceLocation):
"""Initializer details for custom registration/storage."""
diff --git a/src/mongo/idl/SConscript b/src/mongo/idl/SConscript
index 28bcb2724b5..dd13816c007 100644
--- a/src/mongo/idl/SConscript
+++ b/src/mongo/idl/SConscript
@@ -32,11 +32,13 @@ env.CppUnitTest(
target='idl_test',
source=[
'config_option_test.cpp',
+ 'feature_flag_test.cpp',
'idl_test.cpp',
'server_parameter_specialized_test.cpp',
'server_parameter_with_storage_test.cpp',
env.Idlc('config_option_no_init_test.idl')[0],
env.Idlc('config_option_test.idl')[0],
+ env.Idlc('feature_flag_test.idl')[0],
env.Idlc('server_parameter_specialized_test.idl')[0],
env.Idlc('server_parameter_with_storage_test.idl')[0],
env.Idlc('unittest.idl')[0],
diff --git a/src/mongo/idl/feature_flag_test.cpp b/src/mongo/idl/feature_flag_test.cpp
new file mode 100644
index 00000000000..cb111756272
--- /dev/null
+++ b/src/mongo/idl/feature_flag_test.cpp
@@ -0,0 +1,60 @@
+/**
+ * Copyright (C) 2020-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/feature_flag_test_gen.h"
+#include "mongo/unittest/unittest.h"
+
+namespace mongo {
+
+namespace {
+
+ServerParameter* getServerParameter(const std::string& name) {
+ const auto& spMap = ServerParameterSet::getGlobal()->getMap();
+ const auto& spIt = spMap.find(name);
+ ASSERT(spIt != spMap.end());
+
+ auto* sp = spIt->second;
+ ASSERT(sp);
+ return sp;
+}
+
+TEST(IDLFeatureFlag, Basic) {
+ // true is set by "default" attribute in the IDL file.
+ ASSERT_EQ(feature_flags::gFeatureFlagToaster, true);
+
+ auto* featureFlagToaster = getServerParameter("featureFlagToaster");
+ ASSERT_OK(featureFlagToaster->setFromString("false"));
+ ASSERT_EQ(feature_flags::gFeatureFlagToaster, false);
+ ASSERT_NOT_OK(featureFlagToaster->setFromString("alpha"));
+}
+
+} // namespace
+} // namespace mongo
diff --git a/src/mongo/idl/feature_flag_test.idl b/src/mongo/idl/feature_flag_test.idl
new file mode 100644
index 00000000000..c756ca5a90f
--- /dev/null
+++ b/src/mongo/idl/feature_flag_test.idl
@@ -0,0 +1,36 @@
+# Copyright (C) 2020-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::feature_flags"
+
+feature_flags:
+ featureFlagToaster:
+ description: "Create a feature flag"
+ cpp_varname: gFeatureFlagToaster
+ default: true