2023-04-26 04:54:28 +00:00
|
|
|
|
|
|
|
#include "Commands.h"
|
2023-06-21 03:41:48 +00:00
|
|
|
#include <queue>
|
2023-04-26 04:54:28 +00:00
|
|
|
#include "KeeperClient.h"
|
2024-03-18 15:58:57 +00:00
|
|
|
#include "Parsers/CommonParsers.h"
|
2023-04-26 04:54:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2024-05-15 17:17:53 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2024-05-27 10:22:03 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2024-05-15 17:17:53 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 04:54:28 +00:00
|
|
|
bool LSCommand::parse(IParser::Pos & pos, std::shared_ptr<ASTKeeperQuery> & node, Expected & expected) const
|
|
|
|
{
|
2023-09-02 03:34:47 +00:00
|
|
|
String path;
|
|
|
|
if (!parseKeeperPath(pos, expected, path))
|
2023-04-26 04:54:28 +00:00
|
|
|
return true;
|
|
|
|
|
2023-09-02 03:34:47 +00:00
|
|
|
node->args.push_back(std::move(path));
|
2023-04-26 04:54:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LSCommand::execute(const ASTKeeperQuery * query, KeeperClient * client) const
|
|
|
|
{
|
|
|
|
String path;
|
|
|
|
if (!query->args.empty())
|
|
|
|
path = client->getAbsolutePath(query->args[0].safeGet<String>());
|
|
|
|
else
|
|
|
|
path = client->cwd;
|
|
|
|
|
2023-06-17 03:24:10 +00:00
|
|
|
auto children = client->zookeeper->getChildren(path);
|
|
|
|
std::sort(children.begin(), children.end());
|
2023-07-28 06:09:57 +00:00
|
|
|
|
|
|
|
bool need_space = false;
|
2023-06-17 03:24:10 +00:00
|
|
|
for (const auto & child : children)
|
2023-07-28 06:09:57 +00:00
|
|
|
{
|
|
|
|
if (std::exchange(need_space, true))
|
|
|
|
std::cout << " ";
|
|
|
|
|
|
|
|
std::cout << child;
|
|
|
|
}
|
2023-06-17 03:24:10 +00:00
|
|
|
|
2023-04-26 04:54:28 +00:00
|
|
|
std::cout << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CDCommand::parse(IParser::Pos & pos, std::shared_ptr<ASTKeeperQuery> & node, Expected & expected) const
|
|
|
|
{
|
2023-09-02 03:34:47 +00:00
|
|
|
String path;
|
|
|
|
if (!parseKeeperPath(pos, expected, path))
|
2023-04-26 04:54:28 +00:00
|
|
|
return true;
|
|
|
|
|
2023-09-02 03:34:47 +00:00
|
|
|
node->args.push_back(std::move(path));
|
2023-04-26 04:54:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CDCommand::execute(const ASTKeeperQuery * query, KeeperClient * client) const
|
|
|
|
{
|
2023-04-26 05:05:51 +00:00
|
|
|
if (query->args.empty())
|
2023-04-26 04:54:28 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
auto new_path = client->getAbsolutePath(query->args[0].safeGet<String>());
|
|
|
|
if (!client->zookeeper->exists(new_path))
|
2024-05-17 08:23:32 +00:00
|
|
|
std::cerr << "Path " << new_path << " does not exist\n";
|
2023-04-26 04:54:28 +00:00
|
|
|
else
|
|
|
|
client->cwd = new_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SetCommand::parse(IParser::Pos & pos, std::shared_ptr<ASTKeeperQuery> & node, Expected & expected) const
|
|
|
|
{
|
2023-09-02 03:34:47 +00:00
|
|
|
String path;
|
|
|
|
if (!parseKeeperPath(pos, expected, path))
|
2023-04-26 04:54:28 +00:00
|
|
|
return false;
|
2023-09-02 03:34:47 +00:00
|
|
|
node->args.push_back(std::move(path));
|
2023-04-26 04:54:28 +00:00
|
|
|
|
2023-09-02 03:34:47 +00:00
|
|
|
String arg;
|
2023-04-26 04:54:28 +00:00
|
|
|
if (!parseKeeperArg(pos, expected, arg))
|
|
|
|
return false;
|
|
|
|
node->args.push_back(std::move(arg));
|
|
|
|
|
|
|
|
ASTPtr version;
|
|
|
|
if (ParserNumber{}.parse(pos, version, expected))
|
|
|
|
node->args.push_back(version->as<ASTLiteral &>().value);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetCommand::execute(const ASTKeeperQuery * query, KeeperClient * client) const
|
|
|
|
{
|
|
|
|
if (query->args.size() == 2)
|
|
|
|
client->zookeeper->set(client->getAbsolutePath(query->args[0].safeGet<String>()), query->args[1].safeGet<String>());
|
|
|
|
else
|
|
|
|
client->zookeeper->set(
|
|
|
|
client->getAbsolutePath(query->args[0].safeGet<String>()),
|
|
|
|
query->args[1].safeGet<String>(),
|
2024-08-10 13:01:55 +00:00
|
|
|
static_cast<Int32>(query->args[2].safeGet<Int32>()));
|
2023-04-26 04:54:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CreateCommand::parse(IParser::Pos & pos, std::shared_ptr<ASTKeeperQuery> & node, Expected & expected) const
|
|
|
|
{
|
2023-09-02 03:34:47 +00:00
|
|
|
String path;
|
|
|
|
if (!parseKeeperPath(pos, expected, path))
|
2023-04-26 04:54:28 +00:00
|
|
|
return false;
|
2023-09-02 03:34:47 +00:00
|
|
|
node->args.push_back(std::move(path));
|
2023-04-26 04:54:28 +00:00
|
|
|
|
2023-09-02 03:34:47 +00:00
|
|
|
String arg;
|
2023-04-26 04:54:28 +00:00
|
|
|
if (!parseKeeperArg(pos, expected, arg))
|
|
|
|
return false;
|
|
|
|
node->args.push_back(std::move(arg));
|
|
|
|
|
2023-04-27 00:10:32 +00:00
|
|
|
int mode = zkutil::CreateMode::Persistent;
|
|
|
|
|
2024-03-18 15:58:57 +00:00
|
|
|
if (ParserKeyword(Keyword::PERSISTENT).ignore(pos, expected))
|
2023-04-27 00:10:32 +00:00
|
|
|
mode = zkutil::CreateMode::Persistent;
|
2024-03-18 15:58:57 +00:00
|
|
|
else if (ParserKeyword(Keyword::EPHEMERAL).ignore(pos, expected))
|
2023-04-27 00:10:32 +00:00
|
|
|
mode = zkutil::CreateMode::Ephemeral;
|
2024-03-18 15:58:57 +00:00
|
|
|
else if (ParserKeyword(Keyword::EPHEMERAL_SEQUENTIAL).ignore(pos, expected))
|
2023-04-27 00:10:32 +00:00
|
|
|
mode = zkutil::CreateMode::EphemeralSequential;
|
2024-03-18 15:58:57 +00:00
|
|
|
else if (ParserKeyword(Keyword::PERSISTENT_SEQUENTIAL).ignore(pos, expected))
|
2023-04-27 00:10:32 +00:00
|
|
|
mode = zkutil::CreateMode::PersistentSequential;
|
|
|
|
|
2024-02-13 09:35:55 +00:00
|
|
|
node->args.push_back(std::move(mode));
|
2023-04-27 00:10:32 +00:00
|
|
|
|
2023-04-26 04:54:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreateCommand::execute(const ASTKeeperQuery * query, KeeperClient * client) const
|
|
|
|
{
|
|
|
|
client->zookeeper->create(
|
|
|
|
client->getAbsolutePath(query->args[0].safeGet<String>()),
|
|
|
|
query->args[1].safeGet<String>(),
|
2023-04-27 00:10:32 +00:00
|
|
|
static_cast<int>(query->args[2].safeGet<Int64>()));
|
2023-04-26 04:54:28 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 16:08:31 +00:00
|
|
|
bool TouchCommand::parse(IParser::Pos & pos, std::shared_ptr<ASTKeeperQuery> & node, Expected & expected) const
|
|
|
|
{
|
|
|
|
String arg;
|
|
|
|
if (!parseKeeperPath(pos, expected, arg))
|
|
|
|
return false;
|
|
|
|
node->args.push_back(std::move(arg));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TouchCommand::execute(const ASTKeeperQuery * query, KeeperClient * client) const
|
|
|
|
{
|
|
|
|
client->zookeeper->createIfNotExists(client->getAbsolutePath(query->args[0].safeGet<String>()), "");
|
|
|
|
}
|
|
|
|
|
2023-04-26 04:54:28 +00:00
|
|
|
bool GetCommand::parse(IParser::Pos & pos, std::shared_ptr<ASTKeeperQuery> & node, Expected & expected) const
|
|
|
|
{
|
2023-09-02 03:34:47 +00:00
|
|
|
String path;
|
|
|
|
if (!parseKeeperPath(pos, expected, path))
|
2023-04-26 04:54:28 +00:00
|
|
|
return false;
|
2023-09-02 03:34:47 +00:00
|
|
|
node->args.push_back(std::move(path));
|
2023-04-26 04:54:28 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetCommand::execute(const ASTKeeperQuery * query, KeeperClient * client) const
|
|
|
|
{
|
|
|
|
std::cout << client->zookeeper->get(client->getAbsolutePath(query->args[0].safeGet<String>())) << "\n";
|
|
|
|
}
|
|
|
|
|
2023-09-07 00:36:39 +00:00
|
|
|
bool ExistsCommand::parse(IParser::Pos & pos, std::shared_ptr<ASTKeeperQuery> & node, DB::Expected & expected) const
|
|
|
|
{
|
|
|
|
String path;
|
|
|
|
if (!parseKeeperPath(pos, expected, path))
|
|
|
|
return false;
|
|
|
|
node->args.push_back(std::move(path));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExistsCommand::execute(const DB::ASTKeeperQuery * query, DB::KeeperClient * client) const
|
|
|
|
{
|
|
|
|
std::cout << client->zookeeper->exists(client->getAbsolutePath(query->args[0].safeGet<String>())) << "\n";
|
|
|
|
}
|
|
|
|
|
2023-06-17 03:24:10 +00:00
|
|
|
bool GetStatCommand::parse(IParser::Pos & pos, std::shared_ptr<ASTKeeperQuery> & node, Expected & expected) const
|
|
|
|
{
|
2023-09-02 03:34:47 +00:00
|
|
|
String path;
|
|
|
|
if (!parseKeeperPath(pos, expected, path))
|
2023-06-17 03:24:10 +00:00
|
|
|
return true;
|
|
|
|
|
2023-09-02 03:34:47 +00:00
|
|
|
node->args.push_back(std::move(path));
|
2023-06-17 03:24:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetStatCommand::execute(const ASTKeeperQuery * query, KeeperClient * client) const
|
|
|
|
{
|
|
|
|
Coordination::Stat stat;
|
|
|
|
String path;
|
|
|
|
if (!query->args.empty())
|
|
|
|
path = client->getAbsolutePath(query->args[0].safeGet<String>());
|
|
|
|
else
|
|
|
|
path = client->cwd;
|
|
|
|
|
|
|
|
client->zookeeper->get(path, &stat);
|
|
|
|
|
|
|
|
std::cout << "cZxid = " << stat.czxid << "\n";
|
|
|
|
std::cout << "mZxid = " << stat.mzxid << "\n";
|
2023-07-21 22:12:26 +00:00
|
|
|
std::cout << "pZxid = " << stat.pzxid << "\n";
|
2023-06-17 03:24:10 +00:00
|
|
|
std::cout << "ctime = " << stat.ctime << "\n";
|
|
|
|
std::cout << "mtime = " << stat.mtime << "\n";
|
|
|
|
std::cout << "version = " << stat.version << "\n";
|
|
|
|
std::cout << "cversion = " << stat.cversion << "\n";
|
|
|
|
std::cout << "aversion = " << stat.aversion << "\n";
|
|
|
|
std::cout << "ephemeralOwner = " << stat.ephemeralOwner << "\n";
|
|
|
|
std::cout << "dataLength = " << stat.dataLength << "\n";
|
|
|
|
std::cout << "numChildren = " << stat.numChildren << "\n";
|
|
|
|
}
|
|
|
|
|
2024-05-30 11:36:33 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
/// Helper class for parallelized tree traversal
|
|
|
|
template <class UserCtx>
|
|
|
|
struct TraversalTask : public std::enable_shared_from_this<TraversalTask<UserCtx>>
|
|
|
|
{
|
|
|
|
using TraversalTaskPtr = std::shared_ptr<TraversalTask<UserCtx>>;
|
|
|
|
|
|
|
|
struct Ctx
|
|
|
|
{
|
|
|
|
std::deque<TraversalTaskPtr> new_tasks; /// Tasks for newly discovered children, that hasn't been started yet
|
|
|
|
std::deque<std::function<void(Ctx &)>> in_flight_list_requests; /// In-flight getChildren requests
|
|
|
|
std::deque<std::function<void(Ctx &)>> finish_callbacks; /// Callbacks to be called
|
|
|
|
KeeperClient * client;
|
|
|
|
UserCtx & user_ctx;
|
2024-05-30 12:40:27 +00:00
|
|
|
|
|
|
|
Ctx(KeeperClient * client_, UserCtx & user_ctx_) : client(client_), user_ctx(user_ctx_) {}
|
2024-05-30 11:36:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
const fs::path path;
|
|
|
|
const TraversalTaskPtr parent;
|
|
|
|
|
|
|
|
Int64 child_tasks = 0;
|
|
|
|
Int64 nodes_in_subtree = 1;
|
|
|
|
|
|
|
|
public:
|
|
|
|
TraversalTask(const fs::path & path_, TraversalTaskPtr parent_)
|
|
|
|
: path(path_)
|
|
|
|
, parent(parent_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Start traversing the subtree
|
|
|
|
void onStart(Ctx & ctx)
|
|
|
|
{
|
|
|
|
/// tryGetChildren doesn't throw if the node is not found (was deleted in the meantime)
|
|
|
|
std::shared_ptr<std::future<Coordination::ListResponse>> list_request =
|
|
|
|
std::make_shared<std::future<Coordination::ListResponse>>(ctx.client->zookeeper->asyncTryGetChildren(path));
|
|
|
|
ctx.in_flight_list_requests.push_back([task = this->shared_from_this(), list_request](Ctx & ctx_) mutable
|
|
|
|
{
|
|
|
|
task->onGetChildren(ctx_, list_request->get());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Called when getChildren request returns
|
|
|
|
void onGetChildren(Ctx & ctx, const Coordination::ListResponse & response)
|
|
|
|
{
|
|
|
|
const bool traverse_children = ctx.user_ctx.onListChildren(path, response.names);
|
|
|
|
|
|
|
|
if (traverse_children)
|
|
|
|
{
|
|
|
|
/// Schedule traversal of each child
|
2024-05-30 19:22:07 +00:00
|
|
|
for (const auto & child : response.names)
|
2024-05-30 11:36:33 +00:00
|
|
|
{
|
|
|
|
auto task = std::make_shared<TraversalTask>(path / child, this->shared_from_this());
|
|
|
|
ctx.new_tasks.push_back(task);
|
|
|
|
}
|
|
|
|
child_tasks = response.names.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (child_tasks == 0)
|
|
|
|
finish(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Called when a child subtree has been traversed
|
|
|
|
void onChildTraversalFinished(Ctx & ctx, Int64 child_nodes_in_subtree)
|
|
|
|
{
|
|
|
|
nodes_in_subtree += child_nodes_in_subtree;
|
|
|
|
|
|
|
|
--child_tasks;
|
|
|
|
|
|
|
|
/// Finish if all children have been traversed
|
|
|
|
if (child_tasks == 0)
|
|
|
|
finish(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// This node and all its children have been traversed
|
|
|
|
void finish(Ctx & ctx)
|
|
|
|
{
|
|
|
|
ctx.user_ctx.onFinishChildrenTraversal(path, nodes_in_subtree);
|
|
|
|
|
|
|
|
if (!parent)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/// Notify the parent that we have finished traversing the subtree
|
|
|
|
ctx.finish_callbacks.push_back([p = this->parent, child_nodes_in_subtree = this->nodes_in_subtree](Ctx & ctx_)
|
|
|
|
{
|
|
|
|
p->onChildTraversalFinished(ctx_, child_nodes_in_subtree);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Traverses the tree in parallel and calls user callbacks
|
|
|
|
/// Parallelization is achieved by sending multiple async getChildren requests to Keeper, but all processing is done in a single thread
|
|
|
|
template <class UserCtx>
|
|
|
|
void parallelized_traverse(const fs::path & path, KeeperClient * client, size_t max_in_flight_requests, UserCtx & ctx_)
|
|
|
|
{
|
2024-05-30 12:40:27 +00:00
|
|
|
typename TraversalTask<UserCtx>::Ctx ctx(client, ctx_);
|
2024-05-30 11:36:33 +00:00
|
|
|
|
|
|
|
auto root_task = std::make_shared<TraversalTask<UserCtx>>(path, nullptr);
|
|
|
|
|
|
|
|
ctx.new_tasks.push_back(root_task);
|
|
|
|
|
|
|
|
/// Until there is something to do
|
|
|
|
while (!ctx.new_tasks.empty() || !ctx.in_flight_list_requests.empty() || !ctx.finish_callbacks.empty())
|
|
|
|
{
|
|
|
|
/// First process all finish callbacks, they don't wait for anything and allow to free memory
|
|
|
|
while (!ctx.finish_callbacks.empty())
|
|
|
|
{
|
|
|
|
auto callback = std::move(ctx.finish_callbacks.front());
|
|
|
|
ctx.finish_callbacks.pop_front();
|
|
|
|
callback(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Make new requests if there are less than max in flight
|
|
|
|
while (!ctx.new_tasks.empty() && ctx.in_flight_list_requests.size() < max_in_flight_requests)
|
|
|
|
{
|
|
|
|
auto task = std::move(ctx.new_tasks.front());
|
|
|
|
ctx.new_tasks.pop_front();
|
|
|
|
task->onStart(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wait for first request in the queue to finish
|
|
|
|
if (!ctx.in_flight_list_requests.empty())
|
|
|
|
{
|
|
|
|
auto request = std::move(ctx.in_flight_list_requests.front());
|
|
|
|
ctx.in_flight_list_requests.pop_front();
|
|
|
|
request(ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} /// anonymous namespace
|
|
|
|
|
2023-07-26 05:57:16 +00:00
|
|
|
bool FindSuperNodes::parse(IParser::Pos & pos, std::shared_ptr<ASTKeeperQuery> & node, Expected & expected) const
|
2023-06-17 03:24:10 +00:00
|
|
|
{
|
|
|
|
ASTPtr threshold;
|
|
|
|
if (!ParserUnsignedInteger{}.parse(pos, threshold, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
node->args.push_back(threshold->as<ASTLiteral &>().value);
|
|
|
|
|
2024-05-16 08:36:04 +00:00
|
|
|
ParserToken{TokenType::Whitespace}.ignore(pos);
|
|
|
|
|
2023-06-17 03:24:10 +00:00
|
|
|
String path;
|
|
|
|
if (!parseKeeperPath(pos, expected, path))
|
|
|
|
path = ".";
|
|
|
|
|
|
|
|
node->args.push_back(std::move(path));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-07-26 05:57:16 +00:00
|
|
|
void FindSuperNodes::execute(const ASTKeeperQuery * query, KeeperClient * client) const
|
2023-06-17 03:24:10 +00:00
|
|
|
{
|
|
|
|
auto threshold = query->args[0].safeGet<UInt64>();
|
|
|
|
auto path = client->getAbsolutePath(query->args[1].safeGet<String>());
|
|
|
|
|
2024-05-30 11:36:33 +00:00
|
|
|
struct
|
|
|
|
{
|
2024-05-31 08:15:46 +00:00
|
|
|
bool onListChildren(const fs::path & path, const Strings & children) const
|
2024-05-30 11:36:33 +00:00
|
|
|
{
|
|
|
|
if (children.size() >= threshold)
|
|
|
|
std::cout << static_cast<String>(path) << "\t" << children.size() << "\n";
|
|
|
|
return true;
|
|
|
|
}
|
2023-06-17 03:24:10 +00:00
|
|
|
|
2024-05-31 08:15:46 +00:00
|
|
|
void onFinishChildrenTraversal(const fs::path &, Int64) const {}
|
2023-06-17 03:24:10 +00:00
|
|
|
|
2024-05-30 11:36:33 +00:00
|
|
|
size_t threshold;
|
|
|
|
} ctx {.threshold = threshold };
|
2024-05-15 15:28:47 +00:00
|
|
|
|
2024-05-30 13:11:35 +00:00
|
|
|
parallelized_traverse(path, client, /* max_in_flight_requests */ 50, ctx);
|
2023-06-17 03:24:10 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 16:08:31 +00:00
|
|
|
bool DeleteStaleBackups::parse(IParser::Pos & /* pos */, std::shared_ptr<ASTKeeperQuery> & /* node */, Expected & /* expected */) const
|
2023-06-21 03:41:48 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-08-07 16:08:31 +00:00
|
|
|
void DeleteStaleBackups::execute(const ASTKeeperQuery * /* query */, KeeperClient * client) const
|
2023-06-21 03:41:48 +00:00
|
|
|
{
|
|
|
|
client->askConfirmation(
|
|
|
|
"You are going to delete all inactive backups in /clickhouse/backups.",
|
|
|
|
[client]
|
|
|
|
{
|
2023-07-28 06:09:57 +00:00
|
|
|
fs::path backup_root = "/clickhouse/backups";
|
2023-06-21 03:41:48 +00:00
|
|
|
auto backups = client->zookeeper->getChildren(backup_root);
|
2023-07-28 14:48:22 +00:00
|
|
|
std::sort(backups.begin(), backups.end());
|
2023-06-21 03:41:48 +00:00
|
|
|
|
2023-07-26 05:57:16 +00:00
|
|
|
for (const auto & child : backups)
|
2023-06-21 03:41:48 +00:00
|
|
|
{
|
2023-07-28 06:09:57 +00:00
|
|
|
auto backup_path = backup_root / child;
|
2023-06-21 03:41:48 +00:00
|
|
|
std::cout << "Found backup " << backup_path << ", checking if it's active\n";
|
|
|
|
|
2023-07-28 06:09:57 +00:00
|
|
|
String stage_path = backup_path / "stage";
|
2023-06-21 03:41:48 +00:00
|
|
|
auto stages = client->zookeeper->getChildren(stage_path);
|
|
|
|
|
|
|
|
bool is_active = false;
|
2023-07-26 05:57:16 +00:00
|
|
|
for (const auto & stage : stages)
|
2023-06-21 03:41:48 +00:00
|
|
|
{
|
|
|
|
if (startsWith(stage, "alive"))
|
|
|
|
{
|
|
|
|
is_active = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_active)
|
|
|
|
{
|
|
|
|
std::cout << "Backup " << backup_path << " is active, not going to delete\n";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "Backup " << backup_path << " is not active, deleting it\n";
|
|
|
|
client->zookeeper->removeRecursive(backup_path);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FindBigFamily::parse(IParser::Pos & pos, std::shared_ptr<ASTKeeperQuery> & node, Expected & expected) const
|
|
|
|
{
|
|
|
|
String path;
|
|
|
|
if (!parseKeeperPath(pos, expected, path))
|
|
|
|
path = ".";
|
|
|
|
|
|
|
|
node->args.push_back(std::move(path));
|
|
|
|
|
|
|
|
ASTPtr count;
|
|
|
|
if (ParserUnsignedInteger{}.parse(pos, count, expected))
|
|
|
|
node->args.push_back(count->as<ASTLiteral &>().value);
|
|
|
|
else
|
|
|
|
node->args.push_back(UInt64(10));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindBigFamily::execute(const ASTKeeperQuery * query, KeeperClient * client) const
|
|
|
|
{
|
|
|
|
auto path = client->getAbsolutePath(query->args[0].safeGet<String>());
|
|
|
|
auto n = query->args[1].safeGet<UInt64>();
|
|
|
|
|
2024-05-30 11:36:33 +00:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
std::vector<std::tuple<Int64, String>> result;
|
|
|
|
|
2024-05-31 08:15:46 +00:00
|
|
|
bool onListChildren(const fs::path &, const Strings &) const { return true; }
|
2024-05-30 11:36:33 +00:00
|
|
|
|
|
|
|
void onFinishChildrenTraversal(const fs::path & path, Int64 nodes_in_subtree)
|
|
|
|
{
|
|
|
|
result.emplace_back(nodes_in_subtree, path.string());
|
|
|
|
}
|
|
|
|
} ctx;
|
2023-06-21 03:41:48 +00:00
|
|
|
|
2024-05-30 13:11:35 +00:00
|
|
|
parallelized_traverse(path, client, /* max_in_flight_requests */ 50, ctx);
|
2023-06-21 03:41:48 +00:00
|
|
|
|
2024-05-30 11:36:33 +00:00
|
|
|
std::sort(ctx.result.begin(), ctx.result.end(), std::greater());
|
|
|
|
for (UInt64 i = 0; i < std::min(ctx.result.size(), static_cast<size_t>(n)); ++i)
|
|
|
|
std::cout << std::get<1>(ctx.result[i]) << "\t" << std::get<0>(ctx.result[i]) << "\n";
|
2023-06-21 03:41:48 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 04:54:28 +00:00
|
|
|
bool RMCommand::parse(IParser::Pos & pos, std::shared_ptr<ASTKeeperQuery> & node, Expected & expected) const
|
|
|
|
{
|
2023-09-02 03:34:47 +00:00
|
|
|
String path;
|
|
|
|
if (!parseKeeperPath(pos, expected, path))
|
2023-04-26 04:54:28 +00:00
|
|
|
return false;
|
2023-09-02 03:34:47 +00:00
|
|
|
node->args.push_back(std::move(path));
|
2023-04-26 04:54:28 +00:00
|
|
|
|
2023-09-16 00:03:46 +00:00
|
|
|
ASTPtr version;
|
|
|
|
if (ParserNumber{}.parse(pos, version, expected))
|
|
|
|
node->args.push_back(version->as<ASTLiteral &>().value);
|
|
|
|
|
2023-04-26 04:54:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RMCommand::execute(const ASTKeeperQuery * query, KeeperClient * client) const
|
|
|
|
{
|
2023-09-16 00:03:46 +00:00
|
|
|
Int32 version{-1};
|
|
|
|
if (query->args.size() == 2)
|
2024-08-10 13:01:55 +00:00
|
|
|
version = static_cast<Int32>(query->args[1].safeGet<Int32>());
|
2023-09-16 00:03:46 +00:00
|
|
|
|
|
|
|
client->zookeeper->remove(client->getAbsolutePath(query->args[0].safeGet<String>()), version);
|
2023-04-26 04:54:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RMRCommand::parse(IParser::Pos & pos, std::shared_ptr<ASTKeeperQuery> & node, Expected & expected) const
|
|
|
|
{
|
2023-09-02 03:34:47 +00:00
|
|
|
String path;
|
|
|
|
if (!parseKeeperPath(pos, expected, path))
|
2023-04-26 04:54:28 +00:00
|
|
|
return false;
|
2023-09-02 03:34:47 +00:00
|
|
|
node->args.push_back(std::move(path));
|
2023-04-26 04:54:28 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RMRCommand::execute(const ASTKeeperQuery * query, KeeperClient * client) const
|
|
|
|
{
|
|
|
|
String path = client->getAbsolutePath(query->args[0].safeGet<String>());
|
2023-09-16 00:03:46 +00:00
|
|
|
client->askConfirmation(
|
|
|
|
"You are going to recursively delete path " + path, [client, path] { client->zookeeper->removeRecursive(path); });
|
2023-04-26 04:54:28 +00:00
|
|
|
}
|
|
|
|
|
2023-09-02 03:34:47 +00:00
|
|
|
bool ReconfigCommand::parse(IParser::Pos & pos, std::shared_ptr<ASTKeeperQuery> & node, DB::Expected & expected) const
|
|
|
|
{
|
2024-03-18 15:58:57 +00:00
|
|
|
ParserKeyword s_add(Keyword::ADD);
|
|
|
|
ParserKeyword s_remove(Keyword::REMOVE);
|
|
|
|
ParserKeyword s_set(Keyword::SET);
|
|
|
|
|
2023-09-02 03:34:47 +00:00
|
|
|
ReconfigCommand::Operation operation;
|
2024-03-18 15:58:57 +00:00
|
|
|
if (s_add.ignore(pos, expected))
|
2023-09-02 03:34:47 +00:00
|
|
|
operation = ReconfigCommand::Operation::ADD;
|
2024-03-18 15:58:57 +00:00
|
|
|
else if (s_remove.ignore(pos, expected))
|
2023-09-02 03:34:47 +00:00
|
|
|
operation = ReconfigCommand::Operation::REMOVE;
|
2024-03-18 15:58:57 +00:00
|
|
|
else if (s_set.ignore(pos, expected))
|
2023-09-02 03:34:47 +00:00
|
|
|
operation = ReconfigCommand::Operation::SET;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
node->args.push_back(operation);
|
|
|
|
ParserToken{TokenType::Whitespace}.ignore(pos);
|
|
|
|
|
|
|
|
String arg;
|
|
|
|
if (!parseKeeperArg(pos, expected, arg))
|
|
|
|
return false;
|
|
|
|
node->args.push_back(std::move(arg));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReconfigCommand::execute(const DB::ASTKeeperQuery * query, DB::KeeperClient * client) const
|
|
|
|
{
|
|
|
|
String joining;
|
|
|
|
String leaving;
|
|
|
|
String new_members;
|
|
|
|
|
2024-08-10 13:01:55 +00:00
|
|
|
auto operation = query->args[0].safeGet<ReconfigCommand::Operation>();
|
2023-09-08 02:48:04 +00:00
|
|
|
switch (operation)
|
|
|
|
{
|
|
|
|
case static_cast<UInt8>(ReconfigCommand::Operation::ADD):
|
2024-01-13 02:48:04 +00:00
|
|
|
joining = query->args[1].safeGet<String>();
|
2023-09-08 02:48:04 +00:00
|
|
|
break;
|
|
|
|
case static_cast<UInt8>(ReconfigCommand::Operation::REMOVE):
|
2024-01-13 02:48:04 +00:00
|
|
|
leaving = query->args[1].safeGet<String>();
|
2023-09-08 02:48:04 +00:00
|
|
|
break;
|
|
|
|
case static_cast<UInt8>(ReconfigCommand::Operation::SET):
|
2024-01-13 02:48:04 +00:00
|
|
|
new_members = query->args[1].safeGet<String>();
|
2023-09-08 02:48:04 +00:00
|
|
|
break;
|
|
|
|
default:
|
2024-05-27 10:22:03 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Unexpected operation: {}", operation);
|
2023-09-08 02:48:04 +00:00
|
|
|
}
|
2023-09-02 03:34:47 +00:00
|
|
|
|
|
|
|
auto response = client->zookeeper->reconfig(joining, leaving, new_members);
|
|
|
|
std::cout << response.value << '\n';
|
|
|
|
}
|
|
|
|
|
2023-09-07 00:36:39 +00:00
|
|
|
bool SyncCommand::parse(IParser::Pos & pos, std::shared_ptr<ASTKeeperQuery> & node, DB::Expected & expected) const
|
|
|
|
{
|
|
|
|
String path;
|
|
|
|
if (!parseKeeperPath(pos, expected, path))
|
|
|
|
return false;
|
|
|
|
node->args.push_back(std::move(path));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SyncCommand::execute(const DB::ASTKeeperQuery * query, DB::KeeperClient * client) const
|
|
|
|
{
|
|
|
|
std::cout << client->zookeeper->sync(client->getAbsolutePath(query->args[0].safeGet<String>())) << "\n";
|
|
|
|
}
|
|
|
|
|
2023-04-26 04:54:28 +00:00
|
|
|
bool HelpCommand::parse(IParser::Pos & /* pos */, std::shared_ptr<ASTKeeperQuery> & /* node */, Expected & /* expected */) const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HelpCommand::execute(const ASTKeeperQuery * /* query */, KeeperClient * /* client */) const
|
|
|
|
{
|
|
|
|
for (const auto & pair : KeeperClient::commands)
|
2023-06-21 03:41:48 +00:00
|
|
|
std::cout << pair.second->generateHelpString() << "\n";
|
2023-04-26 04:54:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FourLetterWordCommand::parse(IParser::Pos & pos, std::shared_ptr<ASTKeeperQuery> & node, Expected & expected) const
|
|
|
|
{
|
|
|
|
expected.add(pos, "four-letter-word command");
|
|
|
|
if (pos->type != TokenType::BareWord)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
String cmd(pos->begin, pos->end);
|
|
|
|
if (cmd.size() != 4)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
++pos;
|
|
|
|
node->args.push_back(std::move(cmd));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FourLetterWordCommand::execute(const ASTKeeperQuery * query, KeeperClient * client) const
|
|
|
|
{
|
|
|
|
std::cout << client->executeFourLetterCommand(query->args[0].safeGet<String>()) << "\n";
|
|
|
|
}
|
|
|
|
|
2023-10-24 15:02:54 +00:00
|
|
|
bool GetDirectChildrenNumberCommand::parse(IParser::Pos & pos, std::shared_ptr<ASTKeeperQuery> & node, Expected & expected) const
|
|
|
|
{
|
|
|
|
String path;
|
|
|
|
if (!parseKeeperPath(pos, expected, path))
|
|
|
|
path = ".";
|
|
|
|
|
|
|
|
node->args.push_back(std::move(path));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetDirectChildrenNumberCommand::execute(const ASTKeeperQuery * query, KeeperClient * client) const
|
|
|
|
{
|
|
|
|
auto path = client->getAbsolutePath(query->args[0].safeGet<String>());
|
|
|
|
|
|
|
|
Coordination::Stat stat;
|
|
|
|
client->zookeeper->get(path, &stat);
|
|
|
|
|
|
|
|
std::cout << stat.numChildren << "\n";
|
|
|
|
}
|
|
|
|
|
2023-10-11 02:28:28 +00:00
|
|
|
bool GetAllChildrenNumberCommand::parse(IParser::Pos & pos, std::shared_ptr<ASTKeeperQuery> & node, Expected & expected) const
|
|
|
|
{
|
|
|
|
String path;
|
|
|
|
if (!parseKeeperPath(pos, expected, path))
|
|
|
|
path = ".";
|
|
|
|
|
|
|
|
node->args.push_back(std::move(path));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetAllChildrenNumberCommand::execute(const ASTKeeperQuery * query, KeeperClient * client) const
|
|
|
|
{
|
|
|
|
auto path = client->getAbsolutePath(query->args[0].safeGet<String>());
|
|
|
|
|
|
|
|
std::queue<fs::path> queue;
|
|
|
|
queue.push(path);
|
|
|
|
Coordination::Stat stat;
|
|
|
|
client->zookeeper->get(path, &stat);
|
|
|
|
|
|
|
|
int totalNumChildren = stat.numChildren;
|
|
|
|
while (!queue.empty())
|
|
|
|
{
|
|
|
|
auto next_path = queue.front();
|
|
|
|
queue.pop();
|
|
|
|
|
|
|
|
auto children = client->zookeeper->getChildren(next_path);
|
2023-10-11 09:47:38 +00:00
|
|
|
for (auto & child : children)
|
|
|
|
child = next_path / child;
|
2023-10-11 02:28:28 +00:00
|
|
|
auto response = client->zookeeper->get(children);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < response.size(); ++i)
|
|
|
|
{
|
|
|
|
totalNumChildren += response[i].stat.numChildren;
|
|
|
|
queue.push(children[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << totalNumChildren << "\n";
|
|
|
|
}
|
|
|
|
|
2024-08-28 13:39:29 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
class CPMVOperation
|
|
|
|
{
|
2024-08-29 12:18:51 +00:00
|
|
|
constexpr static UInt64 kTryLimit = 1000;
|
|
|
|
|
2024-08-28 13:39:29 +00:00
|
|
|
public:
|
|
|
|
CPMVOperation(String src_, String dest_, bool remove_src_, KeeperClient * client_)
|
|
|
|
: src(std::move(src_)), dest(std::move(dest_)), remove_src(remove_src_), client(client_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-08-29 13:28:00 +00:00
|
|
|
bool isTryLimitReached() const { return failed_tries_count >= kTryLimit; }
|
2024-08-29 12:18:51 +00:00
|
|
|
|
2024-08-29 13:28:00 +00:00
|
|
|
bool isCompleted() const { return is_completed; }
|
2024-08-29 12:18:51 +00:00
|
|
|
|
|
|
|
void perform()
|
2024-08-28 13:39:29 +00:00
|
|
|
{
|
|
|
|
Coordination::Stat src_stat;
|
|
|
|
String data = client->zookeeper->get(src, &src_stat);
|
|
|
|
|
|
|
|
Coordination::Requests ops{
|
|
|
|
zkutil::makeCheckRequest(src, src_stat.version),
|
|
|
|
zkutil::makeCreateRequest(dest, data, zkutil::CreateMode::Persistent), // Do we need to copy ACLs here?
|
|
|
|
};
|
|
|
|
|
|
|
|
if (remove_src)
|
|
|
|
ops.push_back(zkutil::makeRemoveRequest(src, src_stat.version));
|
|
|
|
|
|
|
|
Coordination::Responses responses;
|
2024-08-29 12:20:42 +00:00
|
|
|
auto code = client->zookeeper->tryMulti(ops, responses);
|
2024-08-28 13:39:29 +00:00
|
|
|
|
|
|
|
switch (code)
|
|
|
|
{
|
2024-08-29 13:28:00 +00:00
|
|
|
case Coordination::Error::ZOK: {
|
2024-08-29 12:18:51 +00:00
|
|
|
is_completed = true;
|
|
|
|
return;
|
|
|
|
}
|
2024-08-29 13:28:00 +00:00
|
|
|
case Coordination::Error::ZBADVERSION: {
|
2024-08-29 12:18:51 +00:00
|
|
|
++failed_tries_count;
|
|
|
|
|
|
|
|
if (isTryLimitReached())
|
|
|
|
zkutil::KeeperMultiException::check(code, ops, responses);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2024-08-28 13:39:29 +00:00
|
|
|
default:
|
|
|
|
zkutil::KeeperMultiException::check(code, ops, responses);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Unreachable");
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
String src;
|
|
|
|
String dest;
|
|
|
|
bool remove_src = false;
|
|
|
|
KeeperClient * client = nullptr;
|
2024-08-29 12:18:51 +00:00
|
|
|
|
|
|
|
bool is_completed = false;
|
|
|
|
uint64_t failed_tries_count = 0;
|
2024-08-28 13:39:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CPCommand::parse(IParser::Pos & pos, std::shared_ptr<ASTKeeperQuery> & node, [[maybe_unused]] Expected & expected) const
|
|
|
|
{
|
|
|
|
String src_path;
|
|
|
|
if (!parseKeeperPath(pos, expected, src_path))
|
|
|
|
return false;
|
|
|
|
node->args.push_back(std::move(src_path));
|
|
|
|
|
|
|
|
String to_path;
|
|
|
|
if (!parseKeeperPath(pos, expected, to_path))
|
|
|
|
return false;
|
|
|
|
node->args.push_back(std::move(to_path));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CPCommand::execute(const ASTKeeperQuery * query, KeeperClient * client) const
|
|
|
|
{
|
|
|
|
auto src = client->getAbsolutePath(query->args[0].safeGet<String>());
|
|
|
|
auto dest = client->getAbsolutePath(query->args[1].safeGet<String>());
|
|
|
|
|
|
|
|
CPMVOperation operation(std::move(src), std::move(dest), /*remove_src_=*/false, /*client_=*/client);
|
|
|
|
|
2024-08-29 12:18:51 +00:00
|
|
|
while (!operation.isTryLimitReached() && !operation.isCompleted())
|
|
|
|
operation.perform();
|
2024-08-28 13:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MVCommand::parse(IParser::Pos & pos, std::shared_ptr<ASTKeeperQuery> & node, Expected & expected) const
|
|
|
|
{
|
|
|
|
String src_path;
|
|
|
|
if (!parseKeeperPath(pos, expected, src_path))
|
|
|
|
return false;
|
|
|
|
node->args.push_back(std::move(src_path));
|
|
|
|
|
|
|
|
String to_path;
|
|
|
|
if (!parseKeeperPath(pos, expected, to_path))
|
|
|
|
return false;
|
|
|
|
node->args.push_back(std::move(to_path));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MVCommand::execute(const ASTKeeperQuery * query, KeeperClient * client) const
|
|
|
|
{
|
|
|
|
auto src = client->getAbsolutePath(query->args[0].safeGet<String>());
|
|
|
|
auto dest = client->getAbsolutePath(query->args[1].safeGet<String>());
|
|
|
|
|
|
|
|
CPMVOperation operation(std::move(src), std::move(dest), /*remove_src_=*/true, /*client_=*/client);
|
|
|
|
|
2024-08-29 12:18:51 +00:00
|
|
|
while (!operation.isTryLimitReached() && !operation.isCompleted())
|
|
|
|
operation.perform();
|
2024-08-28 13:39:29 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 04:54:28 +00:00
|
|
|
}
|