Add ability to start as follower

This commit is contained in:
alesapin 2021-02-11 13:25:10 +03:00
parent 0acd018361
commit 99a471e047
8 changed files with 22 additions and 4 deletions

View File

@ -28,6 +28,9 @@ InMemoryStateManager::InMemoryStateManager(
int port = config.getInt(full_prefix + ".port");
bool can_become_leader = config.getBool(full_prefix + ".can_become_leader", true);
int32_t priority = config.getInt(full_prefix + ".priority", 1);
bool start_as_follower = config.getBool(full_prefix + ".start_as_follower", false);
if (start_as_follower)
start_as_follower_servers.insert(server_id);
auto endpoint = hostname + ":" + std::to_string(port);
auto peer_config = nuraft::cs_new<nuraft::srv_config>(server_id, 0, endpoint, "", !can_become_leader, priority);
@ -41,6 +44,9 @@ InMemoryStateManager::InMemoryStateManager(
}
if (!my_server_config)
throw Exception(ErrorCodes::RAFT_ERROR, "Our server id {} not found in raft_configuration section");
if (start_as_follower_servers.size() == cluster_config->get_servers().size())
throw Exception(ErrorCodes::RAFT_ERROR, "At least one of servers should be able to start as leader (without <start_as_follower>)");
}
void InMemoryStateManager::save_config(const nuraft::cluster_config & config)

View File

@ -35,9 +35,15 @@ public:
int getPort() const { return my_port; }
bool shouldStartAsFollower() const
{
return start_as_follower_servers.count(my_server_id);
}
private:
int my_server_id;
int my_port;
std::unordered_set<int> start_as_follower_servers;
nuraft::ptr<InMemoryLogStore> log_store;
nuraft::ptr<nuraft::srv_config> my_server_config;
nuraft::ptr<nuraft::cluster_config> cluster_config;

View File

@ -31,7 +31,7 @@ NuKeeperServer::NuKeeperServer(
{
}
void NuKeeperServer::startup(bool should_build_quorum)
void NuKeeperServer::startup()
{
nuraft::raft_params params;
params.heart_beat_interval_ = coordination_settings->heart_beat_interval_ms.totalMilliseconds();
@ -47,7 +47,7 @@ void NuKeeperServer::startup(bool should_build_quorum)
nuraft::asio_service::options asio_opts{};
nuraft::raft_server::init_options init_options;
init_options.skip_initial_election_timeout_ = !should_build_quorum;
init_options.skip_initial_election_timeout_ = state_manager->shouldStartAsFollower();
init_options.raft_callback_ = [this] (nuraft::cb_func::Type type, nuraft::cb_func::Param * param)
{
return callbackFunc(type, param);

View File

@ -43,7 +43,7 @@ public:
const Poco::Util::AbstractConfiguration & config,
ResponsesQueue & responses_queue_);
void startup(bool should_build_quorum);
void startup();
void putRequest(const NuKeeperStorage::RequestForSession & request);

View File

@ -114,7 +114,7 @@ void NuKeeperStorageDispatcher::initialize(const Poco::Util::AbstractConfigurati
try
{
LOG_DEBUG(log, "Waiting server to initialize");
server->startup(true);
server->startup();
LOG_DEBUG(log, "Server initialized, waiting for quorum");
server->waitInit();

View File

@ -22,6 +22,7 @@
<hostname>node2</hostname>
<port>44444</port>
<can_become_leader>true</can_become_leader>
<start_as_follower>true</start_as_follower>
<priority>2</priority>
</server>
<server>
@ -29,6 +30,7 @@
<hostname>node3</hostname>
<port>44444</port>
<can_become_leader>true</can_become_leader>
<start_as_follower>true</start_as_follower>
<priority>1</priority>
</server>
</raft_configuration>

View File

@ -22,6 +22,7 @@
<hostname>node2</hostname>
<port>44444</port>
<can_become_leader>true</can_become_leader>
<start_as_follower>true</start_as_follower>
<priority>2</priority>
</server>
<server>
@ -29,6 +30,7 @@
<hostname>node3</hostname>
<port>44444</port>
<can_become_leader>true</can_become_leader>
<start_as_follower>true</start_as_follower>
<priority>1</priority>
</server>
</raft_configuration>

View File

@ -22,6 +22,7 @@
<hostname>node2</hostname>
<port>44444</port>
<can_become_leader>true</can_become_leader>
<start_as_follower>true</start_as_follower>
<priority>2</priority>
</server>
<server>
@ -29,6 +30,7 @@
<hostname>node3</hostname>
<port>44444</port>
<can_become_leader>true</can_become_leader>
<start_as_follower>true</start_as_follower>
<priority>1</priority>
</server>
</raft_configuration>