|
| 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.indices.pollingingest; |
| 10 | + |
| 11 | +import org.apache.logging.log4j.LogManager; |
| 12 | +import org.apache.logging.log4j.Logger; |
| 13 | +import org.opensearch.action.index.IndexRequest; |
| 14 | +import org.opensearch.common.Nullable; |
| 15 | +import org.opensearch.index.IndexSettings; |
| 16 | +import org.opensearch.ingest.IngestService; |
| 17 | +import org.opensearch.threadpool.ThreadPool; |
| 18 | + |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.Objects; |
| 22 | +import java.util.concurrent.CompletableFuture; |
| 23 | +import java.util.concurrent.ExecutionException; |
| 24 | +import java.util.concurrent.TimeUnit; |
| 25 | +import java.util.concurrent.TimeoutException; |
| 26 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 27 | + |
| 28 | +/** |
| 29 | + * Handles ingest pipeline resolution and execution for pull-based ingestion. |
| 30 | + * |
| 31 | + * <p>Resolves configured pipelines from index settings at initialization and executes them |
| 32 | + * synchronously by bridging IngestService's async callback API with CompletableFuture. |
| 33 | + * Also registers a dynamic settings listener to pick up runtime changes to {@code final_pipeline}. |
| 34 | + * Only {@code final_pipeline} is supported. |
| 35 | + * |
| 36 | + * <p>Unlike push-based indexing, pipeline execution in pull-based ingestion does not require the |
| 37 | + * node to have the {@code ingest} role. Transformations are executed locally on the node hosting the |
| 38 | + * shard, and requests are not forwarded to dedicated ingest nodes. |
| 39 | + */ |
| 40 | +public class IngestPipelineExecutor { |
| 41 | + |
| 42 | + private static final Logger logger = LogManager.getLogger(IngestPipelineExecutor.class); |
| 43 | + |
| 44 | + // TODO: consider making this configurable via index settings if use cases with slow processors arise |
| 45 | + static final long PIPELINE_EXECUTION_TIMEOUT_SECONDS = 30; |
| 46 | + |
| 47 | + // TODO: explore synchronous pipeline execution (IngestService.executeBulkRequestSync) to avoid |
| 48 | + // thread pool dispatch and execute pipelines directly on the processor thread |
| 49 | + |
| 50 | + private final IngestService ingestService; |
| 51 | + private final String index; |
| 52 | + private volatile String resolvedFinalPipeline; |
| 53 | + |
| 54 | + /** |
| 55 | + * Creates an IngestPipelineExecutor for the given index. |
| 56 | + * Resolves the final pipeline from index settings and registers a dynamic settings listener. |
| 57 | + * |
| 58 | + * @param ingestService the ingest service for pipeline execution |
| 59 | + * @param index the index name |
| 60 | + * @param indexSettings the index settings to resolve a pipeline from and register listener on |
| 61 | + */ |
| 62 | + public IngestPipelineExecutor(IngestService ingestService, String index, IndexSettings indexSettings) { |
| 63 | + this.ingestService = Objects.requireNonNull(ingestService); |
| 64 | + this.index = Objects.requireNonNull(index); |
| 65 | + indexSettings.getScopedSettings().addSettingsUpdateConsumer(IndexSettings.FINAL_PIPELINE, this::updateFinalPipeline); |
| 66 | + updateFinalPipeline(IndexSettings.FINAL_PIPELINE.get(indexSettings.getSettings())); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Visible for testing. Creates an executor with a pre-resolved pipeline name, |
| 71 | + * bypassing resolution from index settings. |
| 72 | + * |
| 73 | + * @param ingestService the ingest service |
| 74 | + * @param index the index name |
| 75 | + * @param finalPipeline the resolved final pipeline name, or null if no pipeline is configured |
| 76 | + */ |
| 77 | + IngestPipelineExecutor(IngestService ingestService, String index, @Nullable String finalPipeline) { |
| 78 | + this.ingestService = Objects.requireNonNull(ingestService); |
| 79 | + this.index = Objects.requireNonNull(index); |
| 80 | + this.resolvedFinalPipeline = finalPipeline; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Updates the cached final pipeline name. Called on initial resolution and on dynamic settings change. |
| 85 | + */ |
| 86 | + void updateFinalPipeline(String finalPipeline) { |
| 87 | + if (IngestService.NOOP_PIPELINE_NAME.equals(finalPipeline)) { |
| 88 | + resolvedFinalPipeline = null; |
| 89 | + } else { |
| 90 | + resolvedFinalPipeline = finalPipeline; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Executes final_pipeline on the source map synchronously using CompletableFuture to bridge |
| 96 | + * IngestService's async callback API. |
| 97 | + * |
| 98 | + * @param id document ID |
| 99 | + * @param sourceMap source map to transform |
| 100 | + * @return the transformed source map, or null if the document was dropped by the pipeline |
| 101 | + * @throws Exception if pipeline execution fails |
| 102 | + */ |
| 103 | + public Map<String, Object> executePipelines(String id, Map<String, Object> sourceMap) throws Exception { |
| 104 | + final String finalPipeline = resolvedFinalPipeline; |
| 105 | + if (finalPipeline == null) { |
| 106 | + return sourceMap; |
| 107 | + } |
| 108 | + |
| 109 | + // Build IndexRequest to carry the document through the pipeline |
| 110 | + IndexRequest indexRequest = new IndexRequest(index); |
| 111 | + indexRequest.id(id); |
| 112 | + indexRequest.source(sourceMap); |
| 113 | + |
| 114 | + indexRequest.setPipeline(IngestService.NOOP_PIPELINE_NAME); |
| 115 | + indexRequest.setFinalPipeline(finalPipeline); |
| 116 | + indexRequest.isPipelineResolved(true); |
| 117 | + |
| 118 | + final String originalId = id; |
| 119 | + final String originalRouting = indexRequest.routing(); |
| 120 | + |
| 121 | + CompletableFuture<Void> future = new CompletableFuture<>(); |
| 122 | + AtomicBoolean dropped = new AtomicBoolean(false); |
| 123 | + |
| 124 | + ingestService.executeBulkRequest( |
| 125 | + 1, |
| 126 | + Collections.singletonList(indexRequest), |
| 127 | + (slot, e) -> future.completeExceptionally(e), |
| 128 | + (thread, e) -> { |
| 129 | + if (e != null) { |
| 130 | + future.completeExceptionally(e); |
| 131 | + } else { |
| 132 | + future.complete(null); |
| 133 | + } |
| 134 | + }, |
| 135 | + slot -> dropped.set(true), |
| 136 | + ThreadPool.Names.WRITE |
| 137 | + ); |
| 138 | + |
| 139 | + // Block until pipeline execution completes (with timeout) |
| 140 | + try { |
| 141 | + future.get(PIPELINE_EXECUTION_TIMEOUT_SECONDS, TimeUnit.SECONDS); |
| 142 | + } catch (TimeoutException e) { |
| 143 | + throw new RuntimeException("Ingest pipeline execution timed out after [" + PIPELINE_EXECUTION_TIMEOUT_SECONDS + "] seconds", e); |
| 144 | + } catch (InterruptedException e) { |
| 145 | + Thread.currentThread().interrupt(); |
| 146 | + throw new RuntimeException("Ingest pipeline execution was interrupted", e); |
| 147 | + } catch (ExecutionException e) { |
| 148 | + throw new RuntimeException("Ingest pipeline execution failed", e.getCause()); |
| 149 | + } |
| 150 | + |
| 151 | + if (dropped.get()) { |
| 152 | + return null; |
| 153 | + } |
| 154 | + |
| 155 | + // verify _id and _routing have not been mutated |
| 156 | + if (Objects.equals(originalId, indexRequest.id()) == false) { |
| 157 | + throw new IllegalStateException( |
| 158 | + "Ingest pipeline attempted to change _id from [" |
| 159 | + + originalId |
| 160 | + + "] to [" |
| 161 | + + indexRequest.id() |
| 162 | + + "]. _id mutations are not allowed in pull-based ingestion." |
| 163 | + ); |
| 164 | + } |
| 165 | + if (Objects.equals(originalRouting, indexRequest.routing()) == false) { |
| 166 | + throw new IllegalStateException( |
| 167 | + "Ingest pipeline attempted to change _routing. _routing mutations are not allowed in pull-based ingestion." |
| 168 | + ); |
| 169 | + } |
| 170 | + |
| 171 | + // _index change is already blocked by final_pipeline semantics in IngestService |
| 172 | + |
| 173 | + return indexRequest.sourceAsMap(); |
| 174 | + } |
| 175 | +} |
0 commit comments