MySQL 26.7.0
Source Code Documentation
rpl_applier_reader.h
Go to the documentation of this file.
1/* Copyright (c) 2018, 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_APPLIER_READER_INCLUDED
25#define RPL_APPLIER_READER_INCLUDED
26
27#include "mysqld.h"
28#include "sql/binlog.h"
29#include "sql/binlog_reader.h"
30
31class Relay_log_info;
32
33/**
34 This class provides the feature to read events from relay log files.
35
36 - Open() function just opens the first relay log file
37 (rli->get_group_relay_log_name()) which need to be applied by slave.
38
39 - read_next_event() just returns the events one by one. It will close()
40 current relay log and open next relay log file automatically when reaching
41 the end of current relay log. Internally each relay log file is opened and
42 read by using a Relaylog_file_reader.
43
44 - It will purge the applied relay logs accordingly when moving to next relay
45 log.
46
47 - When reaching the end of active relay log file, it will wait for new events
48 coming and make MTS checkpoints accordingly while waiting for events.
49*/
51 public:
52 /**
53 @param[in] rli relay log info is used in the function. So rli should be
54 initialized before initializing Rpl_applier_reader object.
55 */
60
61 /**
62 Open the first relay log file which will be read by applier and
63 seek to correct position. The file name and position are read from
64 rli->get_group_master_log_name() and rli->get_group_relay_log_pos().
65
66 @param[out] errmsg Set the error message to it if any error happens.
67
68 @retval false Success
69 @retval true Error
70 */
71 bool open(const char **errmsg);
72 void close();
73
74 /**
75 Read next event from relay log.
76 - It will wait until the receiver writes some events to relay log in case
77 where there are no more events left to read from the active relay log.
78 - The caller must hold m_rli->data_lock. The lock will be released
79 temporarily while waiting and required after waking up.
80 - The wait is protected by relay_log.lock_binlog_end_pos().
81
82 @retval Log_event* A valid Log_event object.
83 @retval nullptr Error happened or sql thread was killed.
84 */
86
87 private:
90 /** Stores the error message which is used internally */
91 const char *m_errmsg = nullptr;
92
93 /// Here we keep the list of logs to be purged (in case later logs are
94 /// applied before), protected with m_rli->data_lock
95 std::unordered_set<std::string> m_logs_to_purge;
96
98 /**
99 Stores active log's end position. Thus avoids to call
100 relay_log.get_binlog_log_pos() for each event.
101 */
103 /*
104 It stores offset of relay log index to speed up finding next relay log
105 files.
106 */
109
110 class Stage_controller;
111 /**
112 When reaching the end of current relay log file, close it and open next
113 relay log. purge old relay logs if necessary.
114
115 @param force_purge This function rarely purges the current log before
116 moving to the next. Sometimes, we need to enforce this purge, e.g. when
117 receiver is waiting for available relay log space. The 'force_purge'
118 will enable an aggressive relay log purge.
119 Beware that 'move_to_next_log' with force_purge enabled, will ignore that
120 current group position is lower than required and will reset it. Therefore,
121 make sure that workers finished executing ALL scheduled jobs.
122
123 @retval false Success
124 @retval true Error
125 */
126 bool move_to_next_log(bool force_purge);
127 /**
128 It reads the coordinates up to which the receiver thread has written and
129 check whether there is any event to be read.
130
131 @retval false The applier has read all events.
132 @retval true The applier is behind the receiver.
133 */
135 /**
136 In the case receiver thread says master skipped some events, it will
137 generate a Rotate_log_event for applier to advance executed master log
138 position.
139 */
141 /**
142 Purge relay log files prior to m_rli->group_relay_log_name.
143 It is used to be called MYSQL_BIN_LOG::purge_first_log
144
145 @retval false Success
146 @retval true Error
147 */
148 bool purge_applied_logs();
149
150 /**
151 Waits for new events coming. Returning successfully just means it gets a
152 signal. That doesn't guarantee that any new event came.
153
154 @retval false Success
155 @retval true Error. The thread is killed or flush failed while
156 executing mta_checkpoint_routine.
157
158 @note the The caller must hold m_rli->data_lock and
159 relaylog.lock_binlog_end_pos().
160 */
161 bool wait_for_new_event();
162
163 /**
164 It checks if the relaylog file reader should be reopened and then reopens
165 the reader if receiver thread truncated some data from active relay log.
166 The caller must hold m_rli->data_lock
167
168 @retval false Success
169 @retval true Error
170 */
172
173 /* reset seconds_behind_source when starting to wait for events coming */
175#ifndef NDEBUG
177#endif
178};
179
180#endif // RPL_APPLIER_READER_INCLUDED
This is the abstract base class for binary log events.
Definition: log_event.h:539
Definition: rpl_rli.h:208
This will be deprecated when we move to using sequence ids.
Definition: log_event.h:2060
It manages a stage and the related mutex and makes the process of locking and entering stage/unlock a...
Definition: rpl_applier_reader.cc:46
This class provides the feature to read events from relay log files.
Definition: rpl_applier_reader.h:50
bool m_relay_log_purge
Definition: rpl_applier_reader.h:108
Relay_log_info * m_rli
Definition: rpl_applier_reader.h:89
bool wait_for_new_event()
Waits for new events coming.
Definition: rpl_applier_reader.cc:334
Rotate_log_event * generate_rotate_event()
In the case receiver thread says master skipped some events, it will generate a Rotate_log_event for ...
Definition: rpl_applier_reader.cc:317
const char * m_errmsg
Stores the error message which is used internally.
Definition: rpl_applier_reader.h:91
Log_info m_linfo
Definition: rpl_applier_reader.h:107
Relaylog_file_reader m_relaylog_file_reader
Definition: rpl_applier_reader.h:88
bool m_reading_active_log
Definition: rpl_applier_reader.h:97
bool read_active_log_end_pos()
It reads the coordinates up to which the receiver thread has written and check whether there is any e...
Definition: rpl_applier_reader.cc:306
bool reopen_log_reader_if_needed()
It checks if the relaylog file reader should be reopened and then reopens the reader if receiver thre...
Definition: rpl_applier_reader.cc:369
Rpl_applier_reader(const Rpl_applier_reader &)=delete
Rpl_applier_reader(Relay_log_info *rli)
Definition: rpl_applier_reader.cc:87
void close()
Definition: rpl_applier_reader.cc:148
void debug_print_next_event_positions()
Definition: rpl_applier_reader.cc:534
bool open(const char **errmsg)
Open the first relay log file which will be read by applier and seek to correct position.
Definition: rpl_applier_reader.cc:96
Rpl_applier_reader & operator=(const Rpl_applier_reader &)=delete
my_off_t m_log_end_pos
Stores active log's end position.
Definition: rpl_applier_reader.h:102
void reset_seconds_behind_master()
Definition: rpl_applier_reader.cc:572
Log_event * read_next_event()
Read next event from relay log.
Definition: rpl_applier_reader.cc:155
std::unordered_set< std::string > m_logs_to_purge
Here we keep the list of logs to be purged (in case later logs are applied before),...
Definition: rpl_applier_reader.h:95
bool purge_applied_logs()
Purge relay log files prior to m_rli->group_relay_log_name.
Definition: rpl_applier_reader.cc:475
~Rpl_applier_reader()
Definition: rpl_applier_reader.cc:94
bool move_to_next_log(bool force_purge)
When reaching the end of current relay log file, close it and open next relay log.
Definition: rpl_applier_reader.cc:410
ulonglong my_off_t
Definition: my_inttypes.h:72
bool relay_log_purge
Definition: mysqld.cc:1300
Definition: binlog_index.h:86