Search in sources :

Example 11 with ShardIterator

use of org.elasticsearch.cluster.routing.ShardIterator in project elasticsearch by elastic.

the class TransportSearchAction method doExecute.

@Override
protected void doExecute(Task task, SearchRequest searchRequest, ActionListener<SearchResponse> listener) {
    // pure paranoia if time goes backwards we are at least positive
    final long startTimeInMillis = Math.max(0, System.currentTimeMillis());
    final String[] localIndices;
    final Map<String, List<String>> remoteClusterIndices;
    final ClusterState clusterState = clusterService.state();
    if (remoteClusterService.isCrossClusterSearchEnabled()) {
        remoteClusterIndices = // empty string is not allowed
        remoteClusterService.groupClusterIndices(// empty string is not allowed
        searchRequest.indices(), idx -> indexNameExpressionResolver.hasIndexOrAlias(idx, clusterState));
        List<String> remove = remoteClusterIndices.remove(RemoteClusterService.LOCAL_CLUSTER_GROUP_KEY);
        localIndices = remove == null ? Strings.EMPTY_ARRAY : remove.toArray(new String[remove.size()]);
    } else {
        remoteClusterIndices = Collections.emptyMap();
        localIndices = searchRequest.indices();
    }
    if (remoteClusterIndices.isEmpty()) {
        executeSearch((SearchTask) task, startTimeInMillis, searchRequest, localIndices, Collections.emptyList(), (nodeId) -> null, clusterState, Collections.emptyMap(), listener);
    } else {
        remoteClusterService.collectSearchShards(searchRequest, remoteClusterIndices, ActionListener.wrap((searchShardsResponses) -> {
            List<ShardIterator> remoteShardIterators = new ArrayList<>();
            Map<String, AliasFilter> remoteAliasFilters = new HashMap<>();
            Function<String, Transport.Connection> connectionFunction = remoteClusterService.processRemoteShards(searchShardsResponses, remoteShardIterators, remoteAliasFilters);
            executeSearch((SearchTask) task, startTimeInMillis, searchRequest, localIndices, remoteShardIterators, connectionFunction, clusterState, remoteAliasFilters, listener);
        }, listener::onFailure));
    }
}
Also used : ShardIterator(org.elasticsearch.cluster.routing.ShardIterator) Property(org.elasticsearch.common.settings.Setting.Property) ClusterService(org.elasticsearch.cluster.service.ClusterService) HashMap(java.util.HashMap) Index(org.elasticsearch.index.Index) Function(java.util.function.Function) SearchService(org.elasticsearch.search.SearchService) GroupShardsIterator(org.elasticsearch.cluster.routing.GroupShardsIterator) Strings(org.elasticsearch.common.Strings) Inject(org.elasticsearch.common.inject.Inject) ArrayList(java.util.ArrayList) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Settings(org.elasticsearch.common.settings.Settings) HandledTransportAction(org.elasticsearch.action.support.HandledTransportAction) Map(java.util.Map) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TransportService(org.elasticsearch.transport.TransportService) ClusterBlockLevel(org.elasticsearch.cluster.block.ClusterBlockLevel) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) ActionFilters(org.elasticsearch.action.support.ActionFilters) Transport(org.elasticsearch.transport.Transport) Setting(org.elasticsearch.common.settings.Setting) Executor(java.util.concurrent.Executor) Set(java.util.Set) QUERY_THEN_FETCH(org.elasticsearch.action.search.SearchType.QUERY_THEN_FETCH) AliasFilter(org.elasticsearch.search.internal.AliasFilter) List(java.util.List) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) Task(org.elasticsearch.tasks.Task) Collections(java.util.Collections) ActionListener(org.elasticsearch.action.ActionListener) ClusterState(org.elasticsearch.cluster.ClusterState) Function(java.util.function.Function) ArrayList(java.util.ArrayList) List(java.util.List) Transport(org.elasticsearch.transport.Transport) HashMap(java.util.HashMap) Map(java.util.Map)

Example 12 with ShardIterator

use of org.elasticsearch.cluster.routing.ShardIterator in project elasticsearch by elastic.

the class TransportUpdateAction method shards.

@Override
protected ShardIterator shards(ClusterState clusterState, UpdateRequest request) {
    if (request.getShardId() != null) {
        return clusterState.routingTable().index(request.concreteIndex()).shard(request.getShardId().getId()).primaryShardIt();
    }
    ShardIterator shardIterator = clusterService.operationRouting().indexShards(clusterState, request.concreteIndex(), request.id(), request.routing());
    ShardRouting shard;
    while ((shard = shardIterator.nextOrNull()) != null) {
        if (shard.primary()) {
            return new PlainShardIterator(shardIterator.shardId(), Collections.singletonList(shard));
        }
    }
    return new PlainShardIterator(shardIterator.shardId(), Collections.emptyList());
}
Also used : PlainShardIterator(org.elasticsearch.cluster.routing.PlainShardIterator) ShardIterator(org.elasticsearch.cluster.routing.ShardIterator) PlainShardIterator(org.elasticsearch.cluster.routing.PlainShardIterator) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 13 with ShardIterator

use of org.elasticsearch.cluster.routing.ShardIterator in project crate by crate.

the class SysShardsTableInfo method getRouting.

/**
     * 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}
     */
@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);
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) GroupShardsIterator(org.elasticsearch.cluster.routing.GroupShardsIterator) ShardIterator(org.elasticsearch.cluster.routing.ShardIterator) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) TreeMap(java.util.TreeMap) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 14 with ShardIterator

use of org.elasticsearch.cluster.routing.ShardIterator in project crate by crate.

the class TransportPutChunkAction method resolveRequest.

@Override
protected void resolveRequest(MetaData metaData, String concreteIndex, PutChunkRequest request) {
    ShardIterator shardIterator = clusterService.operationRouting().indexShards(clusterService.state(), concreteIndex, null, null, request.digest());
    request.setShardId(shardIterator.shardId());
}
Also used : ShardIterator(org.elasticsearch.cluster.routing.ShardIterator)

Example 15 with ShardIterator

use of org.elasticsearch.cluster.routing.ShardIterator in project crate by crate.

the class BulkShardProcessorTest method mockShard.

private void mockShard(OperationRouting operationRouting, Integer shardId) {
    ShardIterator shardIterator = mock(ShardIterator.class);
    when(operationRouting.indexShards(any(ClusterState.class), anyString(), anyString(), Matchers.eq(shardId.toString()), anyString())).thenReturn(shardIterator);
    when(shardIterator.shardId()).thenReturn(new ShardId("foo", shardId));
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ClusterState(org.elasticsearch.cluster.ClusterState) ShardIterator(org.elasticsearch.cluster.routing.ShardIterator)

Aggregations

ShardIterator (org.elasticsearch.cluster.routing.ShardIterator)30 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)22 GroupShardsIterator (org.elasticsearch.cluster.routing.GroupShardsIterator)16 ClusterState (org.elasticsearch.cluster.ClusterState)14 PlainShardIterator (org.elasticsearch.cluster.routing.PlainShardIterator)11 HashMap (java.util.HashMap)7 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)7 Index (org.elasticsearch.index.Index)7 ArrayList (java.util.ArrayList)6 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)6 MetaData (org.elasticsearch.cluster.metadata.MetaData)6 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)6 ShardId (org.elasticsearch.index.shard.ShardId)6 AliasFilter (org.elasticsearch.search.internal.AliasFilter)6 HashSet (java.util.HashSet)4 Map (java.util.Map)4 Set (java.util.Set)4 ActionListener (org.elasticsearch.action.ActionListener)4 Transport (org.elasticsearch.transport.Transport)4 Path (java.nio.file.Path)3