Search in sources :

Example 1 with ShardResponse

use of io.crate.execution.dml.ShardResponse in project crate by crate.

the class TransportShardDeleteAction method processRequestItems.

@Override
protected WritePrimaryResult<ShardDeleteRequest, ShardResponse> processRequestItems(IndexShard indexShard, ShardDeleteRequest request, AtomicBoolean killed) throws IOException {
    ShardResponse shardResponse = new ShardResponse();
    Translog.Location translogLocation = null;
    boolean debugEnabled = logger.isDebugEnabled();
    for (ShardDeleteRequest.Item item : request.items()) {
        int location = item.location();
        if (killed.get()) {
            // set failure on response, mark current item and skip all next items.
            // this way replica operation will be executed, but only items already processed here
            // will be processed on the replica
            request.skipFromLocation(location);
            shardResponse.failure(new InterruptedException(JobKilledException.MESSAGE));
            break;
        }
        try {
            Engine.DeleteResult deleteResult = shardDeleteOperationOnPrimary(item, indexShard);
            translogLocation = deleteResult.getTranslogLocation();
            Exception failure = deleteResult.getFailure();
            if (failure == null) {
                if (deleteResult.isFound()) {
                    if (debugEnabled) {
                        logger.debug("shardId={} successfully deleted id={}", request.shardId(), item.id());
                    }
                    shardResponse.add(location);
                } else {
                    if (debugEnabled) {
                        logger.debug("shardId={} failed to execute delete for id={}, doc not found", request.shardId(), item.id());
                    }
                    shardResponse.add(location, new ShardResponse.Failure(item.id(), "Document not found while deleting", false));
                }
            } else {
                if (debugEnabled) {
                    logger.debug("shardId={} failed to execute delete for id={}: {}", request.shardId(), item.id(), failure);
                }
                shardResponse.add(location, new ShardResponse.Failure(item.id(), userFriendlyMessageInclNested(failure), (failure instanceof VersionConflictEngineException)));
            }
        } catch (Exception e) {
            if (!TransportActions.isShardNotAvailableException(e)) {
                throw e;
            } else {
                if (debugEnabled) {
                    logger.debug("shardId={} failed to execute delete for id={}: {}", request.shardId(), item.id(), e);
                }
                shardResponse.add(location, new ShardResponse.Failure(item.id(), userFriendlyMessageInclNested(e), (e instanceof VersionConflictEngineException)));
            }
        }
    }
    return new WritePrimaryResult<>(request, shardResponse, translogLocation, null, indexShard);
}
Also used : JobKilledException(io.crate.exceptions.JobKilledException) IOException(java.io.IOException) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) Translog(org.elasticsearch.index.translog.Translog) ShardResponse(io.crate.execution.dml.ShardResponse) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) Engine(org.elasticsearch.index.engine.Engine)

Example 2 with ShardResponse

use of io.crate.execution.dml.ShardResponse in project crate by crate.

the class TransportShardUpsertActionTest method testKilledSetWhileProcessingItemsDoesNotThrowException.

@Test
public void testKilledSetWhileProcessingItemsDoesNotThrowException() throws Exception {
    ShardId shardId = new ShardId(TABLE_IDENT.indexNameOrAlias(), charactersIndexUUID, 0);
    ShardUpsertRequest request = new ShardUpsertRequest.Builder(DUMMY_SESSION_INFO, TimeValue.timeValueSeconds(30), DuplicateKeyAction.UPDATE_OR_FAIL, false, null, new Reference[] { ID_REF }, null, UUID.randomUUID(), false).newRequest(shardId);
    request.add(1, new ShardUpsertRequest.Item("1", null, new Object[] { 1 }, null, null, null));
    TransportWriteAction.WritePrimaryResult<ShardUpsertRequest, ShardResponse> result = transportShardUpsertAction.processRequestItems(indexShard, request, new AtomicBoolean(true));
    assertThat(result.finalResponseIfSuccessful.failure(), instanceOf(InterruptedException.class));
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ShardResponse(io.crate.execution.dml.ShardResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Reference(io.crate.metadata.Reference) TransportWriteAction(org.elasticsearch.action.support.replication.TransportWriteAction) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 3 with ShardResponse

use of io.crate.execution.dml.ShardResponse in project crate by crate.

the class TransportShardUpsertActionTest method testExceptionWhileProcessingItemsContinueOnError.

@Test
public void testExceptionWhileProcessingItemsContinueOnError() throws Exception {
    ShardId shardId = new ShardId(TABLE_IDENT.indexNameOrAlias(), charactersIndexUUID, 0);
    ShardUpsertRequest request = new ShardUpsertRequest.Builder(DUMMY_SESSION_INFO, TimeValue.timeValueSeconds(30), DuplicateKeyAction.UPDATE_OR_FAIL, true, null, new Reference[] { ID_REF }, null, UUID.randomUUID(), false).newRequest(shardId);
    request.add(1, new ShardUpsertRequest.Item("1", null, new Object[] { 1 }, null, null, null));
    TransportWriteAction.WritePrimaryResult<ShardUpsertRequest, ShardResponse> result = transportShardUpsertAction.processRequestItems(indexShard, request, new AtomicBoolean(false));
    ShardResponse response = result.finalResponseIfSuccessful;
    assertThat(response.failures().size(), is(1));
    assertThat(response.failures().get(0).message(), is("[1]: version conflict, document with id: 1 already exists in 'characters'"));
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ShardResponse(io.crate.execution.dml.ShardResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Reference(io.crate.metadata.Reference) TransportWriteAction(org.elasticsearch.action.support.replication.TransportWriteAction) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 4 with ShardResponse

use of io.crate.execution.dml.ShardResponse in project crate by crate.

the class TransportShardUpsertAction method processRequestItems.

@Override
protected WritePrimaryResult<ShardUpsertRequest, ShardResponse> processRequestItems(IndexShard indexShard, ShardUpsertRequest request, AtomicBoolean killed) {
    ShardResponse shardResponse = new ShardResponse(request.returnValues());
    String indexName = request.index();
    DocTableInfo tableInfo = schemas.getTableInfo(RelationName.fromIndexName(indexName), Operation.INSERT);
    Reference[] insertColumns = request.insertColumns();
    GeneratedColumns.Validation valueValidation = request.validateConstraints() ? GeneratedColumns.Validation.VALUE_MATCH : GeneratedColumns.Validation.NONE;
    TransactionContext txnCtx = TransactionContext.of(request.sessionSettings());
    InsertSourceGen insertSourceGen = insertColumns == null ? null : InsertSourceGen.of(txnCtx, nodeCtx, tableInfo, indexName, valueValidation, Arrays.asList(insertColumns));
    UpdateSourceGen updateSourceGen = request.updateColumns() == null ? null : new UpdateSourceGen(txnCtx, nodeCtx, tableInfo, request.updateColumns());
    ReturnValueGen returnValueGen = request.returnValues() == null ? null : new ReturnValueGen(txnCtx, nodeCtx, tableInfo, request.returnValues());
    Translog.Location translogLocation = null;
    for (ShardUpsertRequest.Item item : request.items()) {
        int location = item.location();
        if (killed.get()) {
            // set failure on response and skip all next items.
            // this way replica operation will be executed, but only items with a valid source (= was processed on primary)
            // will be processed on the replica
            shardResponse.failure(new InterruptedException());
            break;
        }
        try {
            IndexItemResponse indexItemResponse = indexItem(request, item, indexShard, updateSourceGen, insertSourceGen, returnValueGen);
            if (indexItemResponse != null) {
                if (indexItemResponse.translog != null) {
                    shardResponse.add(location);
                    translogLocation = indexItemResponse.translog;
                }
                if (indexItemResponse.returnValues != null) {
                    shardResponse.addResultRows(indexItemResponse.returnValues);
                }
            }
        } catch (Exception e) {
            if (retryPrimaryException(e)) {
                throw Exceptions.toRuntimeException(e);
            }
            if (logger.isDebugEnabled()) {
                logger.debug("Failed to execute upsert on nodeName={}, shardId={} id={} error={}", clusterService.localNode().getName(), request.shardId(), item.id(), e);
            }
            // *mark* the item as failed by setting the source to null
            // to prevent the replica operation from processing this concrete item
            item.source(null);
            if (!request.continueOnError()) {
                shardResponse.failure(e);
                break;
            }
            shardResponse.add(location, new ShardResponse.Failure(item.id(), userFriendlyCrateExceptionTopOnly(e), (e instanceof VersionConflictEngineException)));
        }
    }
    return new WritePrimaryResult<>(request, shardResponse, translogLocation, null, indexShard);
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) GeneratedReference(io.crate.metadata.GeneratedReference) BytesReference(org.elasticsearch.common.bytes.BytesReference) Reference(io.crate.metadata.Reference) UncheckedIOException(java.io.UncheckedIOException) DocumentMissingException(org.elasticsearch.index.engine.DocumentMissingException) DocumentSourceMissingException(org.elasticsearch.index.engine.DocumentSourceMissingException) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) IOException(java.io.IOException) Translog(org.elasticsearch.index.translog.Translog) ShardResponse(io.crate.execution.dml.ShardResponse) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) TransactionContext(io.crate.metadata.TransactionContext)

Example 5 with ShardResponse

use of io.crate.execution.dml.ShardResponse 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)

Aggregations

ShardResponse (io.crate.execution.dml.ShardResponse)9 Reference (io.crate.metadata.Reference)5 ShardId (org.elasticsearch.index.shard.ShardId)5 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 TransportWriteAction (org.elasticsearch.action.support.replication.TransportWriteAction)4 Test (org.junit.Test)4 FutureActionListener (io.crate.action.FutureActionListener)3 ConcurrencyLimit (io.crate.concurrent.limits.ConcurrencyLimit)3 VersionConflictEngineException (org.elasticsearch.index.engine.VersionConflictEngineException)3 RamAccounting (io.crate.breaker.RamAccounting)2 TypeGuessEstimateRowSize (io.crate.breaker.TypeGuessEstimateRowSize)2 Row (io.crate.data.Row)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 DocTableInfo (io.crate.metadata.doc.DocTableInfo)2 Iterator (java.util.Iterator)2