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.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 {

View File

@ -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;
}

View File

@ -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();
}
}