Apache Lucene open-source search software
SOLR-7746: Ping requests stopped working with distrib=true in Solr 5.2.1
Gregory Chanan555cc509307e
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex d35c83d5435..9b8980bb71d 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -188,6 +188,8 @@ Bug Fixesrelated to leader initiated recovery is performed by a dedicated LIR thread in the background.(Ramkumar Aiyengar, shalin)+* SOLR-7746: Ping requests stopped working with distrib=true in Solr 5.2.1. (Michael Sun)+Optimizations----------------------diff --git a/solr/core/src/java/org/apache/solr/handler/PingRequestHandler.java b/solr/core/src/java/org/apache/solr/handler/PingRequestHandler.javaindex 49c2e748e51..c073f6364b3 100644--- a/solr/core/src/java/org/apache/solr/handler/PingRequestHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/PingRequestHandler.java@@ -23,18 +23,19 @@ import java.nio.file.Files;import java.util.Date;import java.util.Locale;+import org.apache.commons.io.FileUtils;import org.apache.solr.common.SolrException;import org.apache.solr.common.params.CommonParams;import org.apache.solr.common.params.ModifiableSolrParams;+import org.apache.solr.common.params.ShardParams;import org.apache.solr.common.params.SolrParams;import org.apache.solr.common.util.NamedList;import org.apache.solr.core.SolrCore;-import org.apache.solr.util.DateFormatUtil;-import org.apache.solr.util.plugin.SolrCoreAware;import org.apache.solr.request.SolrQueryRequest;import org.apache.solr.request.SolrRequestHandler;import org.apache.solr.response.SolrQueryResponse;-import org.apache.commons.io.FileUtils;+import org.apache.solr.util.DateFormatUtil;+import org.apache.solr.util.plugin.SolrCoreAware;import org.slf4j.Logger;import org.slf4j.LoggerFactory;@@ -53,10 +54,13 @@ import org.slf4j.LoggerFactory;* executed. If the request succeeds, then the PingRequestHandler* will respond back with a simple "OK" status. If the request fails,* then the PingRequestHandler will respond back with the- * corrisponding HTTP Error code. Clients (such as load balancers)+ * corresponding HTTP Error code. Clients (such as load balancers)* can be configured to poll the PingRequestHandler monitoring for* these types of responses (or for a simple connection failure) to* know if there is a problem with the Solr server.+ *+ * Note in case isShard=true, PingRequestHandler respond back with+ * what the delegated handler returns (by default it's /select handler).* </p>** <pre class="prettyprint">@@ -233,7 +237,7 @@ public class PingRequestHandler extends RequestHandlerBase implements SolrCoreAwSolrCore core = req.getCore();// Get the RequestHandler- String qt = params.get( CommonParams.QT );//optional; you get the default otherwise+ String qt = params.get( CommonParams.QT );//optional; you get the default otherwiseSolrRequestHandler handler = core.getRequestHandler( qt );if( handler == null ) {throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,@@ -241,27 +245,54 @@ public class PingRequestHandler extends RequestHandlerBase implements SolrCoreAw}if( handler instanceof PingRequestHandler ) {- throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,- "Cannot execute the PingRequestHandler recursively" );+ // In case it's a query for shard, use default handler+ if (params.getBool(ShardParams.IS_SHARD, false)) {+ handler = core.getRequestHandler( null );+ ModifiableSolrParams wparams = new ModifiableSolrParams(params);+ wparams.remove(CommonParams.QT);+ req.setParams(wparams);+ } else {+ throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,+ "Cannot execute the PingRequestHandler recursively" );+ }}// Execute the ping query and catch any possible exceptionThrowable ex = null;- try {- SolrQueryResponse pingrsp = new SolrQueryResponse();- core.execute(handler, req, pingrsp );- ex = pingrsp.getException();- }- catch( Exception e ) {- ex = e;- }- // Send an error or an 'OK' message (response code will be 200)- if( ex != null ) {- throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,- "Ping query caused exception: "+ex.getMessage(), ex );- }- rsp.add( "status", "OK" );+ // In case it's a query for shard, return the result from delegated handler for distributed query to merge result+ if (params.getBool(ShardParams.IS_SHARD, false)) {+ try {+ core.execute(handler, req, rsp );+ ex = rsp.getException();+ }+ catch( Exception e ) {+ ex = e;+ }+ // Send an error or return+ if( ex != null ) {+ throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,+ "Ping query caused exception: "+ex.getMessage(), ex );+ }+ } else {+ try {+ SolrQueryResponse pingrsp = new SolrQueryResponse();+ core.execute(handler, req, pingrsp );+ ex = pingrsp.getException();+ }+ catch( Exception e ) {+ ex = e;+ }++ // Send an error or an 'OK' message (response code will be 200)+ if( ex != null ) {+ throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,+ "Ping query caused exception: "+ex.getMessage(), ex );+ }++ rsp.add( "status", "OK" );+ }+}protected void handleEnable(boolean enable) throws SolrException {diff --git a/solr/core/src/test/org/apache/solr/handler/PingRequestHandlerTest.java b/solr/core/src/test/org/apache/solr/handler/PingRequestHandlerTest.javaindex 29e9343a6dc..f901ba92c6c 100644--- a/solr/core/src/test/org/apache/solr/handler/PingRequestHandlerTest.java+++ b/solr/core/src/test/org/apache/solr/handler/PingRequestHandlerTest.java@@ -19,9 +19,15 @@ package org.apache.solr.handler;import java.io.File;import java.io.IOException;+import java.util.List;import org.apache.commons.io.FileUtils;import org.apache.solr.SolrTestCaseJ4;+import org.apache.solr.client.solrj.embedded.JettySolrRunner;+import org.apache.solr.client.solrj.impl.CloudSolrClient;+import org.apache.solr.client.solrj.request.SolrPing;+import org.apache.solr.client.solrj.response.SolrPingResponse;+import org.apache.solr.cloud.MiniSolrCloudCluster;import org.apache.solr.common.SolrException;import org.apache.solr.common.util.NamedList;import org.apache.solr.request.SolrQueryRequest;@@ -30,6 +36,9 @@ import org.junit.Before;import org.junit.BeforeClass;public class PingRequestHandlerTest extends SolrTestCaseJ4 {+ protected int NUM_SERVERS = 5;+ protected int NUM_SHARDS = 2;+ protected int REPLICATION_FACTOR = 2;private final String fileName = this.getClass().getName() + ".server-enabled";private File healthcheckFile = null;@@ -164,6 +173,48 @@ public class PingRequestHandlerTest extends SolrTestCaseJ4 {}}+ public void testPingInClusterWithNoHealthCheck() throws Exception {++ File solrXml = new File(SolrTestCaseJ4.TEST_HOME(), "solr-no-core.xml");+ MiniSolrCloudCluster miniCluster = new MiniSolrCloudCluster(NUM_SERVERS, createTempDir().toFile(), solrXml, buildJettyConfig("/solr"));++ final CloudSolrClient cloudSolrClient = miniCluster.getSolrClient();++ try {+ assertNotNull(miniCluster.getZkServer());+ List<JettySolrRunner> jettys = miniCluster.getJettySolrRunners();+ assertEquals(NUM_SERVERS, jettys.size());+ for (JettySolrRunner jetty : jettys) {+ assertTrue(jetty.isRunning());+ }++ // create collection+ String collectionName = "testSolrCloudCollection";+ String configName = "solrCloudCollectionConfig";+ File configDir = new File(SolrTestCaseJ4.TEST_HOME() + File.separator + "collection1" + File.separator + "conf");+ miniCluster.uploadConfigDir(configDir, configName);+ miniCluster.createCollection(collectionName, NUM_SHARDS, REPLICATION_FACTOR, configName, null);++ // Send distributed and non-distributed ping query+ SolrPingWithDistrib reqDistrib = new SolrPingWithDistrib();+ reqDistrib.setDistrib(true);+ SolrPingResponse rsp = reqDistrib.process(cloudSolrClient, collectionName);+ assertEquals(0, rsp.getStatus());++ SolrPing reqNonDistrib = new SolrPing();+ rsp = reqNonDistrib.process(cloudSolrClient, collectionName);+ assertEquals(0, rsp.getStatus());++ // delete the collection we created earlier+ miniCluster.deleteCollection(collectionName);++ }+ finally {+ miniCluster.shutdown();+ }+ }++/*** Helper Method: Executes the request against the handler, returns* the response, and closes the request.@@ -181,4 +232,12 @@ public class PingRequestHandlerTest extends SolrTestCaseJ4 {return rsp;}+ class SolrPingWithDistrib extends SolrPing {+ public SolrPing setDistrib(boolean distrib) {+ getParams().add("distrib", distrib ? "true" : "false");+ return this;+ }+ }++}