1
0
mirror of https://github.com/codership/wsrep-lib.git synced 2025-10-18 00:31:19 +03:00

Added SST methods, able to start several nodes in simulator

This commit is contained in:
Teemu Ollakka
2018-04-19 15:03:38 +03:00
parent 3c6eff581c
commit 3510d0739c
10 changed files with 165 additions and 20 deletions

View File

@@ -15,6 +15,7 @@
#include "view.hpp"
#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
#include <iostream>
#include <memory>
@@ -31,7 +32,8 @@ public:
size_t n_clients,
const std::string& wsrep_provider,
const std::string& wsrep_provider_options)
: servers_()
: mutex_()
, servers_()
, n_servers_(n_servers)
, n_clients_(n_clients)
, wsrep_provider_(wsrep_provider)
@@ -39,8 +41,18 @@ public:
{ }
void start();
void stop();
void donate_sst(dbms_server&,
const std::string& req, const wsrep_gtid_t& gtid, bool);
private:
std::string server_port(size_t i) const
{
std::ostringstream os;
os << (10000 + (i + 1)*10);
return os.str();
}
std::string build_cluster_address() const;
trrep::default_mutex mutex_;
std::map<size_t, std::unique_ptr<dbms_server>> servers_;
size_t n_servers_;
size_t n_clients_;
@@ -59,8 +71,11 @@ public:
s_connected,
s_synced
};
dbms_server(const std::string& name, const std::string& id)
: trrep::server_context(name, id, trrep::server_context::rm_async)
dbms_server(dbms_simulator& simulator,
const std::string& name, const std::string& id)
: trrep::server_context(name, id, name + "_data",
trrep::server_context::rm_async)
, simulator_(simulator)
, mutex_()
, cond_()
, state_(s_disconnected)
@@ -123,6 +138,27 @@ public:
std::cerr << "Synced with group" << "\n";
}
std::string on_sst_request()
{
return id();
}
void on_sst_donate_request(const std::string& req,
const wsrep_gtid_t& gtid,
bool bypass)
{
simulator_.donate_sst(*this, req, gtid, bypass);
}
void sst_sent(const wsrep_gtid_t& gtid)
{
provider().sst_sent(gtid, 0);
}
void sst_received(const wsrep_gtid_t& gtid)
{
provider().sst_received(gtid, 0);
}
void wait_until_connected()
{
trrep::unique_lock<trrep::mutex> lock(mutex_);
@@ -143,6 +179,7 @@ public:
// Client context management
trrep::client_context* local_client_context();
private:
dbms_simulator& simulator_;
trrep::default_mutex mutex_;
trrep::default_condition_variable cond_;
enum state state_;
@@ -212,13 +249,18 @@ void dbms_simulator::start()
id_os << (i + 1);
auto it(servers_.insert(std::make_pair((i + 1),
std::make_unique<dbms_server>(
name_os.str(), id_os.str()))));
*this, name_os.str(), id_os.str()))));
if (it.second == false)
{
throw trrep::runtime_error("Failed to add server");
}
boost::filesystem::path dir(std::string("./") + id_os.str() + "_data");
boost::filesystem::create_directory(dir);
dbms_server& server(*it.first->second);
server.load_provider(wsrep_provider_, wsrep_provider_options_);
std::string server_options(wsrep_provider_options_);
server_options += "; base_port=" + server_port(i);
server.load_provider(wsrep_provider_, server_options);
server.provider().connect("sim_cluster", cluster_address, "",
i == 0);
server.start_applier();
@@ -237,6 +279,27 @@ void dbms_simulator::stop()
}
}
void dbms_simulator::donate_sst(dbms_server& server,
const std::string& req,
const wsrep_gtid_t& gtid,
bool bypass)
{
size_t id;
std::istringstream is(req);
is >> id;
trrep::unique_lock<trrep::mutex> lock(mutex_);
auto i(servers_.find(id));
if (i == servers_.end())
{
throw trrep::runtime_error("Server " + req + " not found");
}
if (bypass == false)
{
std::cout << "SST " << server.id() << " -> " << id << "\n";
}
i->second->sst_received(gtid);
server.sst_sent(gtid);
}
std::string dbms_simulator::build_cluster_address() const
{
std::string ret("gcomm://");
@@ -244,7 +307,7 @@ std::string dbms_simulator::build_cluster_address() const
{
std::ostringstream sa_os;
sa_os << "127.0.0.1:";
sa_os << (10000 + (i + 1)*10);
sa_os << server_port(i);
ret += sa_os.str();
if (i < n_servers_ - 1) ret += ",";
}