|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.ingest; |
| 10 | + |
| 11 | +import org.opensearch.OpenSearchParseException; |
| 12 | +import org.opensearch.test.OpenSearchTestCase; |
| 13 | + |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.Arrays; |
| 16 | +import java.util.Collections; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.function.Consumer; |
| 21 | + |
| 22 | +public class AbstractBatchingProcessorTests extends OpenSearchTestCase { |
| 23 | + |
| 24 | + public void testBatchExecute_emptyInput() { |
| 25 | + DummyProcessor processor = new DummyProcessor(3); |
| 26 | + Consumer<List<IngestDocumentWrapper>> handler = (results) -> assertTrue(results.isEmpty()); |
| 27 | + processor.batchExecute(Collections.emptyList(), handler); |
| 28 | + assertTrue(processor.getSubBatches().isEmpty()); |
| 29 | + } |
| 30 | + |
| 31 | + public void testBatchExecute_singleBatchSize() { |
| 32 | + DummyProcessor processor = new DummyProcessor(3); |
| 33 | + List<IngestDocumentWrapper> wrapperList = Arrays.asList( |
| 34 | + IngestDocumentPreparer.createIngestDocumentWrapper(1), |
| 35 | + IngestDocumentPreparer.createIngestDocumentWrapper(2), |
| 36 | + IngestDocumentPreparer.createIngestDocumentWrapper(3) |
| 37 | + ); |
| 38 | + List<IngestDocumentWrapper> resultList = new ArrayList<>(); |
| 39 | + processor.batchExecute(wrapperList, resultList::addAll); |
| 40 | + assertEquals(wrapperList, resultList); |
| 41 | + assertEquals(1, processor.getSubBatches().size()); |
| 42 | + assertEquals(wrapperList, processor.getSubBatches().get(0)); |
| 43 | + } |
| 44 | + |
| 45 | + public void testBatchExecute_multipleBatches() { |
| 46 | + DummyProcessor processor = new DummyProcessor(2); |
| 47 | + List<IngestDocumentWrapper> wrapperList = Arrays.asList( |
| 48 | + IngestDocumentPreparer.createIngestDocumentWrapper(1), |
| 49 | + IngestDocumentPreparer.createIngestDocumentWrapper(2), |
| 50 | + IngestDocumentPreparer.createIngestDocumentWrapper(3), |
| 51 | + IngestDocumentPreparer.createIngestDocumentWrapper(4), |
| 52 | + IngestDocumentPreparer.createIngestDocumentWrapper(5) |
| 53 | + ); |
| 54 | + List<IngestDocumentWrapper> resultList = new ArrayList<>(); |
| 55 | + processor.batchExecute(wrapperList, resultList::addAll); |
| 56 | + assertEquals(wrapperList, resultList); |
| 57 | + assertEquals(3, processor.getSubBatches().size()); |
| 58 | + assertEquals(wrapperList.subList(0, 2), processor.getSubBatches().get(0)); |
| 59 | + assertEquals(wrapperList.subList(2, 4), processor.getSubBatches().get(1)); |
| 60 | + assertEquals(wrapperList.subList(4, 5), processor.getSubBatches().get(2)); |
| 61 | + } |
| 62 | + |
| 63 | + public void testBatchExecute_randomBatches() { |
| 64 | + int batchSize = randomIntBetween(2, 32); |
| 65 | + int docCount = randomIntBetween(2, 32); |
| 66 | + DummyProcessor processor = new DummyProcessor(batchSize); |
| 67 | + List<IngestDocumentWrapper> wrapperList = new ArrayList<>(); |
| 68 | + for (int i = 0; i < docCount; ++i) { |
| 69 | + wrapperList.add(IngestDocumentPreparer.createIngestDocumentWrapper(i)); |
| 70 | + } |
| 71 | + List<IngestDocumentWrapper> resultList = new ArrayList<>(); |
| 72 | + processor.batchExecute(wrapperList, resultList::addAll); |
| 73 | + assertEquals(wrapperList, resultList); |
| 74 | + assertEquals(docCount / batchSize + (docCount % batchSize == 0 ? 0 : 1), processor.getSubBatches().size()); |
| 75 | + } |
| 76 | + |
| 77 | + public void testBatchExecute_defaultBatchSize() { |
| 78 | + DummyProcessor processor = new DummyProcessor(1); |
| 79 | + List<IngestDocumentWrapper> wrapperList = Arrays.asList( |
| 80 | + IngestDocumentPreparer.createIngestDocumentWrapper(1), |
| 81 | + IngestDocumentPreparer.createIngestDocumentWrapper(2), |
| 82 | + IngestDocumentPreparer.createIngestDocumentWrapper(3) |
| 83 | + ); |
| 84 | + List<IngestDocumentWrapper> resultList = new ArrayList<>(); |
| 85 | + processor.batchExecute(wrapperList, resultList::addAll); |
| 86 | + assertEquals(wrapperList, resultList); |
| 87 | + assertEquals(3, processor.getSubBatches().size()); |
| 88 | + assertEquals(wrapperList.subList(0, 1), processor.getSubBatches().get(0)); |
| 89 | + assertEquals(wrapperList.subList(1, 2), processor.getSubBatches().get(1)); |
| 90 | + assertEquals(wrapperList.subList(2, 3), processor.getSubBatches().get(2)); |
| 91 | + } |
| 92 | + |
| 93 | + public void testFactory_invalidBatchSize() { |
| 94 | + Map<String, Object> config = new HashMap<>(); |
| 95 | + config.put("batch_size", 0); |
| 96 | + DummyProcessor.DummyProcessorFactory factory = new DummyProcessor.DummyProcessorFactory("DummyProcessor"); |
| 97 | + OpenSearchParseException exception = assertThrows(OpenSearchParseException.class, () -> factory.create(config)); |
| 98 | + assertEquals("[batch_size] batch size must be a positive integer", exception.getMessage()); |
| 99 | + } |
| 100 | + |
| 101 | + public void testFactory_defaultBatchSize() throws Exception { |
| 102 | + Map<String, Object> config = new HashMap<>(); |
| 103 | + DummyProcessor.DummyProcessorFactory factory = new DummyProcessor.DummyProcessorFactory("DummyProcessor"); |
| 104 | + DummyProcessor processor = (DummyProcessor) factory.create(config); |
| 105 | + assertEquals(1, processor.batchSize); |
| 106 | + } |
| 107 | + |
| 108 | + public void testFactory_callNewProcessor() throws Exception { |
| 109 | + Map<String, Object> config = new HashMap<>(); |
| 110 | + config.put("batch_size", 3); |
| 111 | + DummyProcessor.DummyProcessorFactory factory = new DummyProcessor.DummyProcessorFactory("DummyProcessor"); |
| 112 | + DummyProcessor processor = (DummyProcessor) factory.create(config); |
| 113 | + assertEquals(3, processor.batchSize); |
| 114 | + } |
| 115 | + |
| 116 | + static class DummyProcessor extends AbstractBatchingProcessor { |
| 117 | + private List<List<IngestDocumentWrapper>> subBatches = new ArrayList<>(); |
| 118 | + |
| 119 | + public List<List<IngestDocumentWrapper>> getSubBatches() { |
| 120 | + return subBatches; |
| 121 | + } |
| 122 | + |
| 123 | + protected DummyProcessor(int batchSize) { |
| 124 | + super("tag", "description", batchSize); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public void subBatchExecute(List<IngestDocumentWrapper> ingestDocumentWrappers, Consumer<List<IngestDocumentWrapper>> handler) { |
| 129 | + subBatches.add(ingestDocumentWrappers); |
| 130 | + handler.accept(ingestDocumentWrappers); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public IngestDocument execute(IngestDocument ingestDocument) throws Exception { |
| 135 | + return ingestDocument; |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public String getType() { |
| 140 | + return null; |
| 141 | + } |
| 142 | + |
| 143 | + public static class DummyProcessorFactory extends Factory { |
| 144 | + |
| 145 | + protected DummyProcessorFactory(String processorType) { |
| 146 | + super(processorType); |
| 147 | + } |
| 148 | + |
| 149 | + public AbstractBatchingProcessor create(Map<String, Object> config) throws Exception { |
| 150 | + final Map<String, org.opensearch.ingest.Processor.Factory> processorFactories = new HashMap<>(); |
| 151 | + return super.create(processorFactories, "tag", "description", config); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + protected AbstractBatchingProcessor newProcessor(String tag, String description, int batchSize, Map<String, Object> config) { |
| 156 | + return new DummyProcessor(batchSize); |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments