use of org.elasticsearch.cluster.routing.ShardRouting in project elasticsearch by elastic.
the class ThrottlingAllocationDecider method canAllocate.
@Override
public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
if (shardRouting.primary() && shardRouting.unassigned()) {
assert initializingShard(shardRouting, node.nodeId()).recoverySource().getType() != RecoverySource.Type.PEER;
// primary is unassigned, means we are going to do recovery from store, snapshot or local shards
// count *just the primaries* currently doing recovery on the node and check against primariesInitialRecoveries
int primariesInRecovery = 0;
for (ShardRouting shard : node) {
// we only count initial recoveries here, so we need to make sure that relocating node is null
if (shard.initializing() && shard.primary() && shard.relocatingNodeId() == null) {
primariesInRecovery++;
}
}
if (primariesInRecovery >= primariesInitialRecoveries) {
// TODO: Should index creation not be throttled for primary shards?
return allocation.decision(THROTTLE, NAME, "reached the limit of ongoing initial primary recoveries [%d], cluster setting [%s=%d]", primariesInRecovery, CLUSTER_ROUTING_ALLOCATION_NODE_INITIAL_PRIMARIES_RECOVERIES_SETTING.getKey(), primariesInitialRecoveries);
} else {
return allocation.decision(YES, NAME, "below primary recovery limit of [%d]", primariesInitialRecoveries);
}
} else {
// Peer recovery
assert initializingShard(shardRouting, node.nodeId()).recoverySource().getType() == RecoverySource.Type.PEER;
// Allocating a shard to this node will increase the incoming recoveries
int currentInRecoveries = allocation.routingNodes().getIncomingRecoveries(node.nodeId());
if (currentInRecoveries >= concurrentIncomingRecoveries) {
return allocation.decision(THROTTLE, NAME, "reached the limit of incoming shard recoveries [%d], cluster setting [%s=%d] (can also be set via [%s])", currentInRecoveries, CLUSTER_ROUTING_ALLOCATION_NODE_CONCURRENT_INCOMING_RECOVERIES_SETTING.getKey(), concurrentIncomingRecoveries, CLUSTER_ROUTING_ALLOCATION_NODE_CONCURRENT_RECOVERIES_SETTING.getKey());
} else {
// search for corresponding recovery source (= primary shard) and check number of outgoing recoveries on that node
ShardRouting primaryShard = allocation.routingNodes().activePrimary(shardRouting.shardId());
if (primaryShard == null) {
return allocation.decision(Decision.NO, NAME, "primary shard for this replica is not yet active");
}
int primaryNodeOutRecoveries = allocation.routingNodes().getOutgoingRecoveries(primaryShard.currentNodeId());
if (primaryNodeOutRecoveries >= concurrentOutgoingRecoveries) {
return allocation.decision(THROTTLE, NAME, "reached the limit of outgoing shard recoveries [%d] on the node [%s] which holds the primary, " + "cluster setting [%s=%d] (can also be set via [%s])", primaryNodeOutRecoveries, node.nodeId(), CLUSTER_ROUTING_ALLOCATION_NODE_CONCURRENT_OUTGOING_RECOVERIES_SETTING.getKey(), concurrentOutgoingRecoveries, CLUSTER_ROUTING_ALLOCATION_NODE_CONCURRENT_RECOVERIES_SETTING.getKey());
} else {
return allocation.decision(YES, NAME, "below shard recovery limit of outgoing: [%d < %d] incoming: [%d < %d]", primaryNodeOutRecoveries, concurrentOutgoingRecoveries, currentInRecoveries, concurrentIncomingRecoveries);
}
}
}
}
use of org.elasticsearch.cluster.routing.ShardRouting in project elasticsearch by elastic.
the class CancelAllocationCommand method execute.
@Override
public RerouteExplanation execute(RoutingAllocation allocation, boolean explain) {
DiscoveryNode discoNode = allocation.nodes().resolveNode(node);
ShardRouting shardRouting = null;
RoutingNodes routingNodes = allocation.routingNodes();
RoutingNode routingNode = routingNodes.node(discoNode.getId());
IndexMetaData indexMetaData = null;
if (routingNode != null) {
indexMetaData = allocation.metaData().index(index());
if (indexMetaData == null) {
throw new IndexNotFoundException(index());
}
ShardId shardId = new ShardId(indexMetaData.getIndex(), shardId());
shardRouting = routingNode.getByShardId(shardId);
}
if (shardRouting == null) {
if (explain) {
return new RerouteExplanation(this, allocation.decision(Decision.NO, "cancel_allocation_command", "can't cancel " + shardId + ", failed to find it on node " + discoNode));
}
throw new IllegalArgumentException("[cancel_allocation] can't cancel " + shardId + ", failed to find it on node " + discoNode);
}
if (shardRouting.primary() && allowPrimary == false) {
if ((shardRouting.initializing() && shardRouting.relocatingNodeId() != null) == false) {
// only allow cancelling initializing shard of primary relocation without allowPrimary flag
if (explain) {
return new RerouteExplanation(this, allocation.decision(Decision.NO, "cancel_allocation_command", "can't cancel " + shardId + " on node " + discoNode + ", shard is primary and " + shardRouting.state().name().toLowerCase(Locale.ROOT)));
}
throw new IllegalArgumentException("[cancel_allocation] can't cancel " + shardId + " on node " + discoNode + ", shard is primary and " + shardRouting.state().name().toLowerCase(Locale.ROOT));
}
}
routingNodes.failShard(Loggers.getLogger(CancelAllocationCommand.class), shardRouting, new UnassignedInfo(UnassignedInfo.Reason.REROUTE_CANCELLED, null), indexMetaData, allocation.changes());
return new RerouteExplanation(this, allocation.decision(Decision.YES, "cancel_allocation_command", "shard " + shardId + " on node " + discoNode + " can be cancelled"));
}
use of org.elasticsearch.cluster.routing.ShardRouting in project elasticsearch by elastic.
the class MoveAllocationCommand method execute.
@Override
public RerouteExplanation execute(RoutingAllocation allocation, boolean explain) {
DiscoveryNode fromDiscoNode = allocation.nodes().resolveNode(fromNode);
DiscoveryNode toDiscoNode = allocation.nodes().resolveNode(toNode);
Decision decision = null;
boolean found = false;
for (ShardRouting shardRouting : allocation.routingNodes().node(fromDiscoNode.getId())) {
if (!shardRouting.shardId().getIndexName().equals(index)) {
continue;
}
if (shardRouting.shardId().id() != shardId) {
continue;
}
found = true;
// TODO we can possibly support also relocating cases, where we cancel relocation and move...
if (!shardRouting.started()) {
if (explain) {
return new RerouteExplanation(this, allocation.decision(Decision.NO, "move_allocation_command", "shard " + shardId + " has not been started"));
}
throw new IllegalArgumentException("[move_allocation] can't move " + shardId + ", shard is not started (state = " + shardRouting.state() + "]");
}
RoutingNode toRoutingNode = allocation.routingNodes().node(toDiscoNode.getId());
decision = allocation.deciders().canAllocate(shardRouting, toRoutingNode, allocation);
if (decision.type() == Decision.Type.NO) {
if (explain) {
return new RerouteExplanation(this, decision);
}
throw new IllegalArgumentException("[move_allocation] can't move " + shardId + ", from " + fromDiscoNode + ", to " + toDiscoNode + ", since its not allowed, reason: " + decision);
}
if (decision.type() == Decision.Type.THROTTLE) {
// its being throttled, maybe have a flag to take it into account and fail? for now, just do it since the "user" wants it...
}
allocation.routingNodes().relocateShard(shardRouting, toRoutingNode.nodeId(), allocation.clusterInfo().getShardSize(shardRouting, ShardRouting.UNAVAILABLE_EXPECTED_SHARD_SIZE), allocation.changes());
}
if (!found) {
if (explain) {
return new RerouteExplanation(this, allocation.decision(Decision.NO, "move_allocation_command", "shard " + shardId + " not found"));
}
throw new IllegalArgumentException("[move_allocation] can't move " + shardId + ", failed to find it on node " + fromDiscoNode);
}
return new RerouteExplanation(this, decision);
}
use of org.elasticsearch.cluster.routing.ShardRouting in project elasticsearch by elastic.
the class RestShardsAction method buildTable.
private Table buildTable(RestRequest request, ClusterStateResponse state, IndicesStatsResponse stats) {
Table table = getTableWithHeader(request);
for (ShardRouting shard : state.getState().routingTable().allShards()) {
ShardStats shardStats = stats.asMap().get(shard);
CommonStats commonStats = null;
CommitStats commitStats = null;
if (shardStats != null) {
commonStats = shardStats.getStats();
commitStats = shardStats.getCommitStats();
}
table.startRow();
table.addCell(shard.getIndexName());
table.addCell(shard.id());
IndexMetaData indexMeta = state.getState().getMetaData().getIndexSafe(shard.index());
boolean usesShadowReplicas = false;
if (indexMeta != null) {
usesShadowReplicas = indexMeta.isIndexUsingShadowReplicas();
}
if (shard.primary()) {
table.addCell("p");
} else {
if (usesShadowReplicas) {
table.addCell("s");
} else {
table.addCell("r");
}
}
table.addCell(shard.state());
table.addCell(commonStats == null ? null : commonStats.getDocs().getCount());
table.addCell(commonStats == null ? null : commonStats.getStore().getSize());
if (shard.assignedToNode()) {
String ip = state.getState().nodes().get(shard.currentNodeId()).getHostAddress();
String nodeId = shard.currentNodeId();
StringBuilder name = new StringBuilder();
name.append(state.getState().nodes().get(shard.currentNodeId()).getName());
if (shard.relocating()) {
String reloIp = state.getState().nodes().get(shard.relocatingNodeId()).getHostAddress();
String reloNme = state.getState().nodes().get(shard.relocatingNodeId()).getName();
String reloNodeId = shard.relocatingNodeId();
name.append(" -> ");
name.append(reloIp);
name.append(" ");
name.append(reloNodeId);
name.append(" ");
name.append(reloNme);
}
table.addCell(ip);
table.addCell(nodeId);
table.addCell(name);
} else {
table.addCell(null);
table.addCell(null);
table.addCell(null);
}
table.addCell(commitStats == null ? null : commitStats.getUserData().get(Engine.SYNC_COMMIT_ID));
if (shard.unassignedInfo() != null) {
table.addCell(shard.unassignedInfo().getReason());
table.addCell(UnassignedInfo.DATE_TIME_FORMATTER.printer().print(shard.unassignedInfo().getUnassignedTimeInMillis()));
table.addCell(TimeValue.timeValueMillis(System.currentTimeMillis() - shard.unassignedInfo().getUnassignedTimeInMillis()));
table.addCell(shard.unassignedInfo().getDetails());
} else {
table.addCell(null);
table.addCell(null);
table.addCell(null);
table.addCell(null);
}
if (shard.recoverySource() != null) {
table.addCell(shard.recoverySource().getType().toString().toLowerCase(Locale.ROOT));
} else {
table.addCell(null);
}
table.addCell(commonStats == null ? null : commonStats.getCompletion().getSize());
table.addCell(commonStats == null ? null : commonStats.getFieldData().getMemorySize());
table.addCell(commonStats == null ? null : commonStats.getFieldData().getEvictions());
table.addCell(commonStats == null ? null : commonStats.getQueryCache().getMemorySize());
table.addCell(commonStats == null ? null : commonStats.getQueryCache().getEvictions());
table.addCell(commonStats == null ? null : commonStats.getFlush().getTotal());
table.addCell(commonStats == null ? null : commonStats.getFlush().getTotalTime());
table.addCell(commonStats == null ? null : commonStats.getGet().current());
table.addCell(commonStats == null ? null : commonStats.getGet().getTime());
table.addCell(commonStats == null ? null : commonStats.getGet().getCount());
table.addCell(commonStats == null ? null : commonStats.getGet().getExistsTime());
table.addCell(commonStats == null ? null : commonStats.getGet().getExistsCount());
table.addCell(commonStats == null ? null : commonStats.getGet().getMissingTime());
table.addCell(commonStats == null ? null : commonStats.getGet().getMissingCount());
table.addCell(commonStats == null ? null : commonStats.getIndexing().getTotal().getDeleteCurrent());
table.addCell(commonStats == null ? null : commonStats.getIndexing().getTotal().getDeleteTime());
table.addCell(commonStats == null ? null : commonStats.getIndexing().getTotal().getDeleteCount());
table.addCell(commonStats == null ? null : commonStats.getIndexing().getTotal().getIndexCurrent());
table.addCell(commonStats == null ? null : commonStats.getIndexing().getTotal().getIndexTime());
table.addCell(commonStats == null ? null : commonStats.getIndexing().getTotal().getIndexCount());
table.addCell(commonStats == null ? null : commonStats.getIndexing().getTotal().getIndexFailedCount());
table.addCell(commonStats == null ? null : commonStats.getMerge().getCurrent());
table.addCell(commonStats == null ? null : commonStats.getMerge().getCurrentNumDocs());
table.addCell(commonStats == null ? null : commonStats.getMerge().getCurrentSize());
table.addCell(commonStats == null ? null : commonStats.getMerge().getTotal());
table.addCell(commonStats == null ? null : commonStats.getMerge().getTotalNumDocs());
table.addCell(commonStats == null ? null : commonStats.getMerge().getTotalSize());
table.addCell(commonStats == null ? null : commonStats.getMerge().getTotalTime());
table.addCell(commonStats == null ? null : commonStats.getRefresh().getTotal());
table.addCell(commonStats == null ? null : commonStats.getRefresh().getTotalTime());
table.addCell(commonStats == null ? null : commonStats.getRefresh().getListeners());
table.addCell(commonStats == null ? null : commonStats.getSearch().getTotal().getFetchCurrent());
table.addCell(commonStats == null ? null : commonStats.getSearch().getTotal().getFetchTime());
table.addCell(commonStats == null ? null : commonStats.getSearch().getTotal().getFetchCount());
table.addCell(commonStats == null ? null : commonStats.getSearch().getOpenContexts());
table.addCell(commonStats == null ? null : commonStats.getSearch().getTotal().getQueryCurrent());
table.addCell(commonStats == null ? null : commonStats.getSearch().getTotal().getQueryTime());
table.addCell(commonStats == null ? null : commonStats.getSearch().getTotal().getQueryCount());
table.addCell(commonStats == null ? null : commonStats.getSearch().getTotal().getScrollCurrent());
table.addCell(commonStats == null ? null : commonStats.getSearch().getTotal().getScrollTime());
table.addCell(commonStats == null ? null : commonStats.getSearch().getTotal().getScrollCount());
table.addCell(commonStats == null ? null : commonStats.getSegments().getCount());
table.addCell(commonStats == null ? null : commonStats.getSegments().getMemory());
table.addCell(commonStats == null ? null : commonStats.getSegments().getIndexWriterMemory());
table.addCell(commonStats == null ? null : commonStats.getSegments().getVersionMapMemory());
table.addCell(commonStats == null ? null : commonStats.getSegments().getBitsetMemory());
table.addCell(shardStats == null || shardStats.getSeqNoStats() == null ? null : shardStats.getSeqNoStats().getMaxSeqNo());
table.addCell(shardStats == null || shardStats.getSeqNoStats() == null ? null : shardStats.getSeqNoStats().getLocalCheckpoint());
table.addCell(commitStats == null || shardStats.getSeqNoStats() == null ? null : shardStats.getSeqNoStats().getGlobalCheckpoint());
table.addCell(commonStats == null ? null : commonStats.getWarmer().current());
table.addCell(commonStats == null ? null : commonStats.getWarmer().total());
table.addCell(commonStats == null ? null : commonStats.getWarmer().totalTime());
table.endRow();
}
return table;
}
use of org.elasticsearch.cluster.routing.ShardRouting in project elasticsearch by elastic.
the class ActiveShardCountTests method startPrimaries.
private ClusterState startPrimaries(final ClusterState clusterState, final String indexName) {
RoutingTable routingTable = clusterState.routingTable();
IndexRoutingTable indexRoutingTable = routingTable.index(indexName);
IndexRoutingTable.Builder newIndexRoutingTable = IndexRoutingTable.builder(indexRoutingTable.getIndex());
for (final ObjectCursor<IndexShardRoutingTable> shardEntry : indexRoutingTable.getShards().values()) {
final IndexShardRoutingTable shardRoutingTable = shardEntry.value;
for (ShardRouting shardRouting : shardRoutingTable.getShards()) {
if (shardRouting.primary()) {
shardRouting = shardRouting.initialize(randomAsciiOfLength(8), null, shardRouting.getExpectedShardSize()).moveToStarted();
}
newIndexRoutingTable.addShard(shardRouting);
}
}
routingTable = RoutingTable.builder(routingTable).add(newIndexRoutingTable).build();
return ClusterState.builder(clusterState).routingTable(routingTable).build();
}
Aggregations