move all allocations to be Decision base

this again will allow to provide better output as to why a specific decision has been made (like a shard needing to move from a node)
This commit is contained in:
Shay Banon 2012-10-16 10:10:06 -04:00
parent a963bc1dc9
commit 2c7850584d
10 changed files with 54 additions and 41 deletions

View File

@ -28,6 +28,7 @@ import org.elasticsearch.cluster.routing.*;
import org.elasticsearch.cluster.routing.allocation.allocator.ShardsAllocators; import org.elasticsearch.cluster.routing.allocation.allocator.ShardsAllocators;
import org.elasticsearch.cluster.routing.allocation.command.AllocationCommands; import org.elasticsearch.cluster.routing.allocation.command.AllocationCommands;
import org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders; import org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders;
import org.elasticsearch.cluster.routing.allocation.decider.Decision;
import org.elasticsearch.common.component.AbstractComponent; import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.ImmutableSettings;
@ -223,7 +224,8 @@ public class AllocationService extends AbstractComponent {
continue; continue;
} }
RoutingNode routingNode = allocation.routingNodes().node(shardRouting.currentNodeId()); RoutingNode routingNode = allocation.routingNodes().node(shardRouting.currentNodeId());
if (!allocation.deciders().canRemain(shardRouting, routingNode, allocation)) { Decision decision = allocation.deciders().canRemain(shardRouting, routingNode, allocation);
if (decision.type() == Decision.Type.NO) {
logger.debug("[{}][{}] allocated on [{}], but can no longer be allocated on it, moving...", shardRouting.index(), shardRouting.id(), routingNode.node()); logger.debug("[{}][{}] allocated on [{}], but can no longer be allocated on it, moving...", shardRouting.index(), shardRouting.id(), routingNode.node());
boolean moved = shardsAllocators.move(shardRouting, routingNode, allocation); boolean moved = shardsAllocators.move(shardRouting, routingNode, allocation);
if (!moved) { if (!moved) {

View File

@ -141,12 +141,13 @@ public class EvenShardsCountAllocator extends AbstractComponent implements Shard
boolean relocated = false; boolean relocated = false;
List<MutableShardRouting> startedShards = highRoutingNode.shardsWithState(STARTED); List<MutableShardRouting> startedShards = highRoutingNode.shardsWithState(STARTED);
for (MutableShardRouting startedShard : startedShards) { for (MutableShardRouting startedShard : startedShards) {
if (!allocation.deciders().canRebalance(startedShard, allocation)) { Decision rebalanceDecision = allocation.deciders().canRebalance(startedShard, allocation);
if (rebalanceDecision.type() == Decision.Type.NO) {
continue; continue;
} }
Decision decision = allocation.deciders().canAllocate(startedShard, lowRoutingNode, allocation); Decision allocateDecision = allocation.deciders().canAllocate(startedShard, lowRoutingNode, allocation);
if (decision.type() == Decision.Type.YES) { if (allocateDecision.type() == Decision.Type.YES) {
changed = true; changed = true;
lowRoutingNode.add(new MutableShardRouting(startedShard.index(), startedShard.id(), lowRoutingNode.add(new MutableShardRouting(startedShard.index(), startedShard.id(),
lowRoutingNode.nodeId(), startedShard.currentNodeId(), lowRoutingNode.nodeId(), startedShard.currentNodeId(),

View File

@ -34,10 +34,16 @@ public abstract class AllocationDecider extends AbstractComponent {
super(settings); super(settings);
} }
public boolean canRebalance(ShardRouting shardRouting, RoutingAllocation allocation) { /**
return true; * Are we allowed to rebalance this shard?
*/
public Decision canRebalance(ShardRouting shardRouting, RoutingAllocation allocation) {
return Decision.ALWAYS;
} }
/**
* Can the provided shard routing be allocated on the node.
*/
public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) { public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
return Decision.ALWAYS; return Decision.ALWAYS;
} }
@ -45,7 +51,7 @@ public abstract class AllocationDecider extends AbstractComponent {
/** /**
* Can the provided shard routing remain on the node? * Can the provided shard routing remain on the node?
*/ */
public boolean canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) { public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
return true; return Decision.ALWAYS;
} }
} }

View File

@ -59,13 +59,15 @@ public class AllocationDeciders extends AllocationDecider {
} }
@Override @Override
public boolean canRebalance(ShardRouting shardRouting, RoutingAllocation allocation) { public Decision canRebalance(ShardRouting shardRouting, RoutingAllocation allocation) {
for (AllocationDecider allocation1 : allocations) { Decision.Multi ret = new Decision.Multi();
if (!allocation1.canRebalance(shardRouting, allocation)) { for (AllocationDecider allocationDecider : allocations) {
return false; Decision decision = allocationDecider.canRebalance(shardRouting, allocation);
if (decision != Decision.ALWAYS) {
ret.add(decision);
} }
} }
return true; return ret;
} }
@Override @Override
@ -86,15 +88,17 @@ public class AllocationDeciders extends AllocationDecider {
} }
@Override @Override
public boolean canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) { public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
if (allocation.shouldIgnoreShardForNode(shardRouting.shardId(), node.nodeId())) { if (allocation.shouldIgnoreShardForNode(shardRouting.shardId(), node.nodeId())) {
return false; return Decision.NO;
} }
for (AllocationDecider allocation1 : allocations) { Decision.Multi ret = new Decision.Multi();
if (!allocation1.canRemain(shardRouting, node, allocation)) { for (AllocationDecider allocationDecider : allocations) {
return false; Decision decision = allocationDecider.canRemain(shardRouting, node, allocation);
if (decision != Decision.ALWAYS) {
ret.add(decision);
} }
} }
return true; return ret;
} }
} }

View File

@ -107,8 +107,8 @@ public class AwarenessAllocationDecider extends AllocationDecider {
} }
@Override @Override
public boolean canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) { public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
return underCapacity(shardRouting, node, allocation, false); return underCapacity(shardRouting, node, allocation, false) ? Decision.YES : Decision.NO;
} }
private boolean underCapacity(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation, boolean moveToNode) { private boolean underCapacity(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation, boolean moveToNode) {

View File

@ -56,11 +56,11 @@ public class ClusterRebalanceAllocationDecider extends AllocationDecider {
} }
@Override @Override
public boolean canRebalance(ShardRouting shardRouting, RoutingAllocation allocation) { public Decision canRebalance(ShardRouting shardRouting, RoutingAllocation allocation) {
if (type == ClusterRebalanceType.INDICES_PRIMARIES_ACTIVE) { if (type == ClusterRebalanceType.INDICES_PRIMARIES_ACTIVE) {
for (MutableShardRouting shard : allocation.routingNodes().unassigned()) { for (MutableShardRouting shard : allocation.routingNodes().unassigned()) {
if (shard.primary()) { if (shard.primary()) {
return false; return Decision.NO;
} }
} }
for (RoutingNode node : allocation.routingNodes()) { for (RoutingNode node : allocation.routingNodes()) {
@ -68,27 +68,27 @@ public class ClusterRebalanceAllocationDecider extends AllocationDecider {
for (int i = 0; i < shards.size(); i++) { for (int i = 0; i < shards.size(); i++) {
MutableShardRouting shard = shards.get(i); MutableShardRouting shard = shards.get(i);
if (shard.primary() && !shard.active() && shard.relocatingNodeId() == null) { if (shard.primary() && !shard.active() && shard.relocatingNodeId() == null) {
return false; return Decision.NO;
} }
} }
} }
return true; return Decision.YES;
} }
if (type == ClusterRebalanceType.INDICES_ALL_ACTIVE) { if (type == ClusterRebalanceType.INDICES_ALL_ACTIVE) {
if (!allocation.routingNodes().unassigned().isEmpty()) { if (!allocation.routingNodes().unassigned().isEmpty()) {
return false; return Decision.NO;
} }
for (RoutingNode node : allocation.routingNodes()) { for (RoutingNode node : allocation.routingNodes()) {
List<MutableShardRouting> shards = node.shards(); List<MutableShardRouting> shards = node.shards();
for (int i = 0; i < shards.size(); i++) { for (int i = 0; i < shards.size(); i++) {
MutableShardRouting shard = shards.get(i); MutableShardRouting shard = shards.get(i);
if (!shard.active() && shard.relocatingNodeId() == null) { if (!shard.active() && shard.relocatingNodeId() == null) {
return false; return Decision.NO;
} }
} }
} }
} }
// type == Type.ALWAYS // type == Type.ALWAYS
return true; return Decision.YES;
} }
} }

View File

@ -61,9 +61,9 @@ public class ConcurrentRebalanceAllocationDecider extends AllocationDecider {
} }
@Override @Override
public boolean canRebalance(ShardRouting shardRouting, RoutingAllocation allocation) { public Decision canRebalance(ShardRouting shardRouting, RoutingAllocation allocation) {
if (clusterConcurrentRebalance == -1) { if (clusterConcurrentRebalance == -1) {
return true; return Decision.YES;
} }
int rebalance = 0; int rebalance = 0;
for (RoutingNode node : allocation.routingNodes()) { for (RoutingNode node : allocation.routingNodes()) {
@ -75,8 +75,8 @@ public class ConcurrentRebalanceAllocationDecider extends AllocationDecider {
} }
} }
if (rebalance >= clusterConcurrentRebalance) { if (rebalance >= clusterConcurrentRebalance) {
return false; return Decision.NO;
} }
return true; return Decision.YES;
} }
} }

View File

@ -72,8 +72,8 @@ public class FilterAllocationDecider extends AllocationDecider {
} }
@Override @Override
public boolean canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) { public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
return !shouldFilter(shardRouting, node, allocation); return shouldFilter(shardRouting, node, allocation) ? Decision.NO : Decision.YES;
} }
private boolean shouldFilter(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) { private boolean shouldFilter(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {

View File

@ -38,15 +38,15 @@ public class RebalanceOnlyWhenActiveAllocationDecider extends AllocationDecider
} }
@Override @Override
public boolean canRebalance(ShardRouting shardRouting, RoutingAllocation allocation) { public Decision canRebalance(ShardRouting shardRouting, RoutingAllocation allocation) {
List<MutableShardRouting> shards = allocation.routingNodes().shardsRoutingFor(shardRouting); List<MutableShardRouting> shards = allocation.routingNodes().shardsRoutingFor(shardRouting);
// its ok to check for active here, since in relocation, a shard is split into two in routing // its ok to check for active here, since in relocation, a shard is split into two in routing
// nodes, once relocating, and one initializing // nodes, once relocating, and one initializing
for (int i = 0; i < shards.size(); i++) { for (int i = 0; i < shards.size(); i++) {
if (!shards.get(i).active()) { if (!shards.get(i).active()) {
return false; return Decision.NO;
} }
} }
return true; return Decision.YES;
} }
} }

View File

@ -75,11 +75,11 @@ public class ShardsLimitAllocationDecider extends AllocationDecider {
} }
@Override @Override
public boolean canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) { public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
IndexMetaData indexMd = allocation.routingNodes().metaData().index(shardRouting.index()); IndexMetaData indexMd = allocation.routingNodes().metaData().index(shardRouting.index());
int totalShardsPerNode = indexMd.settings().getAsInt(INDEX_TOTAL_SHARDS_PER_NODE, -1); int totalShardsPerNode = indexMd.settings().getAsInt(INDEX_TOTAL_SHARDS_PER_NODE, -1);
if (totalShardsPerNode <= 0) { if (totalShardsPerNode <= 0) {
return true; return Decision.YES;
} }
int nodeCount = 0; int nodeCount = 0;
@ -96,8 +96,8 @@ public class ShardsLimitAllocationDecider extends AllocationDecider {
nodeCount++; nodeCount++;
} }
if (nodeCount > totalShardsPerNode) { if (nodeCount > totalShardsPerNode) {
return false; return Decision.NO;
} }
return true; return Decision.YES;
} }
} }