Search in sources :

Example 6 with IntIndexedContainer

use of com.carrotsearch.hppc.IntIndexedContainer in project crate by crate.

the class RoutingBuilder method allocateRoutingNodes.

private static void allocateRoutingNodes(Map<String, Map<Integer, String>> shardNodes, Map<String, Map<String, IntIndexedContainer>> locations) {
    for (Map.Entry<String, Map<String, IntIndexedContainer>> indicesByNodeId : locations.entrySet()) {
        String nodeId = indicesByNodeId.getKey();
        for (Map.Entry<String, IntIndexedContainer> shardsByIndexEntry : indicesByNodeId.getValue().entrySet()) {
            String index = shardsByIndexEntry.getKey();
            IntIndexedContainer shards = shardsByIndexEntry.getValue();
            Map<Integer, String> shardsOnIndex = shardNodes.get(index);
            if (shardsOnIndex == null) {
                shardsOnIndex = new HashMap<>(shards.size());
                shardNodes.put(index, shardsOnIndex);
                for (IntCursor id : shards) {
                    shardsOnIndex.put(id.value, nodeId);
                }
            } else {
                for (IntCursor id : shards) {
                    String allocatedNodeId = shardsOnIndex.get(id.value);
                    assert allocatedNodeId == null || allocatedNodeId.equals(nodeId) : "allocatedNodeId must match nodeId";
                    shardsOnIndex.put(id.value, nodeId);
                }
            }
        }
    }
}
Also used : IntCursor(com.carrotsearch.hppc.cursors.IntCursor) IntIndexedContainer(com.carrotsearch.hppc.IntIndexedContainer) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with IntIndexedContainer

use of com.carrotsearch.hppc.IntIndexedContainer 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 8 with IntIndexedContainer

use of com.carrotsearch.hppc.IntIndexedContainer in project crate by crate.

the class SysShardsTableInfo method processShardRouting.

private static void processShardRouting(String localNodeId, Map<String, Map<String, IntIndexedContainer>> routing, ShardRouting shardRouting, ShardId shardId) {
    String node;
    int id;
    String index = shardId.getIndex().getName();
    if (shardRouting == null) {
        node = localNodeId;
        id = UnassignedShard.markUnassigned(shardId.id());
    } else {
        node = shardRouting.currentNodeId();
        id = shardRouting.id();
    }
    Map<String, IntIndexedContainer> nodeMap = routing.computeIfAbsent(node, k -> new TreeMap<>());
    IntIndexedContainer shards = nodeMap.get(index);
    if (shards == null) {
        shards = new IntArrayList();
        nodeMap.put(index, shards);
    }
    shards.add(id);
}
Also used : IntIndexedContainer(com.carrotsearch.hppc.IntIndexedContainer) IntArrayList(com.carrotsearch.hppc.IntArrayList)

Example 9 with IntIndexedContainer

use of com.carrotsearch.hppc.IntIndexedContainer in project crate by crate.

the class FetchTask method start.

@Override
public void start() {
    synchronized (jobId) {
        if (killed != null) {
            result.completeExceptionally(killed);
            return;
        }
        HashMap<String, RelationName> index2TableIdent = new HashMap<>();
        for (Map.Entry<RelationName, Collection<String>> entry : phase.tableIndices().entrySet()) {
            for (String indexName : entry.getValue()) {
                index2TableIdent.put(indexName, entry.getKey());
            }
        }
        Set<RelationName> tablesWithFetchRefs = new HashSet<>();
        for (Reference reference : phase.fetchRefs()) {
            tablesWithFetchRefs.add(reference.ident().tableIdent());
        }
        String source = "fetch-task: " + jobId.toString() + '-' + phase.phaseId() + '-' + phase.name();
        for (Routing routing : routingIterable) {
            Map<String, Map<String, IntIndexedContainer>> locations = routing.locations();
            Map<String, IntIndexedContainer> indexShards = locations.get(localNodeId);
            for (Map.Entry<String, IntIndexedContainer> indexShardsEntry : indexShards.entrySet()) {
                String indexName = indexShardsEntry.getKey();
                Integer base = phase.bases().get(indexName);
                if (base == null) {
                    continue;
                }
                IndexMetadata indexMetadata = metadata.index(indexName);
                if (indexMetadata == null) {
                    if (IndexParts.isPartitioned(indexName)) {
                        continue;
                    }
                    throw new IndexNotFoundException(indexName);
                }
                Index index = indexMetadata.getIndex();
                RelationName ident = index2TableIdent.get(indexName);
                assert ident != null : "no relationName found for index " + indexName;
                tableIdents.put(base, ident);
                toFetch.put(ident, new ArrayList<>());
                for (IntCursor shard : indexShardsEntry.getValue()) {
                    ShardId shardId = new ShardId(index, shard.value);
                    int readerId = base + shardId.id();
                    SharedShardContext shardContext = shardContexts.get(readerId);
                    if (shardContext == null) {
                        try {
                            shardContext = sharedShardContexts.createContext(shardId, readerId);
                            shardContexts.put(readerId, shardContext);
                            if (tablesWithFetchRefs.contains(ident)) {
                                searchers.put(readerId, shardContext.acquireSearcher(source));
                            }
                        } catch (IndexNotFoundException e) {
                            if (!IndexParts.isPartitioned(indexName)) {
                                throw e;
                            }
                        }
                    }
                }
            }
        }
        for (Reference reference : phase.fetchRefs()) {
            Collection<Reference> references = toFetch.get(reference.ident().tableIdent());
            if (references != null) {
                references.add(reference);
            }
        }
    }
    if (searchers.isEmpty() || phase.fetchRefs().isEmpty()) {
        // no fetch references means there will be no fetch requests
        // this context is only here to allow the collectors to generate docids with the right bases
        // the bases are fetched in the prepare phase therefore this context can be closed
        close();
    }
}
Also used : HashMap(java.util.HashMap) IntObjectHashMap(com.carrotsearch.hppc.IntObjectHashMap) Index(org.elasticsearch.index.Index) ShardId(org.elasticsearch.index.shard.ShardId) IntCursor(com.carrotsearch.hppc.cursors.IntCursor) RelationName(io.crate.metadata.RelationName) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) HashSet(java.util.HashSet) Reference(io.crate.metadata.Reference) Routing(io.crate.metadata.Routing) IntIndexedContainer(com.carrotsearch.hppc.IntIndexedContainer) Collection(java.util.Collection) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) IntObjectHashMap(com.carrotsearch.hppc.IntObjectHashMap) SharedShardContext(io.crate.execution.jobs.SharedShardContext)

Example 10 with IntIndexedContainer

use of com.carrotsearch.hppc.IntIndexedContainer in project crate by crate.

the class Routing method forTableOnAllNodes.

public static Routing forTableOnAllNodes(RelationName relationName, DiscoveryNodes nodes) {
    TreeMap<String, Map<String, IntIndexedContainer>> indicesByNode = new TreeMap<>();
    Map<String, IntIndexedContainer> shardsByIndex = Collections.singletonMap(relationName.indexNameOrAlias(), IntArrayList.from(IntArrayList.EMPTY_ARRAY));
    for (DiscoveryNode node : nodes) {
        indicesByNode.put(node.getId(), shardsByIndex);
    }
    return new Routing(indicesByNode);
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) IntIndexedContainer(com.carrotsearch.hppc.IntIndexedContainer) TreeMap(java.util.TreeMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Aggregations

IntIndexedContainer (com.carrotsearch.hppc.IntIndexedContainer)30 Map (java.util.Map)17 IntCursor (com.carrotsearch.hppc.cursors.IntCursor)10 ArrayList (java.util.ArrayList)10 TreeMap (java.util.TreeMap)7 Index (org.elasticsearch.index.Index)7 IntArrayList (com.carrotsearch.hppc.IntArrayList)6 RelationName (io.crate.metadata.RelationName)6 List (java.util.List)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)6 Metadata (org.elasticsearch.cluster.metadata.Metadata)6 ShardId (org.elasticsearch.index.shard.ShardId)6 Test (org.junit.Test)6 Routing (io.crate.metadata.Routing)5 ClusterService (org.elasticsearch.cluster.service.ClusterService)5 IndexNotFoundException (org.elasticsearch.index.IndexNotFoundException)5 ShardCollectorProvider (io.crate.execution.engine.collect.ShardCollectorProvider)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 IllegalIndexShardStateException (org.elasticsearch.index.shard.IllegalIndexShardStateException)4