SOLR-8083: convert the ZookeeperInfoServlet to a request handler at /admin/zookeeper

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1705662 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Noble Paul 2015-09-28 12:10:39 +00:00
parent ab3ff9f0fe
commit a72c536a2a
7 changed files with 202 additions and 186 deletions

View File

@ -49,6 +49,7 @@ import org.apache.solr.handler.admin.ConfigSetsHandler;
import org.apache.solr.handler.admin.CoreAdminHandler;
import org.apache.solr.handler.admin.InfoHandler;
import org.apache.solr.handler.admin.SecurityConfHandler;
import org.apache.solr.handler.admin.ZookeeperInfoHandler;
import org.apache.solr.handler.component.HttpShardHandlerFactory;
import org.apache.solr.handler.component.ShardHandlerFactory;
import org.apache.solr.logging.LogWatcher;
@ -398,6 +399,7 @@ public class CoreContainer {
initializeAuthorizationPlugin((Map<String, Object>) securityConfig.data.get("authorization"));
initializeAuthenticationPlugin((Map<String, Object>) securityConfig.data.get("authentication"));
containerHandlers.put(ZK_PATH, new ZookeeperInfoHandler(this));
securityConfHandler = new SecurityConfHandler(this);
collectionsHandler = createHandler(cfg.getCollectionsHandlerClass(), CollectionsHandler.class);
containerHandlers.put(COLLECTIONS_HANDLER_PATH, collectionsHandler);

View File

@ -15,13 +15,12 @@
* limitations under the License.
*/
package org.apache.solr.servlet;
package org.apache.solr.handler.admin;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
@ -29,6 +28,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@ -46,21 +46,33 @@ import org.apache.solr.common.cloud.OnReconnect;
import org.apache.solr.common.cloud.Replica;
import org.apache.solr.common.cloud.SolrZkClient;
import org.apache.solr.common.cloud.ZkStateReader;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.MapSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.ContentStream;
import org.apache.solr.common.util.Utils;
import org.apache.solr.core.CoreContainer;
import org.apache.solr.util.FastWriter;
import org.apache.solr.handler.ReplicationHandler;
import org.apache.solr.handler.RequestHandlerBase;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.JSONResponseWriter;
import org.apache.solr.response.RawResponseWriter;
import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.util.SimplePostTool.BAOS;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.data.Stat;
import org.apache.zookeeper.server.ByteBufferInputStream;
import org.noggit.CharArr;
import org.noggit.JSONWriter;
import org.noggit.ObjectBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.apache.solr.common.params.CommonParams.OMIT_HEADER;
import static org.apache.solr.common.params.CommonParams.PATH;
import static org.apache.solr.common.params.CommonParams.WT;
/**
@ -68,13 +80,25 @@ import static org.apache.solr.common.params.CommonParams.PATH;
*
* @since solr 4.0
*/
public final class ZookeeperInfoServlet extends BaseSolrServlet {
static final Logger log = LoggerFactory.getLogger(ZookeeperInfoServlet.class);
public final class ZookeeperInfoHandler extends RequestHandlerBase {
private final CoreContainer cores;
static final Logger log = LoggerFactory.getLogger(ZookeeperInfoHandler.class);
// used for custom sorting collection names looking like prefix##
// only go out to 7 digits (which safely fits in an int)
private static final Pattern endsWithDigits = Pattern.compile("^(\\D*)(\\d{1,7}?)$");
public ZookeeperInfoHandler(CoreContainer cc) {
this.cores = cc;
}
@Override
public String getDescription() {
return "Fetch Zookeeper contents";
}
/**
* Enumeration of ways to filter collections on the graph panel.
*/
@ -323,15 +347,12 @@ public final class ZookeeperInfoServlet extends BaseSolrServlet {
private PagedCollectionSupport pagingSupport;
@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,IOException {
// This attribute is set by the SolrDispatchFilter
CoreContainer cores = (CoreContainer) request.getAttribute("org.apache.solr.CoreContainer");
if (cores == null) {
throw new ServletException("Missing request attribute org.apache.solr.CoreContainer.");
}
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
final SolrParams params = req.getParams();
Map<String, String> map = new HashMap<>(1);
map.put(WT, "raw");
map.put(OMIT_HEADER, "true");
req.setParams(SolrParams.wrapDefaults(new MapSolrParams(map), params));
synchronized (this) {
if (pagingSupport == null) {
pagingSupport = new PagedCollectionSupport();
@ -343,18 +364,6 @@ public final class ZookeeperInfoServlet extends BaseSolrServlet {
}
}
final SolrParams params;
try {
params = SolrRequestParsers.DEFAULT.parse(null, request.getServletPath(), request).getParams();
} catch (Exception e) {
int code=500;
if (e instanceof SolrException) {
code = Math.min(599, Math.max(100, ((SolrException)e).code()));
}
response.sendError(code, e.toString());
return;
}
String path = params.get(PATH);
String addr = params.get("addr");
@ -368,8 +377,8 @@ public final class ZookeeperInfoServlet extends BaseSolrServlet {
String dumpS = params.get("dump");
boolean dump = dumpS != null && dumpS.equals("true");
int start = paramAsInt("start", params, 0);
int rows = paramAsInt("rows", params, -1);
int start = params.getInt("start", 0);
int rows = params.getInt("rows", -1);
String filterType = params.get("filterType");
if (filterType != null) {
@ -386,12 +395,7 @@ public final class ZookeeperInfoServlet extends BaseSolrServlet {
filter = null;
}
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json");
Writer out = new FastWriter(new OutputStreamWriter(response.getOutputStream(), StandardCharsets.UTF_8));
ZKPrinter printer = new ZKPrinter(response, out, cores.getZkController(), addr);
ZKPrinter printer = new ZKPrinter(cores.getZkController(), addr);
printer.detail = detail;
printer.dump = dump;
boolean isGraphView = "graph".equals(params.get("view"));
@ -404,38 +408,14 @@ public final class ZookeeperInfoServlet extends BaseSolrServlet {
} finally {
printer.close();
}
out.flush();
}
@Override
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,IOException {
doGet(request, response);
}
protected int paramAsInt(final String paramName, final SolrParams params, final int defaultVal) {
int val = defaultVal;
String paramS = params.get(paramName);
if (paramS != null) {
String trimmed = paramS.trim();
if (trimmed.length() > 0) {
try {
val = Integer.parseInt(trimmed);
} catch (NumberFormatException nfe) {
log.warn("Invalid value "+paramS+" passed for parameter "+paramName+"; expected integer!");
}
}
}
return val;
rsp.getValues().add(RawResponseWriter.CONTENT,printer);
}
//--------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------
static class ZKPrinter {
static class ZKPrinter implements ContentStream {
static boolean FULLPATH_DEFAULT = false;
boolean indent = true;
@ -448,8 +428,8 @@ public final class ZookeeperInfoServlet extends BaseSolrServlet {
boolean doClose; // close the client after done if we opened it
final HttpServletResponse response;
final Writer out;
final BAOS baos = new BAOS();
final Writer out = new OutputStreamWriter(baos, StandardCharsets.UTF_8);
SolrZkClient zkClient;
int level;
@ -459,10 +439,8 @@ public final class ZookeeperInfoServlet extends BaseSolrServlet {
PagedCollectionSupport pagingSupport;
ZkController zkController;
public ZKPrinter(HttpServletResponse response, Writer out, ZkController controller, String addr) throws IOException {
public ZKPrinter(ZkController controller, String addr) throws IOException {
this.zkController = controller;
this.response = response;
this.out = out;
this.addr = addr;
if (addr == null) {
@ -500,6 +478,11 @@ public final class ZookeeperInfoServlet extends BaseSolrServlet {
if (doClose) {
zkClient.close();
}
try {
out.flush();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
// main entry point
@ -551,7 +534,8 @@ public final class ZookeeperInfoServlet extends BaseSolrServlet {
}
void writeError(int code, String msg) throws IOException {
response.setStatus(code);
throw new SolrException(ErrorCode.getErrorCode(code), msg);
/*response.setStatus(code);
CharArr chars = new CharArr();
JSONWriter w = new JSONWriter(chars, 2);
@ -567,7 +551,7 @@ public final class ZookeeperInfoServlet extends BaseSolrServlet {
w.writeString(msg);
w.endObject();
out.write(chars.toString());
out.write(chars.toString());*/
}
@ -588,7 +572,7 @@ public final class ZookeeperInfoServlet extends BaseSolrServlet {
json.writeString("attr");
json.writeNameSeparator();
json.startObject();
writeKeyValue(json, "href", "zookeeper?detail=true&path=" + URLEncoder.encode(path, "UTF-8"), true);
writeKeyValue(json, "href", "admin/zookeeper?detail=true&path=" + URLEncoder.encode(path, "UTF-8"), true);
json.endObject();
json.endObject();
@ -826,5 +810,41 @@ public final class ZookeeperInfoServlet extends BaseSolrServlet {
}
return true;
}
/* @Override
public void write(OutputStream os) throws IOException {
ByteBuffer bytes = baos.getByteBuffer();
os.write(bytes.array(),0,bytes.limit());
}
*/
@Override
public String getName() {
return null;
}
@Override
public String getSourceInfo() {
return null;
}
@Override
public String getContentType() {
return JSONResponseWriter.CONTENT_TYPE_JSON_UTF8;
}
@Override
public Long getSize() {
return null;
}
@Override
public InputStream getStream() throws IOException {
return new ByteBufferInputStream(baos.getByteBuffer());
}
@Override
public Reader getReader() throws IOException {
return null;
}
}
}

View File

@ -42,7 +42,7 @@ import org.apache.solr.search.SolrReturnFields;
*/
public class JSONResponseWriter implements QueryResponseWriter {
static String CONTENT_TYPE_JSON_UTF8 = "application/json; charset=UTF-8";
public static String CONTENT_TYPE_JSON_UTF8 = "application/json; charset=UTF-8";
private String contentType = CONTENT_TYPE_JSON_UTF8;

View File

@ -169,6 +169,7 @@ public interface CommonParams {
public static final String CONFIGSETS_HANDLER_PATH = "/admin/configs";
public static final String AUTHZ_PATH = "/admin/authorization";
public static final String AUTHC_PATH = "/admin/authentication";
public static final String ZK_PATH = "/admin/zookeeper";
/** valid values for: <code>echoParams</code> */
public enum EchoParamStyle {

View File

@ -75,11 +75,6 @@
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>Zookeeper</servlet-name>
<servlet-class>org.apache.solr.servlet.ZookeeperInfoServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>LoadAdminUI</servlet-name>
<servlet-class>org.apache.solr.servlet.LoadAdminUiServlet</servlet-class>
@ -101,7 +96,7 @@
<servlet-class>org.apache.solr.servlet.RedirectServlet</servlet-class>
<init-param>
<param-name>destination</param-name>
<param-value>${context}/zookeeper</param-value>
<param-value>${context}/admin/zookeeper</param-value>
</init-param>
</servlet>
@ -135,17 +130,15 @@
<servlet-name>RedirectOldZookeeper</servlet-name>
<url-pattern>/zookeeper.jsp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>RedirectOldZookeeper</servlet-name>
<url-pattern>/zookeeper</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>RedirectLogging</servlet-name>
<url-pattern>/logging</url-pattern>
</servlet-mapping>
<!-- Servlet Mapping -->
<servlet-mapping>
<servlet-name>Zookeeper</servlet-name>
<url-pattern>/zookeeper</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LoadAdminUI</servlet-name>
<url-pattern>/admin.html</url-pattern>

View File

@ -60,7 +60,7 @@ solrAdminServices.factory('System',
}])
.factory('Zookeeper',
['$resource', function($resource) {
return $resource('/solr/zookeeper', {wt:'json', _:Date.now()}, {
return $resource('/solr/admin/zookeeper', {wt:'json', _:Date.now()}, {
"simple": {},
"dump": {params: {dump: "true"}},
"liveNodes": {params: {path: '/live_nodes'}},

View File

@ -91,7 +91,7 @@ var init_debug = function( cloud_element )
$.ajax
(
{
url : app.config.solr_path + '/zookeeper?wt=json&dump=true',
url : app.config.solr_path + '/admin/zookeeper?wt=json&dump=true',
dataType : 'text',
context : debug_element,
beforeSend : function( xhr, settings )
@ -455,7 +455,7 @@ var prepare_graph = function( graph_element, callback )
$.ajax
(
{
url : app.config.solr_path + '/zookeeper?wt=json&path=%2Flive_nodes',
url : app.config.solr_path + '/admin/zookeeper?wt=json&path=%2Flive_nodes',
dataType : 'json',
success : function( response, text_status, xhr )
{
@ -467,7 +467,7 @@ var prepare_graph = function( graph_element, callback )
var start = $( '#cloudGraphPagingStart' ).val();
var rows = $( '#cloudGraphPagingRows' ).val();
var clusterStateUrl = app.config.solr_path + '/zookeeper?wt=json&detail=true&path=%2Fclusterstate.json&view=graph';
var clusterStateUrl = app.config.solr_path + '/admin/zookeeper?wt=json&detail=true&path=%2Fclusterstate.json&view=graph';
if (start && rows)
clusterStateUrl += ('&start='+start+'&rows='+rows);
@ -591,7 +591,7 @@ var init_tree = function( tree_element )
$.ajax
(
{
url : app.config.solr_path + '/zookeeper?wt=json',
url : app.config.solr_path + '/admin/zookeeper?wt=json',
dataType : 'json',
context : tree_element,
beforeSend : function( xhr, settings )
@ -859,7 +859,7 @@ sammy.get
$.ajax
(
{
url : app.config.solr_path + '/zookeeper?wt=json',
url : app.config.solr_path + '/admin/zookeeper?wt=json',
dataType : 'json',
context : cloud_element,
success : function( response, text_status, xhr )