Repository navigation
Expand file tree
/
Copy pathmariadb_replication.cpp
More file actions
295 lines (252 loc) · 8.26 KB
/
Copy pathmariadb_replication.cpp
File metadata and controls
295 lines (252 loc) · 8.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include "mariadb_replication.h"
#include <cerrno>
#include <cctype>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <limits>
namespace {
bool is_hex(char c) {
return std::isdigit(static_cast<unsigned char>(c)) ||
(c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
}
bool is_separator_whitespace(char c) {
return c == ' ' || c == '\t' || c == '\r' || c == '\n';
}
std::string trim_separator_whitespace(const std::string& input) {
size_t start = 0;
while (start < input.size() && is_separator_whitespace(input[start]))
++start;
size_t end = input.size();
while (end > start && is_separator_whitespace(input[end - 1]))
--end;
return input.substr(start, end - start);
}
bool normalize_uuid(const std::string& input, std::string* output) {
if (!output || (input.size() != 32 && input.size() != 36))
return false;
std::string normalized;
normalized.reserve(32);
for (size_t i = 0; i < input.size(); ++i) {
const char c = input[i];
const bool dash_position = i == 8 || i == 13 || i == 18 || i == 23;
if (input.size() == 36 && dash_position) {
if (c != '-')
return false;
continue;
}
if (!is_hex(c))
return false;
normalized += static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
}
if (normalized.size() != 32)
return false;
*output = normalized;
return true;
}
bool parse_positive_trxid(const std::string& input, trxid_t* out) {
if (!out || input.empty())
return false;
uint64_t value = 0;
for (char c : input) {
if (!std::isdigit(static_cast<unsigned char>(c)))
return false;
const uint64_t digit = static_cast<uint64_t>(c - '0');
if (value > (static_cast<uint64_t>(std::numeric_limits<trxid_t>::max()) - digit) / 10)
return false;
value = value * 10 + digit;
}
if (value == 0)
return false;
*out = static_cast<trxid_t>(value);
return true;
}
bool is_unsigned_decimal_no_leading_zeros(const std::string& input) {
if (input.empty())
return false;
if (input.size() > 1 && input[0] == '0')
return false;
for (char c : input) {
if (!std::isdigit(static_cast<unsigned char>(c)))
return false;
}
return true;
}
// MariaDB carries domain_id and server_id as uint32, both in GTID_EVENT and in
// the snapshot text, so anything wider cannot be represented without truncation.
bool parse_uint32_field(const std::string& input, uint32_t* out) {
if (!out)
return false;
uint64_t value = 0;
for (char c : input) {
if (!std::isdigit(static_cast<unsigned char>(c)))
return false;
const uint64_t digit = static_cast<uint64_t>(c - '0');
if (value > (static_cast<uint64_t>(UINT32_MAX) - digit) / 10)
return false;
value = value * 10 + digit;
}
*out = static_cast<uint32_t>(value);
return true;
}
bool parse_mariadb_gtid_token(const std::string& token, GTID_Set* set) {
const size_t first = token.find('-');
if (first == std::string::npos)
return false;
const size_t second = token.find('-', first + 1);
if (second == std::string::npos || token.find('-', second + 1) != std::string::npos)
return false;
if (first == 0 || second == first + 1 || second == token.size() - 1)
return false;
const std::string domain = token.substr(0, first);
const std::string server = token.substr(first + 1, second - first - 1);
const std::string sequence = token.substr(second + 1);
if (!is_unsigned_decimal_no_leading_zeros(domain) ||
!is_unsigned_decimal_no_leading_zeros(server) ||
!is_unsigned_decimal_no_leading_zeros(sequence))
return false;
trxid_t seq = 0;
if (!parse_positive_trxid(sequence, &seq))
return false;
uint32_t domain_id = 0;
uint32_t server_id = 0;
if (!parse_uint32_field(domain, &domain_id) ||
!parse_uint32_field(server, &server_id))
return false;
const std::string domain_key = std::to_string(domain_id);
set->add(domain_key, trxid_t(1), seq);
set->set_server_id(domain_key, server_id);
return true;
}
bool add_interval(const std::string& input, const std::string& uuid, GTID_Set* set) {
const size_t dash = input.find('-');
if (dash == std::string::npos) {
trxid_t trxid = 0;
return parse_positive_trxid(input, &trxid) && set->add(uuid, trxid);
}
if (dash == 0 || dash == input.size() - 1 || input.find('-', dash + 1) != std::string::npos)
return false;
trxid_t start = 0;
trxid_t end = 0;
if (!parse_positive_trxid(input.substr(0, dash), &start) ||
!parse_positive_trxid(input.substr(dash + 1), &end) || start > end)
return false;
set->add(uuid, start, end);
return true;
}
} // namespace
std::string mysql_uuid_from_bytes(const unsigned char* source_id) {
if (!source_id)
return "";
static const char hex[] = "0123456789abcdef";
std::string uuid;
uuid.reserve(32);
for (size_t i = 0; i < 16; ++i) {
uuid += hex[(source_id[i] >> 4) & 0x0f];
uuid += hex[source_id[i] & 0x0f];
}
return uuid;
}
bool parse_mysql_gtid_executed(const std::string& encoded, GTID_Set* out) {
if (!out)
return false;
GTID_Set parsed;
if (encoded.empty()) {
*out = parsed;
return true;
}
size_t set_start = 0;
while (set_start < encoded.size()) {
const size_t set_end = encoded.find(',', set_start);
const std::string entry = trim_separator_whitespace(encoded.substr(
set_start, set_end == std::string::npos ? std::string::npos : set_end - set_start));
const size_t uuid_end = entry.find(':');
if (uuid_end == std::string::npos || uuid_end == 0 || uuid_end == entry.size() - 1)
return false;
std::string uuid;
if (!normalize_uuid(entry.substr(0, uuid_end), &uuid))
return false;
size_t interval_start = uuid_end + 1;
while (interval_start < entry.size()) {
const size_t interval_end = entry.find(':', interval_start);
const std::string interval = entry.substr(
interval_start, interval_end == std::string::npos
? std::string::npos
: interval_end - interval_start);
if (!add_interval(interval, uuid, &parsed))
return false;
if (interval_end == std::string::npos)
break;
interval_start = interval_end + 1;
}
if (set_end == std::string::npos)
break;
set_start = set_end + 1;
if (set_start == encoded.size())
return false;
}
*out = parsed;
return true;
}
bool parse_mariadb_gtid_executed(const std::string& encoded, GTID_Set* out) {
if (!out)
return false;
GTID_Set parsed;
// MariaDB reports an empty @@gtid_binlog_pos until its first GTID. That is a
// valid empty GTID_Set, not a malformed value: the reader's main loop waits
// for a non-empty position before opening the stream. Only malformed
// non-empty text fails.
if (encoded.empty()) {
*out = parsed;
return true;
}
size_t set_start = 0;
while (set_start < encoded.size()) {
const size_t set_end = encoded.find(',', set_start);
const std::string entry = trim_separator_whitespace(encoded.substr(
set_start, set_end == std::string::npos ? std::string::npos : set_end - set_start));
if (!parse_mariadb_gtid_token(entry, &parsed))
return false;
if (set_end == std::string::npos)
break;
set_start = set_end + 1;
if (set_start == encoded.size())
return false;
}
*out = parsed;
return true;
}
bool parse_gtid_executed(const std::string& encoded, GTID_Set* out) {
if (encoded.find(':') != std::string::npos)
return parse_mysql_gtid_executed(encoded, out);
return parse_mariadb_gtid_executed(encoded, out);
}
bool snapshot_gtid_set(const char* executed_gtid_set_or_null,
const std::string& mariadb_binlog_pos,
GTID_Set* out) {
if (!out)
return false;
if (executed_gtid_set_or_null)
return parse_mysql_gtid_executed(executed_gtid_set_or_null, out);
return parse_mariadb_gtid_executed(mariadb_binlog_pos, out);
}
bool is_mariadb_server(const char* server_version) {
return server_version != nullptr
&& std::strstr(server_version, "MariaDB") != nullptr;
}
bool parse_mysql_snapshot_position(const char* filename, const char* encoded_position,
unsigned long* out) {
if (!filename || !*filename || !encoded_position || !*encoded_position || !out)
return false;
for (const char* cursor = encoded_position; *cursor; ++cursor) {
if (*cursor < '0' || *cursor > '9')
return false;
}
errno = 0;
char* end = nullptr;
const unsigned long position = std::strtoul(encoded_position, &end, 10);
if (errno == ERANGE || !end || *end != '\0' || position < 4)
return false;
*out = position;
return true;
}