Author: kwright
Date: Wed Nov 13 00:31:53 2013
New Revision: 1541337

URL: http://svn.apache.org/r1541337
Log:
Fix a number of problems, with both the test and the locking code.

Modified:
    
manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/ZooKeeperConnection.java
    
manifoldcf/branches/CONNECTORS-13/framework/core/src/test/java/org/apache/manifoldcf/core/lockmanager/TestZooKeeperLocks.java
    
manifoldcf/branches/CONNECTORS-13/framework/core/src/test/java/org/apache/manifoldcf/core/lockmanager/ZooKeeperInstance.java

Modified: 
manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/ZooKeeperConnection.java
URL: 
http://svn.apache.org/viewvc/manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/ZooKeeperConnection.java?rev=1541337&r1=1541336&r2=1541337&view=diff
==============================================================================
--- 
manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/ZooKeeperConnection.java
 (original)
+++ 
manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/ZooKeeperConnection.java
 Wed Nov 13 00:31:53 2013
@@ -238,15 +238,26 @@ public class ZooKeeperConnection
   {
     try
     {
-      try
+      if (data == null)
       {
-        zookeeper.create(resourcePath, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, 
CreateMode.PERSISTENT);
+        try
+        {
+          zookeeper.delete(resourcePath, -1);
+        }
+        catch (KeeperException.NoNodeException e)
+        {
+        }
       }
-      catch (KeeperException e)
+      else
       {
-        if (!(e instanceof KeeperException.NodeExistsException))
-          throw e;
-        zookeeper.setData(resourcePath, data, -1);
+        try
+        {
+          zookeeper.create(resourcePath, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, 
CreateMode.PERSISTENT);
+        }
+        catch (KeeperException.NodeExistsException e)
+        {
+          zookeeper.setData(resourcePath, data, -1);
+        }
       }
     }
     catch (KeeperException e)
@@ -260,12 +271,17 @@ public class ZooKeeperConnection
   {
     try
     {
-      zookeeper.create(flagPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, 
CreateMode.PERSISTENT);
+      try
+      {
+        zookeeper.create(flagPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, 
CreateMode.PERSISTENT);
+      }
+      catch (KeeperException.NodeExistsException e)
+      {
+      }
     }
     catch (KeeperException e)
     {
-      if (!(e instanceof KeeperException.NodeExistsException))
-        throw new ManifoldCFException(e.getMessage(),e);
+      throw new ManifoldCFException(e.getMessage(),e);
     }
   }
 
@@ -274,12 +290,16 @@ public class ZooKeeperConnection
   {
     try
     {
-      zookeeper.delete(flagPath,-1);
+      try
+      {
+        zookeeper.delete(flagPath,-1);
+      }
+      catch (KeeperException.NoNodeException e)
+      {
+      }
     }
     catch (KeeperException e)
     {
-      if (!(e instanceof KeeperException.NoNodeException))
-        return;
       throw new ManifoldCFException(e.getMessage(),e);
     }
   }
@@ -316,17 +336,24 @@ public class ZooKeeperConnection
   protected String createSequentialChild(String mainNode, String childPrefix)
     throws KeeperException, InterruptedException
   {
-    try
-    {
-      zookeeper.create(mainNode, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, 
CreateMode.PERSISTENT);
-    }
-    catch (KeeperException e)
+    // Because zookeeper is so slow, AND reports all exceptions to the log, we 
do the minimum.
+    while (true)
     {
-      if (!(e instanceof KeeperException.NodeExistsException))
-        throw e;
+      try
+      {
+        return zookeeper.create(mainNode + "/" + childPrefix, new byte[0], 
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
+      }
+      catch (KeeperException.NoNodeException e)
+      {
+        try
+        {
+          zookeeper.create(mainNode, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, 
CreateMode.PERSISTENT);
+        }
+        catch (KeeperException.NodeExistsException e2)
+        {
+        }
+      }
     }
-    
-    return zookeeper.create(mainNode + "/" + childPrefix, new byte[0], 
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
   }
 
   /** Watcher class for zookeeper, so we get notified about zookeeper events. 
*/

Modified: 
manifoldcf/branches/CONNECTORS-13/framework/core/src/test/java/org/apache/manifoldcf/core/lockmanager/TestZooKeeperLocks.java
URL: 
http://svn.apache.org/viewvc/manifoldcf/branches/CONNECTORS-13/framework/core/src/test/java/org/apache/manifoldcf/core/lockmanager/TestZooKeeperLocks.java?rev=1541337&r1=1541336&r2=1541337&view=diff
==============================================================================
--- 
manifoldcf/branches/CONNECTORS-13/framework/core/src/test/java/org/apache/manifoldcf/core/lockmanager/TestZooKeeperLocks.java
 (original)
+++ 
manifoldcf/branches/CONNECTORS-13/framework/core/src/test/java/org/apache/manifoldcf/core/lockmanager/TestZooKeeperLocks.java
 Wed Nov 13 00:31:53 2013
@@ -18,6 +18,7 @@
 */
 package org.apache.manifoldcf.core.lockmanager;
 
+import org.apache.manifoldcf.core.interfaces.*;
 import java.io.*;
 import java.util.*;
 import java.util.concurrent.atomic.*;
@@ -83,6 +84,58 @@ public class TestZooKeeperLocks extends 
     
   }
   
+  protected static void enterReadLock(LockObject lo)
+    throws Exception
+  {
+    try
+    {
+      lo.enterReadLock();
+    }
+    catch (ExpiredObjectException e)
+    {
+      throw new ManifoldCFException("Unexpected exception: "+e.getMessage(),e);
+    }
+  }
+  
+  protected static void leaveReadLock(LockObject lo)
+    throws Exception
+  {
+    try
+    {
+      lo.leaveReadLock();
+    }
+    catch (ExpiredObjectException e)
+    {
+      throw new ManifoldCFException("Unexpected exception: "+e.getMessage(),e);
+    }
+  }
+
+  protected static void enterWriteLock(LockObject lo)
+    throws Exception
+  {
+    try
+    {
+      lo.enterWriteLock();
+    }
+    catch (ExpiredObjectException e)
+    {
+      throw new ManifoldCFException("Unexpected exception: "+e.getMessage(),e);
+    }
+  }
+  
+  protected static void leaveWriteLock(LockObject lo)
+    throws Exception
+  {
+    try
+    {
+      lo.leaveWriteLock();
+    }
+    catch (ExpiredObjectException e)
+    {
+      throw new ManifoldCFException("Unexpected exception: "+e.getMessage(),e);
+    }
+  }
+  
   /** Reader thread */
   protected static class ReaderThread extends Thread
   {
@@ -107,13 +160,16 @@ public class TestZooKeeperLocks extends 
         // Create a new lock pool since that is the best way to insure real
         // zookeeper action.
         LockPool lp = new LockPool(new LockObjectFactory());
-        LockObject lo = new ZooKeeperLockObject(lp, lockKey, pool);
+        LockObject lo;
         // First test: count all reader threads inside read lock.
         // This guarantees that read locks are non-exclusive.
         // Enter read lock
-        lo.enterReadLock();
+        System.out.println("Entering read lock");
+        lo = new ZooKeeperLockObject(lp, lockKey, pool);
+        enterReadLock(lo);
         try
         {
+          System.out.println(" Read lock entered!");
           // Count this thread
           ai.incrementAndGet();
           // Wait until all readers have been counted.  This test will hang if 
the readers function
@@ -125,12 +181,16 @@ public class TestZooKeeperLocks extends 
         }
         finally
         {
-          lo.leaveReadLock();
+          System.out.println("Leaving read lock");
+          leaveReadLock(lo);
+          System.out.println(" Left read lock!");
         }
         // Now, all the writers will get involved; we just need to make sure 
we never see an inconsistent value
         while (ai.get() < readerThreadCount + 2*writerThreadCount)
         {
-          lo.enterReadLock();
+          System.out.println("Waiting for all read threads to succeed...");
+          lo = new ZooKeeperLockObject(lp, lockKey, pool);
+          enterReadLock(lo);
           try
           {
             // The writer thread will increment the counter twice for every 
thread, both times within the lock.
@@ -140,9 +200,10 @@ public class TestZooKeeperLocks extends 
           }
           finally
           {
-            lo.leaveReadLock();
+            leaveReadLock(lo);
           }
         }
+        System.out.println("Done with reader thread");
       }
       catch (InterruptedException e)
       {
@@ -187,11 +248,12 @@ public class TestZooKeeperLocks extends 
         // zookeeper action.
         // LockPool is a dummy
         LockPool lp = new LockPool(new LockObjectFactory());
-        LockObject lo = new ZooKeeperLockObject(lp, lockKey, pool);
+        LockObject lo;
         // Take write locks but free them if read is what's active
         while (true)
         {
-          lo.enterWriteLock();
+          lo = new ZooKeeperLockObject(lp, lockKey, pool);
+          enterWriteLock(lo);
           try
           {
             // Check if we made it in during read cycle... that would be bad.
@@ -202,13 +264,14 @@ public class TestZooKeeperLocks extends 
           }
           finally
           {
-            lo.leaveWriteLock();
+            leaveWriteLock(lo);
           }
           Thread.sleep(10L);
         }
         
         // Get write lock, increment twice, and leave write lock
-        lo.enterWriteLock();
+        lo = new ZooKeeperLockObject(lp, lockKey, pool);
+        enterWriteLock(lo);
         try
         {
           if ((ai.get() - readerThreadCount) % 2 == 1)
@@ -221,7 +284,7 @@ public class TestZooKeeperLocks extends 
         }
         finally
         {
-          lo.leaveWriteLock();
+          leaveWriteLock(lo);
         }
         // Completed successfully!
       }

Modified: 
manifoldcf/branches/CONNECTORS-13/framework/core/src/test/java/org/apache/manifoldcf/core/lockmanager/ZooKeeperInstance.java
URL: 
http://svn.apache.org/viewvc/manifoldcf/branches/CONNECTORS-13/framework/core/src/test/java/org/apache/manifoldcf/core/lockmanager/ZooKeeperInstance.java?rev=1541337&r1=1541336&r2=1541337&view=diff
==============================================================================
--- 
manifoldcf/branches/CONNECTORS-13/framework/core/src/test/java/org/apache/manifoldcf/core/lockmanager/ZooKeeperInstance.java
 (original)
+++ 
manifoldcf/branches/CONNECTORS-13/framework/core/src/test/java/org/apache/manifoldcf/core/lockmanager/ZooKeeperInstance.java
 Wed Nov 13 00:31:53 2013
@@ -53,7 +53,8 @@ public class ZooKeeperInstance
     zookeeperThread = new ZooKeeperThread(configuration);
     zookeeperThread.start();
     // We have no way of knowing whether zookeeper is alive or not, but the
-    // client is supposed to know about that.
+    // client is supposed to know about that.  But it doesn't, so wait for 5 
seconds
+    Thread.sleep(5000L);
   }
   
   public void stop()


Reply via email to