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