curator-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From randg...@apache.org
Subject git commit: Added LeaderSelectorListenerAdapter to enforce/help with correct connection state handling. Also, updated docs and added another test,
Date Wed, 11 Sep 2013 15:58:50 GMT
Updated Branches:
  refs/heads/CURATOR-54 37cf6524c -> 9a7d49776


Added LeaderSelectorListenerAdapter to enforce/help with correct connection state handling.
Also, updated
docs and added another test,


Project: http://git-wip-us.apache.org/repos/asf/incubator-curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-curator/commit/9a7d4977
Tree: http://git-wip-us.apache.org/repos/asf/incubator-curator/tree/9a7d4977
Diff: http://git-wip-us.apache.org/repos/asf/incubator-curator/diff/9a7d4977

Branch: refs/heads/CURATOR-54
Commit: 9a7d497762efba56baaa0c11173dc81e08bec4ca
Parents: 37cf652
Author: jordan.zimmerman <jordan.zimmerman@riotgames.com>
Authored: Wed Sep 11 10:51:18 2013 -0500
Committer: jordan.zimmerman <jordan.zimmerman@riotgames.com>
Committed: Wed Sep 11 10:51:18 2013 -0500

----------------------------------------------------------------------
 .../src/main/java/leader/ExampleClient.java     | 20 +++--------
 .../leader/LeaderSelectorListenerAdapter.java   | 38 ++++++++++++++++++++
 .../site/confluence/leader-election.confluence  |  4 ++-
 .../recipes/leader/TestLeaderSelector.java      | 37 +++++++++++++++++++
 4 files changed, 82 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/9a7d4977/curator-examples/src/main/java/leader/ExampleClient.java
----------------------------------------------------------------------
diff --git a/curator-examples/src/main/java/leader/ExampleClient.java b/curator-examples/src/main/java/leader/ExampleClient.java
index eebe5c0..6ec4a1f 100644
--- a/curator-examples/src/main/java/leader/ExampleClient.java
+++ b/curator-examples/src/main/java/leader/ExampleClient.java
@@ -19,19 +19,18 @@
 package leader;
 
 import org.apache.curator.framework.CuratorFramework;
-import org.apache.curator.framework.recipes.leader.CancelLeadershipException;
+import org.apache.curator.framework.recipes.leader.LeaderSelectorListenerAdapter;
 import org.apache.curator.framework.recipes.leader.LeaderSelector;
-import org.apache.curator.framework.recipes.leader.LeaderSelectorListener;
-import org.apache.curator.framework.state.ConnectionState;
 import java.io.Closeable;
 import java.io.IOException;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
 
 /**
- * An example leader selector client
+ * An example leader selector client. Note that {@link LeaderSelectorListenerAdapter} which
+ * has the recommended handling for connection state issues
  */
-public class ExampleClient implements Closeable, LeaderSelectorListener
+public class ExampleClient extends LeaderSelectorListenerAdapter implements Closeable
 {
     private final String name;
     private final LeaderSelector leaderSelector;
@@ -86,15 +85,4 @@ public class ExampleClient implements Closeable, LeaderSelectorListener
             System.out.println(name + " relinquishing leadership.\n");
         }
     }
-
-    @Override
-    public void stateChanged(CuratorFramework client, ConnectionState newState)
-    {
-        // you MUST handle connection state changes. This WILL happen in production code.
-
-        if ( (newState == ConnectionState.LOST) || (newState == ConnectionState.SUSPENDED)
)
-        {
-            throw new CancelLeadershipException();
-        }
-    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/9a7d4977/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderSelectorListenerAdapter.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderSelectorListenerAdapter.java
b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderSelectorListenerAdapter.java
new file mode 100644
index 0000000..7402fa7
--- /dev/null
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderSelectorListenerAdapter.java
@@ -0,0 +1,38 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.curator.framework.recipes.leader;
+
+import org.apache.curator.framework.CuratorFramework;
+import org.apache.curator.framework.state.ConnectionState;
+
+/**
+ * An implementation of {@link LeaderSelectorListener} that adds the recommended handling
+ * for connection state problems
+ */
+public abstract class LeaderSelectorListenerAdapter implements LeaderSelectorListener
+{
+    @Override
+    public void stateChanged(CuratorFramework client, ConnectionState newState)
+    {
+        if ( (newState == ConnectionState.SUSPENDED) || (newState == ConnectionState.LOST)
)
+        {
+            throw new CancelLeadershipException();
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/9a7d4977/curator-recipes/src/site/confluence/leader-election.confluence
----------------------------------------------------------------------
diff --git a/curator-recipes/src/site/confluence/leader-election.confluence b/curator-recipes/src/site/confluence/leader-election.confluence
index 15ecdcc..1830bea 100644
--- a/curator-recipes/src/site/confluence/leader-election.confluence
+++ b/curator-recipes/src/site/confluence/leader-election.confluence
@@ -8,6 +8,7 @@ NOTE: Curator has two leader election recipes. Which one to use depends on
your
 h2. Participating Classes
 * LeaderSelector
 * LeaderSelectorListener
+* LeaderSelectorListenerAdapter
 * CancelLeadershipException
 
 h2. Usage
@@ -56,4 +57,5 @@ being SUSPENDED or LOST. If the SUSPENDED state is reported, the instance
must a
 state is reported, the instance is no longer the leader and its {{takeLeadership}} method
should exit.
 
 IMPORTANT: The recommended action for receiving SUSPENDED or LOST is to throw {{CancelLeadershipException}}.
This will cause the LeaderSelector instance to attempt
-to interrupt and cancel the thread that is executing the {{takeLeadership}} method.
+to interrupt and cancel the thread that is executing the {{takeLeadership}} method. Because
this is so important, you should consider extending {{LeaderSelectorListenerAdapter}}.
+{{LeaderSelectorListenerAdapter}} has the recommended handling already written for you.

http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/9a7d4977/curator-recipes/src/test/java/org/apache/curator/framework/recipes/leader/TestLeaderSelector.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/leader/TestLeaderSelector.java
b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/leader/TestLeaderSelector.java
index f0c703a..63eeaf2 100644
--- a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/leader/TestLeaderSelector.java
+++ b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/leader/TestLeaderSelector.java
@@ -46,6 +46,43 @@ public class TestLeaderSelector extends BaseClassForTests
     private static final String PATH_NAME = "/one/two/me";
 
     @Test
+    public void testInterruptLeadershipWithRequeue() throws Exception
+    {
+        Timing timing = new Timing();
+        LeaderSelector selector = null;
+        CuratorFramework client = null;
+        try
+        {
+            client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(),
timing.connection(), new RetryOneTime(1));
+            client.start();
+
+            final Semaphore semaphore = new Semaphore(0);
+            LeaderSelectorListener listener = new LeaderSelectorListenerAdapter()
+            {
+                @Override
+                public void takeLeadership(CuratorFramework client) throws Exception
+                {
+                    semaphore.release();
+                    Thread.currentThread().join();
+                }
+            };
+            selector = new LeaderSelector(client, "/leader", listener);
+            selector.autoRequeue();
+            selector.start();
+
+            Assert.assertTrue(timing.acquireSemaphore(semaphore));
+            selector.interruptLeadership();
+
+            Assert.assertTrue(timing.acquireSemaphore(semaphore));
+        }
+        finally
+        {
+            Closeables.closeQuietly(selector);
+            Closeables.closeQuietly(client);
+        }
+    }
+
+    @Test
     public void testInterruptLeadership() throws Exception
     {
         LeaderSelector selector = null;


Mime
View raw message