@@ -2781,9 +2781,14 @@ public List<String> getChildren(final String path, Watcher watcher) throws Keepe
27812781 * @param outputNextPage
27822782 * - if not null, {@link PaginationNextPage} info returned from server will be copied to
27832783 * {@code outputNextPage}. The info can be used for fetching the next page of remaining children,
2784- * or checking whether the returned page is the last page.
2784+ * or checking whether the returned page is the last page by comparing
2785+ * {@link PaginationNextPage#getMinCzxid()} with
2786+ * {@link org.apache.zookeeper.ZooDefs.GetChildrenPaginated#lastPageMinCzxid}
27852787 * @return
2786- * a list of children nodes, up to {@code maxReturned}
2788+ * a list of children nodes, up to {@code maxReturned}.
2789+ * <p>When params are set {@code maxReturned} = {@code Integer.MAX_VALUE}, {@code minCzxid} <= 0:
2790+ * the children list is unordered if all the children can be returned in a page;
2791+ * <p>Otherwise, the children list is ordered by czxid.
27872792 * @throws KeeperException
27882793 * if the server signals an error with a non-zero error code.
27892794 * @throws IllegalArgumentException
@@ -2860,7 +2865,7 @@ public List<String> getChildren(final String path,
28602865 *
28612866 * @param path the path of the parent node
28622867 * @param watch whether or not leave a watch on the given node
2863- * @return a list of all children of the given path
2868+ * @return a unordered list of all children of the given path
28642869 * @throws KeeperException if the server signals an error with a non-zero error code.
28652870 * @throws InterruptedException if the server transaction is interrupted.
28662871 */
@@ -2874,21 +2879,39 @@ public List<String> getAllChildrenPaginated(String path, boolean watch)
28742879 }
28752880
28762881 Set <String > childrenSet = new HashSet <>(childrenPaginated );
2882+ getChildrenRemainingPages (path , watcher , nextPage , childrenSet );
2883+
2884+ return new ArrayList <>(childrenSet );
2885+ }
2886+
2887+ private void getChildrenRemainingPages (String path ,
2888+ Watcher watcher ,
2889+ PaginationNextPage nextPage ,
2890+ Set <String > childrenSet ) throws KeeperException , InterruptedException {
2891+ int retryCount = 0 ;
28772892 do {
2893+ List <String > childrenPaginated ;
28782894 long minCzxid = nextPage .getMinCzxid ();
2879- // If a child with the same czxid is returned in the previous page, and then deleted
2880- // on the server, the children not in the previous page but offset is less than czxidOffset
2881- // would be missed in the next page. Use the last child in the previous page to determine whether or not
2882- // the deletion case happens. If it happens, retry fetching the page starting from offset 0.
2883- int czxidOffset = nextPage .getMinCzxidOffset () - 1 ;
2884- childrenPaginated = getChildren (path , watcher , Integer .MAX_VALUE , minCzxid , czxidOffset , null , nextPage );
2885- if (!childrenPaginated .isEmpty () && !childrenSet .contains (childrenPaginated .get (0 ))) {
2886- childrenPaginated = getChildren (path , watcher , Integer .MAX_VALUE , minCzxid , 0 , null , nextPage );
2895+ if (nextPage .getMinCzxidOffset () == 0 ) {
2896+ childrenPaginated = getChildren (path , watcher , Integer .MAX_VALUE , minCzxid ,
2897+ nextPage .getMinCzxidOffset (), null , nextPage );
2898+ } else {
2899+ // If a child with the same czxid is returned in the previous page, and then deleted
2900+ // on the server, the children not in the previous page but offset is less than czxidOffset
2901+ // would be missed in the next page. Use the last child in the previous page to determine whether or not
2902+ // the deletion case happens. If it happens, retry fetching the page starting from offset 0.
2903+ childrenPaginated = getChildren (path , watcher , Integer .MAX_VALUE , minCzxid ,
2904+ nextPage .getMinCzxidOffset () - 1 , null , nextPage );
2905+ if (!childrenPaginated .isEmpty () && !childrenSet .contains (childrenPaginated .get (0 ))) {
2906+ if (retryCount ++ >= 3 ) {
2907+ throw KeeperException .create (Code .OPERATIONTIMEOUT );
2908+ }
2909+ LOG .info ("Retry fetching children for minZxid = {}, retries = {}" , minCzxid , retryCount );
2910+ childrenPaginated = getChildren (path , watcher , Integer .MAX_VALUE , minCzxid , 0 , null , nextPage );
2911+ }
28872912 }
28882913 childrenSet .addAll (childrenPaginated );
28892914 } while (nextPage .getMinCzxid () != ZooDefs .GetChildrenPaginated .lastPageMinCzxid );
2890-
2891- return new ArrayList <>(childrenSet );
28922915 }
28932916
28942917 private void updateNextPage (PaginationNextPage nextPage , long minCzxId , int czxIdOffset ) {
0 commit comments