use of org.opensearch.action.DocWriteRequest in project OpenSearch by opensearch-project.
the class TransportShardBulkAction method onComplete.
private static void onComplete(Engine.Result r, BulkPrimaryExecutionContext context, UpdateHelper.Result updateResult) {
context.markOperationAsExecuted(r);
final DocWriteRequest<?> docWriteRequest = context.getCurrent();
final DocWriteRequest.OpType opType = docWriteRequest.opType();
final boolean isUpdate = opType == DocWriteRequest.OpType.UPDATE;
final BulkItemResponse executionResult = context.getExecutionResult();
final boolean isFailed = executionResult.isFailed();
if (isUpdate && isFailed && isConflictException(executionResult.getFailure().getCause()) && context.getRetryCounter() < ((UpdateRequest) docWriteRequest).retryOnConflict()) {
context.resetForExecutionForRetry();
return;
}
final BulkItemResponse response;
if (isUpdate) {
response = processUpdateResponse((UpdateRequest) docWriteRequest, context.getConcreteIndex(), executionResult, updateResult);
} else {
if (isFailed) {
final Exception failure = executionResult.getFailure().getCause();
final MessageSupplier messageSupplier = () -> new ParameterizedMessage("{} failed to execute bulk item ({}) {}", context.getPrimary().shardId(), opType.getLowercase(), docWriteRequest);
if (TransportShardBulkAction.isConflictException(failure)) {
logger.trace(messageSupplier, failure);
} else {
logger.debug(messageSupplier, failure);
}
}
response = executionResult;
}
context.markAsCompleted(response);
assert context.isInitial();
}
use of org.opensearch.action.DocWriteRequest in project OpenSearch by opensearch-project.
the class TransportShardBulkActionTests method testExecuteBulkIndexRequestWithErrorWhileUpdatingMapping.
public void testExecuteBulkIndexRequestWithErrorWhileUpdatingMapping() throws Exception {
IndexShard shard = newStartedShard(true);
BulkItemRequest[] items = new BulkItemRequest[1];
DocWriteRequest<IndexRequest> writeRequest = new IndexRequest("index").id("id").source(Requests.INDEX_CONTENT_TYPE, "foo", "bar");
items[0] = new BulkItemRequest(0, writeRequest);
BulkShardRequest bulkShardRequest = new BulkShardRequest(shardId, RefreshPolicy.NONE, items);
// Return an exception when trying to update the mapping, or when waiting for it to come
RuntimeException err = new RuntimeException("some kind of exception");
boolean errorOnWait = randomBoolean();
randomlySetIgnoredPrimaryResponse(items[0]);
BulkPrimaryExecutionContext context = new BulkPrimaryExecutionContext(bulkShardRequest, shard);
final CountDownLatch latch = new CountDownLatch(1);
TransportShardBulkAction.executeBulkItemRequest(context, null, threadPool::absoluteTimeInMillis, errorOnWait == false ? new ThrowingMappingUpdatePerformer(err) : new NoopMappingUpdatePerformer(), errorOnWait ? listener -> listener.onFailure(err) : listener -> listener.onResponse(null), new LatchedActionListener<>(new ActionListener<Void>() {
@Override
public void onResponse(Void aVoid) {
}
@Override
public void onFailure(final Exception e) {
assertEquals(err, e);
}
}, latch));
latch.await();
assertFalse(context.hasMoreOperationsToExecute());
// Translog shouldn't be synced, as there were conflicting mappings
assertThat(context.getLocationToSync(), nullValue());
BulkItemResponse primaryResponse = bulkShardRequest.items()[0].getPrimaryResponse();
// Since this was not a conflict failure, the primary response
// should be filled out with the failure information
assertThat(primaryResponse.getItemId(), equalTo(0));
assertThat(primaryResponse.getId(), equalTo("id"));
assertThat(primaryResponse.getOpType(), equalTo(DocWriteRequest.OpType.INDEX));
assertTrue(primaryResponse.isFailed());
assertThat(primaryResponse.getFailureMessage(), containsString("some kind of exception"));
BulkItemResponse.Failure failure = primaryResponse.getFailure();
assertThat(failure.getIndex(), equalTo("index"));
assertThat(failure.getId(), equalTo("id"));
assertThat(failure.getCause(), equalTo(err));
closeShards(shard);
}
use of org.opensearch.action.DocWriteRequest in project OpenSearch by opensearch-project.
the class BulkPrimaryExecutionContextTests method testAbortedSkipped.
public void testAbortedSkipped() {
BulkShardRequest shardRequest = generateRandomRequest();
ArrayList<DocWriteRequest<?>> nonAbortedRequests = new ArrayList<>();
for (BulkItemRequest request : shardRequest.items()) {
if (randomBoolean()) {
request.abort("index", new OpenSearchException("bla"));
} else {
nonAbortedRequests.add(request.request());
}
}
ArrayList<DocWriteRequest<?>> visitedRequests = new ArrayList<>();
for (BulkPrimaryExecutionContext context = new BulkPrimaryExecutionContext(shardRequest, null); context.hasMoreOperationsToExecute(); ) {
visitedRequests.add(context.getCurrent());
context.setRequestToExecute(context.getCurrent());
// using failures prevents caring about types
context.markOperationAsExecuted(new Engine.IndexResult(new OpenSearchException("bla"), 1));
context.markAsCompleted(context.getExecutionResult());
}
assertThat(visitedRequests, equalTo(nonAbortedRequests));
}
use of org.opensearch.action.DocWriteRequest in project OpenSearch by opensearch-project.
the class IndexNameExpressionResolverTests method testConcreteWriteIndexWithNoWriteIndexWithMultipleIndices.
public void testConcreteWriteIndexWithNoWriteIndexWithMultipleIndices() {
Metadata.Builder mdBuilder = Metadata.builder().put(indexBuilder("test-0").state(State.OPEN).putAlias(AliasMetadata.builder("test-alias").writeIndex(randomFrom(false, null)))).put(indexBuilder("test-1").state(State.OPEN).putAlias(AliasMetadata.builder("test-alias").writeIndex(randomFrom(false, null))));
ClusterState state = ClusterState.builder(new ClusterName("_name")).metadata(mdBuilder).build();
String[] strings = indexNameExpressionResolver.indexAliases(state, "test-0", x -> true, true, new HashSet<>(Arrays.asList("test-0", "test-1", "test-alias")));
Arrays.sort(strings);
assertArrayEquals(new String[] { "test-alias" }, strings);
DocWriteRequest request = randomFrom(new IndexRequest("test-alias"), new UpdateRequest("test-alias", "_id"), new DeleteRequest("test-alias"));
IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> indexNameExpressionResolver.concreteWriteIndex(state, request.indicesOptions(), request.indices()[0], false, false));
assertThat(exception.getMessage(), equalTo("no write index is defined for alias [test-alias]." + " The write index may be explicitly disabled using is_write_index=false or the alias points to multiple" + " indices without one being designated as a write index"));
}
use of org.opensearch.action.DocWriteRequest in project OpenSearch by opensearch-project.
the class IndexNameExpressionResolverTests method testConcreteWriteIndexWithNoWriteIndexWithSingleIndex.
public void testConcreteWriteIndexWithNoWriteIndexWithSingleIndex() {
Metadata.Builder mdBuilder = Metadata.builder().put(indexBuilder("test-0").state(State.OPEN).putAlias(AliasMetadata.builder("test-alias").writeIndex(false)));
ClusterState state = ClusterState.builder(new ClusterName("_name")).metadata(mdBuilder).build();
String[] strings = indexNameExpressionResolver.indexAliases(state, "test-0", x -> true, true, new HashSet<>(Arrays.asList("test-0", "test-alias")));
Arrays.sort(strings);
assertArrayEquals(new String[] { "test-alias" }, strings);
DocWriteRequest request = randomFrom(new IndexRequest("test-alias"), new UpdateRequest("test-alias", "_id"), new DeleteRequest("test-alias"));
IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> indexNameExpressionResolver.concreteWriteIndex(state, request.indicesOptions(), request.indices()[0], false, false));
assertThat(exception.getMessage(), equalTo("no write index is defined for alias [test-alias]." + " The write index may be explicitly disabled using is_write_index=false or the alias points to multiple" + " indices without one being designated as a write index"));
}
Aggregations