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

The following examples show how to use org.elasticsearch.cluster.routing.ShardRouting#unassigned() . 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: SnapshotsService.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private boolean waitingShardsStartedOrUnassigned(ClusterChangedEvent event) {
    SnapshotsInProgress curr = event.state().custom(SnapshotsInProgress.TYPE);
    if (curr != null) {
        for (SnapshotsInProgress.Entry entry : curr.entries()) {
            if (entry.state() == State.STARTED && !entry.waitingIndices().isEmpty()) {
                for (String index : entry.waitingIndices().keySet()) {
                    if (event.indexRoutingTableChanged(index)) {
                        IndexRoutingTable indexShardRoutingTable = event.state().getRoutingTable().index(index);
                        for (ShardId shardId : entry.waitingIndices().get(index)) {
                            ShardRouting shardRouting = indexShardRoutingTable.shard(shardId.id()).primaryShard();
                            if (shardRouting != null && (shardRouting.started() || shardRouting.unassigned())) {
                                return true;
                            }
                        }
                    }
                }
            }
        }
    }
    return false;
}
 
Example 2
Source File: TransportLeaderShardIngestAction.java    From elasticsearch-helper with Apache License 2.0 6 votes vote down vote up
private int findReplicaLevel(ShardIterator shardIt) {
    int replicaLevel = 0;
    shardIt.reset();
    ShardRouting shard;
    while ((shard = shardIt.nextOrNull()) != null) {
        if (shard.unassigned()) {
            continue;
        }
        boolean doOnlyOnRelocating = false;
        if (shard.primary()) {
            if (shard.relocating()) {
                doOnlyOnRelocating = true;
            } else {
                continue;
            }
        }
        String nodeId = !doOnlyOnRelocating ? shard.currentNodeId() : shard.relocating() ? shard.relocatingNodeId() : null;
        if (nodeId == null) {
            continue;
        }
        replicaLevel++;
    }
    return replicaLevel;
}
 
Example 3
Source File: BalancedShardsAllocator.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public ShardAllocationDecision decideShardAllocation(final ShardRouting shard, final RoutingAllocation allocation) {
    Balancer balancer = new Balancer(LOGGER, allocation, weightFunction, threshold);
    AllocateUnassignedDecision allocateUnassignedDecision = AllocateUnassignedDecision.NOT_TAKEN;
    MoveDecision moveDecision = MoveDecision.NOT_TAKEN;
    if (shard.unassigned()) {
        allocateUnassignedDecision = balancer.decideAllocateUnassigned(shard, Sets.newHashSet());
    } else {
        moveDecision = balancer.decideMove(shard);
        if (moveDecision.isDecisionTaken() && moveDecision.canRemain()) {
            MoveDecision rebalanceDecision = balancer.decideRebalance(shard);
            moveDecision = rebalanceDecision.withRemainDecision(moveDecision.getCanRemainDecision());
        }
    }
    return new ShardAllocationDecision(allocateUnassignedDecision, moveDecision);
}
 
Example 4
Source File: FilterAllocationDecider.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
    if (shardRouting.unassigned()) {
        // only for unassigned - we filter allocation right after the index creation ie. for shard shrinking etc. to ensure
        // that once it has been allocated post API the replicas can be allocated elsewhere without user interaction
        // this is a setting that can only be set within the system!
        IndexMetaData indexMd = allocation.metaData().getIndexSafe(shardRouting.index());
        DiscoveryNodeFilters initialRecoveryFilters = indexMd.getInitialRecoveryFilters();
        if (initialRecoveryFilters != null &&
            INITIAL_RECOVERY_TYPES.contains(shardRouting.recoverySource().getType()) &&
            initialRecoveryFilters.match(node.node()) == false) {
            String explanation = (shardRouting.recoverySource().getType() == RecoverySource.Type.LOCAL_SHARDS) ?
                "initial allocation of the shrunken index is only allowed on nodes [%s] that hold a copy of every shard in the index" :
                "initial allocation of the index is only allowed on nodes [%s]";
            return allocation.decision(Decision.NO, NAME, explanation, initialRecoveryFilters);
        }
    }
    return shouldFilter(shardRouting, node, allocation);
}
 
Example 5
Source File: SnapshotsService.java    From crate with Apache License 2.0 6 votes vote down vote up
private static boolean waitingShardsStartedOrUnassigned(SnapshotsInProgress snapshotsInProgress, ClusterChangedEvent event) {
    for (SnapshotsInProgress.Entry entry : snapshotsInProgress.entries()) {
        if (entry.state() == State.STARTED) {
            for (ObjectCursor<String> index : entry.waitingIndices().keys()) {
                if (event.indexRoutingTableChanged(index.value)) {
                    IndexRoutingTable indexShardRoutingTable = event.state().getRoutingTable().index(index.value);
                    for (ShardId shardId : entry.waitingIndices().get(index.value)) {
                        ShardRouting shardRouting = indexShardRoutingTable.shard(shardId.id()).primaryShard();
                        if (shardRouting != null && (shardRouting.started() || shardRouting.unassigned())) {
                            return true;
                        }
                    }
                }
            }
        }
    }
    return false;
}
 
Example 6
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 7
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 8
Source File: ReplicationOperationTests.java    From crate with Apache License 2.0 6 votes vote down vote up
private Set<ShardRouting> getExpectedReplicas(ShardId shardId, ClusterState state, Set<String> trackedShards) {
    Set<ShardRouting> expectedReplicas = new HashSet<>();
    String localNodeId = state.nodes().getLocalNodeId();
    if (state.routingTable().hasIndex(shardId.getIndexName())) {
        for (ShardRouting shardRouting : state.routingTable().shardRoutingTable(shardId)) {
            if (shardRouting.unassigned()) {
                continue;
            }
            if (localNodeId.equals(shardRouting.currentNodeId()) == false) {
                if (trackedShards.contains(shardRouting.allocationId().getId())) {
                    expectedReplicas.add(shardRouting);
                }
            }

            if (shardRouting.relocating() && localNodeId.equals(shardRouting.relocatingNodeId()) == false) {
                if (trackedShards.contains(shardRouting.getTargetRelocatingShard().allocationId().getId())) {
                    expectedReplicas.add(shardRouting.getTargetRelocatingShard());
                }
            }
        }
    }
    return expectedReplicas;
}
 
Example 9
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 10
Source File: PrimaryShardAllocator.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Is the allocator responsible for allocating the given {@link ShardRouting}?
 */
private static boolean isResponsibleFor(final ShardRouting shard) {
    return shard.primary() // must be primary
            && shard.unassigned() // must be unassigned
            // only handle either an existing store or a snapshot recovery
            && (shard.recoverySource().getType() == RecoverySource.Type.EXISTING_STORE
                || shard.recoverySource().getType() == RecoverySource.Type.SNAPSHOT);
}
 
Example 11
Source File: ReplicaShardAllocator.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Is the allocator responsible for allocating the given {@link ShardRouting}?
 */
private static boolean isResponsibleFor(final ShardRouting shard) {
    return shard.primary() == false // must be a replica
               && shard.unassigned() // must be unassigned
               // if we are allocating a replica because of index creation, no need to go and find a copy, there isn't one...
               && shard.unassignedInfo().getReason() != UnassignedInfo.Reason.INDEX_CREATED;
}
 
Example 12
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();
}