Search in sources :

Example 1 with RetryListener

use of io.crate.execution.support.RetryListener in project crate by crate.

the class ShardingUpsertExecutor method execRequests.

private CompletableFuture<UpsertResults> execRequests(ShardedRequests<ShardUpsertRequest, ShardUpsertRequest.Item> requests, final UpsertResults upsertResults) {
    if (requests.itemsByShard.isEmpty()) {
        requests.close();
        // could be that processing the source uri only results in errors, so no items per shard exists
        return CompletableFuture.completedFuture(upsertResults);
    }
    final AtomicInteger numRequests = new AtomicInteger(requests.itemsByShard.size());
    final AtomicReference<Exception> interrupt = new AtomicReference<>(null);
    final CompletableFuture<UpsertResults> resultFuture = new CompletableFuture<>();
    Iterator<Map.Entry<ShardLocation, ShardUpsertRequest>> it = requests.itemsByShard.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<ShardLocation, ShardUpsertRequest> entry = it.next();
        ShardUpsertRequest request = entry.getValue();
        it.remove();
        String nodeId = entry.getKey().nodeId;
        ConcurrencyLimit nodeLimit = nodeLimits.get(nodeId);
        ActionListener<ShardResponse> listener = new ShardResponseActionListener(numRequests, interrupt, upsertResults, resultCollector.accumulator(), requests.rowSourceInfos, nodeLimit, resultFuture);
        listener = new RetryListener<>(scheduler, l -> requestExecutor.execute(request, l), listener, BackoffPolicy.unlimitedDynamic(nodeLimit));
        requestExecutor.execute(request, listener);
    }
    return resultFuture.whenComplete((r, err) -> requests.close());
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ByteSizeUnit(org.elasticsearch.common.unit.ByteSizeUnit) Item(io.crate.execution.dml.upsert.ShardUpsertRequest.Item) ClusterService(org.elasticsearch.cluster.service.ClusterService) CollectExpression(io.crate.execution.engine.collect.CollectExpression) CompletableFuture(java.util.concurrent.CompletableFuture) BatchIterator(io.crate.data.BatchIterator) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) NodeLimits(io.crate.execution.jobs.NodeLimits) BackoffPolicy(org.elasticsearch.action.bulk.BackoffPolicy) TransportCreatePartitionsAction(org.elasticsearch.action.admin.indices.create.TransportCreatePartitionsAction) RetryListener(io.crate.execution.support.RetryListener) BlockBasedRamAccounting(io.crate.breaker.BlockBasedRamAccounting) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) CircuitBreaker(org.elasticsearch.common.breaker.CircuitBreaker) TypeGuessEstimateRowSize(io.crate.breaker.TypeGuessEstimateRowSize) ConcurrencyLimit(io.crate.concurrent.limits.ConcurrencyLimit) BulkRequestExecutor(org.elasticsearch.action.bulk.BulkRequestExecutor) ToLongFunction(java.util.function.ToLongFunction) Nullable(javax.annotation.Nullable) FutureActionListener(io.crate.action.FutureActionListener) Iterator(java.util.Iterator) Setting(org.elasticsearch.common.settings.Setting) Executor(java.util.concurrent.Executor) Predicate(java.util.function.Predicate) UUID(java.util.UUID) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) RamAccounting(io.crate.breaker.RamAccounting) TimeUnit(java.util.concurrent.TimeUnit) ShardResponse(io.crate.execution.dml.ShardResponse) ShardUpsertRequest(io.crate.execution.dml.upsert.ShardUpsertRequest) List(java.util.List) Logger(org.apache.logging.log4j.Logger) BatchIterators(io.crate.data.BatchIterators) Row(io.crate.data.Row) TimeValue(io.crate.common.unit.TimeValue) RowShardResolver(io.crate.execution.engine.collect.RowShardResolver) LogManager(org.apache.logging.log4j.LogManager) CreatePartitionsRequest(org.elasticsearch.action.admin.indices.create.CreatePartitionsRequest) ActionListener(org.elasticsearch.action.ActionListener) ConcurrencyLimit(io.crate.concurrent.limits.ConcurrencyLimit) ShardUpsertRequest(io.crate.execution.dml.upsert.ShardUpsertRequest) AtomicReference(java.util.concurrent.atomic.AtomicReference) ShardResponse(io.crate.execution.dml.ShardResponse) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map)

Example 2 with RetryListener

use of io.crate.execution.support.RetryListener in project crate by crate.

the class InsertFromValues method execute.

private CompletableFuture<ShardResponse.CompressedResult> execute(NodeLimits nodeLimits, ClusterState state, Collection<ShardUpsertRequest> shardUpsertRequests, TransportShardUpsertAction shardUpsertAction, ScheduledExecutorService scheduler) {
    ShardResponse.CompressedResult compressedResult = new ShardResponse.CompressedResult();
    if (shardUpsertRequests.isEmpty()) {
        return CompletableFuture.completedFuture(compressedResult);
    }
    CompletableFuture<ShardResponse.CompressedResult> result = new CompletableFuture<>();
    AtomicInteger numRequests = new AtomicInteger(shardUpsertRequests.size());
    AtomicReference<Throwable> lastFailure = new AtomicReference<>(null);
    Consumer<ShardUpsertRequest> countdown = request -> {
        if (numRequests.decrementAndGet() == 0) {
            Throwable throwable = lastFailure.get();
            if (throwable == null) {
                result.complete(compressedResult);
            } else {
                throwable = SQLExceptions.unwrap(throwable, t -> t instanceof RuntimeException);
                // we want to report duplicate key exceptions
                if (!SQLExceptions.isDocumentAlreadyExistsException(throwable) && (partitionWasDeleted(throwable, request.index()) || partitionClosed(throwable, request.index()) || mixedArgumentTypesFailure(throwable))) {
                    result.complete(compressedResult);
                } else {
                    result.completeExceptionally(throwable);
                }
            }
        }
    };
    for (ShardUpsertRequest request : shardUpsertRequests) {
        String nodeId;
        try {
            nodeId = state.routingTable().shardRoutingTable(request.shardId()).primaryShard().currentNodeId();
        } catch (IndexNotFoundException e) {
            lastFailure.set(e);
            if (!IndexParts.isPartitioned(request.index())) {
                synchronized (compressedResult) {
                    compressedResult.markAsFailed(request.items());
                }
            }
            countdown.accept(request);
            continue;
        }
        final ConcurrencyLimit nodeLimit = nodeLimits.get(nodeId);
        final long startTime = nodeLimit.startSample();
        ActionListener<ShardResponse> listener = new ActionListener<>() {

            @Override
            public void onResponse(ShardResponse shardResponse) {
                Throwable throwable = shardResponse.failure();
                if (throwable == null) {
                    nodeLimit.onSample(startTime, false);
                    synchronized (compressedResult) {
                        compressedResult.update(shardResponse);
                    }
                } else {
                    nodeLimit.onSample(startTime, true);
                    lastFailure.set(throwable);
                }
                countdown.accept(request);
            }

            @Override
            public void onFailure(Exception e) {
                nodeLimit.onSample(startTime, true);
                Throwable t = SQLExceptions.unwrap(e);
                if (!partitionWasDeleted(t, request.index())) {
                    synchronized (compressedResult) {
                        compressedResult.markAsFailed(request.items());
                    }
                }
                lastFailure.set(t);
                countdown.accept(request);
            }
        };
        shardUpsertAction.execute(request, new RetryListener<>(scheduler, l -> shardUpsertAction.execute(request, l), listener, BackoffPolicy.limitedDynamic(nodeLimit)));
    }
    return result;
}
Also used : GeneratedColumns(io.crate.execution.dml.upsert.GeneratedColumns) IndexParts(io.crate.metadata.IndexParts) INDEX_CLOSED_BLOCK(org.elasticsearch.cluster.metadata.IndexMetadata.INDEX_CLOSED_BLOCK) Arrays(java.util.Arrays) TransportShardUpsertAction(io.crate.execution.dml.upsert.TransportShardUpsertAction) ShardIterator(org.elasticsearch.cluster.routing.ShardIterator) ShardedRequests(io.crate.execution.engine.indexing.ShardedRequests) TableFunctionRelation(io.crate.analyze.relations.TableFunctionRelation) NodeLimits(io.crate.execution.jobs.NodeLimits) TransportCreatePartitionsAction(org.elasticsearch.action.admin.indices.create.TransportCreatePartitionsAction) RetryListener(io.crate.execution.support.RetryListener) DependencyCarrier(io.crate.planner.DependencyCarrier) ClusterState(org.elasticsearch.cluster.ClusterState) RowN(io.crate.data.RowN) SymbolEvaluator(io.crate.analyze.SymbolEvaluator) TableStats(io.crate.statistics.TableStats) ClusterBlock(org.elasticsearch.cluster.block.ClusterBlock) ColumnIndexWriterProjection(io.crate.execution.dsl.projection.ColumnIndexWriterProjection) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntArrayList(com.carrotsearch.hppc.IntArrayList) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) Map(java.util.Map) TypeGuessEstimateRowSize(io.crate.breaker.TypeGuessEstimateRowSize) ConcurrencyLimit(io.crate.concurrent.limits.ConcurrencyLimit) SelectSymbol(io.crate.expression.symbol.SelectSymbol) GroupRowsByShard(io.crate.execution.engine.indexing.GroupRowsByShard) DocTableInfo(io.crate.metadata.doc.DocTableInfo) Collection(java.util.Collection) InMemoryBatchIterator(io.crate.data.InMemoryBatchIterator) Set(java.util.Set) UUID(java.util.UUID) InputRow(io.crate.expression.InputRow) ShardRequest(io.crate.execution.dml.ShardRequest) ExecutionPlan(io.crate.planner.ExecutionPlan) List(java.util.List) OrderBy(io.crate.analyze.OrderBy) Row(io.crate.data.Row) Symbol(io.crate.expression.symbol.Symbol) RowShardResolver(io.crate.execution.engine.collect.RowShardResolver) Assignments(io.crate.expression.symbol.Assignments) Row1(io.crate.data.Row1) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) Input(io.crate.data.Input) SENTINEL(io.crate.data.SentinelRow.SENTINEL) ClusterService(org.elasticsearch.cluster.service.ClusterService) CollectExpression(io.crate.execution.engine.collect.CollectExpression) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Operation(io.crate.metadata.table.Operation) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) InsertSourceFromCells(io.crate.execution.dml.upsert.InsertSourceFromCells) ArrayList(java.util.ArrayList) BackoffPolicy(org.elasticsearch.action.bulk.BackoffPolicy) Metadata(org.elasticsearch.cluster.metadata.Metadata) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) ShardLocation(io.crate.execution.engine.indexing.ShardLocation) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) StreamSupport(java.util.stream.StreamSupport) ColumnValidationException(io.crate.exceptions.ColumnValidationException) Nullable(javax.annotation.Nullable) FutureActionListener(io.crate.action.FutureActionListener) ProjectionBuilder(io.crate.execution.dsl.projection.builder.ProjectionBuilder) BULK_REQUEST_TIMEOUT_SETTING(io.crate.execution.engine.indexing.ShardingUpsertExecutor.BULK_REQUEST_TIMEOUT_SETTING) Iterator(java.util.Iterator) Reference(io.crate.metadata.Reference) DataType(io.crate.types.DataType) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) RamAccounting(io.crate.breaker.RamAccounting) Consumer(java.util.function.Consumer) RowConsumer(io.crate.data.RowConsumer) ShardResponse(io.crate.execution.dml.ShardResponse) ShardUpsertRequest(io.crate.execution.dml.upsert.ShardUpsertRequest) CollectionBucket(io.crate.data.CollectionBucket) TableFunctionImplementation(io.crate.metadata.tablefunctions.TableFunctionImplementation) IndexNameResolver(io.crate.execution.engine.indexing.IndexNameResolver) NotSerializableExceptionWrapper(org.elasticsearch.common.io.stream.NotSerializableExceptionWrapper) AbstractTableRelation(io.crate.analyze.relations.AbstractTableRelation) PlannerContext(io.crate.planner.PlannerContext) InputColumns(io.crate.execution.dsl.projection.builder.InputColumns) SQLExceptions(io.crate.exceptions.SQLExceptions) InputFactory(io.crate.expression.InputFactory) CreatePartitionsRequest(org.elasticsearch.action.admin.indices.create.CreatePartitionsRequest) ActionListener(org.elasticsearch.action.ActionListener) ConcurrencyLimit(io.crate.concurrent.limits.ConcurrencyLimit) ShardUpsertRequest(io.crate.execution.dml.upsert.ShardUpsertRequest) AtomicReference(java.util.concurrent.atomic.AtomicReference) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) ColumnValidationException(io.crate.exceptions.ColumnValidationException) ShardResponse(io.crate.execution.dml.ShardResponse) CompletableFuture(java.util.concurrent.CompletableFuture) FutureActionListener(io.crate.action.FutureActionListener) ActionListener(org.elasticsearch.action.ActionListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException)

Aggregations

FutureActionListener (io.crate.action.FutureActionListener)2 RamAccounting (io.crate.breaker.RamAccounting)2 TypeGuessEstimateRowSize (io.crate.breaker.TypeGuessEstimateRowSize)2 ConcurrencyLimit (io.crate.concurrent.limits.ConcurrencyLimit)2 Row (io.crate.data.Row)2 ShardResponse (io.crate.execution.dml.ShardResponse)2 ShardUpsertRequest (io.crate.execution.dml.upsert.ShardUpsertRequest)2 CollectExpression (io.crate.execution.engine.collect.CollectExpression)2 RowShardResolver (io.crate.execution.engine.collect.RowShardResolver)2 NodeLimits (io.crate.execution.jobs.NodeLimits)2 RetryListener (io.crate.execution.support.RetryListener)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Map (java.util.Map)2 UUID (java.util.UUID)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Function (java.util.function.Function)2