This is an automated email from the ASF dual-hosted git repository.

cpoerschke 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 321309f  SOLR-15693: replace magic ShardRequest purposes (#346)
321309f is described below

commit 321309f0a871d5a90cbe11461d461446b887742c
Author: Christine Poerschke <[email protected]>
AuthorDate: Fri Oct 15 16:01:25 2021 +0100

    SOLR-15693: replace magic ShardRequest purposes (#346)
---
 .../apache/solr/cloud/OverseerNodePrioritizer.java |  2 +-
 .../java/org/apache/solr/cloud/SyncStrategy.java   |  2 +-
 .../api/collections/CollectionHandlingUtils.java   |  4 +-
 .../cloud/api/collections/CreateCollectionCmd.java |  2 +-
 .../handler/component/RealTimeGetComponent.java    |  2 +-
 .../src/java/org/apache/solr/update/PeerSync.java  | 17 ++++---
 .../solr/handler/component/ShardRequestTest.java   | 52 ++++++++++++++++++++++
 .../test/org/apache/solr/update/PeerSyncTest.java  |  2 +
 8 files changed, 71 insertions(+), 12 deletions(-)

diff --git 
a/solr/core/src/java/org/apache/solr/cloud/OverseerNodePrioritizer.java 
b/solr/core/src/java/org/apache/solr/cloud/OverseerNodePrioritizer.java
index bb3c3d9..c02403e 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.java
index be450fc..fd34bc7 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.java
index 8034817..7741353 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.java
index 45211d4..66c21b6 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.java
index a5cbc14..8063968 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.java
index c8c6e00..270b10d 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 object
     ShardRequest 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.java
new file mode 100644
index 0000000..1dbf6e3
--- /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.java
index a592777..3c3cf64 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();
   }
 

Reply via email to