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
This commit is contained in:
Christopher L. Shannon (cshannon) 2016-09-23 15:47:27 -04:00
parent 09456480b8
commit a82c95cd29
3 changed files with 84 additions and 10 deletions

View File

@ -39,7 +39,9 @@ import org.apache.activemq.util.IOHelper;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -52,6 +54,9 @@ public abstract class PListTestSupport {
final Vector<Throwable> exceptions = new Vector<Throwable>(); final Vector<Throwable> exceptions = new Vector<Throwable>();
ExecutorService executor; ExecutorService executor;
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();
@Test @Test
public void testAddLast() throws Exception { public void testAddLast() throws Exception {
final int COUNT = 1000; final int COUNT = 1000;
@ -645,11 +650,8 @@ public abstract class PListTestSupport {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
File directory = new File("target/test/PlistDB"); File directory = tempFolder.newFolder();
IOHelper.mkdirs(directory);
IOHelper.deleteChildren(directory);
startStore(directory); startStore(directory);
} }
protected void startStore(File directory) throws Exception { protected void startStore(File directory) throws Exception {

View File

@ -162,12 +162,14 @@ public class PListStoreImpl extends ServiceSupport implements BrokerServiceAware
MetaDataMarshaller(PListStoreImpl store) { MetaDataMarshaller(PListStoreImpl store) {
this.store = store; this.store = store;
} }
@Override
public MetaData readPayload(DataInput dataIn) throws IOException { public MetaData readPayload(DataInput dataIn) throws IOException {
MetaData rc = new MetaData(this.store); MetaData rc = new MetaData(this.store);
rc.read(dataIn); rc.read(dataIn);
return rc; return rc;
} }
@Override
public void writePayload(MetaData object, DataOutput dataOut) throws IOException { public void writePayload(MetaData object, DataOutput dataOut) throws IOException {
object.write(dataOut); object.write(dataOut);
} }
@ -178,12 +180,14 @@ public class PListStoreImpl extends ServiceSupport implements BrokerServiceAware
PListMarshaller(PListStoreImpl store) { PListMarshaller(PListStoreImpl store) {
this.store = store; this.store = store;
} }
@Override
public PListImpl readPayload(DataInput dataIn) throws IOException { public PListImpl readPayload(DataInput dataIn) throws IOException {
PListImpl result = new PListImpl(this.store); PListImpl result = new PListImpl(this.store);
result.read(dataIn); result.read(dataIn);
return result; return result;
} }
@Override
public void writePayload(PListImpl list, DataOutput dataOut) throws IOException { public void writePayload(PListImpl list, DataOutput dataOut) throws IOException {
list.write(dataOut); list.write(dataOut);
} }
@ -211,6 +215,7 @@ public class PListStoreImpl extends ServiceSupport implements BrokerServiceAware
this.indexDirectory = indexDirectory; this.indexDirectory = indexDirectory;
} }
@Override
public long size() { public long size() {
synchronized (this) { synchronized (this) {
if (!initialized) { if (!initialized) {
@ -237,6 +242,7 @@ public class PListStoreImpl extends ServiceSupport implements BrokerServiceAware
final PListImpl pl = new PListImpl(this); final PListImpl pl = new PListImpl(this);
pl.setName(name); pl.setName(name);
getPageFile().tx().execute(new Transaction.Closure<IOException>() { getPageFile().tx().execute(new Transaction.Closure<IOException>() {
@Override
public void execute(Transaction tx) throws IOException { public void execute(Transaction tx) throws IOException {
pl.setHeadPageId(tx.allocate().getPageId()); pl.setHeadPageId(tx.allocate().getPageId());
pl.load(tx); pl.load(tx);
@ -248,6 +254,7 @@ public class PListStoreImpl extends ServiceSupport implements BrokerServiceAware
} }
final PListImpl toLoad = result; final PListImpl toLoad = result;
getPageFile().tx().execute(new Transaction.Closure<IOException>() { getPageFile().tx().execute(new Transaction.Closure<IOException>() {
@Override
public void execute(Transaction tx) throws IOException { public void execute(Transaction tx) throws IOException {
toLoad.load(tx); toLoad.load(tx);
} }
@ -267,6 +274,7 @@ public class PListStoreImpl extends ServiceSupport implements BrokerServiceAware
result = pl != null; result = pl != null;
if (result) { if (result) {
getPageFile().tx().execute(new Transaction.Closure<IOException>() { getPageFile().tx().execute(new Transaction.Closure<IOException>() {
@Override
public void execute(Transaction tx) throws IOException { public void execute(Transaction tx) throws IOException {
metaData.lists.remove(tx, name); metaData.lists.remove(tx, name);
pl.destroy(); pl.destroy();
@ -282,7 +290,7 @@ public class PListStoreImpl extends ServiceSupport implements BrokerServiceAware
if (isStarted()) { if (isStarted()) {
if (this.initialized == false) { if (this.initialized == false) {
if (this.directory == null) { if (this.directory == null) {
this.directory = new File(IOHelper.getDefaultDataDirectory() + File.pathSeparator + "delayedDB"); this.directory = getDefaultDirectory();
} }
IOHelper.mkdirs(this.directory); IOHelper.mkdirs(this.directory);
IOHelper.deleteChildren(this.directory); IOHelper.deleteChildren(this.directory);
@ -304,6 +312,7 @@ public class PListStoreImpl extends ServiceSupport implements BrokerServiceAware
this.pageFile.load(); this.pageFile.load();
this.pageFile.tx().execute(new Transaction.Closure<IOException>() { this.pageFile.tx().execute(new Transaction.Closure<IOException>() {
@Override
public void execute(Transaction tx) throws IOException { public void execute(Transaction tx) throws IOException {
if (pageFile.getPageCount() == 0) { if (pageFile.getPageCount() == 0) {
Page<MetaData> page = tx.allocate(); 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 @Override
protected synchronized void doStart() throws Exception { protected synchronized void doStart() throws Exception {
if (!lazyInit) { if (!lazyInit) {
intialize(); 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"); LOG.info(this + " started");
} }
@ -371,6 +397,7 @@ public class PListStoreImpl extends ServiceSupport implements BrokerServiceAware
} }
@Override
public void run() { public void run() {
try { try {
if (isStopping()) { if (isStopping()) {
@ -455,6 +482,7 @@ public class PListStoreImpl extends ServiceSupport implements BrokerServiceAware
this.failIfDatabaseIsLocked = failIfDatabaseIsLocked; this.failIfDatabaseIsLocked = failIfDatabaseIsLocked;
} }
@Override
public int getJournalMaxFileLength() { public int getJournalMaxFileLength() {
return journalMaxFileLength; return journalMaxFileLength;
} }

View File

@ -16,14 +16,18 @@
*/ */
package org.apache.activemq.store.kahadb.plist; package org.apache.activemq.store.kahadb.plist;
import org.apache.activemq.store.PListStore; import static org.junit.Assert.assertEquals;
import org.apache.activemq.store.PListTestSupport; import static org.junit.Assert.assertFalse;
import org.junit.Test; import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import java.io.File; import java.io.File;
import static org.junit.Assert.assertEquals; import org.apache.activemq.store.PListStore;
import static org.junit.Assert.assertNotEquals; 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> * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
@ -36,6 +40,7 @@ public class PListImplTest extends PListTestSupport {
return new PListStoreImpl(); return new PListStoreImpl();
} }
@Override
protected PListStore createConcurrentAddIteratePListStore() { protected PListStore createConcurrentAddIteratePListStore() {
PListStoreImpl store = createPListStore(); PListStoreImpl store = createPListStore();
store.setIndexPageSize(2 * 1024); store.setIndexPageSize(2 * 1024);
@ -84,10 +89,49 @@ public class PListImplTest extends PListTestSupport {
final File directory = pListStore.getDirectory(); final File directory = pListStore.getDirectory();
pListStore.stop(); pListStore.stop();
pListStore = createPListStore(); pListStore = createPListStore();
pListStore.setDirectory(directory);
pListStore.setLazyInit(false); pListStore.setLazyInit(false);
pListStore.setIndexDirectory(new File(directory, "indexDir")); pListStore.setIndexDirectory(new File(directory, "indexDir"));
pListStore.start(); pListStore.start();
assertNotEquals(pListStore.getDirectory(), pListStore.getIndexDirectory()); assertNotEquals(pListStore.getDirectory(), pListStore.getIndexDirectory());
pListStore.stop(); 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();
}
} }