manifoldcf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kwri...@apache.org
Subject svn commit: r1539587 - in /manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager: FileLockObject.java LockObject.java LockPool.java ZooKeeperConnection.java ZookeeperLockManager.java
Date Thu, 07 Nov 2013 10:20:39 GMT
Author: kwright
Date: Thu Nov  7 10:20:39 2013
New Revision: 1539587

URL: http://svn.apache.org/r1539587
Log:
Create a Zookeeper connection object, and separate global from local sync

Added:
    manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/FileLockObject.java
      - copied, changed from r1539514, manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/LockObject.java
    manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/ZooKeeperConnection.java
  (with props)
Modified:
    manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/LockObject.java
    manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/LockPool.java
    manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/ZookeeperLockManager.java

Copied: manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/FileLockObject.java
(from r1539514, manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/LockObject.java)
URL: http://svn.apache.org/viewvc/manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/FileLockObject.java?p2=manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/FileLockObject.java&p1=manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/LockObject.java&r1=1539514&r2=1539587&rev=1539587&view=diff
==============================================================================
--- manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/LockObject.java
(original)
+++ manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/FileLockObject.java
Thu Nov  7 10:20:39 2013
@@ -24,33 +24,26 @@ import org.apache.manifoldcf.core.system
 import java.io.*;
 
 /** One instance of this object exists for each lock on each JVM!
+* This is the file-system version of the lock.
 */
-public class LockObject
+public class FileLockObject extends LockObject
 {
   public static final String _rcsid = "@(#)$Id: LockObject.java 988245 2010-08-23 18:39:35Z
kwright $";
 
   private final static int STATUS_WRITELOCKED = -1;
 
-  private LockPool lockPool;
-  private Object lockKey;
   private File lockDirectoryName = null;
   private File lockFileName = null;
-  private boolean obtainedWrite = false;  // Set to true if this object already owns the
permission to exclusively write
-  private int obtainedRead = 0;           // Set to a count if this object already owns the
permission to read
-  private int obtainedNonExWrite = 0;     // Set to a count if this object already owns the
permission to non-exclusively write
   private boolean isSync;                 // True if we need to be synchronizing across JVM's
 
   private final static String DOTLOCK = ".lock";
   private final static String DOTFILE = ".file";
   private final static String SLASH = "/";
-  private static final String LOCKEDANOTHERTHREAD = "Locked by another thread in this JVM";
-  private static final String LOCKEDANOTHERJVM = "Locked by another JVM";
 
 
-  public LockObject(LockPool lockPool, Object lockKey, File synchDir)
+  public FileLockObject(LockPool lockPool, Object lockKey, File synchDir)
   {
-    this.lockPool = lockPool;
-    this.lockKey = lockKey;
+    super(lockPool,lockKey);
     this.isSync = (synchDir != null);
     if (isSync)
     {
@@ -70,77 +63,15 @@ public class LockObject
     }
   }
 
-  public synchronized void makeInvalid()
-  {
-    this.lockPool = null;
-  }
-
   private static String createFileName(Object lockKey)
   {
     return "lock-"+ManifoldCF.safeFileName(lockKey.toString());
   }
 
-  /** This method WILL NOT BE CALLED UNLESS we are actually committing a write lock for the
-  * first time for a given thread.
-  */
-  public void enterWriteLock()
-    throws InterruptedException, ExpiredObjectException
-  {
-    // if (lockFileName != null)
-    //  System.out.println("Entering write lock for resource "+lockFileName.toString());
-    while (true)
-    {
-      try
-      {
-        synchronized (this)
-        {
-          if (lockPool == null)
-            throw new ExpiredObjectException("Invalid");
-
-          while (true)
-          {
-            try
-            {
-              enterWriteLockNoWait();
-              // if (lockFileName != null)
-              //      System.out.println("Leaving write lock for resource "+lockFileName.toString());
-              return;
-            }
-            catch (LocalLockException le)
-            {
-              wait();
-            }
-          }
-        }
-      }
-      catch (LockException le2)
-      {
-        // Cross JVM lock; sleep!
-        ManifoldCF.sleep(10);
-      }
-    }
-  }
-
-  /** Note well: Upgrading a read lock to a non-ex write lock is tricky.  The code inside
the
-  * lock should execute only when there are NO threads that are executing in a read-locked
area that
-  * aren't waiting to enter the non-ex write lock area!  This is therefore essentially an
illegal codepath,
-  * because it will lead inevitably to deadlock, as is going from a read-locked area into
a write-locked area,
-  * or from a non-ex write area into an
-  * exclusive write area.
-  */
-  public synchronized void enterWriteLockNoWait()
-    throws LockException, LocalLockException, InterruptedException, ExpiredObjectException
+  @Override
+  protected void obtainGlobalWriteLock()
+    throws LockException, InterruptedException
   {
-    if (lockPool == null)
-      throw new ExpiredObjectException("Invalid");
-
-    // Does another thread in this JVM have the writelock?
-    if (obtainedWrite)
-      throw new LocalLockException(LOCKEDANOTHERTHREAD);
-    // Got the write token!
-    if (obtainedRead > 0 || obtainedNonExWrite > 0)
-      throw new LocalLockException(LOCKEDANOTHERTHREAD);
-    // Attempt to obtain a global write lock
     if (isSync)
     {
       grabFileLock();
@@ -158,133 +89,30 @@ public class LockObject
         releaseFileLock();
       }
     }
-    obtainedWrite = true;
-  }
-
-  public void leaveWriteLock()
-    throws InterruptedException, ExpiredObjectException
-  {
-    // if (lockFileName != null)
-    //      System.out.println("Releasing write lock for resource "+lockFileName.toString());
-    while (true)
-    {
-      try
-      {
-        synchronized (this)
-        {
-          if (lockPool == null)
-            throw new ExpiredObjectException("Invalid");
-
-          if (obtainedWrite == false)
-            throw new RuntimeException("JVM failure: Don't hold lock for object "+this.toString());
-          obtainedWrite = false;
-          if (isSync)
-          {
-            try
-            {
-              grabFileLock();
-              try
-              {
-                writeFile(0);
-              }
-              finally
-              {
-                releaseFileLock();
-              }
-            }
-            catch (LockException le)
-            {
-              obtainedWrite = true;
-              throw le;
-            }
-            catch (Error e)
-            {
-              obtainedWrite = true;
-              throw e;
-            }
-            catch (RuntimeException e)
-            {
-              obtainedWrite = true;
-              throw e;
-            }
-          }
-
-          // Lock is free, so release this object from the pool
-          lockPool.releaseObject(lockKey,this);
-
-          notifyAll();
-          // if (lockFileName != null)
-          //      System.out.println("Write lock released for resource "+lockFileName.toString());
-          return;
-        }
-      }
-      catch (LockException le)
-      {
-        ManifoldCF.sleep(10);
-        // Loop around
-      }
-    }
-
   }
 
-  public void enterNonExWriteLock()
-    throws InterruptedException, ExpiredObjectException
+  @Override
+  protected void clearGlobalWriteLock()
+    throws LockException, InterruptedException
   {
-    while (true)
+    if (isSync)
     {
+      grabFileLock();
       try
       {
-        synchronized (this)
-        {
-          if (lockPool == null)
-            throw new ExpiredObjectException("Invalid");
-
-          // System.out.println("Entering write lock for resource "+lockFileName);
-          while (true)
-          {
-            try
-            {
-              enterNonExWriteLockNoWait();
-              return;
-            }
-            catch (LocalLockException le)
-            {
-              wait();
-            }
-          }
-        }
+        writeFile(0);
       }
-      catch (LockException le2)
+      finally
       {
-        // Cross JVM lock; sleep!
-        ManifoldCF.sleep(10);
+        releaseFileLock();
       }
     }
   }
 
-  /** Note well: Upgrading a read lock to a non-ex write lock is tricky.  The code inside
the
-  * lock should execute only when there are NO threads that are executing in a read-locked
area that
-  * aren't waiting to enter the non-ex write lock area!  This is therefore essentially an
illegal codepath,
-  * because it will lead inevitably to deadlock, as is going from a read-locked area into
a write-locked area,
-  * or from a non-ex write area into an
-  * exclusive write area.
-  */
-  public synchronized void enterNonExWriteLockNoWait()
-    throws LockException, LocalLockException, InterruptedException, ExpiredObjectException
+  @Override
+  protected void enterGlobalNonExWriteLock()
+    throws LockException, InterruptedException
   {
-    if (lockPool == null)
-      throw new ExpiredObjectException("Invalid");
-
-    // Does another thread in this JVM have the lock?
-    if (obtainedWrite || obtainedRead > 0)
-      throw new LocalLockException(LOCKEDANOTHERTHREAD);
-    // We've got the local non-ex write token
-    if (obtainedNonExWrite > 0)
-    {
-      obtainedNonExWrite++;
-      return;
-    }
-
     // Attempt to obtain a global write lock
     if (isSync)
     {
@@ -305,134 +133,36 @@ public class LockObject
         releaseFileLock();
       }
     }
-    obtainedNonExWrite++;
   }
 
-  public void leaveNonExWriteLock()
-    throws InterruptedException, ExpiredObjectException
-  {
-    // System.out.println("Releasing non-ex-write lock for resource "+lockFileName.toString());
-    while (true)
-    {
-      try
-      {
-        synchronized (this)
-        {
-          if (lockPool == null)
-            throw new ExpiredObjectException("Invalid");
-
-          if (obtainedNonExWrite == 0)
-            throw new RuntimeException("JVM error: Don't hold lock for object "+this.toString());
-          obtainedNonExWrite--;
-          if (obtainedNonExWrite > 0)
-            return;
-
-          if (isSync)
-          {
-            try
-            {
-              grabFileLock();
-              try
-              {
-                int status = readFile();
-                if (status >= STATUS_WRITELOCKED)
-                  throw new RuntimeException("JVM error: File lock is not in expected state
for object "+this.toString());
-                status++;
-                if (status == STATUS_WRITELOCKED)
-                  status = 0;
-                writeFile(status);
-              }
-              finally
-              {
-                releaseFileLock();
-              }
-            }
-            catch (LockException le)
-            {
-              obtainedNonExWrite++;
-              throw le;
-            }
-            catch (Error e)
-            {
-              obtainedNonExWrite++;
-              throw e;
-            }
-            catch (RuntimeException e)
-            {
-              obtainedNonExWrite++;
-              throw e;
-            }
-          }
-
-          // Lock is free, so release this object from the pool
-          lockPool.releaseObject(lockKey,this);
-
-          notifyAll();
-          break;
-        }
-      }
-      catch (LockException le)
-      {
-        ManifoldCF.sleep(10);
-        // Loop around
-      }
-    }
-    // System.out.println("Non-ex Write lock released for resource "+lockFileName.toString());
-  }
-
-  public void enterReadLock()
-    throws InterruptedException, ExpiredObjectException
+  @Override
+  protected void leaveGlobalNonExWriteLock()
+    throws LockException, InterruptedException
   {
-    // if (lockFileName != null)
-    //      System.out.println("Entering read lock for resource "+lockFileName.toString()+"
"+toString());
-    while (true)
+    if (isSync)
     {
+      grabFileLock();
       try
       {
-        synchronized (this)
-        {
-          if (lockPool == null)
-            throw new ExpiredObjectException("Invalid");
-
-          while (true)
-          {
-            try
-            {
-              enterReadLockNoWait();
-              // if (lockFileName != null)
-              //      System.out.println("Obtained read permission for resource "+lockFileName.toString());
-              return;
-            }
-            catch (LocalLockException le)
-            {
-              wait();
-            }
-          }
-        }
+        int status = readFile();
+        if (status >= STATUS_WRITELOCKED)
+          throw new RuntimeException("JVM error: File lock is not in expected state for object
"+this.toString());
+        status++;
+        if (status == STATUS_WRITELOCKED)
+          status = 0;
+        writeFile(status);
       }
-      catch (LockException le)
+      finally
       {
-        ManifoldCF.sleep(10);
-        // Loop around
+        releaseFileLock();
       }
     }
   }
 
-  public synchronized void enterReadLockNoWait()
-    throws LockException, LocalLockException, InterruptedException, ExpiredObjectException
+  @Override
+  protected void enterGlobalReadLock()
+    throws LockException, InterruptedException
   {
-    if (lockPool == null)
-      throw new ExpiredObjectException("Invalid");
-
-    if (obtainedWrite || obtainedNonExWrite > 0)
-      throw new LocalLockException(LOCKEDANOTHERTHREAD);
-    if (obtainedRead > 0)
-    {
-      obtainedRead++;
-      return;
-    }
-    // Got the read token locally!
-
     // Attempt to obtain a global read lock
     if (isSync)
     {
@@ -440,99 +170,40 @@ public class LockObject
       try
       {
         int status = readFile();
-        // System.out.println(" Read "+Integer.toString(status));
         if (status <= STATUS_WRITELOCKED)
         {
           throw new LockException(LOCKEDANOTHERJVM);
         }
         status++;
         writeFile(status);
-        // System.out.println(" Wrote "+Integer.toString(status));
       }
       finally
       {
         releaseFileLock();
       }
-      // System.out.println(" Exiting");
     }
-
-    obtainedRead = 1;
   }
 
-  public void leaveReadLock()
-    throws InterruptedException, ExpiredObjectException
+  @Override
+  protected void leaveGlobalReadLock()
+    throws LockException, InterruptedException
   {
-    // if (lockFileName != null)
-    //      System.out.println("Leaving read lock for resource "+lockFileName.toString()+"
"+toString());
-    while (true)
+    if (isSync)
     {
+      grabFileLock();
       try
       {
-        synchronized (this)
-        {
-          if (lockPool == null)
-            throw new ExpiredObjectException("Invalid");
-
-          if (obtainedRead == 0)
-            throw new RuntimeException("JVM error: Don't hold lock for object "+this.toString());
-          obtainedRead--;
-          if (obtainedRead > 0)
-          {
-            // if (lockFileName != null)
-            //      System.out.println("Freed read lock for resource "+lockFileName.toString()+"
(obtainedRead > 0)");
-            return;
-          }
-          if (isSync)
-          {
-            try
-            {
-              grabFileLock();
-              try
-              {
-                int status = readFile();
-                // System.out.println(" Read status = "+Integer.toString(status));
-                if (status == 0)
-                  throw new RuntimeException("JVM error: File lock is not in expected state
for object "+this.toString());
-                status--;
-                writeFile(status);
-                // System.out.println(" Wrote status = "+Integer.toString(status));
-              }
-              finally
-              {
-                releaseFileLock();
-              }
-              // System.out.println(" Done");
-            }
-            catch (LockException le)
-            {
-              obtainedRead++;
-              throw le;
-            }
-            catch (Error e)
-            {
-              obtainedRead++;
-              throw e;
-            }
-            catch (RuntimeException e)
-            {
-              obtainedRead++;
-              throw e;
-            }
-          }
-
-          // Lock is free, so release this object from the pool
-          lockPool.releaseObject(lockKey,this);
-
-          notifyAll();
-          // if (lockFileName != null)
-          //      System.out.println("Freed read lock for resource "+lockFileName.toString());
-          return;
-        }
+        int status = readFile();
+        // System.out.println(" Read status = "+Integer.toString(status));
+        if (status == 0)
+          throw new RuntimeException("JVM error: File lock is not in expected state for object
"+this.toString());
+        status--;
+        writeFile(status);
+        // System.out.println(" Wrote status = "+Integer.toString(status));
       }
-      catch (LockException le)
+      finally
       {
-        ManifoldCF.sleep(10);
-        // Loop around
+        releaseFileLock();
       }
     }
   }

Modified: manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/LockObject.java
URL: http://svn.apache.org/viewvc/manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/LockObject.java?rev=1539587&r1=1539586&r2=1539587&view=diff
==============================================================================
--- manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/LockObject.java
(original)
+++ manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/LockObject.java
Thu Nov  7 10:20:39 2013
@@ -23,51 +23,26 @@ import org.apache.manifoldcf.core.system
 import org.apache.manifoldcf.core.system.Logging;
 import java.io.*;
 
-/** One instance of this object exists for each lock on each JVM!
+/** Base class.  One instance of this object exists for each lock on each JVM!
 */
 public class LockObject
 {
   public static final String _rcsid = "@(#)$Id: LockObject.java 988245 2010-08-23 18:39:35Z
kwright $";
 
-  private final static int STATUS_WRITELOCKED = -1;
-
   private LockPool lockPool;
   private Object lockKey;
-  private File lockDirectoryName = null;
-  private File lockFileName = null;
   private boolean obtainedWrite = false;  // Set to true if this object already owns the
permission to exclusively write
   private int obtainedRead = 0;           // Set to a count if this object already owns the
permission to read
   private int obtainedNonExWrite = 0;     // Set to a count if this object already owns the
permission to non-exclusively write
-  private boolean isSync;                 // True if we need to be synchronizing across JVM's
 
-  private final static String DOTLOCK = ".lock";
-  private final static String DOTFILE = ".file";
-  private final static String SLASH = "/";
-  private static final String LOCKEDANOTHERTHREAD = "Locked by another thread in this JVM";
-  private static final String LOCKEDANOTHERJVM = "Locked by another JVM";
+  protected static final String LOCKEDANOTHERTHREAD = "Locked by another thread in this JVM";
+  protected static final String LOCKEDANOTHERJVM = "Locked by another JVM";
 
 
-  public LockObject(LockPool lockPool, Object lockKey, File synchDir)
+  public LockObject(LockPool lockPool, Object lockKey)
   {
     this.lockPool = lockPool;
     this.lockKey = lockKey;
-    this.isSync = (synchDir != null);
-    if (isSync)
-    {
-      // Hash the filename
-      int hashcode = lockKey.hashCode();
-      int outerDirNumber = (hashcode & (1023));
-      int innerDirNumber = ((hashcode >> 10) & (1023));
-      String fullDir = synchDir.toString();
-      if (fullDir.length() == 0 || !fullDir.endsWith(SLASH))
-        fullDir = fullDir + SLASH;
-      fullDir = fullDir + Integer.toString(outerDirNumber)+SLASH+Integer.toString(innerDirNumber);
-      (new File(fullDir)).mkdirs();
-      String filename = createFileName(lockKey);
-
-      lockDirectoryName = new File(fullDir,filename+DOTLOCK);
-      lockFileName = new File(fullDir,filename+DOTFILE);
-    }
   }
 
   public synchronized void makeInvalid()
@@ -75,19 +50,12 @@ public class LockObject
     this.lockPool = null;
   }
 
-  private static String createFileName(Object lockKey)
-  {
-    return "lock-"+ManifoldCF.safeFileName(lockKey.toString());
-  }
-
   /** This method WILL NOT BE CALLED UNLESS we are actually committing a write lock for the
   * first time for a given thread.
   */
   public void enterWriteLock()
     throws InterruptedException, ExpiredObjectException
   {
-    // if (lockFileName != null)
-    //  System.out.println("Entering write lock for resource "+lockFileName.toString());
     while (true)
     {
       try
@@ -102,8 +70,6 @@ public class LockObject
             try
             {
               enterWriteLockNoWait();
-              // if (lockFileName != null)
-              //      System.out.println("Leaving write lock for resource "+lockFileName.toString());
               return;
             }
             catch (LocalLockException le)
@@ -141,31 +107,19 @@ public class LockObject
     if (obtainedRead > 0 || obtainedNonExWrite > 0)
       throw new LocalLockException(LOCKEDANOTHERTHREAD);
     // Attempt to obtain a global write lock
-    if (isSync)
-    {
-      grabFileLock();
-      try
-      {
-        int status = readFile();
-        if (status != 0)
-        {
-          throw new LockException(LOCKEDANOTHERJVM);
-        }
-        writeFile(STATUS_WRITELOCKED);
-      }
-      finally
-      {
-        releaseFileLock();
-      }
-    }
+    obtainGlobalWriteLock();
     obtainedWrite = true;
   }
 
+  protected void obtainGlobalWriteLock()
+    throws LockException, InterruptedException
+  {
+  }
+  
+
   public void leaveWriteLock()
     throws InterruptedException, ExpiredObjectException
   {
-    // if (lockFileName != null)
-    //      System.out.println("Releasing write lock for resource "+lockFileName.toString());
     while (true)
     {
       try
@@ -178,43 +132,30 @@ public class LockObject
           if (obtainedWrite == false)
             throw new RuntimeException("JVM failure: Don't hold lock for object "+this.toString());
           obtainedWrite = false;
-          if (isSync)
+          try
           {
-            try
-            {
-              grabFileLock();
-              try
-              {
-                writeFile(0);
-              }
-              finally
-              {
-                releaseFileLock();
-              }
-            }
-            catch (LockException le)
-            {
-              obtainedWrite = true;
-              throw le;
-            }
-            catch (Error e)
-            {
-              obtainedWrite = true;
-              throw e;
-            }
-            catch (RuntimeException e)
-            {
-              obtainedWrite = true;
-              throw e;
-            }
+            clearGlobalWriteLock();
+          }
+          catch (LockException le)
+          {
+            obtainedWrite = true;
+            throw le;
+          }
+          catch (Error e)
+          {
+            obtainedWrite = true;
+            throw e;
+          }
+          catch (RuntimeException e)
+          {
+            obtainedWrite = true;
+            throw e;
           }
 
           // Lock is free, so release this object from the pool
           lockPool.releaseObject(lockKey,this);
 
           notifyAll();
-          // if (lockFileName != null)
-          //      System.out.println("Write lock released for resource "+lockFileName.toString());
           return;
         }
       }
@@ -227,6 +168,11 @@ public class LockObject
 
   }
 
+  protected void clearGlobalWriteLock()
+    throws LockException, InterruptedException
+  {
+  }
+  
   public void enterNonExWriteLock()
     throws InterruptedException, ExpiredObjectException
   {
@@ -284,30 +230,16 @@ public class LockObject
       obtainedNonExWrite++;
       return;
     }
-
-    // Attempt to obtain a global write lock
-    if (isSync)
-    {
-      grabFileLock();
-      try
-      {
-        int status = readFile();
-        if (status >= STATUS_WRITELOCKED)
-        {
-          throw new LockException(LOCKEDANOTHERJVM);
-        }
-        if (status == 0)
-          status = STATUS_WRITELOCKED;
-        writeFile(status-1);
-      }
-      finally
-      {
-        releaseFileLock();
-      }
-    }
+    enterGlobalNonExWriteLock();
     obtainedNonExWrite++;
   }
 
+  protected void enterGlobalNonExWriteLock()
+    throws LockException, InterruptedException
+  {
+  }
+  
+
   public void leaveNonExWriteLock()
     throws InterruptedException, ExpiredObjectException
   {
@@ -327,41 +259,24 @@ public class LockObject
           if (obtainedNonExWrite > 0)
             return;
 
-          if (isSync)
+          try
           {
-            try
-            {
-              grabFileLock();
-              try
-              {
-                int status = readFile();
-                if (status >= STATUS_WRITELOCKED)
-                  throw new RuntimeException("JVM error: File lock is not in expected state
for object "+this.toString());
-                status++;
-                if (status == STATUS_WRITELOCKED)
-                  status = 0;
-                writeFile(status);
-              }
-              finally
-              {
-                releaseFileLock();
-              }
-            }
-            catch (LockException le)
-            {
-              obtainedNonExWrite++;
-              throw le;
-            }
-            catch (Error e)
-            {
-              obtainedNonExWrite++;
-              throw e;
-            }
-            catch (RuntimeException e)
-            {
-              obtainedNonExWrite++;
-              throw e;
-            }
+            leaveGlobalNonExWriteLock();
+          }
+          catch (LockException le)
+          {
+            obtainedNonExWrite++;
+            throw le;
+          }
+          catch (Error e)
+          {
+            obtainedNonExWrite++;
+            throw e;
+          }
+          catch (RuntimeException e)
+          {
+            obtainedNonExWrite++;
+            throw e;
           }
 
           // Lock is free, so release this object from the pool
@@ -380,6 +295,12 @@ public class LockObject
     // System.out.println("Non-ex Write lock released for resource "+lockFileName.toString());
   }
 
+  protected void leaveGlobalNonExWriteLock()
+    throws LockException, InterruptedException
+  {
+  }
+  
+
   public void enterReadLock()
     throws InterruptedException, ExpiredObjectException
   {
@@ -432,38 +353,20 @@ public class LockObject
       return;
     }
     // Got the read token locally!
-
-    // Attempt to obtain a global read lock
-    if (isSync)
-    {
-      grabFileLock();
-      try
-      {
-        int status = readFile();
-        // System.out.println(" Read "+Integer.toString(status));
-        if (status <= STATUS_WRITELOCKED)
-        {
-          throw new LockException(LOCKEDANOTHERJVM);
-        }
-        status++;
-        writeFile(status);
-        // System.out.println(" Wrote "+Integer.toString(status));
-      }
-      finally
-      {
-        releaseFileLock();
-      }
-      // System.out.println(" Exiting");
-    }
+    enterGlobalReadLock();
 
     obtainedRead = 1;
   }
 
+  protected void enterGlobalReadLock()
+    throws LockException, InterruptedException
+  {
+  }
+  
+
   public void leaveReadLock()
     throws InterruptedException, ExpiredObjectException
   {
-    // if (lockFileName != null)
-    //      System.out.println("Leaving read lock for resource "+lockFileName.toString()+"
"+toString());
     while (true)
     {
       try
@@ -478,54 +381,32 @@ public class LockObject
           obtainedRead--;
           if (obtainedRead > 0)
           {
-            // if (lockFileName != null)
-            //      System.out.println("Freed read lock for resource "+lockFileName.toString()+"
(obtainedRead > 0)");
             return;
           }
-          if (isSync)
+          try
           {
-            try
-            {
-              grabFileLock();
-              try
-              {
-                int status = readFile();
-                // System.out.println(" Read status = "+Integer.toString(status));
-                if (status == 0)
-                  throw new RuntimeException("JVM error: File lock is not in expected state
for object "+this.toString());
-                status--;
-                writeFile(status);
-                // System.out.println(" Wrote status = "+Integer.toString(status));
-              }
-              finally
-              {
-                releaseFileLock();
-              }
-              // System.out.println(" Done");
-            }
-            catch (LockException le)
-            {
-              obtainedRead++;
-              throw le;
-            }
-            catch (Error e)
-            {
-              obtainedRead++;
-              throw e;
-            }
-            catch (RuntimeException e)
-            {
-              obtainedRead++;
-              throw e;
-            }
+            leaveGlobalReadLock();
+          }
+          catch (LockException le)
+          {
+            obtainedRead++;
+            throw le;
+          }
+          catch (Error e)
+          {
+            obtainedRead++;
+            throw e;
+          }
+          catch (RuntimeException e)
+          {
+            obtainedRead++;
+            throw e;
           }
 
           // Lock is free, so release this object from the pool
           lockPool.releaseObject(lockKey,this);
 
           notifyAll();
-          // if (lockFileName != null)
-          //      System.out.println("Freed read lock for resource "+lockFileName.toString());
           return;
         }
       }
@@ -537,273 +418,10 @@ public class LockObject
     }
   }
 
-  private final static String FILELOCKED = "File locked";
-
-  private synchronized void grabFileLock()
+  protected void leaveGlobalReadLock()
     throws LockException, InterruptedException
   {
-    while (true)
-    {
-      // Try to create the lock file
-      try
-      {
-        if (lockDirectoryName.createNewFile() == false)
-          throw new LockException(FILELOCKED);
-        break;
-      }
-      catch (InterruptedIOException e)
-      {
-        throw new InterruptedException("Interrupted IO: "+e.getMessage());
-      }
-      catch (IOException e)
-      {
-        // Log this if possible
-        try
-        {
-          Logging.lock.warn("Attempt to set file lock '"+lockDirectoryName.toString()+"'
failed: "+e.getMessage(),e);
-        }
-        catch (Throwable e2)
-        {
-          e.printStackTrace();
-        }
-        // Winnt sometimes throws an exception when you can't do the lock
-        ManifoldCF.sleep(100);
-        continue;
-      }
-    }
-  }
-
-  private synchronized void releaseFileLock()
-    throws InterruptedException
-  {
-    Throwable ie = null;
-    while (true)
-    {
-      try
-      {
-        if (lockDirectoryName.delete())
-          break;
-        try
-        {
-          Logging.lock.fatal("Failure deleting file lock '"+lockDirectoryName.toString()+"'");
-        }
-        catch (Throwable e2)
-        {
-          System.out.println("Failure deleting file lock '"+lockDirectoryName.toString()+"'");
-        }
-        // Fail hard
-        System.exit(-100);
-      }
-      catch (Error e)
-      {
-        // An error - must try again to delete
-        // Attempting to log this to the log may not work due to disk being full, but try
anyway.
-        String message = "Error deleting file lock '"+lockDirectoryName.toString()+"': "+e.getMessage();
-        try
-        {
-          Logging.lock.error(message,e);
-        }
-        catch (Throwable e2)
-        {
-          // Ok, we failed, send it to standard out
-          System.out.println(message);
-          e.printStackTrace();
-        }
-        ie = e;
-        ManifoldCF.sleep(100);
-        continue;
-      }
-      catch (RuntimeException e)
-      {
-        // A runtime exception - try again to delete
-        // Attempting to log this to the log may not work due to disk being full, but try
anyway.
-        String message = "Error deleting file lock '"+lockDirectoryName.toString()+"': "+e.getMessage();
-        try
-        {
-          Logging.lock.error(message,e);
-        }
-        catch (Throwable e2)
-        {
-          // Ok, we failed, send it to standard out
-          System.out.println(message);
-          e.printStackTrace();
-        }
-        ie = e;
-        ManifoldCF.sleep(100);
-        continue;
-      }
-    }
-
-    // Succeeded finally - but we need to rethrow any exceptions we got
-    if (ie != null)
-    {
-      if (ie instanceof InterruptedException)
-        throw (InterruptedException)ie;
-      if (ie instanceof Error)
-        throw (Error)ie;
-      if (ie instanceof RuntimeException)
-        throw (RuntimeException)ie;
-    }
-
-  }
-
-  private synchronized int readFile()
-    throws InterruptedException
-  {
-    try
-    {
-      FileReader fr = new FileReader(lockFileName);
-      try
-      {
-        BufferedReader x = new BufferedReader(fr);
-        try
-        {
-          StringBuilder sb = new StringBuilder();
-          while (true)
-          {
-            int rval = x.read();
-            if (rval == -1)
-              break;
-            sb.append((char)rval);
-          }
-          try
-          {
-            return Integer.parseInt(sb.toString());
-          }
-          catch (NumberFormatException e)
-          {
-            // We should never be in a situation where we can't parse a number we have supposedly
written.
-            // But, print a stack trace and throw IOException, so we recover.
-            throw new IOException("Lock number read was not valid: "+e.getMessage());
-          }
-        }
-        finally
-        {
-          x.close();
-        }
-      }
-      catch (InterruptedIOException e)
-      {
-        throw new InterruptedException("Interrupted IO: "+e.getMessage());
-      }
-      catch (IOException e)
-      {
-        String message = "Could not read from lock file: '"+lockFileName.toString()+"'";
-        try
-        {
-          Logging.lock.error(message,e);
-        }
-        catch (Throwable e2)
-        {
-          System.out.println(message);
-          e.printStackTrace();
-        }
-        // Don't fail hard or there is no way to recover
-        throw e;
-      }
-      finally
-      {
-        fr.close();
-      }
-    }
-    catch (InterruptedIOException e)
-    {
-      throw new InterruptedException("Interrupted IO: "+e.getMessage());
-    }
-    catch (IOException e)
-    {
-      return 0;
-    }
-
   }
-
-  private synchronized void writeFile(int value)
-    throws InterruptedException
-  {
-    try
-    {
-      if (value == 0)
-      {
-        if (lockFileName.delete() == false)
-          throw new IOException("Could not delete file '"+lockFileName.toString()+"'");
-      }
-      else
-      {
-        FileWriter fw = new FileWriter(lockFileName);
-        try
-        {
-          BufferedWriter x = new BufferedWriter(fw);
-          try
-          {
-            x.write(Integer.toString(value));
-          }
-          finally
-          {
-            x.close();
-          }
-        }
-        finally
-        {
-          fw.close();
-        }
-      }
-    }
-    catch (Error e)
-    {
-      // Couldn't write for some reason!  Write to BOTH stdout and the log, since we
-      // can't be sure we will succeed at the latter.
-      String message = "Couldn't write to lock file; hard error occurred.  Shutting down
process; locks may be left dangling.  You must cleanup before restarting.";
-      try
-      {
-        Logging.lock.error(message,e);
-      }
-      catch (Throwable e2)
-      {
-        System.out.println(message);
-        e.printStackTrace();
-      }
-      System.exit(-100);
-    }
-    catch (RuntimeException e)
-    {
-      // Couldn't write for some reason!  Write to BOTH stdout and the log, since we
-      // can't be sure we will succeed at the latter.
-      String message = "Couldn't write to lock file; JVM error.  Shutting down process; locks
may be left dangling.  You must cleanup before restarting.";
-      try
-      {
-        Logging.lock.error(message,e);
-      }
-      catch (Throwable e2)
-      {
-        System.out.println(message);
-        e.printStackTrace();
-      }
-      System.exit(-100);
-    }
-    catch (InterruptedIOException e)
-    {
-      throw new InterruptedException("Interrupted IO: "+e.getMessage());
-    }
-    catch (IOException e)
-    {
-      // Couldn't write for some reason!  Write to BOTH stdout and the log, since we
-      // can't be sure we will succeed at the latter.
-      String message = "Couldn't write to lock file; disk may be full.  Shutting down process;
locks may be left dangling.  You must cleanup before restarting.";
-      try
-      {
-        Logging.lock.error(message,e);
-      }
-      catch (Throwable e2)
-      {
-        System.out.println(message);
-        e.printStackTrace();
-      }
-      System.exit(-100);
-      // Hard failure is called for
-      // throw new Error("Lock management system failure",e);
-    }
-  }
-
-
+  
 }
 

Modified: manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/LockPool.java
URL: http://svn.apache.org/viewvc/manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/LockPool.java?rev=1539587&r1=1539586&r2=1539587&view=diff
==============================================================================
--- manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/LockPool.java
(original)
+++ manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/LockPool.java
Thu Nov  7 10:20:39 2013
@@ -32,7 +32,7 @@ public class LockPool
     LockObject lo = (LockObject)myLocks.get(lockKey);
     if (lo == null)
     {
-      lo = new LockObject(this,lockKey,synchDir);
+      lo = new FileLockObject(this,lockKey,synchDir);
       myLocks.put(lockKey,lo);
     }
     return lo;

Added: 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=1539587&view=auto
==============================================================================
--- manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/ZooKeeperConnection.java
(added)
+++ manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/ZooKeeperConnection.java
Thu Nov  7 10:20:39 2013
@@ -0,0 +1,91 @@
+/* $Id$ */
+
+/**
+* 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.manifoldcf.core.lockmanager;
+
+import org.apache.manifoldcf.core.interfaces.*;
+import org.apache.manifoldcf.core.system.Logging;
+import org.apache.manifoldcf.core.system.ManifoldCF;
+
+import org.apache.zookeeper.*;
+
+import java.util.*;
+import java.io.*;
+
+/** An instance of this class is the Zookeeper analog to a database connection.
+* Basically, it bundles up the Zookeeper functionality we need in a nice package,
+* which we can share between users as needed.  These connections will be pooled,
+* and will be closed when the process they live in is shut down.
+*/
+public class ZooKeeperConnection
+{
+  public static final String _rcsid = "@(#)$Id$";
+	
+  // One zookeeper client per thread
+  protected ZooKeeper zookeeper = null;
+  protected ZooKeeperWatcher zookeeperWatcher = null;
+
+  /** Constructor. */
+  public ZooKeeperConnection(String connectString, int sessionTimeout)
+    throws ManifoldCFException
+  {
+    try
+    {
+      zookeeperWatcher = new ZooKeeperWatcher();
+      zookeeper = new ZooKeeper(connectString, sessionTimeout, zookeeperWatcher);
+    }
+    catch (IOException e)
+    {
+      throw new ManifoldCFException("Zookeeper initialization error: "+e.getMessage(),e);
+    }
+  }
+
+  // MHL
+  
+  /** Close this connection. */
+  public void close()
+    throws ManifoldCFException
+  {
+    try
+    {
+      zookeeper.close();
+      zookeeper = null;
+      zookeeperWatcher = null;
+    }
+    catch (InterruptedException e)
+    {
+      throw new ManifoldCFException(e.getMessage(),e,ManifoldCFException.INTERRUPTED);
+    }
+  }
+  
+  /** Watcher class for zookeeper, so we get notified about zookeeper events. */
+  protected static class ZooKeeperWatcher implements Watcher
+  {
+    public ZooKeeperWatcher()
+    {
+      // MHL
+    }
+    
+    public void process(WatchedEvent event)
+    {
+      // MHL
+    }
+
+  }
+
+}

Propchange: manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/ZooKeeperConnection.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/ZooKeeperConnection.java
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/ZookeeperLockManager.java
URL: http://svn.apache.org/viewvc/manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/ZookeeperLockManager.java?rev=1539587&r1=1539586&r2=1539587&view=diff
==============================================================================
--- manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/ZookeeperLockManager.java
(original)
+++ manifoldcf/branches/CONNECTORS-13/framework/core/src/main/java/org/apache/manifoldcf/core/lockmanager/ZookeeperLockManager.java
Thu Nov  7 10:20:39 2013
@@ -159,6 +159,8 @@ public class ZookeeperLockManager extend
   {
     // From Zookeeper recipes
 
+    ZooKeeper zk = getSession();
+    
     /*
     1. Call create( ) to create a node with pathname "guid-/write-". This is the lock node
        spoken of later in the protocol. Make sure to set both sequence and ephemeral flags.



Mime
View raw message