-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.cpp
390 lines (323 loc) · 9.55 KB
/
main.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include <time.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <thread>
#include <signal.h>
#include <yaml-cpp/yaml.h>
#include "serializable.h"
#include "dbreader.h"
#include "tpwriter.h"
#include "serializable.h"
#include "queue.h"
#include "logger.h"
// =========
using std::placeholders::_1;
namespace replicator {
static const char *default_pid_filename = "/var/run/replicatord.pid";
static const char *default_log_filename = "/var/log/replicatord.log";
static const char *default_config_filename = "/usr/local/etc/replicatord.cfg";
static volatile bool is_term = false;
static volatile bool reset = false;
static std::string binlog_name;
static unsigned long binlog_pos;
static TPWriter *tpwriter = NULL;
static DBReader *dbreader = NULL;
static void sigint_handler(int sig);
static Queue<SerializableBinlogEventPtr> queue(50);
// ===============
static void tpwriter_worker()
{
while (!is_term)
{
while (!tpwriter->Connect());
// send initial binlog position to the db thread
try {
tpwriter->ReadBinlogPos(binlog_name, binlog_pos);
reset = true;
const std::chrono::milliseconds timeout(1000);
while (!is_term) {
for (unsigned cnt = queue.wait(timeout); cnt > 0; --cnt) {
tpwriter->BinlogEventCallback(queue.pop());
}
tpwriter->Sync();
tpwriter->RecvAll();
}
}
catch (std::range_error& ex) {
is_term = true;
std::cout << ex.what() << std::endl;
// loop exit
}
catch (std::exception& ex) {
std::cout << ex.what() << std::endl;
tpwriter->Disconnect();
// reconnect
}
}
tpwriter->Disconnect();
}
// ====================
static bool dbread_callback(SerializableBinlogEventPtr&& ev)
{
if (is_term || reset) {
return true;
}
queue.push(std::forward<SerializableBinlogEventPtr>(ev));
return false;
}
static void mysql_worker()
{
while (!is_term) {
// read initial binlog pos from Tarantool
while (!reset) ::sleep(1);
reset = false;
try {
if (!is_term && !reset && binlog_name == "" && binlog_pos == 0) {
std::cout << "Tarantool reported null binlog position. Dumping tables..." << std::endl;
dbreader->DumpTables(binlog_name, binlog_pos, dbread_callback);
}
if (!is_term && !reset) {
std::cout << "Reading binlogs (" << binlog_name << ", " << binlog_pos << ")..." << std::endl;
dbreader->ReadBinlog(binlog_name, binlog_pos, dbread_callback);
}
}
catch (std::exception& ex) {
std::cerr << "Error in reading binlogs: " << ex.what() << std::endl;
sigint_handler(0);
}
}
}
static void init(YAML::Node& cfg)
{
try {
// read Mysql settings
{
const YAML::Node& mysql = cfg["mysql"];
nanomysql::mysql_conn_opts opts;
opts.mysql_host = mysql["host"].as<std::string>();
opts.mysql_port = mysql["port"].as<unsigned>();
opts.mysql_user = mysql["user"].as<std::string>();
opts.mysql_pass = mysql["password"].as<std::string>();
dbreader = new DBReader(opts, mysql["connect_retry"].as<unsigned>());
}
// read Tarantool config
{
const YAML::Node& tarantool = cfg["tarantool"];
tpwriter = new TPWriter(
tarantool["host"].as<std::string>(),
tarantool["binlog_pos_space"].as<unsigned>(),
tarantool["binlog_pos_key"].as<unsigned>(),
tarantool["connect_retry"].as<unsigned>(),
tarantool["sync_retry"].as<unsigned>()
);
}
// read Mysql to Tarantool mappings (each table maps to a single Tarantool space)
{
std::map<unsigned, bool> has_primary;
const YAML::Node& mappings = cfg["mappings"];
for (int i = 0; i < mappings.size(); i++) {
const YAML::Node& mapping = mappings[i];
const std::string database = mapping["database"].as<std::string>();
const std::string table = mapping["table"].as<std::string>();
std::string insert_call = mapping["insert_call"] ? mapping["insert_call"].as<std::string>() : TPWriter::empty_call;
std::string update_call = mapping["update_call"] ? mapping["update_call"].as<std::string>() : TPWriter::empty_call;
std::string delete_call = mapping["delete_call"] ? mapping["delete_call"].as<std::string>() : TPWriter::empty_call;
const unsigned space = mapping["space"].as<unsigned>();
std::map<std::string, std::pair<unsigned, bool>> columns;
std::vector<unsigned> keys;
unsigned index_max = tpwriter->space_last_id[space];
bool is_primary;
if (has_primary.find(space) == has_primary.end()) {
is_primary = has_primary[space] = true;;
} else {
is_primary = false;
}
// read key tarantool fields we'll use for delete requests
{
const YAML::Node& keys_ = mapping["key_fields"];
for (int i = 0; i < keys_.size(); i++) {
unsigned key = keys_[i].as<unsigned>();
index_max = std::max(index_max, key);
keys.push_back(key);
}
}
// read columns tuple
{
const YAML::Node& columns_ = mapping["columns"];
for (int i = 0; i < columns_.size(); i++) {
const bool is_key = i < keys.size();
const unsigned index = is_key ? keys[i] : ++index_max;
columns.emplace(
std::piecewise_construct,
std::forward_as_tuple(columns_[i].as<std::string>()),
std::forward_as_tuple(index, is_key)
);
}
}
dbreader->AddTable(database, table, columns, is_primary);
std::sort(keys.begin(), keys.end());
tpwriter->AddTable(database, table, space, keys, insert_call, update_call, delete_call);
tpwriter->space_last_id[space] = index_max;
}
}
// read space settings
{
const YAML::Node& spaces = cfg["spaces"];
for (auto its = spaces.begin(); its != spaces.end(); ++its) {
unsigned space = its->first.as<unsigned>();
std::map<unsigned, SerializableValue>& rn_ = tpwriter->replace_null[ space ];
const YAML::Node& replace_null = its->second["replace_null"];
for (auto itrn = replace_null.begin(); itrn != replace_null.end(); ++itrn) {
const YAML::Node& field = itrn->second;
auto itf = field.begin();
if (itf == field.end()) continue;
const unsigned index = itrn->first.as<unsigned>();
std::string type = itf->first.as<std::string>();
const YAML::Node& value = itf->second;
if (type == "str" || type == "string") {
rn_[ index ] = value.as<std::string>();
} else if (type == "unsigned") {
rn_[ index ] = value.as<unsigned long long>();
} else if (type == "int" || type == "integer") {
rn_[ index ] = value.as<long long>();
} else {
std::cerr << "Config error: unknown type for non-null value for column " << index << std::endl;
exit(EXIT_FAILURE);
}
}
}
}
}
catch(YAML::Exception& ex)
{
std::cerr << "Config error: " << ex.what() << std::endl;
exit(EXIT_FAILURE);
}
}
static void shutdown()
{
if (dbreader) {
// sighandler protection
auto dbreader_ = dbreader;
dbreader = NULL;
delete dbreader_;
}
if (tpwriter) {
auto tpwriter_ = tpwriter;
tpwriter = NULL;
delete tpwriter_;
}
}
static void sigint_handler(int sig)
{
std::cerr << "Terminating" << std::endl;
is_term = true;
if (dbreader) {
dbreader->Stop();
}
}
}
static replicator::Logger *ol, *el;
static std::streambuf *ol_sink, *el_sink;
static std::ofstream *flog;
static std::string log_filename(replicator::default_log_filename);
static std::string pid_filename(replicator::default_pid_filename);
static void writepidtofile()
{
// write pid to file
std::ofstream fpid(pid_filename);
fpid << getpid();
fpid.flush();
fpid.close();
}
static void removepidfile()
{
unlink(pid_filename.c_str());
}
static void openlogfile()
{
flog = new std::ofstream(log_filename, std::ofstream::app);
// redirect cout and cerr streams, appending timestamps and log levels
ol = new replicator::Logger(std::cout, 'I');
el = new replicator::Logger(std::cerr, 'E');
ol_sink = ol->rdsink();
el_sink = el->rdsink();
// redirect loggers to file
ol->rdsink(flog->rdbuf());
el->rdsink(flog->rdbuf());
}
static void closelogfile()
{
if (flog == NULL) {
return;
}
flog->flush();
flog->close();
delete flog;
flog = NULL;
// restore streams
ol->rdsink(ol_sink);
el->rdsink(el_sink);
delete ol;
delete el;
ol = NULL;
el = NULL;
}
static void sighup_handler(int sig)
{
closelogfile();
openlogfile();
std::cout << "Caught SIGHUP, continuing..." << std::endl;
}
int main(int argc, char** argv)
{
bool print_usage = false;
std::string config_name(replicator::default_config_filename);
int c;
while (-1 != (c = ::getopt(argc, argv, "c:l:i:zp")))
{
switch (c)
{
case 'c': config_name = optarg; break;
case 'p': print_usage = true; break;
case 'l': log_filename = optarg; break;
case 'i': pid_filename = optarg; break;
default: print_usage = true; break;
}
}
if (print_usage) {
std::cout
<< "Usage: " << argv[0] << " [-c <config_name>]" << " [-l <log_name>]"<< " [-i <pid_name>]" << " [-p]" << std::endl
<< " -c configuration file (" << config_name << ")" << std::endl
<< " -p print usage" << std::endl
<< " -l log filename (" << log_filename << ")" << std::endl
<< " -i pid filename (" << pid_filename << ")" << std::endl
;
return 1;
}
writepidtofile();
atexit(removepidfile);
openlogfile();
atexit(closelogfile);
YAML::Node cfg;
// Read the file. If there is an error, report it and exit.
try {
cfg = YAML::LoadFile(config_name.c_str());
}
catch(YAML::Exception& ex)
{
std::cerr << "Config error: " << ex.what() << std::endl;
return EXIT_FAILURE;
}
signal(SIGINT, replicator::sigint_handler);
signal(SIGTERM, replicator::sigint_handler);
signal(SIGHUP, sighup_handler);
replicator::init(cfg);
std::thread t1(replicator::mysql_worker);
std::thread t2(replicator::tpwriter_worker);
t2.join();
t1.join();
replicator::shutdown();
return 0;
}