-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.cpp
More file actions
713 lines (614 loc) · 26.1 KB
/
app.cpp
File metadata and controls
713 lines (614 loc) · 26.1 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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
// Copyright (c) 2023-2024 Percona and/or its affiliates.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License, version 2.0,
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License, version 2.0, for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include <algorithm>
#include <atomic>
#include <cassert>
#include <chrono>
#include <csignal>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <locale>
#include <memory>
#include <optional>
#include <sstream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <thread>
#include <utility>
#include <boost/lexical_cast.hpp>
#include <boost/lexical_cast/try_lexical_convert.hpp>
#include "app_version.hpp"
#include "binsrv/basic_logger.hpp"
#include "binsrv/exception_handling_helpers.hpp"
#include "binsrv/log_severity.hpp"
#include "binsrv/logger_factory.hpp"
#include "binsrv/main_config.hpp"
#include "binsrv/operation_mode_type.hpp"
#include "binsrv/replication_mode_type.hpp"
#include "binsrv/size_unit.hpp"
#include "binsrv/storage.hpp"
// needed for storage_backend_type's operator <<
#include "binsrv/storage_backend_type.hpp" // IWYU pragma: keep
#include "binsrv/time_unit.hpp"
#include "binsrv/gtids/common_types.hpp"
#include "binsrv/gtids/gtid_set.hpp"
#include "binsrv/event/code_type.hpp"
#include "binsrv/event/common_header_flag_type.hpp"
#include "binsrv/event/event.hpp"
#include "binsrv/event/reader_context.hpp"
#include "easymysql/connection.hpp"
#include "easymysql/connection_config.hpp"
#include "easymysql/core_error.hpp"
#include "easymysql/library.hpp"
// Needed for ssl_mode_type's operator <<
#include "easymysql/ssl_mode_type.hpp" // IWYU pragma: keep
#include "util/byte_span_fwd.hpp"
#include "util/command_line_helpers.hpp"
#include "util/common_optional_types.hpp"
#include "util/ct_string.hpp"
#include "util/exception_location_helpers.hpp"
#include "util/nv_tuple.hpp"
namespace {
[[nodiscard]] bool check_cmd_args(const util::command_line_arg_view &cmd_args,
binsrv::operation_mode_type &operation_mode,
std::string_view &config_file_path) noexcept {
const auto number_of_cmd_args = std::size(cmd_args);
static constexpr std::size_t expected_number_of_cmd_args_min{2U};
if (number_of_cmd_args < expected_number_of_cmd_args_min) {
return false;
}
operation_mode = binsrv::operation_mode_type::delimiter;
if (!boost::conversion::try_lexical_convert(cmd_args[1], operation_mode)) {
return false;
}
if (operation_mode == binsrv::operation_mode_type::delimiter) {
return false;
}
static constexpr std::size_t expected_number_of_cmd_args_with_config{3U};
switch (operation_mode) {
case binsrv::operation_mode_type::fetch:
case binsrv::operation_mode_type::pull:
if (number_of_cmd_args != expected_number_of_cmd_args_with_config) {
return false;
}
config_file_path = cmd_args[expected_number_of_cmd_args_with_config - 1U];
return true;
case binsrv::operation_mode_type::version:
return number_of_cmd_args == expected_number_of_cmd_args_min;
break;
default:
// should never be here
assert(false);
return false;
}
}
template <typename T> util::optional_string to_log_string(const T &value) {
return boost::lexical_cast<std::string>(value);
}
util::optional_string to_log_string(const binsrv::size_unit &value) {
return value.get_description();
}
util::optional_string to_log_string(const binsrv::time_unit &value) {
return value.get_description();
}
util::optional_string to_log_string(bool value) {
return {value ? "true" : "false"};
}
template <typename T>
util::optional_string to_log_string(const std::optional<T> &value) {
if (!value.has_value()) {
return {};
}
return to_log_string(value.value());
}
template <util::ct_string CTS, util::derived_from_named_value_tuple Config>
void log_config_param(binsrv::basic_logger &logger, const Config &config,
std::string_view label) {
const auto opt_log_string{to_log_string(config.template get<CTS>())};
if (opt_log_string.has_value()) {
std::string msg{label};
msg += ": ";
msg += opt_log_string.value();
logger.log(binsrv::log_severity::info, msg);
}
}
void log_ssl_config_info(binsrv::basic_logger &logger,
const easymysql::ssl_config &ssl_config) {
log_config_param<"mode">(logger, ssl_config, "SSL mode");
log_config_param<"ca">(logger, ssl_config, "SSL ca");
log_config_param<"capath">(logger, ssl_config, "SSL capath");
log_config_param<"crl">(logger, ssl_config, "SSL crl");
log_config_param<"crlpath">(logger, ssl_config, "SSL crlpath");
log_config_param<"cert">(logger, ssl_config, "SSL cert");
log_config_param<"key">(logger, ssl_config, "SSL key");
log_config_param<"cipher">(logger, ssl_config, "SSL cipher");
}
void log_tls_config_info(binsrv::basic_logger &logger,
const easymysql::tls_config &tls_config) {
log_config_param<"ciphersuites">(logger, tls_config, "TLS ciphersuites");
log_config_param<"version">(logger, tls_config, "TLS version");
}
void log_connection_config_info(
binsrv::basic_logger &logger,
const easymysql::connection_config &connection_config) {
std::string msg;
msg = "mysql connection string: ";
msg += connection_config.get_connection_string();
logger.log(binsrv::log_severity::info, msg);
log_config_param<"connect_timeout">(logger, connection_config,
"mysql connect timeout (seconds)");
log_config_param<"read_timeout">(logger, connection_config,
"mysql read timeout (seconds)");
log_config_param<"write_timeout">(logger, connection_config,
"mysql write timeout (seconds)");
const auto &optional_ssl_config{connection_config.get<"ssl">()};
if (optional_ssl_config.has_value()) {
log_ssl_config_info(logger, optional_ssl_config.value());
}
const auto &optional_tls_config{connection_config.get<"tls">()};
if (optional_tls_config.has_value()) {
log_tls_config_info(logger, optional_tls_config.value());
}
}
void log_replication_config_info(
binsrv::basic_logger &logger,
const binsrv::replication_config &replication_config) {
log_config_param<"server_id">(logger, replication_config,
"mysql replication server id");
log_config_param<"idle_time">(logger, replication_config,
"mysql replication idle time (seconds)");
log_config_param<"verify_checksum">(
logger, replication_config, "mysql replication checksum verification");
log_config_param<"mode">(logger, replication_config,
"mysql replication mode");
}
void log_storage_config_info(binsrv::basic_logger &logger,
const binsrv::storage_config &storage_config) {
log_config_param<"backend">(logger, storage_config,
"binlog storage backend type");
// not printing "uri" here deliberately to avoid credentials leaking
log_config_param<"fs_buffer_directory">(
logger, storage_config,
"binlog storage backend filesystem buffer directory");
log_config_param<"checkpoint_size">(
logger, storage_config, "binlog storage backend checkpointing size");
log_config_param<"checkpoint_interval">(
logger, storage_config, "binlog storage backend checkpointing interval");
}
void log_storage_info(binsrv::basic_logger &logger,
const binsrv::storage &storage) {
std::string msg{"created binlog storage with the following backend: "};
msg += storage.get_backend_description();
logger.log(binsrv::log_severity::info, msg);
msg.clear();
msg = "binlog storage initialized in ";
msg += boost::lexical_cast<std::string>(storage.get_replication_mode());
msg += " mode";
logger.log(binsrv::log_severity::info, msg);
msg.clear();
if (storage.has_current_binlog_name()) {
msg = "binlog storage initialized at \"";
msg += storage.get_current_binlog_name();
msg += "\":";
msg += std::to_string(storage.get_current_position());
} else {
msg = "binlog storage initialized on an empty directory";
}
logger.log(binsrv::log_severity::info, msg);
}
void log_library_info(binsrv::basic_logger &logger,
const easymysql::library &mysql_lib) {
std::string msg{};
msg = "mysql client version: ";
msg += mysql_lib.get_readable_client_version();
logger.log(binsrv::log_severity::info, msg);
}
void log_connection_info(binsrv::basic_logger &logger,
const easymysql::connection &connection) {
std::string msg{};
msg = "mysql server version: ";
msg += connection.get_readable_server_version();
logger.log(binsrv::log_severity::info, msg);
logger.log(binsrv::log_severity::info,
"mysql protocol version: " +
std::to_string(connection.get_protocol_version()));
msg = "mysql server connection info: ";
msg += connection.get_server_connection_info();
logger.log(binsrv::log_severity::info, msg);
msg = "mysql connection character set: ";
msg += connection.get_character_set_name();
logger.log(binsrv::log_severity::info, msg);
}
void log_replication_info(
binsrv::basic_logger &logger, std::uint32_t server_id,
const binsrv::storage &storage, bool verify_checksum,
easymysql::connection_replication_mode_type blocking_mode) {
const auto replication_mode{storage.get_replication_mode()};
std::string msg{"switched to replication (checksum "};
msg += (verify_checksum ? "enabled" : "disabled");
msg += ", ";
msg += boost::lexical_cast<std::string>(replication_mode);
msg += +" mode)";
logger.log(binsrv::log_severity::info, msg);
msg = "replication info (server id ";
msg += std::to_string(server_id);
msg += ", ";
msg += (blocking_mode == easymysql::connection_replication_mode_type::blocking
? "blocking"
: "non-blocking");
msg += ", starting from ";
if (replication_mode == binsrv::replication_mode_type::position) {
if (storage.has_current_binlog_name()) {
msg += "the very beginning";
} else {
msg += storage.get_current_binlog_name();
msg += ":";
msg += std::to_string(storage.get_current_position());
}
} else {
const auto >ids{storage.get_gtids()};
if (gtids.is_empty()) {
msg += "an empty";
} else {
msg += "the ";
msg += boost::lexical_cast<std::string>(gtids);
}
msg += " GTID set";
}
msg += ")";
logger.log(binsrv::log_severity::info, msg);
}
void log_span_dump(binsrv::basic_logger &logger,
util::const_byte_span portion) {
static constexpr std::size_t bytes_per_dump_line{16U};
std::size_t offset{0U};
while (offset < std::size(portion)) {
std::ostringstream oss;
oss << '[';
oss << std::setfill('0') << std::hex;
auto sub = portion.subspan(
offset, std::min(bytes_per_dump_line, std::size(portion) - offset));
for (auto current_byte : sub) {
oss << ' ' << std::setw(2)
<< std::to_integer<std::uint16_t>(current_byte);
}
const std::size_t filler_length =
(bytes_per_dump_line - std::size(sub)) * 3U;
oss << std::setfill(' ') << std::setw(static_cast<int>(filler_length))
<< "";
oss << " ] ";
const auto &ctype_facet{
std::use_facet<std::ctype<char>>(std::locale::classic())};
for (auto current_byte : sub) {
auto current_char{std::to_integer<char>(current_byte)};
if (!ctype_facet.is(std::ctype_base::print, current_char)) {
current_char = '.';
}
oss.put(current_char);
}
logger.log(binsrv::log_severity::trace, oss.str());
offset += bytes_per_dump_line;
}
}
void process_binlog_event(const binsrv::event::event ¤t_event,
util::const_byte_span portion,
binsrv::storage &storage, bool &skip_open_binlog) {
const auto ¤t_common_header = current_event.get_common_header();
const auto code = current_common_header.get_type_code();
const auto is_artificial{current_common_header.get_flags().has_element(
binsrv::event::common_header_flag_type::artificial)};
const auto is_pseudo{current_common_header.get_next_event_position_raw() ==
0U};
if (code == binsrv::event::code_type::rotate && is_artificial) {
const auto ¤t_rotate_body =
current_event.get_body<binsrv::event::code_type::rotate>();
if (skip_open_binlog) {
// we are supposed to get here after reconnection, so doing
// basic integrity checks
if (current_rotate_body.get_binlog() !=
storage.get_current_binlog_name()) {
util::exception_location().raise<std::logic_error>(
"unexpected binlog name in artificial rotate event after "
"reconnection");
}
const auto ¤t_rotate_post_header =
current_event.get_post_header<binsrv::event::code_type::rotate>();
if (current_rotate_post_header.get_position_raw() !=
storage.get_current_position()) {
util::exception_location().raise<std::logic_error>(
"unexpected binlog position in artificial rotate event after "
"reconnection");
}
skip_open_binlog = false;
} else {
// in case when the server was not shut down properly, it won't have
// ROTATE or STOP event as the last one in the binlog, so here we
// handle this case by closing the old binlog and opening a new one
if (storage.is_binlog_open()) {
storage.close_binlog();
}
storage.open_binlog(current_rotate_body.get_binlog());
}
}
if (!is_artificial && !is_pseudo) {
storage.write_event(portion);
}
if ((code == binsrv::event::code_type::rotate && !is_artificial) ||
code == binsrv::event::code_type::stop) {
storage.close_binlog();
}
}
void receive_binlog_events(
binsrv::operation_mode_type operation_mode,
const volatile std::atomic_flag &termination_flag,
binsrv::basic_logger &logger, const easymysql::library &mysql_lib,
const easymysql::connection_config &connection_config,
std::uint32_t server_id, bool verify_checksum, binsrv::storage &storage) {
easymysql::connection connection{};
try {
connection = mysql_lib.create_connection(connection_config);
} catch (const easymysql::core_error &) {
if (operation_mode == binsrv::operation_mode_type::fetch) {
throw;
}
logger.log(binsrv::log_severity::info,
"unable to establish connection to mysql server");
return;
}
logger.log(binsrv::log_severity::info,
"established connection to mysql server");
log_connection_info(logger, connection);
const auto replication_mode{storage.get_replication_mode()};
const auto blocking_mode{
operation_mode == binsrv::operation_mode_type::fetch
? easymysql::connection_replication_mode_type::non_blocking
: easymysql::connection_replication_mode_type::blocking};
try {
if (replication_mode == binsrv::replication_mode_type::position) {
if (storage.has_current_binlog_name()) {
connection.switch_to_position_replication(
server_id, storage.get_current_binlog_name(),
storage.get_current_position(), verify_checksum, blocking_mode);
} else {
connection.switch_to_position_replication(server_id, verify_checksum,
blocking_mode);
}
} else {
const binsrv::gtids::gtid_set empty_gtids{};
const auto encoded_size{empty_gtids.calculate_encoded_size()};
binsrv::gtids::gtid_set_storage buffer(encoded_size);
util::byte_span destination{buffer};
empty_gtids.encode_to(destination);
connection.switch_to_gtid_replication(server_id,
util::const_byte_span{buffer},
verify_checksum, blocking_mode);
}
} catch (const easymysql::core_error &) {
if (operation_mode == binsrv::operation_mode_type::fetch) {
throw;
}
logger.log(binsrv::log_severity::info, "unable to switch to replication");
return;
}
log_replication_info(logger, server_id, storage, verify_checksum,
blocking_mode);
// Network streams are requested with COM_BINLOG_DUMP and
// each Binlog Event response is prepended with 00 OK-byte.
static constexpr std::byte expected_event_packet_prefix{'\0'};
util::const_byte_span portion;
binsrv::event::reader_context context{connection.get_server_version(),
verify_checksum, replication_mode};
// if binlog is still open, there is no sense to close it and re-open
// instead, we will just instruct this loop to process the
// very first artificial rotate event in a special way
bool skip_open_binlog{storage.is_binlog_open()};
bool fetch_result{};
while (!termination_flag.test() &&
(fetch_result = connection.fetch_binlog_event(portion)) &&
!portion.empty()) {
if (portion[0] != expected_event_packet_prefix) {
util::exception_location().raise<std::runtime_error>(
"unexpected event prefix");
}
portion = portion.subspan(1U);
logger.log(binsrv::log_severity::info,
"fetched " + std::to_string(std::size(portion)) +
"-byte(s) event from binlog");
log_span_dump(logger, portion);
// TODO: just for redirection to another byte stream we need to parse
// the ROTATE and FORMAT_DESCRIPTION events only, every other one
// can be just considered as a data portion (unless we want to do
// basic integrity checks like event sizes / position and CRC)
const binsrv::event::event current_event{context, portion};
logger.log(binsrv::log_severity::debug,
"Parsed event:\n" +
boost::lexical_cast<std::string>(current_event));
process_binlog_event(current_event, portion, storage, skip_open_binlog);
}
if (termination_flag.test()) {
logger.log(binsrv::log_severity::info,
"fetching binlog events loop terminated by signal");
return;
}
if (fetch_result) {
logger.log(binsrv::log_severity::info,
"fetched everything and disconnected");
return;
}
if (operation_mode == binsrv::operation_mode_type::fetch) {
util::exception_location().raise<std::logic_error>(
"fetch operation did not reach EOF reading binlog events");
}
logger.log(binsrv::log_severity::info,
"timed out waiting for events and disconnected");
}
bool wait_for_interruptable(std::uint32_t idle_time_seconds,
const volatile std::atomic_flag &termination_flag) {
// instead of
// 'std::this_thread::sleep_for(std::chrono::seconds(idle_time_seconds))'
// we do 'std::this_thread::sleep_for(1s)' '<idle_time_seconds>' times
// in a loop also checking for termination condition
// standard pattern with declaring an instance of
// std::conditional_variable and waiting for it (for
// '<idle_time_seconds>' seconds) to be notified from the signal handler
// can be dangerous as the chances of signal handler being called on the
// same thread as this one ('main()') are pretty big.
for (std::uint32_t sleep_iteration{0U};
sleep_iteration < idle_time_seconds && !termination_flag.test();
++sleep_iteration) {
std::this_thread::sleep_for(std::chrono::seconds(1U));
}
return !termination_flag.test();
}
// since c++20 it is no longer needed to initialize std::atomic_flag with
// ATOMIC_FLAG_INIT as this flag is modified from a signal handler it is marked
// as volatile to make sure optimizer do optimizations which will be unsafe for
// this scenario
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
volatile std::atomic_flag global_termination_flag{};
} // anonymous namespace
extern "C" void custom_signal_handler(int /*signo*/) {
global_termination_flag.test_and_set();
}
int main(int argc, char *argv[]) {
using namespace std::string_literals;
const auto cmd_args = util::to_command_line_agg_view(argc, argv);
const auto executable_name = util::extract_executable_name(cmd_args);
binsrv::operation_mode_type operation_mode{
binsrv::operation_mode_type::delimiter};
std::string_view config_file_path;
const auto cmd_args_checked{
check_cmd_args(cmd_args, operation_mode, config_file_path)};
if (!cmd_args_checked) {
std::cerr << "usage: " << executable_name
<< " (fetch|pull)) <json_config_file>\n"
<< " " << executable_name << " version\n";
return EXIT_FAILURE;
}
if (operation_mode == binsrv::operation_mode_type::version) {
std::cout << app_version.get_string() << '\n';
return EXIT_SUCCESS;
}
int exit_code = EXIT_FAILURE;
binsrv::basic_logger_ptr logger;
try {
static constexpr auto default_log_level = binsrv::log_severity::trace;
const binsrv::logger_config initial_logger_config{
{{default_log_level}, {""}}};
logger = binsrv::logger_factory::create(initial_logger_config);
// logging with "delimiter" level has the highest priority and empty label
logger->log(binsrv::log_severity::delimiter,
'"' + executable_name + '"' +
" started with the following command line arguments:");
logger->log(binsrv::log_severity::delimiter,
util::get_readable_command_line_arguments(cmd_args));
binsrv::main_config_ptr config;
logger->log(binsrv::log_severity::delimiter,
"reading configuration from the JSON file.");
config = std::make_shared<binsrv::main_config>(config_file_path);
assert(config);
const auto &logger_config = config->root().get<"logger">();
if (!logger_config.has_file()) {
logger->set_min_level(logger_config.get<"level">());
} else {
logger->log(binsrv::log_severity::delimiter,
"redirecting logging to \"" + logger_config.get<"file">() +
"\"");
auto new_logger = binsrv::logger_factory::create(logger_config);
std::swap(logger, new_logger);
}
const auto log_level_label =
binsrv::to_string_view(logger->get_min_level());
logger->log(binsrv::log_severity::delimiter,
"logging level set to \""s + std::string{log_level_label} +
'"');
logger->log(binsrv::log_severity::delimiter,
"application version: " + app_version.get_string());
assert(operation_mode == binsrv::operation_mode_type::fetch ||
operation_mode == binsrv::operation_mode_type::pull);
std::string msg;
msg = '\'';
msg += boost::lexical_cast<std::string>(operation_mode);
msg += "' operation mode specified";
logger->log(binsrv::log_severity::delimiter, msg);
// setting custom SIGINT and SIGTERM signal handlers
if (std::signal(SIGTERM, &custom_signal_handler) == SIG_ERR) {
util::exception_location().raise<std::logic_error>(
"cannot set custom signal handler for SIGTERM");
}
if (std::signal(SIGINT, &custom_signal_handler) == SIG_ERR) {
util::exception_location().raise<std::logic_error>(
"cannot set custom signal handler for SIGINT");
}
logger->log(binsrv::log_severity::info,
"set custom handlers for SIGINT and SIGTERM signals");
const volatile std::atomic_flag &termination_flag{global_termination_flag};
const auto &storage_config = config->root().get<"storage">();
log_storage_config_info(*logger, storage_config);
const auto &connection_config = config->root().get<"connection">();
log_connection_config_info(*logger, connection_config);
const auto &replication_config = config->root().get<"replication">();
log_replication_config_info(*logger, replication_config);
const auto server_id{replication_config.get<"server_id">()};
const auto idle_time_seconds{replication_config.get<"idle_time">()};
const auto verify_checksum{replication_config.get<"verify_checksum">()};
const auto replication_mode{replication_config.get<"mode">()};
binsrv::storage storage{storage_config, replication_mode};
log_storage_info(*logger, storage);
const easymysql::library mysql_lib;
logger->log(binsrv::log_severity::info, "initialized mysql client library");
log_library_info(*logger, mysql_lib);
receive_binlog_events(operation_mode, termination_flag, *logger, mysql_lib,
connection_config, server_id, verify_checksum,
storage);
if (operation_mode == binsrv::operation_mode_type::pull) {
std::size_t iteration_number{1U};
while (!termination_flag.test()) {
msg = "entering idle mode for ";
msg += std::to_string(idle_time_seconds);
msg += " seconds";
logger->log(binsrv::log_severity::info, msg);
if (!wait_for_interruptable(idle_time_seconds, termination_flag)) {
break;
}
msg = "awoke after sleeping and trying to reconnect (iteration ";
msg += std::to_string(iteration_number);
msg += ')';
logger->log(binsrv::log_severity::info, msg);
receive_binlog_events(operation_mode, termination_flag, *logger,
mysql_lib, connection_config, server_id,
verify_checksum, storage);
++iteration_number;
}
}
if (termination_flag.test()) {
logger->log(
binsrv::log_severity::info,
"successfully shut down after receiving a termination signal");
} else {
logger->log(
binsrv::log_severity::info,
"successfully shut down after finishing the requested operation");
}
exit_code = EXIT_SUCCESS;
} catch (...) {
handle_std_exception(logger);
}
return exit_code;
}