activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r679524 - in /activemq/sandbox/zookeeper/zookeeper-protocols/src: main/java/org/apache/zookeeper/protocols/ main/java/org/apache/zookeeper/protocols/doc-files/ test/java/org/apache/zookeeper/protocols/
Date Thu, 24 Jul 2008 20:13:14 GMT
Author: jstrachan
Date: Thu Jul 24 13:13:14 2008
New Revision: 679524

URL: http://svn.apache.org/viewvc?rev=679524&view=rev
Log:
add a first cut at a SharedLock implementation. Needs more work and more testing; but its
kinda working

Added:
    activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/SharedLockProtocol.java
  (with props)
    activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/doc-files/sharedLockProtocol.html
  (contents, props changed)
      - copied, changed from r679495, activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/doc-files/writeLockProtocol.html
    activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/SharedLockProtocolTest.java
  (contents, props changed)
      - copied, changed from r679495, activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/WriteLockProtocolTest.java
Modified:
    activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/WriteLockProtocol.java
    activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/WriteLockProtocolTest.java
    activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/ZooKeeperTestSupport.java

Added: activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/SharedLockProtocol.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/SharedLockProtocol.java?rev=679524&view=auto
==============================================================================
--- activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/SharedLockProtocol.java
(added)
+++ activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/SharedLockProtocol.java
Thu Jul 24 13:13:14 2008
@@ -0,0 +1,75 @@
+/**
+ *
+ * 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.zookeeper.protocols;
+
+import java.util.SortedSet;
+import java.util.Collections;
+
+/**
+ * A <a href="doc-files/sharedLockProtocol.html">protocol to implement a shared lock</a>
which works along with the
+ * exclusive {@link WriteLockProtocol}.
+ * <p/>
+ * <p/>
+ * You invoke {@link #acquire()} to start the process of grabbing the lock; you may get the
lock then or it may be some time later.
+ * <p/>
+ * You can register a {@link WhenOwnerListener} so that you are invoked when you get the
lock or when you loose ownership of it; otherwise you can ask if you have the lock
+ * by calling {@link #isOwner()}
+ *
+ * @version $Revision: 1.1 $
+ */
+public class SharedLockProtocol extends WriteLockProtocol {
+
+    public SharedLockProtocol(ZooKeeperFacade zookeeper, String dir, WhenOwnerListener whenOwnerListener)
{
+        super(zookeeper, dir, whenOwnerListener);
+    }
+
+    @Override
+    protected String getNodeNamePrefix() {
+        return "s-";
+    }
+
+    protected String getWriteLockNodeNamePrefix() {
+        return super.getNodeNamePrefix();
+    }
+
+    /**
+     * Find the ZNNode name to watch. This can change for different lock protocols but for
the
+     * <a href="doc-files/sharedLockProtocol.html">Shared Lock Protocol</a> it
is the last node that starts with the exclusive lock
+     * prefix returned by {@link #getWriteLockNodeNamePrefix()} or null if there is no exclusive
lock.
+     *
+     * @param lessThanMe the collection of available ZNode names that are less than this
ZNodes name
+     * @return the ZNode name that we should watch as part of the protocol or null if there
is no node to watch
+     */
+    @Override
+    protected String findZNodeToWatch(SortedSet<ZNodeName> lessThanMe) {
+        int size = lessThanMe.size();
+        if (size == 0) {
+            return null;
+        }
+        String writePrefix = getWriteLockNodeNamePrefix();
+        ZNodeName[] array = new ZNodeName[size];
+        lessThanMe.toArray(array);
+        for (int i = array.length - 1; i >= 0; i--) {
+            String name = array[i].getName();
+            if (name.startsWith(writePrefix)) {
+                return name;
+            }
+        }
+        return null;
+    }
+}

Propchange: activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/SharedLockProtocol.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/WriteLockProtocol.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/WriteLockProtocol.java?rev=679524&r1=679523&r2=679524&view=diff
==============================================================================
--- activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/WriteLockProtocol.java
(original)
+++ activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/WriteLockProtocol.java
Thu Jul 24 13:13:14 2008
@@ -46,7 +46,7 @@
     private String id;
     private ZNodeName idName;
     private String ownerId;
-    private String lastChildId;
+    private String watchChildId;
     private byte[] data = {0x12, 0x34};
     private final WhenOwnerDelegate whenOwnerListener;
 
@@ -102,7 +102,7 @@
                 do {
                     if (id == null) {
                         long sessionId = zookeeper.getSessionId();
-                        String prefix = "x-" + sessionId + "-";
+                        String prefix = getNodeNamePrefix() + sessionId + "-";
 
                         // lets try look up the current ID if we failed in the middle of
creating the znode
                         List<String> names = zookeeper.getChildren(dir, false);
@@ -143,16 +143,15 @@
                             }
 
                             SortedSet<ZNodeName> lessThanMe = sortedNames.headSet(idName);
-                            if (!lessThanMe.isEmpty()) {
-                                ZNodeName lastChildName = lessThanMe.last();
-                                lastChildId = lastChildName.getName();
+                            watchChildId = findZNodeToWatch(lessThanMe);
+                            if (watchChildId != null) {
 
                                 if (LOG.isDebugEnabled()) {
-                                    LOG.debug("watching less than me node: " + lastChildId);
+                                    LOG.debug("watching less than me node: " + watchChildId);
                                 }
 
 
-                                Stat stat = zookeeper.exists(lastChildId, new Watcher() {
+                                Stat stat = zookeeper.exists(watchChildId, new Watcher()
{
                                     public void process(WatcherEvent event) {
                                         // lets either become the leader or watch the new/updated
node
                                         LOG.debug("Watcher fired on path: " + event.getPath()
+ " state: " + event.getState() + " type " + event.getType());
@@ -168,7 +167,7 @@
                                 if (stat != null) {
                                     return Boolean.FALSE;
                                 } else {
-                                    LOG.warn("Could not find the stats for less than me:
" + lastChildName.getName());
+                                    LOG.warn("Could not find the stats for less than me:
" + watchChildId);
                                 }
                             } else {
                                 if (isIDTheOwnerID()) {
@@ -190,6 +189,21 @@
         return answer;
     }
 
+    /**
+     * Find the ZNNode name to watch. This can change for different lock protocols but for
the
+     *  <a href="doc-files/writeLockProtocol.html">Exclusive Lock Protocol</a>
it is the last node that is less
+     * than the current ZNode name
+     *
+     * @param lessThanMe the collection of available ZNode names that are less than this
ZNodes name
+     * @return the ZNode name that we should watch as part of the protocol or null if there
is no node to watch
+     */
+    protected String findZNodeToWatch(SortedSet<ZNodeName> lessThanMe) {
+        if (lessThanMe.isEmpty()) {
+            return null;
+        }
+        return lessThanMe.last().getName();
+    }
+
     public String getDir() {
         return dir;
     }
@@ -202,8 +216,8 @@
         return ownerId;
     }
 
-    public String getLastChildId() {
-        return lastChildId;
+    public String getWatchChildId() {
+        return watchChildId;
     }
 
 
@@ -221,6 +235,11 @@
     // Implementation methods
     //-------------------------------------------------------------------------
 
+    protected String getNodeNamePrefix() {
+        return "x-";
+    }
+
+
     @Override
     protected void doClose() {
         zookeeper.removeWhenOwnerListener(whenOwnerListener);

Copied: activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/doc-files/sharedLockProtocol.html
(from r679495, activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/doc-files/writeLockProtocol.html)
URL: http://svn.apache.org/viewvc/activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/doc-files/sharedLockProtocol.html?p2=activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/doc-files/sharedLockProtocol.html&p1=activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/doc-files/writeLockProtocol.html&r1=679495&r2=679524&rev=679524&view=diff
==============================================================================
--- activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/doc-files/writeLockProtocol.html
(original)
+++ activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/doc-files/sharedLockProtocol.html
Thu Jul 24 13:13:14 2008
@@ -1,10 +1,9 @@
 <html>
 <body>
-<h1>Write Lock Protocol</h1>
+<h1>Shared Lock Protocol</h1>
 
 <p>
-  The protocol devised by Runping Qi implements exclusive write locks; it can also be used
for Leader Election where
-  owning the lock is equivalent to being the leader.
+  The shared lock protocol is an extension to the <a href="writeLockProtocol.html">Write
Lock Protocol</a>
 </p>
 
 <p>
@@ -13,17 +12,17 @@
 
 <ol>
   <li>
-    id = create(".../locks/x-", SEQUENCE | EPHEMERAL)
+    id = create(".../locks/s-", SEQUENCE | EPHEMERAL)
   </li>
   <li>
     getChildren(Ò.../locks"/, false)
   </li>
   <li>
     sort the children in number order
-    if id is the 1st child, exit - you have the lock
+    if no children that start with x- before id, then exist - you have the lock
   </li>
   <li>
-    exists(name of last child before id, true) to detect if the znode before you goes away
+    exists(name of the last x- before id, true) to detect if the znode before you goes away
   </li>
   <li>
     if does not exist, goto 2)
@@ -36,30 +35,5 @@
   </li>
 </ol>
 
-<p>
-  First of all, when you create child nodes of the node that holds the leadership bids,
-  you must create them with the EPHEMERAL and SEQUENCE flag.
-  ZooKeeper guarantees to give you an ephemeral node named uniquely and with a sequence number
larger by at least one
-  than any previously created node in the sequence.
-  You provide a prefix, like "L_" or your own choice, and ZooKeeper creates nodes named "L_23",
"L_24", etc.
-  The sequence number starts at 0 and increases monotonously.
-</p>
-
-<p>
-  Once you've placed your leadership bid, you search backwards from the sequence number of
*your* node to see if there
-  are any preceding (in terms of the sequence number) nodes.
-  When you find one, you place a watch on it and wait for it to disappear.
-  When you get the watch notification, you search again, until you do not find a preceding
node, then you know you're
-  the leader.
-</p>
-
-<p>
-  This protocol guarantees that there is at any time only one node that thinks it is the
leader.
-  But it does not disseminate information about who is the leader. If you want everyone to
know who is the leader,
-  you can have an additional Znode whose value is the name of the current leader
-  (or some identifying information on how to contact the leader, etc.).
-  Note that this cannot be done atomically, so by the time other nodes find out who the leader
is,
-  the leadership may already have passed on to a different node.
-</p>
 </body>
 </html>
\ No newline at end of file

Propchange: activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/doc-files/sharedLockProtocol.html
------------------------------------------------------------------------------
    svn:mergeinfo = 

Propchange: activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/doc-files/sharedLockProtocol.html
------------------------------------------------------------------------------
    svn:mime-type = text/html

Copied: activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/SharedLockProtocolTest.java
(from r679495, activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/WriteLockProtocolTest.java)
URL: http://svn.apache.org/viewvc/activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/SharedLockProtocolTest.java?p2=activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/SharedLockProtocolTest.java&p1=activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/WriteLockProtocolTest.java&r1=679495&r2=679524&rev=679524&view=diff
==============================================================================
--- activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/WriteLockProtocolTest.java
(original)
+++ activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/SharedLockProtocolTest.java
Thu Jul 24 13:13:14 2008
@@ -21,141 +21,89 @@
 import org.apache.zookeeper.Watcher;
 import org.apache.zookeeper.proto.WatcherEvent;
 
+import java.io.IOException;
 import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.TimeUnit;
 
 /**
  * @version $Revision: 1.1 $
  */
-public class WriteLockProtocolTest extends ZooKeeperTestSupport {
-    private static final Logger LOG = Logger.getLogger(WriteLockProtocolTest.class);
+public class SharedLockProtocolTest extends ZooKeeperTestSupport {
+    private static final Logger LOG = Logger.getLogger(SharedLockProtocolTest.class);
 
     protected int sessionTimeout = 10 * 1000;
     protected String dir = "/" + getClass().getName();
-    protected WriteLockProtocol[] nodes;
+
+    protected WriteLockProtocol[] writeLockNodes;
+    protected SharedLockProtocol[] sharedLockNodes;
+    protected int writeLockNodeCount = 3;
+    protected int sharedLockNodeCount = 10;
+
     protected CountDownLatch latch = new CountDownLatch(1);
     protected boolean restartServer = true;
-    protected boolean killLeader = true;
-
-    // TODO set this value to false when the patch for ZOOKEEPER-63 is applied!
-    protected boolean workAroundClosingLastZNodeFails = true;
 
     public void testRun() throws Exception {
-        runTest(3);
-    }
-
-    protected void runTest(int count) throws Exception {
-        nodes = new WriteLockProtocol[count];
-        for (int i = 0; i < count; i++) {
-            final int nodeId = i;
-            ZooKeeperFacade keeper = new ZooKeeperFacade("127.0.0.1:2181", sessionTimeout,
new Watcher() {
-                public void process(WatcherEvent event) {
-                    LOG.info("ZooKeeper Event for znode: " + nodeId + " on path: " + event.getPath()
+ " state: " + event.getState() + " type: " + event.getType());
-                }
-            });
-            WriteLockProtocol leader = new WriteLockProtocol(keeper, dir, new WhenOwnerListener()
{
-                public void whenOwner() {
-                    LOG.info(">>>>>>>>>> OWNER ZNode " + nodeId);
-                    latch.countDown();
-                }
-
-                public void whenNotOwner() {
-                    LOG.info("<<<<<<<<<< NOT OWNER ZNode "
+ nodeId);
-                }
-            });
-            nodes[i] = leader;
+        sharedLockNodes = new SharedLockProtocol[sharedLockNodeCount];
+        for (int i = 0; i < sharedLockNodeCount; i++) {
+            sharedLockNodes[i] = createSharedLockNode(i);
+        }
 
-            leader.acquire();
+        writeLockNodes = new WriteLockProtocol[writeLockNodeCount];
+        for (int i = 0; i < writeLockNodeCount; i++) {
+            writeLockNodes[i] = createWriteLockNode(i);
         }
 
-        // lets wait for any previous leaders to die and one of our new
-        // nodes to become the new leader
-        latch.await(30, TimeUnit.SECONDS);
-
-        WriteLockProtocol first = nodes[0];
-        dumpNodes(count);
-
-        // lets assert that the first election is the leader
-        assertTrue("The first znode should be the leader " + first.getId(), first.isIDTheOwnerID());
-
-        for (int i = 1; i < count; i++) {
-            WriteLockProtocol node = nodes[i];
-            assertFalse("Node should not be the leader " + node.getId(), node.isIDTheOwnerID());
+        // now lets try acquire some read locks
+        for (int i = 0; i < sharedLockNodeCount; i++) {
+            LOG.info("Attempting to acquire sharedNode: " + i);
+            sharedLockNodes[i].acquire();
+
+            dumpNodes(sharedLockNodes, "shared");
+            dumpNodes(writeLockNodes, "write");
         }
+    }
 
-        if (count > 1) {
-            if (killLeader) {
-                System.out.println("Now killing the leader");
-                // now lets kill the leader
-                latch = new CountDownLatch(1);
-                first.unlock();
-                latch.await(30, TimeUnit.SECONDS);
-
-                //Thread.sleep(10000);
-                WriteLockProtocol second = nodes[1];
-
-                dumpNodes(count);
-
-                // lets assert that the first election is the leader
-                assertTrue("The second znode should be the leader " + second.getId(), second.isIDTheOwnerID());
-
-                for (int i = 2; i < count; i++) {
-                    WriteLockProtocol node = nodes[i];
-                    assertFalse("Node should not be the leader " + node.getId(), node.isIDTheOwnerID());
-                }
+    private WriteLockProtocol createWriteLockNode(final int nodeId) throws IOException {
+        ZooKeeperFacade keeper = new ZooKeeperFacade("127.0.0.1:2181", sessionTimeout, new
Watcher() {
+            public void process(WatcherEvent event) {
+                LOG.info("ZooKeeper Event for write znode: " + nodeId + " on path: " + event.getPath()
+ " state: " + event.getState() + " type: " + event.getType());
+            }
+        });
+        return new WriteLockProtocol(keeper, dir, new WhenOwnerListener() {
+            public void whenOwner() {
+                LOG.info(">>>>>>>>>> OWNER Write ZNode " +
nodeId);
+                latch.countDown();
             }
 
-
-            if (restartServer) {
-                // now lets stop the server
-                System.out.println("Now stopping the server");
-                stopServer();
-                Thread.sleep(10000);
-
-                // TODO lets assert that we are no longer the leader
-                dumpNodes(count);
-
-                System.out.println("Starting the server");
-                startServer();
-                Thread.sleep(10000);
-
-                for (int i = 0; i < count - 1; i++) {
-                    System.out.println("Calling acquire for node: " + i);
-                    nodes[i].acquire();
-                }
-                dumpNodes(count);
-                System.out.println("Now closing down...");
+            public void whenNotOwner() {
+                LOG.info("<<<<<<<<<< NOT OWNER Write ZNode
" + nodeId);
             }
-        }
+        });
     }
 
-    protected void dumpNodes(int count) {
-        for (int i = 0; i < count; i++) {
-            WriteLockProtocol node = nodes[i];
-            System.out.println("node: " + i + " id: " + node.getId() + " is leader: " + node.isIDTheOwnerID());
-        }
+    protected SharedLockProtocol createSharedLockNode(final int nodeId) throws IOException
{
+        ZooKeeperFacade keeper = new ZooKeeperFacade("127.0.0.1:2181", sessionTimeout, new
Watcher() {
+            public void process(WatcherEvent event) {
+                LOG.info("ZooKeeper Event for shared znode: " + nodeId + " on path: " + event.getPath()
+ " state: " + event.getState() + " type: " + event.getType());
+            }
+        });
+        return new SharedLockProtocol(keeper, dir, new WhenOwnerListener() {
+            public void whenOwner() {
+                LOG.info(">>>>>>>>>> OWNER shared ZNode " +
nodeId);
+                latch.countDown();
+            }
+
+            public void whenNotOwner() {
+                LOG.info("<<<<<<<<<< NOT OWNER shared ZNode
" + nodeId);
+            }
+        });
     }
 
+
     @Override
     protected void tearDown() throws Exception {
-        if (nodes != null) {
-            for (int i = 0; i < nodes.length; i++) {
-                WriteLockProtocol node = nodes[i];
-                if (node != null) {
-                    System.out.println("Closing node: " + i);
-                    node.close();
-                    if (workAroundClosingLastZNodeFails && i == nodes.length - 1)
{
-                        System.out.println("Not closing zookeeper: " + i + " due to bug!");
-                    } else {
-                        System.out.println("Closing zookeeper: " + i);
-                        node.getZookeeper().close();
-                        System.out.println("Closed zookeeper: " + i);
-                    }
-                }
-            }
-        }
-        System.out.println("Now lets stop the server");
+        closeLockNodes(writeLockNodes);
+        closeLockNodes(sharedLockNodes);
         super.tearDown();
 
     }
-}
+}
\ No newline at end of file

Propchange: activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/SharedLockProtocolTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/SharedLockProtocolTest.java
------------------------------------------------------------------------------
    svn:mergeinfo = 

Modified: activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/WriteLockProtocolTest.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/WriteLockProtocolTest.java?rev=679524&r1=679523&r2=679524&view=diff
==============================================================================
--- activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/WriteLockProtocolTest.java
(original)
+++ activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/WriteLockProtocolTest.java
Thu Jul 24 13:13:14 2008
@@ -37,9 +37,6 @@
     protected boolean restartServer = true;
     protected boolean killLeader = true;
 
-    // TODO set this value to false when the patch for ZOOKEEPER-63 is applied!
-    protected boolean workAroundClosingLastZNodeFails = true;
-
     public void testRun() throws Exception {
         runTest(3);
     }
@@ -138,24 +135,9 @@
 
     @Override
     protected void tearDown() throws Exception {
-        if (nodes != null) {
-            for (int i = 0; i < nodes.length; i++) {
-                WriteLockProtocol node = nodes[i];
-                if (node != null) {
-                    System.out.println("Closing node: " + i);
-                    node.close();
-                    if (workAroundClosingLastZNodeFails && i == nodes.length - 1)
{
-                        System.out.println("Not closing zookeeper: " + i + " due to bug!");
-                    } else {
-                        System.out.println("Closing zookeeper: " + i);
-                        node.getZookeeper().close();
-                        System.out.println("Closed zookeeper: " + i);
-                    }
-                }
-            }
-        }
-        System.out.println("Now lets stop the server");
+        closeLockNodes(nodes);
         super.tearDown();
 
     }
+
 }

Modified: activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/ZooKeeperTestSupport.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/ZooKeeperTestSupport.java?rev=679524&r1=679523&r2=679524&view=diff
==============================================================================
--- activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/ZooKeeperTestSupport.java
(original)
+++ activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/ZooKeeperTestSupport.java
Thu Jul 24 13:13:14 2008
@@ -37,6 +37,9 @@
     protected int zkPort = 2181;
     protected NIOServerCnxn.Factory serverFactory;
 
+    // TODO set this value to false when the patch for ZOOKEEPER-63 is applied!
+    protected boolean workAroundClosingLastZNodeFails = true;
+
     @Override
     protected void setUp() throws Exception {
         startServer();
@@ -46,6 +49,7 @@
 
     @Override
     protected void tearDown() throws Exception {
+        System.out.println("Now lets stop the ZooKeeper server");
         stopServer();
         super.tearDown();
     }
@@ -86,4 +90,30 @@
             server = null;
         }
     }
+
+    protected void dumpNodes(WriteLockProtocol[] nodes, String type) {
+        for (int i = 0, count = nodes.length; i < count; i++) {
+            WriteLockProtocol node = nodes[i];
+            System.out.println(type + ": " + i + " id: " + node.getId() + " is leader: "
+ node.isIDTheOwnerID());
+        }
+    }
+
+    protected void closeLockNodes(WriteLockProtocol[] nodes) throws InterruptedException
{
+        if (nodes != null) {
+            for (int i = 0; i < nodes.length; i++) {
+                WriteLockProtocol node = nodes[i];
+                if (node != null) {
+                    System.out.println("Closing node: " + i);
+                    node.close();
+                    if (workAroundClosingLastZNodeFails && i == nodes.length - 1)
{
+                        System.out.println("Not closing zookeeper: " + i + " due to bug!");
+                    } else {
+                        System.out.println("Closing zookeeper: " + i);
+                        node.getZookeeper().close();
+                        System.out.println("Closed zookeeper: " + i);
+                    }
+                }
+            }
+        }
+    }
 }



Mime
View raw message