Skip to content

Commit

Permalink
Query http listeners. Closes #10.
Browse files Browse the repository at this point in the history
  • Loading branch information
kimchy committed Feb 14, 2010
1 parent cfafb52 commit b5f3fc9
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,71 @@

package org.elasticsearch.action.admin.cluster.node.info;

import com.google.common.collect.ImmutableMap;
import org.elasticsearch.action.support.nodes.NodeOperationResponse;
import org.elasticsearch.cluster.node.Node;
import org.elasticsearch.util.settings.ImmutableSettings;
import org.elasticsearch.util.settings.Settings;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Map;

/**
* @author kimchy (Shay Banon)
*/
public class NodeInfo extends NodeOperationResponse {

protected NodeInfo() {
private ImmutableMap<String, String> attributes;

private Settings settings;

NodeInfo() {
}

public NodeInfo(Node node, Map<String, String> attributes, Settings settings) {
this(node, ImmutableMap.copyOf(attributes), settings);
}

public NodeInfo(Node node) {
public NodeInfo(Node node, ImmutableMap<String, String> attributes, Settings settings) {
super(node);
this.attributes = attributes;
this.settings = settings;
}

public ImmutableMap<String, String> attributes() {
return this.attributes;
}

public Settings settings() {
return this.settings;
}

public static NodeInfo readNodeInfo(DataInput in) throws ClassNotFoundException, IOException {
NodeInfo nodeInfo = new NodeInfo();
nodeInfo.readFrom(in);
return nodeInfo;
}

@Override public void readFrom(DataInput in) throws IOException, ClassNotFoundException {
super.readFrom(in);
ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
int size = in.readInt();
for (int i = 0; i < size; i++) {
builder.put(in.readUTF(), in.readUTF());
}
attributes = builder.build();
settings = ImmutableSettings.readSettingsFromStream(in);
}

@Override public void writeTo(DataOutput out) throws IOException {
super.writeTo(out);
out.writeInt(attributes.size());
for (Map.Entry<String, String> entry : attributes.entrySet()) {
out.writeUTF(entry.getKey());
out.writeUTF(entry.getValue());
}
ImmutableSettings.writeSettingsToStream(settings, out);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.action.admin.cluster.node.info;

import com.google.common.collect.ImmutableMap;
import com.google.inject.Inject;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.action.TransportActions;
Expand All @@ -28,6 +29,7 @@
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;
import org.elasticsearch.util.MapBuilder;
import org.elasticsearch.util.settings.Settings;

import java.util.ArrayList;
Expand All @@ -39,11 +41,21 @@
*/
public class TransportNodesInfo extends TransportNodesOperationAction<NodesInfoRequest, NodesInfoResponse, TransportNodesInfo.NodeInfoRequest, NodeInfo> {

private volatile ImmutableMap<String, String> nodeAttributes = ImmutableMap.of();

@Inject public TransportNodesInfo(Settings settings, ClusterName clusterName, ThreadPool threadPool,
ClusterService clusterService, TransportService transportService) {
super(settings, clusterName, threadPool, clusterService, transportService);
}

public synchronized void putNodeAttribute(String key, String value) {
nodeAttributes = new MapBuilder<String, String>().putAll(nodeAttributes).put(key, value).immutableMap();
}

public synchronized void removeNodeAttribute(String key) {
nodeAttributes = new MapBuilder<String, String>().putAll(nodeAttributes).remove(key).immutableMap();
}

@Override protected String transportAction() {
return TransportActions.Admin.Cluster.Node.INFO;
}
Expand Down Expand Up @@ -80,7 +92,7 @@ public class TransportNodesInfo extends TransportNodesOperationAction<NodesInfoR
}

@Override protected NodeInfo nodeOperation(NodeInfoRequest nodeInfoRequest) throws ElasticSearchException {
return new NodeInfo(clusterService.state().nodes().localNode());
return new NodeInfo(clusterService.state().nodes().localNode(), nodeAttributes, settings);
}

@Override protected boolean accumulateExceptions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.google.inject.Inject;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.action.admin.cluster.node.info.TransportNodesInfo;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.util.component.AbstractComponent;
import org.elasticsearch.util.component.Lifecycle;
Expand All @@ -42,15 +43,19 @@ public class HttpServer extends AbstractComponent implements LifecycleComponent<

private final ThreadPool threadPool;

private final TransportNodesInfo nodesInfo;

private final PathTrie<HttpServerHandler> getHandlers;
private final PathTrie<HttpServerHandler> postHandlers;
private final PathTrie<HttpServerHandler> putHandlers;
private final PathTrie<HttpServerHandler> deleteHandlers;

@Inject public HttpServer(Settings settings, HttpServerTransport transport, ThreadPool threadPool) {
@Inject public HttpServer(Settings settings, HttpServerTransport transport, ThreadPool threadPool,
TransportNodesInfo nodesInfo) {
super(settings);
this.transport = transport;
this.threadPool = threadPool;
this.nodesInfo = nodesInfo;

getHandlers = new PathTrie<HttpServerHandler>();
postHandlers = new PathTrie<HttpServerHandler>();
Expand Down Expand Up @@ -88,13 +93,15 @@ public HttpServer start() throws ElasticSearchException {
if (logger.isInfoEnabled()) {
logger.info("{}", transport.boundAddress());
}
nodesInfo.putNodeAttribute("httpAddress", transport.boundAddress().publishAddress().toString());
return this;
}

public HttpServer stop() throws ElasticSearchException {
if (!lifecycle.moveToStopped()) {
return this;
}
nodesInfo.removeNodeAttribute("httpAddress");
transport.stop();
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.elasticsearch.util.settings.Settings;

import java.io.IOException;
import java.util.Map;

/**
* @author kimchy (Shay Banon)
Expand All @@ -47,6 +48,7 @@ public class HttpNodesInfoAction extends BaseHttpServerHandler {

@Override public void handleRequest(final HttpRequest request, final HttpChannel channel) {
String[] nodesIds = HttpActions.splitNodes(request.param("nodeId"));
final boolean includeSettings = HttpActions.paramAsBoolean("settings", false);
NodesInfoRequest nodesInfoRequest = new NodesInfoRequest(nodesIds);
nodesInfoRequest.listenerThreaded(false);
client.admin().cluster().execNodesInfo(nodesInfoRequest, new ActionListener<NodesInfoResponse>() {
Expand All @@ -55,15 +57,31 @@ public class HttpNodesInfoAction extends BaseHttpServerHandler {
JsonBuilder builder = HttpJsonBuilder.cached(request);
builder.startObject();
builder.field("clusterName", result.clusterName().value());

builder.startObject("nodes");
for (NodeInfo nodeInfo : result) {
builder.startObject(nodeInfo.node().id());

builder.field("name", nodeInfo.node().name());
builder.field("transportAddress", nodeInfo.node().address().toString());
builder.field("dataNode", nodeInfo.node().dataNode());

for (Map.Entry<String, String> nodeAttribute : nodeInfo.attributes().entrySet()) {
builder.field(nodeAttribute.getKey(), nodeAttribute.getValue());
}

if (includeSettings) {
builder.startObject("settings");
for (Map.Entry<String, String> entry : nodeInfo.settings().getAsMap().entrySet()) {
builder.field(entry.getKey(), entry.getValue());
}
builder.endObject();
}

builder.endObject();
}
builder.endObject();

builder.endObject();
channel.sendResponse(new JsonHttpResponse(request, HttpResponse.Status.OK, builder));
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class HttpClusterStateAction extends BaseHttpServerHandler {

builder.startObject("settings");
for (Map.Entry<String, String> entry : indexMetaData.settings().getAsMap().entrySet()) {
builder.startObject("setting").field("name", entry.getKey()).field("value", entry.getValue()).endObject();
builder.field(entry.getKey(), entry.getValue());
}
builder.endObject();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public class HttpIndicesStatusAction extends BaseHttpServerHandler {

builder.startObject("settings");
for (Map.Entry<String, String> entry : indexStatus.settings().getAsMap().entrySet()) {
builder.startObject("setting").field("name", entry.getKey()).field("value", entry.getValue()).endObject();
builder.field(entry.getKey(), entry.getValue());
}
builder.endObject();

Expand Down

0 comments on commit b5f3fc9

Please sign in to comment.