Skip to content

Commit

Permalink
A new _shard_stores API provides store information for shard copies…
Browse files Browse the repository at this point in the history
… of indices.

Store information reports on which nodes shard copies exist, the shard
copy version, indicating how recent they are, and any exceptions
encountered while opening the shard index or from earlier engine failure.

closes #10952
  • Loading branch information
areek committed Jul 16, 2015
1 parent d902012 commit 7a21d84
Show file tree
Hide file tree
Showing 32 changed files with 1,703 additions and 119 deletions.
3 changes: 3 additions & 0 deletions core/src/main/java/org/elasticsearch/action/ActionModule.java
Expand Up @@ -96,6 +96,8 @@
import org.elasticsearch.action.admin.indices.recovery.TransportRecoveryAction;
import org.elasticsearch.action.admin.indices.refresh.RefreshAction;
import org.elasticsearch.action.admin.indices.refresh.TransportRefreshAction;
import org.elasticsearch.action.admin.indices.shards.IndicesShardStoresAction;
import org.elasticsearch.action.admin.indices.shards.TransportIndicesShardStoresAction;
import org.elasticsearch.action.admin.indices.segments.IndicesSegmentsAction;
import org.elasticsearch.action.admin.indices.segments.TransportIndicesSegmentsAction;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsAction;
Expand Down Expand Up @@ -242,6 +244,7 @@ protected void configure() {

registerAction(IndicesStatsAction.INSTANCE, TransportIndicesStatsAction.class);
registerAction(IndicesSegmentsAction.INSTANCE, TransportIndicesSegmentsAction.class);
registerAction(IndicesShardStoresAction.INSTANCE, TransportIndicesShardStoresAction.class);
registerAction(CreateIndexAction.INSTANCE, TransportCreateIndexAction.class);
registerAction(DeleteIndexAction.INSTANCE, TransportDeleteIndexAction.class);
registerAction(GetIndexAction.INSTANCE, TransportGetIndexAction.class);
Expand Down
Expand Up @@ -107,11 +107,11 @@ public ClusterHealthResponse(String clusterName, String[] concreteIndices, Clust
status = ClusterHealthStatus.GREEN;

for (ClusterIndexHealth indexHealth : indices.values()) {
activePrimaryShards += indexHealth.activePrimaryShards;
activeShards += indexHealth.activeShards;
relocatingShards += indexHealth.relocatingShards;
initializingShards += indexHealth.initializingShards;
unassignedShards += indexHealth.unassignedShards;
activePrimaryShards += indexHealth.getActivePrimaryShards();
activeShards += indexHealth.getActiveShards();
relocatingShards += indexHealth.getRelocatingShards();
initializingShards += indexHealth.getInitializingShards();
unassignedShards += indexHealth.getUnassignedShards();
if (indexHealth.getStatus() == ClusterHealthStatus.RED) {
status = ClusterHealthStatus.RED;
} else if (indexHealth.getStatus() == ClusterHealthStatus.YELLOW && status != ClusterHealthStatus.RED) {
Expand Down
Expand Up @@ -50,4 +50,16 @@ public static ClusterHealthStatus fromValue(byte value) {
throw new IllegalArgumentException("No cluster health status for value [" + value + "]");
}
}

public static ClusterHealthStatus fromString(String status) {
if (status.equalsIgnoreCase("green")) {
return GREEN;
} else if (status.equalsIgnoreCase("yellow")) {
return YELLOW;
} else if (status.equalsIgnoreCase("red")) {
return RED;
} else {
throw new IllegalArgumentException("unknown cluster health status [" + status + "]");
}
}
}
Expand Up @@ -51,21 +51,21 @@ public class ClusterIndexHealth implements Iterable<ClusterShardHealth>, Streama

private int numberOfReplicas;

int activeShards = 0;
private int activeShards = 0;

int relocatingShards = 0;
private int relocatingShards = 0;

int initializingShards = 0;
private int initializingShards = 0;

int unassignedShards = 0;
private int unassignedShards = 0;

int activePrimaryShards = 0;
private int activePrimaryShards = 0;

ClusterHealthStatus status = ClusterHealthStatus.RED;
private ClusterHealthStatus status = ClusterHealthStatus.RED;

final Map<Integer, ClusterShardHealth> shards = Maps.newHashMap();
private final Map<Integer, ClusterShardHealth> shards = Maps.newHashMap();

List<String> validationFailures;
private List<String> validationFailures;

private ClusterIndexHealth() {
}
Expand All @@ -77,33 +77,8 @@ public ClusterIndexHealth(IndexMetaData indexMetaData, IndexRoutingTable indexRo
this.validationFailures = indexRoutingTable.validate(indexMetaData);

for (IndexShardRoutingTable shardRoutingTable : indexRoutingTable) {
ClusterShardHealth shardHealth = new ClusterShardHealth(shardRoutingTable.shardId().id());
for (ShardRouting shardRouting : shardRoutingTable) {
if (shardRouting.active()) {
shardHealth.activeShards++;
if (shardRouting.relocating()) {
// the shard is relocating, the one it is relocating to will be in initializing state, so we don't count it
shardHealth.relocatingShards++;
}
if (shardRouting.primary()) {
shardHealth.primaryActive = true;
}
} else if (shardRouting.initializing()) {
shardHealth.initializingShards++;
} else if (shardRouting.unassigned()) {
shardHealth.unassignedShards++;
}
}
if (shardHealth.primaryActive) {
if (shardHealth.activeShards == shardRoutingTable.size()) {
shardHealth.status = ClusterHealthStatus.GREEN;
} else {
shardHealth.status = ClusterHealthStatus.YELLOW;
}
} else {
shardHealth.status = ClusterHealthStatus.RED;
}
shards.put(shardHealth.getId(), shardHealth);
int shardId = shardRoutingTable.shardId().id();
shards.put(shardId, new ClusterShardHealth(shardId, shardRoutingTable));
}

// update the index status
Expand All @@ -113,10 +88,10 @@ public ClusterIndexHealth(IndexMetaData indexMetaData, IndexRoutingTable indexRo
if (shardHealth.isPrimaryActive()) {
activePrimaryShards++;
}
activeShards += shardHealth.activeShards;
relocatingShards += shardHealth.relocatingShards;
initializingShards += shardHealth.initializingShards;
unassignedShards += shardHealth.unassignedShards;
activeShards += shardHealth.getActiveShards();
relocatingShards += shardHealth.getRelocatingShards();
initializingShards += shardHealth.getInitializingShards();
unassignedShards += shardHealth.getUnassignedShards();

if (shardHealth.getStatus() == ClusterHealthStatus.RED) {
status = ClusterHealthStatus.RED;
Expand Down
Expand Up @@ -19,6 +19,8 @@

package org.elasticsearch.action.admin.cluster.health;

import org.elasticsearch.cluster.routing.IndexShardRoutingTable;
import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;
Expand All @@ -34,22 +36,47 @@ public class ClusterShardHealth implements Streamable {

ClusterHealthStatus status = ClusterHealthStatus.RED;

int activeShards = 0;
private int activeShards = 0;

int relocatingShards = 0;
private int relocatingShards = 0;

int initializingShards = 0;
private int initializingShards = 0;

int unassignedShards = 0;
private int unassignedShards = 0;

boolean primaryActive = false;
private boolean primaryActive = false;

private ClusterShardHealth() {

}

ClusterShardHealth(int shardId) {
public ClusterShardHealth(int shardId, final IndexShardRoutingTable shardRoutingTable) {
this.shardId = shardId;
for (ShardRouting shardRouting : shardRoutingTable) {
if (shardRouting.active()) {
activeShards++;
if (shardRouting.relocating()) {
// the shard is relocating, the one it is relocating to will be in initializing state, so we don't count it
relocatingShards++;
}
if (shardRouting.primary()) {
primaryActive = true;
}
} else if (shardRouting.initializing()) {
initializingShards++;
} else if (shardRouting.unassigned()) {
unassignedShards++;
}
}
if (primaryActive) {
if (activeShards == shardRoutingTable.size()) {
status = ClusterHealthStatus.GREEN;
} else {
status = ClusterHealthStatus.YELLOW;
}
} else {
status = ClusterHealthStatus.RED;
}
}

public int getId() {
Expand Down
Expand Up @@ -84,17 +84,7 @@ public void writeTo(StreamOutput out) throws IOException {
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject("nodes");
for (DiscoveryNode node : nodes) {
builder.startObject(node.getId(), XContentBuilder.FieldCaseConversion.NONE);
builder.field("name", node.name());
builder.field("transport_address", node.getAddress());
if (!node.attributes().isEmpty()) {
builder.startObject("attributes");
for (Map.Entry<String, String> attr : node.attributes().entrySet()) {
builder.field(attr.getKey(), attr.getValue());
}
builder.endObject();
}
builder.endObject();
node.toXContent(builder, params);
}
builder.endObject();
builder.startArray("shards");
Expand Down
@@ -0,0 +1,62 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch 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.elasticsearch.action.admin.indices.shards;

import org.elasticsearch.action.Action;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.master.MasterNodeReadOperationRequestBuilder;
import org.elasticsearch.client.ElasticsearchClient;

/**
* Request builder for {@link IndicesShardStoresRequest}
*/
public class IndicesShardStoreRequestBuilder extends MasterNodeReadOperationRequestBuilder<IndicesShardStoresRequest, IndicesShardStoresResponse, IndicesShardStoreRequestBuilder> {

public IndicesShardStoreRequestBuilder(ElasticsearchClient client, Action<IndicesShardStoresRequest, IndicesShardStoresResponse, IndicesShardStoreRequestBuilder> action, String... indices) {
super(client, action, new IndicesShardStoresRequest(indices));
}

/**
* Sets the indices for the shard stores request
*/
public IndicesShardStoreRequestBuilder setIndices(String... indices) {
request.indices(indices);
return this;
}

/**
* Specifies what type of requested indices to ignore and wildcard indices expressions
* By default, expands wildcards to both open and closed indices
*/
public IndicesShardStoreRequestBuilder setIndicesOptions(IndicesOptions indicesOptions) {
request.indicesOptions(indicesOptions);
return this;
}

/**
* Set statuses to filter shards to get stores info on.
* @param shardStatuses acceptable values are "green", "yellow", "red" and "all"
* see {@link org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus} for details
*/
public IndicesShardStoreRequestBuilder setShardStatuses(String... shardStatuses) {
request.shardStatuses(shardStatuses);
return this;
}
}
@@ -0,0 +1,50 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch 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.elasticsearch.action.admin.indices.shards;

import org.elasticsearch.action.Action;
import org.elasticsearch.client.ElasticsearchClient;

/**
* Action for {@link TransportIndicesShardStoresAction}
*
* Exposes shard store information for requested indices.
* Shard store information reports which nodes hold shard copies, how recent they are
* and any exceptions on opening the shard index or from previous engine failures
*/
public class IndicesShardStoresAction extends Action<IndicesShardStoresRequest, IndicesShardStoresResponse, IndicesShardStoreRequestBuilder> {

public static final IndicesShardStoresAction INSTANCE = new IndicesShardStoresAction();
public static final String NAME = "indices:monitor/shard_stores";

private IndicesShardStoresAction() {
super(NAME);
}

@Override
public IndicesShardStoresResponse newResponse() {
return new IndicesShardStoresResponse();
}

@Override
public IndicesShardStoreRequestBuilder newRequestBuilder(ElasticsearchClient client) {
return new IndicesShardStoreRequestBuilder(client, this);
}
}

0 comments on commit 7a21d84

Please sign in to comment.