Skip to main content

risingwave_connector/sink/iceberg/
mod.rs

1// Copyright 2023 RisingWave Labs
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#[cfg(test)]
16mod test;
17
18mod commit;
19pub mod commit_retry;
20mod config;
21mod create_table;
22mod engine_options;
23mod metadata;
24#[cfg(any(test, madsim))]
25pub mod mock_v3_catalog_registry;
26mod position_delete;
27mod prometheus;
28mod writer;
29
30use std::collections::BTreeMap;
31use std::fmt::Debug;
32use std::num::NonZeroU64;
33
34use anyhow::{Context, anyhow};
35pub use commit::*;
36pub use config::*;
37pub use create_table::*;
38pub use engine_options::*;
39use iceberg::table::Table;
40pub use metadata::*;
41pub use position_delete::*;
42use risingwave_common::bail;
43use tokio::sync::mpsc::UnboundedSender;
44pub use writer::*;
45
46use super::{
47    GLOBAL_SINK_METRICS, SINK_TYPE_APPEND_ONLY, SINK_TYPE_OPTION, SINK_TYPE_UPSERT, Sink,
48    SinkError, SinkWriterParam,
49};
50use crate::connector_common::{IcebergCatalogKind, IcebergSinkCompactionUpdate};
51use crate::enforce_secret::EnforceSecret;
52use crate::sink::coordinate::CoordinatedLogSinker;
53use crate::sink::{Result, SinkCommitCoordinator, SinkParam};
54
55pub const ICEBERG_SINK: &str = "iceberg";
56
57pub struct IcebergSink {
58    pub config: IcebergConfig,
59    param: SinkParam,
60    // In upsert mode, it is never None or empty.
61    upsert_primary_key_column_names: Option<Vec<String>>,
62}
63
64impl EnforceSecret for IcebergSink {
65    fn enforce_secret<'a>(
66        prop_iter: impl Iterator<Item = &'a str>,
67    ) -> crate::error::ConnectorResult<()> {
68        for prop in prop_iter {
69            IcebergConfig::enforce_one(prop)?;
70        }
71        Ok(())
72    }
73}
74
75impl TryFrom<SinkParam> for IcebergSink {
76    type Error = SinkError;
77
78    fn try_from(param: SinkParam) -> std::result::Result<Self, Self::Error> {
79        let config = IcebergConfig::from_btreemap(param.properties.clone())?;
80        IcebergSink::new(config, param)
81    }
82}
83
84impl Debug for IcebergSink {
85    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
86        f.debug_struct("IcebergSink")
87            .field("config", &self.config)
88            .finish()
89    }
90}
91
92impl IcebergSink {
93    pub async fn create_and_validate_table(&self) -> Result<Table> {
94        create_and_validate_table_impl(&self.config, &self.param).await
95    }
96
97    /// Returns `true` if this call created the table, `false` if it already existed.
98    pub async fn create_table_if_not_exists(&self) -> Result<bool> {
99        create_table_if_not_exists_impl(&self.config, &self.param).await
100    }
101
102    pub fn new(config: IcebergConfig, param: SinkParam) -> Result<Self> {
103        if let Some(order_key) = &config.order_key {
104            validate_order_key_columns(
105                order_key,
106                param.columns.iter().map(|column| column.name.as_str()),
107            )
108            .context("invalid order_key")
109            .map_err(SinkError::Config)?;
110        }
111
112        let upsert_primary_key_column_names =
113            if config.r#type == SINK_TYPE_UPSERT && !config.force_append_only {
114                let pk_indices = param
115                    .downstream_pk
116                    .as_ref()
117                    .filter(|pk| !pk.is_empty())
118                    .ok_or_else(|| {
119                        SinkError::Config(anyhow!(
120                            "primary key must be specified for upsert iceberg sink"
121                        ))
122                    })?;
123                Some(
124                    pk_indices
125                        .iter()
126                        .map(|&idx| {
127                            param
128                                .columns
129                                .get(idx)
130                                .map(|column| column.name.clone())
131                                .ok_or_else(|| {
132                                    SinkError::Config(anyhow!(
133                                        "primary key column index {} out of range in sink schema",
134                                        idx
135                                    ))
136                                })
137                        })
138                        .collect::<Result<Vec<_>>>()?,
139                )
140            } else {
141                None
142            };
143        Ok(Self {
144            config,
145            param,
146            upsert_primary_key_column_names,
147        })
148    }
149}
150
151impl Sink for IcebergSink {
152    type LogSinker = CoordinatedLogSinker<IcebergSinkWriter>;
153
154    const SINK_NAME: &'static str = ICEBERG_SINK;
155
156    crate::impl_validate_sink_unknown_fields!();
157
158    async fn validate(&self) -> Result<()> {
159        let catalog_kind = self.config.catalog_kind()?;
160        if matches!(catalog_kind, IcebergCatalogKind::Snowflake) {
161            bail!("Snowflake catalog only supports iceberg sources");
162        }
163
164        if matches!(catalog_kind, IcebergCatalogKind::Glue(_)) {
165            risingwave_common::license::Feature::IcebergSinkWithGlue
166                .check_available()
167                .map_err(|e| anyhow::anyhow!(e))?;
168        }
169
170        // Enforce merge-on-read for append-only tables
171        IcebergConfig::validate_append_only_write_mode(
172            &self.config.r#type,
173            self.config.write_mode,
174        )?;
175
176        // Validate compaction type configuration
177        let compaction_type = self.config.compaction_type();
178
179        // Check COW mode constraints
180        // COW mode only supports 'full' compaction type
181        if self.config.write_mode == IcebergWriteMode::CopyOnWrite
182            && compaction_type != CompactionType::Full
183        {
184            bail!(
185                "'copy-on-write' mode only supports 'full' compaction type, got: '{}'",
186                compaction_type
187            );
188        }
189
190        match compaction_type {
191            CompactionType::SmallFiles => {
192                // 1. check license
193                risingwave_common::license::Feature::IcebergCompaction
194                    .check_available()
195                    .map_err(|e| anyhow::anyhow!(e))?;
196
197                // 2. check write mode
198                if self.config.write_mode != IcebergWriteMode::MergeOnRead {
199                    bail!(
200                        "'small-files' compaction type only supports 'merge-on-read' write mode, got: '{}'",
201                        self.config.write_mode
202                    );
203                }
204
205                // 3. check conflicting parameters
206                if self.config.delete_files_count_threshold.is_some() {
207                    bail!(
208                        "`compaction.delete-files-count-threshold` is not supported for 'small-files' compaction type"
209                    );
210                }
211            }
212            CompactionType::FilesWithDelete => {
213                // 1. check license
214                risingwave_common::license::Feature::IcebergCompaction
215                    .check_available()
216                    .map_err(|e| anyhow::anyhow!(e))?;
217
218                // 2. check write mode
219                if self.config.write_mode != IcebergWriteMode::MergeOnRead {
220                    bail!(
221                        "'files-with-delete' compaction type only supports 'merge-on-read' write mode, got: '{}'",
222                        self.config.write_mode
223                    );
224                }
225
226                // 3. check conflicting parameters
227                if self.config.small_files_threshold_mb.is_some() {
228                    bail!(
229                        "`compaction.small-files-threshold-mb` must not be set for 'files-with-delete' compaction type"
230                    );
231                }
232            }
233            CompactionType::Full => {
234                // Full compaction has no special requirements
235            }
236        }
237
238        let table = self.create_and_validate_table().await?;
239        self.config
240            .validate_manifest_rewrite_format(table.metadata().format_version())?;
241        Ok(())
242    }
243
244    fn support_schema_change() -> bool {
245        true
246    }
247
248    fn validate_alter_config(config: &BTreeMap<String, String>) -> Result<()> {
249        let iceberg_config = IcebergConfig::from_btreemap(config.clone())?;
250
251        // Validate compaction interval
252        if let Some(compaction_interval) = iceberg_config.compaction_interval_sec {
253            if iceberg_config.enable_compaction && compaction_interval == 0 {
254                bail!(
255                    "`compaction-interval-sec` must be greater than 0 when `enable-compaction` is true"
256                );
257            }
258
259            tracing::info!(
260                "Alter config compaction_interval set to {} seconds",
261                compaction_interval
262            );
263        }
264
265        // Validate max snapshots
266        if let Some(max_snapshots) = iceberg_config.max_snapshots_num_before_compaction
267            && max_snapshots < 1
268        {
269            bail!(
270                "`compaction.max_snapshots_num` must be greater than 0, got: {}",
271                max_snapshots
272            );
273        }
274
275        // Validate target file size
276        if let Some(target_file_size_mb) = iceberg_config.target_file_size_mb
277            && target_file_size_mb == 0
278        {
279            bail!("`compaction.target_file_size_mb` must be greater than 0");
280        }
281
282        // Validate parquet max row group rows
283        if let Some(max_row_group_rows) = iceberg_config.write_parquet_max_row_group_rows
284            && max_row_group_rows == 0
285        {
286            bail!("`compaction.write_parquet_max_row_group_rows` must be greater than 0");
287        }
288
289        // Validate parquet max row group bytes
290        if let Some(max_row_group_bytes) = iceberg_config.write_parquet_max_row_group_bytes
291            && max_row_group_bytes == 0
292        {
293            bail!("`compaction.write_parquet_max_row_group_bytes` must be greater than 0");
294        }
295
296        // Validate parquet compression codec
297        if let Some(ref compression) = iceberg_config.write_parquet_compression {
298            let valid_codecs = [
299                "uncompressed",
300                "snappy",
301                "gzip",
302                "lzo",
303                "brotli",
304                "lz4",
305                "zstd",
306            ];
307            if !valid_codecs.contains(&compression.to_lowercase().as_str()) {
308                bail!(
309                    "`compaction.write_parquet_compression` must be one of {:?}, got: {}",
310                    valid_codecs,
311                    compression
312                );
313            }
314        }
315
316        Ok(())
317    }
318
319    async fn new_log_sinker(&self, writer_param: SinkWriterParam) -> Result<Self::LogSinker> {
320        let writer = IcebergSinkWriter::new(
321            self.config.clone(),
322            self.param.clone(),
323            writer_param.clone(),
324            self.upsert_primary_key_column_names.clone(),
325        );
326
327        let commit_checkpoint_interval =
328            NonZeroU64::new(self.config.commit_checkpoint_interval).expect(
329                "commit_checkpoint_interval should be greater than 0, and it should be checked in config validation",
330            );
331        let log_sinker = CoordinatedLogSinker::new(
332            &writer_param,
333            self.param.clone(),
334            writer,
335            commit_checkpoint_interval,
336        )
337        .await?;
338
339        Ok(log_sinker)
340    }
341
342    fn is_coordinated_sink(&self) -> bool {
343        true
344    }
345
346    async fn new_coordinator(
347        &self,
348        iceberg_compact_stat_sender: Option<UnboundedSender<IcebergSinkCompactionUpdate>>,
349    ) -> Result<SinkCommitCoordinator> {
350        let catalog = self.config.create_catalog().await?;
351        let table = self.create_and_validate_table().await?;
352        let coordinator = IcebergSinkCommitter {
353            catalog,
354            table,
355            last_commit_epoch: 0,
356            sink_id: self.param.sink_id,
357            config: self.config.clone(),
358            param: self.param.clone(),
359            commit_retry_num: self.config.commit_retry_num,
360            iceberg_compact_stat_sender,
361        };
362        if self.config.is_exactly_once.unwrap_or_default() {
363            Ok(SinkCommitCoordinator::TwoPhase(Box::new(coordinator)))
364        } else {
365            Ok(SinkCommitCoordinator::SinglePhase(Box::new(coordinator)))
366        }
367    }
368}