You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@paimon.apache.org by lz...@apache.org on 2023/04/06 13:39:30 UTC
[incubator-paimon] branch master updated: [common] Use caffeine to replace with guava cache (#665)
This is an automated email from the ASF dual-hosted git repository.
lzljs3620320 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-paimon.git
The following commit(s) were added to refs/heads/master by this push:
new 96800458c [common] Use caffeine to replace with guava cache (#665)
96800458c is described below
commit 96800458c717acd42a9e9d6f8b06801770aa9b14
Author: Kerwin <37...@users.noreply.github.com>
AuthorDate: Thu Apr 6 21:39:25 2023 +0800
[common] Use caffeine to replace with guava cache (#665)
---
.../org/apache/paimon/codegen/CompileUtils.java | 8 +++----
.../org/apache/paimon/io/cache/CacheManager.java | 28 ++++++++++++----------
.../paimon/io/cache/CachedRandomInputViewTest.java | 2 +-
3 files changed, 21 insertions(+), 17 deletions(-)
diff --git a/paimon-common/src/main/java/org/apache/paimon/codegen/CompileUtils.java b/paimon-common/src/main/java/org/apache/paimon/codegen/CompileUtils.java
index 8d66de5d4..6bfc1c38d 100644
--- a/paimon-common/src/main/java/org/apache/paimon/codegen/CompileUtils.java
+++ b/paimon-common/src/main/java/org/apache/paimon/codegen/CompileUtils.java
@@ -18,8 +18,8 @@
package org.apache.paimon.codegen;
-import org.apache.paimon.shade.guava30.com.google.common.cache.Cache;
-import org.apache.paimon.shade.guava30.com.google.common.cache.CacheBuilder;
+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.codehaus.janino.SimpleCompiler;
import org.slf4j.Logger;
@@ -44,7 +44,7 @@ public final class CompileUtils {
* avoid this problem.
*/
static final Cache<ClassKey, Class<?>> COMPILED_CLASS_CACHE =
- CacheBuilder.newBuilder()
+ Caffeine.newBuilder()
// estimated maximum planning/startup time
.expireAfterAccess(Duration.ofMinutes(5))
// estimated cache size
@@ -68,7 +68,7 @@ public final class CompileUtils {
// to prevent class leaks we don't cache the class loader directly
// but only its hash code
final ClassKey classKey = new ClassKey(cl.hashCode(), code);
- return (Class<T>) COMPILED_CLASS_CACHE.get(classKey, () -> doCompile(cl, name, code));
+ return (Class<T>) COMPILED_CLASS_CACHE.get(classKey, key -> doCompile(cl, name, code));
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
diff --git a/paimon-common/src/main/java/org/apache/paimon/io/cache/CacheManager.java b/paimon-common/src/main/java/org/apache/paimon/io/cache/CacheManager.java
index e3c4edef0..7e758b445 100644
--- a/paimon-common/src/main/java/org/apache/paimon/io/cache/CacheManager.java
+++ b/paimon-common/src/main/java/org/apache/paimon/io/cache/CacheManager.java
@@ -21,14 +21,13 @@ package org.apache.paimon.io.cache;
import org.apache.paimon.annotation.VisibleForTesting;
import org.apache.paimon.memory.MemorySegment;
import org.apache.paimon.options.MemorySize;
-import org.apache.paimon.shade.guava30.com.google.common.cache.Cache;
-import org.apache.paimon.shade.guava30.com.google.common.cache.CacheBuilder;
-import org.apache.paimon.shade.guava30.com.google.common.cache.RemovalNotification;
+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 java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Objects;
-import java.util.concurrent.ExecutionException;
import java.util.function.Consumer;
/** Cache manager to cache bytes to paged {@link MemorySegment}s. */
@@ -40,7 +39,7 @@ public class CacheManager {
public CacheManager(int pageSize, MemorySize maxMemorySize) {
this.pageSize = pageSize;
this.cache =
- CacheBuilder.newBuilder()
+ Caffeine.newBuilder()
.weigher(this::weigh)
.maximumWeight(maxMemorySize.getBytes())
.removalListener(this::onRemoval)
@@ -60,11 +59,16 @@ public class CacheManager {
RandomAccessFile file, int pageNumber, Consumer<Integer> cleanCallback) {
CacheKey key = new CacheKey(file, pageNumber);
CacheValue value;
- try {
- value = cache.get(key, () -> createValue(key, cleanCallback));
- } catch (ExecutionException e) {
- throw new RuntimeException(e);
- }
+ value =
+ cache.get(
+ key,
+ cacheKey -> {
+ try {
+ return createValue(key, cleanCallback);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ });
return value.segment;
}
@@ -76,8 +80,8 @@ public class CacheManager {
return cacheValue.segment.size();
}
- private void onRemoval(RemovalNotification<CacheKey, CacheValue> notification) {
- notification.getValue().cleanCallback.accept(notification.getKey().pageNumber);
+ private void onRemoval(CacheKey key, CacheValue value, RemovalCause cause) {
+ value.cleanCallback.accept(key.pageNumber);
}
private CacheValue createValue(CacheKey key, Consumer<Integer> cleanCallback)
diff --git a/paimon-common/src/test/java/org/apache/paimon/io/cache/CachedRandomInputViewTest.java b/paimon-common/src/test/java/org/apache/paimon/io/cache/CachedRandomInputViewTest.java
index 066be864e..05894f63a 100644
--- a/paimon-common/src/test/java/org/apache/paimon/io/cache/CachedRandomInputViewTest.java
+++ b/paimon-common/src/test/java/org/apache/paimon/io/cache/CachedRandomInputViewTest.java
@@ -94,7 +94,7 @@ public class CachedRandomInputViewTest {
}
view.close();
- assertThat(cacheManager.cache().size()).isEqualTo(0);
+ assertThat(cacheManager.cache().asMap().size()).isEqualTo(0);
}
private File writeFile(byte[] bytes) throws IOException {