CURATOR-114 - Modified the restart() method so that it will restart the
server from any state other than CLOSED (because this server has had all
of its state cleared).

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

Branch: refs/heads/CURATOR-114
Commit: 61eb3886e371f7285b8a0572a6b1c896c64b8065
Parents: 27c70aa
Author: Cameron McKenzie <came...@unico.com.au>
Authored: Wed Jun 18 08:57:26 2014 +1000
Committer: Cameron McKenzie <came...@unico.com.au>
Committed: Wed Jun 18 08:57:26 2014 +1000

----------------------------------------------------------------------
 .../org/apache/curator/test/TestingServer.java  |  6 +-
 .../curator/test/TestingZooKeeperServer.java    | 98 ++++++++++++--------
 2 files changed, 64 insertions(+), 40 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/61eb3886/curator-test/src/main/java/org/apache/curator/test/TestingServer.java
----------------------------------------------------------------------
diff --git 
a/curator-test/src/main/java/org/apache/curator/test/TestingServer.java 
b/curator-test/src/main/java/org/apache/curator/test/TestingServer.java
index d20a9f8..c69bf6a 100644
--- a/curator-test/src/main/java/org/apache/curator/test/TestingServer.java
+++ b/curator-test/src/main/java/org/apache/curator/test/TestingServer.java
@@ -171,8 +171,10 @@ public class TestingServer implements Closeable
     }
 
     /**
-     * Restart the server. This is only valid if the server has previously been
-     * stopped.
+     * Restart the server. If the server is currently running it will be 
stopped
+     * and restarted. If it's not currently running then it will be started. If
+     * it has been closed (had close() called on it) then an exception will be
+     * thrown.
      * 
      * @throws Exception
      */

http://git-wip-us.apache.org/repos/asf/curator/blob/61eb3886/curator-test/src/main/java/org/apache/curator/test/TestingZooKeeperServer.java
----------------------------------------------------------------------
diff --git 
a/curator-test/src/main/java/org/apache/curator/test/TestingZooKeeperServer.java
 
b/curator-test/src/main/java/org/apache/curator/test/TestingZooKeeperServer.java
index e706847..cb855e9 100644
--- 
a/curator-test/src/main/java/org/apache/curator/test/TestingZooKeeperServer.java
+++ 
b/curator-test/src/main/java/org/apache/curator/test/TestingZooKeeperServer.java
@@ -18,34 +18,34 @@
  */
 package org.apache.curator.test;
 
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.concurrent.atomic.AtomicReference;
+
 import org.apache.zookeeper.server.quorum.QuorumPeer;
 import org.apache.zookeeper.server.quorum.QuorumPeerConfig;
 import org.apache.zookeeper.server.quorum.QuorumPeerMain;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import java.io.Closeable;
-import java.io.IOException;
-import java.util.Collection;
-import java.util.concurrent.atomic.AtomicReference;
 
 /**
  * Thanks to Jeremie BORDIER (ahfeel) for this code
  */
 public class TestingZooKeeperServer extends QuorumPeerMain implements Closeable
 {
-    private static final Logger logger = 
LoggerFactory.getLogger(TestingZooKeeperServer.class);
+    private static final Logger logger = LoggerFactory
+            .getLogger(TestingZooKeeperServer.class);
 
     private final QuorumConfigBuilder configBuilder;
     private final int thisInstanceIndex;
     private volatile ZooKeeperMainFace main;
-    private final AtomicReference<State> state = new 
AtomicReference<State>(State.LATENT);
+    private final AtomicReference<State> state = new AtomicReference<State>(
+            State.LATENT);
 
     private enum State
     {
-        LATENT,
-        STARTED,
-        STOPPED,
-        CLOSED
+        LATENT, STARTED, STOPPED, CLOSED
     }
 
     public TestingZooKeeperServer(QuorumConfigBuilder configBuilder)
@@ -53,11 +53,13 @@ public class TestingZooKeeperServer extends QuorumPeerMain 
implements Closeable
         this(configBuilder, 0);
     }
 
-    public TestingZooKeeperServer(QuorumConfigBuilder configBuilder, int 
thisInstanceIndex)
+    public TestingZooKeeperServer(QuorumConfigBuilder configBuilder,
+            int thisInstanceIndex)
     {
         this.configBuilder = configBuilder;
         this.thisInstanceIndex = thisInstanceIndex;
-        main = (configBuilder.size() > 1) ? new TestingQuorumPeerMain() : new 
TestingZooKeeperMain();
+        main = (configBuilder.size() > 1) ? new TestingQuorumPeerMain()
+                : new TestingZooKeeperMain();
     }
 
     public QuorumPeer getQuorumPeer()
@@ -70,32 +72,51 @@ public class TestingZooKeeperServer extends QuorumPeerMain 
implements Closeable
         return configBuilder.getInstanceSpecs();
     }
 
-    public void     kill()
+    public void kill()
     {
         main.kill();
         state.set(State.STOPPED);
     }
 
-    public void     restart() throws Exception
+    /**
+     * Restart the server. If the server is running it will be stopped and then
+     * started again. If it is not running (in a LATENT or STOPPED state) then
+     * it will be restarted. If it is in a CLOSED state then an exception will
+     * be thrown.
+     * 
+     * @throws Exception
+     */
+    public void restart() throws Exception
     {
-        if ( !state.compareAndSet(State.STOPPED, State.LATENT) )
+        // Can't restart from a closed state as all the temporary data is gone
+        if (state.get() == State.CLOSED)
+        {
+            throw new IllegalStateException("Cannot restart a closed 
instance");
+        }
+
+        // If the server's currently running then stop it.
+        if (state.get() == State.STARTED)
         {
-            throw new IllegalStateException("Instance not stopped");
+            stop();
         }
 
-        main = (configBuilder.size() > 1) ? new TestingQuorumPeerMain() : new 
TestingZooKeeperMain();
+        // Set to a LATENT state so we can restart
+        state.set(State.LATENT);
+
+        main = (configBuilder.size() > 1) ? new TestingQuorumPeerMain()
+                : new TestingZooKeeperMain();
         start();
     }
 
-    public void     stop() throws IOException
+    public void stop() throws IOException
     {
-        if ( state.compareAndSet(State.STARTED, State.STOPPED) )
+        if (state.compareAndSet(State.STARTED, State.STOPPED))
         {
             main.close();
         }
     }
 
-    public InstanceSpec     getInstanceSpec()
+    public InstanceSpec getInstanceSpec()
     {
         return configBuilder.getInstanceSpec(thisInstanceIndex);
     }
@@ -105,10 +126,10 @@ public class TestingZooKeeperServer extends 
QuorumPeerMain implements Closeable
     {
         stop();
 
-        if ( state.compareAndSet(State.STOPPED, State.CLOSED) )
+        if (state.compareAndSet(State.STOPPED, State.CLOSED))
         {
-            InstanceSpec        spec = getInstanceSpec();
-            if ( spec.deleteDataDirectoryOnClose() )
+            InstanceSpec spec = getInstanceSpec();
+            if (spec.deleteDataDirectoryOnClose())
             {
                 DirectoryUtils.deleteRecursively(spec.getDataDirectory());
             }
@@ -117,29 +138,30 @@ public class TestingZooKeeperServer extends 
QuorumPeerMain implements Closeable
 
     public void start() throws Exception
     {
-        if ( !state.compareAndSet(State.LATENT, State.STARTED) )
+        if (!state.compareAndSet(State.LATENT, State.STARTED))
         {
             return;
         }
 
-        new Thread
-        (
-            new Runnable()
+        new Thread(new Runnable()
+        {
+            public void run()
             {
-                public void run()
+                try
+                {
+                    QuorumPeerConfig config = configBuilder
+                            .buildConfig(thisInstanceIndex);
+                    main.runFromConfig(config);
+                } catch (Exception e)
                 {
-                    try
-                    {
-                        QuorumPeerConfig config = 
configBuilder.buildConfig(thisInstanceIndex);
-                        main.runFromConfig(config);
-                    }
-                    catch ( Exception e )
-                    {
-                        logger.error(String.format("From testing server 
(random state: %s) for instance: %s", 
String.valueOf(configBuilder.isFromRandom()), getInstanceSpec()), e);
-                    }
+                    logger.error(
+                            String.format(
+                                    "From testing server (random state: %s) 
for instance: %s",
+                                    
String.valueOf(configBuilder.isFromRandom()),
+                                    getInstanceSpec()), e);
                 }
             }
-        ).start();
+        }).start();
 
         main.blockUntilStarted();
     }

Reply via email to