MySQL 26.10.0
Source Code Documentation
mrs_component_backend.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2026, 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 MRS_COMPONENT_BACKEND_H
27#define MRS_COMPONENT_BACKEND_H
28
29#include <sys/types.h>
30#include <atomic>
31#include <chrono>
32#include <cstdint>
33#include <mutex>
34#include <optional>
35#include <thread>
36
38#include "dim.h"
42
43namespace mrs_component {
44
45enum class LogLevel : uint8_t { ERROR, WARNING, DEBUG };
46
47inline std::string to_string(LogLevel level) {
48 switch (level) {
49 case LogLevel::ERROR:
50 return "error";
52 return "warning";
53 case LogLevel::DEBUG:
54 return "debug";
55 }
56 throw std::logic_error("Unknown log level");
57}
58
59inline LogLevel log_level_from_string(const std::string &level_str) {
60 if (level_str == "1") return LogLevel::ERROR;
61 if (level_str == "2") return LogLevel::WARNING;
62 if (level_str == "3") return LogLevel::DEBUG;
63
64 throw std::logic_error("Unexpected value for @@log_error_verbosity: " +
65 level_str);
66}
67
69 struct MRSAccount {
70 std::string username;
71 std::string provider_role;
72 };
73
74 std::string http_port{"0"};
75 std::string logging_folder; // Empty defaults to stdout
76 std::string local_address;
77 // metadata_account, used to access MRS metadata in
78 // `mysql_rest_service_metadata`
80 .username = "mysql_mrs_metadata",
81 .provider_role = "mysql_rest_service_meta_provider"};
82
83 // data_account: used to execute limited SQL operations needed to serve REST
84 // endpoints
85 MRSAccount data_account{.username = "mysql_mrs_data",
86 .provider_role = "mysql_rest_service_data_provider"};
87 std::string ssl_cert;
88 std::string ssl_key;
89 std::string ssl_cipher;
90 unsigned long io_worker_threads{0};
91 uint64_t router_id = 0;
92 std::string name; // @@hostname:http_port
93};
94
96 std::string priv_user, user;
97 std::string priv_hostname;
98};
99
100enum class AccountProvisioning : std::uint8_t {
101 // Normal lifecycle starts require the internal accounts to exist already.
103 // The automatic install-time start may provision accounts using the
104 // component installer's security context.
106};
107
108/**
109 Owns and controls the embedded MRS backend lifecycle.
110*/
112 public:
113 Backend() = default;
114 ~Backend() override = default;
115 Backend(const Backend &src) = delete;
116 Backend(Backend &&src) = delete;
117 Backend &operator=(const Backend &src) = delete;
118 Backend &operator=(Backend &&src) = delete;
119
120 // Enables lifecycle requests after component initialization has registered
121 // every SQL interface. shutdown() disables them before teardown.
123
124 // Accepts an asynchronous start. Repeated starts are idempotent.
125 int start(AccountProvisioning account_provisioning);
126
127 // Stops the current lifecycle synchronously, then accepts a new
128 // asynchronous start.
129 int restart();
130
131 // Stops the current lifecycle synchronously. Repeated stops are idempotent.
132 int stop();
133
134 // Permanently disables lifecycle requests for this component instance and
135 // synchronously releases the backend during component teardown.
136 int shutdown();
137
138 // Returns the coarse lifecycle state exposed by mysql_rest_service.state.
139 [[nodiscard]] const char *lifecycle_state_name() const;
140
142
143 private:
144 // Coarse externally visible lifecycle state. Internal startup stages and
145 // individual metadata or request failures are intentionally not represented.
146 enum class LifecycleState : std::uint8_t {
147 kStopped,
148 kStarting,
150 kReady,
151 kStopping,
152 kError,
153 };
154
155 // Raw runtime operations. Their callers serialize access with
156 // m_lifecycle_operation_mtx.
157 int start_backend(AccountProvisioning account_provisioning);
158 int stop_backend();
159 void log_disabled_lifecycle_operation_rejection(const char *operation) const;
163
166 bool provision_accounts(bool ignore_missing_provider_role_error) const;
167 void configure_accounts(bool retry_account_provisioning,
168 bool validate_metadata_account);
169 uint64_t populate_mrs_metadata() const;
171 void setup_logger(mysql_harness::DIM &dim) const;
172 void configure_loader(mysql_harness::DIM &dim) const;
175 void breakable_sleep(std::chrono::seconds duration) const;
176 bool wait_for_metadata_schema(const std::string &user,
177 const std::string &host,
178 bool validate_metadata_account);
179 void run(std::promise<int> p, bool has_bootstrap_context);
180
182 void init_builtins();
183
187
188 // Serializes complete start, restart, stop, and shutdown operations.
190
191 // Guards the externally visible state and lifecycle-admission flag. It is
192 // released before backend cancellation and thread joins.
193 mutable std::mutex m_lifecycle_state_mtx;
196
197 // Synchronizes Loader creation between run() and stop_backend().
199
200 std::unique_ptr<mysql_harness::Loader> m_loader;
201 std::unique_ptr<ComponentEmbeddedRuntimeServices> m_embedded_runtime_services;
202 // Thread handling
204 std::atomic<bool> m_backend_thd_stop_requested{false};
205 // Retain the installer identity while install-time account provisioning is
206 // pending. Lifecycle retries may continue that operation, but cannot provide
207 // replacement provisioning authority.
208 std::optional<CurrentSecurityCtx> m_deferred_account_provisioning_context;
209#if !defined(NDEBUG)
211 // MTR synchronization hook. When enabled at backend start, pause the
212 // MRS-ready callback until stop() requests shutdown or an idempotent start
213 // explicitly releases it.
216#endif
217
219};
220
221} // namespace mrs_component
222
223#endif /* MRS_COMPONENT_BACKEND_H */
Definition: mrs_start_observer.h:33
Owns and controls the embedded MRS backend lifecycle.
Definition: mrs_component_backend.h:111
void reset_embedded_runtime_integration()
Definition: mrs_component_backend.cc:1210
LifecycleState m_lifecycle_state
Definition: mrs_component_backend.h:194
LifecycleState
Definition: mrs_component_backend.h:146
CurrentSecurityCtx get_current_sec_ctx() const
Definition: mrs_component_backend.h:141
void install_embedded_runtime_integration()
Definition: mrs_component_backend.cc:1143
void run(std::promise< int > p, bool has_bootstrap_context)
Definition: mrs_component_backend.cc:1313
const char * lifecycle_state_name() const
Definition: mrs_component_backend.cc:552
void fetch_current_sec_ctx()
Definition: mrs_component_backend.cc:118
void enable_lifecycle_operations()
Definition: mrs_component_backend.cc:541
CurrentSecurityCtx m_sec_ctx
Definition: mrs_component_backend.h:218
std::unique_ptr< mysql_harness::Loader > m_loader
Definition: mrs_component_backend.h:200
mrs::MrsStartResult on_mrs_start() override
Definition: mrs_component_backend.cc:1157
Backend & operator=(Backend &&src)=delete
void report_backend_error()
Definition: mrs_component_backend.cc:794
int restart()
Definition: mrs_component_backend.cc:649
mysql_harness::DIM & init_DIM()
Definition: mrs_component_backend.cc:960
bool m_lifecycle_admission_open
Definition: mrs_component_backend.h:195
void configure_loader(mysql_harness::DIM &dim) const
Definition: mrs_component_backend.cc:1005
std::mutex m_lifecycle_operation_mtx
Definition: mrs_component_backend.h:189
int start_backend(AccountProvisioning account_provisioning)
Definition: mrs_component_backend.cc:803
mysql_harness::logging::DomainLogger m_logger
Definition: mrs_component_backend.h:186
std::unique_ptr< ComponentEmbeddedRuntimeServices > m_embedded_runtime_services
Definition: mrs_component_backend.h:201
void configure_accounts(bool retry_account_provisioning, bool validate_metadata_account)
Definition: mrs_component_backend.cc:399
void init_builtins()
Definition: mrs_component_backend.cc:413
std::thread m_service_thread
Definition: mrs_component_backend.h:203
void log_disabled_lifecycle_operation_rejection(const char *operation) const
Definition: mrs_component_backend.cc:546
void breakable_sleep(std::chrono::seconds duration) const
Definition: mrs_component_backend.cc:1216
std::atomic< bool > m_backend_thd_stop_requested
Definition: mrs_component_backend.h:204
int stop_backend()
Definition: mrs_component_backend.cc:1519
bool provision_accounts(bool ignore_missing_provider_role_error) const
Definition: mrs_component_backend.cc:381
~Backend() override=default
Backend(const Backend &src)=delete
std::mutex m_loader_create_mtx
Definition: mrs_component_backend.h:198
bool m_debug_pause_before_runtime_status_publication
Definition: mrs_component_backend.h:214
std::mutex m_lifecycle_state_mtx
Definition: mrs_component_backend.h:193
bool wait_for_metadata_schema(const std::string &user, const std::string &host, bool validate_metadata_account)
Definition: mrs_component_backend.cc:1224
int shutdown()
Definition: mrs_component_backend.cc:736
void report_waiting_for_metadata()
Definition: mrs_component_backend.cc:777
LogLevel m_log_level
Definition: mrs_component_backend.h:185
Backend(Backend &&src)=delete
void setup_logger(mysql_harness::DIM &dim) const
Definition: mrs_component_backend.cc:990
std::optional< CurrentSecurityCtx > m_deferred_account_provisioning_context
Definition: mrs_component_backend.h:208
uint64_t populate_mrs_metadata() const
Definition: mrs_component_backend.cc:434
bool m_debug_pause_before_account_provisioning
Definition: mrs_component_backend.h:210
Backend & operator=(const Backend &src)=delete
void reset_process_state_component() const
Definition: mrs_component_backend.cc:978
void report_metadata_ready()
Definition: mrs_component_backend.cc:785
std::atomic< bool > m_debug_resume_runtime_status_publication
Definition: mrs_component_backend.h:215
int start(AccountProvisioning account_provisioning)
Definition: mrs_component_backend.cc:574
int stop()
Definition: mrs_component_backend.cc:700
std::thread m_backend_starter_thread
Definition: mrs_component_backend.h:203
RouterConfig m_router_config
Definition: mrs_component_backend.h:184
Definition: dim.h:49
const char * p
Definition: ctype-mb.cc:1227
const char * host
Definition: mysqladmin.cc:66
std::chrono::seconds seconds
Definition: authorize_manager.cc:71
Definition: mrs_component.cc:137
std::string to_string(LogLevel level)
Definition: mrs_component_backend.h:47
AccountProvisioning
Definition: mrs_component_backend.h:100
LogLevel log_level_from_string(const std::string &level_str)
Definition: mrs_component_backend.h:59
LogLevel
Definition: mrs_component_backend.h:45
MrsStartResult
Definition: mrs_start_observer.h:31
static const char *const level_str[]
Definition: handler.cc:55
Definition: mrs_component_backend.h:95
std::string priv_hostname
Definition: mrs_component_backend.h:97
std::string user
Definition: mrs_component_backend.h:96
std::string priv_user
Definition: mrs_component_backend.h:96
Definition: mrs_component_backend.h:69
std::string provider_role
Definition: mrs_component_backend.h:71
std::string username
Definition: mrs_component_backend.h:70
Definition: mrs_component_backend.h:68
std::string http_port
Definition: mrs_component_backend.h:74
std::string local_address
Definition: mrs_component_backend.h:76
uint64_t router_id
Definition: mrs_component_backend.h:91
std::string ssl_cipher
Definition: mrs_component_backend.h:89
std::string name
Definition: mrs_component_backend.h:92
std::string ssl_key
Definition: mrs_component_backend.h:88
MRSAccount data_account
Definition: mrs_component_backend.h:85
std::string ssl_cert
Definition: mrs_component_backend.h:87
std::string logging_folder
Definition: mrs_component_backend.h:75
MRSAccount metadata_account
Definition: mrs_component_backend.h:79
unsigned long io_worker_threads
Definition: mrs_component_backend.h:90