2014-04-04 10:37:33 +00:00
|
|
|
#pragma once
|
|
|
|
|
2018-01-15 19:07:47 +00:00
|
|
|
#include "ZooKeeper.h"
|
2018-04-19 18:16:18 +00:00
|
|
|
#include "KeeperException.h"
|
2014-04-04 10:37:33 +00:00
|
|
|
#include <functional>
|
2016-05-28 17:31:50 +00:00
|
|
|
#include <memory>
|
2015-09-29 19:19:54 +00:00
|
|
|
#include <common/logger_useful.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/CurrentMetrics.h>
|
2017-12-29 22:32:04 +00:00
|
|
|
#include <Common/BackgroundSchedulePool.h>
|
2016-10-24 13:47:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace ProfileEvents
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const Event ObsoleteEphemeralNode;
|
|
|
|
extern const Event LeaderElectionAcquiredLeadership;
|
2016-10-24 13:47:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace CurrentMetrics
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const Metric LeaderElection;
|
2016-10-24 13:47:15 +00:00
|
|
|
}
|
2014-04-04 10:37:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace zkutil
|
|
|
|
{
|
|
|
|
|
2016-10-23 03:46:51 +00:00
|
|
|
/** Implements leader election algorithm described here: http://zookeeper.apache.org/doc/r3.4.5/recipes.html#sc_leaderElection
|
2014-04-04 10:37:33 +00:00
|
|
|
*/
|
|
|
|
class LeaderElection
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
using LeadershipHandler = std::function<void()>;
|
|
|
|
|
|
|
|
/** handler is called when this instance become leader.
|
|
|
|
*
|
|
|
|
* identifier - if not empty, must uniquely (within same path) identify participant of leader election.
|
|
|
|
* It means that different participants of leader election have different identifiers
|
2018-04-05 20:47:06 +00:00
|
|
|
* and existence of more than one ephemeral node with same identifier indicates an error.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
2017-12-29 22:32:04 +00:00
|
|
|
LeaderElection(DB::BackgroundSchedulePool & pool_, const std::string & path_, ZooKeeper & zookeeper_, LeadershipHandler handler_, const std::string & identifier_ = "")
|
|
|
|
: pool(pool_), path(path_), zookeeper(zookeeper_), handler(handler_), identifier(identifier_)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-29 22:32:04 +00:00
|
|
|
task_handle = pool.addTask("LeaderElection", [this] { threadFunction(); });
|
2017-04-01 07:20:54 +00:00
|
|
|
createNode();
|
|
|
|
}
|
|
|
|
|
2018-04-06 16:06:07 +00:00
|
|
|
void shutdown()
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-04-06 16:06:07 +00:00
|
|
|
if (shutdown_called)
|
|
|
|
return;
|
|
|
|
|
|
|
|
shutdown_called = true;
|
2018-04-10 13:20:14 +00:00
|
|
|
task_handle->deactivate();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~LeaderElection()
|
|
|
|
{
|
|
|
|
releaseNode();
|
2017-12-29 22:32:04 +00:00
|
|
|
pool.removeTask(task_handle);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2014-04-04 10:37:33 +00:00
|
|
|
|
|
|
|
private:
|
2017-12-29 22:32:04 +00:00
|
|
|
DB::BackgroundSchedulePool & pool;
|
|
|
|
DB::BackgroundSchedulePool::TaskHandle task_handle;
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string path;
|
|
|
|
ZooKeeper & zookeeper;
|
|
|
|
LeadershipHandler handler;
|
|
|
|
std::string identifier;
|
|
|
|
|
|
|
|
EphemeralNodeHolderPtr node;
|
|
|
|
std::string node_name;
|
|
|
|
|
2018-04-06 16:06:07 +00:00
|
|
|
std::atomic<bool> shutdown_called {false};
|
2017-12-21 18:17:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
CurrentMetrics::Increment metric_increment{CurrentMetrics::LeaderElection};
|
|
|
|
|
|
|
|
void createNode()
|
|
|
|
{
|
2018-04-06 16:06:07 +00:00
|
|
|
shutdown_called = false;
|
2017-04-01 07:20:54 +00:00
|
|
|
node = EphemeralNodeHolder::createSequential(path + "/leader_election-", zookeeper, identifier);
|
|
|
|
|
|
|
|
std::string node_path = node->getPath();
|
|
|
|
node_name = node_path.substr(node_path.find_last_of('/') + 1);
|
|
|
|
|
2017-12-29 22:32:04 +00:00
|
|
|
task_handle->activate();
|
|
|
|
task_handle->schedule();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void releaseNode()
|
|
|
|
{
|
2018-04-06 16:06:07 +00:00
|
|
|
shutdown();
|
2017-04-01 07:20:54 +00:00
|
|
|
node = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void threadFunction()
|
|
|
|
{
|
2017-12-29 22:32:04 +00:00
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
try
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-29 22:32:04 +00:00
|
|
|
Strings children = zookeeper.getChildren(path);
|
|
|
|
std::sort(children.begin(), children.end());
|
|
|
|
auto it = std::lower_bound(children.begin(), children.end(), node_name);
|
|
|
|
if (it == children.end() || *it != node_name)
|
|
|
|
throw Poco::Exception("Assertion failed in LeaderElection");
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-29 22:32:04 +00:00
|
|
|
if (it == children.begin())
|
2017-12-21 18:17:06 +00:00
|
|
|
{
|
2017-12-29 22:32:04 +00:00
|
|
|
ProfileEvents::increment(ProfileEvents::LeaderElectionAcquiredLeadership);
|
|
|
|
handler();
|
|
|
|
return;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2018-03-26 19:46:14 +00:00
|
|
|
if (!zookeeper.existsWatch(path + "/" + *(it - 1), nullptr, task_handle->getWatchCallback()))
|
2017-12-29 22:32:04 +00:00
|
|
|
task_handle->schedule();
|
|
|
|
|
|
|
|
success = true;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2018-04-24 17:11:59 +00:00
|
|
|
catch (const KeeperException & e)
|
|
|
|
{
|
|
|
|
DB::tryLogCurrentException("LeaderElection");
|
|
|
|
|
|
|
|
if (e.code == ZooKeeperImpl::ZooKeeper::ZSESSIONEXPIRED)
|
|
|
|
return;
|
|
|
|
}
|
2017-12-29 22:32:04 +00:00
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
DB::tryLogCurrentException("LeaderElection");
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2017-12-29 22:32:04 +00:00
|
|
|
|
|
|
|
if (!success)
|
|
|
|
task_handle->scheduleAfter(10 * 1000);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2014-04-04 10:37:33 +00:00
|
|
|
};
|
|
|
|
|
2016-05-28 17:31:50 +00:00
|
|
|
using LeaderElectionPtr = std::shared_ptr<LeaderElection>;
|
2014-04-04 10:37:33 +00:00
|
|
|
|
|
|
|
}
|