Search in sources :

Example 26 with IntIndexedContainer

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

the class ShardCollectSource method getIterators.

private List<CompletableFuture<BatchIterator<Row>>> getIterators(CollectTask collectTask, RoutedCollectPhase collectPhase, boolean requiresScroll, Map<String, IntIndexedContainer> indexShards) {
    Metadata metadata = clusterService.state().metadata();
    List<CompletableFuture<BatchIterator<Row>>> iterators = new ArrayList<>();
    for (Map.Entry<String, IntIndexedContainer> entry : indexShards.entrySet()) {
        String indexName = entry.getKey();
        IndexMetadata indexMD = metadata.index(indexName);
        if (indexMD == null) {
            if (IndexParts.isPartitioned(indexName)) {
                continue;
            }
            throw new IndexNotFoundException(indexName);
        }
        Index index = indexMD.getIndex();
        try {
            indicesService.indexServiceSafe(index);
        } catch (IndexNotFoundException e) {
            if (IndexParts.isPartitioned(indexName)) {
                continue;
            }
            throw e;
        }
        for (IntCursor shardCursor : entry.getValue()) {
            ShardId shardId = new ShardId(index, shardCursor.value);
            try {
                ShardCollectorProvider shardCollectorProvider = getCollectorProviderSafe(shardId);
                CompletableFuture<BatchIterator<Row>> iterator = shardCollectorProvider.getFutureIterator(collectPhase, requiresScroll, collectTask);
                iterators.add(iterator);
            } catch (ShardNotFoundException | IllegalIndexShardStateException e) {
                // and the reader required in the fetchPhase would be missing.
                if (Symbols.containsColumn(collectPhase.toCollect(), DocSysColumns.FETCHID)) {
                    throw e;
                }
                iterators.add(remoteCollectorFactory.createCollector(shardId, collectPhase, collectTask, shardCollectorProviderFactory));
            } catch (IndexNotFoundException e) {
                // Prevent wrapping this to not break retry-detection
                throw e;
            } catch (Throwable t) {
                Exceptions.rethrowRuntimeException(t);
            }
        }
    }
    return iterators;
}
Also used : IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) ArrayList(java.util.ArrayList) IntIndexedContainer(com.carrotsearch.hppc.IntIndexedContainer) Index(org.elasticsearch.index.Index) InMemoryBatchIterator(io.crate.data.InMemoryBatchIterator) CompositeBatchIterator(io.crate.data.CompositeBatchIterator) BatchIterator(io.crate.data.BatchIterator) IllegalIndexShardStateException(org.elasticsearch.index.shard.IllegalIndexShardStateException) ShardId(org.elasticsearch.index.shard.ShardId) CompletableFuture(java.util.concurrent.CompletableFuture) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) IntCursor(com.carrotsearch.hppc.cursors.IntCursor) ShardCollectorProvider(io.crate.execution.engine.collect.ShardCollectorProvider) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) Row(io.crate.data.Row) SentinelRow(io.crate.data.SentinelRow) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 27 with IntIndexedContainer

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

the class SystemCollectSource method getIterator.

@Override
public CompletableFuture<BatchIterator<Row>> getIterator(TransactionContext txnCtx, CollectPhase phase, CollectTask collectTask, boolean supportMoveToStart) {
    RoutedCollectPhase collectPhase = (RoutedCollectPhase) phase;
    Map<String, Map<String, IntIndexedContainer>> locations = collectPhase.routing().locations();
    String table = Iterables.getOnlyElement(locations.get(clusterService.localNode().getId()).keySet());
    RelationName relationName = RelationName.fromIndexName(table);
    StaticTableDefinition<?> tableDefinition = tableDefinition(relationName);
    User user = requireNonNull(userLookup.findUser(txnCtx.sessionSettings().userName()), "User who invoked a statement must exist");
    return CompletableFuture.completedFuture(CollectingBatchIterator.newInstance(() -> {
    }, // If data is already local, then `CollectingBatchIterator` takes care of kill handling.
    t -> {
    }, () -> tableDefinition.retrieveRecords(txnCtx, user).thenApply(records -> recordsToRows(collectPhase, collectTask.txnCtx(), tableDefinition.getReferenceResolver(), supportMoveToStart, records)), tableDefinition.involvesIO()));
}
Also used : UserLookup(io.crate.user.UserLookup) TransactionContext(io.crate.metadata.TransactionContext) InformationSchemaTableDefinitions(io.crate.metadata.information.InformationSchemaTableDefinitions) RelationName(io.crate.metadata.RelationName) ClusterService(org.elasticsearch.cluster.service.ClusterService) BatchIterator(io.crate.data.BatchIterator) ReferenceResolver(io.crate.expression.reference.ReferenceResolver) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) PgCatalogSchemaInfo(io.crate.metadata.pgcatalog.PgCatalogSchemaInfo) SysRowUpdater(io.crate.expression.reference.sys.SysRowUpdater) Inject(org.elasticsearch.common.inject.Inject) ArrayList(java.util.ArrayList) SysNodeChecks(io.crate.expression.reference.sys.check.node.SysNodeChecks) SchemaUnknownException(io.crate.exceptions.SchemaUnknownException) SysSchemaInfo(io.crate.metadata.sys.SysSchemaInfo) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) CollectPhase(io.crate.execution.dsl.phases.CollectPhase) PgCatalogTableDefinitions(io.crate.metadata.pgcatalog.PgCatalogTableDefinitions) InformationSchemaInfo(io.crate.metadata.information.InformationSchemaInfo) IntIndexedContainer(com.carrotsearch.hppc.IntIndexedContainer) NodeContext(io.crate.metadata.NodeContext) User(io.crate.user.User) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase) RowsTransformer(io.crate.execution.engine.collect.RowsTransformer) StaticTableDefinition(io.crate.expression.reference.StaticTableDefinition) Iterables(io.crate.common.collections.Iterables) CollectingBatchIterator(io.crate.data.CollectingBatchIterator) CollectTask(io.crate.execution.engine.collect.CollectTask) SysNodeChecksTableInfo(io.crate.metadata.sys.SysNodeChecksTableInfo) Row(io.crate.data.Row) SysTableDefinitions(io.crate.metadata.sys.SysTableDefinitions) UserManager(io.crate.user.UserManager) InputFactory(io.crate.expression.InputFactory) RelationUnknown(io.crate.exceptions.RelationUnknown) User(io.crate.user.User) RelationName(io.crate.metadata.RelationName) Map(java.util.Map) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase)

Example 28 with IntIndexedContainer

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

the class SchemasITest method testShardsTable.

@Test
public void testShardsTable() throws Exception {
    execute("create table t2 (id int primary key) clustered into 4 shards with(number_of_replicas=0)");
    execute("create table t3 (id int primary key) clustered into 8 shards with(number_of_replicas=0)");
    ensureYellow();
    TableInfo ti = schemas.getTableInfo(new RelationName("sys", "shards"));
    ClusterService clusterService = clusterService();
    Routing routing = ti.getRouting(clusterService.state(), routingProvider, null, null, SessionContext.systemSessionContext());
    Set<String> tables = new HashSet<>();
    Set<String> expectedTables = Set.of(getFqn("t2"), getFqn("t3"));
    int numShards = 0;
    for (Map.Entry<String, Map<String, IntIndexedContainer>> nodeEntry : routing.locations().entrySet()) {
        for (Map.Entry<String, IntIndexedContainer> indexEntry : nodeEntry.getValue().entrySet()) {
            tables.add(indexEntry.getKey());
            numShards += indexEntry.getValue().size();
        }
    }
    assertThat(numShards, is(12));
    assertThat(tables, is(expectedTables));
}
Also used : ClusterService(org.elasticsearch.cluster.service.ClusterService) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo) IntIndexedContainer(com.carrotsearch.hppc.IntIndexedContainer) Map(java.util.Map) CheckConstraint(io.crate.sql.tree.CheckConstraint) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 29 with IntIndexedContainer

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

the class SchemasITest method testDocTable.

@Test
public void testDocTable() {
    execute("create table t1 (" + "id int primary key, " + "name string, " + "CONSTRAINT not_miguel CHECK (name != 'miguel'), " + "details object(dynamic) as (size byte, created timestamp with time zone)" + ") clustered into 10 shards with (number_of_replicas=1)");
    ensureYellow();
    DocTableInfo ti = schemas.getTableInfo(new RelationName(sqlExecutor.getCurrentSchema(), "t1"));
    assertThat(ti.ident().name(), is("t1"));
    assertThat(ti.columns().size(), is(3));
    assertThat(ti.primaryKey().size(), is(1));
    assertThat(ti.primaryKey().get(0), is(new ColumnIdent("id")));
    assertThat(ti.clusteredBy(), is(new ColumnIdent("id")));
    List<CheckConstraint<Symbol>> checkConstraints = ti.checkConstraints();
    assertEquals(1, checkConstraints.size());
    assertEquals(checkConstraints.get(0).name(), "not_miguel");
    assertThat(checkConstraints.get(0).expressionStr(), equalTo("\"name\" <> 'miguel'"));
    ClusterService clusterService = clusterService();
    Routing routing = ti.getRouting(clusterService.state(), routingProvider, WhereClause.MATCH_ALL, RoutingProvider.ShardSelection.ANY, SessionContext.systemSessionContext());
    Set<String> nodes = routing.nodes();
    // for the rare case
    assertThat(nodes.size(), isOneOf(1, 2));
    // where all shards are on 1 node
    int numShards = 0;
    for (Map.Entry<String, Map<String, IntIndexedContainer>> nodeEntry : routing.locations().entrySet()) {
        for (Map.Entry<String, IntIndexedContainer> indexEntry : nodeEntry.getValue().entrySet()) {
            assertThat(indexEntry.getKey(), is(getFqn("t1")));
            numShards += indexEntry.getValue().size();
        }
    }
    assertThat(numShards, is(10));
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) IntIndexedContainer(com.carrotsearch.hppc.IntIndexedContainer) CheckConstraint(io.crate.sql.tree.CheckConstraint) ClusterService(org.elasticsearch.cluster.service.ClusterService) Map(java.util.Map) CheckConstraint(io.crate.sql.tree.CheckConstraint) Test(org.junit.Test)

Example 30 with IntIndexedContainer

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

the class PlanPrinter method xContentSafeRoutingLocations.

/**
 * Converts the shardId's of each node->table from a {@link IntIndexedContainer} to a list of Integers as custom
 * classes are not supported by the {@link org.elasticsearch.common.xcontent.XContentBuilder}.
 */
private static Map<String, Map<String, List<Integer>>> xContentSafeRoutingLocations(Map<String, Map<String, IntIndexedContainer>> locations) {
    HashMap<String, Map<String, List<Integer>>> safeLocations = new HashMap<>(locations.size(), 1f);
    for (Map.Entry<String, Map<String, IntIndexedContainer>> nodeEntry : locations.entrySet()) {
        HashMap<String, List<Integer>> tableShards = new HashMap<>(nodeEntry.getValue().size(), 1f);
        for (Map.Entry<String, IntIndexedContainer> tableEntry : nodeEntry.getValue().entrySet()) {
            ArrayList<Integer> shardList = new ArrayList<>(tableEntry.getValue().size());
            for (IntCursor cursor : tableEntry.getValue()) {
                shardList.add(cursor.value);
            }
            // ensure a deterministic shard list by sorting it (important for test assertions but maybe also for apps)
            shardList.sort(Integer::compareTo);
            tableShards.put(tableEntry.getKey(), shardList);
        }
        safeLocations.put(nodeEntry.getKey(), tableShards);
    }
    return safeLocations;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IntIndexedContainer(com.carrotsearch.hppc.IntIndexedContainer) IntCursor(com.carrotsearch.hppc.cursors.IntCursor) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) 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