MySQL 9.7.0
Source Code Documentation
rpl_injector.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 INJECTOR_H
25#define INJECTOR_H
26
27#include <stddef.h>
28#include <string_view>
29
30#include "my_dbug.h"
31#include "my_inttypes.h"
32#include "my_io.h" // FN_REFLEN
33#include "sql/table.h" // TABLE
34
35class THD;
36struct MY_BITMAP;
37
38/*
39 Injector to inject rows into the MySQL server.
40
41 The injector class is used to notify the MySQL server of new rows that have
42 appeared outside of MySQL control.
43
44 The original purpose of this is to allow clusters---which handle replication
45 inside the cluster through other means---to insert new rows into binary log.
46 Note, however, that the injector should be used whenever rows are altered in
47 any manner that is outside of MySQL server visibility and which therefore
48 are not seen by the MySQL server.
49 */
50class injector {
51 public:
52 /*
53 Get an instance of the injector.
54
55 DESCRIPTION
56 The injector is a Singleton, so this static function return the
57 available instance of the injector.
58
59 RETURN VALUE
60 A pointer to the available injector object.
61 */
62 static injector *instance();
63
64 /*
65 Delete the singleton instance (if allocated). Used during server shutdown.
66 */
67 static void free_instance();
68
69 /*
70 Return current binlog filename into provided buffer.
71
72 Writes an empty string if no binlog file is active.
73
74 Only buffers of size FN_REFLEN are supported (compile-time enforced).
75 */
77
78 /*
79 A transaction where rows can be added.
80
81 DESCRIPTION
82 The transaction class satisfy the **CopyConstructible** and
83 **Assignable** requirements. Note that the transaction is *not*
84 default constructible.
85 */
87 public:
88 /* Convenience definitions */
91
92 /*
93 Table reference.
94
95 RESPONSIBILITY
96
97 The class contains constructors to handle several forms of
98 references to tables. The constructors can implicitly be used to
99 construct references from, e.g., strings containing table names.
100
101 EXAMPLE
102
103 The class is intended to be used *by value*. Please, do not try to
104 construct objects of this type using 'new'; instead construct an
105 object, possibly a temporary object. For example:
106
107 injector::transaction::table tbl(share->table, true);
108 MY_BITMAP cols;
109 bitmap_init(&cols, NULL, (i + 7) / 8, false);
110 inj->write_row(::server_id, tbl, &cols, row_data);
111
112 or
113
114 MY_BITMAP cols;
115 bitmap_init(&cols, NULL, (i + 7) / 8, false);
116 inj->write_row(::server_id,
117 injector::transaction::table(share->table, true),
118 &cols, row_data);
119
120 This will work, be more efficient, and have greater chance of
121 inlining, not run the risk of losing pointers.
122
123 COLLABORATION
124
125 injector::transaction
126 Provide a flexible interface to the representation of tables.
127
128 */
129 class table {
130 public:
131 class save_sets {
132 public:
133 save_sets(table const &tbl, MY_BITMAP const *new_rs,
134 MY_BITMAP const *new_ws)
135 : m_table(tbl.get_table()),
136 save_read_set(m_table->read_set),
137 save_write_set(m_table->write_set) {
139 const_cast<MY_BITMAP *>(new_rs), const_cast<MY_BITMAP *>(new_ws));
140 }
141
144 }
145
146 private:
150 };
151
153 : m_table(table),
156
157 TABLE *get_table() const { return m_table; }
158 bool is_transactional() const { return m_is_transactional; }
159 bool skip_hash() const { return m_skip_hash; }
160
161 private:
164 const bool m_skip_hash;
165 };
166
167 /*
168 Binlog position as a structure.
169 */
171 friend class transaction;
172
173 public:
174 char const *file_name() const { return m_file_name; }
175 my_off_t file_pos() const { return m_file_pos; }
176
177 private:
178 char const *m_file_name;
180 };
181
182 /*
183 Create a new transaction.
184
185 The parameter "calc_writeset_hash" controls whether writeset hashes for
186 transaction dependencies will be calculated for rows added to the
187 transaction, without this the list of hashes will be empty and thus no
188 dependencies are detected. The ability to control this is important where
189 MTA is not used, calculating writeset hashes is wasted work, and in NDB
190 all binlog content is processed by a single thread and CPU consumption
191 might be a bottleneck.
192 */
193 transaction(THD *thd, bool calc_writeset_hash);
194 transaction(const transaction &) = delete;
195 transaction(const transaction &&) = delete;
197 transaction &operator=(const transaction &&) = delete;
198
200
201 /*
202
203 DESCRIPTION
204
205 Register table for use within the transaction. All tables
206 that are going to be used need to be registered before being
207 used below. The member function will fail with an error if
208 use_table() is called after any *_row() function has been
209 called for the transaction.
210
211 RETURN VALUE
212
213 0 All OK
214 >0 Failure
215
216 */
217 int use_table(server_id_type sid, table tbl);
218
219 /*
220 Add a 'write row' entry to the transaction.
221 */
222 int write_row(server_id_type sid, table tbl, MY_BITMAP const *cols,
223 record_type record, const unsigned char *extra_row_info);
224
225 /*
226 Add a 'delete row' entry to the transaction.
227 */
228 int delete_row(server_id_type sid, table tbl, MY_BITMAP const *cols,
229 record_type record, const unsigned char *extra_row_info);
230 /*
231 Add an 'update row' entry to the transaction.
232 */
233 int update_row(server_id_type sid, table tbl, MY_BITMAP const *before_cols,
234 MY_BITMAP const *after_cols, record_type before,
235 record_type after, const unsigned char *extra_row_info);
236
237 /*
238 Commit a transaction.
239
240 This member function will clean up after a sequence of *_row calls by,
241 for example, releasing resource and unlocking files.
242 */
243 int commit();
244
245 /*
246 Rollback a transaction.
247
248 This member function will clean up after a sequence of *_row calls by,
249 for example, releasing resource and unlocking files.
250 */
251 int rollback();
252
253 /*
254 Get the position for the start of the transaction.
255
256 This is the current 'tail of Binlog' at the time the transaction
257 was started. The first event recorded by the transaction may
258 be at this, or some subsequent position. The first event recorded
259 by the transaction will not be before this position.
260 */
261 binlog_pos start_pos() const;
262
263 /*
264 Get the next position after the end of the transaction
265
266 This call is only valid after a transaction has been committed.
267 It returns the next Binlog position after the committed transaction.
268 It is guaranteed that no other events will be recorded between the
269 COMMIT event of the Binlog transaction, and this position.
270 Note that this position may be in a different log file to the COMMIT
271 event.
272
273 If the commit had an error, or the transaction was empty and nothing
274 was binlogged then the next_pos will have a NULL file_name(), and
275 0 file_pos().
276
277 */
278 binlog_pos next_pos() const;
279
280 private:
282 START_STATE, /* Start state */
283 TABLE_STATE, /* At least one table has been registered */
284 ROW_STATE, /* At least one row has been registered */
285 STATE_COUNT /* State count and sink state */
287
288 /*
289 Check and update the state.
290
291 PARAMETER(S)
292
293 target_state
294 The state we are moving to: TABLE_STATE if we are
295 writing a table and ROW_STATE if we are writing a row.
296
297 DESCRIPTION
298
299 The internal state will be updated to the target state if
300 and only if it is a legal move. The only legal moves are:
301
302 START_STATE -> START_STATE
303 START_STATE -> TABLE_STATE
304 TABLE_STATE -> TABLE_STATE
305 TABLE_STATE -> ROW_STATE
306
307 That is:
308 - It is not possible to write any row before having written at
309 least one table
310 - It is not possible to write a table after at least one row
311 has been written
312
313 RETURN VALUE
314
315 0 All OK
316 -1 Incorrect call sequence
317 */
318 int check_state(enum_state const target_state) {
319#ifndef NDEBUG
320 static char const *state_name[] = {"START_STATE", "TABLE_STATE",
321 "ROW_STATE", "STATE_COUNT"};
322
323 assert(0 <= target_state && target_state <= STATE_COUNT);
324 DBUG_PRINT("info", ("In state %s", state_name[m_state]));
325#endif
326
327 if (m_state <= target_state && target_state <= m_state + 1 &&
329 m_state = target_state;
330 else
332 return m_state == STATE_COUNT ? 1 : 0;
333 }
334
339 THD *const m_thd;
340 const bool m_calc_writeset_hash{false};
341 };
342
343 int record_incident(THD *, std::string_view message);
344
345 private:
346 explicit injector();
347 ~injector() = default; /* Nothing needs to be done */
348 injector(injector const &); /* You're not allowed to copy injector
349 instances.
350 */
351};
352
353#endif /* INJECTOR_H */
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Definition: rpl_injector.h:170
char const * m_file_name
Definition: rpl_injector.h:178
char const * file_name() const
Definition: rpl_injector.h:174
my_off_t file_pos() const
Definition: rpl_injector.h:175
my_off_t m_file_pos
Definition: rpl_injector.h:179
Definition: rpl_injector.h:131
MY_BITMAP * save_write_set
Definition: rpl_injector.h:149
TABLE *const m_table
Definition: rpl_injector.h:147
~save_sets()
Definition: rpl_injector.h:142
MY_BITMAP * save_read_set
Definition: rpl_injector.h:148
save_sets(table const &tbl, MY_BITMAP const *new_rs, MY_BITMAP const *new_ws)
Definition: rpl_injector.h:133
Definition: rpl_injector.h:129
const bool m_is_transactional
Definition: rpl_injector.h:163
const bool m_skip_hash
Definition: rpl_injector.h:164
TABLE * get_table() const
Definition: rpl_injector.h:157
TABLE *const m_table
Definition: rpl_injector.h:162
table(TABLE *table, bool is_transactional, bool skip_hash=false)
Definition: rpl_injector.h:152
bool skip_hash() const
Definition: rpl_injector.h:159
bool is_transactional() const
Definition: rpl_injector.h:158
Definition: rpl_injector.h:86
transaction(const transaction &)=delete
uchar * record_type
Definition: rpl_injector.h:89
int write_row(server_id_type sid, table tbl, MY_BITMAP const *cols, record_type record, const unsigned char *extra_row_info)
Definition: rpl_injector.cc:151
enum_state
Definition: rpl_injector.h:281
@ ROW_STATE
Definition: rpl_injector.h:284
@ START_STATE
Definition: rpl_injector.h:282
@ STATE_COUNT
Definition: rpl_injector.h:285
@ TABLE_STATE
Definition: rpl_injector.h:283
binlog_pos m_start_pos
Definition: rpl_injector.h:337
transaction & operator=(const transaction &&)=delete
binlog_pos next_pos() const
Definition: rpl_injector.cc:235
~transaction()
Definition: rpl_injector.h:199
binlog_pos m_next_pos
Definition: rpl_injector.h:338
THD *const m_thd
Definition: rpl_injector.h:339
char m_end_name_buf[FN_REFLEN]
Definition: rpl_injector.h:336
transaction & operator=(const transaction &)=delete
int delete_row(server_id_type sid, table tbl, MY_BITMAP const *cols, record_type record, const unsigned char *extra_row_info)
Definition: rpl_injector.cc:176
int commit()
Definition: rpl_injector.cc:72
int use_table(server_id_type sid, table tbl)
Definition: rpl_injector.cc:139
transaction(const transaction &&)=delete
uint32 server_id_type
Definition: rpl_injector.h:90
int check_state(enum_state const target_state)
Definition: rpl_injector.h:318
transaction(THD *thd, bool calc_writeset_hash)
Definition: rpl_injector.cc:37
binlog_pos start_pos() const
Definition: rpl_injector.cc:231
int update_row(server_id_type sid, table tbl, MY_BITMAP const *before_cols, MY_BITMAP const *after_cols, record_type before, record_type after, const unsigned char *extra_row_info)
Definition: rpl_injector.cc:201
char m_start_name_buf[FN_REFLEN]
Definition: rpl_injector.h:335
enum injector::transaction::enum_state m_state
int rollback()
Definition: rpl_injector.cc:115
const bool m_calc_writeset_hash
Definition: rpl_injector.h:340
Definition: rpl_injector.h:50
static injector * instance()
Definition: rpl_injector.cc:247
int record_incident(THD *, std::string_view message)
Definition: rpl_injector.cc:262
static void free_instance()
Definition: rpl_injector.cc:253
injector(injector const &)
static void get_current_binlog_filename(char(&filename)[FN_REFLEN])
Definition: rpl_injector.cc:266
~injector()=default
#define DBUG_PRINT(keyword, arglist)
Definition: my_dbug.h:181
Some integer typedefs for easier portability.
ulonglong my_off_t
Definition: my_inttypes.h:72
unsigned char uchar
Definition: my_inttypes.h:52
uint32_t uint32
Definition: my_inttypes.h:67
Common #defines and includes for file and socket I/O.
#define FN_REFLEN
Definition: my_io.h:87
static int record
Definition: mysqltest.cc:195
static PFS_engine_table_share_proxy table
Definition: pfs.cc:61
const char * filename
Definition: pfs_example_component_population.cc:67
Definition: my_bitmap.h:43
Definition: table.h:1456
void column_bitmaps_set_no_signal(MY_BITMAP *read_set_arg, MY_BITMAP *write_set_arg)
Definition: table.h:2042