Skip to content

Apache Lucene open-source search software

SOLR-8327: Cluster state caching for nodes not hosting the collection

Ishan Chattopadhyayab212f2e9b7b5
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 57aad64cdef..5c0351e9a61 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -234,6 +234,8 @@ Optimizations
* SOLR-11879: avoid EOFException for empty input streams (noble)
+* SOLR-8327: Cluster state caching to avoid live fetch from ZK of cluster state for forwarding requests on nodes
+ that do not host any replica of the collection (Jessica Cheng Mallet, John Gallagher, Noble Paul, Ishan Chattopadhyaya)
Other Changes
----------------------
diff --git a/solr/core/src/java/org/apache/solr/cloud/Overseer.java b/solr/core/src/java/org/apache/solr/cloud/Overseer.java
index b94af278fe7..608fb208330 100644
--- a/solr/core/src/java/org/apache/solr/cloud/Overseer.java
+++ b/solr/core/src/java/org/apache/solr/cloud/Overseer.java
@@ -70,7 +70,7 @@ public class Overseer implements SolrCloseable {
public static final String QUEUE_OPERATION = "operation";
// System properties are used in tests to make them run fast
- public static final int STATE_UPDATE_DELAY = Integer.getInteger("solr.OverseerStateUpdateDelay", 2000); // delay between cloud state updates
+ public static final int STATE_UPDATE_DELAY = ZkStateReader.STATE_UPDATE_DELAY;
public static final int STATE_UPDATE_BATCH_SIZE = Integer.getInteger("solr.OverseerStateUpdateBatchSize", 10000);
public static final int STATE_UPDATE_MAX_QUEUE = 20000;
diff --git a/solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.java b/solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.java
index 6b10f588225..50fa71c0452 100644
--- a/solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.java
+++ b/solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.java
@@ -819,7 +819,7 @@ public class HttpSolrCall {
ZkStateReader zkStateReader = cores.getZkController().getZkStateReader();
ClusterState clusterState = zkStateReader.getClusterState();
- DocCollection collection = clusterState.getCollectionOrNull(collectionName);
+ DocCollection collection = clusterState.getCollectionOrNull(collectionName, true);
if (collection == null) {
return null;
}
diff --git a/solr/solrj/src/java/org/apache/solr/common/cloud/ClusterState.java b/solr/solrj/src/java/org/apache/solr/common/cloud/ClusterState.java
index 4333a000502..970d95d0d9f 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
@@ -123,18 +123,29 @@ public class ClusterState implements JSONWriter.Writable {
return collectionStates.get(coll);
}
+ /**
+ * Returns the corresponding {@link DocCollection} object for the given collection name
+ * if such a collection exists. Returns null otherwise. Equivalent to getCollectionOrNull(collectionName, false)
+ */
+ public DocCollection getCollectionOrNull(String collectionName) {
+ return getCollectionOrNull(collectionName, false);
+ }
+
/**
* Returns the corresponding {@link DocCollection} object for the given collection name
* if such a collection exists. Returns null otherwise.
*
+ * @param collectionName Name of the collection
+ * @param allowCached allow LazyCollectionRefs to use a time-based cached value
+ *
* Implementation note: This method resolves the collection reference by calling
* {@link CollectionRef#get()} which may make a call to ZooKeeper. This is necessary
* because the semantics of how collection list is loaded have changed in SOLR-6629.
* Please see javadocs in {@link ZkStateReader#refreshCollectionList(Watcher)}
*/
- public DocCollection getCollectionOrNull(String collectionName) {
+ public DocCollection getCollectionOrNull(String collectionName, boolean allowCached) {
CollectionRef ref = collectionStates.get(collectionName);
- return ref == null ? null : ref.get();
+ return ref == null ? null : ref.get(allowCached);
}
/**
@@ -368,7 +379,18 @@ public class ClusterState implements JSONWriter.Writable {
this.coll = coll;
}
+ /** Return the DocCollection, always refetching if lazy. Equivalent to get(false)
+ * @return The collection state modeled in zookeeper
+ */
public DocCollection get(){
+ return get(false);
+ }
+
+ /** Return the DocCollection
+ * @param allowCached Determines if cached value can be used. Applies only to LazyCollectionRef.
+ * @return The collection state modeled in zookeeper
+ */
+ public DocCollection get(boolean allowCached) {
gets.incrementAndGet();
return coll;
}
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.java
index f4e4d1af902..dbda3d883cf 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
@@ -70,6 +70,7 @@ import static java.util.Collections.unmodifiableSet;
import static org.apache.solr.common.util.Utils.fromJSON;
public class ZkStateReader implements Closeable {
+ public static final int STATE_UPDATE_DELAY = Integer.getInteger("solr.OverseerStateUpdateDelay", 2000); // delay between cloud state updates
private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
public static final String BASE_URL_PROP = "base_url";
@@ -136,7 +137,7 @@ public class ZkStateReader implements Closeable {
public static final String SHARD_LEADERS_ZKNODE = "leaders";
public static final String ELECTION_NODE = "election";
-
+
/** Collections tracked in the legacy (shared) state format, reflects the contents of clusterstate.json. */
private Map<String, ClusterState.CollectionRef> legacyCollectionStates = emptyMap();
@@ -163,6 +164,8 @@ public class ZkStateReader implements Closeable {
private final ExecutorService notifications = ExecutorUtil.newMDCAwareCachedThreadPool("watches");
+ private static final long LAZY_CACHE_TIME = TimeUnit.NANOSECONDS.convert(STATE_UPDATE_DELAY, TimeUnit.MILLISECONDS);
+
/**
* Get current {@link AutoScalingConfig}.
* @return current configuration from <code>autoscaling.json</code>. NOTE:
@@ -632,19 +635,36 @@ public class ZkStateReader implements Closeable {
}
private class LazyCollectionRef extends ClusterState.CollectionRef {
-
private final String collName;
+ private long lastUpdateTime;
+ private DocCollection cachedDocCollection;
public LazyCollectionRef(String collName) {
super(null);
this.collName = collName;
+ this.lastUpdateTime = -1;
}
@Override
- public DocCollection get() {
+ public synchronized DocCollection get(boolean allowCached) {
gets.incrementAndGet();
- // TODO: consider limited caching
- return getCollectionLive(ZkStateReader.this, collName);
+ if (!allowCached || lastUpdateTime < 0 || System.nanoTime() - lastUpdateTime > LAZY_CACHE_TIME) {
+ boolean shouldFetch = true;
+ if (cachedDocCollection != null) {
+ Stat exists = null;
+ try {
+ exists = zkClient.exists(getCollectionPath(collName), null, true);
+ } catch (Exception e) {}
+ if (exists != null && exists.getVersion() == cachedDocCollection.getZNodeVersion()) {
+ shouldFetch = false;
+ }
+ }
+ if (shouldFetch) {
+ cachedDocCollection = getCollectionLive(ZkStateReader.this, collName);
+ lastUpdateTime = System.nanoTime();
+ }
+ }
+ return cachedDocCollection;
}
@Override