|
| 1 | +// Copyright 2021-Present Datadog, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +//! Parquet merge executor actor. |
| 16 | +//! |
| 17 | +//! Calls the Phase 1 merge engine (`merge_sorted_parquet_files`) via |
| 18 | +//! `run_cpu_intensive()`, builds output split metadata using |
| 19 | +//! `merge_parquet_split_metadata()`, and sends the result to the uploader. |
| 20 | +
|
| 21 | +use anyhow::Context; |
| 22 | +use async_trait::async_trait; |
| 23 | +use quickwit_actors::{Actor, ActorContext, ActorExitStatus, Handler, Mailbox, QueueCapacity}; |
| 24 | +use quickwit_common::thread_pool::run_cpu_intensive; |
| 25 | +use quickwit_parquet_engine::merge::metadata_aggregation::merge_parquet_split_metadata; |
| 26 | +use quickwit_parquet_engine::merge::{MergeConfig, MergeOutputFile, merge_sorted_parquet_files}; |
| 27 | +use quickwit_parquet_engine::storage::ParquetWriterConfig; |
| 28 | +use quickwit_proto::types::IndexUid; |
| 29 | +use tracing::{info, instrument, warn}; |
| 30 | + |
| 31 | +use super::ParquetUploader; |
| 32 | +use super::parquet_indexer::ParquetSplitBatch; |
| 33 | +use super::parquet_merge_messages::ParquetMergeScratch; |
| 34 | +use crate::models::PublishLock; |
| 35 | + |
| 36 | +/// Executes Parquet merge operations using the Phase 1 k-way merge engine. |
| 37 | +/// |
| 38 | +/// Receives `ParquetMergeScratch` from the downloader, runs the merge as a |
| 39 | +/// CPU-intensive task, builds output metadata, and sends the result to the |
| 40 | +/// uploader for staging and upload. |
| 41 | +/// |
| 42 | +/// No separate Packager step is needed — the merge engine produces |
| 43 | +/// ready-to-upload Parquet files with complete metadata. |
| 44 | +pub struct ParquetMergeExecutor { |
| 45 | + uploader_mailbox: Mailbox<ParquetUploader>, |
| 46 | +} |
| 47 | + |
| 48 | +impl ParquetMergeExecutor { |
| 49 | + pub fn new(uploader_mailbox: Mailbox<ParquetUploader>) -> Self { |
| 50 | + Self { uploader_mailbox } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +#[async_trait] |
| 55 | +impl Actor for ParquetMergeExecutor { |
| 56 | + type ObservableState = (); |
| 57 | + |
| 58 | + fn observable_state(&self) {} |
| 59 | + |
| 60 | + fn name(&self) -> String { |
| 61 | + "ParquetMergeExecutor".to_string() |
| 62 | + } |
| 63 | + |
| 64 | + fn queue_capacity(&self) -> QueueCapacity { |
| 65 | + QueueCapacity::Bounded(1) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +#[async_trait] |
| 70 | +impl Handler<ParquetMergeScratch> for ParquetMergeExecutor { |
| 71 | + type Reply = (); |
| 72 | + |
| 73 | + #[instrument(name = "parquet_merge_executor", skip_all, fields( |
| 74 | + merge_split_id = %scratch.merge_operation.merge_split_id, |
| 75 | + num_inputs = scratch.merge_operation.splits.len(), |
| 76 | + ))] |
| 77 | + async fn handle( |
| 78 | + &mut self, |
| 79 | + scratch: ParquetMergeScratch, |
| 80 | + ctx: &ActorContext<Self>, |
| 81 | + ) -> Result<(), ActorExitStatus> { |
| 82 | + let merge_split_id = scratch.merge_operation.merge_split_id.to_string(); |
| 83 | + let num_inputs = scratch.merge_operation.splits.len(); |
| 84 | + |
| 85 | + info!( |
| 86 | + merge_split_id = %merge_split_id, |
| 87 | + num_inputs, |
| 88 | + total_bytes = scratch.merge_operation.total_size_bytes(), |
| 89 | + "executing parquet merge" |
| 90 | + ); |
| 91 | + |
| 92 | + // Separate output subdirectory so the merge engine's temp files |
| 93 | + // don't collide with the downloaded inputs in scratch_directory. |
| 94 | + let output_dir = scratch.scratch_directory.path().join("merged_output"); |
| 95 | + std::fs::create_dir_all(&output_dir) |
| 96 | + .context("failed to create merge output directory") |
| 97 | + .map_err(|e| ActorExitStatus::from(anyhow::anyhow!(e)))?; |
| 98 | + |
| 99 | + // Run the CPU-intensive merge on the dedicated thread pool. |
| 100 | + let input_paths = scratch.downloaded_parquet_files.clone(); |
| 101 | + let output_dir_clone = output_dir.clone(); |
| 102 | + let merge_result = run_cpu_intensive(move || { |
| 103 | + let config = MergeConfig { |
| 104 | + num_outputs: 1, |
| 105 | + writer_config: ParquetWriterConfig::default(), |
| 106 | + }; |
| 107 | + merge_sorted_parquet_files(&input_paths, &output_dir_clone, &config) |
| 108 | + }) |
| 109 | + .await; |
| 110 | + |
| 111 | + // We return Ok(()) on merge failure rather than Err to keep the actor |
| 112 | + // alive — same strategy as Tantivy's MergeExecutor. This prevents a |
| 113 | + // single "split of death" from crash-looping the entire pipeline. |
| 114 | + // The trade-off: failed splits aren't retried until pipeline respawn. |
| 115 | + let outputs: Vec<MergeOutputFile> = match merge_result { |
| 116 | + Ok(Ok(outputs)) => outputs, |
| 117 | + Ok(Err(merge_err)) => { |
| 118 | + warn!( |
| 119 | + error = %merge_err, |
| 120 | + merge_split_id = %merge_split_id, |
| 121 | + "parquet merge failed" |
| 122 | + ); |
| 123 | + // Input splits were drained from the planner by operations(). |
| 124 | + // They remain published but won't be re-planned until respawn. |
| 125 | + return Ok(()); |
| 126 | + } |
| 127 | + Err(panicked) => { |
| 128 | + warn!( |
| 129 | + error = %panicked, |
| 130 | + merge_split_id = %merge_split_id, |
| 131 | + "parquet merge panicked" |
| 132 | + ); |
| 133 | + return Ok(()); |
| 134 | + } |
| 135 | + }; |
| 136 | + |
| 137 | + // Empty output is valid (all input rows were empty). Nothing to publish. |
| 138 | + if outputs.is_empty() { |
| 139 | + info!( |
| 140 | + merge_split_id = %merge_split_id, |
| 141 | + "merge produced no output (all inputs empty)" |
| 142 | + ); |
| 143 | + return Ok(()); |
| 144 | + } |
| 145 | + |
| 146 | + // Build metadata for each output file and rename to match split IDs. |
| 147 | + let input_splits = &scratch.merge_operation.splits; |
| 148 | + let index_uid: IndexUid = input_splits[0] |
| 149 | + .index_uid |
| 150 | + .parse() |
| 151 | + .context("invalid index_uid in merge input") |
| 152 | + .map_err(|e| ActorExitStatus::from(anyhow::anyhow!(e)))?; |
| 153 | + |
| 154 | + let replaced_split_ids: Vec<String> = input_splits |
| 155 | + .iter() |
| 156 | + .map(|s| s.split_id.as_str().to_string()) |
| 157 | + .collect(); |
| 158 | + |
| 159 | + let mut merged_splits = Vec::with_capacity(outputs.len()); |
| 160 | + for output in &outputs { |
| 161 | + let metadata = merge_parquet_split_metadata(input_splits, output) |
| 162 | + .context("failed to build merge output metadata") |
| 163 | + .map_err(|e| ActorExitStatus::from(anyhow::anyhow!(e)))?; |
| 164 | + |
| 165 | + // The merge engine writes to a temp filename (merge_output_*.parquet). |
| 166 | + // Rename to {split_id}.parquet so the uploader can find it at the |
| 167 | + // path derived from ParquetSplitMetadata::parquet_filename(). |
| 168 | + let expected_path = output_dir.join(&metadata.parquet_file); |
| 169 | + if output.path != expected_path { |
| 170 | + std::fs::rename(&output.path, &expected_path) |
| 171 | + .with_context(|| { |
| 172 | + format!( |
| 173 | + "failed to rename merge output {} to {}", |
| 174 | + output.path.display(), |
| 175 | + expected_path.display() |
| 176 | + ) |
| 177 | + }) |
| 178 | + .map_err(|e| ActorExitStatus::from(anyhow::anyhow!(e)))?; |
| 179 | + } |
| 180 | + |
| 181 | + info!( |
| 182 | + split_id = %metadata.split_id, |
| 183 | + num_rows = metadata.num_rows, |
| 184 | + size_bytes = metadata.size_bytes, |
| 185 | + "merge produced output split" |
| 186 | + ); |
| 187 | + |
| 188 | + merged_splits.push(metadata); |
| 189 | + } |
| 190 | + |
| 191 | + // Send to uploader. Merges have no checkpoint delta, no publish lock, |
| 192 | + // and no publish token — they're just reorganizing existing data. |
| 193 | + // The scratch directory is passed along to keep it alive until the |
| 194 | + // uploader finishes reading the merged files. |
| 195 | + let batch = ParquetSplitBatch { |
| 196 | + index_uid, |
| 197 | + splits: merged_splits, |
| 198 | + output_dir, |
| 199 | + checkpoint_delta_opt: None, |
| 200 | + publish_lock: PublishLock::default(), |
| 201 | + publish_token_opt: None, |
| 202 | + replaced_split_ids, |
| 203 | + _scratch_directory_opt: Some(scratch.scratch_directory), |
| 204 | + }; |
| 205 | + |
| 206 | + ctx.send_message(&self.uploader_mailbox, batch).await?; |
| 207 | + |
| 208 | + // The merge permit is dropped here when `scratch` goes out of scope, |
| 209 | + // releasing the semaphore slot for the next merge. |
| 210 | + info!( |
| 211 | + merge_split_id = %merge_split_id, |
| 212 | + "parquet merge complete, sent to uploader" |
| 213 | + ); |
| 214 | + Ok(()) |
| 215 | + } |
| 216 | +} |
0 commit comments