Search in sources :

Example 1 with ShardRowContext

use of io.crate.expression.reference.sys.shard.ShardRowContext in project crate by crate.

the class ShardCollectSource method getShardsIterator.

private Iterable<Row> getShardsIterator(TransactionContext txnCtx, RoutedCollectPhase collectPhase, String localNodeId) {
    Map<String, Map<String, IntIndexedContainer>> locations = collectPhase.routing().locations();
    List<UnassignedShard> unassignedShards = new ArrayList<>();
    List<ShardRowContext> shardRowContexts = new ArrayList<>();
    Map<String, IntIndexedContainer> indexShardsMap = locations.get(localNodeId);
    Metadata metadata = clusterService.state().metadata();
    for (Map.Entry<String, IntIndexedContainer> indexShards : indexShardsMap.entrySet()) {
        String indexName = indexShards.getKey();
        IndexMetadata indexMetadata = metadata.index(indexName);
        if (indexMetadata == null) {
            continue;
        }
        Index index = indexMetadata.getIndex();
        IntIndexedContainer shards = indexShards.getValue();
        IndexService indexService = indicesService.indexService(index);
        if (indexService == null) {
            for (IntCursor shard : shards) {
                unassignedShards.add(toUnassignedShard(index.getName(), UnassignedShard.markAssigned(shard.value)));
            }
            continue;
        }
        for (IntCursor shard : shards) {
            if (UnassignedShard.isUnassigned(shard.value)) {
                unassignedShards.add(toUnassignedShard(index.getName(), UnassignedShard.markAssigned(shard.value)));
                continue;
            }
            ShardId shardId = new ShardId(index, shard.value);
            try {
                ShardCollectorProvider shardCollectorProvider = getCollectorProviderSafe(shardId);
                shardRowContexts.add(shardCollectorProvider.shardRowContext());
            } catch (ShardNotFoundException | IllegalIndexShardStateException e) {
                unassignedShards.add(toUnassignedShard(index.getName(), shard.value));
            }
        }
    }
    Iterable<Row> assignedShardRows = RowsTransformer.toRowsIterable(txnCtx, inputFactory, shardReferenceResolver, collectPhase, shardRowContexts, false);
    Iterable<Row> rows;
    if (unassignedShards.size() > 0) {
        Iterable<Row> unassignedShardRows = RowsTransformer.toRowsIterable(txnCtx, inputFactory, unassignedShardReferenceResolver, collectPhase, unassignedShards, false);
        rows = Iterables.concat(assignedShardRows, unassignedShardRows);
    } else {
        rows = assignedShardRows;
    }
    if (collectPhase.orderBy() != null) {
        return RowsTransformer.sortRows(Iterables.transform(rows, Row::materialize), collectPhase);
    }
    return rows;
}
Also used : IndexService(org.elasticsearch.index.IndexService) ArrayList(java.util.ArrayList) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) IntIndexedContainer(com.carrotsearch.hppc.IntIndexedContainer) Index(org.elasticsearch.index.Index) UnassignedShard(io.crate.metadata.shard.unassigned.UnassignedShard) IllegalIndexShardStateException(org.elasticsearch.index.shard.IllegalIndexShardStateException) ShardId(org.elasticsearch.index.shard.ShardId) ShardRowContext(io.crate.expression.reference.sys.shard.ShardRowContext) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) IntCursor(com.carrotsearch.hppc.cursors.IntCursor) ShardCollectorProvider(io.crate.execution.engine.collect.ShardCollectorProvider) 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 2 with ShardRowContext

use of io.crate.expression.reference.sys.shard.ShardRowContext in project crate by crate.

the class SysShardsExpressionsTest method prepare.

@Before
public void prepare() {
    NodeContext nodeCtx = createNodeContext();
    indexShard = mockIndexShard();
    CrateSettings crateSettings = new CrateSettings(clusterService, clusterService.getSettings());
    UserDefinedFunctionService udfService = new UserDefinedFunctionService(clusterService, nodeCtx);
    schemas = new Schemas(Map.of("sys", new SysSchemaInfo(this.clusterService, crateSettings)), clusterService, new DocSchemaInfoFactory(new TestingDocTableInfoFactory(Collections.emptyMap()), (ident, state) -> null, nodeCtx, udfService));
    resolver = new ShardReferenceResolver(schemas, new ShardRowContext(indexShard, clusterService));
    sysShards = schemas.getTableInfo(SysShardsTableInfo.IDENT);
}
Also used : UserDefinedFunctionService(io.crate.expression.udf.UserDefinedFunctionService) ShardRowContext(io.crate.expression.reference.sys.shard.ShardRowContext) SysSchemaInfo(io.crate.metadata.sys.SysSchemaInfo) DocSchemaInfoFactory(io.crate.metadata.doc.DocSchemaInfoFactory) CrateSettings(io.crate.metadata.settings.CrateSettings) TestingDocTableInfoFactory(io.crate.metadata.doc.TestingDocTableInfoFactory) TestingHelpers.createNodeContext(io.crate.testing.TestingHelpers.createNodeContext) NodeContext(io.crate.metadata.NodeContext) Schemas(io.crate.metadata.Schemas) ShardReferenceResolver(io.crate.metadata.shard.ShardReferenceResolver) Before(org.junit.Before)

Example 3 with ShardRowContext

use of io.crate.expression.reference.sys.shard.ShardRowContext in project crate by crate.

the class SysShardsExpressionsTest method testShardSizeExpressionWhenIndexShardHasBeenClosed.

@Test
public void testShardSizeExpressionWhenIndexShardHasBeenClosed() {
    IndexShard mock = mockIndexShard();
    when(mock.storeStats()).thenThrow(new AlreadyClosedException("shard already closed"));
    ShardReferenceResolver resolver = new ShardReferenceResolver(schemas, new ShardRowContext(mock, clusterService));
    Reference refInfo = refInfo("sys.shards.size", DataTypes.LONG, RowGranularity.SHARD);
    NestableInput<Long> shardSizeExpression = (NestableInput<Long>) resolver.getImplementation(refInfo);
    assertThat(shardSizeExpression.value(), is(0L));
}
Also used : ShardRowContext(io.crate.expression.reference.sys.shard.ShardRowContext) NestableInput(io.crate.expression.NestableInput) Reference(io.crate.metadata.Reference) IndexShard(org.elasticsearch.index.shard.IndexShard) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) ShardReferenceResolver(io.crate.metadata.shard.ShardReferenceResolver) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 4 with ShardRowContext

use of io.crate.expression.reference.sys.shard.ShardRowContext in project crate by crate.

the class SysShardsExpressionsTest method test_retention_lease_is_null_on_index_shard_closed_exception.

@Test
public void test_retention_lease_is_null_on_index_shard_closed_exception() throws Exception {
    IndexShard mock = mockIndexShard();
    var shardId = mock.shardId();
    doThrow(new IndexShardClosedException(shardId)).when(mock).getRetentionLeaseStats();
    ShardReferenceResolver resolver = new ShardReferenceResolver(schemas, new ShardRowContext(mock, clusterService));
    Reference refInfo = refInfo("sys.shards.retention_leases", DataTypes.LONG, RowGranularity.SHARD, "version");
    NestableInput<Long> input = (NestableInput<Long>) resolver.getImplementation(refInfo);
    assertThat(input.value(), Matchers.nullValue());
}
Also used : ShardRowContext(io.crate.expression.reference.sys.shard.ShardRowContext) NestableInput(io.crate.expression.NestableInput) IndexShardClosedException(org.elasticsearch.index.shard.IndexShardClosedException) Reference(io.crate.metadata.Reference) IndexShard(org.elasticsearch.index.shard.IndexShard) ShardReferenceResolver(io.crate.metadata.shard.ShardReferenceResolver) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Aggregations

ShardRowContext (io.crate.expression.reference.sys.shard.ShardRowContext)4 ShardReferenceResolver (io.crate.metadata.shard.ShardReferenceResolver)3 NestableInput (io.crate.expression.NestableInput)2 Reference (io.crate.metadata.Reference)2 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)2 IndexShard (org.elasticsearch.index.shard.IndexShard)2 Test (org.junit.Test)2 IntIndexedContainer (com.carrotsearch.hppc.IntIndexedContainer)1 IntCursor (com.carrotsearch.hppc.cursors.IntCursor)1 Row (io.crate.data.Row)1 SentinelRow (io.crate.data.SentinelRow)1 ShardCollectorProvider (io.crate.execution.engine.collect.ShardCollectorProvider)1 UserDefinedFunctionService (io.crate.expression.udf.UserDefinedFunctionService)1 NodeContext (io.crate.metadata.NodeContext)1 Schemas (io.crate.metadata.Schemas)1 DocSchemaInfoFactory (io.crate.metadata.doc.DocSchemaInfoFactory)1 TestingDocTableInfoFactory (io.crate.metadata.doc.TestingDocTableInfoFactory)1 CrateSettings (io.crate.metadata.settings.CrateSettings)1 UnassignedShard (io.crate.metadata.shard.unassigned.UnassignedShard)1 SysSchemaInfo (io.crate.metadata.sys.SysSchemaInfo)1