Skip to content

Commit

Permalink
Added replication section to main configuration
Browse files Browse the repository at this point in the history
Added 'easymysql::replication_config' class with two fields:
* 'server_id' - the ID of the MySQL pseudo server that will be send
to the server when switching connection to replication mode.
* 'idle_time' - the number of seconds the utility will wait between
reconnection attempts.
Both parameters can be specified both in JSON configuration file
and via command line arguments.
Sample JSON configuration file ('main_config.json') and
'binlog_streaming.binsrv' MTR test case updated correspondingly.

After this change the replication connection (after reading all available
binlog events) will wait for '<connection.read_timeout>' seconds, then
will close the connection and will wait for '<replication.idle_time>'
seconds before trying to reconnect.

Logging of connection configuration info and replication configuration
info moved from the 'main()' into separate functions:
* 'log_connection_config_info()'
* 'log_replication_info()'
  • Loading branch information
percona-ysorokin committed May 16, 2024
1 parent 2c272e1 commit 83a9d25
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 30 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ set(source_files
src/easymysql/library_fwd.hpp
src/easymysql/library.hpp
src/easymysql/library.cpp

src/easymysql/replication_config_fwd.hpp
src/easymysql/replication_config.hpp
)

add_executable(binlog_server ${source_files})
Expand Down
4 changes: 4 additions & 0 deletions main_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
"read_timeout": 60,
"write_timeout": 60
},
"replication": {
"server_id": 42,
"idle_time": 10
},
"storage": {
"uri": "file:///home/user/vault"
}
Expand Down
4 changes: 4 additions & 0 deletions mtr/binlog_streaming/t/binsrv.test
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ eval SET @binsrv_config_json = JSON_OBJECT(
'read_timeout', 60,
'write_timeout', 60
),
'replication', JSON_OBJECT(
'server_id', @@server_id + 1,
'idle_time', 10
),
'storage', JSON_OBJECT(
'uri', @storage_uri
)
Expand Down
81 changes: 55 additions & 26 deletions src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,36 @@

namespace {

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);

msg = "mysql connect timeout (seconds): ";
msg += std::to_string(connection_config.get<"connect_timeout">());
logger.log(binsrv::log_severity::info, msg);

msg = "mysql read timeout (seconds): ";
msg += std::to_string(connection_config.get<"read_timeout">());
logger.log(binsrv::log_severity::info, msg);

msg = "mysql write timeout (seconds): ";
msg += std::to_string(connection_config.get<"write_timeout">());
logger.log(binsrv::log_severity::info, msg);
}

void log_replication_info(
binsrv::basic_logger &logger,
const easymysql::replication_config &replication_config) {
std::string msg;
msg = "mysql replication server id: ";
msg += std::to_string(replication_config.get<"server_id">());
logger.log(binsrv::log_severity::info, msg);
}

void log_storage_info(binsrv::basic_logger &logger,
const binsrv::storage &storage) {
std::string msg{};
Expand Down Expand Up @@ -324,7 +354,9 @@ int main(int argc, char *argv[]) {
std::cerr << "usage: " << executable_name << " (fetch|pull))"
<< " <logger.level> <logger.file> <connection.host>"
" <connection.port> <connection.user> <connection.password>"
" <connect_timeout> <read_timeout> <write_timeout>"
" <connection.connect_timeout> <connection.read_timeout> "
"<connection.write_timeout>"
" <replication.server_id> <replication.idle_time>"
" <storage.uri>\n"
<< " " << executable_name
<< " (fetch|pull)) <json_config_file>\n";
Expand All @@ -346,24 +378,22 @@ int main(int argc, char *argv[]) {
logger->log(binsrv::log_severity::delimiter,
util::get_readable_command_line_arguments(cmd_args));

if (operation_mode == binsrv::operation_mode_type::fetch) {
logger->log(binsrv::log_severity::delimiter,
"'fetch' operation mode specified");
} else if (operation_mode == binsrv::operation_mode_type::pull) {
logger->log(binsrv::log_severity::delimiter,
"'pull' operation mode specified");
} else {
assert(false);
}
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);

binsrv::main_config_ptr config;
if (number_of_cmd_args == 3U) {
logger->log(binsrv::log_severity::delimiter,
"reading connection configuration from the JSON file.");
"reading configuration from the JSON file.");
config = std::make_shared<binsrv::main_config>(cmd_args[2]);
} else if (number_of_cmd_args == binsrv::main_config::flattened_size + 2U) {
logger->log(binsrv::log_severity::delimiter,
"reading connection configuration from the command line "
"reading configuration from the command line "
"arguments.");
config = std::make_shared<binsrv::main_config>(cmd_args.subspan(2U));
} else {
Expand All @@ -388,12 +418,10 @@ int main(int argc, char *argv[]) {
"logging level set to \""s + std::string{log_level_label} +
'"');

std::string msg;
const auto &storage_config = config->root().get<"storage">();
auto storage_backend{
binsrv::storage_backend_factory::create(storage_config)};
logger->log(binsrv::log_severity::info, "created binlog storage backend");
msg = "description: ";
msg = "created binlog storage backend: ";
msg += storage_backend->get_description();
logger->log(binsrv::log_severity::info, msg);

Expand All @@ -404,16 +432,13 @@ int main(int argc, char *argv[]) {
log_storage_info(*logger, storage);

const auto &connection_config = config->root().get<"connection">();
logger->log(binsrv::log_severity::info,
"mysql connection string: " +
connection_config.get_connection_string());
log_connection_config_info(*logger, connection_config);

// TODO: put these parameters into configuration
static constexpr std::uint32_t default_server_id{42U};
static constexpr std::uint32_t default_idle_time_seconds{5U};
const auto &replication_config = config->root().get<"replication">();
log_replication_info(*logger, replication_config);

const auto server_id{default_server_id};
const auto idle_time_seconds{default_idle_time_seconds};
const auto server_id{replication_config.get<"server_id">()};
const auto idle_time_seconds{replication_config.get<"idle_time">()};

const easymysql::library mysql_lib;
logger->log(binsrv::log_severity::info, "initialized mysql client library");
Expand All @@ -428,9 +453,13 @@ int main(int argc, char *argv[]) {
while (true) {
std::this_thread::sleep_for(std::chrono::seconds(idle_time_seconds));

logger->log(binsrv::log_severity::info,
"awoke after sleep and trying to reconnect (" +
std::to_string(iteration_number) + ")");
msg = "awoke after sleeping for ";
msg += std::to_string(idle_time_seconds);
msg += " seconds and trying to reconnect (iteration ";
msg += std::to_string(iteration_number);
msg += ')';
logger->log(binsrv::log_severity::info, msg);

receive_binlog_events(operation_mode, *logger, mysql_lib,
connection_config, server_id, storage);
++iteration_number;
Expand Down
10 changes: 6 additions & 4 deletions src/binsrv/main_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
#include "binsrv/logger_config.hpp" // IWYU pragma: export
#include "binsrv/storage_config.hpp" // IWYU pragma: export

#include "easymysql/connection_config.hpp" // IWYU pragma: export
#include "easymysql/connection_config.hpp" // IWYU pragma: export
#include "easymysql/replication_config.hpp" // IWYU pragma: export

#include "util/command_line_helpers_fwd.hpp"
#include "util/nv_tuple.hpp"
Expand All @@ -32,9 +33,10 @@ class [[nodiscard]] main_config {
private:
using impl_type = util::nv_tuple<
// clang-format off
util::nv<"logger" , logger_config>,
util::nv<"connection", easymysql::connection_config>,
util::nv<"storage" , storage_config>
util::nv<"logger" , logger_config>,
util::nv<"connection" , easymysql::connection_config>,
util::nv<"replication", easymysql::replication_config>,
util::nv<"storage" , storage_config>
// clang-format on
>;

Expand Down
37 changes: 37 additions & 0 deletions src/easymysql/replication_config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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

#ifndef EASYMYSQL_REPLICATION_CONFIG_HPP
#define EASYMYSQL_REPLICATION_CONFIG_HPP

#include "easymysql/replication_config_fwd.hpp" // IWYU pragma: export

#include <cstdint>

#include "util/nv_tuple.hpp"

namespace easymysql {

struct [[nodiscard]] replication_config
: util::nv_tuple<
// clang-format off
util::nv<"server_id", std::uint32_t>,
util::nv<"idle_time", std::uint32_t>
// clang-format on
> {};

} // namespace easymysql

#endif // EASYMYSQL_REPLICATION_CONFIG_HPP
25 changes: 25 additions & 0 deletions src/easymysql/replication_config_fwd.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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

#ifndef EASYMYSQL_REPLICATION_CONFIG_FWD_HPP
#define EASYMYSQL_REPLICATION_CONFIG_FWD_HPP

namespace easymysql {

struct replication_config;

} // namespace easymysql

#endif // EASYMYSQL_REPLICATION_CONFIG_FWD_HPP

0 comments on commit 83a9d25

Please sign in to comment.