1#include <list>
2#include <iostream>
3#include <boost/program_options.hpp>
4#include <Common/Exception.h>
5#include <Common/ZooKeeper/ZooKeeper.h>
6#include <Common/ZooKeeper/KeeperException.h>
7
8
9int main(int argc, char ** argv)
10try
11{
12 boost::program_options::options_description desc("Allowed options");
13 desc.add_options()
14 ("help,h", "produce help message")
15 ("address,a", boost::program_options::value<std::string>()->required(),
16 "addresses of ZooKeeper instances, comma separated. Example: example01e.yandex.ru:2181")
17 ("path,p", boost::program_options::value<std::string>()->default_value("/"),
18 "where to start")
19 ;
20
21 boost::program_options::variables_map options;
22 boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), options);
23
24 if (options.count("help"))
25 {
26 std::cout << "Dump paths of all nodes in ZooKeeper." << std::endl;
27 std::cout << "Usage: " << argv[0] << " [options]" << std::endl;
28 std::cout << desc << std::endl;
29 return 1;
30 }
31
32 zkutil::ZooKeeper zookeeper(options.at("address").as<std::string>());
33
34 std::string initial_path = options.at("path").as<std::string>();
35
36 std::list<std::pair<std::string, std::future<Coordination::ListResponse>>> list_futures;
37 list_futures.emplace_back(initial_path, zookeeper.asyncGetChildren(initial_path));
38
39 for (auto it = list_futures.begin(); it != list_futures.end(); ++it)
40 {
41 Coordination::ListResponse response;
42 try
43 {
44 response = it->second.get();
45 }
46 catch (const Coordination::Exception & e)
47 {
48 if (e.code == Coordination::ZNONODE)
49 continue;
50 throw;
51 }
52
53 std::cout << it->first << '\t' << response.stat.numChildren << '\t' << response.stat.dataLength << '\n';
54
55 for (const auto & name : response.names)
56 {
57 std::string child_path = it->first == "/" ? it->first + name : it->first + '/' + name;
58 list_futures.emplace_back(child_path, zookeeper.asyncGetChildren(child_path));
59 }
60 }
61}
62catch (...)
63{
64 std::cerr << DB::getCurrentExceptionMessage(true) << '\n';
65 throw;
66}
67