MySQL 8.4.0
Source Code Documentation
dynamic_config.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQL_HARNESS_DYNAMIC_CONFIG_INCLUDED
27#define MYSQL_HARNESS_DYNAMIC_CONFIG_INCLUDED
28
29#include <map>
30#include <string>
31#include <variant>
32
33#ifdef RAPIDJSON_NO_SIZETYPEDEFINE
34#include "my_rapidjson_size_t.h"
35#endif
36
37#include <rapidjson/document.h>
38
39#include "harness_export.h"
40
41namespace mysql_harness {
42
43/** @class DynamicConfig
44 *
45 * @brief Respresents the current Router configuration. It is initialized at
46 * start with the defaults and configuration from the configuration file(s).
47 */
48class HARNESS_EXPORT DynamicConfig {
49 public:
50 using OptionName = std::string;
51 // std::monotstate is used as "option not set"
53 std::variant<std::monostate, int64_t, bool, double, std::string>;
54
55 // The first srting is the plugin name. The second string is the plugin
56 // section key if there are multiple plugin instances.
57 // Examples:
58 // for metadata_cache <"metadata_cache", "">
59 // for routing:bootstrap_ro
60 // <"endoints", "bootstrap_ro">
61 // for filelog <"loggers", "filelog">
62 using SectionId = std::pair<std::string, std::string>;
63 using SectionOptions = std::map<OptionName, OptionValue>;
64
65 using JsonAllocator = rapidjson::CrtAllocator;
67 rapidjson::GenericDocument<rapidjson::UTF8<>, JsonAllocator>;
68
69 /**
70 * Sets a given option in a given section to a specific value.
71 *
72 * @param section_id identifier of a section for this operation
73 * @param option_name name of the option inside a section for this operation
74 * @param value value to be set
75 */
76 void set_option_configured(const SectionId &section_id,
77 const OptionName &option_name,
78 const OptionValue &value);
79
80 /**
81 * Sets a default for an option in a given section to a specific value.
82 *
83 * @param section_id identifier of a section for this operation
84 * @param option_name name of the option inside a section for this operation
85 * @param default_value_cluster default value for srtandalne cluster setup to
86 * be set
87 * @param default_value_clusterset default value for the clusterset setup to
88 * be set
89 */
90 void set_option_default(const SectionId &section_id,
91 const OptionName &option_name,
92 const OptionValue &default_value_cluster,
93 const OptionValue &default_value_clusterset);
94
95 /**
96 * Sets a default for an option in a given section to a specific value.
97 * Overload for more common case when cluster and clusterset values are the
98 * same.
99 *
100 * @param section_id identifier of a section for this operation
101 * @param option_name name of the option inside a section for this operation
102 * @param default_value default value for both srtandalne cluster and
103 * clusterset setup to be set
104 */
105 void set_option_default(const SectionId &section_id,
106 const OptionName &option_name,
107 const OptionValue &default_value);
108
109 /**
110 * @brief Type of the options stored in the dynamic configuration object.
111 */
112 enum class ValueType {
113 ConfiguredValue, // value currently configured
114 DefaultForCluster, // default value for the standalone cluster setup
115 DefaultForClusterSet // default value for the clusterset setup
116 };
117
118 /**
119 * @brief Return the current configuration options and their values stored in
120 * the dynamic configuration object.
121 *
122 * @param value_type type of the options to be returned
123 *
124 * @returns JSON Document containing all the options of a selected type.
125 */
126 JsonDocument get_json(const ValueType value_type) const;
127
128 /**
129 * @brief Return the current configuration options and their values stored in
130 * the dynamic configuration object as a string.
131 *
132 * @param value_type type of the options to be returned
133 *
134 * @returns JSON string containing all the options of a selected type.
135 */
136 std::string get_json_as_string(const ValueType value_type) const;
137
138 /**
139 * Returns a singleton object of DynamicConfig class.
140 */
141 static DynamicConfig &instance();
142
143 /**
144 * Clear the DynamicConfig object (remove all registered sections with their
145 * option values and defaults).
146 */
147 void clear();
148
149 private:
150 DynamicConfig() = default;
151 DynamicConfig(const DynamicConfig &) = delete;
153
154 void set_option(const ValueType value_type, const SectionId &section_id,
155 const OptionName &option_name, const OptionValue &value);
156
159 };
160 using Config = std::map<SectionId, SectionConfig>;
161
162 Config &get_config(const ValueType value_type);
163 Config const &get_config(const ValueType value_type) const;
164
165 // stores the currently configured values
167 // stores the defaults for the standalone cluster configuration
169 // stores the defaults for the clusterset configuration
171};
172
173} // namespace mysql_harness
174
175#endif // MYSQL_HARNESS_DYNAMIC_CONFIG_INCLUDED
Respresents the current Router configuration.
Definition: dynamic_config.h:48
std::variant< std::monostate, int64_t, bool, double, std::string > OptionValue
Definition: dynamic_config.h:53
DynamicConfig(const DynamicConfig &)=delete
std::pair< std::string, std::string > SectionId
Definition: dynamic_config.h:62
rapidjson::CrtAllocator JsonAllocator
Definition: dynamic_config.h:65
std::string OptionName
Definition: dynamic_config.h:50
std::map< SectionId, SectionConfig > Config
Definition: dynamic_config.h:160
Config defaults_clusterset_
Definition: dynamic_config.h:170
std::map< OptionName, OptionValue > SectionOptions
Definition: dynamic_config.h:63
DynamicConfig & operator=(const DynamicConfig &)=delete
Config configured_
Definition: dynamic_config.h:166
rapidjson::GenericDocument< rapidjson::UTF8<>, JsonAllocator > JsonDocument
Definition: dynamic_config.h:67
Config defaults_cluster_
Definition: dynamic_config.h:168
ValueType
Type of the options stored in the dynamic configuration object.
Definition: dynamic_config.h:112
rapidjson::GenericDocument< rapidjson::UTF8<>, JsonAllocator > JsonDocument
Definition: dynamic_config.cc:46
Define rapidjson::SizeType to be std::size_t.
uint16_t value_type
Definition: vt100.h:184
Definition: common.h:42
static mysql_service_status_t clear(reference_caching_channel channel) noexcept
Definition: component.cc:146
Definition: dynamic_config.h:157
SectionOptions options
Definition: dynamic_config.h:158