You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ra...@apache.org on 2008/04/09 10:26:37 UTC

svn commit: r646219 - in /activemq/trunk/activemq-core/src/main/java/org/apache/activemq/kaha/impl: KahaStore.java index/IndexManager.java

Author: rajdavies
Date: Wed Apr  9 01:26:36 2008
New Revision: 646219

URL: http://svn.apache.org/viewvc?rev=646219&view=rev
Log:
Fix for https://issues.apache.org/activemq/browse/AMQ-1254

Modified:
    activemq/trunk/activemq-core/src/main/java/org/apache/activemq/kaha/impl/KahaStore.java
    activemq/trunk/activemq-core/src/main/java/org/apache/activemq/kaha/impl/index/IndexManager.java

Modified: activemq/trunk/activemq-core/src/main/java/org/apache/activemq/kaha/impl/KahaStore.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/kaha/impl/KahaStore.java?rev=646219&r1=646218&r2=646219&view=diff
==============================================================================
--- activemq/trunk/activemq-core/src/main/java/org/apache/activemq/kaha/impl/KahaStore.java (original)
+++ activemq/trunk/activemq-core/src/main/java/org/apache/activemq/kaha/impl/KahaStore.java Wed Apr  9 01:26:36 2008
@@ -61,7 +61,9 @@
     private static final boolean DISABLE_LOCKING = "true".equals(System.getProperty(PROPERTY_PREFIX
                                                                                     + ".DisableLocking",
                                                                                     "false"));
-
+    //according to the String javadoc, all constant strings are interned so this will be the same object throughout the vm
+    //and we can use it as a monitor for the lockset.
+    private final static String LOCKSET_MONITOR = PROPERTY_PREFIX + ".Lock.Monitor";
     private static final Log LOG = LogFactory.getLog(KahaStore.class);
 
     private final File directory;
@@ -80,7 +82,6 @@
     private long maxDataFileLength = 1024 * 1024 * 32;
     private FileLock lock;
     private boolean persistentIndex = true;
-    private RandomAccessFile lockFile;
     private final AtomicLong storeSize;
     private String defaultContainerName = DEFAULT_CONTAINER_NAME;
 
@@ -109,8 +110,6 @@
             closed = true;
             if (initialized) {
                 unlock();
-                lockFile.close();
-
                 for (ListContainerImpl container : lists.values()) {
                     container.close();
                 }
@@ -130,9 +129,6 @@
                     iter.remove();
                 }
             }
-            if (lockFile != null) {
-                lockFile.close();
-            }
         }
     }
 
@@ -470,14 +466,11 @@
         if (closed) {
             throw new IOException("Store has been closed.");
         }
-        if (!initialized) {
-
-            
-            lockFile = new RandomAccessFile(new File(directory, "lock"), "rw");
-            lock();
+        if (!initialized) {       
             LOG.info("Kaha Store using data directory " + directory);
             DataManager defaultDM = getDataManager(defaultContainerName);
             rootIndexManager = getIndexManager(defaultDM, defaultContainerName);
+            lock();
             IndexItem mapRoot = new IndexItem();
             IndexItem listRoot = new IndexItem();
             if (rootIndexManager.isEmpty()) {
@@ -505,40 +498,38 @@
         }
     }
 
-    private synchronized void lock() throws IOException {
-        if (!DISABLE_LOCKING && directory != null && lock == null) {
-            String key = getPropertyKey();
-            String property = System.getProperty(key);
-            if (null == property) {
-                if (!BROKEN_FILE_LOCK) {
-                    lock = lockFile.getChannel().tryLock();
-                    if (lock == null) {
-                        initialized=false;
-                        throw new StoreLockedExcpetion("Kaha Store " + directory.getName()
-                                                       + "  is already opened by another application");
-                    } else {
-                        System.setProperty(key, new Date().toString());
+    private void lock() throws IOException {
+        synchronized (LOCKSET_MONITOR) {
+            if (!DISABLE_LOCKING && directory != null && lock == null) {
+                String key = getPropertyKey();
+                String property = System.getProperty(key);
+                if (null == property) {
+                    if (!BROKEN_FILE_LOCK) {
+                        lock = rootIndexManager.getLock();
+                        if (lock == null) {
+                            throw new StoreLockedExcpetion("Kaha Store " + directory.getName() + "  is already opened by another application");
+                        } else
+                            System.setProperty(key, new Date().toString());
                     }
+                } else { //already locked
+                    throw new StoreLockedExcpetion("Kaha Store " + directory.getName() + " is already opened by this application.");
                 }
-            } else { // already locked
-                initialized=false;
-                throw new StoreLockedExcpetion("Kaha Store " + directory.getName()
-                                               + " is already opened by this application.");
             }
         }
     }
 
-    private synchronized void unlock() throws IOException {
-        if (!DISABLE_LOCKING && (null != directory) && (null != lock)) {
-            //clear property doesn't work on some platforms
-            System.getProperties().remove(getPropertyKey());
-            if (lock.isValid()) {
-                lock.release();
-                lock.channel().close();
+    private void unlock() throws IOException {
+        synchronized (LOCKSET_MONITOR) {
+            if (!DISABLE_LOCKING && (null != directory) && (null != lock)) {
+                System.getProperties().remove(getPropertyKey());
+                if (lock.isValid()) {
+                    lock.release();
+                }
+                lock = null;
             }
-            lock = null;
         }
     }
+
 
     private String getPropertyKey() throws IOException {
         return getClass().getName() + ".lock." + directory.getCanonicalPath();

Modified: activemq/trunk/activemq-core/src/main/java/org/apache/activemq/kaha/impl/index/IndexManager.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/kaha/impl/index/IndexManager.java?rev=646219&r1=646218&r2=646219&view=diff
==============================================================================
--- activemq/trunk/activemq-core/src/main/java/org/apache/activemq/kaha/impl/index/IndexManager.java (original)
+++ activemq/trunk/activemq-core/src/main/java/org/apache/activemq/kaha/impl/index/IndexManager.java Wed Apr  9 01:26:36 2008
@@ -19,6 +19,7 @@
 import java.io.File;
 import java.io.IOException;
 import java.io.RandomAccessFile;
+import java.nio.channels.FileLock;
 import java.util.concurrent.atomic.AtomicLong;
 
 import org.apache.activemq.kaha.impl.DataManager;
@@ -170,6 +171,11 @@
         this.length = value;
         storeSize.addAndGet(length);
     }
+    
+    public synchronized FileLock getLock() throws IOException {
+        return indexFile.getChannel().tryLock();
+    }
+
 
     public String toString() {
         return "IndexManager:(" + NAME_PREFIX + name + ")";