|
69 | 69 | import org.elasticsearch.test.MockLog; |
70 | 70 | import org.elasticsearch.threadpool.ThreadPool; |
71 | 71 | import org.elasticsearch.xcontent.XContentBuilder; |
| 72 | +import org.elasticsearch.xcontent.XContentParseException; |
72 | 73 | import org.elasticsearch.xcontent.XContentType; |
73 | 74 | import org.elasticsearch.xcontent.cbor.CborXContent; |
74 | 75 | import org.junit.Before; |
@@ -1632,6 +1633,92 @@ public void testBulkRequestExecutionWithFailures() throws Exception { |
1632 | 1633 | verify(completionHandler, times(1)).accept(Thread.currentThread(), null); |
1633 | 1634 | } |
1634 | 1635 |
|
| 1636 | + public void testBulkRequestExecutionWithInvalidJsonDocument() throws Exception { |
| 1637 | + // Test that when a document with invalid JSON (e.g., duplicate keys) is in a bulk request with a pipeline, |
| 1638 | + // the invalid document fails gracefully without causing the entire bulk request to fail. |
| 1639 | + BulkRequest bulkRequest = new BulkRequest(); |
| 1640 | + String pipelineId = "_id"; |
| 1641 | + |
| 1642 | + // Valid document that should succeed |
| 1643 | + IndexRequest validRequest = new IndexRequest("_index").id("valid").setPipeline(pipelineId).setFinalPipeline("_none"); |
| 1644 | + validRequest.source(Requests.INDEX_CONTENT_TYPE, "field1", "value1"); |
| 1645 | + validRequest.setListExecutedPipelines(true); |
| 1646 | + bulkRequest.add(validRequest); |
| 1647 | + |
| 1648 | + // Invalid document with missing closing brace |
| 1649 | + String invalidJson = "{\"invalid\":\"json\""; |
| 1650 | + IndexRequest invalidRequest = new IndexRequest("_index").id("invalid").setPipeline(pipelineId).setFinalPipeline("_none"); |
| 1651 | + invalidRequest.source(new BytesArray(invalidJson), XContentType.JSON); |
| 1652 | + bulkRequest.add(invalidRequest); |
| 1653 | + |
| 1654 | + // Another valid document that should succeed |
| 1655 | + IndexRequest validRequest2 = new IndexRequest("_index").id("valid2").setPipeline(pipelineId).setFinalPipeline("_none"); |
| 1656 | + validRequest2.source(Requests.INDEX_CONTENT_TYPE, "field2", "value2"); |
| 1657 | + validRequest2.setListExecutedPipelines(true); |
| 1658 | + bulkRequest.add(validRequest2); |
| 1659 | + |
| 1660 | + // Invalid document with duplicated keys |
| 1661 | + String invalidJson2 = "{\"@timestamp\":\"2024-06-01T00:00:00Z\",\"@timestamp\":\"2024-06-01T00:00:00Z\"}"; |
| 1662 | + IndexRequest invalidRequest2 = new IndexRequest("_index").id("invalid").setPipeline(pipelineId).setFinalPipeline("_none"); |
| 1663 | + invalidRequest2.source(new BytesArray(invalidJson2), XContentType.JSON); |
| 1664 | + bulkRequest.add(invalidRequest2); |
| 1665 | + |
| 1666 | + final Processor processor = mock(Processor.class); |
| 1667 | + when(processor.getType()).thenReturn("mock"); |
| 1668 | + when(processor.getTag()).thenReturn("mockTag"); |
| 1669 | + doAnswer(args -> { |
| 1670 | + BiConsumer<IngestDocument, Exception> handler = args.getArgument(1); |
| 1671 | + handler.accept(RandomDocumentPicks.randomIngestDocument(random()), null); |
| 1672 | + return null; |
| 1673 | + }).when(processor).execute(any(), any()); |
| 1674 | + |
| 1675 | + IngestService ingestService = createWithProcessors(Map.of("mock", (factories, tag, description, config) -> processor)); |
| 1676 | + PutPipelineRequest putRequest = new PutPipelineRequest( |
| 1677 | + "_id1", |
| 1678 | + new BytesArray("{\"processors\": [{\"mock\" : {}}]}"), |
| 1679 | + XContentType.JSON |
| 1680 | + ); |
| 1681 | + |
| 1682 | + ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) |
| 1683 | + .build(); |
| 1684 | + ClusterState previousClusterState = clusterState; |
| 1685 | + clusterState = executePut(putRequest, clusterState); |
| 1686 | + ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState)); |
| 1687 | + |
| 1688 | + TriConsumer<Integer, Exception, IndexDocFailureStoreStatus> requestItemErrorHandler = mock(); |
| 1689 | + final BiConsumer<Thread, Exception> onCompletion = mock(); |
| 1690 | + |
| 1691 | + ingestService.executeBulkRequest( |
| 1692 | + 4, |
| 1693 | + bulkRequest.requests(), |
| 1694 | + indexReq -> {}, |
| 1695 | + (s) -> false, |
| 1696 | + (slot, targetIndex, e) -> fail("Should not redirect to failure store"), |
| 1697 | + requestItemErrorHandler, |
| 1698 | + onCompletion, |
| 1699 | + EsExecutors.DIRECT_EXECUTOR_SERVICE |
| 1700 | + ); |
| 1701 | + |
| 1702 | + // The invalid documents should fail with a parsing error |
| 1703 | + verify(requestItemErrorHandler).apply( |
| 1704 | + eq(1), // slot 1 is the invalid document |
| 1705 | + argThat(e -> e instanceof XContentParseException), |
| 1706 | + eq(IndexDocFailureStoreStatus.NOT_APPLICABLE_OR_UNKNOWN) |
| 1707 | + ); |
| 1708 | + verify(requestItemErrorHandler).apply( |
| 1709 | + eq(3), // slot 3 is the other invalid document |
| 1710 | + argThat(e -> e instanceof XContentParseException), |
| 1711 | + eq(IndexDocFailureStoreStatus.NOT_APPLICABLE_OR_UNKNOWN) |
| 1712 | + ); |
| 1713 | + |
| 1714 | + // The bulk listener should still be called with success |
| 1715 | + verify(onCompletion).accept(any(), eq(null)); |
| 1716 | + assertStats(ingestService.stats().totalStats(), 4, 2, 0); |
| 1717 | + // Verify that the valid documents were processed (they should have their pipelines executed) |
| 1718 | + assertThat(validRequest.getExecutedPipelines(), equalTo(List.of(pipelineId))); |
| 1719 | + assertThat(validRequest2.getExecutedPipelines(), equalTo(List.of(pipelineId))); |
| 1720 | + } |
| 1721 | + |
1635 | 1722 | public void testExecuteFailureRedirection() throws Exception { |
1636 | 1723 | final CompoundProcessor processor = mockCompoundProcessor(); |
1637 | 1724 | IngestService ingestService = createWithProcessors( |
|
0 commit comments