2014-10-17 01:05:51 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Poco/Event.h>
|
2018-08-20 15:34:37 +00:00
|
|
|
#include <Core/BackgroundSchedulePool.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/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:
|
2022-03-13 12:23:51 +00:00
|
|
|
explicit ReplicatedMergeTreeRestartingThread(StorageReplicatedMergeTree & storage_);
|
2018-08-21 14:03:06 +00:00
|
|
|
|
2023-05-19 15:06:02 +00:00
|
|
|
void start(bool schedule = true)
|
|
|
|
{
|
|
|
|
if (schedule)
|
|
|
|
task->activateAndSchedule();
|
|
|
|
else
|
|
|
|
task->activate();
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-07-30 18:30:33 +00:00
|
|
|
void wakeup() { task->schedule(); }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-12-29 17:52:20 +00:00
|
|
|
void shutdown(bool part_of_full_shutdown);
|
2018-08-21 14:03:06 +00:00
|
|
|
|
2023-05-19 12:48:19 +00:00
|
|
|
void run();
|
2014-10-17 01:05:51 +00:00
|
|
|
private:
|
|
|
|
StorageReplicatedMergeTree & storage;
|
2018-05-31 13:05:05 +00:00
|
|
|
String log_name;
|
2020-05-30 21:57:37 +00:00
|
|
|
Poco::Logger * log;
|
2016-07-31 03:53:16 +00:00
|
|
|
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`.
|
2014-10-17 01:05:51 +00:00
|
|
|
String active_node_identifier;
|
|
|
|
|
2018-05-31 13:05:05 +00:00
|
|
|
BackgroundSchedulePool::TaskHolder task;
|
2017-12-29 22:32:04 +00:00
|
|
|
Int64 check_period_ms; /// The frequency of checking expiration of session in ZK.
|
2022-10-25 11:26:29 +00:00
|
|
|
UInt32 consecutive_check_failures = 0; /// How many consecutive checks have failed
|
2017-12-29 22:32:04 +00:00
|
|
|
bool first_time = true; /// Activate replica for the first time.
|
2014-10-17 21:44:56 +00:00
|
|
|
|
2022-02-03 10:10:05 +00:00
|
|
|
/// Restarts table if needed, returns false if it failed to restart replica.
|
|
|
|
bool runImpl();
|
|
|
|
|
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.
|
2014-10-17 01:05:51 +00:00
|
|
|
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).
|
2015-09-20 11:02:59 +00:00
|
|
|
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.
|
2015-09-20 11:02:59 +00:00
|
|
|
void updateQuorumIfWeHavePart();
|
|
|
|
|
2022-02-03 10:10:05 +00:00
|
|
|
void partialShutdown(bool part_of_full_shutdown = false);
|
2021-05-14 08:32:41 +00:00
|
|
|
|
|
|
|
/// Set readonly mode for table
|
2022-02-03 10:10:05 +00:00
|
|
|
void setReadonly(bool on_shutdown = false);
|
|
|
|
|
|
|
|
/// Disable readonly mode for table
|
|
|
|
void setNotReadonly();
|
2014-10-17 01:05:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|