Search in sources :

Example 1 with UnassignedShard

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);
}
Also used : IndexService(org.elasticsearch.index.IndexService) ArrayList(java.util.ArrayList) UnassignedShard(io.crate.metadata.shard.unassigned.UnassignedShard) IllegalIndexShardStateException(org.elasticsearch.index.shard.IllegalIndexShardStateException) ShardId(org.elasticsearch.index.shard.ShardId) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) UnhandledServerException(io.crate.exceptions.UnhandledServerException) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with UnassignedShard

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));
}
Also used : OrderBy(io.crate.analyze.OrderBy) UnassignedShard(io.crate.metadata.shard.unassigned.UnassignedShard) ShardId(org.elasticsearch.index.shard.ShardId) ClusterService(org.elasticsearch.cluster.ClusterService) Row(io.crate.data.Row) RoutedCollectPhase(io.crate.planner.node.dql.RoutedCollectPhase) Test(org.junit.Test) SQLTransportIntegrationTest(io.crate.integrationtests.SQLTransportIntegrationTest)

Aggregations

UnassignedShard (io.crate.metadata.shard.unassigned.UnassignedShard)2 ShardId (org.elasticsearch.index.shard.ShardId)2 OrderBy (io.crate.analyze.OrderBy)1 Row (io.crate.data.Row)1 UnhandledServerException (io.crate.exceptions.UnhandledServerException)1 SQLTransportIntegrationTest (io.crate.integrationtests.SQLTransportIntegrationTest)1 RoutedCollectPhase (io.crate.planner.node.dql.RoutedCollectPhase)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ClusterService (org.elasticsearch.cluster.ClusterService)1 IndexService (org.elasticsearch.index.IndexService)1 IllegalIndexShardStateException (org.elasticsearch.index.shard.IllegalIndexShardStateException)1 ShardNotFoundException (org.elasticsearch.index.shard.ShardNotFoundException)1 Test (org.junit.Test)1