2014-10-17 01:05:51 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Poco/Event.h>
|
2015-09-29 19:19:54 +00:00
|
|
|
#include <common/logger_useful.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Core/Types.h>
|
2014-10-17 01:05:51 +00:00
|
|
|
#include <thread>
|
2015-11-05 19:44:19 +00:00
|
|
|
#include <atomic>
|
2014-10-17 01:05:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
class StorageReplicatedMergeTree;
|
|
|
|
|
|
|
|
|
2017-04-16 15:00:33 +00:00
|
|
|
/** Initializes ZK session.
|
2017-05-09 19:07:35 +00:00
|
|
|
* Exposes ephemeral nodes. It sets the node values that are required for replica detection.
|
2017-04-16 15:00:33 +00:00
|
|
|
* Starts participation in the leader selection. Starts all background threads.
|
|
|
|
* Then monitors whether the session has expired. And if it expired, it will reinitialize it.
|
2014-10-17 01:05:51 +00:00
|
|
|
*/
|
|
|
|
class ReplicatedMergeTreeRestartingThread
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
ReplicatedMergeTreeRestartingThread(StorageReplicatedMergeTree & storage_);
|
2017-12-21 18:17:06 +00:00
|
|
|
|
|
|
|
~ReplicatedMergeTreeRestartingThread()
|
|
|
|
{
|
|
|
|
if (thread.joinable())
|
|
|
|
thread.join();
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
void wakeup()
|
|
|
|
{
|
|
|
|
wakeup_event.set();
|
|
|
|
}
|
|
|
|
|
|
|
|
Poco::Event & getWakeupEvent()
|
|
|
|
{
|
|
|
|
return wakeup_event;
|
|
|
|
}
|
|
|
|
|
|
|
|
void stop()
|
|
|
|
{
|
|
|
|
need_stop = true;
|
2017-12-21 18:17:06 +00:00
|
|
|
wakeup();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2014-10-17 01:05:51 +00:00
|
|
|
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
StorageReplicatedMergeTree & storage;
|
|
|
|
Logger * log;
|
|
|
|
Poco::Event wakeup_event;
|
|
|
|
std::atomic<bool> need_stop {false};
|
2014-10-17 01:05:51 +00:00
|
|
|
|
2017-04-16 15:00:33 +00:00
|
|
|
/// The random data we wrote into `/replicas/me/is_active`.
|
2017-04-01 07:20:54 +00:00
|
|
|
String active_node_identifier;
|
2014-10-17 01:05:51 +00:00
|
|
|
|
2017-12-21 18:17:06 +00:00
|
|
|
std::thread thread;
|
2014-10-17 21:44:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void run();
|
2014-10-17 01:05:51 +00:00
|
|
|
|
2017-04-16 15:00:33 +00:00
|
|
|
/// Start or stop background threads. Used for partial reinitialization when re-creating a session in ZooKeeper.
|
|
|
|
bool tryStartup(); /// Returns false if ZooKeeper is not available.
|
2014-10-17 01:05:51 +00:00
|
|
|
|
2017-04-16 15:00:33 +00:00
|
|
|
/// Note in ZooKeeper that this replica is currently active.
|
2017-04-01 07:20:54 +00:00
|
|
|
void activateReplica();
|
2014-10-17 01:05:51 +00:00
|
|
|
|
2017-04-16 15:00:33 +00:00
|
|
|
/// Delete the parts for which the quorum has failed (for the time when the replica was inactive).
|
2017-04-01 07:20:54 +00:00
|
|
|
void removeFailedQuorumParts();
|
2015-09-20 11:02:59 +00:00
|
|
|
|
2017-04-16 15:00:33 +00:00
|
|
|
/// If there is an unreachable quorum, and we have a part, then add this replica to the quorum.
|
2017-04-01 07:20:54 +00:00
|
|
|
void updateQuorumIfWeHavePart();
|
2015-09-20 11:02:59 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void partialShutdown();
|
2014-10-17 01:05:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|