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));
}
}
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());
}
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);
}
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());
}
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));
}
Aggregations