Uploaded image for project: 'ZooKeeper'
  1. ZooKeeper
  2. ZOOKEEPER-4325

IllegalArgumentException when use ZkUtil::listSubTreeBFS to list "/"

    XMLWordPrintableJSON

Details

    Description

      ZkUtil::listSubTreeBFS

      Assume zk has these nodes: /a_test/a, /b_test/b.

      When use this method to list "/", according to BFS logic, the BFS queue would be added node like: "//a_test", then getChildren by this path, the next exception occurs:

      java.lang.IllegalArgumentException: Invalid path string "//a_test" caused by empty node name specified @1
      

       

       Solutions:

      public static List<String> listSubTreeBFS(
              ZooKeeper zk,
              final String pathRoot) throws KeeperException, InterruptedException {
          Queue<String> queue = new ArrayDeque<>();
          List<String> tree = new ArrayList<>();
          queue.add(pathRoot);
          tree.add(pathRoot);
          while (!queue.isEmpty()) {
              String node = queue.poll();
              List<String> children = zk.getChildren(node, false);
              for (final String child : children) {
                  // Fix "//some_path" bugs when list "/"                
                  final String childPath = (node.equals("/") ? "" : node) + "/" + child;
                  queue.add(childPath);
                  tree.add(childPath);
              }
          }
          return tree;
      }
      

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              justcoding Reno Shen
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 4.5h
                  4.5h