use of org.elasticsearch.cluster.routing.GroupShardsIterator in project crate by crate.
the class DocTableInfo method getRouting.
@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;
}
}
use of org.elasticsearch.cluster.routing.GroupShardsIterator in project crate by crate.
the class BlobTableInfo method getRouting.
@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);
}
use of org.elasticsearch.cluster.routing.GroupShardsIterator 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}
*/
public static Routing getRouting(ClusterState clusterState, RoutingProvider routingProvider, SessionContext sessionContext) {
String[] concreteIndices = Arrays.stream(clusterState.metadata().getConcreteAllIndices()).filter(index -> !IndexParts.isDangling(index)).toArray(String[]::new);
User user = sessionContext != null ? sessionContext.sessionUser() : 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);
for (final ShardIterator shardIt : groupShardsIterator) {
final ShardRouting shardRouting = shardIt.nextOrNull();
processShardRouting(clusterState.getNodes().getLocalNodeId(), locations, shardRouting, shardIt.shardId());
}
return new Routing(locations);
}
use of org.elasticsearch.cluster.routing.GroupShardsIterator in project crate by crate.
the class DocTableInfo method getRouting.
@Override
public Routing getRouting(final WhereClause whereClause, @Nullable final String preference) {
Routing routing = getRouting(clusterService.state(), whereClause, preference, new ArrayList<ShardId>(0));
if (routing != null)
return routing;
ClusterStateObserver observer = new ClusterStateObserver(clusterService, routingFetchTimeout, logger);
final SettableFuture<Routing> routingSettableFuture = SettableFuture.create();
observer.waitForNextChange(new FetchRoutingListener(routingSettableFuture, whereClause, preference), new ClusterStateObserver.ChangePredicate() {
@Override
public boolean apply(ClusterState previousState, ClusterState.ClusterStateStatus previousStatus, ClusterState newState, ClusterState.ClusterStateStatus newStatus) {
return validate(newState);
}
@Override
public boolean apply(ClusterChangedEvent changedEvent) {
return validate(changedEvent.state());
}
private boolean validate(ClusterState state) {
final Map<String, Map<String, List<Integer>>> locations = new TreeMap<>();
GroupShardsIterator shardIterators;
try {
shardIterators = getShardIterators(whereClause, preference, state);
} catch (IndexNotFoundException e) {
return true;
}
final List<ShardId> missingShards = new ArrayList<>(0);
fillLocationsFromShardIterators(locations, shardIterators, missingShards);
return missingShards.isEmpty();
}
});
try {
return routingSettableFuture.get();
} catch (ExecutionException e) {
throw Throwables.propagate(e.getCause());
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
use of org.elasticsearch.cluster.routing.GroupShardsIterator 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);
}
Aggregations