Skip to content

Apache Lucene open-source search software

SOLR-9334: CloudSolrClient.collectionStateCache is unbounded

Noble Paul4ed68bc80e79
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 55fae4737f2..e392df34d59 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -161,6 +161,9 @@ Bug Fixes
* SOLR-9309: Fix SolrCloud RTG response structure when multi ids requested but only 1 found (hossman)
+* SOLR-9334: CloudSolrClient.collectionStateCache is unbounded (noble)
+
+
Optimizations
----------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrClient.java
index 876f7f86cba..b4c9b4f9560 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrClient.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrClient.java
@@ -38,6 +38,8 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
import org.apache.http.NoHttpResponseException;
import org.apache.http.client.HttpClient;
@@ -147,10 +149,16 @@ public class CloudSolrClient extends SolrClient {
protected final Map<String, ExpiringCachedDocCollection> collectionStateCache = new ConcurrentHashMap<String, ExpiringCachedDocCollection>(){
+ final Lock evictLock = new ReentrantLock(true);
@Override
public ExpiringCachedDocCollection get(Object key) {
ExpiringCachedDocCollection val = super.get(key);
- if(val == null) return null;
+ if(val == null) {
+ // a new collection is likely to be added now.
+ //check if there are stale items and remove them
+ evictStale();
+ return null;
+ }
if(val.isExpired(timeToLive)) {
super.remove(key);
return null;
@@ -158,6 +166,19 @@ public class CloudSolrClient extends SolrClient {
return val;
}
+ void evictStale() {
+ if(!evictLock.tryLock()) return;
+ try {
+ for (Entry<String, ExpiringCachedDocCollection> e : entrySet()) {
+ if(e.getValue().isExpired(timeToLive)){
+ super.remove(e.getKey());
+ }
+ }
+ } finally {
+ evictLock.unlock();
+ }
+ }
+
};
class ExpiringCachedDocCollection {