ClickHouse/dbms/src/Storages/MergeTree/ReplicatedMergeTreeRestartingThread.h

78 lines
1.8 KiB
C++
Raw Normal View History

#pragma once
#include <Poco/Event.h>
2015-09-29 19:19:54 +00:00
#include <common/logger_useful.h>
#include <Core/Types.h>
#include <thread>
#include <atomic>
namespace DB
{
class StorageReplicatedMergeTree;
2017-04-16 15:00:33 +00:00
/** Initializes ZK session.
* 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.
*/
class ReplicatedMergeTreeRestartingThread
{
public:
ReplicatedMergeTreeRestartingThread(StorageReplicatedMergeTree & storage_);
~ReplicatedMergeTreeRestartingThread()
{
if (thread.joinable())
thread.join();
}
void wakeup()
{
wakeup_event.set();
}
Poco::Event & getWakeupEvent()
{
return wakeup_event;
}
void stop()
{
need_stop = true;
wakeup();
}
private:
StorageReplicatedMergeTree & storage;
Logger * log;
Poco::Event wakeup_event;
std::atomic<bool> need_stop {false};
2017-04-16 15:00:33 +00:00
/// The random data we wrote into `/replicas/me/is_active`.
String active_node_identifier;
std::thread thread;
2014-10-17 21:44:56 +00:00
void run();
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.
2017-04-16 15:00:33 +00:00
/// Note in ZooKeeper that this replica is currently active.
void activateReplica();
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).
void removeFailedQuorumParts();
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.
void updateQuorumIfWeHavePart();
void partialShutdown();
};
}