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

The following examples show how to use org.elasticsearch.cluster.routing.ShardRouting#active() . 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: DocTableInfo.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private void fillLocationsFromShardIterators(Map<String, Map<String, List<Integer>>> locations,
                                             GroupShardsIterator shardIterators,
                                             List<ShardId> missingShards) {
    ShardRouting shardRouting;
    for (ShardIterator shardIterator : shardIterators) {
        shardRouting = shardIterator.nextOrNull();
        if (shardRouting != null) {
            if (shardRouting.active()) {
                processShardRouting(locations, shardRouting);
            } else {
                missingShards.add(shardIterator.shardId());
            }
        } else {
            if (isPartitioned) {
                // if the table is partitioned maybe a index/shard just got newly created ...
                missingShards.add(shardIterator.shardId());
            } else {
                throw new UnavailableShardsException(shardIterator.shardId());
            }
        }
    }
}
 
Example 3
Source File: InsertFromValues.java    From crate with Apache License 2.0 6 votes vote down vote up
private static ShardLocation getShardLocation(String indexName,
                                              String id,
                                              @Nullable String routing,
                                              ClusterService clusterService) {
    ShardIterator shardIterator = clusterService.operationRouting().indexShards(
        clusterService.state(),
        indexName,
        id,
        routing);

    final String nodeId;
    ShardRouting shardRouting = shardIterator.nextOrNull();
    if (shardRouting == null) {
        nodeId = null;
    } else if (shardRouting.active() == false) {
        nodeId = shardRouting.relocatingNodeId();
    } else {
        nodeId = shardRouting.currentNodeId();
    }
    return new ShardLocation(shardIterator.shardId(), nodeId);
}
 
Example 4
Source File: BlobService.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * @param index  the name of blob-enabled index
 * @param digest sha-1 hash value of the file
 * @return null if no redirect is required, Otherwise the address to which should be redirected.
 */
public String getRedirectAddress(String index, String digest) throws MissingHTTPEndpointException {
    ShardIterator shards = clusterService.operationRouting().getShards(
        clusterService.state(), index, null, digest, "_local");

    String localNodeId = clusterService.localNode().getId();
    DiscoveryNodes nodes = clusterService.state().getNodes();
    ShardRouting shard;
    while ((shard = shards.nextOrNull()) != null) {
        if (!shard.active()) {
            continue;
        }
        if (shard.currentNodeId().equals(localNodeId)) {
            // no redirect required if the shard is on this node
            return null;
        }

        DiscoveryNode node = nodes.get(shard.currentNodeId());
        String httpAddress = node.getAttributes().get("http_address");
        if (httpAddress != null) {
            return httpAddress + "/_blobs/" + BlobIndex.stripPrefix(index) + "/" + digest;
        }
    }
    throw new MissingHTTPEndpointException("Can't find a suitable http server to serve the blob");
}
 
Example 5
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 6
Source File: DiskThresholdDecider.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the expected shard size for the given shard or the default value provided if not enough information are available
 * to estimate the shards size.
 */
public static long getExpectedShardSize(ShardRouting shard,
                                        long defaultValue,
                                        ClusterInfo clusterInfo,
                                        MetaData metaData,
                                        RoutingTable routingTable) {
    final IndexMetaData indexMetaData = metaData.getIndexSafe(shard.index());
    if (indexMetaData.getResizeSourceIndex() != null && shard.active() == false &&
        shard.recoverySource().getType() == RecoverySource.Type.LOCAL_SHARDS) {
        // in the shrink index case we sum up the source index shards since we basically make a copy of the shard in
        // the worst case
        long targetShardSize = 0;
        final Index mergeSourceIndex = indexMetaData.getResizeSourceIndex();
        final IndexMetaData sourceIndexMeta = metaData.index(mergeSourceIndex);
        if (sourceIndexMeta != null) {
            final Set<ShardId> shardIds = IndexMetaData.selectRecoverFromShards(shard.id(),
                sourceIndexMeta, indexMetaData.getNumberOfShards());
            for (IndexShardRoutingTable shardRoutingTable : routingTable.index(mergeSourceIndex.getName())) {
                if (shardIds.contains(shardRoutingTable.shardId())) {
                    targetShardSize += clusterInfo.getShardSize(shardRoutingTable.primaryShard(), 0);
                }
            }
        }
        return targetShardSize == 0 ? defaultValue : targetShardSize;
    } else {
        return clusterInfo.getShardSize(shard, defaultValue);
    }
}
 
Example 7
Source File: IndexMetaDataUpdater.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public void shardFailed(ShardRouting failedShard, UnassignedInfo unassignedInfo) {
    if (failedShard.active() && failedShard.primary()) {
        Updates updates = changes(failedShard.shardId());
        if (updates.firstFailedPrimary == null) {
            // more than one primary can be failed (because of batching, primary can be failed, replica promoted and then failed...)
            updates.firstFailedPrimary = failedShard;
        }
        increasePrimaryTerm(failedShard.shardId());
    }
}
 
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: TransportReplicationAction.java    From crate with Apache License 2.0 5 votes vote down vote up
private boolean retryIfUnavailable(ClusterState state, ShardRouting primary) {
    if (primary == null || primary.active() == false) {
        logger.trace("primary shard [{}] is not yet active, scheduling a retry: action [{}], request [{}], "
            + "cluster state version [{}]", request.shardId(), actionName, request, state.version());
        retryBecauseUnavailable(request.shardId(), "primary shard is not active");
        return true;
    }
    if (state.nodes().nodeExists(primary.currentNodeId()) == false) {
        logger.trace("primary shard [{}] is assigned to an unknown node [{}], scheduling a retry: action [{}], request [{}], "
            + "cluster state version [{}]", request.shardId(), primary.currentNodeId(), actionName, request, state.version());
        retryBecauseUnavailable(request.shardId(), "primary shard isn't assigned to a known node.");
        return true;
    }
    return false;
}
 
Example 10
Source File: GroupRowsByShard.java    From crate with Apache License 2.0 5 votes vote down vote up
@Nullable
private ShardLocation getShardLocation(String indexName, String id, @Nullable String routing) {
    try {
        ShardIterator shardIterator = clusterService.operationRouting().indexShards(
            clusterService.state(),
            indexName,
            id,
            routing
        );

        final String nodeId;
        ShardRouting shardRouting = shardIterator.nextOrNull();
        if (shardRouting == null) {
            nodeId = null;
        } else if (shardRouting.active() == false) {
            nodeId = shardRouting.relocatingNodeId();
        } else {
            nodeId = shardRouting.currentNodeId();
        }
        if (nodeId == null && LOGGER.isDebugEnabled()) {
            LOGGER.debug("Unable to get the node id for index {} and shard {}", indexName, id);
        }
        return new ShardLocation(shardIterator.shardId(), nodeId);
    } catch (IndexNotFoundException e) {
        if (!autoCreateIndices) {
            throw e;
        }
        return null;
    }
}
 
Example 11
Source File: IndexShardTestCase.java    From crate with Apache License 2.0 5 votes vote down vote up
public static void updateRoutingEntry(IndexShard shard, ShardRouting shardRouting) throws IOException {
    Set<String> inSyncIds =
        shardRouting.active() ? Collections.singleton(shardRouting.allocationId().getId()) : Collections.emptySet();
    IndexShardRoutingTable newRoutingTable = new IndexShardRoutingTable.Builder(shardRouting.shardId())
        .addShard(shardRouting)
        .build();
    shard.updateShardState(
        shardRouting,
        shard.getPendingPrimaryTerm(),
        null,
        currentClusterStateVersion.incrementAndGet(),
        inSyncIds,
        newRoutingTable);
}
 
Example 12
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 13
Source File: IndexMetaDataUpdater.java    From crate with Apache License 2.0 4 votes vote down vote up
/**
 * Remove allocation id of this shard from the set of in-sync shard copies
 */
void removeAllocationId(ShardRouting shardRouting) {
    if (shardRouting.active()) {
        changes(shardRouting.shardId()).removedAllocationIds.add(shardRouting.allocationId().getId());
    }
}