You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by da...@apache.org on 2026/04/15 18:21:26 UTC
(jackrabbit-oak) branch OAK-12159 created (now fa9198b785)
This is an automated email from the ASF dual-hosted git repository.
daim pushed a change to branch OAK-12159
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
at fa9198b785 OAK-12159 : removed guava cache from oak-blob-plugins
This branch includes the following new commits:
new fa9198b785 OAK-12159 : removed guava cache from oak-blob-plugins
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(jackrabbit-oak) 01/01: OAK-12159 : removed guava cache from oak-blob-plugins
Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
daim pushed a commit to branch OAK-12159
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
commit fa9198b785a0e76a0ad77e67104d7d1ac5cc54bc
Author: rishabhdaim <ri...@gmail.com>
AuthorDate: Wed Apr 15 23:51:06 2026 +0530
OAK-12159 : removed guava cache from oak-blob-plugins
---
.../blob/AbstractSharedCachingDataStore.java | 9 +-
.../oak/plugins/blob/CachingBlobStore.java | 34 +++---
.../oak/plugins/blob/CompositeDataStoreCache.java | 7 +-
.../jackrabbit/oak/plugins/blob/FileCache.java | 116 ++++++++++-----------
.../oak/plugins/blob/UploadStagingCache.java | 2 +-
.../oak/plugins/blob/cloud/CloudBlobStore.java | 2 +-
.../plugins/blob/datastore/DataStoreBlobStore.java | 40 ++++---
.../plugins/blob/AbstractDataStoreCacheTest.java | 4 +-
.../jackrabbit/oak/plugins/blob/FileCacheTest.java | 6 +-
.../org/apache/jackrabbit/oak/cache/api/Cache.java | 19 ++++
.../cache/impl/caffeine/CaffeineCacheAdapter.java | 14 +++
.../oak/cache/impl/lirs/LirsCacheAdapter.java | 10 ++
.../oak/plugins/document/mongo/MongoBlobStore.java | 2 +-
.../document/persistentCache/NodeCache.java | 10 ++
.../oak/plugins/document/rdb/RDBBlobStore.java | 2 +-
15 files changed, 156 insertions(+), 121 deletions(-)
diff --git a/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/AbstractSharedCachingDataStore.java b/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/AbstractSharedCachingDataStore.java
index 718ea36cf0..b35a53210e 100644
--- a/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/AbstractSharedCachingDataStore.java
+++ b/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/AbstractSharedCachingDataStore.java
@@ -46,7 +46,7 @@ import org.apache.jackrabbit.oak.spi.blob.data.DataIdentifier;
import org.apache.jackrabbit.oak.spi.blob.data.DataRecord;
import org.apache.jackrabbit.oak.spi.blob.data.DataStoreException;
import org.apache.jackrabbit.oak.spi.blob.data.MultiDataStoreAware;
-import org.apache.jackrabbit.guava.common.cache.CacheLoader;
+import org.apache.jackrabbit.oak.cache.api.CacheLoader;
import org.apache.jackrabbit.oak.plugins.blob.datastore.TypedDataStore;
import org.apache.jackrabbit.oak.spi.blob.AbstractDataRecord;
import org.apache.jackrabbit.oak.spi.blob.AbstractSharedBackend;
@@ -160,11 +160,8 @@ public abstract class AbstractSharedCachingDataStore extends AbstractDataStore
this.cache =
new CompositeDataStoreCache(path, new File(home), cacheSize, stagingSplitPercentage,
uploadThreads,
- new CacheLoader<String, InputStream>() {
- @Override public InputStream load(String key) throws Exception {
- return backend.read(new DataIdentifier(key));
- }
- }, new StagingUploader() {
+ key -> backend.read(new DataIdentifier(key)),
+ new StagingUploader() {
@Override public void write(String id, File file) throws DataStoreException {
backend.write(new DataIdentifier(id), file);
}
diff --git a/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/CachingBlobStore.java b/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/CachingBlobStore.java
index 1fbb03c0fc..a3b569ef7d 100644
--- a/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/CachingBlobStore.java
+++ b/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/CachingBlobStore.java
@@ -16,12 +16,13 @@
*/
package org.apache.jackrabbit.oak.plugins.blob;
-import org.apache.jackrabbit.guava.common.cache.Weigher;
+import org.apache.jackrabbit.oak.cache.AbstractCacheStats;
import org.apache.jackrabbit.oak.cache.CacheLIRS;
-import org.apache.jackrabbit.oak.cache.CacheStats;
+import org.apache.jackrabbit.oak.cache.api.Cache;
+import org.apache.jackrabbit.oak.cache.api.CacheStatsAdapter;
+import org.apache.jackrabbit.oak.cache.api.Weigher;
import org.apache.jackrabbit.oak.commons.StringUtils;
import org.apache.jackrabbit.oak.spi.blob.AbstractBlobStore;
-import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -34,23 +35,20 @@ public abstract class CachingBlobStore extends AbstractBlobStore {
protected static final long DEFAULT_CACHE_SIZE = 16 * 1024 * 1024;
- protected final CacheLIRS<String, byte[]> cache;
+ protected final Cache<String, byte[]> cache;
protected final long blobCacheSize;
- private final Weigher<String, byte[]> weigher = new Weigher<String, byte[]>() {
- @Override
- public int weigh(@NotNull String key, @NotNull byte[] value) {
- long weight = (long)StringUtils.estimateMemoryUsage(key) + value.length;
- if (weight > Integer.MAX_VALUE) {
- LOG.debug("Calculated weight larger than Integer.MAX_VALUE: {}.", weight);
- weight = Integer.MAX_VALUE;
- }
- return (int) weight;
+ private final Weigher<String, byte[]> weigher = (key, value) -> {
+ long weight = (long)StringUtils.estimateMemoryUsage(key) + value.length;
+ if (weight > Integer.MAX_VALUE) {
+ LOG.debug("Calculated weight larger than Integer.MAX_VALUE: {}.", weight);
+ weight = Integer.MAX_VALUE;
}
+ return (int) weight;
};
- private final CacheStats cacheStats;
+ private final AbstractCacheStats cacheStats;
public static final String MEM_CACHE_NAME = "BlobStore-MemCache";
@@ -61,10 +59,10 @@ public abstract class CachingBlobStore extends AbstractBlobStore {
module(MEM_CACHE_NAME).
maximumWeight(cacheSize).
averageWeight(getBlockSize() / 2).
- weigher(weigher).
- build();
+ weigher(weigher::weigh).
+ build().asOakCache();
- cacheStats = new CacheStats(cache, MEM_CACHE_NAME, weigher, cacheSize);
+ cacheStats = new CacheStatsAdapter(cache, MEM_CACHE_NAME, weigher, cacheSize);
}
public CachingBlobStore() {
@@ -81,7 +79,7 @@ public abstract class CachingBlobStore extends AbstractBlobStore {
return blobCacheSize;
}
- public CacheStats getCacheStats() {
+ public AbstractCacheStats getCacheStats() {
return cacheStats;
}
}
diff --git a/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/CompositeDataStoreCache.java b/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/CompositeDataStoreCache.java
index d1605d326c..1536402f43 100644
--- a/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/CompositeDataStoreCache.java
+++ b/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/CompositeDataStoreCache.java
@@ -27,8 +27,7 @@ import java.io.InputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
-import org.apache.jackrabbit.guava.common.cache.AbstractCache;
-import org.apache.jackrabbit.guava.common.cache.CacheLoader;
+import org.apache.jackrabbit.oak.cache.api.CacheLoader;
import org.apache.jackrabbit.oak.commons.IOUtils;
import org.apache.jackrabbit.oak.stats.StatisticsProvider;
import org.jetbrains.annotations.Nullable;
@@ -37,7 +36,7 @@ import org.slf4j.LoggerFactory;
/**
*/
-public class CompositeDataStoreCache extends AbstractCache<String, File> implements Closeable {
+public class CompositeDataStoreCache implements Closeable {
/**
* Logger instance.
*/
@@ -96,7 +95,6 @@ public class CompositeDataStoreCache extends AbstractCache<String, File> impleme
}
@Nullable
- @Override
public File getIfPresent(Object key) {
return getIfPresent((String) key);
}
@@ -116,7 +114,6 @@ public class CompositeDataStoreCache extends AbstractCache<String, File> impleme
}
}
- @Override
public void invalidate(Object key) {
stagingCache.invalidate((String) key);
downloadCache.invalidate(key);
diff --git a/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/FileCache.java b/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/FileCache.java
index 049b2823b3..94fa0bfed6 100644
--- a/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/FileCache.java
+++ b/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/FileCache.java
@@ -27,7 +27,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.concurrent.Callable;
-import java.util.concurrent.ExecutionException;
+import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
@@ -35,12 +35,12 @@ import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Stream;
import org.apache.commons.io.FileUtils;
-import org.apache.jackrabbit.guava.common.cache.Cache;
-import org.apache.jackrabbit.guava.common.cache.CacheLoader;
import org.apache.jackrabbit.guava.common.cache.RemovalCause;
-import org.apache.jackrabbit.guava.common.cache.Weigher;
import org.apache.jackrabbit.oak.cache.CacheLIRS;
-import org.apache.jackrabbit.oak.cache.CacheStats;
+import org.apache.jackrabbit.oak.cache.api.Cache;
+import org.apache.jackrabbit.oak.cache.api.CacheLoader;
+import org.apache.jackrabbit.oak.cache.api.CacheStatsAdapter;
+import org.apache.jackrabbit.oak.cache.api.Weigher;
import org.apache.jackrabbit.oak.commons.StringUtils;
import org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser;
import org.apache.jackrabbit.oak.commons.io.FileTreeTraverser;
@@ -49,11 +49,9 @@ import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.apache.jackrabbit.guava.common.cache.AbstractCache;
-
/**
*/
-public class FileCache extends AbstractCache<String, File> implements Closeable {
+public class FileCache implements Closeable {
/**
* Logger instance.
*/
@@ -80,7 +78,7 @@ public class FileCache extends AbstractCache<String, File> implements Closeable
*/
private File cacheRoot;
- private CacheLIRS<String, String> cache;
+ private Cache<String, String> cache;
private FileCacheStats cacheStats;
@@ -126,33 +124,30 @@ public class FileCache extends AbstractCache<String, File> implements Closeable
//Rough estimate of the in-memory key, value pair
memWeigher = (key, value) -> (StringUtils.estimateMemoryUsage(key) + 128);
- cacheLoader = new CacheLoader<>() {
- @Override
- public String load(String key) throws Exception {
- // Fetch from local cache directory and if not found load from backend
- File cachedFile = getFile(key);
- if (cachedFile.exists()) {
- return key;
- } else {
- long startNanos = System.nanoTime();
- try (InputStream is = loader.load(key)) {
- copyInputStreamToFile(is, cachedFile);
- } catch (Exception e) {
- LOG.warn("Error reading object for id [{}] from backend", key, e);
- throw e;
- }
- if (LOG.isDebugEnabled()) {
- LOG.debug("Loaded file: {} in {}", key, (System.nanoTime() - startNanos) / 1_000_000);
- }
- return key;
+ cacheLoader = key -> {
+ // Fetch from local cache directory and if not found load from backend
+ File cachedFile = getFile(key);
+ if (cachedFile.exists()) {
+ return key;
+ } else {
+ long startNanos = System.nanoTime();
+ try (InputStream is = loader.load(key)) {
+ copyInputStreamToFile(is, cachedFile);
+ } catch (Exception e) {
+ LOG.warn("Error reading object for id [{}] from backend", key, e);
+ throw e;
+ }
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Loaded file: {} in {}", key, (System.nanoTime() - startNanos) / 1_000_000);
}
+ return key;
}
};
cache = new CacheLIRS.Builder<String, String>()
.maximumWeight(maxBlocks)
.recordStats()
- .weigher(weigher)
+ .weigher(weigher::weigh)
.segmentCount(SEGMENT_COUNT)
.evictionCallback((key, value, cause) -> {
try {
@@ -175,7 +170,7 @@ public class FileCache extends AbstractCache<String, File> implements Closeable
LOG.info("Cached file deletion failed after eviction", e);
}
})
- .build();
+ .build().asOakCache();
this.cacheStats =
new FileCacheStats(cache, weigher, memWeigher, maxSize);
@@ -201,7 +196,7 @@ public class FileCache extends AbstractCache<String, File> implements Closeable
* Get the current entry count (number of files).
*/
public long getEntryCount() {
- return cache.size();
+ return cache.estimatedSize();
}
private FileCache() {
@@ -219,7 +214,7 @@ public class FileCache extends AbstractCache<String, File> implements Closeable
}
return new FileCache() {
- private final Cache<?, ?> cache = new CacheLIRS<>(0);
+ private final Cache<?, ?> cache = new CacheLIRS<>(0).asOakCache();
@Override public void put(String key, File file) {
}
@@ -257,7 +252,6 @@ public class FileCache extends AbstractCache<String, File> implements Closeable
* @param key of the file
* @param file to put into cache
*/
- @Override
public void put(String key, File file) {
adjustSize();
put(key, file, true);
@@ -281,7 +275,7 @@ public class FileCache extends AbstractCache<String, File> implements Closeable
}
public boolean containsKey(String key) {
- return cache.containsKey(key);
+ return cache.asMap().containsKey(key);
}
/**
@@ -302,7 +296,6 @@ public class FileCache extends AbstractCache<String, File> implements Closeable
}
@Nullable
- @Override
public File getIfPresent(Object key) {
return getIfPresent((String) key);
}
@@ -311,16 +304,24 @@ public class FileCache extends AbstractCache<String, File> implements Closeable
adjustSize();
try {
// get from cache and download if not available
- cache.get(key, () -> cacheLoader.load(key));
+ cache.get(key, k -> {
+ try {
+ return cacheLoader.load(k);
+ } catch (RuntimeException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new CompletionException(e);
+ }
+ });
return getFile(key);
- } catch (ExecutionException e) {
+ } catch (RuntimeException e) {
LOG.error("Error loading [{}] from cache", key);
throw new IOException(e);
}
}
private void adjustSize() {
- long currentSize = cache.size();
+ long currentSize = cache.estimatedSize();
if (currentSize > highWaterMark) {
highWaterMark = currentSize;
// low for each additional 50'000 entries
@@ -347,28 +348,27 @@ public class FileCache extends AbstractCache<String, File> implements Closeable
// starting at the current size
currentBlockLimit = Math.max(
currentBlockLimit + 10,
- cache.getUsedMemory() + 10);
+ cache.getUsedWeight() + 10);
// never grow larger than the configured size
currentBlockLimit = Math.min(currentBlockLimit, maxBlocks);
LOG.debug("Grow the cache size to {}", currentBlockLimit);
- cache.setMaxMemory(currentBlockLimit);
+ cache.setMaximumWeight(currentBlockLimit);
}
return;
}
// shrink the cache, 2 percent at the time, starting at the current size
currentBlockLimit = Math.min(
(int) (currentBlockLimit * 0.98 - 1),
- (int) (cache.getUsedMemory() * 0.98 - 1));
+ (int) (cache.getUsedWeight() * 0.98 - 1));
// never grow larger than the configured size
currentBlockLimit = Math.min(currentBlockLimit, maxBlocks);
LOG.info("Shrinking the file cache size to {} because there are {} files (limit: {})",
- currentBlockLimit, cache.size(), maxEntryCount);
- cache.setMaxMemory(currentBlockLimit);
+ currentBlockLimit, cache.estimatedSize(), maxEntryCount);
+ cache.setMaximumWeight(currentBlockLimit);
}
- @Override
public void invalidate(Object key) {
- cache.invalidate(key);
+ cache.invalidate((String) key);
}
public DataStoreCacheStatsMBean getStats() {
@@ -423,9 +423,8 @@ public class FileCache extends AbstractCache<String, File> implements Closeable
}
}
-class FileCacheStats extends CacheStats implements DataStoreCacheStatsMBean {
+class FileCacheStats extends CacheStatsAdapter implements DataStoreCacheStatsMBean {
private static final long BLOCK_SIZE = 4 * 1024;
- private final Weigher<Object, Object> memWeigher;
private final Weigher<Object, Object> weigher;
private final Cache<Object, Object> cache;
@@ -435,40 +434,33 @@ class FileCacheStats extends CacheStats implements DataStoreCacheStatsMBean {
* @param weigher the weigher used to estimate the current weight
* @param maxWeight the maximum weight
*/
+ @SuppressWarnings("unchecked")
public FileCacheStats(Cache<?, ?> cache, Weigher<?, ?> weigher, Weigher<?, ?> memWeigher,
long maxWeight) {
- super(cache, "DataStore-DownloadCache", weigher, maxWeight);
- this.memWeigher = (Weigher<Object, Object>) memWeigher;
- this.weigher = (Weigher<Object, Object>) weigher;
+ super((Cache<Object, Object>) cache, "DataStore-DownloadCache",
+ (Weigher<Object, Object>) weigher, maxWeight);
+ this.weigher = (Weigher<Object, Object>) memWeigher;
this.cache = (Cache<Object, Object>) cache;
}
@Override
public long estimateCurrentMemoryWeight() {
- if (memWeigher == null) {
+ if (weigher == null) {
return -1;
}
long size = 0;
for (Map.Entry<?, ?> e : cache.asMap().entrySet()) {
Object k = e.getKey();
Object v = e.getValue();
- size += memWeigher.weigh(k, v);
+ size += weigher.weigh(k, v);
}
return size;
}
@Override
public long estimateCurrentWeight() {
- if (weigher == null) {
- return -1;
- }
- long size = 0;
- for (Map.Entry<?, ?> e : cache.asMap().entrySet()) {
- Object k = e.getKey();
- Object v = e.getValue();
- size += weigher.weigh(k, v) * BLOCK_SIZE;
- }
- return size;
+ long rawWeight = super.estimateCurrentWeight();
+ return rawWeight < 0 ? rawWeight : rawWeight * BLOCK_SIZE;
}
}
diff --git a/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/UploadStagingCache.java b/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/UploadStagingCache.java
index d63c5a33f5..01c489a0d0 100644
--- a/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/UploadStagingCache.java
+++ b/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/UploadStagingCache.java
@@ -45,7 +45,7 @@ import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
-import org.apache.jackrabbit.guava.common.cache.Weigher;
+import org.apache.jackrabbit.oak.cache.api.Weigher;
import org.apache.jackrabbit.oak.spi.blob.data.DataStoreException;
import org.apache.jackrabbit.oak.commons.StringUtils;
import org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser;
diff --git a/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/cloud/CloudBlobStore.java b/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/cloud/CloudBlobStore.java
index cd56cb9226..facd8e9ece 100644
--- a/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/cloud/CloudBlobStore.java
+++ b/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/cloud/CloudBlobStore.java
@@ -148,7 +148,7 @@ public class CloudBlobStore extends CachingBlobStore {
Objects.requireNonNull(context);
String id = StringUtils.convertBytesToHex(blockId.getDigest());
- byte[] data = cache.get(id);
+ byte[] data = cache.getIfPresent(id);
if (data == null) {
Blob cloudBlob = context.getBlobStore().getBlob(cloudContainer, id);
if (cloudBlob == null) {
diff --git a/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/DataStoreBlobStore.java b/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/DataStoreBlobStore.java
index 0ac27cafe0..6a44a2c855 100644
--- a/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/DataStoreBlobStore.java
+++ b/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/DataStoreBlobStore.java
@@ -34,28 +34,29 @@ import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
-import java.util.concurrent.Callable;
-import java.util.concurrent.ExecutionException;
+import java.util.concurrent.CompletionException;
import java.util.concurrent.TimeUnit;
import javax.jcr.RepositoryException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
+import org.apache.jackrabbit.oak.cache.CacheLIRS;
import org.apache.jackrabbit.oak.spi.blob.data.DataIdentifier;
import org.apache.jackrabbit.oak.spi.blob.data.DataRecord;
import org.apache.jackrabbit.oak.spi.blob.data.DataStore;
import org.apache.jackrabbit.oak.spi.blob.data.DataStoreException;
import org.apache.jackrabbit.oak.spi.blob.data.MultiDataStoreAware;
-import org.apache.jackrabbit.guava.common.cache.LoadingCache;
-import org.apache.jackrabbit.guava.common.cache.Weigher;
import org.apache.jackrabbit.oak.api.Blob;
import org.apache.jackrabbit.oak.api.blob.BlobAccessProvider;
import org.apache.jackrabbit.oak.api.blob.BlobDownloadOptions;
import org.apache.jackrabbit.oak.api.blob.BlobUpload;
import org.apache.jackrabbit.oak.api.blob.BlobUploadOptions;
-import org.apache.jackrabbit.oak.cache.CacheLIRS;
-import org.apache.jackrabbit.oak.cache.CacheStats;
+import org.apache.jackrabbit.oak.cache.AbstractCacheStats;
+import org.apache.jackrabbit.oak.cache.api.Cache;
+import org.apache.jackrabbit.oak.cache.api.CacheBuilder;
+import org.apache.jackrabbit.oak.cache.api.CacheStatsAdapter;
+import org.apache.jackrabbit.oak.cache.api.Weigher;
import org.apache.jackrabbit.oak.commons.StringUtils;
import org.apache.jackrabbit.oak.commons.collections.IteratorUtils;
import org.apache.jackrabbit.oak.plugins.blob.BlobStoreBlob;
@@ -111,7 +112,7 @@ public class DataStoreBlobStore
*/
private final boolean encodeLengthInId;
- protected final LoadingCache<String, byte[]> cache;
+ protected final Cache<String, byte[]> cache;
public static final int DEFAULT_CACHE_SIZE = 16;
@@ -133,7 +134,7 @@ public class DataStoreBlobStore
}
};
- private final CacheStats cacheStats;
+ private final AbstractCacheStats cacheStats;
public static final String MEM_CACHE_NAME = "BlobStore-MemCache";
@@ -156,9 +157,9 @@ public class DataStoreBlobStore
.module(MEM_CACHE_NAME)
.recordStats()
.maximumWeight(cacheSize)
- .weigher(weigher)
- .build();
- this.cacheStats = new CacheStats(cache, MEM_CACHE_NAME, weigher, cacheSize);
+ .weigher(weigher::weigh)
+ .build().asOakCache();
+ this.cacheStats = new CacheStatsAdapter(cache, MEM_CACHE_NAME, weigher, cacheSize);
}
//~----------------------------------< DataStore >
@@ -420,19 +421,16 @@ public class DataStoreBlobStore
&& blobId.hasLengthInfo()
&& blobId.length <= maxCachedBinarySize) {
try {
- byte[] content = cache.get(blobId.blobId, new Callable<byte[]>() {
- @Override
- public byte[] call() throws Exception {
- boolean threw = true;
- try (InputStream stream = getStream(blobId.blobId)) {
- byte[] result = IOUtils.toByteArray(stream);
- return result;
- }
+ byte[] content = cache.get(blobId.blobId, k -> {
+ try (InputStream stream = getStream(blobId.blobId)) {
+ return IOUtils.toByteArray(stream);
+ } catch (IOException e) {
+ throw new CompletionException(e);
}
});
return new ByteArrayInputStream(content);
- } catch (ExecutionException e) {
+ } catch (RuntimeException e) {
log.warn("Error occurred while loading bytes from steam while fetching for id {}", encodedBlobId, e);
}
}
@@ -830,7 +828,7 @@ public class DataStoreBlobStore
return delegate;
}
- public CacheStats getCacheStats() {
+ public AbstractCacheStats getCacheStats() {
return cacheStats;
}
diff --git a/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/AbstractDataStoreCacheTest.java b/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/AbstractDataStoreCacheTest.java
index 83f868986c..8d130ef5d3 100644
--- a/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/AbstractDataStoreCacheTest.java
+++ b/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/AbstractDataStoreCacheTest.java
@@ -55,7 +55,7 @@ import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.apache.jackrabbit.oak.spi.blob.data.DataIdentifier;
import org.apache.jackrabbit.oak.spi.blob.data.DataRecord;
import org.apache.jackrabbit.oak.spi.blob.data.DataStoreException;
-import org.apache.jackrabbit.guava.common.cache.CacheLoader;
+import org.apache.jackrabbit.oak.cache.api.CacheLoader;
import org.apache.jackrabbit.oak.commons.FileIOUtils;
import org.apache.jackrabbit.oak.spi.blob.AbstractDataRecord;
import org.apache.jackrabbit.oak.spi.blob.AbstractSharedBackend;
@@ -112,7 +112,7 @@ public class AbstractDataStoreCacheTest {
}
- static class TestCacheLoader extends CacheLoader<String, InputStream> {
+ static class TestCacheLoader implements CacheLoader<String, InputStream> {
protected File root;
public TestCacheLoader(File dir) {
diff --git a/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/FileCacheTest.java b/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/FileCacheTest.java
index 883435f33e..ef629a4793 100644
--- a/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/FileCacheTest.java
+++ b/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/FileCacheTest.java
@@ -78,7 +78,7 @@ public class FileCacheTest extends AbstractDataStoreCacheTest {
TestExecutor executor = new TestExecutor(1, beforeLatch, afterLatch, afterExecuteLatch);
beforeLatch.countDown();
afterLatch.countDown();
- cache = FileCache.build(4 * 1024/* KB */, root, loader, executor);
+ cache = FileCache.build(16 * 1024/* KB */, root, loader, executor);
FutureUtils.successfulAsList(executor.futures).get();
closer.register(cache);
@@ -235,7 +235,7 @@ public class FileCacheTest extends AbstractDataStoreCacheTest {
public void getDifferentConcurrent() throws Exception {
LOG.info("Started getDifferentConcurrent");
- cache = FileCache.build(4 * 1024/* KB */, root, loader, null);
+ cache = FileCache.build(16 * 1024/* KB */, root, loader, null);
closer.register(cache);
File f = createFile(0, loader, cache, folder);
@@ -464,7 +464,7 @@ public class FileCacheTest extends AbstractDataStoreCacheTest {
TestExecutor executor = new TestExecutor(1, beforeLatch, afterLatch, afterExecuteLatch);
beforeLatch.countDown();
afterLatch.countDown();
- cache = FileCache.build(4 * 1024/* bytes */, root, loader, executor);
+ cache = FileCache.build(16 * 1024/* bytes */, root, loader, executor);
closer.register(cache);
afterExecuteLatch.await();
FutureUtils.successfulAsList(executor.futures).get();
diff --git a/oak-core-spi/src/main/java/org/apache/jackrabbit/oak/cache/api/Cache.java b/oak-core-spi/src/main/java/org/apache/jackrabbit/oak/cache/api/Cache.java
index 1101f74fab..ed4bb976b4 100644
--- a/oak-core-spi/src/main/java/org/apache/jackrabbit/oak/cache/api/Cache.java
+++ b/oak-core-spi/src/main/java/org/apache/jackrabbit/oak/cache/api/Cache.java
@@ -149,4 +149,23 @@ public interface Cache<K, V> {
* future release if it remains unused.</em></p>
*/
void cleanUp();
+
+ /**
+ * Returns the approximate total weight of all currently cached entries,
+ * in the same units as the weigher passed to {@link CacheBuilder#weigher(Weigher)}.
+ * Returns {@code -1} if the cache was not configured with a weigher or
+ * if the implementation does not track weight.
+ *
+ * @return approximate total weight, or {@code -1} if unsupported
+ */
+ long getUsedWeight();
+
+ /**
+ * Dynamically changes the maximum weight (or size) the cache may hold.
+ * The cache will evict entries until it satisfies the new limit.
+ * This is a no-op if the implementation does not support dynamic resizing.
+ *
+ * @param maximumWeight the new maximum weight; must be non-negative
+ */
+ void setMaximumWeight(long maximumWeight);
}
diff --git a/oak-core-spi/src/main/java/org/apache/jackrabbit/oak/cache/impl/caffeine/CaffeineCacheAdapter.java b/oak-core-spi/src/main/java/org/apache/jackrabbit/oak/cache/impl/caffeine/CaffeineCacheAdapter.java
index 44c86c00e0..92510a885e 100644
--- a/oak-core-spi/src/main/java/org/apache/jackrabbit/oak/cache/impl/caffeine/CaffeineCacheAdapter.java
+++ b/oak-core-spi/src/main/java/org/apache/jackrabbit/oak/cache/impl/caffeine/CaffeineCacheAdapter.java
@@ -20,6 +20,7 @@ import java.util.Map;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;
+import com.github.benmanes.caffeine.cache.Policy;
import com.github.benmanes.caffeine.cache.RemovalCause;
import org.apache.jackrabbit.oak.cache.api.CacheStatsSnapshot;
import org.apache.jackrabbit.oak.cache.api.Cache;
@@ -99,6 +100,19 @@ public class CaffeineCacheAdapter<K, V> implements Cache<K, V> {
cache.cleanUp();
}
+ @Override
+ public long getUsedWeight() {
+ return cache.policy().eviction()
+ .map(eviction -> eviction.weightedSize().orElse(cache.estimatedSize()))
+ .orElse(cache.estimatedSize());
+ }
+
+ @Override
+ public void setMaximumWeight(long maximumWeight) {
+ cache.policy().eviction().ifPresent(eviction -> eviction.setMaximum(maximumWeight));
+ }
+
+
/**
* Maps a Caffeine {@code RemovalCause} to the Oak-neutral {@link EvictionCause}.
*/
diff --git a/oak-core-spi/src/main/java/org/apache/jackrabbit/oak/cache/impl/lirs/LirsCacheAdapter.java b/oak-core-spi/src/main/java/org/apache/jackrabbit/oak/cache/impl/lirs/LirsCacheAdapter.java
index ba7860d324..02fbf7f44c 100644
--- a/oak-core-spi/src/main/java/org/apache/jackrabbit/oak/cache/impl/lirs/LirsCacheAdapter.java
+++ b/oak-core-spi/src/main/java/org/apache/jackrabbit/oak/cache/impl/lirs/LirsCacheAdapter.java
@@ -110,6 +110,16 @@ class LirsCacheAdapter<K, V> implements Cache<K, V> {
cache.cleanUp();
}
+ @Override
+ public long getUsedWeight() {
+ return cache.getUsedMemory();
+ }
+
+ @Override
+ public void setMaximumWeight(long maximumWeight) {
+ cache.setMaxMemory(maximumWeight);
+ }
+
/**
* Maps a Guava shim {@code RemovalCause} to the Oak-neutral {@link EvictionCause}.
*/
diff --git a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoBlobStore.java b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoBlobStore.java
index f838f9c3ba..e753249a85 100644
--- a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoBlobStore.java
+++ b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoBlobStore.java
@@ -147,7 +147,7 @@ public class MongoBlobStore extends CachingBlobStore {
@Override
protected byte[] readBlockFromBackend(BlockId blockId) throws Exception {
String id = StringUtils.convertBytesToHex(blockId.getDigest());
- byte[] data = cache.get(id);
+ byte[] data = cache.getIfPresent(id);
if (data == null) {
long start = System.nanoTime();
MongoBlob blobMongo = getBlob(id, 0);
diff --git a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/NodeCache.java b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/NodeCache.java
index 20705e4cb9..2811d2cb4d 100644
--- a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/NodeCache.java
+++ b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/NodeCache.java
@@ -305,6 +305,16 @@ class NodeCache<K extends CacheValue, V extends CacheValue>
memCacheMetadata.clear();
}
+ @Override
+ public long getUsedWeight() {
+ return memCache.getUsedWeight();
+ }
+
+ @Override
+ public void setMaximumWeight(long maximumWeight) {
+ memCache.setMaximumWeight(maximumWeight);
+ }
+
@Override
@SuppressWarnings("unchecked")
public void receive(ByteBuffer buff) {
diff --git a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBBlobStore.java b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBBlobStore.java
index 7c19c44eda..5faca925aa 100644
--- a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBBlobStore.java
+++ b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBBlobStore.java
@@ -438,7 +438,7 @@ public class RDBBlobStore extends CachingBlobStore implements Closeable {
protected byte[] readBlockFromBackend(BlockId blockId) throws Exception {
String id = StringUtils.convertBytesToHex(blockId.getDigest());
- byte[] data = cache.get(id);
+ byte[] data = cache.getIfPresent(id);
if (data == null) {
long start = System.nanoTime();