From 74f3e4e2ea0bfb050d55a1831adfb580644627d7 Mon Sep 17 00:00:00 2001 From: Yiming Liu Date: Thu, 7 Jul 2016 12:30:25 +0800 Subject: [PATCH] KYLIN-1843: support LRU cache on SnapshotManager --- .../org/apache/kylin/common/KylinConfigBase.java | 4 +++ .../apache/kylin/dict/lookup/SnapshotManager.java | 42 +++++++++++++++++----- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/core-common/src/main/java/org/apache/kylin/common/KylinConfigBase.java b/core-common/src/main/java/org/apache/kylin/common/KylinConfigBase.java index 84c4db9..fd340bd 100644 --- a/core-common/src/main/java/org/apache/kylin/common/KylinConfigBase.java +++ b/core-common/src/main/java/org/apache/kylin/common/KylinConfigBase.java @@ -502,6 +502,10 @@ abstract public class KylinConfigBase implements Serializable { return Integer.parseInt(getOptional("kylin.dict.cache.max.entry", "3000")); } + public int getCachedSnapshotMaxEntrySize(){ + return Integer.parseInt(getOptional("kylin.snapshot.cache.max.entry", "500")); + } + public boolean getQueryRunLocalCoprocessor() { return Boolean.parseBoolean(getOptional("kylin.query.run.local.coprocessor", "false")); } diff --git a/core-dictionary/src/main/java/org/apache/kylin/dict/lookup/SnapshotManager.java b/core-dictionary/src/main/java/org/apache/kylin/dict/lookup/SnapshotManager.java index 6e93ae4..adfe003 100644 --- a/core-dictionary/src/main/java/org/apache/kylin/dict/lookup/SnapshotManager.java +++ b/core-dictionary/src/main/java/org/apache/kylin/dict/lookup/SnapshotManager.java @@ -21,6 +21,8 @@ package org.apache.kylin.dict.lookup; import java.io.IOException; import java.util.NavigableSet; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; import org.apache.kylin.common.KylinConfig; import org.apache.kylin.common.persistence.ResourceStore; @@ -31,6 +33,12 @@ import org.apache.kylin.source.ReadableTable.TableSignature; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; +import com.google.common.cache.RemovalListener; +import com.google.common.cache.RemovalNotification; + /** * @author yangli9 */ @@ -61,33 +69,49 @@ public class SnapshotManager { // ============================================================================ private KylinConfig config; - private ConcurrentHashMap snapshotCache; // resource + private LoadingCache snapshotCache; // resource // path ==> // SnapshotTable private SnapshotManager(KylinConfig config) { this.config = config; - snapshotCache = new ConcurrentHashMap(); + this.snapshotCache = CacheBuilder.newBuilder().removalListener(new RemovalListener() { + @Override + public void onRemoval(RemovalNotification notification) { + SnapshotManager.logger.info("Snapshot with resource path " + notification.getKey() + " is removed due to " + notification.getCause()); + } + }).maximumSize(config.getCachedSnapshotMaxEntrySize())// + .expireAfterWrite(1, TimeUnit.DAYS).build(new CacheLoader() { + @Override + public SnapshotTable load(String key) throws Exception { + SnapshotTable snapshotTable = SnapshotManager.this.load(key, true); + return snapshotTable; + } + }); } public void wipeoutCache() { - snapshotCache.clear(); + snapshotCache.invalidateAll(); } public SnapshotTable getSnapshotTable(String resourcePath) throws IOException { - SnapshotTable r = snapshotCache.get(resourcePath); - if (r == null) { - r = load(resourcePath, true); - snapshotCache.put(resourcePath, r); + try { + SnapshotTable r = snapshotCache.get(resourcePath); + if (r == null) { + r = load(resourcePath, true); + snapshotCache.put(resourcePath, r); + } + return r; + } catch (ExecutionException e) { + throw new RuntimeException(e.getCause()); } - return r; } public void removeSnapshot(String resourcePath) throws IOException { ResourceStore store = MetadataManager.getInstance(this.config).getStore(); store.deleteResource(resourcePath); - snapshotCache.remove(resourcePath); + snapshotCache.invalidate(resourcePath); } public SnapshotTable buildSnapshot(ReadableTable table, TableDesc tableDesc) throws IOException { -- 2.7.4 (Apple Git-66)