|
73 | 73 | import org.elasticsearch.test.MockLog; |
74 | 74 | import org.elasticsearch.threadpool.ThreadPool; |
75 | 75 | import org.elasticsearch.xcontent.XContentBuilder; |
| 76 | +import org.elasticsearch.xcontent.XContentParseException; |
76 | 77 | import org.elasticsearch.xcontent.XContentType; |
77 | 78 | import org.elasticsearch.xcontent.cbor.CborXContent; |
78 | 79 | import org.junit.Before; |
@@ -1723,6 +1724,89 @@ public void testBulkRequestExecutionWithFailures() throws Exception { |
1723 | 1724 | verify(completionHandler, times(1)).accept(Thread.currentThread(), null); |
1724 | 1725 | } |
1725 | 1726 |
|
| 1727 | + public void testBulkRequestExecutionWithInvalidJsonDocument() throws Exception { |
| 1728 | + // Test that when a document with invalid JSON (e.g., duplicate keys) is in a bulk request with a pipeline, |
| 1729 | + // the invalid document fails gracefully without causing the entire bulk request to fail. |
| 1730 | + BulkRequest bulkRequest = new BulkRequest(); |
| 1731 | + String pipelineId = "_id"; |
| 1732 | + |
| 1733 | + // Valid document that should succeed |
| 1734 | + IndexRequest validRequest = new IndexRequest("_index").id("valid").setPipeline(pipelineId).setFinalPipeline("_none"); |
| 1735 | + validRequest.source(Requests.INDEX_CONTENT_TYPE, "field1", "value1"); |
| 1736 | + validRequest.setListExecutedPipelines(true); |
| 1737 | + bulkRequest.add(validRequest); |
| 1738 | + |
| 1739 | + // Invalid document with missing closing brace |
| 1740 | + String invalidJson = "{\"invalid\":\"json\""; |
| 1741 | + IndexRequest invalidRequest = new IndexRequest("_index").id("invalid").setPipeline(pipelineId).setFinalPipeline("_none"); |
| 1742 | + invalidRequest.source(new BytesArray(invalidJson), XContentType.JSON); |
| 1743 | + bulkRequest.add(invalidRequest); |
| 1744 | + |
| 1745 | + // Another valid document that should succeed |
| 1746 | + IndexRequest validRequest2 = new IndexRequest("_index").id("valid2").setPipeline(pipelineId).setFinalPipeline("_none"); |
| 1747 | + validRequest2.source(Requests.INDEX_CONTENT_TYPE, "field2", "value2"); |
| 1748 | + validRequest2.setListExecutedPipelines(true); |
| 1749 | + bulkRequest.add(validRequest2); |
| 1750 | + |
| 1751 | + // Invalid document with duplicated keys |
| 1752 | + String invalidJson2 = "{\"@timestamp\":\"2024-06-01T00:00:00Z\",\"@timestamp\":\"2024-06-01T00:00:00Z\"}"; |
| 1753 | + IndexRequest invalidRequest2 = new IndexRequest("_index").id("invalid").setPipeline(pipelineId).setFinalPipeline("_none"); |
| 1754 | + invalidRequest2.source(new BytesArray(invalidJson2), XContentType.JSON); |
| 1755 | + bulkRequest.add(invalidRequest2); |
| 1756 | + |
| 1757 | + final Processor processor = mock(Processor.class); |
| 1758 | + when(processor.getType()).thenReturn("mock"); |
| 1759 | + when(processor.getTag()).thenReturn("mockTag"); |
| 1760 | + doAnswer(args -> { |
| 1761 | + BiConsumer<IngestDocument, Exception> handler = args.getArgument(1); |
| 1762 | + handler.accept(RandomDocumentPicks.randomIngestDocument(random()), null); |
| 1763 | + return null; |
| 1764 | + }).when(processor).execute(any(), any()); |
| 1765 | + |
| 1766 | + IngestService ingestService = createWithProcessors(Map.of("mock", (factories, tag, description, config, projectId) -> processor)); |
| 1767 | + PutPipelineRequest putRequest = putJsonPipelineRequest("_id", "{\"processors\": [{\"mock\" : {}}]}"); |
| 1768 | + var projectId = randomProjectIdOrDefault(); |
| 1769 | + ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) |
| 1770 | + .putProjectMetadata(ProjectMetadata.builder(projectId).build()) |
| 1771 | + .build(); |
| 1772 | + ClusterState previousClusterState = clusterState; |
| 1773 | + clusterState = executePut(projectId, putRequest, clusterState); |
| 1774 | + ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState)); |
| 1775 | + |
| 1776 | + TriConsumer<Integer, Exception, IndexDocFailureStoreStatus> requestItemErrorHandler = mock(); |
| 1777 | + final BiConsumer<Thread, Exception> onCompletion = mock(); |
| 1778 | + |
| 1779 | + ingestService.executeBulkRequest( |
| 1780 | + projectId, |
| 1781 | + 4, |
| 1782 | + bulkRequest.requests(), |
| 1783 | + indexReq -> {}, |
| 1784 | + (s) -> false, |
| 1785 | + (slot, targetIndex, e) -> fail("Should not redirect to failure store"), |
| 1786 | + requestItemErrorHandler, |
| 1787 | + onCompletion, |
| 1788 | + EsExecutors.DIRECT_EXECUTOR_SERVICE |
| 1789 | + ); |
| 1790 | + // The invalid documents should fail with a parsing error |
| 1791 | + verify(requestItemErrorHandler).apply( |
| 1792 | + eq(1), // slot 1 is the invalid document |
| 1793 | + argThat(e -> e instanceof XContentParseException), |
| 1794 | + eq(IndexDocFailureStoreStatus.NOT_APPLICABLE_OR_UNKNOWN) |
| 1795 | + ); |
| 1796 | + verify(requestItemErrorHandler).apply( |
| 1797 | + eq(3), // slot 3 is the other invalid document |
| 1798 | + argThat(e -> e instanceof XContentParseException), |
| 1799 | + eq(IndexDocFailureStoreStatus.NOT_APPLICABLE_OR_UNKNOWN) |
| 1800 | + ); |
| 1801 | + |
| 1802 | + // The bulk listener should still be called with success |
| 1803 | + verify(onCompletion).accept(any(), eq(null)); |
| 1804 | + assertStats(ingestService.stats().totalStats(), 4, 2, 0); |
| 1805 | + // Verify that the valid documents were processed (they should have their pipelines executed) |
| 1806 | + assertThat(validRequest.getExecutedPipelines(), equalTo(List.of(pipelineId))); |
| 1807 | + assertThat(validRequest2.getExecutedPipelines(), equalTo(List.of(pipelineId))); |
| 1808 | + } |
| 1809 | + |
1726 | 1810 | public void testExecuteFailureRedirection() throws Exception { |
1727 | 1811 | final CompoundProcessor processor = mockCompoundProcessor(); |
1728 | 1812 | IngestService ingestService = createWithProcessors( |
|
0 commit comments