use of org.opensearch.action.bulk.BulkItemResponse.Failure in project OpenSearch by opensearch-project.
the class AbstractAsyncBulkByScrollAction method onBulkResponse.
/**
* Processes bulk responses, accounting for failures.
*/
void onBulkResponse(BulkResponse response, Runnable onSuccess) {
try {
List<Failure> failures = new ArrayList<>();
Set<String> destinationIndicesThisBatch = new HashSet<>();
for (BulkItemResponse item : response) {
if (item.isFailed()) {
recordFailure(item.getFailure(), failures);
continue;
}
switch(item.getOpType()) {
case CREATE:
case INDEX:
if (item.getResponse().getResult() == DocWriteResponse.Result.CREATED) {
worker.countCreated();
} else {
worker.countUpdated();
}
break;
case UPDATE:
worker.countUpdated();
break;
case DELETE:
worker.countDeleted();
break;
}
// Track the indexes we've seen so we can refresh them if requested
destinationIndicesThisBatch.add(item.getIndex());
}
if (task.isCancelled()) {
logger.debug("[{}]: Finishing early because the task was cancelled", task.getId());
finishHim(null);
return;
}
addDestinationIndices(destinationIndicesThisBatch);
if (false == failures.isEmpty()) {
refreshAndFinish(unmodifiableList(failures), emptyList(), false);
return;
}
if (mainRequest.getMaxDocs() != MAX_DOCS_ALL_MATCHES && worker.getSuccessfullyProcessed() >= mainRequest.getMaxDocs()) {
// We've processed all the requested docs.
refreshAndFinish(emptyList(), emptyList(), false);
return;
}
onSuccess.run();
} catch (Exception t) {
finishHim(t);
}
}
use of org.opensearch.action.bulk.BulkItemResponse.Failure in project OpenSearch by opensearch-project.
the class BulkByScrollResponseTests method assertResponseEquals.
private void assertResponseEquals(BulkByScrollResponse expected, BulkByScrollResponse actual) {
assertEquals(expected.getTook(), actual.getTook());
BulkByScrollTaskStatusTests.assertTaskStatusEquals(Version.CURRENT, expected.getStatus(), actual.getStatus());
assertEquals(expected.getBulkFailures().size(), actual.getBulkFailures().size());
for (int i = 0; i < expected.getBulkFailures().size(); i++) {
Failure expectedFailure = expected.getBulkFailures().get(i);
Failure actualFailure = actual.getBulkFailures().get(i);
assertEquals(expectedFailure.getIndex(), actualFailure.getIndex());
assertEquals(expectedFailure.getId(), actualFailure.getId());
assertEquals(expectedFailure.getMessage(), actualFailure.getMessage());
assertEquals(expectedFailure.getStatus(), actualFailure.getStatus());
}
assertEquals(expected.getSearchFailures().size(), actual.getSearchFailures().size());
for (int i = 0; i < expected.getSearchFailures().size(); i++) {
ScrollableHitSource.SearchFailure expectedFailure = expected.getSearchFailures().get(i);
ScrollableHitSource.SearchFailure actualFailure = actual.getSearchFailures().get(i);
assertEquals(expectedFailure.getIndex(), actualFailure.getIndex());
assertEquals(expectedFailure.getShardId(), actualFailure.getShardId());
assertEquals(expectedFailure.getNodeId(), actualFailure.getNodeId());
assertEquals(expectedFailure.getReason().getClass(), actualFailure.getReason().getClass());
assertEquals(expectedFailure.getReason().getMessage(), actualFailure.getReason().getMessage());
assertEquals(expectedFailure.getStatus(), actualFailure.getStatus());
}
}
use of org.opensearch.action.bulk.BulkItemResponse.Failure in project OpenSearch by opensearch-project.
the class BulkByScrollResponseTests method assertEqualBulkResponse.
public static void assertEqualBulkResponse(BulkByScrollResponse expected, BulkByScrollResponse actual, boolean includeUpdated, boolean includeCreated) {
assertEquals(expected.getTook(), actual.getTook());
BulkByScrollTaskStatusTests.assertEqualStatus(expected.getStatus(), actual.getStatus(), includeUpdated, includeCreated);
assertEquals(expected.getBulkFailures().size(), actual.getBulkFailures().size());
for (int i = 0; i < expected.getBulkFailures().size(); i++) {
Failure expectedFailure = expected.getBulkFailures().get(i);
Failure actualFailure = actual.getBulkFailures().get(i);
assertEquals(expectedFailure.getIndex(), actualFailure.getIndex());
assertEquals(expectedFailure.getId(), actualFailure.getId());
assertEquals(expectedFailure.getStatus(), actualFailure.getStatus());
}
assertEquals(expected.getSearchFailures().size(), actual.getSearchFailures().size());
for (int i = 0; i < expected.getSearchFailures().size(); i++) {
ScrollableHitSource.SearchFailure expectedFailure = expected.getSearchFailures().get(i);
ScrollableHitSource.SearchFailure actualFailure = actual.getSearchFailures().get(i);
assertEquals(expectedFailure.getIndex(), actualFailure.getIndex());
assertEquals(expectedFailure.getShardId(), actualFailure.getShardId());
assertEquals(expectedFailure.getNodeId(), actualFailure.getNodeId());
assertEquals(expectedFailure.getStatus(), actualFailure.getStatus());
}
}
use of org.opensearch.action.bulk.BulkItemResponse.Failure in project OpenSearch by opensearch-project.
the class BulkItemResponseTests method testFailureToAndFromXContent.
public void testFailureToAndFromXContent() throws IOException {
final XContentType xContentType = randomFrom(XContentType.values());
int itemId = randomIntBetween(0, 100);
String index = randomAlphaOfLength(5);
String id = randomAlphaOfLength(5);
DocWriteRequest.OpType opType = randomFrom(DocWriteRequest.OpType.values());
final Tuple<Throwable, OpenSearchException> exceptions = randomExceptions();
Exception bulkItemCause = (Exception) exceptions.v1();
Failure bulkItemFailure = new Failure(index, id, bulkItemCause);
BulkItemResponse bulkItemResponse = new BulkItemResponse(itemId, opType, bulkItemFailure);
Failure expectedBulkItemFailure = new Failure(index, id, exceptions.v2(), ExceptionsHelper.status(bulkItemCause));
BulkItemResponse expectedBulkItemResponse = new BulkItemResponse(itemId, opType, expectedBulkItemFailure);
BytesReference originalBytes = toShuffledXContent(bulkItemResponse, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean());
// Shuffle the XContent fields
if (randomBoolean()) {
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
originalBytes = BytesReference.bytes(shuffleXContent(parser, randomBoolean()));
}
}
BulkItemResponse parsedBulkItemResponse;
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
parsedBulkItemResponse = BulkItemResponse.fromXContent(parser, itemId);
assertNull(parser.nextToken());
}
assertBulkItemResponse(expectedBulkItemResponse, parsedBulkItemResponse);
}
use of org.opensearch.action.bulk.BulkItemResponse.Failure in project OpenSearch by opensearch-project.
the class BulkItemResponseTests method testFailureToString.
public void testFailureToString() {
Failure failure = new Failure("index", "id", new RuntimeException("test"));
String toString = failure.toString();
assertThat(toString, containsString("\"type\":\"runtime_exception\""));
assertThat(toString, containsString("\"reason\":\"test\""));
assertThat(toString, containsString("\"status\":500"));
}
Aggregations