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

The following examples show how to use org.elasticsearch.cluster.routing.ShardRouting#primary() . 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: PeerRecoverySourceService.java    From crate with Apache License 2.0 7 votes vote down vote up
private void recover(StartRecoveryRequest request, ActionListener<RecoveryResponse> listener) throws IOException {
    final IndexService indexService = indicesService.indexServiceSafe(request.shardId().getIndex());
    final IndexShard shard = indexService.getShard(request.shardId().id());

    final ShardRouting routingEntry = shard.routingEntry();

    if (routingEntry.primary() == false || routingEntry.active() == false) {
        throw new DelayRecoveryException("source shard [" + routingEntry + "] is not an active primary");
    }

    if (request.isPrimaryRelocation()
        && (
            routingEntry.relocating() == false
            || routingEntry.relocatingNodeId().equals(request.targetNode().getId()) == false)) {
        LOGGER.debug(
            "delaying recovery of {} as source shard is not marked yet as relocating to {}",
            request.shardId(), request.targetNode());
        throw new DelayRecoveryException("source shard is not marked yet as relocating to [" + request.targetNode() + "]");
    }

    RecoverySourceHandler handler = ongoingRecoveries.addNewRecovery(request, shard);
    LOGGER.trace(
        "[{}][{}] starting recovery to {}",
        request.shardId().getIndex().getName(), request.shardId().id(), request.targetNode());
    handler.recoverToTarget(ActionListener.runAfter(listener, () -> ongoingRecoveries.remove(shard, handler)));
}
 
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: 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 4
Source File: SnapshotInProgressAllocationDecider.java    From crate with Apache License 2.0 6 votes vote down vote up
private Decision canMove(ShardRouting shardRouting, RoutingAllocation allocation) {
    if (shardRouting.primary()) {
        // Only primary shards are snapshotted

        SnapshotsInProgress snapshotsInProgress = allocation.custom(SnapshotsInProgress.TYPE);
        if (snapshotsInProgress == null || snapshotsInProgress.entries().isEmpty()) {
            // Snapshots are not running
            return allocation.decision(Decision.YES, NAME, "no snapshots are currently running");
        }

        for (SnapshotsInProgress.Entry snapshot : snapshotsInProgress.entries()) {
            SnapshotsInProgress.ShardSnapshotStatus shardSnapshotStatus = snapshot.shards().get(shardRouting.shardId());
            if (shardSnapshotStatus != null && !shardSnapshotStatus.state().completed() && shardSnapshotStatus.nodeId() != null &&
                    shardSnapshotStatus.nodeId().equals(shardRouting.currentNodeId())) {
                if (LOGGER.isTraceEnabled()) {
                    LOGGER.trace("Preventing snapshotted shard [{}] from being moved away from node [{}]",
                            shardRouting.shardId(), shardSnapshotStatus.nodeId());
                }
                return allocation.decision(Decision.THROTTLE, NAME,
                    "waiting for snapshotting of shard [%s] to complete on this node [%s]",
                    shardRouting.shardId(), shardSnapshotStatus.nodeId());
            }
        }
    }
    return allocation.decision(Decision.YES, NAME, "the shard is not being snapshotted");
}
 
Example 5
Source File: DecommissionAllocationDecider.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private Decision canRemainOrAllocate(String nodeId, ShardRouting shardRouting, RoutingAllocation allocation) {
    if (dataAvailability == DataAvailability.NONE) {
        return allocation.decision(Decision.YES, NAME, "dataAvailability=none");
    }

    if (decommissioningNodes.contains(nodeId)) {
        if (dataAvailability == DataAvailability.PRIMARIES && !shardRouting.primary()) {
            return allocation.decision(Decision.YES, NAME, "dataAvailability=primaries shard=replica decommissioned=true");
        }
        return allocation.decision(Decision.NO, NAME, "node is being decommissioned");
    }
    return allocation.decision(Decision.YES, NAME, "node isn't decommissioned");
}
 
Example 6
Source File: EnableAllocationDecider.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Decision canRebalance(ShardRouting shardRouting, RoutingAllocation allocation) {
    if (allocation.ignoreDisable()) {
        return allocation.decision(Decision.YES, NAME, "allocation is explicitly ignoring any disabling of relocation");
    }

    Settings indexSettings = allocation.metaData().getIndexSafe(shardRouting.index()).getSettings();
    final Rebalance enable;
    final boolean usedIndexSetting;
    if (INDEX_ROUTING_REBALANCE_ENABLE_SETTING.exists(indexSettings)) {
        enable = INDEX_ROUTING_REBALANCE_ENABLE_SETTING.get(indexSettings);
        usedIndexSetting = true;
    } else {
        enable = this.enableRebalance;
        usedIndexSetting = false;
    }
    switch (enable) {
        case ALL:
            return allocation.decision(Decision.YES, NAME, "all rebalancing is allowed");
        case NONE:
            return allocation.decision(Decision.NO, NAME, "no rebalancing is allowed due to %s", setting(enable, usedIndexSetting));
        case PRIMARIES:
            if (shardRouting.primary()) {
                return allocation.decision(Decision.YES, NAME, "primary rebalancing is allowed");
            } else {
                return allocation.decision(Decision.NO, NAME, "replica rebalancing is forbidden due to %s",
                                            setting(enable, usedIndexSetting));
            }
        case REPLICAS:
            if (shardRouting.primary() == false) {
                return allocation.decision(Decision.YES, NAME, "replica rebalancing is allowed");
            } else {
                return allocation.decision(Decision.NO, NAME, "primary rebalancing is forbidden due to %s",
                                            setting(enable, usedIndexSetting));
            }
        default:
            throw new IllegalStateException("Unknown rebalance option");
    }
}
 
Example 7
Source File: DecommissionAllocationDecider.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
    if (decommissioningNodes.contains(node.nodeId())
        && dataAvailability == DataAvailability.PRIMARIES
        && !shardRouting.primary()) {

        // if primaries are removed from this node it will try to re-balance non-primaries onto this node
        // prevent this - replicas that are already here can remain, but no new replicas should be assigned
        return allocation.decision(Decision.NO, NAME, "dataAvailability=primaries, shard=replica, decommissioned=true");
    }
    return canRemainOrAllocate(node.nodeId(), shardRouting, allocation);
}
 
Example 8
Source File: DecommissionAllocationDecider.java    From crate with Apache License 2.0 5 votes vote down vote up
private Decision canRemainOrAllocate(String nodeId, ShardRouting shardRouting, RoutingAllocation allocation) {
    if (dataAvailability == DataAvailability.NONE) {
        return allocation.decision(Decision.YES, NAME, "dataAvailability=none");
    }

    if (decommissioningNodes.contains(nodeId)) {
        if (dataAvailability == DataAvailability.PRIMARIES && !shardRouting.primary()) {
            return allocation.decision(Decision.YES, NAME, "dataAvailability=primaries shard=replica decommissioned=true");
        }
        return allocation.decision(Decision.NO, NAME, "node is being decommissioned");
    }
    return allocation.decision(Decision.YES, NAME, "node isn't decommissioned");
}
 
Example 9
Source File: RestoreService.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public void shardStarted(ShardRouting initializingShard, ShardRouting startedShard) {
    // mark snapshot as completed
    if (initializingShard.primary()) {
        RecoverySource recoverySource = initializingShard.recoverySource();
        if (recoverySource.getType() == RecoverySource.Type.SNAPSHOT) {
            Snapshot snapshot = ((SnapshotRecoverySource) recoverySource).snapshot();
            changes(snapshot).shards.put(initializingShard.shardId(),
                new ShardRestoreStatus(initializingShard.currentNodeId(), RestoreInProgress.State.SUCCESS));
        }
    }
}
 
Example 10
Source File: DisableAllocationDecider.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
    if (allocation.ignoreDisable()) {
        return allocation.decision(Decision.YES, NAME, "allocation disabling is ignored");
    }
    Settings indexSettings = allocation.routingNodes().metaData().index(shardRouting.index()).getSettings();
    if (shardRouting.primary() && shardRouting.allocatedPostIndexCreate() == false) {
        // if its primary, and it hasn't been allocated post API (meaning its a "fresh newly created shard"), only disable allocation
        // on a special disable allocation flag
        if (indexSettings.getAsBoolean(INDEX_ROUTING_ALLOCATION_DISABLE_NEW_ALLOCATION, disableNewAllocation)) {
            return allocation.decision(Decision.NO, NAME, "new primary allocation is disabled");
        } else {
            return allocation.decision(Decision.YES, NAME, "new primary allocation is enabled");
        }
    }
    if (indexSettings.getAsBoolean(INDEX_ROUTING_ALLOCATION_DISABLE_ALLOCATION, disableAllocation)) {
        return allocation.decision(Decision.NO, NAME, "all allocation is disabled");
    }
    if (indexSettings.getAsBoolean(INDEX_ROUTING_ALLOCATION_DISABLE_REPLICA_ALLOCATION, disableReplicaAllocation)) {
        if (shardRouting.primary()) {
            return allocation.decision(Decision.YES, NAME, "primary allocation is enabled");
        } else {
            return allocation.decision(Decision.NO, NAME, "replica allocation is disabled");
        }
    }
    return allocation.decision(Decision.YES, NAME, "all allocation is enabled");
}
 
Example 11
Source File: EnableAllocationDecider.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
    if (allocation.ignoreDisable()) {
        return allocation.decision(Decision.YES, NAME, "allocation disabling is ignored");
    }

    Settings indexSettings = allocation.routingNodes().metaData().index(shardRouting.index()).getSettings();
    String enableIndexValue = indexSettings.get(INDEX_ROUTING_ALLOCATION_ENABLE);
    final Allocation enable;
    if (enableIndexValue != null) {
        enable = Allocation.parse(enableIndexValue);
    } else {
        enable = this.enableAllocation;
    }
    switch (enable) {
        case ALL:
            return allocation.decision(Decision.YES, NAME, "all allocations are allowed");
        case NONE:
            return allocation.decision(Decision.NO, NAME, "no allocations are allowed");
        case NEW_PRIMARIES:
            if (shardRouting.primary() && shardRouting.allocatedPostIndexCreate() == false) {
                return allocation.decision(Decision.YES, NAME, "new primary allocations are allowed");
            } else {
                return allocation.decision(Decision.NO, NAME, "non-new primary allocations are forbidden");
            }
        case PRIMARIES:
            if (shardRouting.primary()) {
                return allocation.decision(Decision.YES, NAME, "primary allocations are allowed");
            } else {
                return allocation.decision(Decision.NO, NAME, "replica allocations are forbidden");
            }
        default:
            throw new IllegalStateException("Unknown allocation option");
    }
}
 
Example 12
Source File: BalancedShardsAllocator.java    From crate with Apache License 2.0 5 votes vote down vote up
public int highestPrimary() {
    if (highestPrimary == -1) {
        int maxId = -1;
        for (ShardRouting shard : shards) {
            if (shard.primary()) {
                maxId = Math.max(maxId, shard.id());
            }
        }
        return highestPrimary = maxId;
    }
    return highestPrimary;
}
 
Example 13
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 14
Source File: GatewayAllocator.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Computes and returns the design for allocating a single unassigned shard.  If called on an assigned shard,
 * {@link AllocateUnassignedDecision#NOT_TAKEN} is returned.
 */
public AllocateUnassignedDecision decideUnassignedShardAllocation(ShardRouting unassignedShard, RoutingAllocation routingAllocation) {
    if (unassignedShard.primary()) {
        return primaryShardAllocator.makeAllocationDecision(unassignedShard, routingAllocation, LOGGER);
    } else {
        return replicaShardAllocator.makeAllocationDecision(unassignedShard, routingAllocation, LOGGER);
    }
}
 
Example 15
Source File: TransportIndicesShardStoresAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private IndicesShardStoresResponse.StoreStatus.Allocation getAllocation(String index, int shardID, DiscoveryNode node) {
    for (ShardRouting shardRouting : routingNodes.node(node.id())) {
        ShardId shardId = shardRouting.shardId();
        if (shardId.id() == shardID && shardId.getIndex().equals(index)) {
            if (shardRouting.primary()) {
                return IndicesShardStoresResponse.StoreStatus.Allocation.PRIMARY;
            } else if (shardRouting.assignedToNode()) {
                return IndicesShardStoresResponse.StoreStatus.Allocation.REPLICA;
            } else {
                return IndicesShardStoresResponse.StoreStatus.Allocation.UNUSED;
            }
        }
    }
    return IndicesShardStoresResponse.StoreStatus.Allocation.UNUSED;
}
 
Example 16
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"));
}
 
Example 17
Source File: EnableAllocationDecider.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
    if (allocation.ignoreDisable()) {
        return allocation.decision(Decision.YES, NAME,
            "explicitly ignoring any disabling of allocation due to manual allocation commands via the reroute API");
    }

    final IndexMetaData indexMetaData = allocation.metaData().getIndexSafe(shardRouting.index());
    final Allocation enable;
    final boolean usedIndexSetting;
    if (INDEX_ROUTING_ALLOCATION_ENABLE_SETTING.exists(indexMetaData.getSettings())) {
        enable = INDEX_ROUTING_ALLOCATION_ENABLE_SETTING.get(indexMetaData.getSettings());
        usedIndexSetting = true;
    } else {
        enable = this.enableAllocation;
        usedIndexSetting = false;
    }
    switch (enable) {
        case ALL:
            return allocation.decision(Decision.YES, NAME, "all allocations are allowed");
        case NONE:
            return allocation.decision(Decision.NO, NAME, "no allocations are allowed due to %s", setting(enable, usedIndexSetting));
        case NEW_PRIMARIES:
            if (shardRouting.primary() && shardRouting.active() == false &&
                shardRouting.recoverySource().getType() != RecoverySource.Type.EXISTING_STORE) {
                return allocation.decision(Decision.YES, NAME, "new primary allocations are allowed");
            } else {
                return allocation.decision(Decision.NO, NAME, "non-new primary allocations are forbidden due to %s",
                                            setting(enable, usedIndexSetting));
            }
        case PRIMARIES:
            if (shardRouting.primary()) {
                return allocation.decision(Decision.YES, NAME, "primary allocations are allowed");
            } else {
                return allocation.decision(Decision.NO, NAME, "replica allocations are forbidden due to %s",
                                            setting(enable, usedIndexSetting));
            }
        default:
            throw new IllegalStateException("Unknown allocation option");
    }
}
 
Example 18
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 19
Source File: IndexShard.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Updates the shards routing entry. This mutate the shards internal state depending
 * on the changes that get introduced by the new routing value. This method will persist shard level metadata
 * unless explicitly disabled.
 */
public void updateRoutingEntry(final ShardRouting newRouting, final boolean persistState) {
    final ShardRouting currentRouting = this.shardRouting;
    if (!newRouting.shardId().equals(shardId())) {
        throw new IllegalArgumentException("Trying to set a routing entry with shardId [" + newRouting.shardId() + "] on a shard with" +
                " shardId [" + shardId() + "]");
    }
    if ((currentRouting == null || newRouting.isSameAllocation(currentRouting)) == false) {
        throw new IllegalArgumentException("Trying to set a routing entry with a different allocation. Current " + currentRouting +
                ", new " + newRouting);
    }
    try {
        if (engineUnsafe() != null) {
            if (engineUnsafe() instanceof DLBasedEngine) {
                ((DLBasedEngine)engineUnsafe()).setIsPrimaryInRouting(newRouting.primary());
            }
        }
        if (currentRouting != null) {
            if (!newRouting.primary() && currentRouting.primary()) {
                logger.warn("suspect illegal state: trying to move shard from primary mode to replica mode");
            }
            // if its the same routing except for some metadata info, return
            if (currentRouting.equalsIgnoringMetaData(newRouting)) {
                this.shardRouting = newRouting; // might have a new version
                return;
            }
        }

        if (state == IndexShardState.POST_RECOVERY) {
            // if the state is started or relocating (cause it might move right away from started to relocating)
            // then move to STARTED
            if (newRouting.state() == ShardRoutingState.STARTED || newRouting.state() == ShardRoutingState.RELOCATING) {
                // we want to refresh *before* we move to internal STARTED state
                try {
                    engine().refresh("cluster_state_started");
                } catch (Throwable t) {
                    logger.debug("failed to refresh due to move to cluster wide started", t);
                }

                boolean movedToStarted = false;
                synchronized (mutex) {
                    // do the check under a mutex, so we make sure to only change to STARTED if in POST_RECOVERY
                    if (state == IndexShardState.POST_RECOVERY) {
                        changeState(IndexShardState.STARTED, "global state is [" + newRouting.state() + "]");
                        movedToStarted = true;
                    } else {
                        logger.debug("state [{}] not changed, not in POST_RECOVERY, global state is [{}]", state, newRouting.state());
                    }
                }
                if (movedToStarted) {
                    indicesLifecycle.afterIndexShardStarted(this);
                }
            }
        }
        this.shardRouting = newRouting;
        indicesLifecycle.shardRoutingChanged(this, currentRouting, newRouting);
    } finally {
        if (persistState) {
            persistMetadata(newRouting, currentRouting);
        }
    }
}
 
Example 20
Source File: ClusterInfo.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * Method that incorporates the ShardId for the shard into a string that
 * includes a 'p' or 'r' depending on whether the shard is a primary.
 */
static String shardIdentifierFromRouting(ShardRouting shardRouting) {
    return shardRouting.shardId().toString() + "[" + (shardRouting.primary() ? "p" : "r") + "]";
}