2020-10-30 14:16:47 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Common/ThreadPool.h>
|
|
|
|
#include <Common/ZooKeeper/IKeeper.h>
|
|
|
|
#include <Common/ConcurrentBoundedQueue.h>
|
|
|
|
#include <Common/ZooKeeper/ZooKeeperCommon.h>
|
2021-02-03 20:32:15 +00:00
|
|
|
#include <Coordination/SessionExpiryQueue.h>
|
2020-11-19 16:06:19 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
2021-01-19 15:51:52 +00:00
|
|
|
#include <vector>
|
2020-10-30 14:16:47 +00:00
|
|
|
|
2021-01-21 15:09:48 +00:00
|
|
|
namespace DB
|
2020-10-30 14:16:47 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
using namespace DB;
|
2021-02-01 14:14:59 +00:00
|
|
|
struct NuKeeperStorageRequest;
|
|
|
|
using NuKeeperStorageRequestPtr = std::shared_ptr<NuKeeperStorageRequest>;
|
2020-11-10 13:43:10 +00:00
|
|
|
using ResponseCallback = std::function<void(const Coordination::ZooKeeperResponsePtr &)>;
|
2021-02-19 07:25:55 +00:00
|
|
|
using ChildrenSet = std::unordered_set<std::string>;
|
2020-10-30 14:16:47 +00:00
|
|
|
|
2021-02-01 14:14:59 +00:00
|
|
|
class NuKeeperStorage
|
2020-10-30 14:16:47 +00:00
|
|
|
{
|
2020-10-30 19:57:30 +00:00
|
|
|
public:
|
2021-01-21 13:53:10 +00:00
|
|
|
int64_t session_id_counter{0};
|
2020-10-30 14:16:47 +00:00
|
|
|
|
|
|
|
struct Node
|
|
|
|
{
|
|
|
|
String data;
|
2021-01-26 11:10:44 +00:00
|
|
|
Coordination::ACLs acls{};
|
2020-10-30 14:16:47 +00:00
|
|
|
bool is_ephemeral = false;
|
|
|
|
bool is_sequental = false;
|
|
|
|
Coordination::Stat stat{};
|
|
|
|
int32_t seq_num = 0;
|
2021-02-19 07:25:55 +00:00
|
|
|
ChildrenSet children;
|
2020-10-30 14:16:47 +00:00
|
|
|
};
|
|
|
|
|
2021-01-19 14:22:28 +00:00
|
|
|
struct ResponseForSession
|
|
|
|
{
|
|
|
|
int64_t session_id;
|
|
|
|
Coordination::ZooKeeperResponsePtr response;
|
|
|
|
};
|
|
|
|
|
2021-01-19 14:45:45 +00:00
|
|
|
using ResponsesForSessions = std::vector<ResponseForSession>;
|
2021-01-19 14:22:28 +00:00
|
|
|
|
|
|
|
struct RequestForSession
|
2020-11-26 14:57:32 +00:00
|
|
|
{
|
|
|
|
int64_t session_id;
|
2021-01-19 14:22:28 +00:00
|
|
|
Coordination::ZooKeeperRequestPtr request;
|
2020-11-26 14:57:32 +00:00
|
|
|
};
|
|
|
|
|
2021-01-19 14:45:45 +00:00
|
|
|
using RequestsForSessions = std::vector<RequestForSession>;
|
2021-01-19 14:22:28 +00:00
|
|
|
|
2021-02-19 07:05:52 +00:00
|
|
|
using Container = std::unordered_map<std::string, Node>;
|
|
|
|
using Ephemerals = std::unordered_map<int64_t, std::unordered_set<std::string>>;
|
|
|
|
using SessionAndWatcher = std::unordered_map<int64_t, std::unordered_set<std::string>>;
|
2021-02-03 20:32:15 +00:00
|
|
|
using SessionAndTimeout = std::unordered_map<int64_t, long>;
|
2021-01-19 14:22:28 +00:00
|
|
|
using SessionIDs = std::vector<int64_t>;
|
2020-10-30 14:16:47 +00:00
|
|
|
|
2021-01-19 14:22:28 +00:00
|
|
|
using Watches = std::map<String /* path, relative of root_path */, SessionIDs>;
|
2020-10-30 14:16:47 +00:00
|
|
|
|
|
|
|
Container container;
|
2020-11-19 16:06:19 +00:00
|
|
|
Ephemerals ephemerals;
|
2020-11-26 14:57:32 +00:00
|
|
|
SessionAndWatcher sessions_and_watchers;
|
2021-02-03 20:32:15 +00:00
|
|
|
SessionExpiryQueue session_expiry_queue;
|
|
|
|
SessionAndTimeout session_and_timeout;
|
2020-10-30 14:16:47 +00:00
|
|
|
|
2021-01-21 13:53:10 +00:00
|
|
|
int64_t zxid{0};
|
|
|
|
bool finalized{false};
|
2020-10-30 14:16:47 +00:00
|
|
|
|
|
|
|
Watches watches;
|
|
|
|
Watches list_watches; /// Watches for 'list' request (watches on children).
|
|
|
|
|
2021-01-19 14:22:28 +00:00
|
|
|
void clearDeadWatches(int64_t session_id);
|
2020-10-30 14:16:47 +00:00
|
|
|
|
2021-01-19 14:22:28 +00:00
|
|
|
int64_t getZXID()
|
2020-10-30 14:16:47 +00:00
|
|
|
{
|
2021-01-21 13:53:10 +00:00
|
|
|
return zxid++;
|
2021-01-19 14:22:28 +00:00
|
|
|
}
|
2020-10-30 14:16:47 +00:00
|
|
|
|
|
|
|
public:
|
2021-02-04 08:28:11 +00:00
|
|
|
NuKeeperStorage(int64_t tick_time_ms);
|
2021-01-19 14:22:28 +00:00
|
|
|
|
2021-02-04 08:28:11 +00:00
|
|
|
int64_t getSessionID(int64_t session_timeout_ms)
|
2021-01-21 14:34:34 +00:00
|
|
|
{
|
2021-02-03 20:32:15 +00:00
|
|
|
auto result = session_id_counter++;
|
|
|
|
session_and_timeout.emplace(result, session_timeout_ms);
|
|
|
|
session_expiry_queue.update(result, session_timeout_ms);
|
|
|
|
return result;
|
2021-01-21 14:34:34 +00:00
|
|
|
}
|
2021-01-21 20:01:25 +00:00
|
|
|
|
2021-01-19 14:22:28 +00:00
|
|
|
ResponsesForSessions processRequest(const Coordination::ZooKeeperRequestPtr & request, int64_t session_id);
|
2021-02-04 12:07:41 +00:00
|
|
|
|
|
|
|
void finalize();
|
2021-02-03 20:32:15 +00:00
|
|
|
|
|
|
|
std::unordered_set<int64_t> getDeadSessions()
|
|
|
|
{
|
|
|
|
return session_expiry_queue.getExpiredSessions();
|
|
|
|
}
|
2020-10-30 14:16:47 +00:00
|
|
|
};
|
2020-11-11 13:55:28 +00:00
|
|
|
|
2020-10-30 14:16:47 +00:00
|
|
|
}
|