From commits-return-7811-archive-asf-public=cust-asf.ponee.io@zookeeper.apache.org Thu May 23 16:43:11 2019 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id 8805718062F for ; Thu, 23 May 2019 18:43:11 +0200 (CEST) Received: (qmail 2007 invoked by uid 500); 23 May 2019 16:43:10 -0000 Mailing-List: contact commits-help@zookeeper.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@zookeeper.apache.org Delivered-To: mailing list commits@zookeeper.apache.org Received: (qmail 1996 invoked by uid 99); 23 May 2019 16:43:10 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 23 May 2019 16:43:10 +0000 Received: by gitbox.apache.org (ASF Mail Server at gitbox.apache.org, from userid 33) id A4B8985DA5; Thu, 23 May 2019 16:43:10 +0000 (UTC) Date: Thu, 23 May 2019 16:43:10 +0000 To: "commits@zookeeper.apache.org" Subject: [zookeeper] branch branch-3.5 updated: ZOOKEEPER-2694: sync CLI command does not wait for result from server MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Message-ID: <155862979054.6818.4057417005673277691@gitbox.apache.org> From: andor@apache.org X-Git-Host: gitbox.apache.org X-Git-Repo: zookeeper X-Git-Refname: refs/heads/branch-3.5 X-Git-Reftype: branch X-Git-Oldrev: 1cf2b846378febf55632faaa8882728eba18065a X-Git-Newrev: 2a1b69a901f0727b7871af2d762048fb623e6702 X-Git-Rev: 2a1b69a901f0727b7871af2d762048fb623e6702 X-Git-NotificationType: ref_changed_plus_diff X-Git-Multimail-Version: 1.5.dev Auto-Submitted: auto-generated This is an automated email from the ASF dual-hosted git repository. andor pushed a commit to branch branch-3.5 in repository https://gitbox.apache.org/repos/asf/zookeeper.git The following commit(s) were added to refs/heads/branch-3.5 by this push: new 2a1b69a ZOOKEEPER-2694: sync CLI command does not wait for result from server 2a1b69a is described below commit 2a1b69a901f0727b7871af2d762048fb623e6702 Author: maoling AuthorDate: Thu May 23 18:42:39 2019 +0200 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 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 (cherry picked from commit e2bb6e80f598415bba975edcff166ff1bd750340) Signed-off-by: Andor Molnar --- .../java/org/apache/zookeeper/cli/SyncCommand.java | 24 +++++++++++++++++++--- .../java/org/apache/zookeeper/ZooKeeperTest.java | 11 ++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/SyncCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/SyncCommand.java index c0be18d..f82ff84 100644 --- a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/SyncCommand.java +++ b/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 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; } } diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/ZooKeeperTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/ZooKeeperTest.java index 9c67374..00c4125 100644 --- a/zookeeper-server/src/test/java/org/apache/zookeeper/ZooKeeperTest.java +++ b/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 expected = new ArrayList(); + expected.add("Sync is OK"); + + runCommandExpect(cmd, expected); + } }