Java Code Examples for org.elasticsearch.cluster.routing.ShardIterator#nextOrNull()

The following examples show how to use org.elasticsearch.cluster.routing.ShardIterator#nextOrNull() . 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: BlobTableInfo.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public Routing getRouting(WhereClause whereClause, @Nullable String preference) {
    Map<String, Map<String, List<Integer>>> locations = new TreeMap<>();
    GroupShardsIterator shardIterators = clusterService.operationRouting().searchShards(
            clusterService.state(),
            new String[]{index},
            null,
            preference
    );
    ShardRouting shardRouting;
    for (ShardIterator shardIterator : shardIterators) {
        shardRouting = shardIterator.nextOrNull();
        processShardRouting(locations, shardRouting, shardIterator.shardId());
    }

    return new Routing(locations);
}
 
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: AbstractSearchAsyncAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public void start() {
    if (expectedSuccessfulOps == 0) {
        // no search shards to search on, bail with empty response (it happens with search across _all with no indices around and consistent with broadcast operations)
        listener.onResponse(new SearchResponse(InternalSearchResponse.empty(), null, 0, 0, buildTookInMillis(), ShardSearchFailure.EMPTY_ARRAY));
        return;
    }
    int shardIndex = -1;
    for (final ShardIterator shardIt : shardsIts) {
        shardIndex++;
        final ShardRouting shard = shardIt.nextOrNull();
        if (shard != null) {
            performFirstPhase(shardIndex, shardIt, shard);
        } else {
            // really, no shards active in this group
            onFirstPhaseResult(shardIndex, null, null, shardIt, new NoShardAvailableActionException(shardIt.shardId()));
        }
    }
}
 
Example 4
Source File: TransportBroadcastAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public void start() {
    if (shardsIts.size() == 0) {
        // no shards
        try {
            listener.onResponse(newResponse(request, new AtomicReferenceArray(0), clusterState));
        } catch (Throwable e) {
            listener.onFailure(e);
        }
        return;
    }
    // count the local operations, and perform the non local ones
    int shardIndex = -1;
    for (final ShardIterator shardIt : shardsIts) {
        shardIndex++;
        final ShardRouting shard = shardIt.nextOrNull();
        if (shard != null) {
            performOperation(shardIt, shard, shardIndex);
        } else {
            // really, no shards active in this group
            onOperation(null, shardIt, shardIndex, new NoShardAvailableActionException(shardIt.shardId()));
        }
    }
}
 
Example 5
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 6
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 7
Source File: SysShardsTableInfo.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves the routing for sys.shards
 * <p>
 * This routing contains ALL shards of ALL indices.
 * Any shards that are not yet assigned to a node will have a NEGATIVE shard id (see {@link UnassignedShard}
 */
public static Routing getRouting(ClusterState clusterState, RoutingProvider routingProvider, SessionContext sessionContext) {
    String[] concreteIndices = Arrays.stream(clusterState.metaData().getConcreteAllOpenIndices())
        .filter(index -> !IndexParts.isDangling(index))
        .toArray(String[]::new);
    User user = sessionContext != null ? sessionContext.user() : null;
    if (user != null) {
        List<String> accessibleTables = new ArrayList<>(concreteIndices.length);
        for (String indexName : concreteIndices) {
            String tableName = RelationName.fqnFromIndexName(indexName);
            if (user.hasAnyPrivilege(Privilege.Clazz.TABLE, tableName)) {
                accessibleTables.add(indexName);
            }
        }
        concreteIndices = accessibleTables.toArray(new String[0]);
    }

    Map<String, Map<String, IntIndexedContainer>> locations = new TreeMap<>();
    GroupShardsIterator<ShardIterator> groupShardsIterator =
        clusterState.getRoutingTable().allAssignedShardsGrouped(concreteIndices, true, true);
    for (final ShardIterator shardIt : groupShardsIterator) {
        final ShardRouting shardRouting = shardIt.nextOrNull();
        processShardRouting(clusterState.getNodes().getLocalNodeId(), locations, shardRouting, shardIt.shardId());
    }
    return new Routing(locations);
}
 
Example 8
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 9
Source File: SysShardsTableInfo.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves the routing for sys.shards
 *
 * This routing contains ALL shards of ALL indices.
 * Any shards that are not yet assigned to a node will have a NEGATIVE shard id (see {@link UnassignedShard}
 */
@Override
public Routing getRouting(WhereClause whereClause, @Nullable String preference) {
    // TODO: filter on whereClause
    Map<String, Map<String, List<Integer>>> locations = new TreeMap<>();
    ClusterState state = service.state();
    String[] concreteIndices = state.metaData().concreteAllIndices();
    GroupShardsIterator groupShardsIterator = state.getRoutingTable().allAssignedShardsGrouped(concreteIndices, true, true);
    for (final ShardIterator shardIt : groupShardsIterator) {
        final ShardRouting shardRouting = shardIt.nextOrNull();
        processShardRouting(locations, shardRouting, shardIt.shardId());
    }
    return new Routing(locations);
}
 
Example 10
Source File: TransportBroadcastAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked"})
void onOperation(@Nullable ShardRouting shard, final ShardIterator shardIt, int shardIndex, Throwable t) {
    // we set the shard failure always, even if its the first in the replication group, and the next one
    // will work (it will just override it...)
    setFailure(shardIt, shardIndex, t);
    ShardRouting nextShard = shardIt.nextOrNull();
    if (nextShard != null) {
        if (t != null) {
            if (logger.isTraceEnabled()) {
                if (!TransportActions.isShardNotAvailableException(t)) {
                    logger.trace("{}: failed to execute [{}]", t, shard != null ? shard.shortSummary() : shardIt.shardId(), request);
                }
            }
        }
        performOperation(shardIt, nextShard, shardIndex);
    } else {
        if (logger.isDebugEnabled()) {
            if (t != null) {
                if (!TransportActions.isShardNotAvailableException(t)) {
                    logger.debug("{}: failed to execute [{}]", t, shard != null ? shard.shortSummary() : shardIt.shardId(), request);
                }
            }
        }
        if (expectedOps == counterOps.incrementAndGet()) {
            finishHim();
        }
    }
}
 
Example 11
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 12
Source File: RoutingProvider.java    From crate with Apache License 2.0 5 votes vote down vote up
private static void fillLocationsFromShardIterator(boolean ignoreMissingShards,
                                                   Map<String, Map<String, IntIndexedContainer>> locations,
                                                   ShardIterator shardIterator) {
    ShardRouting shardRouting = shardIterator.nextOrNull();
    if (shardRouting == null) {
        if (ignoreMissingShards) {
            return;
        }
        throw new UnavailableShardsException(shardIterator.shardId());
    }
    processShardRouting(locations, shardRouting);
}
 
Example 13
Source File: AbstractSearchAsyncAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
void onFirstPhaseResult(final int shardIndex, @Nullable ShardRouting shard, @Nullable String nodeId, final ShardIterator shardIt, Throwable t) {
    // we always add the shard failure for a specific shard instance
    // we do make sure to clean it on a successful response from a shard
    SearchShardTarget shardTarget = new SearchShardTarget(nodeId, shardIt.shardId().getIndex(), shardIt.shardId().getId());
    addShardFailure(shardIndex, shardTarget, t);

    if (totalOps.incrementAndGet() == expectedTotalOps) {
        if (logger.isDebugEnabled()) {
            if (t != null && !TransportActions.isShardNotAvailableException(t)) {
                if (shard != null) {
                    logger.debug(shard.shortSummary() + ": Failed to execute [" + request + "]", t);
                } else {
                    logger.debug(shardIt.shardId() + ": Failed to execute [" + request + "]", t);
                }
            } else if (logger.isTraceEnabled()) {
                logger.trace("{}: Failed to execute [{}]", t, shard, request);
            }
        }
        if (successfulOps.get() == 0) {
            if (logger.isDebugEnabled()) {
                logger.debug("All shards failed for phase: [{}]", t, firstPhaseName());
            }
            // no successful ops, raise an exception
            raiseEarlyFailure(new SearchPhaseExecutionException(firstPhaseName(), "all shards failed", buildShardFailures()));
        } else {
            try {
                innerMoveToSecondPhase();
            } catch (Throwable e) {
                raiseEarlyFailure(new ReduceSearchPhaseException(firstPhaseName(), "", e, buildShardFailures()));
            }
        }
    } else {
        final ShardRouting nextShard = shardIt.nextOrNull();
        final boolean lastShard = nextShard == null;
        // trace log this exception
        if (logger.isTraceEnabled()) {
            logger.trace(executionFailureMsg(shard, shardIt, request, lastShard), t);
        }
        if (!lastShard) {
            try {
                performFirstPhase(shardIndex, shardIt, nextShard);
            } catch (Throwable t1) {
                onFirstPhaseResult(shardIndex, shard, shard.currentNodeId(), shardIt, t1);
            }
        } else {
            // no more shards active, add a failure
            if (logger.isDebugEnabled() && !logger.isTraceEnabled()) { // do not double log this exception
                if (t != null && !TransportActions.isShardNotAvailableException(t)) {
                    logger.debug(executionFailureMsg(shard, shardIt, request, lastShard), t);
                }
            }
        }
    }
}