Browse Source

ZOOKEEPER-2694: sync CLI command does not wait for result from server

- Thanks the original work from [arshad.mohammad ](https://issues.apache.org/jira/secure/ViewProfile.jspa?name=arshad.mohammad)
- more details in [ZOOKEEPER-2694](https://issues.apache.org/jira/browse/ZOOKEEPER-2694)

Author: maoling <maoling199210191@sina.com>

Reviewers: eolivelli@apache.org, andor@apache.org

Closes #823 from maoling/ZOOKEEPER-2694 and squashes the following commits:

41723cdca [maoling] print the resultCode when sync has failed
95123c003 [maoling] make the Sync more user-friendly
a3ce1704d [maoling] SYNC_TIMEOUT & use the TimeUnit.SECONDS.toMillis & a responsible way of handling the InterruptedException
df0bf7371 [maoling] CliWrapperException
52c3ad0ac [maoling] change CountDownLatch to CompletableFuture
0cc1edd98 [maoling] add the wait_timeout: 30s
cf01294f4 [maoling] ZOOKEEPER-2694:sync CLI command does not wait for result from server
maoling 6 years ago
parent
commit
e2bb6e80f5

+ 21 - 3
zookeeper-server/src/main/java/org/apache/zookeeper/cli/SyncCommand.java

@@ -16,6 +16,11 @@
  */
 package org.apache.zookeeper.cli;
 
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Options;
 import org.apache.commons.cli.ParseException;
@@ -30,6 +35,7 @@ public class SyncCommand extends CliCommand {
 
     private static Options options = new Options();
     private String[] args;
+    public static final long SYNC_TIMEOUT = TimeUnit.SECONDS.toMillis(30L);
 
     public SyncCommand() {
         super("sync", "path");
@@ -55,18 +61,30 @@ public class SyncCommand extends CliCommand {
     @Override
     public boolean exec() throws CliException {
         String path = args[1];
+        CompletableFuture<Integer> cf = new CompletableFuture<>();
+
         try {
             zk.sync(path, new AsyncCallback.VoidCallback() {
-
                 public void processResult(int rc, String path, Object ctx) {
-                    out.println("Sync returned " + rc);
+                    cf.complete(rc);
                 }
             }, null);
+
+            int resultCode = cf.get(SYNC_TIMEOUT, TimeUnit.MILLISECONDS);
+            if(resultCode == 0) {
+                out.println("Sync is OK");
+            } else {
+                out.println("Sync has failed. rc=" + resultCode);
+            }
         } catch (IllegalArgumentException ex) {
             throw new MalformedPathException(ex.getMessage());
+        } catch (InterruptedException ie) {
+            Thread.currentThread().interrupt();
+            throw new CliWrapperException(ie);
+        } catch (TimeoutException | ExecutionException ex) {
+            throw new CliWrapperException(ex);
         }
 
-
         return false;
     }
 }

+ 11 - 0
zookeeper-server/src/test/java/org/apache/zookeeper/ZooKeeperTest.java

@@ -619,4 +619,15 @@ public class ZooKeeperTest extends ClientBase {
         }
     }
 
+    @Test
+    public void testSyncCommand() throws Exception {
+        final ZooKeeper zk = createClient();
+        SyncCommand cmd = new SyncCommand();
+        cmd.setZk(zk);
+        cmd.parse("sync /".split(" "));
+        List<String> expected = new ArrayList<String>();
+        expected.add("Sync is OK");
+
+        runCommandExpect(cmd, expected);
+    }
 }