MySQL 8.0.40
Source Code Documentation
dynamic_state.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018, 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_STATE_INCLUDED
27#define MYSQL_HARNESS_DYNAMIC_STATE_INCLUDED
28
29#include <fstream>
30#include <functional>
31#include <iterator>
32#include <list>
33#include <map>
34#include <memory>
35#include <mutex>
36#include <stdexcept>
37#include <string>
38#include <vector>
39
40#include "harness_export.h"
41
42#ifdef RAPIDJSON_NO_SIZETYPEDEFINE
43#include "my_rapidjson_size_t.h"
44#endif
45
46#include <rapidjson/document.h>
47
48namespace mysql_harness {
49
50// default allocator for rapidJson (MemoryPoolAllocator) is broken for
51// SparcSolaris
52using JsonAllocator = rapidjson::CrtAllocator;
53using JsonValue = rapidjson::GenericValue<rapidjson::UTF8<>, JsonAllocator>;
54
55/**
56 * @brief DynamicState represents a MySQLRouter dynamic state object.
57 *
58 * It's meant to be used as a singleton that provides methods to read/update
59 * sections from the specific modules requiring saving their runtime state.
60 *
61 * It handles the file handling synchronization, versioning and validation.
62 *
63 */
64class HARNESS_EXPORT DynamicState {
65 public:
66 /**
67 * @brief Creates and initializes dynamic state object.
68 *
69 * @param file_name path to the json file where the state is being stored
70 */
71 DynamicState(const std::string &file_name);
72
73 /**
74 * @brief Destructor.
75 */
77
78 /**
79 * @brief Loads the json state object from the associated file, overwrites the
80 * current with the file content.
81 *
82 * @return success of operation
83 * @retval true operation succeeded
84 * @retval false operation failed
85 */
86 bool load();
87
88 /**
89 * @brief Saves the json state object to the associated file, overwrites the
90 * the file content.
91 *
92 * @param is_clusterset true if the metadata is configured to work with a
93 * ClusterSet, false if a single Cluster
94 * @param pretty if true the json data is written in a human readable json
95 * format
96 *
97 * @return success of operation
98 * @retval true operation succeeded
99 * @retval false operation failed
100 */
101 bool save(bool is_clusterset, bool pretty = true);
102
103 /**
104 * @brief Saves the json state object to the output stream given as a
105 * parameter, overwrites the stream content.
106 *
107 * @param output_stream stream where json content should be written to
108 * @param is_clusterset true if the metadata is configured to work with a
109 * ClusterSet, false if a single Cluster
110 * @param pretty if true the json data is written in a human readable json
111 * format
112 *
113 * @return success of operation
114 * @retval true operation succeeded
115 * @retval false operation failed
116 */
117 bool save_to_stream(std::ostream &output_stream, bool is_clusterset,
118 bool pretty = true);
119
120 /**
121 * @brief Returns selected state object section by its name.
122 *
123 * @param section_name name of the section to retrieve
124 *
125 * @return pointer to the rapidJson object containing the section, nullptr if
126 * the section with a given name does not exist
127 */
128 std::unique_ptr<JsonValue> get_section(const std::string &section_name);
129
130 /**
131 * @brief Updates selected state object section.
132 *
133 * @param section_name name of the section to update
134 * @param value rapidJson object to replaces the section value
135 *
136 * @return success of operation
137 * @retval true operation succeeded
138 * @retval false operation failed
139 */
140 bool update_section(const std::string &section_name, JsonValue &&value);
141
142 private:
143 bool load_from_stream(std::istream &input_stream);
144
145 // throws std::runtime_error if does adhere to the schema
146 void ensure_valid_against_schema();
147 // throws std::runtime_error if version not compatible
148 void ensure_version_compatibility();
149
150 std::ifstream open_for_read();
151 std::ofstream open_for_write();
152
153 struct Pimpl;
154 std::unique_ptr<Pimpl> pimpl_;
155 std::string file_name_;
156};
157
158} // namespace mysql_harness
159
160#endif /* MYSQL_HARNESS_DYNAMIC_STATE_INCLUDED */
DynamicState represents a MySQLRouter dynamic state object.
Definition: dynamic_state.h:64
std::string file_name_
Definition: dynamic_state.h:155
std::unique_ptr< Pimpl > pimpl_
Definition: dynamic_state.h:153
static enum_log_json_pretty_print pretty
Definition: log_sink_json.cc:90
Define rapidjson::SizeType to be std::size_t.
bool load(THD *, const dd::String_type &fname, dd::String_type *buf)
Read an sdi file from disk and store in a buffer.
Definition: sdi_file.cc:308
std::string file_name(Log_file_id file_id)
Provides name of the log file with the given file id, e.g.
Definition: log0pre_8_0_30.cc:94
Definition: common.h:42
rapidjson::CrtAllocator JsonAllocator
Definition: dynamic_state.h:52
rapidjson::GenericValue< rapidjson::UTF8<>, JsonAllocator > JsonValue
Definition: dynamic_state.h:53
Definition: dynamic_state.cc:82