Add extra validation into `cluster/stats`

The cluster health and cluster stats disagree on the status.
Add a extra validation step in `cluster/stats`.

closes #7390
This commit is contained in:
xuzha 2015-11-09 13:09:59 -08:00
parent aae60e5b6e
commit f33184329f
2 changed files with 23 additions and 0 deletions

View File

@ -34,6 +34,7 @@ import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.routing.IndexRoutingTable; import org.elasticsearch.cluster.routing.IndexRoutingTable;
import org.elasticsearch.cluster.routing.RoutingTableValidation;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
@ -42,6 +43,7 @@ import org.elasticsearch.index.IndexService;
import org.elasticsearch.index.shard.IndexShard; import org.elasticsearch.index.shard.IndexShard;
import org.elasticsearch.indices.IndicesService; import org.elasticsearch.indices.IndicesService;
import org.elasticsearch.node.service.NodeService; import org.elasticsearch.node.service.NodeService;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService; import org.elasticsearch.transport.TransportService;
@ -134,6 +136,14 @@ public class TransportClusterStatsAction extends TransportNodesAction<ClusterSta
break; break;
} }
} }
RoutingTableValidation validation = clusterService.state().routingTable().validate(clusterService.state().metaData());
if (!validation.failures().isEmpty()) {
clusterStatus = ClusterHealthStatus.RED;
} else if (clusterService.state().blocks().hasGlobalBlock(RestStatus.SERVICE_UNAVAILABLE)) {
clusterStatus = ClusterHealthStatus.RED;
}
} }
return new ClusterStatsNodeResponse(nodeInfo.getNode(), clusterStatus, nodeInfo, nodeStats, shardsStats.toArray(new ShardStats[shardsStats.size()])); return new ClusterStatsNodeResponse(nodeInfo.getNode(), clusterStatus, nodeInfo, nodeStats, shardsStats.toArray(new ShardStats[shardsStats.size()]));

View File

@ -170,4 +170,17 @@ public class ClusterStatsIT extends ESIntegTestCase {
ClusterStatsResponse response = client().admin().cluster().prepareClusterStats().get(); ClusterStatsResponse response = client().admin().cluster().prepareClusterStats().get();
assertThat(response.getNodesStats().getOs().getAllocatedProcessors(), equalTo(7)); assertThat(response.getNodesStats().getOs().getAllocatedProcessors(), equalTo(7));
} }
public void testClusterStatus() throws Exception {
// stop all other nodes
internalCluster().ensureAtMostNumDataNodes(0);
internalCluster().startNode(Settings.builder().put("gateway.recover_after_nodes", 2).build());
ClusterStatsResponse response = client().admin().cluster().prepareClusterStats().get();
assertThat(response.getStatus(), equalTo(ClusterHealthStatus.RED));
internalCluster().ensureAtLeastNumDataNodes(3);
response = client().admin().cluster().prepareClusterStats().get();
assertThat(response.getStatus(), equalTo(ClusterHealthStatus.GREEN));
}
} }