This is an automated email from the ASF dual-hosted git repository.
dsmiley pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/solr.git
The following commit(s) were added to refs/heads/main by this push:
new 459157a2b8d SOLR-18606: CloudSolrClient: get collection state without
repeated alias lookups (#5314)
459157a2b8d is described below
commit 459157a2b8d2d9ee5ae5bf2bc2c58d9a808353e3
Author: Nick <[email protected]>
AuthorDate: Sat Nov 14 22:46:17 2026 -0700
SOLR-18606: CloudSolrClient: get collection state without repeated alias
lookups (#5314)
Refresh missing ZooKeeper collection state in CloudSolrClient without
repeatedly refreshing known aliases. A performance issue when aliases are in
play.
---
changelog/unreleased/SOLR-18606.yml | 9 ++
.../solrj/impl/ZkClientClusterStateProvider.java | 33 +++++-
.../impl/ZkClientClusterStateProviderTest.java | 127 +++++++++++++++++++++
.../solr/client/solrj/impl/CloudSolrClient.java | 17 ++-
.../solrj/impl/CloudHttp2SolrClientTest.java | 22 ++++
.../solrj/impl/CloudSolrClientCacheTest.java | 20 ++++
.../solrj/impl/ClusterStateProviderTest.java | 89 +++++++++++++++
7 files changed, 311 insertions(+), 6 deletions(-)
diff --git a/changelog/unreleased/SOLR-18606.yml
b/changelog/unreleased/SOLR-18606.yml
new file mode 100644
index 00000000000..50a51b64dfd
--- /dev/null
+++ b/changelog/unreleased/SOLR-18606.yml
@@ -0,0 +1,9 @@
+title: >
+ Refresh missing ZooKeeper collection state in CloudSolrClient without
repeatedly
+ refreshing known aliases.
+type: fixed
+authors:
+ - name: Nick Shanin
+links:
+ - name: SOLR-18606
+ url: https://issues.apache.org/jira/browse/SOLR-18606
diff --git
a/solr/solrj-zookeeper/src/java/org/apache/solr/client/solrj/impl/ZkClientClusterStateProvider.java
b/solr/solrj-zookeeper/src/java/org/apache/solr/client/solrj/impl/ZkClientClusterStateProvider.java
index 4f6f21dff63..6068d6e8acd 100644
---
a/solr/solrj-zookeeper/src/java/org/apache/solr/client/solrj/impl/ZkClientClusterStateProvider.java
+++
b/solr/solrj-zookeeper/src/java/org/apache/solr/client/solrj/impl/ZkClientClusterStateProvider.java
@@ -80,12 +80,37 @@ public class ZkClientClusterStateProvider
@Override
public ClusterState.CollectionRef getState(String collection) {
- ClusterState clusterState = getZkStateReader().getClusterState();
- if (clusterState != null) {
- return clusterState.getCollectionRef(collection);
- } else {
+ ZkStateReader reader = getZkStateReader();
+ ClusterState clusterState = reader.getClusterState();
+ if (clusterState == null) {
return null;
}
+ ClusterState.CollectionRef ref = clusterState.getCollectionRef(collection);
+ // Alias names are probed as collections before they are resolved. Avoid a
ZooKeeper
+ // lookup for a known alias on every request.
+ if (ref != null || reader.getAliases().hasAlias(collection)) {
+ return ref;
+ }
+
+ try {
+ reader.forceUpdateCollection(collection);
+ return reader.getClusterState().getCollectionRef(collection);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ throw new ZooKeeperException(SolrException.ErrorCode.SERVER_ERROR,
e.toString(), e);
+ } catch (KeeperException e) {
+ throw new ZooKeeperException(SolrException.ErrorCode.SERVER_ERROR,
e.toString(), e);
+ } catch (SolrException e) {
+ Throwable cause = e.getCause();
+ if (e.code() == SolrException.ErrorCode.BAD_REQUEST.code
+ && (cause instanceof KeeperException || cause instanceof
InterruptedException)) {
+ if (cause instanceof InterruptedException) {
+ Thread.currentThread().interrupt();
+ }
+ throw new ZooKeeperException(SolrException.ErrorCode.SERVER_ERROR,
e.toString(), cause);
+ }
+ throw e;
+ }
}
@Override
diff --git
a/solr/solrj-zookeeper/src/test/org/apache/solr/client/solrj/impl/ZkClientClusterStateProviderTest.java
b/solr/solrj-zookeeper/src/test/org/apache/solr/client/solrj/impl/ZkClientClusterStateProviderTest.java
new file mode 100644
index 00000000000..ec9e1b4328c
--- /dev/null
+++
b/solr/solrj-zookeeper/src/test/org/apache/solr/client/solrj/impl/ZkClientClusterStateProviderTest.java
@@ -0,0 +1,127 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.client.solrj.impl;
+
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.TimeUnit;
+import org.apache.solr.SolrTestCase;
+import org.apache.solr.cloud.ZkController;
+import org.apache.solr.cloud.ZkTestServer;
+import org.apache.solr.common.cloud.DocCollection;
+import org.apache.solr.common.cloud.SolrZkClient;
+import org.apache.solr.common.cloud.ZkStateReader;
+import org.apache.solr.common.util.Utils;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.Watcher;
+import org.apache.zookeeper.data.Stat;
+import org.junit.Test;
+
+public class ZkClientClusterStateProviderTest extends SolrTestCase {
+
+ private record ReadAttempt(String path, String failure) {}
+
+ private static class RecordingSolrZkClient extends SolrZkClient {
+ private final List<ReadAttempt> readAttempts = new
CopyOnWriteArrayList<>();
+
+ RecordingSolrZkClient(Builder builder) {
+ super(builder);
+ }
+
+ @Override
+ public byte[] getData(String path, Watcher watcher, Stat stat)
+ throws KeeperException, InterruptedException {
+ try {
+ byte[] data = super.getData(path, watcher, stat);
+ readAttempts.add(new ReadAttempt(path, null));
+ return data;
+ } catch (KeeperException | InterruptedException e) {
+ readAttempts.add(new ReadAttempt(path, e.getClass().getSimpleName()));
+ throw e;
+ }
+ }
+
+ List<ReadAttempt> getReadAttempts() {
+ return List.copyOf(readAttempts);
+ }
+
+ void clearReadAttempts() {
+ readAttempts.clear();
+ }
+ }
+
+ @Test
+ public void testKnownAliasDoesNotReadAliasCollectionState() throws Exception
{
+ String collection = "target";
+ String alias = collection + "_alias";
+ String aliasCollectionPath = DocCollection.getCollectionPath(alias);
+ ZkTestServer server = new ZkTestServer(createTempDir("alias-read-signal"));
+ RecordingSolrZkClient zkClient = null;
+ ZkStateReader reader = null;
+ try {
+ server.run();
+ zkClient =
+ new RecordingSolrZkClient(
+ new SolrZkClient.Builder()
+ .withUrl(server.getZkAddress())
+ .withTimeout(30, TimeUnit.SECONDS));
+ ZkController.createClusterZkNodes(zkClient);
+ zkClient.setData(
+ ZkStateReader.ALIASES, Utils.toJSON(Map.of("collection",
Map.of(alias, collection))));
+
+ reader = new ZkStateReader(zkClient);
+ reader.createClusterStateWatchersAndUpdate();
+ assertTrue(reader.getAliases().hasAlias(alias));
+ ZkClientClusterStateProvider provider = new
ZkClientClusterStateProvider(reader);
+
+ // Positive control: this client-specific recorder observes the exact
missing path and
+ // distinguishes the failed ZooKeeper read from a successful read.
+ zkClient.clearReadAttempts();
+ RecordingSolrZkClient observedClient = zkClient;
+ KeeperException.NoNodeException missing =
+ expectThrows(
+ KeeperException.NoNodeException.class,
+ () -> observedClient.getData(aliasCollectionPath, null, null));
+ assertTrue(
+ "Unexpected missing path: " + missing.getPath(),
+ missing.getPath().endsWith(aliasCollectionPath));
+ assertEquals(
+ List.of(new ReadAttempt(aliasCollectionPath, "NoNodeException")),
+ zkClient.getReadAttempts());
+
+ // With the known-alias guard present, probing the alias must not read
its collection-state
+ // path. Removing the guard makes this assertion fail with the exact
NoNode path recorded.
+ zkClient.clearReadAttempts();
+ assertNull(provider.getState(alias));
+ assertFalse(
+ "Known alias triggered a read of "
+ + aliasCollectionPath
+ + ": "
+ + zkClient.getReadAttempts(),
+ zkClient.getReadAttempts().stream().anyMatch(a ->
aliasCollectionPath.equals(a.path())));
+ } finally {
+ if (reader != null) {
+ reader.close();
+ }
+ if (zkClient != null) {
+ zkClient.close();
+ }
+ server.shutdown();
+ }
+ }
+}
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 2eb9a48d7d5..c25a636c332 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
@@ -642,12 +642,17 @@ public abstract class CloudSolrClient extends SolrClient {
if (!inputCollections.isEmpty()
&& !isAdmin
&& !isCollectionRequestOfV2) { // don't do _stateVer_ checking for
admin, v2 api requests
- Set<String> requestedCollectionNames = resolveAliases(inputCollections);
+ Set<String> missingInputCollections = new HashSet<>();
+ Set<String> requestedCollectionNames =
+ resolveAliases(inputCollections, missingInputCollections);
StringBuilder stateVerParamBuilder = null;
for (String requestedCollection : requestedCollectionNames) {
// track the version of state we're using on the client side using the
_stateVer_ param
- DocCollection coll = getDocCollection(requestedCollection, null);
+ DocCollection coll =
+ missingInputCollections.contains(requestedCollection)
+ ? null
+ : getDocCollection(requestedCollection, null);
if (coll == null) {
throw new SolrException(
SolrException.ErrorCode.BAD_REQUEST, "Collection not found: " +
requestedCollection);
@@ -1056,12 +1061,20 @@ public abstract class CloudSolrClient extends
SolrClient {
* collection existence.
*/
private Set<String> resolveAliases(List<String> inputCollections) {
+ return resolveAliases(inputCollections, null);
+ }
+
+ private Set<String> resolveAliases(
+ List<String> inputCollections, Set<String> missingInputCollections) {
if (inputCollections.isEmpty()) {
return Set.of();
}
LinkedHashSet<String> uniqueNames = new LinkedHashSet<>(); // consistent
ordering
for (String collectionName : inputCollections) {
if (getDocCollection(collectionName, -1) == null) {
+ if (missingInputCollections != null) {
+ missingInputCollections.add(collectionName);
+ }
// perhaps it's an alias
uniqueNames.addAll(getClusterStateProvider().resolveAlias(collectionName));
} else {
diff --git
a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/CloudHttp2SolrClientTest.java
b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/CloudHttp2SolrClientTest.java
index 7f4a7971779..fc9825b30ca 100644
---
a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/CloudHttp2SolrClientTest.java
+++
b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/CloudHttp2SolrClientTest.java
@@ -360,6 +360,28 @@ public class CloudHttp2SolrClientTest extends
SolrCloudTestCase {
return new CloudSolrClient.Builder(solrUrls).build();
}
+ @Test
+ public void testZkAliasRequestsResolveAndQuery() throws Exception {
+ String collection = getSaferTestName();
+ String alias = collection + "_alias";
+ CollectionAdminRequest.createCollection(collection, "conf", 2, 1)
+ .process(cluster.getSolrClient());
+ cluster.waitForActiveCollection(collection, 2, 2);
+ CollectionAdminRequest.createAlias(alias,
collection).process(cluster.getSolrClient());
+
+ try (CloudSolrClient client =
+ new
CloudSolrClient.Builder(cluster.getZkServer().getZkAddress()).build()) {
+ assertEquals(List.of(collection),
client.getClusterStateProvider().resolveAlias(alias));
+ assertEquals(0, client.query(alias, params("q",
"*:*")).getResults().getNumFound());
+ for (int i = 0; i < 3; i++) {
+ assertEquals(0, client.query(alias, params("q",
"*:*")).getResults().getNumFound());
+ }
+ } finally {
+
CollectionAdminRequest.deleteAlias(alias).process(cluster.getSolrClient());
+
CollectionAdminRequest.deleteCollection(collection).process(cluster.getSolrClient());
+ }
+ }
+
@Test
public void testShardReplicationFactors() throws Exception {
final String collection = "replication_factor_collection";
diff --git
a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/CloudSolrClientCacheTest.java
b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/CloudSolrClientCacheTest.java
index f5b29e44ab2..4295ce953d2 100644
---
a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/CloudSolrClientCacheTest.java
+++
b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/CloudSolrClientCacheTest.java
@@ -20,6 +20,9 @@ package org.apache.solr.client.solrj.impl;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.IOException;
@@ -138,6 +141,23 @@ public class CloudSolrClientCacheTest extends
SolrTestCaseJ4 {
}
}
+ public void testMissingCollectionIsCheckedOncePerRequest() throws Exception {
+ String collection = "gettingstarted";
+ Map<String, ClusterState.CollectionRef> refs = new HashMap<>();
+ try (ClusterStateProvider provider = spy(getStateProvider(Set.of(), refs));
+ RecordingCloudSolrClient client = new
RecordingCloudSolrClient(provider, 2)) {
+ SolrException error =
+ expectThrows(
+ SolrException.class, () -> client.request(new
DummyRequest(collection), collection));
+ assertEquals(SolrException.ErrorCode.BAD_REQUEST.code, error.code());
+ verify(provider, times(1)).getState(collection);
+
+ refs.put(collection, new
ClusterState.CollectionRef(loadCollection(collection, 1)));
+ assertNotNull(client.request(new DummyRequest(collection), collection));
+ verify(provider, times(2)).getState(collection);
+ }
+ }
+
public void testStaleStateRetrySkipsStateVersionBeforeWait() throws
Exception {
String collName = "gettingstarted";
Set<String> liveNodes = new HashSet<>(Set.of("192.168.1.108:8983_solr"));
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.java
index 294b5b05e38..5f4624bba13 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
@@ -22,6 +22,11 @@ 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 static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.spy;
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
import java.io.IOException;
@@ -29,6 +34,7 @@ import java.lang.invoke.MethodHandles;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Path;
import java.time.Instant;
+import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -40,14 +46,24 @@ import
org.apache.solr.client.solrj.jetty.HttpJettySolrClient;
import org.apache.solr.client.solrj.request.CollectionAdminRequest;
import org.apache.solr.client.solrj.response.CollectionAdminResponse;
import org.apache.solr.cloud.SolrCloudTestCase;
+import org.apache.solr.cloud.Stats;
import org.apache.solr.cloud.ZkController;
import org.apache.solr.cloud.ZkTestServer;
+import org.apache.solr.cloud.overseer.ZkStateWriter;
+import org.apache.solr.cloud.overseer.ZkWriteCommand;
+import org.apache.solr.common.SolrException;
import org.apache.solr.common.cloud.ClusterProperties;
import org.apache.solr.common.cloud.ClusterState;
import org.apache.solr.common.cloud.DocCollection;
+import org.apache.solr.common.cloud.DocRouter;
+import org.apache.solr.common.cloud.PerReplicaStatesOps;
import org.apache.solr.common.cloud.SolrZkClient;
import org.apache.solr.common.cloud.ZkStateReader;
import org.apache.solr.common.util.NamedList;
+import org.apache.solr.common.util.ZLibCompressor;
+import org.apache.solr.handler.admin.ConfigSetsHandler;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.data.Stat;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpHeader;
import org.hamcrest.Matchers;
@@ -216,6 +232,79 @@ public class ClusterStateProviderTest extends
SolrCloudTestCase {
}
}
+ @Test
+ public void testProviderRefreshesCollectionMissingFromLocalState() throws
Exception {
+ ZkTestServer server = new ZkTestServer(createTempDir("stale-state"));
+ server.run();
+ try (SolrZkClient zkClient = new
SolrZkClient.Builder().withUrl(server.getZkAddress()).build();
+ ZkStateReader reader = new ZkStateReader(zkClient);
+ ZkClientClusterStateProvider provider = new
ZkClientClusterStateProvider(reader)) {
+ ZkController.createClusterZkNodes(zkClient);
+ reader.createClusterStateWatchersAndUpdate();
+ ZkStateWriter writer = new ZkStateWriter(reader, new Stats(), -1, new
ZLibCompressor());
+ synchronized (reader.getUpdateLock()) {
+
assertNull(reader.getClusterState().getCollectionRef("new_collection"));
+ zkClient.makePath(ZkStateReader.COLLECTIONS_ZKNODE +
"/new_collection", true);
+ DocCollection state =
+ DocCollection.create(
+ "new_collection",
+ new HashMap<>(),
+ Map.of(ZkStateReader.CONFIGNAME_PROP,
ConfigSetsHandler.DEFAULT_CONFIGSET_NAME),
+ DocRouter.DEFAULT,
+ 0,
+ Instant.now(),
+ PerReplicaStatesOps.getZkClientPrsSupplier(
+ zkClient,
DocCollection.getCollectionPath("new_collection")));
+ writer.enqueueUpdate(
+ reader.getClusterState(), List.of(new
ZkWriteCommand("new_collection", state)), null);
+ writer.writePendingUpdates();
+
+ // The watcher cannot publish the new collection while this lock is
held.
+
assertNull(reader.getClusterState().getCollectionRef("new_collection"));
+ ClusterState.CollectionRef refreshed =
provider.getState("new_collection");
+ assertNotNull(refreshed);
+ assertEquals("new_collection", refreshed.get().getName());
+ }
+ } finally {
+ server.shutdown();
+ }
+ }
+
+ @Test
+ public void testProviderPropagatesCollectionReadFailures() throws Exception {
+ assumeWorkingMockito();
+ SolrZkClient zkClient = spy(cluster.getZkClient());
+ try (ZkStateReader reader = new ZkStateReader(zkClient);
+ ZkClientClusterStateProvider provider = new
ZkClientClusterStateProvider(reader)) {
+ reader.createClusterStateWatchersAndUpdate();
+ KeeperException failure = new KeeperException.ConnectionLossException();
+ doThrow(failure)
+ .when(zkClient)
+ .getData(eq(DocCollection.getCollectionPath("missing")), isNull(),
any(Stat.class));
+ SolrException readerError =
+ expectThrows(SolrException.class, () ->
reader.forceUpdateCollection("missing"));
+ assertEquals(SolrException.ErrorCode.BAD_REQUEST.code,
readerError.code());
+ assertSame(failure, readerError.getCause());
+
+ SolrException error = expectThrows(SolrException.class, () ->
provider.getState("missing"));
+ assertEquals(SolrException.ErrorCode.SERVER_ERROR.code, error.code());
+ assertSame(failure, error.getCause());
+
+ InterruptedException interrupted = new
InterruptedException("interrupted");
+ doThrow(interrupted)
+ .when(zkClient)
+ .getData(eq(DocCollection.getCollectionPath("interrupted")),
isNull(), any(Stat.class));
+ try {
+ error = expectThrows(SolrException.class, () ->
provider.getState("interrupted"));
+ assertEquals(SolrException.ErrorCode.SERVER_ERROR.code, error.code());
+ assertSame(interrupted, error.getCause());
+ assertTrue(Thread.currentThread().isInterrupted());
+ } finally {
+ Thread.interrupted();
+ }
+ }
+ }
+
@Test
public void testGetState() throws Exception {