org.elasticsearch.cluster.routing.GroupShardsIterator Java Examples

The following examples show how to use org.elasticsearch.cluster.routing.GroupShardsIterator. 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 GroupShardsIterator getShardIterators(WhereClause whereClause,
                                              @Nullable String preference,
                                              ClusterState clusterState) throws IndexNotFoundException {
    String[] routingIndices = concreteIndices;
    if (whereClause.partitions().size() > 0) {
        routingIndices = whereClause.partitions().toArray(new String[whereClause.partitions().size()]);
    }

    Map<String, Set<String>> routingMap = null;
    if (whereClause.clusteredBy().isPresent()) {
        routingMap = indexNameExpressionResolver.resolveSearchRouting(
                clusterState, whereClause.routingValues(), routingIndices);
    }
    return clusterService.operationRouting().searchShards(
            clusterState,
            routingIndices,
            routingMap,
            preference
    );
}
 
Example #3
Source File: DocTableInfo.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Nullable
private Routing getRouting(ClusterState state, WhereClause whereClause, String preference, final List<ShardId> missingShards) {
    final Map<String, Map<String, List<Integer>>> locations = new TreeMap<>();
    GroupShardsIterator shardIterators;
    try {
        shardIterators = getShardIterators(whereClause, preference, state);
    } catch (IndexNotFoundException e) {
        return new Routing(locations);
    }

    fillLocationsFromShardIterators(locations, shardIterators, missingShards);

    if (missingShards.isEmpty()) {
        return new Routing(locations);
    } else {
        return null;
    }
}
 
Example #4
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 #5
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 #6
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 #7
Source File: AbstractTransportSearchIntoAction.java    From elasticsearch-inout-plugin with Apache License 2.0 5 votes vote down vote up
@Override
protected GroupShardsIterator shards(ClusterState clusterState,
        SearchIntoRequest request, String[] concreteIndices) {
    Map<String, Set<String>> routingMap = clusterState.metaData()
            .resolveSearchRouting(request.routing(), request.indices());
    return clusterService.operationRouting().searchShards(clusterState,
            request.indices(), concreteIndices, routingMap,
            request.preference());
}
 
Example #8
Source File: TransportExistsAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected GroupShardsIterator shards(ClusterState clusterState, ExistsRequest request, String[] concreteIndices) {
    Map<String, Set<String>> routingMap = indexNameExpressionResolver.resolveSearchRouting(clusterState, request.routing(), request.indices());
    return clusterService.operationRouting().searchShards(clusterState, concreteIndices, routingMap, request.preference());
}
 
Example #9
Source File: TransportValidateQueryAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected GroupShardsIterator shards(ClusterState clusterState, ValidateQueryRequest request, String[] concreteIndices) {
    // Hard-code routing to limit request to a single shard, but still, randomize it...
    Map<String, Set<String>> routingMap = indexNameExpressionResolver.resolveSearchRouting(clusterState, Integer.toString(ThreadLocalRandom.current().nextInt(1000)), request.indices());
    return clusterService.operationRouting().searchShards(clusterState, concreteIndices, routingMap, "_local");
}
 
Example #10
Source File: TransportFieldStatsTransportAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected GroupShardsIterator shards(ClusterState clusterState, FieldStatsRequest request, String[] concreteIndices) {
    return clusterService.operationRouting().searchShards(clusterState, concreteIndices, null, null);
}
 
Example #11
Source File: TransportPercolateAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected GroupShardsIterator shards(ClusterState clusterState, PercolateRequest request, String[] concreteIndices) {
    Map<String, Set<String>> routingMap = indexNameExpressionResolver.resolveSearchRouting(clusterState, request.routing(), request.indices());
    return clusterService.operationRouting().searchShards(clusterState, concreteIndices, routingMap, request.preference());
}
 
Example #12
Source File: TransportSuggestAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected GroupShardsIterator shards(ClusterState clusterState, SuggestRequest request, String[] concreteIndices) {
    Map<String, Set<String>> routingMap = indexNameExpressionResolver.resolveSearchRouting(clusterState, request.routing(), request.indices());
    return clusterService.operationRouting().searchShards(clusterState, concreteIndices, routingMap, request.preference());
}
 
Example #13
Source File: TransportDfsOnlyAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected GroupShardsIterator shards(ClusterState clusterState, DfsOnlyRequest request, String[] concreteIndices) {
    Map<String, Set<String>> routingMap = indexNameExpressionResolver.resolveSearchRouting(clusterState, request.routing(), request.indices());
    return clusterService.operationRouting().searchShards(clusterState, concreteIndices, routingMap, request.preference());
}
 
Example #14
Source File: TransportTermsByQueryAction.java    From siren-join with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * The shards this request will execute against.
 */
@Override
protected GroupShardsIterator shards(ClusterState clusterState, TermsByQueryRequest request, String[] concreteIndices) {
  Map<String, Set<String>> routingMap = indexNameExpressionResolver.resolveSearchRouting(clusterState, request.routing(), request.indices());
  return clusterService.operationRouting().searchShards(clusterState, concreteIndices, routingMap, request.preference());
}
 
Example #15
Source File: AbstractTransportExportAction.java    From elasticsearch-inout-plugin with Apache License 2.0 4 votes vote down vote up
@Override
protected GroupShardsIterator shards(ClusterState clusterState, ExportRequest request, String[] concreteIndices) {
    Map<String, Set<String>> routingMap = clusterState.metaData().resolveSearchRouting(request.routing(), request.indices());
    return clusterService.operationRouting().searchShards(clusterState, request.indices(), concreteIndices, routingMap, request.preference());
}
 
Example #16
Source File: TransportBroadcastAction.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * Determines the shards this operation will be executed on. The operation is executed once per shard iterator, typically
 * on the first shard in it. If the operation fails, it will be retried on the next shard in the iterator.
 */
protected abstract GroupShardsIterator shards(ClusterState clusterState, Request request, String[] concreteIndices);