1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
#include <Common/ZooKeeper/ZooKeeper.h>
#include <Common/ZooKeeper/KeeperException.h>
#include <iostream>
#include <sstream>
#include <Poco/ConsoleChannel.h>
#include <common/logger_useful.h>
#include <common/readline_use.h>
#include <IO/ReadHelpers.h>
#include <IO/ReadBufferFromString.h>
void printStat(const Coordination::Stat & s)
{
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;
}
void waitForWatch(const zkutil::EventPtr & event)
{
std::cout << "waiting for watch" << std::endl;
event->wait();
std::cout << "watch event was signalled" << std::endl;
}
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);
}
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);
Logger::root().setChannel(channel);
Logger::root().setLevel("trace");
zkutil::ZooKeeper zk(argv[1]);
while (char * line_ = readline(":3 "))
{
add_history(line_);
std::string line(line_);
free(line_);
try
{
std::stringstream ss(line);
std::string cmd;
ss >> cmd;
if (cmd == "q" || cmd == "quit" || cmd == "exit" || cmd == ":q")
break;
std::string path;
ss >> path;
if (cmd == "ls")
{
std::string w;
ss >> w;
bool watch = w == "w";
zkutil::EventPtr event = watch ? std::make_shared<Poco::Event>() : nullptr;
std::vector<std::string> v = zk.getChildren(path, nullptr, event);
for (size_t i = 0; i < v.size(); ++i)
{
std::cout << v[i] << std::endl;
}
if (watch)
waitForWatch(event);
}
else if (cmd == "create")
{
DB::ReadBufferFromString in(line);
std::string path;
std::string data;
std::string mode;
DB::assertString("create", in);
DB::skipWhitespaceIfAny(in);
readMaybeQuoted(path, in);
DB::skipWhitespaceIfAny(in);
readMaybeQuoted(data, in);
DB::skipWhitespaceIfAny(in);
readUntilSpace(mode, in);
int32_t m;
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;
}
std::cout << zk.create(path, data, m) << std::endl;
}
else if (cmd == "remove")
{
zk.remove(path);
}
else if (cmd == "rmr")
{
zk.removeRecursive(path);
}
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;
bool e = zk.exists(path, &stat, event);
if (e)
printStat(stat);
else
std::cout << path << " does not exist" << std::endl;
if (watch)
waitForWatch(event);
}
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;
std::string data = zk.get(path, &stat, event);
std::cout << "Data: " << data << std::endl;
printStat(stat);
if (watch)
waitForWatch(event);
}
else if (cmd == "set")
{
DB::ReadBufferFromString in(line);
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;
zk.set(path, data, version, &stat);
printStat(stat);
}
else if (cmd != "")
{
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";
std::cout << " exists path [w]\n";
std::cout << " get path [w]\n";
std::cout << " set path data [version]" << std::endl;
continue;
}
}
catch (const Coordination::Exception & e)
{
std::cerr << "KeeperException: " << e.displayText() << std::endl;
}
}
}
catch (const Coordination::Exception & e)
{
std::cerr << "KeeperException: " << e.displayText() << std::endl;
return 1;
}
return 0;
}
|