Apache Solr open-source search software
SOLR-4228: Add full ping functionality to SolrPing in SolrJ
Shawn Heiseya41bfba67303
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 85004b2e8cc..431f68a46d5 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -84,6 +84,9 @@ New Features* SOLR-4048: Add findRecursive method to NamedList. (Shawn Heisey)+* SOLR-4228: SolrJ's SolrPing object has new methods for ping, enable, and+ disable. (Shawn Heisey)+Bug Fixes----------------------diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/request/SolrPing.java b/solr/solrj/src/java/org/apache/solr/client/solrj/request/SolrPing.javaindex 8c06d84e308..ca81425c656 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/request/SolrPing.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/request/SolrPing.java@@ -17,50 +17,102 @@package org.apache.solr.client.solrj.request;+import java.io.IOException;+import java.util.Collection;+import org.apache.solr.client.solrj.SolrRequest;import org.apache.solr.client.solrj.SolrServer;import org.apache.solr.client.solrj.SolrServerException;import org.apache.solr.client.solrj.response.SolrPingResponse;+import org.apache.solr.common.params.CommonParams;import org.apache.solr.common.params.ModifiableSolrParams;import org.apache.solr.common.util.ContentStream;-import java.io.IOException;-import java.util.Collection;-/*** Verify that there is a working Solr core at the URL of a {@link SolrServer}.* To use this class, the solrconfig.xml for the relevant core must include the* request handler for <code>/admin/ping</code>.- *+ ** @since solr 1.3*/-public class SolrPing extends SolrRequest-{+public class SolrPing extends SolrRequest {++ /** serialVersionUID. */+ private static final long serialVersionUID = 5828246236669090017L;++ /** Request parameters. */private ModifiableSolrParams params;- public SolrPing()- {- super( METHOD.GET, "/admin/ping" );+ /**+ * Create a new SolrPing object.+ */+ public SolrPing() {+ super(METHOD.GET, CommonParams.PING_HANDLER);params = new ModifiableSolrParams();}-+@Overridepublic Collection<ContentStream> getContentStreams() {return null;}-+@Overridepublic ModifiableSolrParams getParams() {return params;}-+@Override- public SolrPingResponse process( SolrServer server ) throws SolrServerException, IOException- {+ public SolrPingResponse process(SolrServer server)+ throws SolrServerException, IOException {long startTime = System.currentTimeMillis();SolrPingResponse res = new SolrPingResponse();- res.setResponse( server.request( this ) );- res.setElapsedTime( System.currentTimeMillis()-startTime );+ res.setResponse(server.request(this));+ res.setElapsedTime(System.currentTimeMillis() - startTime);return res;}++ /**+ * Remove the action parameter from this request. This will result in the same+ * behavior as {@code SolrPing#setActionPing()}. For Solr server version 4.0+ * and later.+ *+ * @return this+ */+ public SolrPing removeAction() {+ params.remove(CommonParams.ACTION);+ return this;+ }++ /**+ * Set the action parameter on this request to enable. This will delete the+ * health-check file for the Solr core. For Solr server version 4.0 and later.+ *+ * @return this+ */+ public SolrPing setActionDisable() {+ params.set(CommonParams.ACTION, CommonParams.DISABLE);+ return this;+ }++ /**+ * Set the action parameter on this request to enable. This will create the+ * health-check file for the Solr core. For Solr server version 4.0 and later.+ *+ * @return this+ */+ public SolrPing setActionEnable() {+ params.set(CommonParams.ACTION, CommonParams.ENABLE);+ return this;+ }++ /**+ * Set the action parameter on this request to ping. This is the same as not+ * including the action at all. For Solr server version 4.0 and later.+ *+ * @return this+ */+ public SolrPing setActionPing() {+ params.set(CommonParams.ACTION, CommonParams.PING);+ return this;+ }}diff --git a/solr/solrj/src/java/org/apache/solr/common/params/CommonParams.java b/solr/solrj/src/java/org/apache/solr/common/params/CommonParams.javaindex dd7987d649e..9f14c89dc8f 100755--- a/solr/solrj/src/java/org/apache/solr/common/params/CommonParams.java+++ b/solr/solrj/src/java/org/apache/solr/common/params/CommonParams.java@@ -59,6 +59,23 @@ public interface CommonParams {/** number of documents to return starting at "start" */public static final String ROWS ="rows";++ // SOLR-4228 start+ /** handler value for SolrPing */+ public static final String PING_HANDLER = "/admin/ping";++ /** "action" parameter for SolrPing */+ public static final String ACTION = "action";++ /** "disable" value for SolrPing action */+ public static final String DISABLE = "disable";++ /** "enable" value for SolrPing action */+ public static final String ENABLE = "enable";++ /** "ping" value for SolrPing action */+ public static final String PING = "ping";+ // SOLR-4228 end//Issue 1726 start/** score of the last document of the previous page */diff --git a/solr/solrj/src/test-files/solrj/solr/collection1/conf/solrconfig.xml b/solr/solrj/src/test-files/solrj/solr/collection1/conf/solrconfig.xmlindex d78ffae8b76..f1a31de6f40 100644--- a/solr/solrj/src/test-files/solrj/solr/collection1/conf/solrconfig.xml+++ b/solr/solrj/src/test-files/solrj/solr/collection1/conf/solrconfig.xml@@ -49,7 +49,17 @@<requestHandler name="standard" class="solr.StandardRequestHandler" default="true" /><requestHandler name="/update" class="solr.UpdateRequestHandler" /><requestHandler name="/admin/" class="org.apache.solr.handler.admin.AdminHandlers" />-++ <requestHandler name="/admin/ping" class="solr.PingRequestHandler">+ <lst name="invariants">+ <str name="q">*:*</str>+ </lst>+ <lst name="defaults">+ <str name="echoParams">all</str>+ </lst>+ <str name="healthcheckFile">server-enabled.txt</str>+ </requestHandler>+<!-- config for the admin interface --><admin><defaultQuery>solr</defaultQuery>diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/request/SolrPingTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/request/SolrPingTest.javanew file mode 100644index 00000000000..0c633d4eb6d--- /dev/null+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/request/SolrPingTest.java@@ -0,0 +1,83 @@+package org.apache.solr.client.solrj.request;++/*+ * 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.+ */++import junit.framework.Assert;++import org.apache.solr.SolrJettyTestBase;+import org.apache.solr.client.solrj.response.SolrPingResponse;+import org.apache.solr.common.SolrException;+import org.apache.solr.common.SolrInputDocument;+import org.junit.Before;+import org.junit.BeforeClass;+import org.junit.Test;++/**+ * Test SolrPing in Solrj+ */+public class SolrPingTest extends SolrJettyTestBase {++ @BeforeClass+ public static void beforeClass() throws Exception {+ // The following works, but it seems like it's probably the wrong way to do+ // this.+ initCore("solrconfig.xml", "schema.xml", "../../test-files/solrj/solr",+ "collection1");+ }++ @Before+ @Override+ public void setUp() throws Exception {+ super.setUp();+ clearIndex();+ assertU(commit());+ assertU(optimize());+ SolrInputDocument doc = new SolrInputDocument();+ doc.setField("id", 1);+ doc.setField("terms_s", "samsung");+ getSolrServer().add(doc);+ getSolrServer().commit(true, true);+ }++ @Test+ public void testEnabledSolrPing() throws Exception {+ SolrPing ping = new SolrPing();+ SolrPingResponse rsp = null;+ ping.setActionEnable();+ ping.process(getSolrServer());+ ping.removeAction();+ rsp = ping.process(getSolrServer());+ Assert.assertNotNull(rsp);+ }++ @Test(expected = SolrException.class)+ public void testDisabledSolrPing() throws Exception {+ SolrPing ping = new SolrPing();+ SolrPingResponse rsp = null;+ ping.setActionDisable();+ try {+ ping.process(getSolrServer());+ } catch (Exception e) {+ throw new Exception("disable action failed!");+ }+ ping.setActionPing();+ rsp = ping.process(getSolrServer());+ // the above line should fail with a 503 SolrException.+ Assert.assertNotNull(rsp);+ }+}