Search in sources :

Example 6 with GroupShardsIterator

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;
    }
}
Also used : GroupShardsIterator(org.elasticsearch.cluster.routing.GroupShardsIterator) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) ImmutableMap(com.google.common.collect.ImmutableMap) Nullable(javax.annotation.Nullable)

Example 7 with GroupShardsIterator

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);
}
Also used : GroupShardsIterator(org.elasticsearch.cluster.routing.GroupShardsIterator) ShardIterator(org.elasticsearch.cluster.routing.ShardIterator) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 8 with GroupShardsIterator

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);
}
Also used : ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) ShardId(org.elasticsearch.index.shard.ShardId) IndexParts(io.crate.metadata.IndexParts) Arrays(java.util.Arrays) LONG(io.crate.types.DataTypes.LONG) SessionContext(io.crate.action.sql.SessionContext) Privilege(io.crate.user.Privilege) ShardIterator(org.elasticsearch.cluster.routing.ShardIterator) RelationName(io.crate.metadata.RelationName) NestableCollectExpression(io.crate.execution.engine.collect.NestableCollectExpression) ArrayList(java.util.ArrayList) GroupShardsIterator(org.elasticsearch.cluster.routing.GroupShardsIterator) SeqNoStats(org.elasticsearch.index.seqno.SeqNoStats) RetentionLease(org.elasticsearch.index.seqno.RetentionLease) ClusterState(org.elasticsearch.cluster.ClusterState) Routing(io.crate.metadata.Routing) NestableInput(io.crate.expression.NestableInput) UnassignedShard(io.crate.metadata.shard.unassigned.UnassignedShard) INTEGER(io.crate.types.DataTypes.INTEGER) IntArrayList(com.carrotsearch.hppc.IntArrayList) Map(java.util.Map) Map.entry(java.util.Map.entry) ShardRowContext(io.crate.expression.reference.sys.shard.ShardRowContext) IntIndexedContainer(com.carrotsearch.hppc.IntIndexedContainer) User(io.crate.user.User) BOOLEAN(io.crate.types.DataTypes.BOOLEAN) RowCollectExpressionFactory(io.crate.metadata.expressions.RowCollectExpressionFactory) ColumnIdent(io.crate.metadata.ColumnIdent) NestableCollectExpression.constant(io.crate.execution.engine.collect.NestableCollectExpression.constant) STRING(io.crate.types.DataTypes.STRING) RoutingProvider(io.crate.metadata.RoutingProvider) NestableCollectExpression.forFunction(io.crate.execution.engine.collect.NestableCollectExpression.forFunction) List(java.util.List) SystemTable(io.crate.metadata.SystemTable) RowGranularity(io.crate.metadata.RowGranularity) TreeMap(java.util.TreeMap) DataTypes(io.crate.types.DataTypes) User(io.crate.user.User) ArrayList(java.util.ArrayList) IntArrayList(com.carrotsearch.hppc.IntArrayList) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) Routing(io.crate.metadata.Routing) TreeMap(java.util.TreeMap) ShardIterator(org.elasticsearch.cluster.routing.ShardIterator) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 9 with GroupShardsIterator

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);
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateObserver(org.elasticsearch.cluster.ClusterStateObserver) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) UnavailableShardsException(io.crate.exceptions.UnavailableShardsException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) ColumnUnknownException(io.crate.exceptions.ColumnUnknownException) ShardId(org.elasticsearch.index.shard.ShardId) GroupShardsIterator(org.elasticsearch.cluster.routing.GroupShardsIterator) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 10 with GroupShardsIterator

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

Aggregations

GroupShardsIterator (org.elasticsearch.cluster.routing.GroupShardsIterator)21 ClusterState (org.elasticsearch.cluster.ClusterState)16 ShardIterator (org.elasticsearch.cluster.routing.ShardIterator)16 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)16 Path (java.nio.file.Path)4 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 PlainShardIterator (org.elasticsearch.cluster.routing.PlainShardIterator)4 Index (org.elasticsearch.index.Index)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Set (java.util.Set)3 TreeSet (java.util.TreeSet)3 NodesStatsResponse (org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse)3 ClusterSettings (org.elasticsearch.common.settings.ClusterSettings)3 ShardId (org.elasticsearch.index.shard.ShardId)3 FsInfo (org.elasticsearch.monitor.fs.FsInfo)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 List (java.util.List)2 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)2