diff --git a/src/main/java/org/elasticsearch/action/admin/cluster/stats/TransportClusterStatsAction.java b/src/main/java/org/elasticsearch/action/admin/cluster/stats/TransportClusterStatsAction.java index e89259ad8ce..9cb038b3623 100644 --- a/src/main/java/org/elasticsearch/action/admin/cluster/stats/TransportClusterStatsAction.java +++ b/src/main/java/org/elasticsearch/action/admin/cluster/stats/TransportClusterStatsAction.java @@ -117,12 +117,11 @@ public class TransportClusterStatsAction extends TransportNodesOperationAction shardsStats = new ArrayList<>(); for (IndexService indexService : indicesService.indices().values()) { for (IndexShard indexShard : indexService) { - if (indexShard.routingEntry().active()) { + if (indexShard.routingEntry() != null && indexShard.routingEntry().active()) { // only report on fully started shards - shardsStats.add(new ShardStats(indexShard, SHARD_STATS_FLAGS)); + shardsStats.add(new ShardStats(indexShard, indexShard.routingEntry(), SHARD_STATS_FLAGS)); } } - } ClusterHealthStatus clusterStatus = null; diff --git a/src/main/java/org/elasticsearch/action/admin/indices/stats/ShardStats.java b/src/main/java/org/elasticsearch/action/admin/indices/stats/ShardStats.java index 04494c1929f..73618125bc4 100644 --- a/src/main/java/org/elasticsearch/action/admin/indices/stats/ShardStats.java +++ b/src/main/java/org/elasticsearch/action/admin/indices/stats/ShardStats.java @@ -43,9 +43,9 @@ public class ShardStats extends BroadcastShardOperationResponse implements ToXCo ShardStats() { } - public ShardStats(IndexShard indexShard, CommonStatsFlags flags) { - super(indexShard.routingEntry().shardId()); - this.shardRouting = indexShard.routingEntry(); + public ShardStats(IndexShard indexShard, ShardRouting shardRouting, CommonStatsFlags flags) { + super(indexShard.shardId()); + this.shardRouting = shardRouting; this.stats = new CommonStats(indexShard, flags); } diff --git a/src/main/java/org/elasticsearch/action/admin/indices/stats/TransportIndicesStatsAction.java b/src/main/java/org/elasticsearch/action/admin/indices/stats/TransportIndicesStatsAction.java index eacc122281e..eb6dd735df1 100644 --- a/src/main/java/org/elasticsearch/action/admin/indices/stats/TransportIndicesStatsAction.java +++ b/src/main/java/org/elasticsearch/action/admin/indices/stats/TransportIndicesStatsAction.java @@ -37,6 +37,7 @@ import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.index.IndexShardMissingException; import org.elasticsearch.index.service.InternalIndexService; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.index.shard.service.InternalIndexShard; @@ -135,6 +136,10 @@ public class TransportIndicesStatsAction extends TransportBroadcastOperationActi protected ShardStats shardOperation(IndexShardStatsRequest request) throws ElasticsearchException { InternalIndexService indexService = (InternalIndexService) indicesService.indexServiceSafe(request.shardId().getIndex()); InternalIndexShard indexShard = (InternalIndexShard) indexService.shardSafe(request.shardId().id()); + // if we don't have the routing entry yet, we need it stats wise, we treat it as if the shard is not ready yet + if (indexShard.routingEntry() == null) { + throw new IndexShardMissingException(indexShard.shardId()); + } CommonStatsFlags flags = new CommonStatsFlags().clear(); @@ -197,7 +202,7 @@ public class TransportIndicesStatsAction extends TransportBroadcastOperationActi flags.set(CommonStatsFlags.Flag.QueryCache); } - return new ShardStats(indexShard, flags); + return new ShardStats(indexShard, indexShard.routingEntry(), flags); } static class IndexShardStatsRequest extends BroadcastShardOperationRequest { diff --git a/src/main/java/org/elasticsearch/index/shard/service/IndexShard.java b/src/main/java/org/elasticsearch/index/shard/service/IndexShard.java index a0d35430ee2..ccea34022cf 100644 --- a/src/main/java/org/elasticsearch/index/shard/service/IndexShard.java +++ b/src/main/java/org/elasticsearch/index/shard/service/IndexShard.java @@ -82,6 +82,11 @@ public interface IndexShard extends IndexShardComponent { ShardFieldData fieldData(); + /** + * Returns the latest cluster routing entry received with this shard. Might be null if the + * shard was just created. + */ + @Nullable ShardRouting routingEntry(); DocsStats docStats(); diff --git a/src/main/java/org/elasticsearch/indices/InternalIndicesService.java b/src/main/java/org/elasticsearch/indices/InternalIndicesService.java index dd7f765dce8..45b78cfabad 100644 --- a/src/main/java/org/elasticsearch/indices/InternalIndicesService.java +++ b/src/main/java/org/elasticsearch/indices/InternalIndicesService.java @@ -206,7 +206,10 @@ public class InternalIndicesService extends AbstractLifecycleComponentnewArrayList(indexShardStats)); } else {