MySQL 26.7.0
Source Code Documentation
rpl_reporting.h
Go to the documentation of this file.
1/* Copyright (c) 2006, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
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, version 2.0, 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#ifndef RPL_REPORTING_H
25#define RPL_REPORTING_H
26
27#include <stdarg.h>
28#include <stdio.h>
29#include <sys/types.h>
30#include <time.h>
31#include <cstring>
32
33#include "my_compiler.h"
34#include "my_inttypes.h"
35#include "my_systime.h" //my_getsystime
37#include "mysql/my_loglevel.h"
39
40/**
41 Maximum size of an error message from a slave thread.
42 */
43#define MAX_SLAVE_ERRMSG 1024
44
45class THD;
47
48/**
49 Mix-in to handle the message logging and reporting for relay log
50 info and master log info structures.
51
52 By inheriting from this class, the class is imbued with
53 capabilities to do slave reporting.
54 */
56 public:
57 /** lock used to synchronize m_last_error on 'SHOW REPLICA STATUS' **/
59 /**
60 Constructor.
61
62 @param thread_name Printable name of the slave thread that is reporting.
63 */
64 Slave_reporting_capability(char const *thread_name);
65
66 /**
67 Writes a message and, if it's an error message, to Last_Error
68 (which will be displayed by SHOW REPLICA STATUS).
69
70 @param level The severity level
71 @param err_code The error code
72 @param msg The message (usually related to the error
73 code, but can contain more information), in
74 printf() format.
75 */
76 virtual void report(loglevel level, int err_code, const char *msg, ...) const
77 MY_ATTRIBUTE((format(printf, 4, 5)));
78
79 virtual void report(loglevel level, int err_code,
80 const Gtid_specification *gtid_next, const char *msg,
81 ...) const MY_ATTRIBUTE((format(printf, 5, 6)));
82
83 void va_report(loglevel level, int err_code, const char *prefix_msg,
84 const char *msg, va_list v_args) const
85 MY_ATTRIBUTE((format(printf, 5, 0)));
86
87 /**
88 Clear errors. They will not show up under <code>SHOW REPLICA
89 STATUS</code>.
90 */
91 void clear_error() {
95 }
96
97 /**
98 Check if the current error is of temporary nature or not.
99 */
100 int has_temporary_error(THD *thd, uint error_arg = 0,
101 bool *silent = nullptr) const;
102
103 /**
104 Error information structure.
105 */
106 class Error {
108
109 public:
110 Error() { clear(); }
111
112 void clear() {
113 number = 0;
114 message[0] = '\0';
115 timestamp[0] = '\0';
116 }
117
119 struct tm tm_tmp;
120 struct tm *start;
121 time_t tt_tmp;
122
123 skr = my_getsystime() / 10;
124 tt_tmp = skr / 1000000;
125 localtime_r(&tt_tmp, &tm_tmp);
126 start = &tm_tmp;
127
128 snprintf(timestamp, sizeof(timestamp), "%02d%02d%02d %02d:%02d:%02d",
129 start->tm_year % 100, start->tm_mon + 1, start->tm_mday,
130 start->tm_hour, start->tm_min, start->tm_sec);
131 timestamp[15] = '\0';
132 }
133
134 void copy_from(const Error &other) {
135 number = other.number;
136 memcpy(message, other.message, MAX_SLAVE_ERRMSG);
137 memcpy(timestamp, other.timestamp, 64);
138 skr = other.skr;
139 }
140
141 /** Error code */
143 /** Error message */
145 /** Error timestamp as string */
146 char timestamp[64];
147 /** Error timestamp in microseconds. Used in performance_schema */
149 };
150
151 Error const &last_error() const { return m_last_error; }
152 void copy_error_from(const Error &error, char message[MAX_SLAVE_ERRMSG]) {
154 memcpy(m_last_error.message, message, MAX_SLAVE_ERRMSG);
155 }
156 bool is_error() const { return last_error().number != 0; }
157
158 /*
159 For MSR, there is a need to introduce error messages per channel.
160 Instead of changing the error messages in share/messages_to_error_log.txt
161 to introduce the clause, FOR CHANNEL "%s", we construct a string like this.
162 There might be problem with a client applications which could print
163 error messages and see no %s.
164 @TODO: fix this.
165 */
166 virtual const char *get_for_channel_str(bool upper_case) const = 0;
167
168 virtual ~Slave_reporting_capability() = 0;
169
170 protected:
171 /// Allows subclasses to adjust the report level while holding err_lock,
172 /// immediately before Last_Error is updated.
173 /// Implementations must not acquire err_lock.
174 /// @param level Report level requested by the caller.
175 /// @param err_code Error code requested by the caller.
176 /// @return Effective report level.
178 int err_code) const;
179
180 virtual void do_report(loglevel level, int err_code, const char *msg,
181 va_list v_args) const
182 MY_ATTRIBUTE((format(printf, 4, 0)));
183
184 virtual void do_report(loglevel level, int err_code,
185 const Gtid_specification *gtid_next, const char *msg,
186 va_list v_args) const
187 MY_ATTRIBUTE((format(printf, 5, 0)));
188
189 /**
190 Last error produced by the I/O or SQL thread respectively.
191 */
193
194 private:
195 char const *const m_thread_name;
196
197 // not implemented
200};
201
202inline void Slave_reporting_capability::do_report(loglevel level, int err_code,
203 const char *msg,
204 va_list v_args) const {
205 va_report(level, err_code, nullptr, msg, v_args);
206}
207
208inline void Slave_reporting_capability::do_report(loglevel level, int err_code,
209 const Gtid_specification *,
210 const char *msg,
211 va_list v_args) const {
212 va_report(level, err_code, nullptr, msg, v_args);
213}
214
216 loglevel level, int) const {
217 return level;
218}
219
220#endif // RPL_REPORTING_H
Error information structure.
Definition: rpl_reporting.h:106
char message[MAX_SLAVE_ERRMSG]
Error message.
Definition: rpl_reporting.h:144
char timestamp[64]
Error timestamp as string.
Definition: rpl_reporting.h:146
Error()
Definition: rpl_reporting.h:110
void clear()
Definition: rpl_reporting.h:112
void update_timestamp()
Definition: rpl_reporting.h:118
ulonglong skr
Error timestamp in microseconds.
Definition: rpl_reporting.h:148
uint32 number
Error code.
Definition: rpl_reporting.h:142
void copy_from(const Error &other)
Definition: rpl_reporting.h:134
Mix-in to handle the message logging and reporting for relay log info and master log info structures.
Definition: rpl_reporting.h:55
Error const & last_error() const
Definition: rpl_reporting.h:151
virtual void do_report(loglevel level, int err_code, const char *msg, va_list v_args) const
Definition: rpl_reporting.h:202
Slave_reporting_capability(char const *thread_name)
Constructor.
Definition: rpl_reporting.cc:42
Slave_reporting_capability(const Slave_reporting_capability &rhs)
virtual loglevel get_effective_report_level(loglevel level, int err_code) const
Allows subclasses to adjust the report level while holding err_lock, immediately before Last_Error is...
Definition: rpl_reporting.h:215
Slave_reporting_capability & operator=(const Slave_reporting_capability &rhs)
virtual const char * get_for_channel_str(bool upper_case) const =0
mysql_mutex_t err_lock
lock used to synchronize m_last_error on 'SHOW REPLICA STATUS'
Definition: rpl_reporting.h:58
void va_report(loglevel level, int err_code, const char *prefix_msg, const char *msg, va_list v_args) const
Definition: rpl_reporting.cc:134
void copy_error_from(const Error &error, char message[MAX_SLAVE_ERRMSG])
Definition: rpl_reporting.h:152
virtual void report(loglevel level, int err_code, const char *msg,...) const
Writes a message and, if it's an error message, to Last_Error (which will be displayed by SHOW REPLIC...
Definition: rpl_reporting.cc:117
int has_temporary_error(THD *thd, uint error_arg=0, bool *silent=nullptr) const
Check if the current error is of temporary nature or not.
Definition: rpl_reporting.cc:67
void clear_error()
Clear errors.
Definition: rpl_reporting.h:91
bool is_error() const
Definition: rpl_reporting.h:156
virtual ~Slave_reporting_capability()=0
Definition: rpl_reporting.cc:206
Error m_last_error
Last error produced by the I/O or SQL thread respectively.
Definition: rpl_reporting.h:192
char const *const m_thread_name
Definition: rpl_reporting.h:195
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
#define mysql_mutex_lock(M)
Definition: mysql_mutex.h:50
#define mysql_mutex_unlock(M)
Definition: mysql_mutex.h:57
static void start(mysql_harness::PluginFuncEnv *env)
Definition: http_auth_backend_plugin.cc:180
Header for compiler-dependent features.
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:56
uint32_t uint32
Definition: my_inttypes.h:67
Definition of the global "loglevel" enumeration.
loglevel
Definition: my_loglevel.h:41
Defines for getting and processing the current system type programmatically.
unsigned long long int my_getsystime()
Get high-resolution time.
Definition: my_systime.h:104
ABI for instrumented mutexes.
static bool silent
Definition: mysqlimport.cc:70
std::string format(const routing_guidelines::Session_info &session_info, bool extended_session_info)
Definition: dest_metadata_cache.cc:170
Instrumentation helpers for mutexes.
#define MAX_SLAVE_ERRMSG
Maximum size of an error message from a slave thread.
Definition: rpl_reporting.h:43
This struct represents a specification of a GTID for a statement to be executed: either "AUTOMATIC",...
Definition: rpl_gtid.h:4034
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:50