diff --git a/exporter/src/main/java/org/elasticsearch/marvel/monitor/ExportersService.java b/exporter/src/main/java/org/elasticsearch/marvel/monitor/ExportersService.java index 82bccb1065b5..c671d0a0767e 100644 --- a/exporter/src/main/java/org/elasticsearch/marvel/monitor/ExportersService.java +++ b/exporter/src/main/java/org/elasticsearch/marvel/monitor/ExportersService.java @@ -32,9 +32,12 @@ import org.elasticsearch.client.Client; import org.elasticsearch.cluster.ClusterChangedEvent; import org.elasticsearch.cluster.ClusterName; import org.elasticsearch.cluster.ClusterService; +import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.block.ClusterBlock; import org.elasticsearch.cluster.node.DiscoveryNode; -import org.elasticsearch.cluster.routing.*; +import org.elasticsearch.cluster.routing.IndexRoutingTable; +import org.elasticsearch.cluster.routing.IndexShardRoutingTable; +import org.elasticsearch.cluster.routing.ShardRouting; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.collect.ImmutableSet; @@ -332,6 +335,41 @@ public class ExportersService extends AbstractLifecycleComponent getAssignedShardRoutingDelta(ClusterState previousState, ClusterState currentState) { + List changedShards = new ArrayList(); + + for (IndexRoutingTable indexRoutingTable : currentState.routingTable()) { + IndexRoutingTable prevIndexRoutingTable = previousState.routingTable().getIndicesRouting().get(indexRoutingTable.index()); + if (prevIndexRoutingTable == null) { + for (IndexShardRoutingTable shardRoutingTable : indexRoutingTable) { + for (ShardRouting shardRouting : shardRoutingTable.assignedShards()) { + changedShards.add(new ShardRoutingDelta(null, shardRouting)); + } + } + continue; + } + for (IndexShardRoutingTable shardRoutingTable : indexRoutingTable) { + IndexShardRoutingTable prevShardRoutingTable = prevIndexRoutingTable.shard(shardRoutingTable.shardId().id()); + if (prevShardRoutingTable == null) { + for (ShardRouting shardRouting : shardRoutingTable.assignedShards()) { + changedShards.add(new ShardRoutingDelta(null, shardRouting)); + } + continue; + } + for (ShardRouting shardRouting : shardRoutingTable.getAssignedShards()) { + + ShardRouting prevShardRouting = null; + for (ShardRouting candidate : prevShardRoutingTable.assignedShards()) { + if (candidate.currentNodeId().equals(shardRouting.currentNodeId())) { + prevShardRouting = candidate; + break; + } else if (shardRouting.currentNodeId().equals(candidate.relocatingNodeId())) { + // the shard relocated here + prevShardRouting = candidate; + break; } } - if (!changed) { - continue; // no event. - } - - if (shardRouting.relocatingNodeId() != null) { - // if relocating node is not null, this shard is initializing due to a relocation - ShardRouting tmpShardRouting = new MutableShardRouting( - shardRouting.index(), shardRouting.id(), shardRouting.relocatingNodeId(), - shardRouting.currentNodeId(), shardRouting.primary(), - ShardRoutingState.RELOCATING, shardRouting.version()); - DiscoveryNode relocatingTo = null; - if (tmpShardRouting.relocatingNodeId() != null) { - relocatingTo = event.state().nodes().get(tmpShardRouting.relocatingNodeId()); - } - pendingEventsQueue.add(new RoutingEvent.ShardRelocating(timestamp, clusterName, tmpShardRouting, - relocatingTo, event.state().nodes().get(tmpShardRouting.currentNodeId()) - )); - } else { - pendingEventsQueue.add(new RoutingEvent.ShardInitializing(timestamp, clusterName, shardRouting, - event.state().nodes().get(shardRouting.currentNodeId()) - )); + if (prevShardRouting != null && prevShardRouting.equals(shardRouting)) { + continue; // nothing changed. } + changedShards.add(new ShardRoutingDelta(prevShardRouting, shardRouting)); } } } + return changedShards; } - class IndicesLifeCycleListener extends IndicesLifecycle.Listener { @Override diff --git a/exporter/src/main/java/org/elasticsearch/marvel/monitor/event/RoutingEvent.java b/exporter/src/main/java/org/elasticsearch/marvel/monitor/event/RoutingEvent.java index 468959a309b0..0296073fba94 100644 --- a/exporter/src/main/java/org/elasticsearch/marvel/monitor/event/RoutingEvent.java +++ b/exporter/src/main/java/org/elasticsearch/marvel/monitor/event/RoutingEvent.java @@ -89,7 +89,41 @@ public abstract class RoutingEvent extends Event { @Override String conciseDescription() { - return shardDescription(shardRouting) + " set to initializing on " + Utils.nodeDescription(node); + return shardDescription(shardRouting) + " initializing on " + Utils.nodeDescription(node); + } + } + + public static class ShardStarted extends RoutingShardEvent { + + public ShardStarted(long timestamp, String clusterName, ShardRouting shardRouting, DiscoveryNode node) { + super(timestamp, clusterName, shardRouting, node); + } + + @Override + public String event() { + return "shard_started"; + } + + @Override + String conciseDescription() { + return shardDescription(shardRouting) + " started on " + Utils.nodeDescription(node); + } + } + + public static class ShardPromotedToPrimary extends RoutingShardEvent { + + public ShardPromotedToPrimary(long timestamp, String clusterName, ShardRouting shardRouting, DiscoveryNode node) { + super(timestamp, clusterName, shardRouting, node); + } + + @Override + public String event() { + return "shard_promoted"; + } + + @Override + String conciseDescription() { + return shardRouting.shardId() + " promoted to primary on " + Utils.nodeDescription(node); } } @@ -110,11 +144,8 @@ public abstract class RoutingEvent extends Event { @Override String conciseDescription() { - String s = shardDescription(shardRouting) + " set to relocate"; - if (relocatingTo != null) { - s += " to " + relocatingTo; - } - return s + " from " + Utils.nodeDescription(node); + return shardDescription(shardRouting) + " relocating to " + Utils.nodeDescription(relocatingTo) + + " from " + Utils.nodeDescription(node); } @Override