use of io.crate.metadata.shard.unassigned.UnassignedShard in project crate by crate.
the class ShardCollectSource method getShardsCollector.
private CrateCollector getShardsCollector(RoutedCollectPhase collectPhase, RoutedCollectPhase normalizedPhase, String localNodeId, BatchConsumer consumer) {
Map<String, Map<String, List<Integer>>> locations = collectPhase.routing().locations();
List<UnassignedShard> unassignedShards = new ArrayList<>();
List<Object[]> rows = new ArrayList<>();
Map<String, List<Integer>> indexShardsMap = locations.get(localNodeId);
for (Map.Entry<String, List<Integer>> indexShards : indexShardsMap.entrySet()) {
String indexName = indexShards.getKey();
List<Integer> shards = indexShards.getValue();
IndexService indexService = indicesService.indexService(indexName);
if (indexService == null) {
for (Integer shard : shards) {
unassignedShards.add(toUnassignedShard(new ShardId(indexName, UnassignedShard.markAssigned(shard))));
}
continue;
}
for (Integer shard : shards) {
if (UnassignedShard.isUnassigned(shard)) {
unassignedShards.add(toUnassignedShard(new ShardId(indexName, UnassignedShard.markAssigned(shard))));
continue;
}
ShardId shardId = new ShardId(indexName, shard);
try {
ShardCollectorProvider shardCollectorProvider = getCollectorProviderSafe(shardId);
Object[] row = shardCollectorProvider.getRowForShard(normalizedPhase);
if (row != null) {
rows.add(row);
}
} catch (ShardNotFoundException | IllegalIndexShardStateException e) {
unassignedShards.add(toUnassignedShard(shardId));
} catch (Throwable t) {
t.printStackTrace();
throw new UnhandledServerException(t);
}
}
}
if (!unassignedShards.isEmpty()) {
// because otherwise if _node was also selected it would contain something which is wrong
for (Row row : systemCollectSource.toRowsIterableTransformation(collectPhase, false).apply(unassignedShards)) {
rows.add(row.materialize());
}
}
if (collectPhase.orderBy() != null) {
rows.sort(OrderingByPosition.arrayOrdering(collectPhase).reverse());
}
return BatchIteratorCollectorBridge.newInstance(RowsBatchIterator.newInstance(Iterables.transform(rows, Buckets.arrayToRowFunction()), collectPhase.outputTypes().size()), consumer);
}
use of io.crate.metadata.shard.unassigned.UnassignedShard in project crate by crate.
the class SystemCollectSourceTest method testOrderBySymbolsDoNotAppearTwiceInRows.
@Test
public void testOrderBySymbolsDoNotAppearTwiceInRows() throws Exception {
SystemCollectSource systemCollectSource = internalCluster().getInstance(SystemCollectSource.class);
Reference shardId = new Reference(new ReferenceIdent(new TableIdent("sys", "shards"), "id"), RowGranularity.SHARD, DataTypes.INTEGER);
RoutedCollectPhase collectPhase = new RoutedCollectPhase(UUID.randomUUID(), 1, "collect", new Routing(ImmutableMap.of()), RowGranularity.SHARD, Collections.singletonList(shardId), ImmutableList.of(), WhereClause.MATCH_ALL, DistributionInfo.DEFAULT_BROADCAST);
collectPhase.orderBy(new OrderBy(Collections.singletonList(shardId), new boolean[] { false }, new Boolean[] { null }));
Iterable<? extends Row> rows = systemCollectSource.toRowsIterableTransformation(collectPhase, false).apply(Collections.singletonList(new UnassignedShard(new ShardId("foo", 1), mock(ClusterService.class), true, ShardRoutingState.UNASSIGNED)));
Row next = rows.iterator().next();
assertThat(next.numColumns(), is(1));
}
Aggregations