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);
}
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));
}
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'"));
}
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);
}
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());
}
Aggregations