MySQL 8.0.31
Source Code Documentation
dynamic_state.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018, 2022, 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 also distributed 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 included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23*/
24
25#ifndef MYSQL_HARNESS_DYNAMIC_STATE_INCLUDED
26#define MYSQL_HARNESS_DYNAMIC_STATE_INCLUDED
27
28#include <fstream>
29#include <functional>
30#include <iterator>
31#include <list>
32#include <map>
33#include <memory>
34#include <mutex>
35#include <stdexcept>
36#include <string>
37#include <vector>
38
39#include "harness_export.h"
40
41#ifdef RAPIDJSON_NO_SIZETYPEDEFINE
42#include "my_rapidjson_size_t.h"
43#endif
44
45#include <rapidjson/document.h>
46
47namespace mysql_harness {
48
49// default allocator for rapidJson (MemoryPoolAllocator) is broken for
50// SparcSolaris
51using JsonAllocator = rapidjson::CrtAllocator;
52using JsonValue = rapidjson::GenericValue<rapidjson::UTF8<>, JsonAllocator>;
53
54/**
55 * @brief DynamicState represents a MySQLRouter dynamic state object.
56 *
57 * It's meant to be used as a singleton that provides methods to read/update
58 * sections from the specific modules requiring saving their runtime state.
59 *
60 * It handles the file handling synchronization, versioning and validation.
61 *
62 */
63class HARNESS_EXPORT DynamicState {
64 public:
65 /**
66 * @brief Creates and initializes dynamic state object.
67 *
68 * @param file_name path to the json file where the state is being stored
69 */
70 DynamicState(const std::string &file_name);
71
72 /**
73 * @brief Destructor.
74 */
76
77 /**
78 * @brief Loads the json state object from the associated file, overwrites the
79 * current with the file content.
80 *
81 * @return success of operation
82 * @retval true operation succeeded
83 * @retval false operation failed
84 */
85 bool load();
86
87 /**
88 * @brief Saves the json state object to the associated file, overwrites the
89 * the file content.
90 *
91 * @param is_clusterset true if the metadata is configured to work with a
92 * ClusterSet, false if a single Cluster
93 * @param pretty if true the json data is written in a human readable json
94 * format
95 *
96 * @return success of operation
97 * @retval true operation succeeded
98 * @retval false operation failed
99 */
100 bool save(bool is_clusterset, bool pretty = true);
101
102 /**
103 * @brief Saves the json state object to the output stream given as a
104 * parameter, overwrites the stream content.
105 *
106 * @param output_stream stream where json content should be written to
107 * @param is_clusterset true if the metadata is configured to work with a
108 * ClusterSet, false if a single Cluster
109 * @param pretty if true the json data is written in a human readable json
110 * format
111 *
112 * @return success of operation
113 * @retval true operation succeeded
114 * @retval false operation failed
115 */
116 bool save_to_stream(std::ostream &output_stream, bool is_clusterset,
117 bool pretty = true);
118
119 /**
120 * @brief Returns selected state object section by its name.
121 *
122 * @param section_name name of the section to retrieve
123 *
124 * @return pointer to the rapidJson object containing the section, nullptr if
125 * the section with a given name does not exist
126 */
127 std::unique_ptr<JsonValue> get_section(const std::string &section_name);
128
129 /**
130 * @brief Updates selected state object section.
131 *
132 * @param section_name name of the section to update
133 * @param value rapidJson object to replaces the section value
134 *
135 * @return success of operation
136 * @retval true operation succeeded
137 * @retval false operation failed
138 */
139 bool update_section(const std::string &section_name, JsonValue &&value);
140
141 private:
142 bool load_from_stream(std::istream &input_stream);
143
144 // throws std::runtime_error if does adhere to the schema
145 void ensure_valid_against_schema();
146 // throws std::runtime_error if version not compatible
147 void ensure_version_compatibility();
148
149 std::ifstream open_for_read();
150 std::ofstream open_for_write();
151
152 struct Pimpl;
153 std::unique_ptr<Pimpl> pimpl_;
154 std::string file_name_;
155};
156
157} // namespace mysql_harness
158
159#endif /* MYSQL_HARNESS_DYNAMIC_STATE_INCLUDED */
DynamicState represents a MySQLRouter dynamic state object.
Definition: dynamic_state.h:63
std::string file_name_
Definition: dynamic_state.h:154
std::unique_ptr< Pimpl > pimpl_
Definition: dynamic_state.h:152
static enum_log_json_pretty_print pretty
Definition: log_sink_json.cc:88
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:307
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:93
Definition: common.h:41
rapidjson::CrtAllocator JsonAllocator
Definition: dynamic_state.h:51
rapidjson::GenericValue< rapidjson::UTF8<>, JsonAllocator > JsonValue
Definition: dynamic_state.h:52
Definition: dynamic_state.cc:81