forked from vozbu/libslave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinlog_pos.cpp
296 lines (266 loc) · 7.42 KB
/
binlog_pos.cpp
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
296
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iterator>
#include <memory>
#include <alloca.h>
#include <mysql/my_global.h>
#undef min
#undef max
#undef test
#include "binlog_pos.h"
#include "slave_log_event.h"
namespace
{
void hex2bin(uint8_t* dst, const char* src, size_t sz_src)
{
if (!dst || !src) return;
uint8_t cur = 0;
for (size_t i = 0; i < sz_src; ++i)
{
const char c = src[i];
if ('0' <= c && c <= '9') cur |= c - '0';
else if ('A' <= c && c <= 'F') cur |= c - 'A' + 10;
else if ('a' <= c && c <= 'f') cur |= c - 'a' + 10;
else throw std::runtime_error("hex2bin failed: bad symbol in hex data");
if (0 == i % 2)
{
cur <<= 4;
}
else
{
*dst++ = cur;
cur = 0;
}
}
}
template <class F>
void backup_invoke_restore(F& f, char* begin, char* end)
{
if (begin == end)
return;
const char backup = *end;
*end = '\0';
f(const_cast<const char*>(begin));
*end = backup;
}
// Parse list on const char* using delimiter and call function with each element
template <class F, class C>
void parse_list_f_custom(const std::string& aList, F f, C c, const char* delim = ",")
{
std::unique_ptr<char[]> sHeap;
char* s = nullptr;
const size_t sRequiredSize = aList.size() + 1;
if (sRequiredSize <= 65536)
{
s = (char*)::alloca(sRequiredSize);
}
else
{
sHeap.reset(new char[sRequiredSize]);
s = sHeap.get();
}
memcpy(s, aList.data(), aList.size());
s[aList.size()] = '\0';
for (char* p = s; '\0' != *p;)
{
p += strspn(p, delim);
if (!c(aList, f, p))
{
auto q = p + strcspn(p, delim);
backup_invoke_restore(f, p, q);
p = q;
}
}
}
template <typename F>
void parse_list_f(const std::string& aList, F f, const char* delim = ",")
{
parse_list_f_custom(aList, f, [] (const std::string&, F&, char*) { return false; }, delim);
}
// Parse list on long long using delimiter and call function with each element
template <typename F>
void parse_list_ll_f(const std::string& aList, F f, const char* delim = ",")
{
parse_list_f(aList, [&f] (const char* s) { f(atoll(s)); }, delim);
}
// Parse list using delimiter and put strings into container
template <class Cont>
void parse_list_cont(const std::string& aList, Cont& aCont, const char* delim = ",")
{
parse_list_f(aList,
[&aCont] (const char* s)
{
aCont.insert(aCont.end(), s);
}
, delim);
}
} // namespace anonymous
namespace slave
{
// parseGtid parse string with gtid
// example: ae00751a-cb5f-11e6-9d92-e03f490fd3db:1-12:15-17
// gtid_set: uuid_set [, uuid_set] ... | ''
// uuid_set: uuid:interval[:interval]...
// uuid: hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh
// h: [0-9|A-F]
// interval: n[-n] (n >= 1)
void Position::parseGtid(const std::string& input)
{
if (input.empty())
return;
gtid_executed.clear();
std::string s;
std::remove_copy_if(input.begin(), input.end(), std::back_inserter(s), [](char c){ return c == ' ' || c == '\n'; });
std::deque<std::string> cont;
parse_list_f(s, [this, &cont](const std::string& token)
{
cont.clear();
parse_list_cont(token, cont, ":");
bool uuid_parsed = false;
std::string sid;
for (const auto& x : cont)
{
if (!uuid_parsed)
{
std::remove_copy(x.begin(), x.end(), std::back_inserter(sid), '-');
uuid_parsed = true;
}
else
{
bool first = true;
gtid_interval_t interval;
parse_list_ll_f(x, [&first, &interval](int64_t y)
{
if (first)
{
interval.first = interval.second = y;
first = false;
}
else
{
interval.second = y;
}
}, "-");
gtid_executed[sid].push_back(interval);
}
}
});
}
void Position::addGtid(const gtid_t& gtid)
{
auto it = gtid_executed.find(gtid.first);
if (it == gtid_executed.end())
{
gtid_executed[gtid.first].emplace_back(gtid.second, gtid.second);
return;
}
bool flag = true;
for (auto itr = it->second.begin(); itr != it->second.end(); ++itr)
{
if (gtid.second >= itr->first && gtid.second <= itr->second)
return;
if (gtid.second + 1 == itr->first)
{
--itr->first;
flag = false;
break;
}
else if (gtid.second == itr->second + 1)
{
++itr->second;
flag = false;
break;
}
else if (gtid.second < itr->first)
{
it->second.emplace(itr, gtid.second, gtid.second);
return;
}
}
if (flag)
it->second.emplace_back(gtid.second, gtid.second);
for (auto itr = it->second.begin(); itr != it->second.end(); ++itr)
{
auto next_itr = std::next(itr);
if (next_itr != it->second.end() && itr->second + 1 == next_itr->first)
{
itr->second = next_itr->second;
it->second.erase(next_itr);
break;
}
}
}
size_t Position::encodedGtidSize() const
{
if (gtid_executed.empty())
return 0;
size_t result = 8;
for (const auto& x : gtid_executed)
result += x.second.size() * 16 + 8 + ENCODED_SID_LENGTH;
return result;
}
void Position::encodeGtid(unsigned char* buf)
{
if (gtid_executed.empty())
return;
int8store(buf, gtid_executed.size());
size_t offset = 8;
for (const auto& x : gtid_executed)
{
hex2bin(buf + offset, x.first.c_str(), ENCODED_SID_LENGTH * 2);
offset += ENCODED_SID_LENGTH;
int8store(buf + offset, x.second.size());
offset += 8;
for (const auto& interval : x.second)
{
int8store(buf + offset, interval.first);
offset += 8;
int8store(buf + offset, interval.second + 1);
offset += 8;
}
}
}
bool Position::reachedOtherPos(const Position& other) const
{
if (gtid_executed.empty())
return log_name > other.log_name ||
(log_name == other.log_name && log_pos >= other.log_pos);
return gtid_executed == other.gtid_executed;
}
std::string Position::str() const
{
std::string result = "'";
if (!log_name.empty() && log_pos)
result += log_name + ":" + std::to_string(log_pos) + ", ";
result += "GTIDs=";
if (gtid_executed.empty())
{
result += "-'";
return result;
}
bool first_a = true;
for (const auto& gtid : gtid_executed)
{
if (first_a)
first_a = false;
else
result += ",";
result += gtid.first + ":";
bool first_b = true;
for (const auto& interv : gtid.second)
{
if (first_b)
first_b = false;
else
result += ":";
result += std::to_string(interv.first);
if (interv.first != interv.second)
result += "-" + std::to_string(interv.second);
}
}
result += "'";
return result;
}
} // namespace slave