Apache Lucene open-source search software
SOLR-408 adding PingRequestHandler.java and deprecating <pingQuery> syntax
Ryan McKinleyc4f99ab049b1
diff --git a/CHANGES.txt b/CHANGES.txtindex 56ab3581124..e85b8ea7873 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -148,6 +148,11 @@ New Featuresand a bevy of new and renamed options (see the wiki).(Mike Krimerman, Scott Taber via klaas).+29. SOLR-408: Added PingRequestHandler and deprecated SolrCore.getPingQueryRequest().+ Ping requests should be configured using standard RequestHandler syntax in+ solrconfig.xml rather then using the <pingQuery></pingQuery> syntax.+ (Karsten Sperling via ryan)+Changes in runtime behaviordiff --git a/client/java/solrj/src/org/apache/solr/client/solrj/request/SolrPing.java b/client/java/solrj/src/org/apache/solr/client/solrj/request/SolrPing.javaindex 33eacd354a4..d21b14731be 100644--- a/client/java/solrj/src/org/apache/solr/client/solrj/request/SolrPing.java+++ b/client/java/solrj/src/org/apache/solr/client/solrj/request/SolrPing.java@@ -34,18 +34,19 @@ import org.apache.solr.common.util.ContentStream;*/public class SolrPing extends RequestBase{+ private ModifiableSolrParams params;+public SolrPing(){super( METHOD.GET, "/admin/ping" );+ params = new ModifiableSolrParams();}public Collection<ContentStream> getContentStreams() {return null;}- public SolrParams getParams() {- ModifiableSolrParams params = new ModifiableSolrParams();- params.set( "q", "solrpingquery" );+ public ModifiableSolrParams getParams() {return params;}diff --git a/client/java/solrj/test/org/apache/solr/client/solrj/SolrExampleTests.java b/client/java/solrj/test/org/apache/solr/client/solrj/SolrExampleTests.javaindex 8bac75ca198..db86963d8fc 100644--- a/client/java/solrj/test/org/apache/solr/client/solrj/SolrExampleTests.java+++ b/client/java/solrj/test/org/apache/solr/client/solrj/SolrExampleTests.java@@ -28,6 +28,7 @@ import junit.framework.Assert;import org.apache.solr.client.solrj.request.DirectXmlRequest;import org.apache.solr.client.solrj.request.LukeRequest;+import org.apache.solr.client.solrj.request.SolrPing;import org.apache.solr.client.solrj.response.LukeResponse;import org.apache.solr.client.solrj.response.QueryResponse;import org.apache.solr.client.solrj.response.UpdateResponse;@@ -255,7 +256,6 @@ abstract public class SolrExampleTests extends SolrExampleTestBaseassertNumFound( "*:*", 0 ); // make sure it got out}-public void testLukeHandler() throws Exception{SolrServer server = getSolrServer();@@ -281,4 +281,28 @@ abstract public class SolrExampleTests extends SolrExampleTestBasersp = luke.process( server );assertNotNull( rsp.getFieldTypeInfo() );}+++ public void testPingHandler() throws Exception+ {+ SolrServer server = getSolrServer();++ // Empty the database...+ server.deleteByQuery( "*:*" );// delete everything!+ server.commit();+ assertNumFound( "*:*", 0 ); // make sure it got in++ // should be ok+ server.ping();++ try {+ SolrPing ping = new SolrPing();+ ping.getParams().set( "qt", "unknown handler!" );+ ping.process( server );+ fail( "sent unknown query type!" );+ }+ catch( Exception ex ) {+ // expected+ }+ }}diff --git a/example/solr/conf/solrconfig.xml b/example/solr/conf/solrconfig.xmlindex 39ee7496d04..d727465d7fe 100755--- a/example/solr/conf/solrconfig.xml+++ b/example/solr/conf/solrconfig.xml@@ -455,6 +455,15 @@<requestHandler name="/admin/threads" class="org.apache.solr.handler.admin.ThreadDumpHandler" /><requestHandler name="/admin/properties" class="org.apache.solr.handler.admin.PropertiesRequestHandler" />+ <!-- ping/healthcheck -->+ <requestHandler name="/admin/ping" class="PingRequestHandler">+ <lst name="defaults">+ <str name="qt">standard</str>+ <str name="q">solrpingquery</str>+ <str name="echoParams">all</str>+ </lst>+ </requestHandler>+<!-- Echo the request contents back to the client --><requestHandler name="/debug/dump" class="solr.DumpRequestHandler" ><lst name="defaults">@@ -523,12 +532,7 @@<admin><defaultQuery>solr</defaultQuery><gettableFiles>solrconfig.xml schema.xml admin-extra.html</gettableFiles>- <!-- pingQuery should be "URLish" ...- & separated key=val pairs ... but there shouldn't be any- URL escaping of the values -->- <pingQuery>- qt=standard&q=solrpingquery- </pingQuery>+<!-- configure a healthcheck file for servers behind a loadbalancer<healthcheck type="file">server-enabled</healthcheck>-->diff --git a/src/java/org/apache/solr/core/SolrConfig.java b/src/java/org/apache/solr/core/SolrConfig.javaindex c1da047c60b..942120b789f 100644--- a/src/java/org/apache/solr/core/SolrConfig.java+++ b/src/java/org/apache/solr/core/SolrConfig.java@@ -18,6 +18,7 @@package org.apache.solr.core;import org.apache.solr.common.util.NamedList;+import org.apache.solr.handler.PingRequestHandler;import org.apache.solr.request.LocalSolrQueryRequest;import org.apache.solr.request.SolrQueryRequest;@@ -151,7 +152,9 @@ public class SolrConfig extends Config {// default & main index configurationspublic final SolrIndexConfig defaultIndexConfig;public final SolrIndexConfig mainIndexConfig;+// ping query request parameters+ @Deprecatedprivate final NamedList pingQueryParams;static private NamedList readPingQueryParams(SolrConfig config) {@@ -172,7 +175,10 @@ public class SolrConfig extends Config {/*** Returns a Request object based on the admin/pingQuery section* of the Solr config file.+ *+ * @use {@link PingRequestHandler} instead*/+ @Deprecatedpublic SolrQueryRequest getPingQueryRequest(SolrCore core) {return new LocalSolrQueryRequest(core, pingQueryParams);}diff --git a/src/java/org/apache/solr/core/SolrCore.java b/src/java/org/apache/solr/core/SolrCore.javaindex 875a3ff168a..2ef4c5c5282 100644--- a/src/java/org/apache/solr/core/SolrCore.java+++ b/src/java/org/apache/solr/core/SolrCore.java@@ -42,6 +42,7 @@ import org.apache.solr.common.params.CommonParams.EchoParamStyle;import org.apache.solr.common.util.DOMUtil;import org.apache.solr.common.util.NamedList;import org.apache.solr.common.util.SimpleOrderedMap;+import org.apache.solr.handler.PingRequestHandler;import org.apache.solr.highlight.SolrHighlighter;import org.apache.solr.request.JSONResponseWriter;import org.apache.solr.request.PythonResponseWriter;@@ -235,7 +236,7 @@ public final class SolrCore {/**- * @return the last core initalized. If you are using multiple cores,+ * @return the last core initialized. If you are using multiple cores,* this is not a function to use.*/@Deprecated@@ -389,7 +390,10 @@ public final class SolrCore {/*** Returns a Request object based on the admin/pingQuery section* of the Solr config file.+ *+ * @use {@link PingRequestHandler} instead*/+ @Deprecatedpublic SolrQueryRequest getPingQueryRequest() {return solrConfig.getPingQueryRequest(this);}diff --git a/src/java/org/apache/solr/handler/PingRequestHandler.java b/src/java/org/apache/solr/handler/PingRequestHandler.javanew file mode 100644index 00000000000..1a5c93d5733--- /dev/null+++ b/src/java/org/apache/solr/handler/PingRequestHandler.java@@ -0,0 +1,100 @@+/**+ * 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;++import java.io.File;++import org.apache.solr.common.SolrException;+import org.apache.solr.common.params.CommonParams;+import org.apache.solr.common.params.SolrParams;+import org.apache.solr.core.SolrCore;+import org.apache.solr.request.SolrQueryRequest;+import org.apache.solr.request.SolrQueryResponse;+import org.apache.solr.request.SolrRequestHandler;+++/**+ * Ping solr core+ *+ * @since solr 1.3+ */+public class PingRequestHandler extends RequestHandlerBase+{+ @Override+ public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception+ {+ SolrParams params = req.getParams();+ SolrParams required = params.required();+ SolrCore core = req.getCore();++ // Check if the service is available+ String healthcheck = core.getSolrConfig().get("admin/healthcheck/text()", null );+ if( healthcheck != null && !new File(healthcheck).exists() ) {+ throw new SolrException(SolrException.ErrorCode.SERVICE_UNAVAILABLE, "Service disabled", true);+ }++ // Get the RequestHandler+ String qt = required.get( CommonParams.QT );+ SolrRequestHandler handler = core.getRequestHandler( qt );+ if( handler == null ) {+ throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,+ "Unknown RequestHandler: "+qt );+ }++ // 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( Throwable th ) {+ ex = th;+ }++ // 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" );+ }+++ //////////////////////// SolrInfoMBeans methods //////////////////////++ @Override+ public String getVersion() {+ return "$Revision$";+ }++ @Override+ public String getDescription() {+ return "Reports application health to a load-balancer";+ }++ @Override+ public String getSourceId() {+ return "$Id$";+ }++ @Override+ public String getSource() {+ return "$URL$";+ }+}diff --git a/src/webapp/WEB-INF/web.xml b/src/webapp/WEB-INF/web.xmlindex f3eaa2a0268..13669b3eae9 100644--- a/src/webapp/WEB-INF/web.xml+++ b/src/webapp/WEB-INF/web.xml@@ -64,6 +64,7 @@<load-on-startup>2</load-on-startup></servlet>+ <!-- @Deprecated --><servlet><servlet-name>ping</servlet-name><jsp-file>/admin/ping.jsp</jsp-file>@@ -79,6 +80,7 @@<url-pattern>/update/*</url-pattern></servlet-mapping>+ <!-- @Deprecated --><servlet-mapping><servlet-name>ping</servlet-name><url-pattern>/admin/ping</url-pattern>diff --git a/src/webapp/resources/admin/ping.jsp b/src/webapp/resources/admin/ping.jspindex a85775c5f9e..6987f0d671f 100644--- a/src/webapp/resources/admin/ping.jsp+++ b/src/webapp/resources/admin/ping.jsp@@ -29,6 +29,10 @@<solr><ping><%+//+// Deprecated -- use PingRequestHandler+//+Object ocore = request.getAttribute("org.apache.solr.SolrCore");SolrCore core = ocore instanceof SolrCore? (SolrCore) ocore : SolrCore.getSolrCore();