Author: thomasm
Date: Fri May 22 09:29:15 2015
New Revision: 1681053
URL: http://svn.apache.org/r1681053
Log:
OAK-2837 Persistent cache: avoid repeated log message after closing
Modified:
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/PersistentCache.java
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheTest.java
Modified:
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/PersistentCache.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/PersistentCache.java?rev=1681053&r1=1681052&r2=1681053&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/PersistentCache.java
(original)
+++
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/PersistentCache.java
Fri May 22 09:29:15 2015
@@ -67,6 +67,8 @@ public class PersistentCache {
private int autoCompact = 50;
private boolean appendOnly;
private boolean manualCommit;
+
+ private int exceptionCount;
public PersistentCache(String url) {
LOG.info("start version 1");
@@ -204,6 +206,7 @@ public class PersistentCache {
builder.backgroundExceptionHandler(new
Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
+ exceptionCount++;
LOG.debug("Error in the background thread of the
persistent cache", e);
LOG.warn("Error in the background thread of the
persistent cache: " + e);
}
@@ -213,6 +216,7 @@ public class PersistentCache {
store.setReuseSpace(false);
}
} catch (Exception e) {
+ exceptionCount++;
LOG.warn("Could not open the store " + fileName, e);
}
}
@@ -231,6 +235,7 @@ public class PersistentCache {
Thread.interrupted();
store.close();
} catch (Exception e) {
+ exceptionCount++;
LOG.debug("Could not close the store", e);
LOG.warn("Could not close the store: " + e);
store.closeImmediately();
@@ -239,6 +244,7 @@ public class PersistentCache {
try {
MVStoreTool.compact(fileName, true);
} catch (Exception e) {
+ exceptionCount++;
LOG.debug("Could not compact the store", e);
LOG.warn("Could not compact the store: " + e);
}
@@ -254,6 +260,7 @@ public class PersistentCache {
}
return store.openMap(name, builder);
} catch (Exception e) {
+ exceptionCount++;
LOG.warn("Could not open the map", e);
return null;
}
@@ -262,12 +269,16 @@ public class PersistentCache {
@Override
long getFileSize() {
try {
+ if (store == null) {
+ return 0;
+ }
FileStore fs = store.getFileStore();
if (fs == null) {
return 0;
}
return fs.size();
} catch (Exception e) {
+ exceptionCount++;
LOG.warn("Could not retrieve the map size", e);
return 0;
}
@@ -345,6 +356,7 @@ public class PersistentCache {
} else if (generation == writeGeneration) {
s = writeStore;
} else {
+ exceptionCount++;
throw new IllegalArgumentException("Unknown generation: " +
generation);
}
return new CacheMap<K, V>(s, name, builder);
@@ -399,6 +411,10 @@ public class PersistentCache {
public int getOpenCount() {
return writeStore.getOpenCount();
}
+
+ public int getExceptionCount() {
+ return exceptionCount;
+ }
interface GenerationCache {
Modified:
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheTest.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheTest.java?rev=1681053&r1=1681052&r2=1681053&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheTest.java
(original)
+++
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheTest.java
Fri May 22 09:29:15 2015
@@ -24,9 +24,17 @@ import static org.junit.Assert.assertTru
import java.io.ByteArrayInputStream;
import java.io.File;
+import java.io.FileOutputStream;
import java.util.Random;
import org.apache.commons.io.FileUtils;
+import org.apache.jackrabbit.oak.cache.CacheLIRS;
+import org.apache.jackrabbit.oak.plugins.document.util.StringValue;
+
+import com.google.common.cache.Cache;
+
+import org.apache.jackrabbit.oak.plugins.document.PathRev;
+import org.apache.jackrabbit.oak.plugins.document.Revision;
import org.apache.jackrabbit.oak.spi.blob.BlobStore;
import org.apache.jackrabbit.oak.spi.blob.MemoryBlobStore;
import org.junit.Test;
@@ -34,6 +42,33 @@ import org.junit.Test;
public class CacheTest {
@Test
+ public void recoverIfCorrupt() throws Exception {
+ FileUtils.deleteDirectory(new File("target/cacheTest"));
+ new File("target/cacheTest").mkdirs();
+ FileOutputStream out = new
FileOutputStream("target/cacheTest/cache-0.data");
+ out.write("corrupt".getBytes());
+ out.close();
+ PersistentCache pCache = new PersistentCache("target/cacheTest");
+ CacheLIRS<PathRev, StringValue> cache = new CacheLIRS.Builder().
+ maximumSize(1).build();
+ Cache<PathRev, StringValue> map = pCache.wrap(null, null, cache,
CacheType.DIFF);
+ String largeString = new String(new char[1024 * 1024]);
+ for (int counter = 0; counter < 10; counter++) {
+ long end = System.currentTimeMillis() + 100;
+ while (System.currentTimeMillis() < end) {
+ Thread.yield();
+ }
+ for (int i = 0; i < 100; i++) {
+ PathRev k = new PathRev("/" + counter, new Revision(0, 0, i));
+ map.getIfPresent(k);
+ map.put(k, new StringValue(largeString));
+ }
+ }
+ assertTrue("Exceptions: " + pCache.getExceptionCount(),
+ pCache.getExceptionCount() < 100);
+ }
+
+ @Test
public void closeAlways() throws Exception {
FileUtils.deleteDirectory(new File("target/cacheTest"));
PersistentCache cache = new
PersistentCache("target/cacheTest,manualCommit");