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