Apache Solr open-source search software
SOLR-15693: replace magic ShardRequest purposes (#346)
Christine Poerschke321309f0a871
diff --git a/solr/core/src/java/org/apache/solr/cloud/OverseerNodePrioritizer.java b/solr/core/src/java/org/apache/solr/cloud/OverseerNodePrioritizer.javaindex bb3c3d98f0e..c02403e8a3a 100644--- a/solr/core/src/java/org/apache/solr/cloud/OverseerNodePrioritizer.java+++ b/solr/core/src/java/org/apache/solr/cloud/OverseerNodePrioritizer.java@@ -103,7 +103,7 @@ public class OverseerNodePrioritizer {params.set("qt", adminPath);params.set("electionNode", electionNode);ShardRequest sreq = new ShardRequest();- sreq.purpose = 1;+ sreq.purpose = ShardRequest.PURPOSE_PRIVATE;String replica = zkStateReader.getBaseUrlForNodeName(LeaderElector.getNodeName(electionNode));sreq.shards = new String[]{replica};sreq.actualShards = sreq.shards;diff --git a/solr/core/src/java/org/apache/solr/cloud/SyncStrategy.java b/solr/core/src/java/org/apache/solr/cloud/SyncStrategy.javaindex be450fcd28e..fd34bc7cc39 100644--- a/solr/core/src/java/org/apache/solr/cloud/SyncStrategy.java+++ b/solr/core/src/java/org/apache/solr/cloud/SyncStrategy.java@@ -273,7 +273,7 @@ public class SyncStrategy {ShardCoreRequest sreq = new ShardCoreRequest();sreq.coreName = coreName;sreq.baseUrl = baseUrl;- sreq.purpose = 1;+ sreq.purpose = ShardRequest.PURPOSE_PRIVATE;sreq.shards = new String[]{replica};sreq.actualShards = sreq.shards;sreq.params = new ModifiableSolrParams();diff --git a/solr/core/src/java/org/apache/solr/cloud/api/collections/CollectionHandlingUtils.java b/solr/core/src/java/org/apache/solr/cloud/api/collections/CollectionHandlingUtils.javaindex 80348175543..7741353f2c1 100644--- a/solr/core/src/java/org/apache/solr/cloud/api/collections/CollectionHandlingUtils.java+++ b/solr/core/src/java/org/apache/solr/cloud/api/collections/CollectionHandlingUtils.java@@ -374,7 +374,7 @@ public class CollectionHandlingUtils {do {sreq = new ShardRequest();params.set("qt", adminPath);- sreq.purpose = 1;+ sreq.purpose = ShardRequest.PURPOSE_PRIVATE;String replica = zkStateReader.getBaseUrlForNodeName(nodeName);sreq.shards = new String[] {replica};sreq.actualShards = sreq.shards;@@ -498,7 +498,7 @@ public class CollectionHandlingUtils {ShardRequest sreq = new ShardRequest();params.set("qt", adminPath);- sreq.purpose = 1;+ sreq.purpose = ShardRequest.PURPOSE_PRIVATE;String replica = zkStateReader.getBaseUrlForNodeName(nodeName);sreq.shards = new String[] {replica};sreq.actualShards = sreq.shards;diff --git a/solr/core/src/java/org/apache/solr/cloud/api/collections/CreateCollectionCmd.java b/solr/core/src/java/org/apache/solr/cloud/api/collections/CreateCollectionCmd.javaindex 45211d4513a..66c21b6173e 100644--- a/solr/core/src/java/org/apache/solr/cloud/api/collections/CreateCollectionCmd.java+++ b/solr/core/src/java/org/apache/solr/cloud/api/collections/CreateCollectionCmd.java@@ -294,7 +294,7 @@ public class CreateCollectionCmd implements CollApiCmds.CollectionApiCommand {ShardRequest sreq = new ShardRequest();sreq.nodeName = nodeName;params.set("qt", ccc.getAdminPath());- sreq.purpose = 1;+ sreq.purpose = ShardRequest.PURPOSE_PRIVATE;sreq.shards = new String[]{baseUrl};sreq.actualShards = sreq.shards;sreq.params = params;diff --git a/solr/core/src/java/org/apache/solr/handler/component/RealTimeGetComponent.java b/solr/core/src/java/org/apache/solr/handler/component/RealTimeGetComponent.javaindex a5cbc14d2f1..80639685631 100644--- a/solr/core/src/java/org/apache/solr/handler/component/RealTimeGetComponent.java+++ b/solr/core/src/java/org/apache/solr/handler/component/RealTimeGetComponent.java@@ -1025,7 +1025,7 @@ public class RealTimeGetComponent extends SearchComponent*/private ShardRequest createShardRequest(final ResponseBuilder rb, final List<String> ids) {final ShardRequest sreq = new ShardRequest();- sreq.purpose = 1;+ sreq.purpose = ShardRequest.PURPOSE_PRIVATE;sreq.params = new ModifiableSolrParams(rb.req.getParams());// TODO: how to avoid hardcoding this and hit the same handler?diff --git a/solr/core/src/java/org/apache/solr/update/PeerSync.java b/solr/core/src/java/org/apache/solr/update/PeerSync.javaindex c8c6e0074c8..270b10dedd4 100644--- a/solr/core/src/java/org/apache/solr/update/PeerSync.java+++ b/solr/core/src/java/org/apache/solr/update/PeerSync.java@@ -78,6 +78,11 @@ public class PeerSync implements SolrMetricProducer {private ShardHandler shardHandler;private List<SyncShardRequest> requests = new ArrayList<>();+ @VisibleForTesting+ static final int SHARD_REQUEST_PURPOSE_GET_UPDATES = 0;+ @VisibleForTesting+ static final int SHARD_REQUEST_PURPOSE_GET_VERSIONS = 1;+private final boolean cantReachIsSuccess;private final boolean doFingerprint;private final boolean onlyIfActive;@@ -325,7 +330,7 @@ public class PeerSync implements SolrMetricProducer {private void requestVersions(String replica) {SyncShardRequest sreq = new SyncShardRequest();requests.add(sreq);- sreq.purpose = 1;+ sreq.purpose = SHARD_REQUEST_PURPOSE_GET_VERSIONS;sreq.shards = new String[]{replica};sreq.actualShards = sreq.shards;sreq.params = new ModifiableSolrParams();@@ -348,7 +353,7 @@ public class PeerSync implements SolrMetricProducer {// If the replica went down between asking for versions and asking for specific updates, that// shouldn't be treated as success since we counted on getting those updates back (and avoided// redundantly asking other replicas for them).- if (cantReachIsSuccess && sreq.purpose == 1 && srsp.getException() instanceof SolrServerException) {+ if (cantReachIsSuccess && sreq.purpose == SHARD_REQUEST_PURPOSE_GET_VERSIONS && srsp.getException() instanceof SolrServerException) {Throwable solrException = ((SolrServerException) srsp.getException()).getRootCause();boolean connectTimeoutExceptionInChain = connectTimeoutExceptionInChain(srsp.getException());@@ -360,12 +365,12 @@ public class PeerSync implements SolrMetricProducer {}}- if (cantReachIsSuccess && sreq.purpose == 1 && srsp.getException() instanceof SolrException && ((SolrException) srsp.getException()).code() == 503) {+ if (cantReachIsSuccess && sreq.purpose == SHARD_REQUEST_PURPOSE_GET_VERSIONS && srsp.getException() instanceof SolrException && ((SolrException) srsp.getException()).code() == 503) {log.warn("{} got a 503 from {}, counting as success ", msg(), srsp.getShardAddress(), srsp.getException());return true;}- if (cantReachIsSuccess && sreq.purpose == 1 && srsp.getException() instanceof SolrException && ((SolrException) srsp.getException()).code() == 404) {+ if (cantReachIsSuccess && sreq.purpose == SHARD_REQUEST_PURPOSE_GET_VERSIONS && srsp.getException() instanceof SolrException && ((SolrException) srsp.getException()).code() == 404) {log.warn("{} got a 404 from {}, counting as success. {} Perhaps /get is not registered?", msg(), srsp.getShardAddress(), srsp.getException());return true;@@ -382,7 +387,7 @@ public class PeerSync implements SolrMetricProducer {return false;}- if (sreq.purpose == 1) {+ if (sreq.purpose == SHARD_REQUEST_PURPOSE_GET_VERSIONS) {return handleVersions(srsp);} else {return handleUpdates(srsp);@@ -474,7 +479,7 @@ public class PeerSync implements SolrMetricProducer {// reuse our original request objectShardRequest sreq = srsp.getShardRequest();- sreq.purpose = 0;+ sreq.purpose = SHARD_REQUEST_PURPOSE_GET_UPDATES;sreq.params = new ModifiableSolrParams();sreq.params.set("qt", "/get");sreq.params.set(DISTRIB, false);diff --git a/solr/core/src/test/org/apache/solr/handler/component/ShardRequestTest.java b/solr/core/src/test/org/apache/solr/handler/component/ShardRequestTest.javanew file mode 100644index 00000000000..1dbf6e34acc--- /dev/null+++ b/solr/core/src/test/org/apache/solr/handler/component/ShardRequestTest.java@@ -0,0 +1,52 @@+/*+ * 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.handler.component;++import org.apache.solr.SolrTestCase;+import org.junit.Test;++public class ShardRequestTest extends SolrTestCase {++ @Test+ public void testDefaultPurposeIsZero() throws Exception {+ final ShardRequest sr = new ShardRequest();+ assertEquals(0, sr.purpose);+ }++ /**+ * Test that the constant stays constant. The constant's value is used in various places, ideally+ * directly via ShardRequest.PURPOSE_PRIVATE but possibly also indirectly via magic '1' hard-coding.+ * If the constant's value needs to change please carefully account for the code impacted by that.+ */+ @Test+ public void testPurposePrivateIsOne() throws Exception {+ assertEquals(1, ShardRequest.PURPOSE_PRIVATE);+ }++ @Test+ public void testToStringWithDefaultPurpose() throws Exception {+ final ShardRequest sr = new ShardRequest();+ assertEquals("ShardRequest:{params=null, purpose=0, nResponses =0}", sr.toString());+ }++ @Test+ public void testToStringWithPurposePrivate() throws Exception {+ final ShardRequest sr = new ShardRequest();+ sr.purpose = ShardRequest.PURPOSE_PRIVATE;+ assertEquals("ShardRequest:{params=null, purpose=1, nResponses =0}", sr.toString());+ }+}diff --git a/solr/core/src/test/org/apache/solr/update/PeerSyncTest.java b/solr/core/src/test/org/apache/solr/update/PeerSyncTest.javaindex a5927778ba7..3c3cf64396b 100644--- a/solr/core/src/test/org/apache/solr/update/PeerSyncTest.java+++ b/solr/core/src/test/org/apache/solr/update/PeerSyncTest.java@@ -310,6 +310,8 @@ public class PeerSyncTest extends BaseDistributedSearchTestCase {assertSync(client1, numVersions, true, shardsArr[0]);validateDocs(docsAdded, client0, client1);+ assertNotEquals(PeerSync.SHARD_REQUEST_PURPOSE_GET_UPDATES, PeerSync.SHARD_REQUEST_PURPOSE_GET_VERSIONS);+handleVersionsWithRangesTests();}