mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-17 10:25:15 +00:00
parent
314499cee0
commit
3c5106ae98
@ -18,6 +18,7 @@ Will return, for example:
|
||||
--------------------------------------------------
|
||||
{
|
||||
"cluster_name": "elasticsearch",
|
||||
"status": "green",
|
||||
"indices": {
|
||||
"count": 3,
|
||||
"shards": {
|
||||
|
@ -19,11 +19,13 @@
|
||||
|
||||
package org.elasticsearch.action.admin.cluster.stats;
|
||||
|
||||
import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
|
||||
import org.elasticsearch.action.admin.cluster.node.info.NodeInfo;
|
||||
import org.elasticsearch.action.admin.cluster.node.stats.NodeStats;
|
||||
import org.elasticsearch.action.admin.indices.stats.ShardStats;
|
||||
import org.elasticsearch.action.support.nodes.NodeOperationResponse;
|
||||
import org.elasticsearch.cluster.node.DiscoveryNode;
|
||||
import org.elasticsearch.common.Nullable;
|
||||
import org.elasticsearch.common.io.stream.StreamInput;
|
||||
import org.elasticsearch.common.io.stream.StreamOutput;
|
||||
|
||||
@ -34,15 +36,17 @@ public class ClusterStatsNodeResponse extends NodeOperationResponse {
|
||||
private NodeInfo nodeInfo;
|
||||
private NodeStats nodeStats;
|
||||
private ShardStats[] shardsStats;
|
||||
private ClusterHealthStatus clusterStatus;
|
||||
|
||||
ClusterStatsNodeResponse() {
|
||||
}
|
||||
|
||||
public ClusterStatsNodeResponse(DiscoveryNode node, NodeInfo nodeInfo, NodeStats nodeStats, ShardStats[] shardsStats) {
|
||||
public ClusterStatsNodeResponse(DiscoveryNode node, @Nullable ClusterHealthStatus clusterStatus, NodeInfo nodeInfo, NodeStats nodeStats, ShardStats[] shardsStats) {
|
||||
super(node);
|
||||
this.nodeInfo = nodeInfo;
|
||||
this.nodeStats = nodeStats;
|
||||
this.shardsStats = shardsStats;
|
||||
this.clusterStatus = clusterStatus;
|
||||
}
|
||||
|
||||
public NodeInfo nodeInfo() {
|
||||
@ -53,6 +57,14 @@ public class ClusterStatsNodeResponse extends NodeOperationResponse {
|
||||
return this.nodeStats;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cluster Health Status, only populated on master nodes.
|
||||
*/
|
||||
@Nullable
|
||||
public ClusterHealthStatus clusterStatus() {
|
||||
return clusterStatus;
|
||||
}
|
||||
|
||||
public ShardStats[] shardsStats() {
|
||||
return this.shardsStats;
|
||||
}
|
||||
@ -66,6 +78,10 @@ public class ClusterStatsNodeResponse extends NodeOperationResponse {
|
||||
@Override
|
||||
public void readFrom(StreamInput in) throws IOException {
|
||||
super.readFrom(in);
|
||||
clusterStatus = null;
|
||||
if (in.readBoolean()) {
|
||||
clusterStatus = ClusterHealthStatus.fromValue(in.readByte());
|
||||
}
|
||||
this.nodeInfo = NodeInfo.readNodeInfo(in);
|
||||
this.nodeStats = NodeStats.readNodeStats(in);
|
||||
int size = in.readVInt();
|
||||
@ -78,6 +94,12 @@ public class ClusterStatsNodeResponse extends NodeOperationResponse {
|
||||
@Override
|
||||
public void writeTo(StreamOutput out) throws IOException {
|
||||
super.writeTo(out);
|
||||
if (clusterStatus == null) {
|
||||
out.writeBoolean(false);
|
||||
} else {
|
||||
out.writeBoolean(true);
|
||||
out.writeByte(clusterStatus.value());
|
||||
}
|
||||
nodeInfo.writeTo(out);
|
||||
nodeStats.writeTo(out);
|
||||
out.writeVInt(shardsStats.length);
|
||||
|
@ -19,6 +19,7 @@
|
||||
|
||||
package org.elasticsearch.action.admin.cluster.stats;
|
||||
|
||||
import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
|
||||
import org.elasticsearch.action.support.nodes.NodesOperationResponse;
|
||||
import org.elasticsearch.cluster.ClusterName;
|
||||
import org.elasticsearch.common.io.stream.StreamInput;
|
||||
@ -30,6 +31,7 @@ import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@ -40,6 +42,7 @@ public class ClusterStatsResponse extends NodesOperationResponse<ClusterStatsNod
|
||||
ClusterStatsNodes nodesStats;
|
||||
ClusterStatsIndices indicesStats;
|
||||
String clusterUUID;
|
||||
ClusterHealthStatus status;
|
||||
long timestamp;
|
||||
|
||||
|
||||
@ -52,12 +55,23 @@ public class ClusterStatsResponse extends NodesOperationResponse<ClusterStatsNod
|
||||
this.clusterUUID = clusterUUID;
|
||||
nodesStats = new ClusterStatsNodes(nodes);
|
||||
indicesStats = new ClusterStatsIndices(nodes);
|
||||
for (ClusterStatsNodeResponse response : nodes) {
|
||||
// only the master node populates the status
|
||||
if (response.clusterStatus() != null) {
|
||||
status = response.clusterStatus();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public long getTimestamp() {
|
||||
return this.timestamp;
|
||||
}
|
||||
|
||||
public ClusterHealthStatus getStatus() {
|
||||
return this.status;
|
||||
}
|
||||
|
||||
public ClusterStatsNodes getNodesStats() {
|
||||
return nodesStats;
|
||||
}
|
||||
@ -90,6 +104,11 @@ public class ClusterStatsResponse extends NodesOperationResponse<ClusterStatsNod
|
||||
public void readFrom(StreamInput in) throws IOException {
|
||||
super.readFrom(in);
|
||||
timestamp = in.readVLong();
|
||||
status = null;
|
||||
if (in.readBoolean()) {
|
||||
// it may be that the master switched on us while doing the operation. In this case the status may be null.
|
||||
status = ClusterHealthStatus.fromValue(in.readByte());
|
||||
}
|
||||
clusterUUID = in.readString();
|
||||
nodesStats = ClusterStatsNodes.readNodeStats(in);
|
||||
indicesStats = ClusterStatsIndices.readIndicesStats(in);
|
||||
@ -99,6 +118,12 @@ public class ClusterStatsResponse extends NodesOperationResponse<ClusterStatsNod
|
||||
public void writeTo(StreamOutput out) throws IOException {
|
||||
super.writeTo(out);
|
||||
out.writeVLong(timestamp);
|
||||
if (status == null) {
|
||||
out.writeBoolean(false);
|
||||
} else {
|
||||
out.writeBoolean(true);
|
||||
out.writeByte(status.value());
|
||||
}
|
||||
out.writeString(clusterUUID);
|
||||
nodesStats.writeTo(out);
|
||||
indicesStats.writeTo(out);
|
||||
@ -109,6 +134,7 @@ public class ClusterStatsResponse extends NodesOperationResponse<ClusterStatsNod
|
||||
static final XContentBuilderString INDICES = new XContentBuilderString("indices");
|
||||
static final XContentBuilderString UUID = new XContentBuilderString("uuid");
|
||||
static final XContentBuilderString CLUSTER_NAME = new XContentBuilderString("cluster_name");
|
||||
static final XContentBuilderString STATUS = new XContentBuilderString("status");
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -118,7 +144,9 @@ public class ClusterStatsResponse extends NodesOperationResponse<ClusterStatsNod
|
||||
if (params.paramAsBoolean("output_uuid", false)) {
|
||||
builder.field(Fields.UUID, clusterUUID);
|
||||
}
|
||||
|
||||
if (status != null) {
|
||||
builder.field(Fields.STATUS, status.name().toLowerCase(Locale.ROOT));
|
||||
}
|
||||
builder.startObject(Fields.INDICES);
|
||||
indicesStats.toXContent(builder, params);
|
||||
builder.endObject();
|
||||
|
@ -20,6 +20,8 @@
|
||||
package org.elasticsearch.action.admin.cluster.stats;
|
||||
|
||||
import org.elasticsearch.ElasticSearchException;
|
||||
import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
|
||||
import org.elasticsearch.action.admin.cluster.health.ClusterIndexHealth;
|
||||
import org.elasticsearch.action.admin.cluster.node.info.NodeInfo;
|
||||
import org.elasticsearch.action.admin.cluster.node.stats.NodeStats;
|
||||
import org.elasticsearch.action.admin.indices.stats.CommonStatsFlags;
|
||||
@ -28,6 +30,8 @@ import org.elasticsearch.action.support.nodes.NodeOperationRequest;
|
||||
import org.elasticsearch.action.support.nodes.TransportNodesOperationAction;
|
||||
import org.elasticsearch.cluster.ClusterName;
|
||||
import org.elasticsearch.cluster.ClusterService;
|
||||
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
||||
import org.elasticsearch.cluster.routing.IndexRoutingTable;
|
||||
import org.elasticsearch.common.inject.Inject;
|
||||
import org.elasticsearch.common.io.stream.StreamInput;
|
||||
import org.elasticsearch.common.io.stream.StreamOutput;
|
||||
@ -129,7 +133,31 @@ public class TransportClusterStatsAction extends TransportNodesOperationAction<C
|
||||
|
||||
}
|
||||
|
||||
return new ClusterStatsNodeResponse(nodeInfo.getNode(), nodeInfo, nodeStats, shardsStats.toArray(new ShardStats[shardsStats.size()]));
|
||||
ClusterHealthStatus clusterStatus = null;
|
||||
if (clusterService.state().nodes().localNodeMaster()) {
|
||||
// populate cluster status
|
||||
clusterStatus = ClusterHealthStatus.GREEN;
|
||||
for (IndexRoutingTable indexRoutingTable : clusterService.state().routingTable()) {
|
||||
IndexMetaData indexMetaData = clusterService.state().metaData().index(indexRoutingTable.index());
|
||||
if (indexRoutingTable == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ClusterIndexHealth indexHealth = new ClusterIndexHealth(indexMetaData, indexRoutingTable);
|
||||
switch (indexHealth.getStatus()) {
|
||||
case RED:
|
||||
clusterStatus = ClusterHealthStatus.RED;
|
||||
break;
|
||||
case YELLOW:
|
||||
if (clusterStatus != ClusterHealthStatus.RED) {
|
||||
clusterStatus = ClusterHealthStatus.YELLOW;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new ClusterStatsNodeResponse(nodeInfo.getNode(), clusterStatus, nodeInfo, nodeStats, shardsStats.toArray(new ShardStats[shardsStats.size()]));
|
||||
|
||||
}
|
||||
|
||||
|
@ -20,6 +20,7 @@ package org.elasticsearch.action.admin.cluster.stats;
|
||||
|
||||
|
||||
import org.elasticsearch.Version;
|
||||
import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
|
||||
import org.elasticsearch.common.settings.ImmutableSettings;
|
||||
import org.elasticsearch.monitor.sigar.SigarService;
|
||||
import org.elasticsearch.test.ElasticsearchIntegrationTest;
|
||||
@ -68,9 +69,15 @@ public class ClusterStatsTests extends ElasticsearchIntegrationTest {
|
||||
@Test
|
||||
public void testIndicesShardStats() {
|
||||
cluster().startNode();
|
||||
|
||||
ClusterStatsResponse response = client().admin().cluster().prepareClusterStats().get();
|
||||
assertThat(response.getStatus(), Matchers.equalTo(ClusterHealthStatus.GREEN));
|
||||
|
||||
|
||||
prepareCreate("test1").setSettings("number_of_shards", 2, "number_of_replicas", 1).get();
|
||||
ensureYellow();
|
||||
ClusterStatsResponse response = client().admin().cluster().prepareClusterStats().get();
|
||||
response = client().admin().cluster().prepareClusterStats().get();
|
||||
assertThat(response.getStatus(), Matchers.equalTo(ClusterHealthStatus.YELLOW));
|
||||
assertThat(response.indicesStats.getDocs().getCount(), Matchers.equalTo(0l));
|
||||
assertThat(response.indicesStats.getIndexCount(), Matchers.equalTo(1));
|
||||
assertShardStats(response.getIndicesStats().getShards(), 1, 2, 2, 0.0);
|
||||
@ -81,12 +88,14 @@ public class ClusterStatsTests extends ElasticsearchIntegrationTest {
|
||||
index("test1", "type", "1", "f", "f");
|
||||
refresh(); // make the doc visible
|
||||
response = client().admin().cluster().prepareClusterStats().get();
|
||||
assertThat(response.getStatus(), Matchers.equalTo(ClusterHealthStatus.GREEN));
|
||||
assertThat(response.indicesStats.getDocs().getCount(), Matchers.equalTo(1l));
|
||||
assertShardStats(response.getIndicesStats().getShards(), 1, 4, 2, 1.0);
|
||||
|
||||
prepareCreate("test2").setSettings("number_of_shards", 3, "number_of_replicas", 0).get();
|
||||
ensureGreen();
|
||||
response = client().admin().cluster().prepareClusterStats().get();
|
||||
assertThat(response.getStatus(), Matchers.equalTo(ClusterHealthStatus.GREEN));
|
||||
assertThat(response.indicesStats.getIndexCount(), Matchers.equalTo(2));
|
||||
assertShardStats(response.getIndicesStats().getShards(), 2, 7, 5, 2.0 / 5);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user