From 555cc509307eb67a49f9902fd213e1005e1861bd Mon Sep 17 00:00:00 2001 From: Gregory Chanan Date: Sat, 12 Sep 2015 01:17:45 +0000 Subject: [PATCH] SOLR-7746: Ping requests stopped working with distrib=true in Solr 5.2.1 git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1702581 13f79535-47bb-0310-9956-ffa450edef68 --- solr/CHANGES.txt | 2 + .../solr/handler/PingRequestHandler.java | 73 +++++++++++++------ .../solr/handler/PingRequestHandlerTest.java | 59 +++++++++++++++ 3 files changed, 113 insertions(+), 21 deletions(-) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index d35c83d5435..9b8980bb71d 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -188,6 +188,8 @@ Bug Fixes related 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.java index 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). *

* *
@@ -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());
+      List 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;    
+    }      
+  }
+  
+
 }