MySQL 26.10.0
Source Code Documentation
health_monitor_server.h
Go to the documentation of this file.
1/* Copyright (c) 2023, 2026, Oracle and/or its affiliates.
2
3This program is free software; you can redistribute it and/or modify
4it under the terms of the GNU General Public License, version 2.0,
5as published by the Free Software Foundation.
6
7This program is designed to work with certain software (including
8but not limited to OpenSSL) that is licensed under separate terms,
9as designated in a particular file or component or in included license
10documentation. The authors of MySQL hereby grant you an additional
11permission to link the program and your derivative works with the
12separately licensed software that they have either included with
13the program or referenced in the documentation.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License, version 2.0, for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#pragma once
25
26#include <shared_mutex>
27#include <string>
28#include <vector>
29
31#include "component_service.h"
32#include "health_monitor_query.h" // Result_Set
33
34namespace hm {
35
36typedef std::vector<std::string> Processlist_Ids;
37
38/**
39 Group Replication status.
40*/
41struct GR_Status {
42 GR_Status() = default;
43 bool valid{false};
48 std::string role() const;
49 std::string view_id;
50};
51
52/**
53 Group replication status listener.
54 Listens for changes to group replication status and roles (primary/secondary).
55*/
57 public:
58 static DEFINE_BOOL_METHOD(notify_member_role_change, (const char *));
59 static DEFINE_BOOL_METHOD(notify_member_state_change, (const char *));
60};
61
62// When an unfenced secondary that is out of disk space is promoted to primary,
63// the new primary will be fenced. The delay until GR sets SUPER_READ_ONLY = OFF
64// during switchover is unbounded. The Health Monitor must wait until
65// SUPER_READ_ONLY = OFF before fencing the server.
66
67// #define DEBUG_SWITCHOVER_LAG
68
69/**
70 Server utility class.
71 Provides server information, status and utilities.
72*/
73class Server {
74 public:
75 // Group replication
78 static GR_Status gr_status();
80 static void set_group_replication_status(const char *view_id);
81 static bool is_group_replication_primary();
84#ifdef DEBUG_SWITCHOVER_LAG
85 static bool m_debug_sro_new_primary;
86#endif
87
88 // Processlist
90
91 int kill_query(const std::string &pid, std::string &errmsg);
92 int kill_query(const std::string &pid);
93 int kill_connection(const std::string &pid, std::string &errmsg);
94 int kill_connection(const std::string &pid);
95
96 // System variables
97 static bool set_system_variable(const std::string &name,
98 const std::string &value);
99 static bool get_system_variable(const std::string &name, std::string &value);
100 static bool get_status_variable(const std::string &name, std::string &value);
101
102 static bool get_offline_mode(bool &error);
103 static std::string get_offline_mode_str();
104 static bool set_offline_mode();
105 static bool clear_offline_mode();
106
107 static bool get_super_read_only(bool &error);
108 static std::string get_super_read_only_str();
109 static bool set_super_read_only();
110 static bool clear_super_read_only();
111
112 static bool get_read_only(bool &error);
113 // READ_ONLY is set when SUPER_READ_ONLY is set
114 static bool clear_read_only();
115
116 static bool get_log_diagnostic_enable(bool &error);
117
118 // Utilities
119 static std::string &to_lower(std::string &str);
120
121 private:
124 static std::shared_mutex m_gr_mutex;
125
126#ifdef DEBUG_SWITCHOVER_LAG
127 // SRO counter to simulate failover lag
128 static bool m_debug_sro;
129 static int m_debug_sro_max_attempts;
130 static int m_debug_sro_count;
131 static bool m_debug_sro_value;
132#endif
133};
134
135} // namespace hm
Group replication status listener.
Definition: health_monitor_server.h:56
static mysql_service_status_t notify_member_state_change(const char *) noexcept
Definition: health_monitor_server.cc:110
static mysql_service_status_t notify_member_role_change(const char *) noexcept
Definition: health_monitor_server.cc:92
Server utility class.
Definition: health_monitor_server.h:73
static bool get_read_only(bool &error)
Get the value of READ_ONLY.
Definition: health_monitor_server.cc:559
static int unregister_group_member_status_listeners()
Unregister the group member status listener.
Definition: health_monitor_server.cc:147
static bool set_super_read_only()
Set SUPER_READ_ONLY = ON.
Definition: health_monitor_server.cc:542
static bool get_system_variable(const std::string &name, std::string &value)
Get server system variable.
Definition: health_monitor_server.cc:405
int kill_query(const std::string &pid, std::string &errmsg)
Terminate the query running on a connection.
Definition: health_monitor_server.cc:296
static std::string get_offline_mode_str()
OFFLINE_MODE message string.
Definition: health_monitor_server.cc:467
static bool is_member_online_with_group_majority()
Determine if the server is online and in a group majority.
Definition: health_monitor_server.cc:228
static int register_group_member_status_listeners()
Register the group member status listener.
Definition: health_monitor_server.cc:138
static bool get_super_read_only(bool &error)
Get the value of SUPER_READ_ONLY.
Definition: health_monitor_server.cc:501
int kill_connection(const std::string &pid, std::string &errmsg)
Terminate a server thread given a proceslist id.
Definition: health_monitor_server.cc:325
static bool clear_offline_mode()
Clear OFFLINE_MODE, set to OFF.
Definition: health_monitor_server.cc:492
Processlist_Ids get_fg_sessions_with_global_lock()
Get list of foreground sessions holding a global read lock.
Definition: health_monitor_server.cc:238
static bool is_group_replication_primary()
Determine if the server is a group replication primary.
Definition: health_monitor_server.cc:212
static bool is_group_replication_secondary()
Determine if the server is a group replication secondary.
Definition: health_monitor_server.cc:220
static bool clear_super_read_only()
Clear SUPER_READ_ONLY, set to OFF.
Definition: health_monitor_server.cc:550
static GR_Status get_group_replication_status()
Get the current group replication status.
Definition: health_monitor_server.cc:165
static GR_Status m_gr_status
Group replication status.
Definition: health_monitor_server.h:123
static bool set_offline_mode()
Set OFFLINE_MODE = ON.
Definition: health_monitor_server.cc:483
static bool get_status_variable(const std::string &name, std::string &value)
Get server global status variable.
Definition: health_monitor_server.cc:428
static std::string get_super_read_only_str()
SUPER_READ_ONLY message string.
Definition: health_monitor_server.cc:525
static Processlist_Ids processlist_id_list(Result_Set &results)
Build a list of processlist ids from a result set.
Definition: health_monitor_server.cc:274
static bool get_offline_mode(bool &error)
Get the value of OFFLINE_MODE.
Definition: health_monitor_server.cc:454
static bool set_system_variable(const std::string &name, const std::string &value)
Set server system variable.
Definition: health_monitor_server.cc:352
static void set_group_replication_status(const char *view_id)
Set the internal group replication status.
Definition: health_monitor_server.cc:202
static bool get_log_diagnostic_enable(bool &error)
Get the value of LOG_DIAGNOSTIC_ENABLE.
Definition: health_monitor_server.cc:587
static GR_Status gr_status()
Get the group replication status.
Definition: health_monitor_server.cc:155
static bool clear_read_only()
Clear READ_ONLY, set to OFF.
Definition: health_monitor_server.cc:573
static std::string & to_lower(std::string &str)
Convert string to lower case.
Definition: health_monitor_server.cc:601
static std::shared_mutex m_gr_mutex
Initialize static members.
Definition: health_monitor_server.h:124
void error(const char *format,...)
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1077
Definition: component_command.h:29
std::vector< Row > Result_Set
Definition: health_monitor_query.h:33
std::vector< std::string > Processlist_Ids
Definition: health_monitor_server.h:36
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
#define DEFINE_BOOL_METHOD(name, args)
A short macro to define method that returns bool, which is the most common case.
Definition: service_implementation.h:88
case opt name
Definition: sslopt-case.h:29
Group Replication status.
Definition: health_monitor_server.h:41
bool online_with_group_majority
Definition: health_monitor_server.h:47
bool valid
Definition: health_monitor_server.h:43
std::string view_id
Definition: health_monitor_server.h:49
bool single_primary_mode
Definition: health_monitor_server.h:44
std::string role() const
Group Replication role name.
Definition: health_monitor_server.cc:124
bool single_primary_mode_secondary
Definition: health_monitor_server.h:46
bool single_primary_mode_primary
Definition: health_monitor_server.h:45
GR_Status()=default