2021-01-19 14:22:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-02-01 13:18:17 +00:00
|
|
|
#if !defined(ARCADIA_BUILD)
|
|
|
|
# include <Common/config.h>
|
|
|
|
# include "config_core.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if USE_NURAFT
|
|
|
|
|
2021-01-19 14:22:28 +00:00
|
|
|
#include <Common/ThreadPool.h>
|
|
|
|
#include <Common/ConcurrentBoundedQueue.h>
|
2021-01-22 16:04:57 +00:00
|
|
|
#include <Poco/Util/AbstractConfiguration.h>
|
2021-02-01 13:18:17 +00:00
|
|
|
#include <Common/Exception.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/logger_useful.h>
|
2021-02-01 13:18:17 +00:00
|
|
|
#include <functional>
|
2021-03-29 08:24:56 +00:00
|
|
|
#include <Coordination/KeeperServer.h>
|
2021-02-09 14:47:18 +00:00
|
|
|
#include <Coordination/CoordinationSettings.h>
|
2021-10-27 12:26:42 +00:00
|
|
|
#include <Coordination/KeeperInfos.h>
|
2021-01-19 14:22:28 +00:00
|
|
|
|
2021-01-21 15:09:48 +00:00
|
|
|
namespace DB
|
2021-01-19 14:22:28 +00:00
|
|
|
{
|
|
|
|
using ZooKeeperResponseCallback = std::function<void(const Coordination::ZooKeeperResponsePtr & response)>;
|
|
|
|
|
2021-08-24 12:30:31 +00:00
|
|
|
/// Highlevel wrapper for ClickHouse Keeper.
|
|
|
|
/// Process user requests via consensus and return responses.
|
2021-10-27 12:26:42 +00:00
|
|
|
class KeeperDispatcher : public IKeeperInfo
|
2021-01-19 14:22:28 +00:00
|
|
|
{
|
2021-02-01 13:18:17 +00:00
|
|
|
|
2021-10-27 12:26:42 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
/// Request statistics
|
|
|
|
class KeeperStats
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit KeeperStats() = default;
|
|
|
|
|
|
|
|
UInt64 getMinLatency() const;
|
|
|
|
UInt64 getMaxLatency() const;
|
|
|
|
|
|
|
|
UInt64 getAvgLatency() const;
|
|
|
|
|
|
|
|
UInt64 getPacketsReceived() const;
|
|
|
|
UInt64 getPacketsSent() const;
|
|
|
|
|
|
|
|
void incrementPacketsReceived();
|
|
|
|
void incrementPacketsSent();
|
|
|
|
|
|
|
|
void updateLatency(UInt64 latency_ms);
|
|
|
|
void reset();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void inline resetLatency();
|
|
|
|
void inline resetRequestCounters();
|
|
|
|
|
|
|
|
mutable std::shared_mutex mutex;
|
|
|
|
|
|
|
|
/// all response with watch response excluded
|
|
|
|
UInt64 packets_sent = 0;
|
|
|
|
/// All client request include ordinary requests, heart beat and session establish etc.
|
|
|
|
UInt64 packets_received = 0;
|
|
|
|
|
|
|
|
/// For consistent with zookeeper measured by millisecond,
|
|
|
|
/// otherwise maybe microsecond is better
|
|
|
|
UInt64 total_latency = 0;
|
|
|
|
UInt64 max_latency = 0;
|
|
|
|
UInt64 min_latency = 0;
|
|
|
|
|
|
|
|
UInt64 count = 0;
|
|
|
|
};
|
|
|
|
|
2021-01-19 14:22:28 +00:00
|
|
|
private:
|
2021-10-27 12:26:42 +00:00
|
|
|
mutable std::mutex push_request_mutex;
|
2021-01-19 14:22:28 +00:00
|
|
|
|
2021-03-29 08:24:56 +00:00
|
|
|
using RequestsQueue = ConcurrentBoundedQueue<KeeperStorage::RequestForSession>;
|
2021-03-05 10:40:24 +00:00
|
|
|
using SessionToResponseCallback = std::unordered_map<int64_t, ZooKeeperResponseCallback>;
|
2021-10-19 12:00:26 +00:00
|
|
|
using UpdateConfigurationQueue = ConcurrentBoundedQueue<ConfigUpdateAction>;
|
2021-03-05 10:40:24 +00:00
|
|
|
|
2021-04-16 13:50:09 +00:00
|
|
|
/// Size depends on coordination settings
|
|
|
|
std::unique_ptr<RequestsQueue> requests_queue;
|
2021-02-08 13:06:55 +00:00
|
|
|
ResponsesQueue responses_queue;
|
2021-03-05 10:40:24 +00:00
|
|
|
SnapshotsQueue snapshots_queue{1};
|
2021-10-19 13:11:29 +00:00
|
|
|
|
|
|
|
/// More than 1k updates is definitely misconfiguration.
|
2021-10-19 12:00:26 +00:00
|
|
|
UpdateConfigurationQueue update_configuration_queue{1000};
|
2021-03-05 10:40:24 +00:00
|
|
|
|
2021-01-26 14:08:31 +00:00
|
|
|
std::atomic<bool> shutdown_called{false};
|
2021-01-19 14:22:28 +00:00
|
|
|
|
2021-10-27 12:26:42 +00:00
|
|
|
mutable std::mutex session_to_response_callback_mutex;
|
2021-04-16 13:50:09 +00:00
|
|
|
/// These two maps looks similar, but serves different purposes.
|
|
|
|
/// The first map is subscription map for normal responses like
|
|
|
|
/// (get, set, list, etc.). Dispatcher determines callback for each response
|
|
|
|
/// using session id from this map.
|
2021-01-19 14:22:28 +00:00
|
|
|
SessionToResponseCallback session_to_response_callback;
|
2021-08-24 12:30:31 +00:00
|
|
|
|
2021-04-16 13:50:09 +00:00
|
|
|
/// But when client connects to the server for the first time it doesn't
|
|
|
|
/// have session_id. It request it from server. We give temporary
|
|
|
|
/// internal id for such requests just to much client with its response.
|
|
|
|
SessionToResponseCallback new_session_id_response_callback;
|
2021-01-19 14:22:28 +00:00
|
|
|
|
2021-04-16 13:50:09 +00:00
|
|
|
/// Reading and batching new requests from client handlers
|
2021-02-08 13:06:55 +00:00
|
|
|
ThreadFromGlobalPool request_thread;
|
2021-04-16 13:50:09 +00:00
|
|
|
/// Pushing responses to clients client handlers
|
|
|
|
/// using session_id.
|
2021-02-08 13:06:55 +00:00
|
|
|
ThreadFromGlobalPool responses_thread;
|
2021-04-16 13:50:09 +00:00
|
|
|
/// Cleaning old dead sessions
|
2021-02-03 20:32:15 +00:00
|
|
|
ThreadFromGlobalPool session_cleaner_thread;
|
2021-04-16 13:50:09 +00:00
|
|
|
/// Dumping new snapshots to disk
|
2021-03-05 10:40:24 +00:00
|
|
|
ThreadFromGlobalPool snapshot_thread;
|
2021-10-19 13:11:29 +00:00
|
|
|
/// Apply or wait for configuration changes
|
2021-10-19 12:00:26 +00:00
|
|
|
ThreadFromGlobalPool update_configuration_thread;
|
|
|
|
|
2021-08-24 12:30:31 +00:00
|
|
|
/// RAFT wrapper.
|
2021-03-29 08:24:56 +00:00
|
|
|
std::unique_ptr<KeeperServer> server;
|
2021-02-01 11:27:26 +00:00
|
|
|
|
2021-10-27 12:26:42 +00:00
|
|
|
std::shared_ptr<KeeperStats> keeper_stats;
|
|
|
|
|
|
|
|
KeeperSettingsPtr settings;
|
|
|
|
|
2021-02-01 11:27:26 +00:00
|
|
|
Poco::Logger * log;
|
2021-01-19 14:22:28 +00:00
|
|
|
|
2021-04-16 13:50:09 +00:00
|
|
|
/// Counter for new session_id requests.
|
|
|
|
std::atomic<int64_t> internal_session_id_counter{0};
|
|
|
|
|
2021-01-19 14:22:28 +00:00
|
|
|
private:
|
2021-08-24 12:30:31 +00:00
|
|
|
/// Thread put requests to raft
|
2021-02-08 13:06:55 +00:00
|
|
|
void requestThread();
|
2021-08-24 12:30:31 +00:00
|
|
|
/// Thread put responses for subscribed sessions
|
2021-02-08 13:06:55 +00:00
|
|
|
void responseThread();
|
2021-08-24 12:30:31 +00:00
|
|
|
/// Thread clean disconnected sessions from memory
|
2021-02-03 20:32:15 +00:00
|
|
|
void sessionCleanerTask();
|
2021-08-24 12:30:31 +00:00
|
|
|
/// Thread create snapshots in the background
|
2021-03-05 10:40:24 +00:00
|
|
|
void snapshotThread();
|
2021-10-19 13:11:29 +00:00
|
|
|
/// Thread apply or wait configuration changes from leader
|
2021-10-19 12:00:26 +00:00
|
|
|
void updateConfigurationThread();
|
|
|
|
|
2021-01-19 14:22:28 +00:00
|
|
|
void setResponse(int64_t session_id, const Coordination::ZooKeeperResponsePtr & response);
|
|
|
|
|
2021-04-16 13:50:09 +00:00
|
|
|
/// Add error responses for requests to responses queue.
|
|
|
|
/// Clears requests.
|
2021-04-17 14:06:49 +00:00
|
|
|
void addErrorResponses(const KeeperStorage::RequestsForSessions & requests_for_sessions, Coordination::Error error);
|
2021-04-16 13:50:09 +00:00
|
|
|
|
|
|
|
/// Forcefully wait for result and sets errors if something when wrong.
|
|
|
|
/// Clears both arguments
|
2021-04-17 14:06:49 +00:00
|
|
|
void forceWaitAndProcessResult(RaftAppendResult & result, KeeperStorage::RequestsForSessions & requests_for_sessions);
|
2021-04-16 13:50:09 +00:00
|
|
|
|
2021-01-19 14:22:28 +00:00
|
|
|
public:
|
2021-08-24 12:30:31 +00:00
|
|
|
/// Just allocate some objects, real initialization is done by `intialize method`
|
|
|
|
KeeperDispatcher();
|
|
|
|
|
|
|
|
/// Call shutdown
|
2021-10-27 12:26:42 +00:00
|
|
|
~KeeperDispatcher() override;
|
2021-01-25 12:29:12 +00:00
|
|
|
|
2021-08-24 12:30:31 +00:00
|
|
|
/// Initialization from config.
|
|
|
|
/// standalone_keeper -- we are standalone keeper application (not inside clickhouse server)
|
2021-10-14 10:21:41 +00:00
|
|
|
void initialize(const Poco::Util::AbstractConfiguration & config, bool standalone_keeper, bool start_async);
|
|
|
|
|
|
|
|
bool checkInit() const
|
|
|
|
{
|
|
|
|
return server && server->checkInit();
|
|
|
|
}
|
2021-01-22 16:04:57 +00:00
|
|
|
|
2021-10-19 13:49:36 +00:00
|
|
|
/// Registered in ConfigReloader callback. Add new configuration changes to
|
2021-10-19 13:11:29 +00:00
|
|
|
/// update_configuration_queue. Keeper Dispatcher apply them asynchronously.
|
2021-10-18 15:27:51 +00:00
|
|
|
void updateConfiguration(const Poco::Util::AbstractConfiguration & config);
|
|
|
|
|
2021-08-24 12:30:31 +00:00
|
|
|
/// Shutdown internal keeper parts (server, state machine, log storage, etc)
|
2021-01-26 14:08:31 +00:00
|
|
|
void shutdown();
|
|
|
|
|
2021-08-24 12:30:31 +00:00
|
|
|
/// Put request to ClickHouse Keeper
|
2021-01-25 12:29:12 +00:00
|
|
|
bool putRequest(const Coordination::ZooKeeperRequestPtr & request, int64_t session_id);
|
2021-01-21 13:53:10 +00:00
|
|
|
|
2021-10-27 12:26:42 +00:00
|
|
|
/// Get new session ID
|
|
|
|
int64_t getSessionID(int64_t session_timeout_ms);
|
|
|
|
|
|
|
|
/// Register session and subscribe for responses with callback
|
|
|
|
void registerSession(int64_t session_id, ZooKeeperResponseCallback callback);
|
|
|
|
|
|
|
|
/// Call if we don't need any responses for this session no more (session was expired)
|
|
|
|
void finishSession(int64_t session_id);
|
|
|
|
|
|
|
|
/// Invoked when a request completes.
|
|
|
|
void updateKeeperStat(UInt64 process_time_ms);
|
|
|
|
|
2021-08-24 12:30:31 +00:00
|
|
|
/// Are we leader
|
2021-10-27 12:26:42 +00:00
|
|
|
bool isLeader() const override
|
2021-01-27 17:54:25 +00:00
|
|
|
{
|
|
|
|
return server->isLeader();
|
|
|
|
}
|
|
|
|
|
2021-10-27 12:26:42 +00:00
|
|
|
bool hasLeader() const override
|
2021-02-01 07:51:10 +00:00
|
|
|
{
|
|
|
|
return server->isLeaderAlive();
|
|
|
|
}
|
|
|
|
|
2021-10-27 12:26:42 +00:00
|
|
|
///
|
|
|
|
String getRole() const override;
|
2021-01-21 13:53:10 +00:00
|
|
|
|
2021-10-27 12:26:42 +00:00
|
|
|
UInt64 getOutstandingRequests() const override;
|
|
|
|
UInt64 getNumAliveConnections() const override;
|
2021-08-24 12:30:31 +00:00
|
|
|
|
2021-10-27 12:26:42 +00:00
|
|
|
/// Request statistics such as qps, latency etc.
|
|
|
|
std::shared_ptr<KeeperStats> getKeeperStats() const
|
|
|
|
{
|
|
|
|
return keeper_stats;
|
|
|
|
}
|
|
|
|
|
|
|
|
const IKeeperInfo & getKeeperInfo() const
|
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
IRaftInfo & getRaftInfo() const
|
|
|
|
{
|
|
|
|
return *server;
|
|
|
|
}
|
|
|
|
|
|
|
|
const IStateMachineInfo & getStateMachineInfo() const
|
|
|
|
{
|
|
|
|
return *server->getKeeperStateMachine();
|
|
|
|
}
|
|
|
|
|
|
|
|
void dumpConf(WriteBufferFromOwnString & buf) const;
|
2021-01-19 14:22:28 +00:00
|
|
|
};
|
|
|
|
|
2021-10-27 12:26:42 +00:00
|
|
|
using KeeperStatsPtr = std::shared_ptr<KeeperDispatcher::KeeperStats>;
|
2021-01-19 14:22:28 +00:00
|
|
|
}
|
2021-02-01 13:18:17 +00:00
|
|
|
|
|
|
|
#endif
|