From 555cc509307eb67a49f9902fd213e1005e1861bd Mon Sep 17 00:00:00 2001
From: Gregory Chanan
@@ -233,7 +237,7 @@ public class PingRequestHandler extends RequestHandlerBase implements SolrCoreAw SolrCore 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 otherwise SolrRequestHandler 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 exception Throwable 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.java index 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()); + Listjettys = 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; + } + } + + }