Rewriting ZooKeeper library [#CLICKHOUSE-2]

This commit is contained in:
Alexey Milovidov 2018-03-19 22:23:05 +03:00
parent 831d38fd59
commit c8f8dc429c
3 changed files with 36 additions and 17 deletions

View File

@ -585,7 +585,7 @@ void ZooKeeper::sendAuth(const String & scheme, const String & data)
request.write(*out);
int32_t length;
int32_t xid;
XID xid;
int64_t zxid;
int32_t err;
@ -701,6 +701,7 @@ ZooKeeper::ResponsePtr ZooKeeper::CheckRequest::makeResponse() const { return st
ZooKeeper::ResponsePtr ZooKeeper::MultiRequest::makeResponse() const { return std::make_shared<MultiResponse>(requests); }
ZooKeeper::ResponsePtr ZooKeeper::CloseRequest::makeResponse() const { return std::make_shared<CloseResponse>(); }
void addRootPath(String & path, const String & root_path)
{
if (path.empty())
@ -754,7 +755,7 @@ void ZooKeeper::MultiResponse::removeRootPath(const String & root_path)
void ZooKeeper::receiveEvent()
{
int32_t length;
int32_t xid;
XID xid;
int64_t zxid;
int32_t err;

View File

@ -174,8 +174,8 @@ public:
{
String path;
String data;
bool is_ephemeral;
bool is_sequential;
bool is_ephemeral = false;
bool is_sequential = false;
ACLs acls;
OpNum getOpNum() const override { return 1; }

View File

@ -33,27 +33,34 @@ try
ZooKeeper zk(addresses, {}, {}, {}, {5, 0}, {0, 50000});
Strings children;
Poco::Event event(true);
std::cout << "create\n";
zk.create("/test", "old", false, false, {}, [](const ZooKeeper::CreateResponse & response)
zk.create("/test", "old", false, false, {},
[&](const ZooKeeper::CreateResponse & response)
{
if (response.error)
std::cerr << "Error (create) " << response.error << ": " << ZooKeeper::errorMessage(response.error) << '\n';
else
std::cerr << "Created path: " << response.path_created << '\n';
//event.set();
});
//event.wait();
std::cout << "get\n";
zk.get("/test",
[](const ZooKeeper::GetResponse & response)
[&](const ZooKeeper::GetResponse & response)
{
if (response.error)
std::cerr << "Error (get) " << response.error << ": " << ZooKeeper::errorMessage(response.error) << '\n';
else
std::cerr << "Value: " << response.data << '\n';
//event.set();
},
[](const ZooKeeper::WatchResponse & response)
{
@ -63,20 +70,27 @@ try
std::cerr << "Watch (get) on /test, path: " << response.path << ", type: " << response.type << '\n';
});
//event.wait();
std::cout << "set\n";
zk.set("/test", "new", -1, [](const ZooKeeper::SetResponse & response)
zk.set("/test", "new", -1,
[&](const ZooKeeper::SetResponse & response)
{
if (response.error)
std::cerr << "Error (set) " << response.error << ": " << ZooKeeper::errorMessage(response.error) << '\n';
else
std::cerr << "Set\n";
//event.set();
});
//event.wait();
std::cout << "list\n";
zk.list("/",
[](const ZooKeeper::ListResponse & response)
[&](const ZooKeeper::ListResponse & response)
{
if (response.error)
std::cerr << "Error (list) " << response.error << ": " << ZooKeeper::errorMessage(response.error) << '\n';
@ -86,6 +100,8 @@ try
for (const auto & name : response.names)
std::cerr << name << "\n";
}
//event.set();
},
[](const ZooKeeper::WatchResponse & response)
{
@ -95,15 +111,19 @@ try
std::cerr << "Watch (list) on /, path: " << response.path << ", type: " << response.type << '\n';
});
//event.wait();
std::cout << "exists\n";
zk.exists("/test",
[](const ZooKeeper::ExistsResponse & response)
[&](const ZooKeeper::ExistsResponse & response)
{
if (response.error)
std::cerr << "Error (exists) " << response.error << ": " << ZooKeeper::errorMessage(response.error) << '\n';
else
std::cerr << "Exists\n";
//event.set();
},
[](const ZooKeeper::WatchResponse & response)
{
@ -113,23 +133,21 @@ try
std::cerr << "Watch (exists) on /test, path: " << response.path << ", type: " << response.type << '\n';
});
std::cout << "remove\n";
//event.wait();
Poco::Event event(true);
std::cout << "remove\n";
zk.remove("/test", -1, [&](const ZooKeeper::RemoveResponse & response)
{
if (response.error)
std::cerr << "Error " << response.error << ": " << ZooKeeper::errorMessage(response.error) << '\n';
std::cerr << "Error (remove) " << response.error << ": " << ZooKeeper::errorMessage(response.error) << '\n';
else
std::cerr << "Removed\n";
event.set();
//event.set();
});
event.wait();
/// Surprising enough, ZooKeeper can execute multi transaction out of order. So, we must to wait for "remove" to execute before sending "multi".
//event.wait();
std::cout << "multi\n";