|
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; |
@@ -1784,6 +1785,89 @@ public void testBulkRequestExecutionWithFailures() throws Exception { |
1784 | 1785 | verify(listener, times(1)).onResponse(null); |
1785 | 1786 | } |
1786 | 1787 |
|
| 1788 | + public void testBulkRequestExecutionWithInvalidJsonDocument() throws Exception { |
| 1789 | + // Test that when a document with invalid JSON (e.g., duplicate keys) is in a bulk request with a pipeline, |
| 1790 | + // the invalid document fails gracefully without causing the entire bulk request to fail. |
| 1791 | + BulkRequest bulkRequest = new BulkRequest(); |
| 1792 | + String pipelineId = "_id"; |
| 1793 | + |
| 1794 | + // Valid document that should succeed |
| 1795 | + IndexRequest validRequest = new IndexRequest("_index").id("valid").setPipeline(pipelineId).setFinalPipeline("_none"); |
| 1796 | + validRequest.source(Requests.INDEX_CONTENT_TYPE, "field1", "value1"); |
| 1797 | + validRequest.setListExecutedPipelines(true); |
| 1798 | + bulkRequest.add(validRequest); |
| 1799 | + |
| 1800 | + // Invalid document with missing closing brace |
| 1801 | + String invalidJson = "{\"invalid\":\"json\""; |
| 1802 | + IndexRequest invalidRequest = new IndexRequest("_index").id("invalid").setPipeline(pipelineId).setFinalPipeline("_none"); |
| 1803 | + invalidRequest.source(new BytesArray(invalidJson), XContentType.JSON); |
| 1804 | + bulkRequest.add(invalidRequest); |
| 1805 | + |
| 1806 | + // Another valid document that should succeed |
| 1807 | + IndexRequest validRequest2 = new IndexRequest("_index").id("valid2").setPipeline(pipelineId).setFinalPipeline("_none"); |
| 1808 | + validRequest2.source(Requests.INDEX_CONTENT_TYPE, "field2", "value2"); |
| 1809 | + validRequest2.setListExecutedPipelines(true); |
| 1810 | + bulkRequest.add(validRequest2); |
| 1811 | + |
| 1812 | + // Invalid document with duplicated keys |
| 1813 | + String invalidJson2 = "{\"@timestamp\":\"2024-06-01T00:00:00Z\",\"@timestamp\":\"2024-06-01T00:00:00Z\"}"; |
| 1814 | + IndexRequest invalidRequest2 = new IndexRequest("_index").id("invalid").setPipeline(pipelineId).setFinalPipeline("_none"); |
| 1815 | + invalidRequest2.source(new BytesArray(invalidJson2), XContentType.JSON); |
| 1816 | + bulkRequest.add(invalidRequest2); |
| 1817 | + |
| 1818 | + final Processor processor = mock(Processor.class); |
| 1819 | + when(processor.getType()).thenReturn("mock"); |
| 1820 | + when(processor.getTag()).thenReturn("mockTag"); |
| 1821 | + doAnswer(args -> { |
| 1822 | + BiConsumer<IngestDocument, Exception> handler = args.getArgument(1); |
| 1823 | + handler.accept(RandomDocumentPicks.randomIngestDocument(random()), null); |
| 1824 | + return null; |
| 1825 | + }).when(processor).execute(any(), any()); |
| 1826 | + |
| 1827 | + IngestService ingestService = createWithProcessors(Map.of("mock", (factories, tag, description, config, projectId) -> processor)); |
| 1828 | + PutPipelineRequest putRequest = putJsonPipelineRequest("_id", "{\"processors\": [{\"mock\" : {}}]}"); |
| 1829 | + var projectId = randomProjectIdOrDefault(); |
| 1830 | + ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) |
| 1831 | + .putProjectMetadata(ProjectMetadata.builder(projectId).build()) |
| 1832 | + .build(); |
| 1833 | + ClusterState previousClusterState = clusterState; |
| 1834 | + clusterState = executePut(projectId, putRequest, clusterState); |
| 1835 | + ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState)); |
| 1836 | + |
| 1837 | + TriConsumer<Integer, Exception, IndexDocFailureStoreStatus> requestItemErrorHandler = mock(); |
| 1838 | + final ActionListener<Void> listener = mock(); |
| 1839 | + |
| 1840 | + ingestService.executeBulkRequest( |
| 1841 | + projectId, |
| 1842 | + 4, |
| 1843 | + bulkRequest.requests(), |
| 1844 | + indexReq -> {}, |
| 1845 | + (s) -> false, |
| 1846 | + (slot, targetIndex, e) -> fail("Should not redirect to failure store"), |
| 1847 | + requestItemErrorHandler, |
| 1848 | + listener |
| 1849 | + ); |
| 1850 | + |
| 1851 | + // The invalid documents should fail with a parsing error |
| 1852 | + verify(requestItemErrorHandler).apply( |
| 1853 | + eq(1), // slot 1 is the invalid document |
| 1854 | + argThat(e -> e instanceof XContentParseException), |
| 1855 | + eq(IndexDocFailureStoreStatus.NOT_APPLICABLE_OR_UNKNOWN) |
| 1856 | + ); |
| 1857 | + verify(requestItemErrorHandler).apply( |
| 1858 | + eq(3), // slot 3 is the other invalid document |
| 1859 | + argThat(e -> e instanceof XContentParseException), |
| 1860 | + eq(IndexDocFailureStoreStatus.NOT_APPLICABLE_OR_UNKNOWN) |
| 1861 | + ); |
| 1862 | + |
| 1863 | + // The bulk listener should still be called with success |
| 1864 | + verify(listener).onResponse(null); |
| 1865 | + assertStats(ingestService.stats().totalStats(), 4, 2, 0); |
| 1866 | + // Verify that the valid documents were processed (they should have their pipelines executed) |
| 1867 | + assertThat(validRequest.getExecutedPipelines(), equalTo(List.of(pipelineId))); |
| 1868 | + assertThat(validRequest2.getExecutedPipelines(), equalTo(List.of(pipelineId))); |
| 1869 | + } |
| 1870 | + |
1787 | 1871 | public void testExecuteFailureRedirection() throws Exception { |
1788 | 1872 | final CompoundProcessor processor = mockCompoundProcessor(); |
1789 | 1873 | IngestService ingestService = createWithProcessors( |
|
0 commit comments