You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@accumulo.apache.org by GitBox <gi...@apache.org> on 2018/01/04 18:42:48 UTC

[GitHub] milleruntime closed pull request #251: ACCUMULO-4626 added weak reference value map for evictions from block?

milleruntime closed pull request #251: ACCUMULO-4626 added weak reference value map for evictions from block?
URL: https://github.com/apache/accumulo/pull/251
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/core/src/main/java/org/apache/accumulo/core/file/blockfile/cache/TinyLfuBlockCache.java b/core/src/main/java/org/apache/accumulo/core/file/blockfile/cache/TinyLfuBlockCache.java
index bab52af919..e2d2078416 100644
--- a/core/src/main/java/org/apache/accumulo/core/file/blockfile/cache/TinyLfuBlockCache.java
+++ b/core/src/main/java/org/apache/accumulo/core/file/blockfile/cache/TinyLfuBlockCache.java
@@ -19,6 +19,8 @@
 
 import static java.util.Objects.requireNonNull;
 
+import java.util.Map;
+
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
@@ -29,7 +31,9 @@
 import com.github.benmanes.caffeine.cache.Cache;
 import com.github.benmanes.caffeine.cache.Caffeine;
 import com.github.benmanes.caffeine.cache.Policy;
+import com.github.benmanes.caffeine.cache.RemovalCause;
 import com.github.benmanes.caffeine.cache.stats.CacheStats;
+import com.google.common.collect.MapMaker;
 import com.google.common.util.concurrent.ThreadFactoryBuilder;
 
 /**
@@ -48,12 +52,13 @@
   private final Cache<String,Block> cache;
   private final Policy.Eviction<String,Block> policy;
   private final ScheduledExecutorService statsExecutor;
+  private final Map<String,CacheEntry> weakEvictionMap = new MapMaker().weakValues().makeMap();
 
   public TinyLfuBlockCache(long maxSize, long blockSize) {
     cache = Caffeine.newBuilder().initialCapacity((int) Math.ceil(1.2 * maxSize / blockSize)).weigher((String blockName, Block block) -> {
       int keyWeight = ClassSize.align(blockName.length()) + ClassSize.STRING;
       return keyWeight + block.weight();
-    }).maximumWeight(maxSize).recordStats().build();
+    }).maximumWeight(maxSize).recordStats().removalListener((String key, Block block, RemovalCause cause) -> weakEvictionMap.put(key, block)).build();
     policy = cache.policy().eviction().get();
 
     statsExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryBuilder().setNameFormat("TinyLfuBlockCacheStatsExecutor").setDaemon(true)
@@ -68,7 +73,10 @@ public long getMaxSize() {
 
   @Override
   public CacheEntry getBlock(String blockName) {
-    return cache.getIfPresent(blockName);
+    CacheEntry ce = cache.getIfPresent(blockName);
+    if (ce != null)
+      return ce;
+    return weakEvictionMap.get(blockName);
   }
 
   @Override
diff --git a/core/src/main/java/org/apache/accumulo/core/file/blockfile/cache/WeakEvictionLruBlockCache.java b/core/src/main/java/org/apache/accumulo/core/file/blockfile/cache/WeakEvictionLruBlockCache.java
new file mode 100644
index 0000000000..119dfb294f
--- /dev/null
+++ b/core/src/main/java/org/apache/accumulo/core/file/blockfile/cache/WeakEvictionLruBlockCache.java
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.accumulo.core.file.blockfile.cache;
+
+import java.util.Map;
+
+import com.google.common.collect.MapMaker;
+
+public class WeakEvictionLruBlockCache extends LruBlockCache {
+
+  private final Map<String,CachedBlock> weakEvictionMap = new MapMaker().weakValues().concurrencyLevel(DEFAULT_CONCURRENCY_LEVEL).makeMap();
+
+  public WeakEvictionLruBlockCache(long maxSize, long blockSize, boolean evictionThread) {
+    super(maxSize, blockSize, evictionThread);
+  }
+
+  public WeakEvictionLruBlockCache(long maxSize, long blockSize) {
+    super(maxSize, blockSize);
+  }
+
+  @Override
+  public CachedBlock getBlock(String blockName) {
+    CachedBlock cb = super.getBlock(blockName);
+    if (cb != null)
+      return cb;
+    return weakEvictionMap.get(blockName);
+  }
+
+  @Override
+  protected long evictBlock(CachedBlock block) {
+    weakEvictionMap.put(block.getName(), block);
+    return super.evictBlock(block);
+  }
+}
diff --git a/core/src/test/java/org/apache/accumulo/core/file/blockfile/cache/TestWeakEvictionLruBlockCache.java b/core/src/test/java/org/apache/accumulo/core/file/blockfile/cache/TestWeakEvictionLruBlockCache.java
new file mode 100644
index 0000000000..7e96712541
--- /dev/null
+++ b/core/src/test/java/org/apache/accumulo/core/file/blockfile/cache/TestWeakEvictionLruBlockCache.java
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.accumulo.core.file.blockfile.cache;
+
+import static org.junit.Assert.assertTrue;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.junit.Test;
+
+public class TestWeakEvictionLruBlockCache {
+
+  @Test
+  public void testWeakEviction() {
+    // create a cache
+    WeakEvictionLruBlockCache cache = new WeakEvictionLruBlockCache(1000, 100, false);
+    // add more things than will fit
+    Map<String,byte[]> blocks = new HashMap<>();
+    for (int i = 0; i < 10; i++) {
+      String key = "" + i;
+      byte[] buf = new byte[200];
+      cache.cacheBlock(key, buf);
+      blocks.put(key, buf);
+    }
+    // force eviction
+    cache.evict();
+
+    assertTrue("expected at least something to get evicted", cache.getEvictedCount() > 0);
+    // read
+    for (Entry<String,byte[]> entry : blocks.entrySet()) {
+      CachedBlock cachedBlock = cache.getBlock(entry.getKey());
+      assertTrue("expected evicted blocks to still be in weak reference map, since we're holding onto a reference in this test", cachedBlock != null
+          && cachedBlock.getBuffer() == entry.getValue());
+    }
+  }
+
+}
diff --git a/server/tserver/src/main/java/org/apache/accumulo/tserver/TabletServerResourceManager.java b/server/tserver/src/main/java/org/apache/accumulo/tserver/TabletServerResourceManager.java
index b84997dcc5..d2942c1273 100644
--- a/server/tserver/src/main/java/org/apache/accumulo/tserver/TabletServerResourceManager.java
+++ b/server/tserver/src/main/java/org/apache/accumulo/tserver/TabletServerResourceManager.java
@@ -40,7 +40,7 @@
 import org.apache.accumulo.core.conf.Property;
 import org.apache.accumulo.core.data.impl.KeyExtent;
 import org.apache.accumulo.core.file.blockfile.cache.BlockCache;
-import org.apache.accumulo.core.file.blockfile.cache.LruBlockCache;
+import org.apache.accumulo.core.file.blockfile.cache.WeakEvictionLruBlockCache;
 import org.apache.accumulo.core.file.blockfile.cache.TinyLfuBlockCache;
 import org.apache.accumulo.core.metadata.schema.DataFileValue;
 import org.apache.accumulo.core.util.Daemon;
@@ -177,9 +177,9 @@ public TabletServerResourceManager(TabletServer tserver, VolumeManager fs) {
 
     String policy = acuConf.get(Property.TSERV_CACHE_POLICY);
     if (policy.equalsIgnoreCase("LRU")) {
-      _iCache = new LruBlockCache(iCacheSize, blockSize);
-      _dCache = new LruBlockCache(dCacheSize, blockSize);
-      _sCache = new LruBlockCache(sCacheSize, blockSize);
+      _iCache = new WeakEvictionLruBlockCache(iCacheSize, blockSize);
+      _dCache = new WeakEvictionLruBlockCache(dCacheSize, blockSize);
+      _sCache = new WeakEvictionLruBlockCache(sCacheSize, blockSize);
     } else if (policy.equalsIgnoreCase("TinyLFU")) {
       _iCache = new TinyLfuBlockCache(iCacheSize, blockSize);
       _dCache = new TinyLfuBlockCache(dCacheSize, blockSize);


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services