|
65 | 65 | import org.elasticsearch.test.MockLog; |
66 | 66 | import org.elasticsearch.threadpool.ThreadPool; |
67 | 67 | import org.elasticsearch.xcontent.XContentBuilder; |
| 68 | +import org.elasticsearch.xcontent.XContentParseException; |
68 | 69 | import org.elasticsearch.xcontent.XContentType; |
69 | 70 | import org.elasticsearch.xcontent.cbor.CborXContent; |
70 | 71 | import org.junit.Before; |
@@ -1572,6 +1573,89 @@ public void testBulkRequestExecutionWithFailures() throws Exception { |
1572 | 1573 | verify(completionHandler, times(1)).accept(Thread.currentThread(), null); |
1573 | 1574 | } |
1574 | 1575 |
|
| 1576 | + public void testBulkRequestExecutionWithInvalidJsonDocument() { |
| 1577 | + // Test that when a document with invalid JSON (e.g., duplicate keys) is in a bulk request with a pipeline, |
| 1578 | + // the invalid document fails gracefully without causing the entire bulk request to fail. |
| 1579 | + BulkRequest bulkRequest = new BulkRequest(); |
| 1580 | + String pipelineId = "_id"; |
| 1581 | + |
| 1582 | + // Valid document that should succeed |
| 1583 | + IndexRequest validRequest = new IndexRequest("_index").id("valid").setPipeline(pipelineId).setFinalPipeline("_none"); |
| 1584 | + validRequest.source(Requests.INDEX_CONTENT_TYPE, "field1", "value1"); |
| 1585 | + validRequest.setListExecutedPipelines(true); |
| 1586 | + bulkRequest.add(validRequest); |
| 1587 | + |
| 1588 | + // Invalid document with missing closing brace |
| 1589 | + String invalidJson = "{\"invalid\":\"json\""; |
| 1590 | + IndexRequest invalidRequest = new IndexRequest("_index").id("invalid").setPipeline(pipelineId).setFinalPipeline("_none"); |
| 1591 | + invalidRequest.source(new BytesArray(invalidJson), XContentType.JSON); |
| 1592 | + bulkRequest.add(invalidRequest); |
| 1593 | + |
| 1594 | + // Another valid document that should succeed |
| 1595 | + IndexRequest validRequest2 = new IndexRequest("_index").id("valid2").setPipeline(pipelineId).setFinalPipeline("_none"); |
| 1596 | + validRequest2.source(Requests.INDEX_CONTENT_TYPE, "field2", "value2"); |
| 1597 | + validRequest2.setListExecutedPipelines(true); |
| 1598 | + bulkRequest.add(validRequest2); |
| 1599 | + |
| 1600 | + // Invalid document with duplicated keys |
| 1601 | + String invalidJson2 = "{\"@timestamp\":\"2024-06-01T00:00:00Z\",\"@timestamp\":\"2024-06-01T00:00:00Z\"}"; |
| 1602 | + IndexRequest invalidRequest2 = new IndexRequest("_index").id("invalid").setPipeline(pipelineId).setFinalPipeline("_none"); |
| 1603 | + invalidRequest2.source(new BytesArray(invalidJson2), XContentType.JSON); |
| 1604 | + bulkRequest.add(invalidRequest2); |
| 1605 | + |
| 1606 | + final Processor processor = mock(Processor.class); |
| 1607 | + when(processor.getType()).thenReturn("mock"); |
| 1608 | + when(processor.getTag()).thenReturn("mockTag"); |
| 1609 | + doAnswer(args -> { |
| 1610 | + BiConsumer<IngestDocument, Exception> handler = args.getArgument(1); |
| 1611 | + handler.accept(RandomDocumentPicks.randomIngestDocument(random()), null); |
| 1612 | + return null; |
| 1613 | + }).when(processor).execute(any(), any()); |
| 1614 | + |
| 1615 | + IngestService ingestService = createWithProcessors(Map.of("mock", (factories, tag, description, config) -> processor)); |
| 1616 | + PutPipelineRequest putRequest = new PutPipelineRequest( |
| 1617 | + "_id1", |
| 1618 | + new BytesArray("{\"processors\": [{\"mock\" : {}}]}"), |
| 1619 | + XContentType.JSON |
| 1620 | + ); |
| 1621 | + ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) |
| 1622 | + .build(); |
| 1623 | + ClusterState previousClusterState = clusterState; |
| 1624 | + clusterState = executePut(putRequest, clusterState); |
| 1625 | + ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState)); |
| 1626 | + |
| 1627 | + BiConsumer<Integer, Exception> requestItemErrorHandler = mock(); |
| 1628 | + final BiConsumer<Thread, Exception> onCompletion = mock(); |
| 1629 | + |
| 1630 | + ingestService.executeBulkRequest( |
| 1631 | + 4, |
| 1632 | + bulkRequest.requests(), |
| 1633 | + indexReq -> {}, |
| 1634 | + (s) -> false, |
| 1635 | + (slot, targetIndex, e) -> fail("Should not redirect to failure store"), |
| 1636 | + requestItemErrorHandler, |
| 1637 | + onCompletion, |
| 1638 | + EsExecutors.DIRECT_EXECUTOR_SERVICE |
| 1639 | + ); |
| 1640 | + |
| 1641 | + // The invalid documents should fail with a parsing error |
| 1642 | + verify(requestItemErrorHandler).accept( |
| 1643 | + eq(1), // slot 1 is the invalid document |
| 1644 | + argThat(e -> e instanceof XContentParseException) |
| 1645 | + ); |
| 1646 | + verify(requestItemErrorHandler).accept( |
| 1647 | + eq(3), // slot 3 is the other invalid document |
| 1648 | + argThat(e -> e instanceof XContentParseException) |
| 1649 | + ); |
| 1650 | + |
| 1651 | + // The bulk listener should still be called with success |
| 1652 | + verify(onCompletion).accept(any(), eq(null)); |
| 1653 | + assertStats(ingestService.stats().totalStats(), 4, 2, 0); |
| 1654 | + // Verify that the valid documents were processed (they should have their pipelines executed) |
| 1655 | + assertThat(validRequest.getExecutedPipelines(), equalTo(List.of(pipelineId))); |
| 1656 | + assertThat(validRequest2.getExecutedPipelines(), equalTo(List.of(pipelineId))); |
| 1657 | + } |
| 1658 | + |
1575 | 1659 | public void testExecuteFailureRedirection() throws Exception { |
1576 | 1660 | final CompoundProcessor processor = mockCompoundProcessor(); |
1577 | 1661 | IngestService ingestService = createWithProcessors( |
|
0 commit comments