Apache Lucene open-source search software
SOLR-6521 CloudSolrServer should synchronize cache cluster state loading
Noble Paul1eda04b2cfde
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex bb31232817d..0949a6691b6 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -794,6 +794,9 @@ Other Changes* SOLR-6976: Remove classes and methods deprecated in 4.x (Alan Woodward, NoblePaul, Chris Hostetter)+* SOLR-6521: CloudSolrClient should synchronize cache cluster state loading+ ( Noble Paul, Jessica Cheng Mallet)+================== 4.10.3 ==================Bug Fixesdiff --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.javaindex 628f0973112..e8c66d5e0c5 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@@ -126,9 +126,10 @@ public class CloudSolrClient extends SolrClient {}private volatile long timeToLive = 60* 1000L;+ private volatile List<Object> locks = objectList(3);- protected Map<String, ExpiringCachedDocCollection> collectionStateCache = new ConcurrentHashMap<String, ExpiringCachedDocCollection>(){+ protected final Map<String, ExpiringCachedDocCollection> collectionStateCache = new ConcurrentHashMap<String, ExpiringCachedDocCollection>(){@Overridepublic ExpiringCachedDocCollection get(Object key) {ExpiringCachedDocCollection val = super.get(key);@@ -143,7 +144,7 @@ public class CloudSolrClient extends SolrClient {};class ExpiringCachedDocCollection {- DocCollection cached;+ final DocCollection cached;long cachedAt;ExpiringCachedDocCollection(DocCollection cached) {@@ -1060,18 +1061,50 @@ public class CloudSolrClient extends SolrClient {return updatesToLeaders;}+ /**If caches are expired they are refreshed after acquiring a lock.+ * use this to set the number of locks+ */+ public void setParallelCacheRefreshes(int n){ locks = objectList(n); }++ private static ArrayList<Object> objectList(int n) {+ ArrayList<Object> l = new ArrayList<>(n);+ for(int i=0;i<n;i++) l.add(new Object());+ return l;+ }++protected DocCollection getDocCollection(ClusterState clusterState, String collection) throws SolrException {- ExpiringCachedDocCollection cachedState = collectionStateCache != null ? collectionStateCache.get(collection) : null;- if (cachedState != null && cachedState.cached != null) {- return cachedState.cached;- }+ if(collection == null) return null;+ DocCollection col = getFromCache(collection);+ if(col != null) return col;- DocCollection col = clusterState.getCollectionOrNull(collection);+ ClusterState.CollectionRef ref = clusterState.getCollectionRef(collection);+ if(ref == null){+ //no such collection exists+ return null;+ }+ if(!ref.isLazilyLoaded()) {+ //it is readily available just return it+ return ref.get();+ }+ List locks = this.locks;+ final Object lock = locks.get(collection.hashCode() % locks.size());+ synchronized (lock){+ //we have waited for sometime just check once again+ col = getFromCache(collection);+ if(col !=null) return col;+ col = ref.get();+ }if(col == null ) return null;if(col.getStateFormat() >1) collectionStateCache.put(collection, new ExpiringCachedDocCollection(col));return col;}+ private DocCollection getFromCache(String c){+ ExpiringCachedDocCollection cachedState = collectionStateCache.get(c);+ return cachedState != null ? cachedState.cached : null;+ }+/*** Useful for determining the minimum achieved replication factor acrossdiff --git a/solr/solrj/src/java/org/apache/solr/common/cloud/ClusterState.java b/solr/solrj/src/java/org/apache/solr/common/cloud/ClusterState.javaindex ceac4ecaf66..9e653a9b0d0 100644--- a/solr/solrj/src/java/org/apache/solr/common/cloud/ClusterState.java+++ b/solr/solrj/src/java/org/apache/solr/common/cloud/ClusterState.java@@ -165,6 +165,9 @@ public class ClusterState implements JSONWriter.Writable {return coll;}+ public CollectionRef getCollectionRef(String coll) {+ return collectionStates.get(coll);+ }public DocCollection getCollectionOrNull(String coll) {CollectionRef ref = collectionStates.get(coll);@@ -397,6 +400,8 @@ public class ClusterState implements JSONWriter.Writable {return coll;}+ public boolean isLazilyLoaded() { return false; }+}}diff --git a/solr/solrj/src/java/org/apache/solr/common/cloud/ZkStateReader.java b/solr/solrj/src/java/org/apache/solr/common/cloud/ZkStateReader.javaindex 07c09fa5a3f..f36f4c9e2da 100644--- a/solr/solrj/src/java/org/apache/solr/common/cloud/ZkStateReader.java+++ b/solr/solrj/src/java/org/apache/solr/common/cloud/ZkStateReader.java@@ -471,6 +471,9 @@ public class ZkStateReader implements Closeable {public DocCollection get() {return getCollectionLive(ZkStateReader.this, collName);}++ @Override+ public boolean isLazilyLoaded() { return true; }});}}