Search in sources :

Example 11 with ShardNotFoundException

use of org.elasticsearch.index.shard.ShardNotFoundException in project crate by crate.

the class Get method build.

@Override
public ExecutionPlan build(PlannerContext plannerContext, Set<PlanHint> hints, ProjectionBuilder projectionBuilder, int limitHint, int offsetHint, @Nullable OrderBy order, @Nullable Integer pageSizeHint, Row params, SubQueryResults subQueryResults) {
    HashMap<String, Map<ShardId, List<PKAndVersion>>> idsByShardByNode = new HashMap<>();
    DocTableInfo docTableInfo = tableRelation.tableInfo();
    for (DocKeys.DocKey docKey : docKeys) {
        String id = docKey.getId(plannerContext.transactionContext(), plannerContext.nodeContext(), params, subQueryResults);
        if (id == null) {
            continue;
        }
        List<String> partitionValues = docKey.getPartitionValues(plannerContext.transactionContext(), plannerContext.nodeContext(), params, subQueryResults);
        String indexName = indexName(docTableInfo, partitionValues);
        String routing = docKey.getRouting(plannerContext.transactionContext(), plannerContext.nodeContext(), params, subQueryResults);
        ShardRouting shardRouting;
        try {
            shardRouting = plannerContext.resolveShard(indexName, id, routing);
        } catch (IndexNotFoundException e) {
            if (docTableInfo.isPartitioned()) {
                continue;
            }
            throw e;
        }
        String currentNodeId = shardRouting.currentNodeId();
        if (currentNodeId == null) {
            // If relocating is fast enough this will work, otherwise it will result in a shard failure which
            // will cause a statement retry
            currentNodeId = shardRouting.relocatingNodeId();
            if (currentNodeId == null) {
                throw new ShardNotFoundException(shardRouting.shardId());
            }
        }
        Map<ShardId, List<PKAndVersion>> idsByShard = idsByShardByNode.get(currentNodeId);
        if (idsByShard == null) {
            idsByShard = new HashMap<>();
            idsByShardByNode.put(currentNodeId, idsByShard);
        }
        List<PKAndVersion> pkAndVersions = idsByShard.get(shardRouting.shardId());
        if (pkAndVersions == null) {
            pkAndVersions = new ArrayList<>();
            idsByShard.put(shardRouting.shardId(), pkAndVersions);
        }
        long version = docKey.version(plannerContext.transactionContext(), plannerContext.nodeContext(), params, subQueryResults).orElse(Versions.MATCH_ANY);
        long sequenceNumber = docKey.sequenceNo(plannerContext.transactionContext(), plannerContext.nodeContext(), params, subQueryResults).orElse(SequenceNumbers.UNASSIGNED_SEQ_NO);
        long primaryTerm = docKey.primaryTerm(plannerContext.transactionContext(), plannerContext.nodeContext(), params, subQueryResults).orElse(SequenceNumbers.UNASSIGNED_PRIMARY_TERM);
        pkAndVersions.add(new PKAndVersion(id, version, sequenceNumber, primaryTerm));
    }
    var docKeyColumns = new ArrayList<>(docTableInfo.primaryKey());
    docKeyColumns.addAll(docTableInfo.partitionedBy());
    docKeyColumns.add(docTableInfo.clusteredBy());
    docKeyColumns.add(DocSysColumns.VERSION);
    docKeyColumns.add(DocSysColumns.SEQ_NO);
    docKeyColumns.add(DocSysColumns.PRIMARY_TERM);
    var binder = new SubQueryAndParamBinder(params, subQueryResults);
    List<Symbol> boundOutputs = Lists2.map(outputs, binder);
    var boundQuery = binder.apply(query);
    // Collect all columns which are used inside the query
    // If the query contains only DocKeys, no filter is needed as all DocKeys are handled by the PKLookupOperation
    AtomicBoolean requiresAdditionalFilteringOnNonDocKeyColumns = new AtomicBoolean(false);
    var toCollectSet = new LinkedHashSet<>(boundOutputs);
    Consumer<Reference> addRefIfMatch = ref -> {
        toCollectSet.add(ref);
        if (docKeyColumns.contains(ref.column()) == false) {
            requiresAdditionalFilteringOnNonDocKeyColumns.set(true);
        }
    };
    RefVisitor.visitRefs(boundQuery, addRefIfMatch);
    var toCollect = boundOutputs;
    ArrayList<Projection> projections = new ArrayList<>();
    if (requiresAdditionalFilteringOnNonDocKeyColumns.get()) {
        toCollect = List.copyOf(toCollectSet);
        var filterProjection = ProjectionBuilder.filterProjection(toCollect, boundQuery);
        filterProjection.requiredGranularity(RowGranularity.SHARD);
        projections.add(filterProjection);
        // reduce outputs which have been added for the filter projection
        var evalProjection = new EvalProjection(InputColumn.mapToInputColumns(boundOutputs), RowGranularity.SHARD);
        projections.add(evalProjection);
    }
    var collect = new Collect(new PKLookupPhase(plannerContext.jobId(), plannerContext.nextExecutionPhaseId(), docTableInfo.partitionedBy(), toCollect, idsByShardByNode), TopN.NO_LIMIT, 0, toCollect.size(), docKeys.size(), null);
    for (var projection : projections) {
        collect.addProjection(projection);
    }
    return collect;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) ShardId(org.elasticsearch.index.shard.ShardId) IndexParts(io.crate.metadata.IndexParts) InputColumn(io.crate.expression.symbol.InputColumn) Versions(org.elasticsearch.common.lucene.uid.Versions) RelationName(io.crate.metadata.RelationName) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) PartitionName(io.crate.metadata.PartitionName) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) ArrayList(java.util.ArrayList) RefVisitor(io.crate.expression.symbol.RefVisitor) TableStats(io.crate.statistics.TableStats) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) Map(java.util.Map) SelectSymbol(io.crate.expression.symbol.SelectSymbol) PKLookupPhase(io.crate.execution.dsl.phases.PKLookupPhase) TopN(io.crate.execution.engine.pipeline.TopN) Nullable(javax.annotation.Nullable) LinkedHashSet(java.util.LinkedHashSet) ProjectionBuilder(io.crate.execution.dsl.projection.builder.ProjectionBuilder) DocSysColumns(io.crate.metadata.doc.DocSysColumns) DocTableInfo(io.crate.metadata.doc.DocTableInfo) DocKeys(io.crate.analyze.where.DocKeys) SequenceNumbers(org.elasticsearch.index.seqno.SequenceNumbers) Collection(java.util.Collection) Reference(io.crate.metadata.Reference) Set(java.util.Set) Lists2(io.crate.common.collections.Lists2) ExecutionPlan(io.crate.planner.ExecutionPlan) Consumer(java.util.function.Consumer) List(java.util.List) OrderBy(io.crate.analyze.OrderBy) RowGranularity(io.crate.metadata.RowGranularity) DocTableRelation(io.crate.analyze.relations.DocTableRelation) Row(io.crate.data.Row) Projection(io.crate.execution.dsl.projection.Projection) Symbol(io.crate.expression.symbol.Symbol) AbstractTableRelation(io.crate.analyze.relations.AbstractTableRelation) PlannerContext(io.crate.planner.PlannerContext) Collect(io.crate.planner.node.dql.Collect) EvalProjection(io.crate.execution.dsl.projection.EvalProjection) DocTableInfo(io.crate.metadata.doc.DocTableInfo) DocKeys(io.crate.analyze.where.DocKeys) HashMap(java.util.HashMap) Collect(io.crate.planner.node.dql.Collect) SelectSymbol(io.crate.expression.symbol.SelectSymbol) Symbol(io.crate.expression.symbol.Symbol) ArrayList(java.util.ArrayList) Projection(io.crate.execution.dsl.projection.Projection) EvalProjection(io.crate.execution.dsl.projection.EvalProjection) ShardId(org.elasticsearch.index.shard.ShardId) PKLookupPhase(io.crate.execution.dsl.phases.PKLookupPhase) ArrayList(java.util.ArrayList) List(java.util.List) Reference(io.crate.metadata.Reference) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) EvalProjection(io.crate.execution.dsl.projection.EvalProjection) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) HashMap(java.util.HashMap) Map(java.util.Map)

Example 12 with ShardNotFoundException

use of org.elasticsearch.index.shard.ShardNotFoundException in project crate by crate.

the class ShardCollectSource method createMultiShardScoreDocCollector.

private CompletableFuture<BatchIterator<Row>> createMultiShardScoreDocCollector(RoutedCollectPhase collectPhase, boolean supportMoveToStart, CollectTask collectTask, String localNodeId) {
    Map<String, Map<String, IntIndexedContainer>> locations = collectPhase.routing().locations();
    SharedShardContexts sharedShardContexts = collectTask.sharedShardContexts();
    Map<String, IntIndexedContainer> indexShards = locations.get(localNodeId);
    List<CompletableFuture<OrderedDocCollector>> orderedDocCollectors = new ArrayList<>();
    Metadata metadata = clusterService.state().metadata();
    for (Map.Entry<String, IntIndexedContainer> entry : indexShards.entrySet()) {
        String indexName = entry.getKey();
        Index index = metadata.index(indexName).getIndex();
        for (IntCursor shard : entry.getValue()) {
            ShardId shardId = new ShardId(index, shard.value);
            try {
                SharedShardContext context = sharedShardContexts.getOrCreateContext(shardId);
                ShardCollectorProvider shardCollectorProvider = getCollectorProviderSafe(shardId);
                orderedDocCollectors.add(shardCollectorProvider.getFutureOrderedCollector(collectPhase, context, collectTask, supportMoveToStart));
            } catch (ShardNotFoundException | IllegalIndexShardStateException e) {
                throw e;
            } catch (IndexNotFoundException e) {
                if (IndexParts.isPartitioned(indexName)) {
                    break;
                }
                throw e;
            }
        }
    }
    List<DataType<?>> columnTypes = Symbols.typeView(collectPhase.toCollect());
    OrderBy orderBy = collectPhase.orderBy();
    assert orderBy != null : "orderBy must not be null";
    return CompletableFutures.allAsList(orderedDocCollectors).thenApply(collectors -> OrderedLuceneBatchIteratorFactory.newInstance(collectors, OrderingByPosition.rowOrdering(OrderByPositionVisitor.orderByPositions(orderBy.orderBySymbols(), collectPhase.toCollect()), orderBy.reverseFlags(), orderBy.nullsFirst()), new RowAccountingWithEstimators(columnTypes, collectTask.getRamAccounting()), executor, availableThreads, supportMoveToStart));
}
Also used : OrderBy(io.crate.analyze.OrderBy) 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) IllegalIndexShardStateException(org.elasticsearch.index.shard.IllegalIndexShardStateException) ShardId(org.elasticsearch.index.shard.ShardId) CompletableFuture(java.util.concurrent.CompletableFuture) SharedShardContexts(io.crate.execution.jobs.SharedShardContexts) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) RowAccountingWithEstimators(io.crate.breaker.RowAccountingWithEstimators) IntCursor(com.carrotsearch.hppc.cursors.IntCursor) ShardCollectorProvider(io.crate.execution.engine.collect.ShardCollectorProvider) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) DataType(io.crate.types.DataType) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SharedShardContext(io.crate.execution.jobs.SharedShardContext)

Example 13 with ShardNotFoundException

use of org.elasticsearch.index.shard.ShardNotFoundException 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 14 with ShardNotFoundException

use of org.elasticsearch.index.shard.ShardNotFoundException in project crate by crate.

the class PKLookupOperation method lookup.

public BatchIterator<Row> lookup(UUID jobId, TransactionContext txnCtx, Supplier<RamAccounting> ramAccountingSupplier, Supplier<MemoryManager> memoryManagerSupplier, boolean ignoreMissing, Map<ShardId, List<PKAndVersion>> idsByShard, Collection<? extends Projection> projections, boolean requiresScroll, Function<Doc, Row> resultToRow) {
    ArrayList<BatchIterator<Row>> iterators = new ArrayList<>(idsByShard.size());
    for (Map.Entry<ShardId, List<PKAndVersion>> idsByShardEntry : idsByShard.entrySet()) {
        ShardId shardId = idsByShardEntry.getKey();
        IndexService indexService = indicesService.indexService(shardId.getIndex());
        if (indexService == null) {
            if (ignoreMissing) {
                continue;
            }
            throw new IndexNotFoundException(shardId.getIndex());
        }
        IndexShard shard = indexService.getShardOrNull(shardId.id());
        if (shard == null) {
            if (ignoreMissing) {
                continue;
            }
            throw new ShardNotFoundException(shardId);
        }
        Stream<Row> rowStream = idsByShardEntry.getValue().stream().map(pkAndVersion -> lookupDoc(shard, pkAndVersion.id(), pkAndVersion.version(), VersionType.EXTERNAL, pkAndVersion.seqNo(), pkAndVersion.primaryTerm())).filter(Objects::nonNull).map(resultToRow);
        if (projections.isEmpty()) {
            final Iterable<Row> rowIterable = requiresScroll ? rowStream.map(row -> new RowN(row.materialize())).collect(Collectors.toList()) : rowStream::iterator;
            iterators.add(InMemoryBatchIterator.of(rowIterable, SentinelRow.SENTINEL, true));
        } else {
            ProjectorFactory projectorFactory;
            try {
                projectorFactory = shardCollectSource.getProjectorFactory(shardId);
            } catch (ShardNotFoundException e) {
                if (ignoreMissing) {
                    continue;
                }
                throw e;
            }
            Projectors projectors = new Projectors(projections, jobId, txnCtx, ramAccountingSupplier.get(), memoryManagerSupplier.get(), projectorFactory);
            final Iterable<Row> rowIterable = requiresScroll && !projectors.providesIndependentScroll() ? rowStream.map(row -> new RowN(row.materialize())).collect(Collectors.toList()) : rowStream::iterator;
            iterators.add(projectors.wrap(InMemoryBatchIterator.of(rowIterable, SentinelRow.SENTINEL, true)));
        }
    }
    // noinspection unchecked
    return CompositeBatchIterator.seqComposite(iterators.toArray(new BatchIterator[0]));
}
Also used : IndexService(org.elasticsearch.index.IndexService) IndexShard(org.elasticsearch.index.shard.IndexShard) ArrayList(java.util.ArrayList) BatchIterator(io.crate.data.BatchIterator) InMemoryBatchIterator(io.crate.data.InMemoryBatchIterator) CompositeBatchIterator(io.crate.data.CompositeBatchIterator) ShardId(org.elasticsearch.index.shard.ShardId) Projectors(io.crate.execution.engine.pipeline.Projectors) RowN(io.crate.data.RowN) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) ProjectorFactory(io.crate.execution.engine.pipeline.ProjectorFactory) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) ArrayList(java.util.ArrayList) List(java.util.List) Row(io.crate.data.Row) SentinelRow(io.crate.data.SentinelRow) Map(java.util.Map)

Example 15 with ShardNotFoundException

use of org.elasticsearch.index.shard.ShardNotFoundException in project crate by crate.

the class AllocateReplicaAllocationCommand method execute.

@Override
public RerouteExplanation execute(RoutingAllocation allocation, boolean explain) {
    final DiscoveryNode discoNode;
    try {
        discoNode = allocation.nodes().resolveNode(node);
    } catch (IllegalArgumentException e) {
        return explainOrThrowRejectedCommand(explain, allocation, e);
    }
    final RoutingNodes routingNodes = allocation.routingNodes();
    RoutingNode routingNode = routingNodes.node(discoNode.getId());
    if (routingNode == null) {
        return explainOrThrowMissingRoutingNode(allocation, explain, discoNode);
    }
    try {
        allocation.routingTable().shardRoutingTable(index, shardId).primaryShard();
    } catch (IndexNotFoundException | ShardNotFoundException e) {
        return explainOrThrowRejectedCommand(explain, allocation, e);
    }
    ShardRouting primaryShardRouting = null;
    for (RoutingNode node : allocation.routingNodes()) {
        for (ShardRouting shard : node) {
            if (shard.getIndexName().equals(index) && shard.getId() == shardId && shard.primary()) {
                primaryShardRouting = shard;
                break;
            }
        }
    }
    if (primaryShardRouting == null) {
        return explainOrThrowRejectedCommand(explain, allocation, "trying to allocate a replica shard [" + index + "][" + shardId + "], while corresponding primary shard is still unassigned");
    }
    List<ShardRouting> replicaShardRoutings = new ArrayList<>();
    for (ShardRouting shard : allocation.routingNodes().unassigned()) {
        if (shard.getIndexName().equals(index) && shard.getId() == shardId && shard.primary() == false) {
            replicaShardRoutings.add(shard);
        }
    }
    ShardRouting shardRouting;
    if (replicaShardRoutings.isEmpty()) {
        return explainOrThrowRejectedCommand(explain, allocation, "all copies of [" + index + "][" + shardId + "] are already assigned. Use the move allocation command instead");
    } else {
        shardRouting = replicaShardRoutings.get(0);
    }
    Decision decision = allocation.deciders().canAllocate(shardRouting, routingNode, allocation);
    if (decision.type() == Decision.Type.NO) {
        // don't use explainOrThrowRejectedCommand to keep the original "NO" decision
        if (explain) {
            return new RerouteExplanation(this, decision);
        }
        throw new IllegalArgumentException("[" + name() + "] allocation of [" + index + "][" + shardId + "] on node " + discoNode + " is not allowed, reason: " + decision);
    }
    initializeUnassignedShard(allocation, routingNodes, routingNode, shardRouting);
    return new RerouteExplanation(this, decision);
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes) ArrayList(java.util.ArrayList) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) RerouteExplanation(org.elasticsearch.cluster.routing.allocation.RerouteExplanation) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) Decision(org.elasticsearch.cluster.routing.allocation.decider.Decision)

Aggregations

ShardNotFoundException (org.elasticsearch.index.shard.ShardNotFoundException)33 IndexNotFoundException (org.elasticsearch.index.IndexNotFoundException)23 ShardId (org.elasticsearch.index.shard.ShardId)16 ArrayList (java.util.ArrayList)9 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)9 Map (java.util.Map)8 RoutingNode (org.elasticsearch.cluster.routing.RoutingNode)8 IllegalIndexShardStateException (org.elasticsearch.index.shard.IllegalIndexShardStateException)8 ElasticsearchException (org.elasticsearch.ElasticsearchException)7 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)7 IndexService (org.elasticsearch.index.IndexService)7 IndexShard (org.elasticsearch.index.shard.IndexShard)7 IOException (java.io.IOException)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)6 RoutingNodes (org.elasticsearch.cluster.routing.RoutingNodes)6 RerouteExplanation (org.elasticsearch.cluster.routing.allocation.RerouteExplanation)6 List (java.util.List)5 ClusterState (org.elasticsearch.cluster.ClusterState)5 ShardCollectorProvider (io.crate.execution.engine.collect.ShardCollectorProvider)4