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-02-01 11:27:26 +00:00
|
|
|
#include <common/logger_useful.h>
|
2021-02-01 13:18:17 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <Coordination/NuKeeperServer.h>
|
2021-02-09 14:47:18 +00:00
|
|
|
#include <Coordination/CoordinationSettings.h>
|
2021-02-01 13:18:17 +00:00
|
|
|
|
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-02-01 14:14:59 +00:00
|
|
|
class NuKeeperStorageDispatcher
|
2021-01-19 14:22:28 +00:00
|
|
|
{
|
2021-02-01 13:18:17 +00:00
|
|
|
|
2021-01-19 14:22:28 +00:00
|
|
|
private:
|
|
|
|
std::mutex push_request_mutex;
|
|
|
|
|
2021-02-09 14:47:18 +00:00
|
|
|
CoordinationSettingsPtr coordination_settings;
|
2021-02-01 14:14:59 +00:00
|
|
|
using RequestsQueue = ConcurrentBoundedQueue<NuKeeperStorage::RequestForSession>;
|
2021-03-05 10:40:24 +00:00
|
|
|
using SessionToResponseCallback = std::unordered_map<int64_t, ZooKeeperResponseCallback>;
|
|
|
|
|
2021-01-19 14:22:28 +00:00
|
|
|
RequestsQueue requests_queue{1};
|
2021-02-08 13:06:55 +00:00
|
|
|
ResponsesQueue responses_queue;
|
2021-03-05 10:40:24 +00:00
|
|
|
SnapshotsQueue snapshots_queue{1};
|
|
|
|
|
2021-01-26 14:08:31 +00:00
|
|
|
std::atomic<bool> shutdown_called{false};
|
2021-01-19 14:22:28 +00:00
|
|
|
|
|
|
|
std::mutex session_to_response_callback_mutex;
|
|
|
|
SessionToResponseCallback session_to_response_callback;
|
|
|
|
|
2021-02-08 13:06:55 +00:00
|
|
|
ThreadFromGlobalPool request_thread;
|
|
|
|
ThreadFromGlobalPool responses_thread;
|
2021-02-03 20:32:15 +00:00
|
|
|
ThreadFromGlobalPool session_cleaner_thread;
|
2021-03-05 10:40:24 +00:00
|
|
|
ThreadFromGlobalPool snapshot_thread;
|
2021-02-03 20:32:15 +00:00
|
|
|
|
2021-01-25 12:29:12 +00:00
|
|
|
std::unique_ptr<NuKeeperServer> server;
|
2021-02-01 11:27:26 +00:00
|
|
|
|
|
|
|
Poco::Logger * log;
|
2021-01-19 14:22:28 +00:00
|
|
|
|
|
|
|
private:
|
2021-02-08 13:06:55 +00:00
|
|
|
void requestThread();
|
|
|
|
void responseThread();
|
2021-02-03 20:32:15 +00:00
|
|
|
void sessionCleanerTask();
|
2021-03-05 10:40:24 +00:00
|
|
|
void snapshotThread();
|
2021-01-19 14:22:28 +00:00
|
|
|
void setResponse(int64_t session_id, const Coordination::ZooKeeperResponsePtr & response);
|
|
|
|
|
|
|
|
public:
|
2021-02-01 14:14:59 +00:00
|
|
|
NuKeeperStorageDispatcher();
|
2021-01-25 12:29:12 +00:00
|
|
|
|
|
|
|
void initialize(const Poco::Util::AbstractConfiguration & config);
|
2021-01-22 16:04:57 +00:00
|
|
|
|
2021-01-26 14:08:31 +00:00
|
|
|
void shutdown();
|
|
|
|
|
2021-02-01 14:14:59 +00:00
|
|
|
~NuKeeperStorageDispatcher();
|
2021-01-19 14:22:28 +00:00
|
|
|
|
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-01-27 17:54:25 +00:00
|
|
|
bool isLeader() const
|
|
|
|
{
|
|
|
|
return server->isLeader();
|
|
|
|
}
|
|
|
|
|
2021-02-01 07:51:10 +00:00
|
|
|
bool hasLeader() const
|
|
|
|
{
|
|
|
|
return server->isLeaderAlive();
|
|
|
|
}
|
|
|
|
|
2021-02-03 20:32:15 +00:00
|
|
|
int64_t getSessionID(long session_timeout_ms)
|
2021-01-19 14:22:28 +00:00
|
|
|
{
|
2021-02-03 20:32:15 +00:00
|
|
|
return server->getSessionID(session_timeout_ms);
|
2021-01-19 14:22:28 +00:00
|
|
|
}
|
2021-01-21 13:53:10 +00:00
|
|
|
|
2021-01-19 14:22:28 +00:00
|
|
|
void registerSession(int64_t session_id, ZooKeeperResponseCallback callback);
|
2021-01-21 11:37:20 +00:00
|
|
|
/// Call if we don't need any responses for this session no more (session was expired)
|
|
|
|
void finishSession(int64_t session_id);
|
2021-01-19 14:22:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2021-02-01 13:18:17 +00:00
|
|
|
|
|
|
|
#endif
|