Repository: activemq
Updated Branches:
  refs/heads/master 09456480b -> a82c95cd2


https://issues.apache.org/jira/browse/AMQ-6436

The temporary store will now delete the old temp directory on start up
if lazyInit is true instead of waiting for the store to initialize to
clear up space.  This prevents space on the disk from being wasted with
old data if the temp store isn't initialized


Project: http://git-wip-us.apache.org/repos/asf/activemq/repo
Commit: http://git-wip-us.apache.org/repos/asf/activemq/commit/a82c95cd
Tree: http://git-wip-us.apache.org/repos/asf/activemq/tree/a82c95cd
Diff: http://git-wip-us.apache.org/repos/asf/activemq/diff/a82c95cd

Branch: refs/heads/master
Commit: a82c95cd29a6b06d2083b1869129b9e2addac7da
Parents: 0945648
Author: Christopher L. Shannon (cshannon) <[email protected]>
Authored: Fri Sep 23 15:47:27 2016 -0400
Committer: Christopher L. Shannon (cshannon) <[email protected]>
Committed: Fri Sep 23 15:47:27 2016 -0400

----------------------------------------------------------------------
 .../apache/activemq/store/PListTestSupport.java | 10 ++--
 .../store/kahadb/plist/PListStoreImpl.java      | 30 ++++++++++-
 .../store/kahadb/plist/PListImplTest.java       | 54 ++++++++++++++++++--
 3 files changed, 84 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/activemq/blob/a82c95cd/activemq-broker/src/test/java/org/apache/activemq/store/PListTestSupport.java
----------------------------------------------------------------------
diff --git 
a/activemq-broker/src/test/java/org/apache/activemq/store/PListTestSupport.java 
b/activemq-broker/src/test/java/org/apache/activemq/store/PListTestSupport.java
index fb265b0..5b51656 100644
--- 
a/activemq-broker/src/test/java/org/apache/activemq/store/PListTestSupport.java
+++ 
b/activemq-broker/src/test/java/org/apache/activemq/store/PListTestSupport.java
@@ -39,7 +39,9 @@ import org.apache.activemq.util.IOHelper;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Ignore;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -52,6 +54,9 @@ public abstract class PListTestSupport {
     final Vector<Throwable> exceptions = new Vector<Throwable>();
     ExecutorService executor;
 
+    @Rule
+    public TemporaryFolder tempFolder = new TemporaryFolder();
+
     @Test
     public void testAddLast() throws Exception {
         final int COUNT = 1000;
@@ -645,11 +650,8 @@ public abstract class PListTestSupport {
 
     @Before
     public void setUp() throws Exception {
-        File directory = new File("target/test/PlistDB");
-        IOHelper.mkdirs(directory);
-        IOHelper.deleteChildren(directory);
+        File directory = tempFolder.newFolder();
         startStore(directory);
-
     }
 
     protected void startStore(File directory) throws Exception {

http://git-wip-us.apache.org/repos/asf/activemq/blob/a82c95cd/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/plist/PListStoreImpl.java
----------------------------------------------------------------------
diff --git 
a/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/plist/PListStoreImpl.java
 
b/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/plist/PListStoreImpl.java
index e39c313..cce93f2 100644
--- 
a/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/plist/PListStoreImpl.java
+++ 
b/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/plist/PListStoreImpl.java
@@ -162,12 +162,14 @@ public class PListStoreImpl extends ServiceSupport 
implements BrokerServiceAware
         MetaDataMarshaller(PListStoreImpl store) {
             this.store = store;
         }
+        @Override
         public MetaData readPayload(DataInput dataIn) throws IOException {
             MetaData rc = new MetaData(this.store);
             rc.read(dataIn);
             return rc;
         }
 
+        @Override
         public void writePayload(MetaData object, DataOutput dataOut) throws 
IOException {
             object.write(dataOut);
         }
@@ -178,12 +180,14 @@ public class PListStoreImpl extends ServiceSupport 
implements BrokerServiceAware
         PListMarshaller(PListStoreImpl store) {
             this.store = store;
         }
+        @Override
         public PListImpl readPayload(DataInput dataIn) throws IOException {
             PListImpl result = new PListImpl(this.store);
             result.read(dataIn);
             return result;
         }
 
+        @Override
         public void writePayload(PListImpl list, DataOutput dataOut) throws 
IOException {
             list.write(dataOut);
         }
@@ -211,6 +215,7 @@ public class PListStoreImpl extends ServiceSupport 
implements BrokerServiceAware
         this.indexDirectory = indexDirectory;
     }
 
+    @Override
     public long size() {
         synchronized (this) {
             if (!initialized) {
@@ -237,6 +242,7 @@ public class PListStoreImpl extends ServiceSupport 
implements BrokerServiceAware
                     final PListImpl pl = new PListImpl(this);
                     pl.setName(name);
                     getPageFile().tx().execute(new 
Transaction.Closure<IOException>() {
+                        @Override
                         public void execute(Transaction tx) throws IOException 
{
                             pl.setHeadPageId(tx.allocate().getPageId());
                             pl.load(tx);
@@ -248,6 +254,7 @@ public class PListStoreImpl extends ServiceSupport 
implements BrokerServiceAware
                 }
                 final PListImpl toLoad = result;
                 getPageFile().tx().execute(new 
Transaction.Closure<IOException>() {
+                    @Override
                     public void execute(Transaction tx) throws IOException {
                         toLoad.load(tx);
                     }
@@ -267,6 +274,7 @@ public class PListStoreImpl extends ServiceSupport 
implements BrokerServiceAware
                 result = pl != null;
                 if (result) {
                     getPageFile().tx().execute(new 
Transaction.Closure<IOException>() {
+                        @Override
                         public void execute(Transaction tx) throws IOException 
{
                             metaData.lists.remove(tx, name);
                             pl.destroy();
@@ -282,7 +290,7 @@ public class PListStoreImpl extends ServiceSupport 
implements BrokerServiceAware
         if (isStarted()) {
             if (this.initialized == false) {
                 if (this.directory == null) {
-                    this.directory = new 
File(IOHelper.getDefaultDataDirectory() + File.pathSeparator + "delayedDB");
+                    this.directory = getDefaultDirectory();
                 }
                 IOHelper.mkdirs(this.directory);
                 IOHelper.deleteChildren(this.directory);
@@ -304,6 +312,7 @@ public class PListStoreImpl extends ServiceSupport 
implements BrokerServiceAware
                 this.pageFile.load();
 
                 this.pageFile.tx().execute(new 
Transaction.Closure<IOException>() {
+                    @Override
                     public void execute(Transaction tx) throws IOException {
                         if (pageFile.getPageCount() == 0) {
                             Page<MetaData> page = tx.allocate();
@@ -337,10 +346,27 @@ public class PListStoreImpl extends ServiceSupport 
implements BrokerServiceAware
         }
     }
 
+    protected File getDefaultDirectory() {
+        return new File(IOHelper.getDefaultDataDirectory() + 
File.pathSeparator + "delayedDB");
+    }
+
+    protected void cleanupDirectory(final File dir) {
+        if (dir != null && dir.exists()) {
+            IOHelper.delete(dir);
+        }
+    }
+
     @Override
     protected synchronized void doStart() throws Exception {
         if (!lazyInit) {
             intialize();
+        } else {
+            if (this.directory == null) {
+                this.directory = getDefaultDirectory();
+            }
+            //Go ahead and clean up previous data on start up
+            cleanupDirectory(this.directory);
+            cleanupDirectory(this.indexDirectory);
         }
         LOG.info(this + " started");
     }
@@ -371,6 +397,7 @@ public class PListStoreImpl extends ServiceSupport 
implements BrokerServiceAware
 
     }
 
+    @Override
     public void run() {
         try {
             if (isStopping()) {
@@ -455,6 +482,7 @@ public class PListStoreImpl extends ServiceSupport 
implements BrokerServiceAware
         this.failIfDatabaseIsLocked = failIfDatabaseIsLocked;
     }
 
+    @Override
     public int getJournalMaxFileLength() {
         return journalMaxFileLength;
     }

http://git-wip-us.apache.org/repos/asf/activemq/blob/a82c95cd/activemq-kahadb-store/src/test/java/org/apache/activemq/store/kahadb/plist/PListImplTest.java
----------------------------------------------------------------------
diff --git 
a/activemq-kahadb-store/src/test/java/org/apache/activemq/store/kahadb/plist/PListImplTest.java
 
b/activemq-kahadb-store/src/test/java/org/apache/activemq/store/kahadb/plist/PListImplTest.java
index eaf4287..ba702b7 100644
--- 
a/activemq-kahadb-store/src/test/java/org/apache/activemq/store/kahadb/plist/PListImplTest.java
+++ 
b/activemq-kahadb-store/src/test/java/org/apache/activemq/store/kahadb/plist/PListImplTest.java
@@ -16,14 +16,18 @@
  */
 package org.apache.activemq.store.kahadb.plist;
 
-import org.apache.activemq.store.PListStore;
-import org.apache.activemq.store.PListTestSupport;
-import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertTrue;
 
 import java.io.File;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotEquals;
+import org.apache.activemq.store.PListStore;
+import org.apache.activemq.store.PListTestSupport;
+import org.apache.activemq.util.IOHelper;
+import org.junit.Ignore;
+import org.junit.Test;
 
 /**
  * @author <a href="http://hiramchirino.com";>Hiram Chirino</a>
@@ -36,6 +40,7 @@ public class PListImplTest extends PListTestSupport {
         return new PListStoreImpl();
     }
 
+    @Override
     protected PListStore createConcurrentAddIteratePListStore() {
         PListStoreImpl store = createPListStore();
         store.setIndexPageSize(2 * 1024);
@@ -84,10 +89,49 @@ public class PListImplTest extends PListTestSupport {
         final File directory = pListStore.getDirectory();
         pListStore.stop();
         pListStore = createPListStore();
+        pListStore.setDirectory(directory);
         pListStore.setLazyInit(false);
         pListStore.setIndexDirectory(new File(directory, "indexDir"));
         pListStore.start();
         assertNotEquals(pListStore.getDirectory(), 
pListStore.getIndexDirectory());
         pListStore.stop();
     }
+
+    //Test that when lazy init is true that the directory gets cleaned up on 
start up
+    @Test
+    public void testLazyInitCleanup() throws Exception {
+        PListStoreImpl pListStore = (PListStoreImpl)store;
+        File directory = pListStore.getDirectory();
+        File indexDir = tempFolder.newFolder();
+        pListStore.stop();
+
+        //Restart one time with index directory so everything gets created
+        pListStore = createPListStore();
+        pListStore.setLazyInit(false);
+        pListStore.setDirectory(directory);
+        pListStore.setIndexDirectory(indexDir);
+        pListStore.start();
+        pListStore.stop();
+
+        assertTrue(directory.exists());
+        assertTrue(indexDir.exists());
+
+        //restart again with lazy init true and make sure that the directories 
are cleared
+        pListStore = createPListStore();
+        pListStore.setLazyInit(true);
+        pListStore.setDirectory(directory);
+        pListStore.setIndexDirectory(indexDir);
+
+        //assert that start cleaned up old data
+        pListStore.start();
+        assertFalse(directory.exists());
+        assertFalse(indexDir.exists());
+
+        //assert that initialize re-created the data dirs
+        pListStore.intialize();
+        assertTrue(directory.exists());
+        assertTrue(indexDir.exists());
+        pListStore.stop();
+
+    }
 }

Reply via email to