Apache Solr open-source search software
SOLR-17921: Prefetch liveNodes in BaseHttpClusterStateProvider (#3673)
Houston Putman5c47042148d8
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 0584672fa1c..57e206adaf6 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -252,6 +252,9 @@ Improvements* SOLR-17641: Solr is now able to start on Java 24 and later, but with Security Manager disabled (Houston Putman, Jan Høydahl)+* SOLR-17921: SolrJ CloudSolrClient configured with a Solr URL (not ZK) now refreshes liveNodes in the background.+ This will reduce spikes in request latency when the cached liveNodes have expired. (Houston Putman, David Smiley)+Optimizations---------------------(No changes)diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/BaseHttpClusterStateProvider.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/BaseHttpClusterStateProvider.javaindex d3f002836ec..c6d94aaeacb 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/BaseHttpClusterStateProvider.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/BaseHttpClusterStateProvider.java@@ -30,6 +30,8 @@ import java.util.Collections;import java.util.List;import java.util.Map;import java.util.Set;+import java.util.concurrent.Executors;+import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;import java.util.stream.Collectors;import org.apache.solr.client.solrj.SolrClient;@@ -46,8 +48,8 @@ import org.apache.solr.common.params.ModifiableSolrParams;import org.apache.solr.common.util.CollectionUtil;import org.apache.solr.common.util.EnvUtils;import org.apache.solr.common.util.SimpleOrderedMap;+import org.apache.solr.common.util.SolrNamedThreadFactory;import org.apache.solr.common.util.URLUtil;-import org.apache.solr.common.util.Utils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;@@ -57,7 +59,7 @@ public abstract class BaseHttpClusterStateProvider implements ClusterStateProvidprivate String urlScheme;private List<URL> configuredNodes;volatile Set<String> liveNodes; // initially null then never null- long liveNodesTimestamp = 0;+ volatile long liveNodesTimestamp = 0;volatile Map<String, List<String>> aliases;volatile Map<String, Map<String, String>> aliasProperties;long aliasesTimestamp = 0;@@ -65,6 +67,11 @@ public abstract class BaseHttpClusterStateProvider implements ClusterStateProvid// the liveNodes and aliases cache will be invalidated after 5 secsprivate int cacheTimeout = EnvUtils.getPropertyAsInteger("solr.solrj.cache.timeout.sec", 5);+ volatile boolean liveNodeReloadingScheduled = false;+ private final ScheduledExecutorService liveNodeReloadingService =+ Executors.newSingleThreadScheduledExecutor(+ new SolrNamedThreadFactory("liveNodeReloadingExecutor"));+protected void initConfiguredNodes(List<String> solrUrls) throws Exception {this.configuredNodes =solrUrls.stream()@@ -98,14 +105,14 @@ public abstract class BaseHttpClusterStateProvider implements ClusterStateProvid@Overridepublic ClusterState.CollectionRef getState(String collection) {for (String nodeName : getLiveNodes()) {- String baseUrl = Utils.getBaseUrlForNodeName(nodeName, urlScheme);+ String baseUrl = URLUtil.getBaseUrlForNodeName(nodeName, urlScheme);try (SolrClient client = getSolrClient(baseUrl)) {DocCollection docCollection = fetchCollectionState(client, collection);return new ClusterState.CollectionRef(docCollection);} catch (SolrServerException | IOException e) {log.warn("Attempt to fetch cluster state from {} failed.",- Utils.getBaseUrlForNodeName(nodeName, urlScheme),+ URLUtil.getBaseUrlForNodeName(nodeName, urlScheme),e);} catch (RemoteSolrException e) {if ("NOT_FOUND".equals(e.getMetadata("CLUSTERSTATUS"))) {@@ -213,25 +220,50 @@ public abstract class BaseHttpClusterStateProvider implements ClusterStateProvid}@Override- public synchronized Set<String> getLiveNodes() {- // synchronized because there's no value in multiple doing this at the same time+ public Set<String> getLiveNodes() {+ // Use cached liveNodes if cached and still valid+ if (liveNodes == null+ || (TimeUnit.SECONDS.convert((System.nanoTime() - liveNodesTimestamp), TimeUnit.NANOSECONDS)+ > getCacheTimeout())) {+ // Do synchronized fetch when there is no cached value or the cached value is expired+ fetchLiveNodes(false);+ }+ return liveNodes;+ }++ private synchronized void fetchLiveNodes(boolean force) {+ if (!liveNodeReloadingScheduled) {+ // Method is synchronized, so this is safe+ liveNodeReloadingScheduled = true;+ long liveNodeReloadDelayMs = (1000L * getCacheTimeout()) / 2;+ liveNodeReloadingService.scheduleWithFixedDelay(+ () -> fetchLiveNodes(true),+ liveNodeReloadDelayMs,+ liveNodeReloadDelayMs,+ TimeUnit.MILLISECONDS);+ }// only in the initial state, liveNodes is nullif (liveNodes != null) {- if (TimeUnit.SECONDS.convert((System.nanoTime() - liveNodesTimestamp), TimeUnit.NANOSECONDS)- <= getCacheTimeout()) {- return this.liveNodes; // cached copy is fresh enough+ // Don't use the cached value if force is enabled+ if (!force+ && TimeUnit.SECONDS.convert(+ (System.nanoTime() - liveNodesTimestamp), TimeUnit.NANOSECONDS)+ <= getCacheTimeout()) {+ // Check the cached value again, as the live nodes might have been updated while blocked on+ // synchronization+ return; // cached copy is fresh enough}if (liveNodes.stream().map(node -> URLUtil.getBaseUrlForNodeName(node, urlScheme)).map(BaseHttpClusterStateProvider::stringToUrl)- .anyMatch(this::updateLiveNodes)) return this.liveNodes;+ .anyMatch(this::updateLiveNodes)) return;log.warn("Failed fetching live_nodes from {}. Trying configured nodes...", liveNodes);}- if (configuredNodes.stream().anyMatch(this::updateLiveNodes)) return this.liveNodes;+ if (configuredNodes.stream().anyMatch(this::updateLiveNodes)) return;throw new RuntimeException("Failed fetching live_nodes from "@@ -280,9 +312,8 @@ public abstract class BaseHttpClusterStateProvider implements ClusterStateProvid|| TimeUnit.SECONDS.convert((System.nanoTime() - aliasesTimestamp), TimeUnit.NANOSECONDS)> getCacheTimeout()) {for (String nodeName : getLiveNodes()) {- String baseUrl = Utils.getBaseUrlForNodeName(nodeName, urlScheme);+ String baseUrl = URLUtil.getBaseUrlForNodeName(nodeName, urlScheme);try (SolrClient client = getSolrClient(baseUrl)) {-CollectionAdminResponse response =new CollectionAdminRequest.ListAliases().process(client);this.aliases = response.getAliasesAsLists();@@ -327,7 +358,7 @@ public abstract class BaseHttpClusterStateProvider implements ClusterStateProvid@Overridepublic ClusterState getClusterState() {for (String nodeName : getLiveNodes()) {- String baseUrl = Utils.getBaseUrlForNodeName(nodeName, urlScheme);+ String baseUrl = URLUtil.getBaseUrlForNodeName(nodeName, urlScheme);try (SolrClient client = getSolrClient(baseUrl)) {return fetchClusterState(client);} catch (SolrServerException | RemoteSolrException | IOException e) {@@ -353,7 +384,7 @@ public abstract class BaseHttpClusterStateProvider implements ClusterStateProvid@Overridepublic Map<String, Object> getClusterProperties() {for (String nodeName : getLiveNodes()) {- String baseUrl = Utils.getBaseUrlForNodeName(nodeName, urlScheme);+ String baseUrl = URLUtil.getBaseUrlForNodeName(nodeName, urlScheme);try (SolrClient client = getSolrClient(baseUrl)) {SimpleOrderedMap<?> cluster =submitClusterStateRequest(client, null, ClusterStateRequestType.FETCH_CLUSTER_PROP);@@ -403,6 +434,16 @@ public abstract class BaseHttpClusterStateProvider implements ClusterStateProvidreturn String.join(",", this.liveNodes);}+ @Override+ public boolean isClosed() {+ return liveNodeReloadingService.isShutdown();+ }++ @Override+ public void close() throws IOException {+ liveNodeReloadingService.shutdown();+ }+private enum ClusterStateRequestType {FETCH_LIVE_NODES,FETCH_CLUSTER_PROP,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.javaindex 00a5018e7b7..647071ef0bf 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@@ -555,6 +555,7 @@ public abstract class CloudSolrClient extends SolrClient {DocCollection col, ReplicaListTransformer replicaListTransformer) {Map<String, List<String>> urlMap = new HashMap<>();Slice[] slices = col.getActiveSlicesArr();+ Set<String> liveNodes = getClusterStateProvider().getLiveNodes();for (Slice slice : slices) {String name = slice.getName();List<Replica> sortedReplicas = new ArrayList<>();@@ -562,9 +563,7 @@ public abstract class CloudSolrClient extends SolrClient {if (directUpdatesToLeadersOnly && leader == null) {for (Replica replica :slice.getReplicas(- replica ->- replica.isActive(getClusterStateProvider().getLiveNodes())- && replica.getType() == Replica.Type.NRT)) {+ replica -> replica.isActive(liveNodes) && replica.getType() == Replica.Type.NRT)) {leader = replica;break;}@@ -783,8 +782,6 @@ public abstract class CloudSolrClient extends SolrClient {protected NamedList<Object> requestWithRetryOnStaleState(SolrRequest<?> request, int retryCount, List<String> inputCollections)throws SolrServerException, IOException {- connect(); // important to call this before you start working with the ZkStateReader-// build up a _stateVer_ param to pass to the server containing all the// external collection state versions involved in this request, which allows// the server to notify us that our cached state for one or more of the external@@ -994,8 +991,6 @@ public abstract class CloudSolrClient extends SolrClient {protected NamedList<Object> sendRequest(SolrRequest<?> request, List<String> inputCollections)throws SolrServerException, IOException {- connect();-boolean sendToLeaders = false;if (request.getRequestType() == SolrRequestType.UPDATE) {@@ -1281,8 +1276,6 @@ public abstract class CloudSolrClient extends SolrClient {* will be only one shard in the return value.*/public Map<String, Integer> getShardReplicationFactor(String collection, NamedList<?> resp) {- connect();-Map<String, Integer> results = new HashMap<>();if (resp instanceof RouteResponse) {NamedList<NamedList<?>> routes = ((RouteResponse<?>) resp).getRouteResponses();diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/Http2ClusterStateProvider.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/Http2ClusterStateProvider.javaindex a7abb80b71d..cb4226c58d5 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/Http2ClusterStateProvider.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/Http2ClusterStateProvider.java@@ -37,6 +37,7 @@ public class Http2ClusterStateProvider extends BaseHttpClusterStateProvider {if (this.closeClient && this.httpClient != null) {httpClient.close();}+ super.close();}@Overridediff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClusterStateProvider.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClusterStateProvider.javaindex 353c15fd1b7..2877184bcde 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClusterStateProvider.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpClusterStateProvider.java@@ -48,5 +48,6 @@ public class HttpClusterStateProvider extends BaseHttpClusterStateProvider {if (this.clientIsInternal && this.httpClient != null) {HttpClientUtil.close(httpClient);}+ super.close();}}diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/ClusterStateProviderTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/ClusterStateProviderTest.javaindex a702fe6ca4f..57479c1c263 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/ClusterStateProviderTest.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/ClusterStateProviderTest.java@@ -20,6 +20,7 @@ package org.apache.solr.client.solrj.impl;import static org.apache.solr.common.util.URLUtil.getNodeNameForBaseUrl;import static org.hamcrest.Matchers.containsInAnyOrder;import static org.hamcrest.Matchers.equalTo;+import static org.hamcrest.Matchers.lessThanOrEqualTo;import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;import java.io.IOException;@@ -29,6 +30,7 @@ import java.time.Instant;import java.util.List;import java.util.Map;import java.util.Set;+import java.util.concurrent.TimeUnit;import java.util.function.Supplier;import org.apache.solr.client.solrj.SolrServerException;import org.apache.solr.client.solrj.request.CollectionAdminRequest;@@ -344,9 +346,17 @@ public class ClusterStateProviderTest extends SolrCloudTestCase {cluster.stopJettySolrRunner(jettyNode2);waitForCSPCacheTimeout();+ long startTimeNs = System.nanoTime();actualKnownNodes = cspHttp.getLiveNodes();+ long liveNodeFetchTimeMs =+ TimeUnit.MILLISECONDS.convert(System.nanoTime() - startTimeNs, TimeUnit.NANOSECONDS);assertEquals(1, actualKnownNodes.size());assertEquals(Set.of(nodeName3), actualKnownNodes);+ // This should already be cached, because it is being updated in the background+ assertThat(+ "Cached getLiveNodes() should take no more than 2 milliseconds",+ liveNodeFetchTimeMs,+ lessThanOrEqualTo(2L));// Bring back a backup node and take down the new nodecluster.startJettySolrRunner(jettyNode2, true);