activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r679435 - in /activemq/sandbox/zookeeper/zookeeper-protocols/src: main/java/org/apache/zookeeper/protocols/ main/java/org/apache/zookeeper/protocols/locks/ test/java/org/apache/zookeeper/protocols/ test/java/org/apache/zookeeper/protocols/l...
Date Thu, 24 Jul 2008 15:17:29 GMT
Author: jstrachan
Date: Thu Jul 24 08:17:28 2008
New Revision: 679435

URL: http://svn.apache.org/viewvc?rev=679435&view=rev
Log:
added initial spike at ZOOKEEPER-88 to implement a java.util.concurrent.locks.Lock using the
WriteLockProtocol

Added:
    activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/locks/
    activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/locks/RuntimeKeeperException.java
  (with props)
    activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/locks/WriteLock.java
  (with props)
    activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/locks/package.html
      - copied, changed from r679387, activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/package.html
    activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/locks/
    activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/locks/WriteLockTest.java
  (with props)
    activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/locks/WriteLockWithConcurrentLocalThreadsTest.java
  (with props)
Removed:
    activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/WriteLock.java
    activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/WriteLockTest.java
Modified:
    activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/ProtocolSupport.java
    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

Modified: activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/ProtocolSupport.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/ProtocolSupport.java?rev=679435&r1=679434&r2=679435&view=diff
==============================================================================
--- activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/ProtocolSupport.java
(original)
+++ activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/ProtocolSupport.java
Thu Jul 24 08:17:28 2008
@@ -20,13 +20,12 @@
 import org.apache.log4j.Logger;
 import org.apache.zookeeper.KeeperException;
 import org.apache.zookeeper.ZooDefs;
-import org.apache.zookeeper.ZooKeeper;
 import org.apache.zookeeper.data.ACL;
 import org.apache.zookeeper.data.Stat;
 
+import java.io.IOException;
 import java.util.List;
 import java.util.concurrent.atomic.AtomicBoolean;
-import java.io.IOException;
 
 /**
  * A base class for protocol implementations which provides a number of higher level helper
methods
@@ -137,21 +136,30 @@
      * @param flags
      */
     protected void ensureExists(final String path, final byte[] data, final List<ACL>
acl, final int flags) {
-        try {
-            retryOperation(new ZooKeeperOperation() {
-                public Object execute() throws KeeperException, InterruptedException {
-                    Stat stat = zookeeper.exists(path, false);
-                    if (stat != null) {
-                        return null;
-                    }
-                    return zookeeper.create(path, data, acl, flags);
+        ZooKeeperOperation operation = new ZooKeeperOperation() {
+            public Object execute() throws KeeperException, InterruptedException {
+                Stat stat = zookeeper.exists(path, false);
+                if (stat != null) {
+                    return null;
                 }
-            });
-        } catch (KeeperException e) {
-            LOG.warn("Caught: " + e, e);
-        } catch (InterruptedException e) {
-            LOG.warn("Caught: " + e, e);
+                return zookeeper.create(path, data, acl, flags);
+            }
+        };
+
+        boolean nodeExistsException = false;
+        do {
+            try {
+                retryOperation(operation);
+            } catch (KeeperException.NodeExistsException e) {
+                LOG.debug("Node exists so going to retry one more time: " + e, e);
+                nodeExistsException = true;
+            } catch (KeeperException e) {
+                LOG.warn("Caught: " + e, e);
+            } catch (InterruptedException e) {
+                LOG.warn("Caught: " + e, e);
+            }
         }
+        while (nodeExistsException);
     }
 
     /**

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=679435&r1=679434&r2=679435&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 08:17:28 2008
@@ -35,7 +35,7 @@
  * 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 Runnable so that you are invoked when you get the lock; otherwise you
can ask if you have the lock
- * by calling {@link #isOwner()}
+ * by calling {@link #isIDTheOwnerID()}
  *
  * @version $Revision: 1.1 $
  */
@@ -48,7 +48,7 @@
     private String ownerId;
     private String lastChildId;
     private byte[] data = {0x12, 0x34};
-    private final WhenOwnerListener whenOwnerListener;
+    private final WhenOwnerDelegate whenOwnerListener;
 
     public WriteLockProtocol(ZooKeeperFacade zookeeper, String dir, WhenOwnerListener whenOwnerListener)
{
         super(zookeeper);
@@ -91,7 +91,7 @@
      * Note that the exclusive lock may be acquired some time later after this method has
been invoked
      * due to the current lock owner going away.
      */
-    public boolean acquire() throws KeeperException, InterruptedException {
+    public synchronized boolean acquire() throws KeeperException, InterruptedException {
         if (isClosed()) {
             return false;
         }
@@ -171,10 +171,8 @@
                                     LOG.warn("Could not find the stats for less than me:
" + lastChildName.getName());
                                 }
                             } else {
-                                if (isOwner()) {
-                                    if (whenOwnerListener != null) {
-                                        whenOwnerListener.whenOwner();
-                                    }
+                                if (isIDTheOwnerID()) {
+                                    whenOwnerListener.whenOwner();
                                     return Boolean.TRUE;
                                 }
                             }
@@ -208,18 +206,18 @@
         return lastChildId;
     }
 
+
     /**
      * Returns true if this node is the owner of the lock (or the leader)
      */
     public boolean isOwner() {
-        return id != null && ownerId != null && id.equals(ownerId);
+        return whenOwnerListener.isOwner();
     }
 
     public WhenOwnerListener getWhenOwnerListener() {
         return whenOwnerListener;
     }
 
-
     // Implementation methods
     //-------------------------------------------------------------------------
 
@@ -229,4 +227,7 @@
         unlock();
     }
 
+    protected boolean isIDTheOwnerID() {
+        return id != null && ownerId != null && id.equals(ownerId);
+    }
 }

Added: activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/locks/RuntimeKeeperException.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/locks/RuntimeKeeperException.java?rev=679435&view=auto
==============================================================================
--- activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/locks/RuntimeKeeperException.java
(added)
+++ activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/locks/RuntimeKeeperException.java
Thu Jul 24 08:17:28 2008
@@ -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.zookeeper.protocols.locks;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+public class RuntimeKeeperException extends RuntimeException {
+    public RuntimeKeeperException() {
+    }
+
+    public RuntimeKeeperException(String s) {
+        super(s);
+    }
+
+    public RuntimeKeeperException(String s, Throwable throwable) {
+        super(s, throwable);
+    }
+
+    public RuntimeKeeperException(Throwable throwable) {
+        super(throwable);
+    }
+}

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

Added: activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/locks/WriteLock.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/locks/WriteLock.java?rev=679435&view=auto
==============================================================================
--- activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/locks/WriteLock.java
(added)
+++ activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/locks/WriteLock.java
Thu Jul 24 08:17:28 2008
@@ -0,0 +1,177 @@
+/**
+ *
+ * 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.locks;
+
+import org.apache.log4j.Logger;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.protocols.WhenOwnerListener;
+import org.apache.zookeeper.protocols.WriteLockProtocol;
+import org.apache.zookeeper.protocols.ZooKeeperFacade;
+
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.Condition;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+/**
+ * An implementation of {@link Lock} which locks this instance against the distributed ZooKeeper
directory name.
+ *
+ * @version $Revision: 1.1 $
+ */
+public class WriteLock implements Lock {
+    private static final Logger LOG = Logger.getLogger(WriteLock.class);
+
+    private final WriteLockProtocol protocol;
+    private final ReentrantLock conditionLock = new ReentrantLock(false);
+    private final Condition ownerCondition = conditionLock.newCondition();
+    private final ReentrantLock threadLock = new ReentrantLock(false);
+    private long timeout = 30 * 1000L;
+    private TimeUnit timeUnit = TimeUnit.MILLISECONDS;
+
+    public WriteLock(ZooKeeperFacade zookeeper, String dir) {
+        this.protocol = new WriteLockProtocol(zookeeper, dir, new WhenOwnerListener() {
+            public void whenOwner() {
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Owner of: " + protocol.getDir());
+                }
+                fireOwnerCondition();
+            }
+
+            public void whenNotOwner() {
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Not owner of: " + protocol.getDir());
+                }
+                fireOwnerCondition();
+            }
+        });
+    }
+
+    protected void fireOwnerCondition() {
+        conditionLock.lock();
+        try {
+            ownerCondition.signal();
+        }
+        finally {
+            conditionLock.unlock();
+        }
+    }
+
+    public void lock() {
+        while (true) {
+            try {
+                lockInterruptibly();
+                return;
+            } catch (InterruptedException e) {
+                LOG.info("Interupted: " + e, e);
+            }
+        }
+
+    }
+
+    public void lockInterruptibly() throws InterruptedException {
+        while (true) {
+            if (tryLock(timeout, timeUnit)) {
+                return;
+            }
+        }
+    }
+
+    public boolean tryLock() {
+        if (threadLock.tryLock()) {
+            while (true) {
+                try {
+                    if (protocol.isOwner()) {
+                        return true;
+                    }
+                    return protocol.acquire();
+                } catch (KeeperException e) {
+                    threadLock.unlock();
+                    throw new RuntimeKeeperException("Failed to acquire remote lock: " +
e, e);
+                } catch (InterruptedException e) {
+                    LOG.debug("Caught: " + e, e);
+                }
+            }
+        }
+        return false;
+    }
+
+    public boolean tryLock(long timeout, TimeUnit timeUnit) throws InterruptedException {
+        // TODO this implementation will typically wait too long!
+        boolean unlockThreadLock = false;
+        try {
+            if (threadLock.tryLock(timeout, timeUnit)) {
+                if (protocol.isOwner()) {
+                    return true;
+                }
+                unlockThreadLock = true;
+                try {
+                    if (protocol.acquire()) {
+                        unlockThreadLock = false;
+                        return true;
+                    }
+                } catch (KeeperException e) {
+                    throw new RuntimeKeeperException("Failed to acquire remote lock: " +
e, e);
+                }
+
+                // now lets wait for the lock condition
+
+                // Note lest make sure we unlock the threadLock
+                // apart from if we manage to
+                conditionLock.lock();
+                try {
+                    ownerCondition.await(timeout, timeUnit);
+                    if (protocol.isOwner()) {
+                        unlockThreadLock = false;
+                        return true;
+                    }
+                }
+                finally {
+                    conditionLock.unlock();
+                }
+            }
+            return false;
+        }
+        finally {
+            if (unlockThreadLock) {
+                threadLock.unlock();
+            }
+        }
+    }
+
+    public void unlock() {
+        try {
+            if (protocol.isOwner()) {
+                protocol.unlock();
+            }
+        }
+        finally {
+            threadLock.unlock();
+        }
+    }
+
+    public Condition newCondition() {
+        throw new UnsupportedOperationException("Conditions are not supported!");
+    }
+
+    // Properties
+    //-------------------------------------------------------------------------
+
+    public WriteLockProtocol getProtocol() {
+        return protocol;
+    }
+}

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

Copied: activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/locks/package.html
(from r679387, activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/package.html)
URL: http://svn.apache.org/viewvc/activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/locks/package.html?p2=activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/locks/package.html&p1=activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/package.html&r1=679387&r2=679435&rev=679435&view=diff
==============================================================================
--- activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/package.html
(original)
+++ activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/locks/package.html
Thu Jul 24 08:17:28 2008
@@ -3,9 +3,6 @@
 </head>
 
 <body>
-Contains a collection of higher level protocols implemented on top of the core ZooKeeper
API such as the
-<a  href="WriteLock.html">WriteLock protocol</a> for exclusive locks or electing
a leader along with a useful
-base class <a  href="ProtocolSupport.html">ProtocolSupport</a> and with a helper
class
-<a  href="ZooKeeperFacade.html">ZooKeeperFacade</a> for dealing with reconnecting
after session expiration errors.
+Distributed implementations of the <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/locks/package-summary.html">java.util.concurrent.locks
package</a>
 </body>
 </html>
\ No newline at end of file

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=679435&r1=679434&r2=679435&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 08:17:28 2008
@@ -34,11 +34,11 @@
     protected String dir = "/" + getClass().getName();
     protected WriteLockProtocol[] nodes;
     protected CountDownLatch latch = new CountDownLatch(1);
-    private boolean restartServer = true;
+    protected boolean restartServer = true;
+    protected boolean killLeader = true;
 
     // TODO set this value to false when the patch for ZOOKEEPER-63 is applied!
-    private boolean workAroundClosingLastZNodeFails = true;
-    private boolean killLeader = true;
+    protected boolean workAroundClosingLastZNodeFails = true;
 
     public void testRun() throws Exception {
         runTest(3);
@@ -76,11 +76,11 @@
         dumpNodes(count);
 
         // lets assert that the first election is the leader
-        assertTrue("The first znode should be the leader " + first.getId(), first.isOwner());
+        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.isOwner());
+            assertFalse("Node should not be the leader " + node.getId(), node.isIDTheOwnerID());
         }
 
         if (count > 1) {
@@ -97,11 +97,11 @@
                 dumpNodes(count);
 
                 // lets assert that the first election is the leader
-                assertTrue("The second znode should be the leader " + second.getId(), second.isOwner());
+                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.isOwner());
+                    assertFalse("Node should not be the leader " + node.getId(), node.isIDTheOwnerID());
                 }
             }
 
@@ -132,7 +132,7 @@
     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.isOwner());
+            System.out.println("node: " + i + " id: " + node.getId() + " is leader: " + node.isIDTheOwnerID());
         }
     }
 

Added: activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/locks/WriteLockTest.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/locks/WriteLockTest.java?rev=679435&view=auto
==============================================================================
--- activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/locks/WriteLockTest.java
(added)
+++ activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/locks/WriteLockTest.java
Thu Jul 24 08:17:28 2008
@@ -0,0 +1,120 @@
+/**
+ *
+ * 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.locks;
+
+import org.apache.log4j.Logger;
+import org.apache.zookeeper.Watcher;
+import org.apache.zookeeper.proto.WatcherEvent;
+import org.apache.zookeeper.protocols.WriteLockProtocol;
+import org.apache.zookeeper.protocols.ZooKeeperFacade;
+import org.apache.zookeeper.protocols.ZooKeeperTestSupport;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+public class WriteLockTest extends ZooKeeperTestSupport {
+    private static final Logger LOG = Logger.getLogger(WriteLockTest.class);
+
+    protected int sessionTimeout = 10 * 1000;
+    protected String dir = "/" + getClass().getName();
+    protected WriteLock[] nodes;
+    protected CountDownLatch latch = new CountDownLatch(1);
+    protected boolean restartServer = true;
+
+    protected int clientCount = 3;
+    protected int threadCount = 1;
+
+    // TODO set this value to false when the patch for ZOOKEEPER-63 is applied!
+    protected boolean workAroundClosingLastZNodeFails = true;
+
+    public void testRun() throws Exception {
+        int total = clientCount * threadCount;
+        latch = new CountDownLatch(total);
+
+        nodes = new WriteLock[clientCount];
+        for (int i = 0; i < clientCount; 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());
+                }
+            });
+            WriteLock protocol = new WriteLock(keeper, dir);
+            nodes[i] = protocol;
+        }
+
+        for (int i = 0; i < clientCount; i++) {
+            createThread(i, nodes[i]);
+        }
+
+        LOG.info("Expecting " + total + " latch countdowns");
+        
+        // now lets try wait until they all get the lock
+        latch.await(100, TimeUnit.SECONDS);
+        assertEquals("latch count should have counted down from: " + total, 0, latch.getCount());
+    }
+
+    protected void createThread(final int nodeId, final WriteLock lock) {
+        for (int i = 0; i < threadCount; i++) {
+            Thread thread = new Thread("Node: " + nodeId + " thread: " + i) {
+                @Override
+                public void run() {
+                    try {
+                        LOG.info("-------- " + this + " attempting to get the lock");
+                        lock.lock();
+                        LOG.info(">>>>>>>> " + this + " LOCKED -
now unlocking");
+                        lock.unlock();
+                        LOG.info("<<<<<<<< " + this + " unlocked
the latch is: " + latch.getCount());
+                        latch.countDown();
+                    } catch (Throwable e) {
+                        LOG.error(this + " caught: " + e, e);
+                    }
+                }
+            };
+            thread.start();
+        }
+    }
+
+
+    @Override
+    protected void tearDown() throws Exception {
+        if (nodes != null) {
+            for (int i = 0; i < nodes.length; i++) {
+                WriteLock lock = nodes[i];
+                if (lock != null) {
+                    WriteLockProtocol node = lock.getProtocol();
+                    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");
+        super.tearDown();
+
+    }
+}

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

Added: activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/locks/WriteLockWithConcurrentLocalThreadsTest.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/locks/WriteLockWithConcurrentLocalThreadsTest.java?rev=679435&view=auto
==============================================================================
--- activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/locks/WriteLockWithConcurrentLocalThreadsTest.java
(added)
+++ activemq/sandbox/zookeeper/zookeeper-protocols/src/test/java/org/apache/zookeeper/protocols/locks/WriteLockWithConcurrentLocalThreadsTest.java
Thu Jul 24 08:17:28 2008
@@ -0,0 +1,32 @@
+/**
+ *
+ * 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.locks;
+
+/**
+ * Similar to {@link WriteLockTest} but lets use multiple concurrent threads in the same
client
+ * using the same lock (i.e. to test the local in-JVM capability of the distributed lock).
+ *
+ * @version $Revision: 1.1 $
+ */
+public class WriteLockWithConcurrentLocalThreadsTest extends WriteLockTest {
+    @Override
+    protected void setUp() throws Exception {
+        threadCount = 5;
+        super.setUp();
+    }
+}

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



Mime
View raw message