Skip to content

Commit

Permalink
add soft ref to all memory cache
Browse files Browse the repository at this point in the history
  • Loading branch information
JingsongLi committed Jul 31, 2024
1 parent 47c7bf5 commit c631b3f
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import org.apache.paimon.shade.caffeine2.com.github.benmanes.caffeine.cache.Cache;
import org.apache.paimon.shade.caffeine2.com.github.benmanes.caffeine.cache.Caffeine;
import org.apache.paimon.shade.guava30.com.google.common.util.concurrent.MoreExecutors;

import org.codehaus.janino.SimpleCompiler;
import org.slf4j.Logger;
Expand All @@ -46,12 +45,12 @@ public final class CompileUtils {
*/
static final Cache<ClassKey, Class<?>> COMPILED_CLASS_CACHE =
Caffeine.newBuilder()
.softValues()
// estimated maximum planning/startup time
.expireAfterAccess(Duration.ofMinutes(5))
// estimated cache size
.maximumSize(300)
.softValues()
.executor(MoreExecutors.directExecutor())
.executor(Runnable::run)
.build();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.apache.paimon.shade.caffeine2.com.github.benmanes.caffeine.cache.Cache;
import org.apache.paimon.shade.caffeine2.com.github.benmanes.caffeine.cache.Caffeine;
import org.apache.paimon.shade.caffeine2.com.github.benmanes.caffeine.cache.RemovalCause;
import org.apache.paimon.shade.guava30.com.google.common.util.concurrent.MoreExecutors;

import java.io.IOException;

Expand All @@ -45,16 +44,17 @@ public class CacheManager {
public CacheManager(MemorySize maxMemorySize) {
this.cache =
Caffeine.newBuilder()
.softValues()
.weigher(this::weigh)
.maximumWeight(maxMemorySize.getBytes())
.removalListener(this::onRemoval)
.executor(MoreExecutors.directExecutor())
.executor(Runnable::run)
.build();
this.fileReadCount = 0;
}

@VisibleForTesting
public Cache<CacheKey, CacheValue> cache() {
public Cache<CacheKey, ?> cache() {
return cache;
}

Expand All @@ -81,8 +81,10 @@ private int weigh(CacheKey cacheKey, CacheValue cacheValue) {
}

private void onRemoval(CacheKey key, CacheValue value, RemovalCause cause) {
value.isClosed = true;
value.callback.onRemoval(key);
if (value != null) {
value.isClosed = true;
value.callback.onRemoval(key);
}
}

public int fileReadCount() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

import org.apache.paimon.shade.caffeine2.com.github.benmanes.caffeine.cache.Cache;
import org.apache.paimon.shade.caffeine2.com.github.benmanes.caffeine.cache.Caffeine;
import org.apache.paimon.shade.guava30.com.google.common.util.concurrent.MoreExecutors;

import org.rocksdb.ColumnFamilyHandle;
import org.rocksdb.RocksDB;
Expand Down Expand Up @@ -80,8 +79,9 @@ public RocksDBState(
this.writeOptions = new WriteOptions().setDisableWAL(true);
this.cache =
Caffeine.newBuilder()
.softValues()
.maximumSize(lruCacheSize)
.executor(MoreExecutors.directExecutor())
.executor(Runnable::run)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public LookupLevels(
this.lookupStoreFactory = lookupStoreFactory;
this.lookupFiles =
Caffeine.newBuilder()
.softValues()
.expireAfterAccess(fileRetention)
.maximumWeight(maxDiskSize.getKibiBytes())
.weigher(this::fileWeigh)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import org.apache.paimon.shade.caffeine2.com.github.benmanes.caffeine.cache.Cache;
import org.apache.paimon.shade.caffeine2.com.github.benmanes.caffeine.cache.Caffeine;
import org.apache.paimon.shade.guava30.com.google.common.util.concurrent.MoreExecutors;

import javax.annotation.Nullable;

Expand All @@ -43,9 +42,10 @@ public SegmentsCache(int pageSize, MemorySize maxMemorySize) {
this.pageSize = pageSize;
this.cache =
Caffeine.newBuilder()
.softValues()
.weigher(this::weigh)
.maximumWeight(maxMemorySize.getBytes())
.executor(MoreExecutors.directExecutor())
.executor(Runnable::run)
.build();
}

Expand Down

0 comments on commit c631b3f

Please sign in to comment.