From 7e9b7448d1f92e54b88884a57562b5ef07b75bec Mon Sep 17 00:00:00 2001 From: Shawn Heisey Date: Wed, 8 Apr 2015 18:23:19 +0000 Subject: [PATCH] SOLR-7355: Switch from ConcurrentLinkedHashMap to Caffeine. Trunk-only change, as it requires java 8. git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1672133 13f79535-47bb-0310-9956-ffa450edef68 --- solr/CHANGES.txt | 3 ++ solr/core/ivy.xml | 2 +- .../solr/store/blockcache/BlockCache.java | 33 +++++++++---------- .../store/blockcache/BlockDirectoryTest.java | 14 +++++--- solr/licenses/caffeine-1.0.1.jar.sha1 | 1 + ...CENSE-ASL.txt => caffeine-LICENSE-ASL.txt} | 0 solr/licenses/caffeine-NOTICE.txt | 1 + .../concurrentlinkedhashmap-lru-1.2.jar.sha1 | 1 - .../concurrentlinkedhashmap-lru-NOTICE.txt | 0 9 files changed, 32 insertions(+), 23 deletions(-) create mode 100644 solr/licenses/caffeine-1.0.1.jar.sha1 rename solr/licenses/{concurrentlinkedhashmap-lru-LICENSE-ASL.txt => caffeine-LICENSE-ASL.txt} (100%) create mode 100644 solr/licenses/caffeine-NOTICE.txt delete mode 100644 solr/licenses/concurrentlinkedhashmap-lru-1.2.jar.sha1 delete mode 100644 solr/licenses/concurrentlinkedhashmap-lru-NOTICE.txt diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index 9757f4e7ca4..c461027827b 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -58,6 +58,9 @@ Other Changes * SOLR-6954: Deprecated SolrClient.shutdown() method removed (Alan Woodward) +* SOLR-7355: Switch from Google's ConcurrentLinkedHashMap to Caffeine. Only + affects HDFS support. (Ben Manes via Shawn Heisey) + ================== 5.2.0 ================== Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release diff --git a/solr/core/ivy.xml b/solr/core/ivy.xml index f51dd6922e5..c8a169f21af 100644 --- a/solr/core/ivy.xml +++ b/solr/core/ivy.xml @@ -66,7 +66,7 @@ - + diff --git a/solr/core/src/java/org/apache/solr/store/blockcache/BlockCache.java b/solr/core/src/java/org/apache/solr/store/blockcache/BlockCache.java index 5f216691ae8..1eeabe291ef 100644 --- a/solr/core/src/java/org/apache/solr/store/blockcache/BlockCache.java +++ b/solr/core/src/java/org/apache/solr/store/blockcache/BlockCache.java @@ -21,8 +21,9 @@ import java.nio.ByteBuffer; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.atomic.AtomicInteger; -import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap; -import com.googlecode.concurrentlinkedhashmap.EvictionListener; +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.RemovalListener; /** * @lucene.experimental @@ -31,7 +32,7 @@ public class BlockCache { public static final int _128M = 134217728; public static final int _32K = 32768; - private final ConcurrentMap cache; + private final Cache cache; private final ByteBuffer[] banks; private final BlockLocks[] locks; private final AtomicInteger[] lockCounters; @@ -68,20 +69,18 @@ public class BlockCache { locks[i] = new BlockLocks(numberOfBlocksPerBank); lockCounters[i] = new AtomicInteger(); } - - EvictionListener listener = new EvictionListener() { - @Override - public void onEviction(BlockCacheKey key, BlockCacheLocation location) { - releaseLocation(location); - } - }; - cache = new ConcurrentLinkedHashMap.Builder() - .maximumWeightedCapacity(maxEntries).listener(listener).build(); + + RemovalListener listener = + notification -> releaseLocation(notification.getValue()); + cache = Caffeine.newBuilder() + .removalListener(listener) + .maximumSize(maxEntries) + .build(); this.blockSize = blockSize; } public void release(BlockCacheKey key) { - releaseLocation(cache.remove(key)); + cache.invalidate(key); } private void releaseLocation(BlockCacheLocation location) { @@ -104,7 +103,7 @@ public class BlockCache { + blockSize + "] got length [" + length + "] with blockOffset [" + blockOffset + "]"); } - BlockCacheLocation location = cache.get(blockCacheKey); + BlockCacheLocation location = cache.getIfPresent(blockCacheKey); boolean newLocation = false; if (location == null) { newLocation = true; @@ -122,7 +121,7 @@ public class BlockCache { bank.position(bankOffset + blockOffset); bank.put(data, offset, length); if (newLocation) { - releaseLocation(cache.put(blockCacheKey.clone(), location)); + cache.put(blockCacheKey.clone(), location); metrics.blockCacheSize.incrementAndGet(); } return true; @@ -130,7 +129,7 @@ public class BlockCache { public boolean fetch(BlockCacheKey blockCacheKey, byte[] buffer, int blockOffset, int off, int length) { - BlockCacheLocation location = cache.get(blockCacheKey); + BlockCacheLocation location = cache.getIfPresent(blockCacheKey); if (location == null) { return false; } @@ -201,6 +200,6 @@ public class BlockCache { } public int getSize() { - return cache.size(); + return cache.asMap().size(); } } diff --git a/solr/core/src/test/org/apache/solr/store/blockcache/BlockDirectoryTest.java b/solr/core/src/test/org/apache/solr/store/blockcache/BlockDirectoryTest.java index 4de0b42d825..fb043f09f50 100644 --- a/solr/core/src/test/org/apache/solr/store/blockcache/BlockDirectoryTest.java +++ b/solr/core/src/test/org/apache/solr/store/blockcache/BlockDirectoryTest.java @@ -22,6 +22,8 @@ import java.io.IOException; import java.util.Map; import java.util.Random; +import com.github.benmanes.caffeine.cache.Caffeine; + import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.IOContext; @@ -34,12 +36,13 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; -import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap; - public class BlockDirectoryTest extends SolrTestCaseJ4 { private class MapperCache implements Cache { - public Map map = new ConcurrentLinkedHashMap.Builder().maximumWeightedCapacity(8).build(); + public Map map = Caffeine.newBuilder() + .maximumSize(8) + .build() + .asMap(); @Override public void update(String name, long blockId, int blockOffset, byte[] buffer, int offset, int length) { @@ -167,7 +170,10 @@ public class BlockDirectoryTest extends SolrTestCaseJ4 { @Test public void testRandomAccessWritesLargeCache() throws IOException { - mapperCache.map = new ConcurrentLinkedHashMap.Builder().maximumWeightedCapacity(10000).build(); + mapperCache.map = Caffeine.newBuilder() + .maximumSize(10_000) + .build() + .asMap(); testRandomAccessWrites(); } diff --git a/solr/licenses/caffeine-1.0.1.jar.sha1 b/solr/licenses/caffeine-1.0.1.jar.sha1 new file mode 100644 index 00000000000..f8e75fde720 --- /dev/null +++ b/solr/licenses/caffeine-1.0.1.jar.sha1 @@ -0,0 +1 @@ +eb95a1eb55cb02018b8e0bc1609ce569b455ea98 diff --git a/solr/licenses/concurrentlinkedhashmap-lru-LICENSE-ASL.txt b/solr/licenses/caffeine-LICENSE-ASL.txt similarity index 100% rename from solr/licenses/concurrentlinkedhashmap-lru-LICENSE-ASL.txt rename to solr/licenses/caffeine-LICENSE-ASL.txt diff --git a/solr/licenses/caffeine-NOTICE.txt b/solr/licenses/caffeine-NOTICE.txt new file mode 100644 index 00000000000..221cbcd5cd5 --- /dev/null +++ b/solr/licenses/caffeine-NOTICE.txt @@ -0,0 +1 @@ +Copyright 2015 by Ben Manes diff --git a/solr/licenses/concurrentlinkedhashmap-lru-1.2.jar.sha1 b/solr/licenses/concurrentlinkedhashmap-lru-1.2.jar.sha1 deleted file mode 100644 index 9c0fe8a75d2..00000000000 --- a/solr/licenses/concurrentlinkedhashmap-lru-1.2.jar.sha1 +++ /dev/null @@ -1 +0,0 @@ -4316d710b6619ffe210c98deb2b0893587dad454 diff --git a/solr/licenses/concurrentlinkedhashmap-lru-NOTICE.txt b/solr/licenses/concurrentlinkedhashmap-lru-NOTICE.txt deleted file mode 100644 index e69de29bb2d..00000000000