Integrated new shard state changed listener method to collect more information for each shard state change

This commit is contained in:
Luca Cavanna 2013-12-13 19:23:01 +01:00 committed by Boaz Leskes
parent 89848397b1
commit 9c0ecc708e
2 changed files with 65 additions and 52 deletions

View file

@ -42,7 +42,7 @@ import org.elasticsearch.common.util.concurrent.ConcurrentCollections;
import org.elasticsearch.common.util.concurrent.EsExecutors;
import org.elasticsearch.discovery.Discovery;
import org.elasticsearch.index.service.IndexService;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.index.shard.IndexShardState;
import org.elasticsearch.index.shard.service.IndexShard;
import org.elasticsearch.indices.IndicesLifecycle;
import org.elasticsearch.indices.IndicesService;
@ -356,38 +356,23 @@ public class ExportersService extends AbstractLifecycleComponent<ExportersServic
}
}
}
class IndicesLifeCycleListener extends IndicesLifecycle.Listener {
@Override
public void afterIndexShardStarted(IndexShard indexShard) {
DiscoveryNode relocatedFrom = null;
if (indexShard.routingEntry().relocatingNodeId() != null) {
relocatedFrom = clusterService.state().nodes().get(indexShard.routingEntry().relocatingNodeId());
public void indexShardStateChanged(IndexShard indexShard, @Nullable IndexShardState previousState, IndexShardState newState, @Nullable String reason) {
DiscoveryNode relocatingNode = null;
if (indexShard.routingEntry() != null) {
if (indexShard.routingEntry().relocatingNodeId() != null) {
relocatingNode = clusterService.state().nodes().get(indexShard.routingEntry().relocatingNodeId());
}
}
pendingEventsQueue.add(new ShardEvent(System.currentTimeMillis(), ShardEvent.EventType.STARTED,
indexShard.shardId(), clusterService.localNode(), relocatedFrom, indexShard.routingEntry()));
}
@Override
public void beforeIndexShardCreated(ShardId shardId) {
pendingEventsQueue.add(new ShardEvent(System.currentTimeMillis(), ShardEvent.EventType.CREATED,
shardId, clusterService.localNode(), null, null));
}
@Override
public void beforeIndexShardClosed(ShardId shardId, @Nullable IndexShard indexShard) {
DiscoveryNode relocatedTo = null;
if (indexShard != null && indexShard.routingEntry().relocating()) {
relocatedTo = clusterService.state().nodes().get(indexShard.routingEntry().relocatingNodeId());
}
pendingEventsQueue.add(new ShardEvent(System.currentTimeMillis(), ShardEvent.EventType.CLOSED,
shardId, clusterService.localNode(), relocatedTo, indexShard != null ? indexShard.routingEntry() : null));
pendingEventsQueue.add(new ShardEvent(System.currentTimeMillis(), newState,
indexShard.shardId(), clusterService.localNode(), relocatingNode, indexShard.routingEntry(), reason));
}
}
}

View file

@ -24,6 +24,7 @@ import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.shard.IndexShardState;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.marvel.monitor.Utils;
@ -34,25 +35,20 @@ public class ShardEvent extends Event {
private final ShardRouting shardRouting;
private final ShardId shardId;
private final String reason;
private final DiscoveryNode node;
private final DiscoveryNode relocatingNode; // either relocating from or relocating to (depending on event)
private EventType event;
public enum EventType {
CREATED,
STARTED,
CLOSED
}
private IndexShardState shardState;
public ShardEvent(long timestamp, EventType event, ShardId shardId, DiscoveryNode node,
DiscoveryNode reloactingNode,
ShardRouting shardRouting) {
public ShardEvent(long timestamp, IndexShardState shardState, ShardId shardId, DiscoveryNode node,
DiscoveryNode relocatingNode, ShardRouting shardRouting, String reason) {
super(timestamp);
this.event = event;
this.shardState = shardState;
this.shardId = shardId;
this.reason = reason;
this.node = node;
this.relocatingNode = reloactingNode;
this.relocatingNode = relocatingNode;
this.shardRouting = shardRouting;
}
@ -63,31 +59,30 @@ public class ShardEvent extends Event {
@Override
String conciseDescription() {
switch (event) {
switch (shardState) {
case CREATED:
// no shard routing
return shardId + " created on" + node;
return new DescriptionBuilder(shardId, "created", node).build();
case RECOVERING:
return new DescriptionBuilder(shardId, "entered recovery", node).relocatedFrom(relocatingNode).build();
case POST_RECOVERY:
return new DescriptionBuilder(shardId, "entered post_recovery", node).relocatedFrom(relocatingNode).build();
case STARTED:
if (relocatingNode != null) {
return shardId + " started on " + node + ", relocated from " + relocatingNode;
} else {
return shardId + " started on " + node;
}
return new DescriptionBuilder(shardId, "started", node).relocatedFrom(relocatingNode).build();
case RELOCATED:
return new DescriptionBuilder(shardId, "relocated", node).relocatedTo(relocatingNode).build();
case CLOSED:
if (relocatingNode != null) {
return shardId + " closed on " + node + ", relocated to " + relocatingNode;
} else {
return shardId + " closed on " + node;
}
return new DescriptionBuilder(shardId, "closed", node).relocatedTo(relocatingNode).build();
default:
throw new ElasticSearchException("unmapped event type [" + event + "]");
throw new ElasticSearchException("unmapped shard event type [" + shardState + "]");
}
}
@Override
public XContentBuilder addXContentBody(XContentBuilder builder, ToXContent.Params params) throws IOException {
super.addXContentBody(builder, params);
builder.field("event", event.toString().toLowerCase(Locale.ROOT));
builder.field("event", shardState.name().toLowerCase(Locale.ROOT));
builder.field("reason", reason);
builder.field("index", shardId.index());
builder.field("shard_id", shardId.id());
builder.startObject("node");
@ -98,10 +93,43 @@ public class ShardEvent extends Event {
shardRouting.toXContent(builder, params);
}
if (relocatingNode != null) {
builder.startObject(event == EventType.STARTED ? "relocated_from" : "relocated_to");
if (shardState == IndexShardState.RELOCATED || shardState == IndexShardState.CLOSED) {
builder.startObject("relocated_to");
} else {
builder.startObject("relocated_from");
}
Utils.nodeToXContent(relocatingNode, builder);
builder.endObject();
}
return builder;
}
private static class DescriptionBuilder {
private final StringBuilder description = new StringBuilder();
DescriptionBuilder (ShardId shardId, String changeDescription, DiscoveryNode node) {
description.append(shardId).append(' ').append(changeDescription).append(" on ").append(node);
}
DescriptionBuilder relocatedFrom(DiscoveryNode node) {
if (node != null) {
description.append(", relocated from ").append(node);
}
return this;
}
DescriptionBuilder relocatedTo(DiscoveryNode node) {
if (node != null) {
description.append(", relocated to ").append(node);
}
return this;
}
String build() {
String description = this.description.toString();
this.description.setLength(0);
return description;
}
}
}