pvary commented on code in PR #13853: URL: https://github.com/apache/iceberg/pull/13853#discussion_r2285237815
########## docs/docs/flink-table-maintenance.md: ########## @@ -0,0 +1,363 @@ +--- +title: "Flink Table Maintenance " +--- +<!-- + - Licensed to the Apache Software Foundation (ASF) under one or more + - contributor license agreements. See the NOTICE file distributed with + - this work for additional information regarding copyright ownership. + - The ASF licenses this file to You under the Apache License, Version 2.0 + - (the "License"); you may not use this file except in compliance with + - the License. You may obtain a copy of the License at + - + - http://www.apache.org/licenses/LICENSE-2.0 + - + - Unless required by applicable law or agreed to in writing, software + - distributed under the License is distributed on an "AS IS" BASIS, + - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + - See the License for the specific language governing permissions and + - limitations under the License. + --> + +# Flink Table Maintenance + +## Overview + +In **Apache Iceberg** deployments within **Flink streaming environments**, implementing automated table maintenance operations—including `snapshot expiration`, `small file compaction`, and `orphan file cleanup`—is critical for optimal query performance and storage efficiency. + +Traditionally, these maintenance operations were exclusively accessible through **Iceberg Spark Actions**, necessitating the deployment and management of dedicated Spark clusters. This dependency on **Spark infrastructure** solely for table optimization introduces significant **architectural complexity** and **operational overhead**. + +The `TableMaintenance` API in **Apache Iceberg** empowers **Flink jobs** to execute maintenance tasks **natively**, either embedded within existing streaming pipelines or deployed as standalone Flink jobs. This eliminates dependencies on external systems, thereby **streamlining architecture**, **reducing operational costs**, and **enhancing automation capabilities**. + +## Supported Features (Flink) + +### ExpireSnapshots +Removes old snapshots and their files. Internally uses `cleanExpiredFiles(true)` when committing, so expired metadata/files are cleaned up automatically. + +```java +.add(ExpireSnapshots.builder() + .maxSnapshotAge(Duration.ofDays(7)) + .retainLast(10) + .deleteBatchSize(1000)) +``` + +### RewriteDataFiles +Compacts small files to optimize file sizes. Supports partial progress commits and limiting maximum rewritten bytes per run. + +```java +.add(RewriteDataFiles.builder() + .targetFileSizeBytes(256 * 1024 * 1024) + .minFileSizeBytes(32 * 1024 * 1024) + .partialProgressEnabled(true) + .partialProgressMaxCommits(5)) +``` + +## Lock Management + +The `TriggerLockFactory` is essential for coordinating maintenance tasks across multiple Flink jobs or instances. It prevents concurrent maintenance operations on the same table, which could lead to conflicts or data corruption. + +### Why Locks Are Needed +- **Concurrent Access**: Multiple Flink jobs may attempt maintenance simultaneously +- **Data Consistency**: Ensures only one maintenance operation runs per table at a time +- **Resource Management**: Prevents resource conflicts and scheduling issues + +### Supported Lock Types + +#### JDBC Lock Factory +Uses a database table to manage distributed locks: + +```java +Map<String, String> jdbcProps = new HashMap<>(); +jdbcProps.put("jdbc.user", "flink"); +jdbcProps.put("jdbc.password", "flinkpw"); + +TriggerLockFactory lockFactory = new JdbcLockFactory( + "jdbc:postgresql://localhost:5432/iceberg", // JDBC URL + "catalog.db.table", // Lock ID (unique identifier) + jdbcProps // JDBC connection properties +); +``` + +#### ZooKeeper Lock Factory +Uses Apache ZooKeeper for distributed coordination: + +```java +TriggerLockFactory lockFactory = new ZkLockFactory( + "localhost:2181", // ZooKeeper connection string + "catalog.db.table" // Lock ID (unique identifier) +); +``` + +#### Default Lock Factory +For single-job scenarios or testing: + +```java +TriggerLockFactory lockFactory = TriggerLockFactory.defaultLockFactory(); Review Comment: I don't remember this code. Where is this coming from? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
