ClickHouse/utils/zookeeper-cli/zookeeper-cli.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

224 lines
7.4 KiB
C++
Raw Permalink Normal View History

2020-01-23 08:18:19 +00:00
#include <IO/ReadBufferFromString.h>
#include <IO/ReadHelpers.h>
#include <Poco/ConsoleChannel.h>
#include <Common/ZooKeeper/KeeperException.h>
2020-01-23 08:18:19 +00:00
#include <Common/ZooKeeper/ZooKeeper.h>
#include <Client/LineReader.h>
2022-04-27 15:05:45 +00:00
#include <Common/logger_useful.h>
2020-01-23 08:18:19 +00:00
2014-03-07 13:50:58 +00:00
#include <iostream>
#include <sstream>
2014-03-07 17:57:53 +00:00
void printStat(const Coordination::Stat & s)
2014-03-07 17:57:53 +00:00
{
std::cout << "Stat:\n";
std::cout << " czxid: " << s.czxid << '\n';
std::cout << " mzxid: " << s.mzxid << '\n';
std::cout << " ctime: " << s.ctime << '\n';
std::cout << " mtime: " << s.mtime << '\n';
std::cout << " version: " << s.version << '\n';
std::cout << " cversion: " << s.cversion << '\n';
std::cout << " aversion: " << s.aversion << '\n';
std::cout << " ephemeralOwner: " << s.ephemeralOwner << '\n';
std::cout << " dataLength: " << s.dataLength << '\n';
std::cout << " numChildren: " << s.numChildren << '\n';
std::cout << " pzxid: " << s.pzxid << std::endl;
2014-03-07 17:57:53 +00:00
}
void waitForWatch(const zkutil::EventPtr & event)
2014-03-07 17:57:53 +00:00
{
std::cout << "waiting for watch" << std::endl;
event->wait();
std::cout << "watch event was signalled" << std::endl;
2014-03-07 17:57:53 +00:00
}
2014-03-07 13:50:58 +00:00
void readUntilSpace(std::string & s, DB::ReadBuffer & buf)
{
s = "";
while (!buf.eof())
{
if (isspace(*buf.position()))
return;
s.push_back(*buf.position());
++buf.position();
}
}
void readMaybeQuoted(std::string & s, DB::ReadBuffer & buf)
{
if (!buf.eof() && *buf.position() == '\'')
DB::readQuotedString(s, buf);
else
readUntilSpace(s, buf);
}
2014-03-07 13:50:58 +00:00
int main(int argc, char ** argv)
{
try
{
if (argc != 2)
{
std::cerr << "usage: " << argv[0] << " hosts" << std::endl;
return 2;
}
Poco::AutoPtr<Poco::ConsoleChannel> channel = new Poco::ConsoleChannel(std::cerr);
2020-05-30 21:57:37 +00:00
Poco::Logger::root().setChannel(channel);
Poco::Logger::root().setLevel("trace");
2024-02-16 13:05:22 +00:00
auto zk = zkutil::ZooKeeper::createWithoutKillingPreviousSessions(zkutil::ZooKeeperArgs(argv[1]));
DB::LineReader lr({}, false, {"\\"}, {});
do
2014-03-07 13:50:58 +00:00
{
const auto & line = lr.readLine(":3 ", ":3 ");
if (line.empty())
break;
2014-03-07 13:50:58 +00:00
try
{
2020-11-10 18:22:26 +00:00
std::stringstream ss(line); // STYLE_CHECK_ALLOW_STD_STRING_STREAM
2014-03-07 13:50:58 +00:00
std::string cmd;
2014-03-07 17:57:53 +00:00
ss >> cmd;
2014-03-07 13:50:58 +00:00
if (cmd == "q" || cmd == "quit" || cmd == "exit" || cmd == ":q")
break;
2014-03-07 13:50:58 +00:00
std::string path;
ss >> path;
if (cmd == "ls")
{
2014-03-07 17:57:53 +00:00
std::string w;
ss >> w;
bool watch = w == "w";
zkutil::EventPtr event = watch ? std::make_shared<Poco::Event>() : nullptr;
2024-02-16 13:05:22 +00:00
std::vector<std::string> v = zk->getChildren(path, nullptr, event);
for (const auto & child : v)
std::cout << child << std::endl;
2014-03-07 17:57:53 +00:00
if (watch)
waitForWatch(event);
2014-03-07 17:57:53 +00:00
}
else if (cmd == "create")
{
DB::ReadBufferFromString in(line);
std::string path_ignored;
std::string data;
std::string mode;
DB::assertString("create", in);
DB::skipWhitespaceIfAny(in);
readMaybeQuoted(path_ignored, in);
DB::skipWhitespaceIfAny(in);
readMaybeQuoted(data, in);
DB::skipWhitespaceIfAny(in);
readUntilSpace(mode, in);
int32_t m;
2014-03-07 17:57:53 +00:00
if (mode == "p")
m = zkutil::CreateMode::Persistent;
else if (mode == "ps")
m = zkutil::CreateMode::PersistentSequential;
else if (mode == "e")
m = zkutil::CreateMode::Ephemeral;
else if (mode == "es")
m = zkutil::CreateMode::EphemeralSequential;
else
{
std::cout << "Bad create mode" << std::endl;
continue;
}
2024-02-16 13:05:22 +00:00
std::cout << zk->create(path, data, m) << std::endl;
2014-03-07 17:57:53 +00:00
}
else if (cmd == "remove")
{
2024-02-16 13:05:22 +00:00
zk->remove(path);
2014-03-07 17:57:53 +00:00
}
else if (cmd == "rmr")
{
2024-02-16 13:05:22 +00:00
zk->removeRecursive(path);
}
2014-03-07 17:57:53 +00:00
else if (cmd == "exists")
{
std::string w;
ss >> w;
bool watch = w == "w";
zkutil::EventPtr event = watch ? std::make_shared<Poco::Event>() : nullptr;
Coordination::Stat stat;
2024-02-16 13:05:22 +00:00
bool e = zk->exists(path, &stat, event);
2014-03-07 17:57:53 +00:00
if (e)
printStat(stat);
else
std::cout << path << " does not exist" << std::endl;
if (watch)
waitForWatch(event);
2014-03-07 17:57:53 +00:00
}
else if (cmd == "get")
{
std::string w;
ss >> w;
bool watch = w == "w";
zkutil::EventPtr event = watch ? std::make_shared<Poco::Event>() : nullptr;
Coordination::Stat stat;
2024-02-16 13:05:22 +00:00
std::string data = zk->get(path, &stat, event);
2014-03-07 17:57:53 +00:00
std::cout << "Data: " << data << std::endl;
printStat(stat);
if (watch)
waitForWatch(event);
2014-03-07 17:57:53 +00:00
}
else if (cmd == "set")
{
DB::ReadBufferFromString in(line);
2014-03-07 17:57:53 +00:00
std::string data;
int version = -1;
DB::assertString("set", in);
DB::skipWhitespaceIfAny(in);
DB::assertString(path, in);
DB::skipWhitespaceIfAny(in);
readMaybeQuoted(data, in);
DB::skipWhitespaceIfAny(in);
if (!in.eof())
DB::readText(version, in);
Coordination::Stat stat;
2024-02-16 13:05:22 +00:00
zk->set(path, data, version, &stat);
2014-03-07 17:57:53 +00:00
printStat(stat);
}
else if (!cmd.empty())
2014-03-07 17:57:53 +00:00
{
std::cout << "commands:\n";
std::cout << " q\n";
std::cout << " ls path [w]\n";
std::cout << " create path data (p|ps|e|es)\n";
std::cout << " remove path\n";
std::cout << " rmr path\n";
2014-03-07 17:57:53 +00:00
std::cout << " exists path [w]\n";
std::cout << " get path [w]\n";
std::cout << " set path data [version]" << std::endl;
continue;
2014-03-07 13:50:58 +00:00
}
2014-03-07 13:50:58 +00:00
}
catch (const Coordination::Exception & e)
2014-03-07 13:50:58 +00:00
{
std::cerr << "KeeperException: " << e.displayText() << std::endl;
}
}
while (true);
2014-03-07 13:50:58 +00:00
}
catch (const Coordination::Exception & e)
2014-03-07 13:50:58 +00:00
{
std::cerr << "KeeperException: " << e.displayText() << std::endl;
return 1;
}
2014-03-07 13:50:58 +00:00
return 0;
}