Skip to content

Apache Lucene open-source search software

SOLR-7355: Switch from ConcurrentLinkedHashMap to Caffeine. Trunk-only change, as it requires java 8.

Shawn Heisey7e9b7448d1f9
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 @@
<dependency org="commons-collections" name="commons-collections" rev="${/commons-collections/commons-collections}" conf="compile.hadoop"/>
<dependency org="com.google.protobuf" name="protobuf-java" rev="${/com.google.protobuf/protobuf-java}" conf="compile.hadoop"/>
- <dependency org="com.googlecode.concurrentlinkedhashmap" name="concurrentlinkedhashmap-lru" rev="${/com.googlecode.concurrentlinkedhashmap/concurrentlinkedhashmap-lru}" conf="compile.hadoop"/>
+ <dependency org="com.github.ben-manes.caffeine" name="caffeine" rev="${/com.github.ben-manes.caffeine/caffeine}" conf="compile.hadoop"/>
<dependency org="org.htrace" name="htrace-core" rev="${/org.htrace/htrace-core}" conf="compile.hadoop"/>
<!-- Hadoop DfsMiniCluster Dependencies-->
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<BlockCacheKey,BlockCacheLocation> cache;
+ private final Cache<BlockCacheKey,BlockCacheLocation> 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<BlockCacheKey,BlockCacheLocation> listener = new EvictionListener<BlockCacheKey,BlockCacheLocation>() {
- @Override
- public void onEviction(BlockCacheKey key, BlockCacheLocation location) {
- releaseLocation(location);
- }
- };
- cache = new ConcurrentLinkedHashMap.Builder<BlockCacheKey,BlockCacheLocation>()
- .maximumWeightedCapacity(maxEntries).listener(listener).build();
+
+ RemovalListener<BlockCacheKey,BlockCacheLocation> 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<String, byte[]> map = new ConcurrentLinkedHashMap.Builder<String, byte[]>().maximumWeightedCapacity(8).build();
+ public Map<String, byte[]> map = Caffeine.newBuilder()
+ .maximumSize(8)
+ .<String, byte[]>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<String, byte[]>().maximumWeightedCapacity(10000).build();
+ mapperCache.map = Caffeine.newBuilder()
+ .maximumSize(10_000)
+ .<String, byte[]>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