Java Code Examples for org.elasticsearch.cluster.routing.ShardRouting#initializing()

The following examples show how to use org.elasticsearch.cluster.routing.ShardRouting#initializing() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IndicesClusterStateService.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Notifies master about shards that don't exist but are supposed to be active on this node.
 *
 * @param state new cluster state
 */
private void failMissingShards(final ClusterState state) {
    RoutingNode localRoutingNode = state.getRoutingNodes().node(state.nodes().getLocalNodeId());
    if (localRoutingNode == null) {
        return;
    }
    for (final ShardRouting shardRouting : localRoutingNode) {
        ShardId shardId = shardRouting.shardId();
        if (shardRouting.initializing() == false &&
            failedShardsCache.containsKey(shardId) == false &&
            indicesService.getShardOrNull(shardId) == null) {
            // the master thinks we are active, but we don't have this shard at all, mark it as failed
            sendFailShard(shardRouting, "master marked shard as active, but shard has not been created, mark shard as failed", null,
                state);
        }
    }
}
 
Example 2
Source File: RestoreService.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public void shardFailed(ShardRouting failedShard, UnassignedInfo unassignedInfo) {
    if (failedShard.primary() && failedShard.initializing()) {
        RecoverySource recoverySource = failedShard.recoverySource();
        if (recoverySource.getType() == RecoverySource.Type.SNAPSHOT) {
            Snapshot snapshot = ((SnapshotRecoverySource) recoverySource).snapshot();
            // mark restore entry for this shard as failed when it's due to a file corruption. There is no need wait on retries
            // to restore this shard on another node if the snapshot files are corrupt. In case where a node just left or crashed,
            // however, we only want to acknowledge the restore operation once it has been successfully restored on another node.
            if (unassignedInfo.getFailure() != null && Lucene.isCorruptionException(unassignedInfo.getFailure().getCause())) {
                changes(snapshot).shards.put(failedShard.shardId(), new ShardRestoreStatus(failedShard.currentNodeId(),
                    RestoreInProgress.State.FAILURE, unassignedInfo.getFailure().getCause().getMessage()));
            }
        }
    }
}
 
Example 3
Source File: SysAllocations.java    From crate with Apache License 2.0 6 votes vote down vote up
private static ClusterAllocationExplanation explainShard(ShardRouting shardRouting,
                                                         RoutingAllocation allocation,
                                                         GatewayAllocator gatewayAllocator,
                                                         ShardsAllocator shardAllocator) {
    allocation.setDebugMode(RoutingAllocation.DebugMode.EXCLUDE_YES_DECISIONS);

    ShardAllocationDecision shardDecision;
    if (shardRouting.initializing() || shardRouting.relocating()) {
        shardDecision = ShardAllocationDecision.NOT_TAKEN;
    } else {
        AllocateUnassignedDecision allocateDecision = shardRouting.unassigned() ?
            gatewayAllocator.decideUnassignedShardAllocation(shardRouting, allocation) : AllocateUnassignedDecision.NOT_TAKEN;
        if (allocateDecision.isDecisionTaken() == false) {
            shardDecision = shardAllocator.decideShardAllocation(shardRouting, allocation);
        } else {
            shardDecision = new ShardAllocationDecision(allocateDecision, MoveDecision.NOT_TAKEN);
        }
    }
    return new ClusterAllocationExplanation(
        shardRouting,
        shardRouting.currentNodeId() != null ? allocation.nodes().get(shardRouting.currentNodeId()) : null,
        shardRouting.relocatingNodeId() != null ? allocation.nodes().get(shardRouting.relocatingNodeId()) : null,
        null,
        shardDecision
    );
}
 
Example 4
Source File: ReplicationOperationTests.java    From crate with Apache License 2.0 6 votes vote down vote up
private void addTrackingInfo(IndexShardRoutingTable indexShardRoutingTable, ShardRouting primaryShard, Set<String> trackedShards,
                             Set<String> untrackedShards) {
    for (ShardRouting shr : indexShardRoutingTable.shards()) {
        if (shr.unassigned() == false) {
            if (shr.initializing()) {
                if (randomBoolean()) {
                    trackedShards.add(shr.allocationId().getId());
                } else {
                    untrackedShards.add(shr.allocationId().getId());
                }
            } else {
                trackedShards.add(shr.allocationId().getId());
                if (shr.relocating()) {
                    if (primaryShard == shr.getTargetRelocatingShard() || randomBoolean()) {
                        trackedShards.add(shr.getTargetRelocatingShard().allocationId().getId());
                    } else {
                        untrackedShards.add(shr.getTargetRelocatingShard().allocationId().getId());
                    }
                }
            }
        }
    }
}
 
Example 5
Source File: DiskThresholdDecider.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the size of all shards that are currently being relocated to
 * the node, but may not be finished transfering yet.
 *
 * If subtractShardsMovingAway is set then the size of shards moving away is subtracted from the total size
 * of all shards
 */
public static long sizeOfRelocatingShards(RoutingNode node, ClusterInfo clusterInfo, boolean subtractShardsMovingAway, String dataPath) {
    long totalSize = 0;
    for (ShardRouting routing : node.shardsWithState(ShardRoutingState.RELOCATING, ShardRoutingState.INITIALIZING)) {
        String actualPath = clusterInfo.getDataPath(routing);
        if (dataPath.equals(actualPath)) {
            if (routing.initializing() && routing.relocatingNodeId() != null) {
                totalSize += getShardSize(routing, clusterInfo);
            } else if (subtractShardsMovingAway && routing.relocating()) {
                totalSize -= getShardSize(routing, clusterInfo);
            }
        }
    }
    return totalSize;
}
 
Example 6
Source File: ThrottlingAllocationDecider.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Decision canAllocate(RoutingNode node, RoutingAllocation allocation) {
    int currentRecoveries = 0;
    for (ShardRouting shard : node) {
        if (shard.initializing()) {
            currentRecoveries++;
        }
    }
    if (currentRecoveries >= concurrentRecoveries) {
        return allocation.decision(Decision.THROTTLE, NAME, "too many shards currently recovering [%d], limit: [%d]",
                currentRecoveries, concurrentRecoveries);
    } else {
        return allocation.decision(Decision.YES, NAME, "below shard recovery limit of [%d]", concurrentRecoveries);
    }
}
 
Example 7
Source File: ClusterShardHealth.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public ClusterShardHealth(int shardId, final IndexShardRoutingTable shardRoutingTable) {
    this.shardId = shardId;
    for (ShardRouting shardRouting : shardRoutingTable) {
        if (shardRouting.active()) {
            activeShards++;
            if (shardRouting.relocating()) {
                // the shard is relocating, the one it is relocating to will be in initializing state, so we don't count it
                relocatingShards++;
            }
            if (shardRouting.primary()) {
                primaryActive = true;
            }
        } else if (shardRouting.initializing()) {
            initializingShards++;
        } else if (shardRouting.unassigned()) {
            unassignedShards++;
        }
    }
    if (primaryActive) {
        if (activeShards == shardRoutingTable.size()) {
            status = ClusterHealthStatus.GREEN;
        } else {
            status = ClusterHealthStatus.YELLOW;
        }
    } else {
        status = ClusterHealthStatus.RED;
    }
}
 
Example 8
Source File: ClusterShardHealth.java    From crate with Apache License 2.0 5 votes vote down vote up
public ClusterShardHealth(final int shardId, final IndexShardRoutingTable shardRoutingTable) {
    this.shardId = shardId;
    int computeActiveShards = 0;
    int computeRelocatingShards = 0;
    int computeInitializingShards = 0;
    int computeUnassignedShards = 0;
    for (ShardRouting shardRouting : shardRoutingTable) {
        if (shardRouting.active()) {
            computeActiveShards++;
            if (shardRouting.relocating()) {
                // the shard is relocating, the one it is relocating to will be in initializing state, so we don't count it
                computeRelocatingShards++;
            }
        } else if (shardRouting.initializing()) {
            computeInitializingShards++;
        } else if (shardRouting.unassigned()) {
            computeUnassignedShards++;
        }
    }
    ClusterHealthStatus computeStatus;
    final ShardRouting primaryRouting = shardRoutingTable.primaryShard();
    if (primaryRouting.active()) {
        if (computeActiveShards == shardRoutingTable.size()) {
            computeStatus = ClusterHealthStatus.GREEN;
        } else {
            computeStatus = ClusterHealthStatus.YELLOW;
        }
    } else {
        computeStatus = getInactivePrimaryHealth(primaryRouting);
    }
    this.status = computeStatus;
    this.activeShards = computeActiveShards;
    this.relocatingShards = computeRelocatingShards;
    this.initializingShards = computeInitializingShards;
    this.unassignedShards = computeUnassignedShards;
    this.primaryActive = primaryRouting.active();
}
 
Example 9
Source File: BlobRecoverySource.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
private RecoveryResponse recover(final StartRecoveryRequest request) {
    final IndexService indexService = indicesService.indexServiceSafe(request.shardId().index().name());
    final IndexShard shard = indexService.shardSafe(request.shardId().id());

    // starting recovery from that our (the source) shard state is marking the shard to be in recovery mode as well, otherwise
    // the index operations will not be routed to it properly
    RoutingNode node = clusterService.state().getRoutingNodes().node(request.targetNode().id());
    if (node == null) {
        logger.debug("delaying recovery of {} as source node {} is unknown", request.shardId(), request.targetNode());
        throw new DelayRecoveryException("source node does not have the node [" + request.targetNode() + "] in its state yet..");
    }
    ShardRouting targetShardRouting = null;
    for (ShardRouting shardRouting : node) {
        if (shardRouting.shardId().equals(request.shardId())) {
            targetShardRouting = shardRouting;
            break;
        }
    }
    if (targetShardRouting == null) {
        logger.debug("delaying recovery of {} as it is not listed as assigned to target node {}", request.shardId(), request.targetNode());
        throw new DelayRecoveryException("source node does not have the shard listed in its state as allocated on the node");
    }
    if (!targetShardRouting.initializing()) {
        logger.debug("delaying recovery of {} as it is not listed as initializing on the target node {}. known shards state is [{}]",
                request.shardId(), request.targetNode(), targetShardRouting.state());
        throw new DelayRecoveryException("source node has the state of the target shard to be [" + targetShardRouting.state() + "], expecting to be [initializing]");
    }

    logger.trace("[{}][{}] starting recovery to {}, mark_as_relocated {}", request.shardId().index().name(), request.shardId().id(), request.targetNode(), request.markAsRelocated());
    final RecoverySourceHandler handler;
    if (IndexMetaData.isOnSharedFilesystem(shard.indexSettings())) {
        handler = new SharedFSRecoverySourceHandler(shard, request, recoverySettings, transportService, logger);
    } else {
        // CRATE CHANGE:
        handler = new BlobRecoverySourceHandler(
                shard, request, recoverySettings, transportService, logger, blobTransferTarget, blobIndices);
    }
    ongoingRecoveries.add(shard, handler);
    try {
        return handler.recoverToTarget();
    } finally {
        ongoingRecoveries.remove(shard, handler);
    }
}
 
Example 10
Source File: RecoverySource.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
private RecoveryResponse recover(final StartRecoveryRequest request) {
    final IndexService indexService = indicesService.indexServiceSafe(request.shardId().index().name());
    final IndexShard shard = indexService.shardSafe(request.shardId().id());

    // starting recovery from that our (the source) shard state is marking the shard to be in recovery mode as well, otherwise
    // the index operations will not be routed to it properly
    RoutingNode node = clusterService.state().getRoutingNodes().node(request.targetNode().id());
    if (node == null) {
        logger.debug("delaying recovery of {} as source node {} is unknown", request.shardId(), request.targetNode());
        throw new DelayRecoveryException("source node does not have the node [" + request.targetNode() + "] in its state yet..");
    }
    ShardRouting targetShardRouting = null;
    for (ShardRouting shardRouting : node) {
        if (shardRouting.shardId().equals(request.shardId())) {
            targetShardRouting = shardRouting;
            break;
        }
    }
    if (targetShardRouting == null) {
        logger.debug("delaying recovery of {} as it is not listed as assigned to target node {}", request.shardId(), request.targetNode());
        throw new DelayRecoveryException("source node does not have the shard listed in its state as allocated on the node");
    }
    if (!targetShardRouting.initializing()) {
        logger.debug("delaying recovery of {} as it is not listed as initializing on the target node {}. known shards state is [{}]",
                request.shardId(), request.targetNode(), targetShardRouting.state());
        throw new DelayRecoveryException("source node has the state of the target shard to be [" + targetShardRouting.state() + "], expecting to be [initializing]");
    }

    logger.trace("[{}][{}] starting recovery to {}, mark_as_relocated {}", request.shardId().index().name(), request.shardId().id(), request.targetNode(), request.markAsRelocated());
    final RecoverySourceHandler handler;
    if (IndexMetaData.isOnSharedFilesystem(shard.indexSettings())) {
        handler = new SharedFSRecoverySourceHandler(shard, request, recoverySettings, transportService, logger);
    } else if (IndexMetaData.isIndexUsingDLEngine(shard.indexSettings())) {
        handler = new DLBasedIndexRecoverySourceHandler(shard, request, recoverySettings, transportService, logger);
    } else {
        handler = new RecoverySourceHandler(shard, request, recoverySettings, transportService, logger);
    }
    ongoingRecoveries.add(shard, handler);
    try {
        return handler.recoverToTarget();
    } finally {
        ongoingRecoveries.remove(shard, handler);
    }
}
 
Example 11
Source File: AwarenessAllocationDecider.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
private Decision underCapacity(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation, boolean moveToNode) {
    if (awarenessAttributes.length == 0) {
        return allocation.decision(Decision.YES, NAME, "no allocation awareness enabled");
    }

    IndexMetaData indexMetaData = allocation.metaData().index(shardRouting.index());
    int shardCount = indexMetaData.getNumberOfReplicas() + 1; // 1 for primary
    for (String awarenessAttribute : awarenessAttributes) {
        // the node the shard exists on must be associated with an awareness attribute
        if (!node.node().attributes().containsKey(awarenessAttribute)) {
            return allocation.decision(Decision.NO, NAME, "node does not contain awareness attribute: [%s]", awarenessAttribute);
        }

        // build attr_value -> nodes map
        ObjectIntHashMap<String> nodesPerAttribute = allocation.routingNodes().nodesPerAttributesCounts(awarenessAttribute);

        // build the count of shards per attribute value
        ObjectIntHashMap<String> shardPerAttribute = new ObjectIntHashMap<>();
        for (ShardRouting assignedShard : allocation.routingNodes().assignedShards(shardRouting)) {
            if (assignedShard.started() || assignedShard.initializing()) {
                // Note: this also counts relocation targets as that will be the new location of the shard.
                // Relocation sources should not be counted as the shard is moving away
                RoutingNode routingNode = allocation.routingNodes().node(assignedShard.currentNodeId());
                shardPerAttribute.addTo(routingNode.node().attributes().get(awarenessAttribute), 1);
            }
        }

        if (moveToNode) {
            if (shardRouting.assignedToNode()) {
                String nodeId = shardRouting.relocating() ? shardRouting.relocatingNodeId() : shardRouting.currentNodeId();
                if (!node.nodeId().equals(nodeId)) {
                    // we work on different nodes, move counts around
                    shardPerAttribute.putOrAdd(allocation.routingNodes().node(nodeId).node().attributes().get(awarenessAttribute), 0, -1);
                    shardPerAttribute.addTo(node.node().attributes().get(awarenessAttribute), 1);
                }
            } else {
                shardPerAttribute.addTo(node.node().attributes().get(awarenessAttribute), 1);
            }
        }

        int numberOfAttributes = nodesPerAttribute.size();
        String[] fullValues = forcedAwarenessAttributes.get(awarenessAttribute);
        if (fullValues != null) {
            for (String fullValue : fullValues) {
                if (!shardPerAttribute.containsKey(fullValue)) {
                    numberOfAttributes++;
                }
            }
        }
        // TODO should we remove ones that are not part of full list?

        int averagePerAttribute = shardCount / numberOfAttributes;
        int totalLeftover = shardCount % numberOfAttributes;
        int requiredCountPerAttribute;
        if (averagePerAttribute == 0) {
            // if we have more attributes values than shard count, no leftover
            totalLeftover = 0;
            requiredCountPerAttribute = 1;
        } else {
            requiredCountPerAttribute = averagePerAttribute;
        }
        int leftoverPerAttribute = totalLeftover == 0 ? 0 : 1;

        int currentNodeCount = shardPerAttribute.get(node.node().attributes().get(awarenessAttribute));
        // if we are above with leftover, then we know we are not good, even with mod
        if (currentNodeCount > (requiredCountPerAttribute + leftoverPerAttribute)) {
            return allocation.decision(Decision.NO, NAME,
                    "too many shards on node for attribute: [%s], required per attribute: [%d], node count: [%d], leftover: [%d]",
                    awarenessAttribute, requiredCountPerAttribute, currentNodeCount, leftoverPerAttribute);
        }
        // all is well, we are below or same as average
        if (currentNodeCount <= requiredCountPerAttribute) {
            continue;
        }
    }

    return allocation.decision(Decision.YES, NAME, "node meets awareness requirements");
}
 
Example 12
Source File: CancelAllocationCommand.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public RerouteExplanation execute(RoutingAllocation allocation, boolean explain) {
    DiscoveryNode discoNode = allocation.nodes().resolveNode(node);
    boolean found = false;
    for (RoutingNodes.RoutingNodeIterator it = allocation.routingNodes().routingNodeIter(discoNode.id()); it.hasNext(); ) {
        ShardRouting shardRouting = it.next();
        if (!shardRouting.shardId().equals(shardId)) {
            continue;
        }
        found = true;
        if (shardRouting.relocatingNodeId() != null) {
            if (shardRouting.initializing()) {
                // the shard is initializing and recovering from another node, simply cancel the recovery
                it.remove();
                // and cancel the relocating state from the shard its being relocated from
                RoutingNode relocatingFromNode = allocation.routingNodes().node(shardRouting.relocatingNodeId());
                if (relocatingFromNode != null) {
                    for (ShardRouting fromShardRouting : relocatingFromNode) {
                        if (fromShardRouting.isSameShard(shardRouting) && fromShardRouting.state() == RELOCATING) {
                            allocation.routingNodes().cancelRelocation(fromShardRouting);
                            break;
                        }
                    }
                }
            } else if (shardRouting.relocating()) {

                // the shard is relocating to another node, cancel the recovery on the other node, and deallocate this one
                if (!allowPrimary && shardRouting.primary()) {
                    // can't cancel a primary shard being initialized
                    if (explain) {
                        return new RerouteExplanation(this, allocation.decision(Decision.NO, "cancel_allocation_command",
                                "can't cancel " + shardId + " on node " + discoNode + ", shard is primary and initializing its state"));
                    }
                    throw new IllegalArgumentException("[cancel_allocation] can't cancel " + shardId + " on node " +
                            discoNode + ", shard is primary and initializing its state");
                }
                it.moveToUnassigned(new UnassignedInfo(UnassignedInfo.Reason.REROUTE_CANCELLED, null));
                // now, go and find the shard that is initializing on the target node, and cancel it as well...
                RoutingNodes.RoutingNodeIterator initializingNode = allocation.routingNodes().routingNodeIter(shardRouting.relocatingNodeId());
                if (initializingNode != null) {
                    while (initializingNode.hasNext()) {
                        ShardRouting initializingShardRouting = initializingNode.next();
                        if (initializingShardRouting.isRelocationTargetOf(shardRouting)) {
                            initializingNode.remove();
                        }
                    }
                }
            }
        } else {
            // the shard is not relocating, its either started, or initializing, just cancel it and move on...
            if (!allowPrimary && shardRouting.primary()) {
                // can't cancel a primary shard being initialized
                if (explain) {
                    return new RerouteExplanation(this, allocation.decision(Decision.NO, "cancel_allocation_command",
                            "can't cancel " + shardId + " on node " + discoNode + ", shard is primary and started"));
                }
                throw new IllegalArgumentException("[cancel_allocation] can't cancel " + shardId + " on node " +
                        discoNode + ", shard is primary and started");
            }
            it.moveToUnassigned(new UnassignedInfo(UnassignedInfo.Reason.REROUTE_CANCELLED, null));
        }
    }
    if (!found) {
        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);
    }
    return new RerouteExplanation(this, allocation.decision(Decision.YES, "cancel_allocation_command",
            "shard " + shardId + " on node " + discoNode + " can be cancelled"));
}
 
Example 13
Source File: AwarenessAllocationDecider.java    From crate with Apache License 2.0 4 votes vote down vote up
private Decision underCapacity(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation, boolean moveToNode) {
    if (awarenessAttributes.isEmpty()) {
        return allocation.decision(Decision.YES, NAME,
            "allocation awareness is not enabled, set cluster setting [%s] to enable it",
            CLUSTER_ROUTING_ALLOCATION_AWARENESS_ATTRIBUTE_SETTING.getKey());
    }

    IndexMetaData indexMetaData = allocation.metaData().getIndexSafe(shardRouting.index());
    int shardCount = indexMetaData.getNumberOfReplicas() + 1; // 1 for primary
    for (String awarenessAttribute : awarenessAttributes) {
        // the node the shard exists on must be associated with an awareness attribute
        if (!node.node().getAttributes().containsKey(awarenessAttribute)) {
            return allocation.decision(Decision.NO, NAME,
                "node does not contain the awareness attribute [%s]; required attributes cluster setting [%s=%s]",
                awarenessAttribute, CLUSTER_ROUTING_ALLOCATION_AWARENESS_ATTRIBUTE_SETTING.getKey(),
                allocation.debugDecision() ? Strings.collectionToCommaDelimitedString(awarenessAttributes) : null);
        }

        // build attr_value -> nodes map
        ObjectIntHashMap<String> nodesPerAttribute = allocation.routingNodes().nodesPerAttributesCounts(awarenessAttribute);

        // build the count of shards per attribute value
        ObjectIntHashMap<String> shardPerAttribute = new ObjectIntHashMap<>();
        for (ShardRouting assignedShard : allocation.routingNodes().assignedShards(shardRouting.shardId())) {
            if (assignedShard.started() || assignedShard.initializing()) {
                // Note: this also counts relocation targets as that will be the new location of the shard.
                // Relocation sources should not be counted as the shard is moving away
                RoutingNode routingNode = allocation.routingNodes().node(assignedShard.currentNodeId());
                shardPerAttribute.addTo(routingNode.node().getAttributes().get(awarenessAttribute), 1);
            }
        }

        if (moveToNode) {
            if (shardRouting.assignedToNode()) {
                String nodeId = shardRouting.relocating() ? shardRouting.relocatingNodeId() : shardRouting.currentNodeId();
                if (!node.nodeId().equals(nodeId)) {
                    // we work on different nodes, move counts around
                    shardPerAttribute.putOrAdd(allocation.routingNodes().node(nodeId).node().getAttributes().get(awarenessAttribute),
                            0, -1);
                    shardPerAttribute.addTo(node.node().getAttributes().get(awarenessAttribute), 1);
                }
            } else {
                shardPerAttribute.addTo(node.node().getAttributes().get(awarenessAttribute), 1);
            }
        }

        int numberOfAttributes = nodesPerAttribute.size();
        List<String> fullValues = forcedAwarenessAttributes.get(awarenessAttribute);
        if (fullValues != null) {
            for (String fullValue : fullValues) {
                if (!shardPerAttribute.containsKey(fullValue)) {
                    numberOfAttributes++;
                }
            }
        }
        // TODO should we remove ones that are not part of full list?

        int averagePerAttribute = shardCount / numberOfAttributes;
        int totalLeftover = shardCount % numberOfAttributes;
        int requiredCountPerAttribute;
        if (averagePerAttribute == 0) {
            // if we have more attributes values than shard count, no leftover
            totalLeftover = 0;
            requiredCountPerAttribute = 1;
        } else {
            requiredCountPerAttribute = averagePerAttribute;
        }
        int leftoverPerAttribute = totalLeftover == 0 ? 0 : 1;

        int currentNodeCount = shardPerAttribute.get(node.node().getAttributes().get(awarenessAttribute));
        // if we are above with leftover, then we know we are not good, even with mod
        if (currentNodeCount > (requiredCountPerAttribute + leftoverPerAttribute)) {
            return allocation.decision(Decision.NO, NAME,
                    "there are too many copies of the shard allocated to nodes with attribute [%s], there are [%d] total configured " +
                    "shard copies for this shard id and [%d] total attribute values, expected the allocated shard count per " +
                    "attribute [%d] to be less than or equal to the upper bound of the required number of shards per attribute [%d]",
                    awarenessAttribute,
                    shardCount,
                    numberOfAttributes,
                    currentNodeCount,
                    requiredCountPerAttribute + leftoverPerAttribute);
        }
        // all is well, we are below or same as average
        if (currentNodeCount <= requiredCountPerAttribute) {
            continue;
        }
    }

    return allocation.decision(Decision.YES, NAME, "node meets all awareness attribute requirements");
}
 
Example 14
Source File: CancelAllocationCommand.java    From crate with Apache License 2.0 4 votes vote down vote up
@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(LogManager.getLogger(CancelAllocationCommand.class), shardRouting,
        new UnassignedInfo(UnassignedInfo.Reason.REROUTE_CANCELLED, null), indexMetaData, allocation.changes());
    // TODO: We don't have to remove a cancelled shard from in-sync set once we have a strict resync implementation.
    allocation.removeAllocationId(shardRouting);
    return new RerouteExplanation(this, allocation.decision(Decision.YES, "cancel_allocation_command",
            "shard " + shardId + " on node " + discoNode + " can be cancelled"));
}