| 1 | #include <Common/ZooKeeper/ZooKeeper.h> |
| 2 | #include <Common/ZooKeeper/KeeperException.h> |
| 3 | #include <Common/typeid_cast.h> |
| 4 | #include <iostream> |
| 5 | #include <port/unistd.h> |
| 6 | |
| 7 | |
| 8 | using namespace zkutil; |
| 9 | |
| 10 | int main(int argc, char ** argv) |
| 11 | try |
| 12 | { |
| 13 | if (argc < 2) |
| 14 | { |
| 15 | std::cerr << "Usage: ./zkutil_test_commands host:port,host:port...\n" ; |
| 16 | return 1; |
| 17 | } |
| 18 | |
| 19 | ZooKeeper zk(argv[1], "" , 5000); |
| 20 | |
| 21 | std::cout << "create path" << std::endl; |
| 22 | zk.create("/test" , "old" , zkutil::CreateMode::Persistent); |
| 23 | Coordination::Stat stat; |
| 24 | zkutil::EventPtr watch = std::make_shared<Poco::Event>(); |
| 25 | |
| 26 | std::cout << "get path" << std::endl; |
| 27 | zk.get("/test" , &stat, watch); |
| 28 | std::cout << "set path" << std::endl; |
| 29 | zk.set("/test" , "new" ); |
| 30 | watch->wait(); |
| 31 | std::cout << "watch happened" << std::endl; |
| 32 | std::cout << "remove path" << std::endl; |
| 33 | |
| 34 | std::cout << "list path" << std::endl; |
| 35 | Strings children = zk.getChildren("/" ); |
| 36 | for (const auto & name : children) |
| 37 | std::cerr << "\t" << name << "\n" ; |
| 38 | |
| 39 | zk.remove("/test" ); |
| 40 | |
| 41 | Coordination::Requests ops; |
| 42 | ops.emplace_back(zkutil::makeCreateRequest("/test" , "multi1" , CreateMode::Persistent)); |
| 43 | ops.emplace_back(zkutil::makeSetRequest("/test" , "multi2" , -1)); |
| 44 | ops.emplace_back(zkutil::makeRemoveRequest("/test" , -1)); |
| 45 | std::cout << "multi" << std::endl; |
| 46 | Coordination::Responses res = zk.multi(ops); |
| 47 | std::cout << "path created: " << dynamic_cast<const Coordination::CreateResponse &>(*res[0]).path_created << std::endl; |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | catch (KeeperException & e) |
| 52 | { |
| 53 | std::cerr << "KeeperException " << e.what() << " " << e.message() << std::endl; |
| 54 | return 1; |
| 55 | } |
| 56 | |