Return-Path: X-Original-To: apmail-curator-commits-archive@minotaur.apache.org Delivered-To: apmail-curator-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 6CBA2112A8 for ; Tue, 17 Jun 2014 19:38:20 +0000 (UTC) Received: (qmail 30716 invoked by uid 500); 17 Jun 2014 19:38:20 -0000 Delivered-To: apmail-curator-commits-archive@curator.apache.org Received: (qmail 30682 invoked by uid 500); 17 Jun 2014 19:38:20 -0000 Mailing-List: contact commits-help@curator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@curator.apache.org Delivered-To: mailing list commits@curator.apache.org Received: (qmail 30668 invoked by uid 99); 17 Jun 2014 19:38:20 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 17 Jun 2014 19:38:20 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id B665C981130; Tue, 17 Jun 2014 19:38:19 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: randgalt@apache.org To: commits@curator.apache.org Date: Tue, 17 Jun 2014 19:38:19 -0000 Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: [1/2] git commit: Now that start() uses AfterConnectionEstablished, no longer correct/needed to handle CONNECTED in state change Repository: curator Updated Branches: refs/heads/CURATOR-110 04cefb47f -> 5954e66fa Now that start() uses AfterConnectionEstablished, no longer correct/needed to handle CONNECTED in state change Project: http://git-wip-us.apache.org/repos/asf/curator/repo Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/fab79a45 Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/fab79a45 Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/fab79a45 Branch: refs/heads/CURATOR-110 Commit: fab79a4577d3af80260d25128cc56f26c7011dbe Parents: 04cefb4 Author: randgalt Authored: Mon Jun 16 14:41:13 2014 -0500 Committer: randgalt Committed: Mon Jun 16 14:41:13 2014 -0500 ---------------------------------------------------------------------- .../framework/recipes/leader/LeaderLatch.java | 100 ++++++++++--------- 1 file changed, 51 insertions(+), 49 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/curator/blob/fab79a45/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderLatch.java ---------------------------------------------------------------------- diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderLatch.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderLatch.java index dce3f5e..9d70645 100644 --- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderLatch.java +++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderLatch.java @@ -44,7 +44,6 @@ import java.io.EOFException; import java.io.IOException; import java.util.Collection; import java.util.List; -import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; @@ -156,9 +155,7 @@ public class LeaderLatch implements Closeable { Preconditions.checkState(state.compareAndSet(State.LATENT, State.STARTED), "Cannot be started more than once"); - AfterConnectionEstablished.execute - ( - client, new Runnable() + AfterConnectionEstablished.execute(client, new Runnable() { @Override public void run() @@ -173,8 +170,7 @@ public class LeaderLatch implements Closeable log.error("An error occurred checking resetting leadership.", e); } } - } - ); + }); } /** @@ -196,7 +192,6 @@ public class LeaderLatch implements Closeable * instances must eventually be closed. * * @param closeMode allows the default close mode to be overridden at the time the latch is closed. - * * @throws IOException errors */ public void close(CloseMode closeMode) throws IOException @@ -218,28 +213,28 @@ public class LeaderLatch implements Closeable switch ( closeMode ) { - case NOTIFY_LEADER: - { - setLeadership(false); - listeners.clear(); - break; - } + case NOTIFY_LEADER: + { + setLeadership(false); + listeners.clear(); + break; + } - default: - { - listeners.clear(); - setLeadership(false); - break; - } + default: + { + listeners.clear(); + setLeadership(false); + break; + } } } } /** * Attaches a listener to this LeaderLatch - *

+ *

* Attaching the same listener multiple times is a noop from the second time on. - *

+ *

* All methods for the listener are run using the provided Executor. It is common to pass in a single-threaded * executor so that you can be certain that listener methods are called in sequence, but if you are fine with * them being called out of order you are welcome to use multiple threads. @@ -253,15 +248,15 @@ public class LeaderLatch implements Closeable /** * Attaches a listener to this LeaderLatch - *

+ *

* Attaching the same listener multiple times is a noop from the second time on. - *

+ *

* All methods for the listener are run using the provided Executor. It is common to pass in a single-threaded * executor so that you can be certain that listener methods are called in sequence, but if you are fine with * them being called out of order you are welcome to use multiple threads. * * @param listener the listener to attach - * @param executor An executor to run the methods for the listener on. + * @param executor An executor to run the methods for the listener on. */ public void addListener(LeaderLatchListener listener, Executor executor) { @@ -282,7 +277,7 @@ public class LeaderLatch implements Closeable *

Causes the current thread to wait until this instance acquires leadership * unless the thread is {@linkplain Thread#interrupt interrupted} or {@linkplain #close() closed}.

*

If this instance already is the leader then this method returns immediately.

- * + *

*

Otherwise the current * thread becomes disabled for thread scheduling purposes and lies * dormant until one of three things happen:

@@ -324,10 +319,10 @@ public class LeaderLatch implements Closeable *

Causes the current thread to wait until this instance acquires leadership * unless the thread is {@linkplain Thread#interrupt interrupted}, * the specified waiting time elapses or the instance is {@linkplain #close() closed}.

- * + *

*

If this instance already is the leader then this method returns immediately * with the value {@code true}.

- * + *

*

Otherwise the current * thread becomes disabled for thread scheduling purposes and lies * dormant until one of four things happen:

@@ -338,7 +333,7 @@ public class LeaderLatch implements Closeable *
  • The specified waiting time elapses.
  • *
  • The instance is {@linkplain #close() closed}
  • * - * + *

    *

    If the current thread:

    *
      *
    • has its interrupted status set on entry to this method; or @@ -346,7 +341,7 @@ public class LeaderLatch implements Closeable *
    *

    then {@link InterruptedException} is thrown and the current thread's * interrupted status is cleared.

    - * + *

    *

    If the specified waiting time elapses or the instance is {@linkplain #close() closed} * then the value {@code false} is returned. If the time is less than or equal to zero, the method * will not wait at all.

    @@ -354,7 +349,7 @@ public class LeaderLatch implements Closeable * @param timeout the maximum time to wait * @param unit the time unit of the {@code timeout} argument * @return {@code true} if the count reached zero and {@code false} - * if the waiting time elapsed before the count reached zero or the instances was closed + * if the waiting time elapsed before the count reached zero or the instances was closed * @throws InterruptedException if the current thread is interrupted * while waiting */ @@ -561,22 +556,35 @@ public class LeaderLatch implements Closeable private void handleStateChange(ConnectionState newState) { - if ( newState == ConnectionState.RECONNECTED ) + switch ( newState ) { - try + default: { - reset(); + // NOP + break; + } + + case RECONNECTED: + { + try + { + reset(); + } + catch ( Exception e ) + { + log.error("Could not reset leader latch", e); + setLeadership(false); + } + break; } - catch (Exception e) + + case SUSPENDED: + case LOST: { - log.error("Could not reset leader latch", e); setLeadership(false); + break; } } - else - { - setLeadership(false); - } } private synchronized void setLeadership(boolean newValue) @@ -585,9 +593,7 @@ public class LeaderLatch implements Closeable if ( oldValue && !newValue ) { // Lost leadership, was true, now false - listeners.forEach - ( - new Function() + listeners.forEach(new Function() { @Override public Void apply(LeaderLatchListener listener) @@ -595,14 +601,11 @@ public class LeaderLatch implements Closeable listener.notLeader(); return null; } - } - ); + }); } else if ( !oldValue && newValue ) { // Gained leadership, was false, now true - listeners.forEach - ( - new Function() + listeners.forEach(new Function() { @Override public Void apply(LeaderLatchListener input) @@ -610,8 +613,7 @@ public class LeaderLatch implements Closeable input.isLeader(); return null; } - } - ); + }); } notifyAll();