Search in sources :

Example 1 with Failure

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);
    }
}
Also used : ArrayList(java.util.ArrayList) BulkItemResponse(org.opensearch.action.bulk.BulkItemResponse) SearchFailure(org.opensearch.index.reindex.ScrollableHitSource.SearchFailure) Failure(org.opensearch.action.bulk.BulkItemResponse.Failure) HashSet(java.util.HashSet)

Example 2 with Failure

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());
    }
}
Also used : Failure(org.opensearch.action.bulk.BulkItemResponse.Failure)

Example 3 with Failure

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());
    }
}
Also used : Failure(org.opensearch.action.bulk.BulkItemResponse.Failure)

Example 4 with Failure

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);
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) OpenSearchException(org.opensearch.OpenSearchException) XContentType(org.opensearch.common.xcontent.XContentType) OpenSearchException(org.opensearch.OpenSearchException) DocWriteRequest(org.opensearch.action.DocWriteRequest) Failure(org.opensearch.action.bulk.BulkItemResponse.Failure) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 5 with Failure

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"));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) Failure(org.opensearch.action.bulk.BulkItemResponse.Failure)

Aggregations

Failure (org.opensearch.action.bulk.BulkItemResponse.Failure)11 SearchFailure (org.opensearch.index.reindex.ScrollableHitSource.SearchFailure)5 BulkItemResponse (org.opensearch.action.bulk.BulkItemResponse)3 Matchers.containsString (org.hamcrest.Matchers.containsString)2 OpenSearchException (org.opensearch.OpenSearchException)2 DocWriteRequest (org.opensearch.action.DocWriteRequest)2 BulkResponse (org.opensearch.action.bulk.BulkResponse)2 ShardSearchFailure (org.opensearch.action.search.ShardSearchFailure)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Assert (org.junit.Assert)1 OpenSearchParseException (org.opensearch.OpenSearchParseException)1 IndexResponse (org.opensearch.action.index.IndexResponse)1 BytesReference (org.opensearch.common.bytes.BytesReference)1 XContentParser (org.opensearch.common.xcontent.XContentParser)1 Token (org.opensearch.common.xcontent.XContentParser.Token)1 XContentParserUtils.ensureExpectedToken (org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken)1 XContentType (org.opensearch.common.xcontent.XContentType)1 Index (org.opensearch.index.Index)1