You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@solr.apache.org by GitBox <gi...@apache.org> on 2021/08/06 15:47:10 UTC

[GitHub] [solr] cpoerschke commented on a change in pull request #176: SOLR-15476: Facilitate 'shards.info' content customisation.

cpoerschke commented on a change in pull request #176:
URL: https://github.com/apache/solr/pull/176#discussion_r684330959



##########
File path: solr/core/src/test/org/apache/solr/handler/component/CustomSearchHandlerTest.java
##########
@@ -0,0 +1,180 @@
+/*
+ * 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 java.util.ArrayList;
+import java.util.List;
+import org.apache.solr.client.solrj.SolrQuery;
+import org.apache.solr.client.solrj.request.CollectionAdminRequest;
+import org.apache.solr.client.solrj.request.QueryRequest;
+import org.apache.solr.client.solrj.request.UpdateRequest;
+import org.apache.solr.client.solrj.response.QueryResponse;
+import org.apache.solr.cloud.AbstractDistribZkTestBase;
+import org.apache.solr.cloud.ConfigRequest;
+import org.apache.solr.cloud.SolrCloudTestCase;
+import org.apache.solr.common.util.NamedList;
+import org.apache.solr.request.SolrQueryRequest;
+import org.apache.solr.response.SolrQueryResponse;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class CustomSearchHandlerTest extends SolrCloudTestCase {
+
+  /** A custom search handler that uses a custom response builder. */
+  public static class CustomSearchHandler extends SearchHandler {
+    @Override
+    protected ResponseBuilder newResponseBuilder(
+        SolrQueryRequest req, SolrQueryResponse rsp, List<SearchComponent> components) {
+      return new CustomResponseBuilder(req, rsp, components);
+    }
+  }
+
+  /** A custom response builder that uses a custom shards info container. */
+  private static class CustomResponseBuilder extends ResponseBuilder {
+
+    public CustomResponseBuilder(
+        SolrQueryRequest req, SolrQueryResponse rsp, List<SearchComponent> components) {
+      super(req, rsp, components);
+    }
+
+    @Override
+    protected ShardsInfoContainer newShardsInfoContainer() {
+      return new CustomShardsInfoContainer();
+    }
+  }
+
+  /**
+   * A container for recording shard to shard info mappings, retaining only the "shardAddress" part
+   * of the shard info.
+   *
+   * @see org.apache.solr.common.params.ShardParams#SHARDS_INFO
+   */
+  private static class CustomShardsInfoContainer extends ShardsInfoContainer {

Review comment:
       1/3 The `CustomResponseBuilder` and `CustomShardsInfoContainer` could in principle be done as inner classes of the `CustomSearchHandler` but like this seems clearer.

##########
File path: solr/core/src/test/org/apache/solr/handler/component/CustomSearchHandlerTest.java
##########
@@ -0,0 +1,180 @@
+/*
+ * 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 java.util.ArrayList;
+import java.util.List;
+import org.apache.solr.client.solrj.SolrQuery;
+import org.apache.solr.client.solrj.request.CollectionAdminRequest;
+import org.apache.solr.client.solrj.request.QueryRequest;
+import org.apache.solr.client.solrj.request.UpdateRequest;
+import org.apache.solr.client.solrj.response.QueryResponse;
+import org.apache.solr.cloud.AbstractDistribZkTestBase;
+import org.apache.solr.cloud.ConfigRequest;
+import org.apache.solr.cloud.SolrCloudTestCase;
+import org.apache.solr.common.util.NamedList;
+import org.apache.solr.request.SolrQueryRequest;
+import org.apache.solr.response.SolrQueryResponse;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class CustomSearchHandlerTest extends SolrCloudTestCase {
+
+  /** A custom search handler that uses a custom response builder. */
+  public static class CustomSearchHandler extends SearchHandler {
+    @Override
+    protected ResponseBuilder newResponseBuilder(
+        SolrQueryRequest req, SolrQueryResponse rsp, List<SearchComponent> components) {
+      return new CustomResponseBuilder(req, rsp, components);
+    }
+  }
+
+  /** A custom response builder that uses a custom shards info container. */
+  private static class CustomResponseBuilder extends ResponseBuilder {
+
+    public CustomResponseBuilder(
+        SolrQueryRequest req, SolrQueryResponse rsp, List<SearchComponent> components) {
+      super(req, rsp, components);
+    }
+
+    @Override
+    protected ShardsInfoContainer newShardsInfoContainer() {
+      return new CustomShardsInfoContainer();
+    }
+  }
+
+  /**
+   * A container for recording shard to shard info mappings, retaining only the "shardAddress" part
+   * of the shard info.
+   *
+   * @see org.apache.solr.common.params.ShardParams#SHARDS_INFO
+   */
+  private static class CustomShardsInfoContainer extends ShardsInfoContainer {
+
+    private final List<Object> container = new ArrayList<>();
+
+    @Override
+    public void accept(String shardInfoName, NamedList<Object> shardInfoValue) {
+      final Object shardAddress = shardInfoValue.remove("shardAddress");
+      if (shardAddress != null) {
+        container.add(shardAddress);
+      }
+    }
+
+    @Override
+    public Object get() {
+      return container;
+    }
+  }
+
+  private static String COLLECTION;
+  private static int NUM_SHARDS;
+
+  @BeforeClass
+  public static void setupCluster() throws Exception {
+
+    // decide collection name ...
+    COLLECTION = "collection" + (1 + random().nextInt(100));
+    // ... and shard/replica/node numbers
+    NUM_SHARDS = 1 + random().nextInt(3);
+    final int numReplicas = 2;
+    final int nodeCount = NUM_SHARDS * numReplicas;
+
+    // create and configure cluster
+    configureCluster(nodeCount).addConfig("conf", configset("cloud-dynamic")).configure();
+
+    // create an empty collection
+    CollectionAdminRequest.createCollection(COLLECTION, "conf", NUM_SHARDS, numReplicas)
+        .processAndWait(cluster.getSolrClient(), DEFAULT_TIMEOUT);
+    AbstractDistribZkTestBase.waitForRecoveriesToFinish(
+        COLLECTION, cluster.getSolrClient().getZkStateReader(), false, true, DEFAULT_TIMEOUT);
+  }
+
+  @SuppressWarnings("unchecked")
+  @Test
+  public void testShardsInfoContainerCustomisation() throws Exception {
+
+    // determine custom search handler name (the exact name should not matter)
+    final String customSearchHandlerName = "/custom_select" + random().nextInt();
+
+    // add custom handler
+    {
+      cluster
+          .getSolrClient()
+          .request(
+              new ConfigRequest(
+                  "{\n"
+                      + "  'add-requesthandler': {\n"
+                      + "    'name' : '"
+                      + customSearchHandlerName
+                      + "',\n"
+                      + "    'class' : '"
+                      + CustomSearchHandler.class.getName()
+                      + "',\n"
+                      + "    'components' : [ '"
+                      + QueryComponent.COMPONENT_NAME
+                      + "' ]\n"
+                      + "  }\n"
+                      + "}"),
+              COLLECTION);
+    }

Review comment:
       2/3 ConfigRequest use here as alternative to yet another `solrconfig-for-my-test.xml` file.

##########
File path: solr/core/src/test/org/apache/solr/handler/component/CustomSearchHandlerTest.java
##########
@@ -0,0 +1,180 @@
+/*
+ * 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 java.util.ArrayList;
+import java.util.List;
+import org.apache.solr.client.solrj.SolrQuery;
+import org.apache.solr.client.solrj.request.CollectionAdminRequest;
+import org.apache.solr.client.solrj.request.QueryRequest;
+import org.apache.solr.client.solrj.request.UpdateRequest;
+import org.apache.solr.client.solrj.response.QueryResponse;
+import org.apache.solr.cloud.AbstractDistribZkTestBase;
+import org.apache.solr.cloud.ConfigRequest;
+import org.apache.solr.cloud.SolrCloudTestCase;
+import org.apache.solr.common.util.NamedList;
+import org.apache.solr.request.SolrQueryRequest;
+import org.apache.solr.response.SolrQueryResponse;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class CustomSearchHandlerTest extends SolrCloudTestCase {
+
+  /** A custom search handler that uses a custom response builder. */
+  public static class CustomSearchHandler extends SearchHandler {
+    @Override
+    protected ResponseBuilder newResponseBuilder(
+        SolrQueryRequest req, SolrQueryResponse rsp, List<SearchComponent> components) {
+      return new CustomResponseBuilder(req, rsp, components);
+    }
+  }
+
+  /** A custom response builder that uses a custom shards info container. */
+  private static class CustomResponseBuilder extends ResponseBuilder {
+
+    public CustomResponseBuilder(
+        SolrQueryRequest req, SolrQueryResponse rsp, List<SearchComponent> components) {
+      super(req, rsp, components);
+    }
+
+    @Override
+    protected ShardsInfoContainer newShardsInfoContainer() {
+      return new CustomShardsInfoContainer();
+    }
+  }
+
+  /**
+   * A container for recording shard to shard info mappings, retaining only the "shardAddress" part
+   * of the shard info.
+   *
+   * @see org.apache.solr.common.params.ShardParams#SHARDS_INFO
+   */
+  private static class CustomShardsInfoContainer extends ShardsInfoContainer {
+
+    private final List<Object> container = new ArrayList<>();
+
+    @Override
+    public void accept(String shardInfoName, NamedList<Object> shardInfoValue) {
+      final Object shardAddress = shardInfoValue.remove("shardAddress");
+      if (shardAddress != null) {
+        container.add(shardAddress);
+      }
+    }
+
+    @Override
+    public Object get() {
+      return container;
+    }
+  }
+
+  private static String COLLECTION;
+  private static int NUM_SHARDS;
+
+  @BeforeClass
+  public static void setupCluster() throws Exception {
+
+    // decide collection name ...
+    COLLECTION = "collection" + (1 + random().nextInt(100));
+    // ... and shard/replica/node numbers
+    NUM_SHARDS = 1 + random().nextInt(3);
+    final int numReplicas = 2;
+    final int nodeCount = NUM_SHARDS * numReplicas;
+
+    // create and configure cluster
+    configureCluster(nodeCount).addConfig("conf", configset("cloud-dynamic")).configure();
+
+    // create an empty collection
+    CollectionAdminRequest.createCollection(COLLECTION, "conf", NUM_SHARDS, numReplicas)
+        .processAndWait(cluster.getSolrClient(), DEFAULT_TIMEOUT);
+    AbstractDistribZkTestBase.waitForRecoveriesToFinish(
+        COLLECTION, cluster.getSolrClient().getZkStateReader(), false, true, DEFAULT_TIMEOUT);
+  }
+
+  @SuppressWarnings("unchecked")
+  @Test
+  public void testShardsInfoContainerCustomisation() throws Exception {
+
+    // determine custom search handler name (the exact name should not matter)
+    final String customSearchHandlerName = "/custom_select" + random().nextInt();
+
+    // add custom handler
+    {
+      cluster
+          .getSolrClient()
+          .request(
+              new ConfigRequest(
+                  "{\n"
+                      + "  'add-requesthandler': {\n"
+                      + "    'name' : '"
+                      + customSearchHandlerName
+                      + "',\n"
+                      + "    'class' : '"
+                      + CustomSearchHandler.class.getName()
+                      + "',\n"
+                      + "    'components' : [ '"
+                      + QueryComponent.COMPONENT_NAME
+                      + "' ]\n"
+                      + "  }\n"
+                      + "}"),

Review comment:
       3/3 The layout here is not to my taste but it's what "spotless" made it to be.
   
   Elsewhere we have https://github.com/apache/lucene-solr/blob/releases/lucene-solr/8.9.0/solr/core/src/test/org/apache/solr/handler/component/CustomHighlightComponentTest.java#L167-L174 and https://github.com/apache/lucene-solr/blob/releases/lucene-solr/8.9.0/solr/core/src/test/org/apache/solr/handler/component/CustomTermsComponentTest.java#L96-L103 layout.
   
   But well for the three new files in this pull request it seemed logical to go with what "spotless" would (eventually) enforce for them.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org