XuQianJin-Stars commented on code in PR #3518: URL: https://github.com/apache/fluss/pull/3518#discussion_r3465663385
########## fluss-lake/fluss-lake-hudi/src/main/java/org/apache/fluss/lake/hudi/tiering/HudiCompactionService.java: ########## @@ -0,0 +1,465 @@ +/* + * 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. + */ + +package org.apache.fluss.lake.hudi.tiering; + +import org.apache.fluss.metadata.TableBucket; + +import org.apache.flink.configuration.Configuration; +import org.apache.hudi.avro.model.HoodieCompactionOperation; +import org.apache.hudi.avro.model.HoodieCompactionPlan; +import org.apache.hudi.client.HoodieFlinkWriteClient; +import org.apache.hudi.client.WriteStatus; +import org.apache.hudi.common.data.HoodieListData; +import org.apache.hudi.common.engine.HoodieReaderContext; +import org.apache.hudi.common.model.CompactionOperation; +import org.apache.hudi.common.model.HoodieCommitMetadata; +import org.apache.hudi.common.model.HoodieWriteStat; +import org.apache.hudi.common.table.HoodieTableMetaClient; +import org.apache.hudi.common.table.timeline.HoodieInstant; +import org.apache.hudi.common.table.timeline.HoodieTimeline; +import org.apache.hudi.common.table.timeline.InstantGenerator; +import org.apache.hudi.common.util.CompactionUtils; +import org.apache.hudi.common.util.Option; +import org.apache.hudi.common.util.collection.Pair; +import org.apache.hudi.config.HoodieWriteConfig; +import org.apache.hudi.configuration.FlinkOptions; +import org.apache.hudi.exception.HoodieException; +import org.apache.hudi.index.bucket.BucketIdentifier; +import org.apache.hudi.sink.compact.FlinkCompactionConfig; +import org.apache.hudi.sink.compact.strategy.CompactionPlanStrategies; +import org.apache.hudi.storage.StorageConfiguration; +import org.apache.hudi.table.HoodieFlinkTable; +import org.apache.hudi.table.action.compact.CompactHelpers; +import org.apache.hudi.table.action.compact.HoodieFlinkMergeOnReadTableCompactor; +import org.apache.hudi.table.format.FlinkRowDataReaderContext; +import org.apache.hudi.table.format.InternalSchemaManager; +import org.apache.hudi.util.CompactionUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nullable; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.function.Supplier; +import java.util.stream.Collectors; + +import static org.apache.fluss.utils.Preconditions.checkNotNull; + +/** Coordinates Hudi compaction scheduling, execution, and commit for tiering writers. */ +public class HudiCompactionService { + + private static final Logger LOG = LoggerFactory.getLogger(HudiCompactionService.class); + + private final HudiWriteTableInfo hudiTableInfo; + private final HoodieFlinkWriteClient<?> writeClient; + private final HoodieTableMetaClient metaClient; + private final HoodieFlinkTable<?> table; + private final Configuration conf; + @Nullable private final TableBucket tableBucket; + @Nullable private final String partition; + + private InternalSchemaManager internalSchemaManager; + + public HudiCompactionService( + HudiWriteTableInfo hudiTableInfo, + @Nullable TableBucket tableBucket, + @Nullable String partition) { + this.hudiTableInfo = checkNotNull(hudiTableInfo, "Hudi write table info must not be null."); + this.writeClient = hudiTableInfo.getWriteClient(); + this.metaClient = hudiTableInfo.getMetaClient(); + this.table = writeClient.getHoodieTable(); + this.conf = hudiTableInfo.getFlinkConfig(); + this.tableBucket = tableBucket; + this.partition = partition; + } + + public boolean scheduleCompaction() { + LOG.info("Scheduling Hudi compaction for table {}.", hudiTableInfo.getTablePath()); + metaClient.reloadActiveTimeline(); + try { + boolean scheduled = writeClient.scheduleCompaction(Option.empty()).isPresent(); + metaClient.reloadActiveTimeline(); + if (!scheduled) { + LOG.info( + "No Hudi compaction plan was scheduled for table {}.", + hudiTableInfo.getTablePath()); + } + return scheduled; + } catch (Exception e) { + LOG.warn( + "Failed to schedule Hudi compaction for table {}.", + hudiTableInfo.getTablePath(), + e); + return false; + } + } + + public void markSelectedCompactionsInflight() { + List<String> compactionInstantTimes = getSelectedCompactionInstantTimes(); + if (compactionInstantTimes.isEmpty()) { + return; + } + + HoodieTimeline pendingCompactionTimeline = + table.getActiveTimeline().filterPendingCompactionTimeline(); + InstantGenerator instantGenerator = table.getInstantGenerator(); + + for (String timestamp : compactionInstantTimes) { + HoodieInstant inflightInstant = + instantGenerator.getCompactionInflightInstant(timestamp); + if (pendingCompactionTimeline.containsInstant(inflightInstant)) { + LOG.info("Rollback stale inflight Hudi compaction instant {}.", timestamp); + table.rollbackInflightCompaction( + inflightInstant, writeClient.getTransactionManager()); + metaClient.reloadActiveTimeline(); + } + } + + pendingCompactionTimeline = table.getActiveTimeline().filterPendingCompactionTimeline(); + for (String timestamp : compactionInstantTimes) { + HoodieInstant requestedInstant = + instantGenerator.getCompactionRequestedInstant(timestamp); + if (pendingCompactionTimeline.containsInstant(requestedInstant)) { + table.getActiveTimeline().transitionCompactionRequestedToInflight(requestedInstant); + LOG.info("Marked Hudi compaction instant {} as inflight.", timestamp); + } + } + metaClient.reloadActiveTimeline(); + } + + public List<String> getInflightCompactionInstantTimes() { + List<String> compactionInstantTimes = getSelectedCompactionInstantTimes(); + if (compactionInstantTimes.isEmpty()) { + return Collections.emptyList(); + } + + HoodieTimeline pendingCompactionTimeline = + metaClient.getActiveTimeline().filterPendingCompactionTimeline(); + InstantGenerator instantGenerator = table.getInstantGenerator(); + + List<String> inflightCompactionInstantTimes = new ArrayList<>(); + for (String timestamp : compactionInstantTimes) { + HoodieInstant inflightInstant = + instantGenerator.getCompactionInflightInstant(timestamp); + if (pendingCompactionTimeline.containsInstant(inflightInstant)) { + inflightCompactionInstantTimes.add(inflightInstant.requestedTime()); + } + } + LOG.info( + "Found {} inflight Hudi compaction instants for table {}.", + inflightCompactionInstantTimes.size(), + hudiTableInfo.getTablePath()); + return inflightCompactionInstantTimes; + } + + public List<Pair<String, HoodieCompactionPlan>> getCompactionPlans( + List<String> compactionInstantTimes) { + if (compactionInstantTimes == null || compactionInstantTimes.isEmpty()) { + return Collections.emptyList(); + } + + List<Pair<String, HoodieCompactionPlan>> compactionPlans = + compactionInstantTimes.stream() + .map( + timestamp -> { + try { + return Pair.of( + timestamp, + CompactionUtils.getCompactionPlan( + metaClient, timestamp)); + } catch (Exception e) { + throw new HoodieException( + "Failed to get Hudi compaction plan " + timestamp, + e); + } + }) + .filter(pair -> isValidCompactionPlan(pair.getRight())) Review Comment: Plans that fail `isValidCompactionPlan` are filtered out here, but at this point `markSelectedCompactionsInflight` has already transitioned the corresponding requested instants to inflight. Nothing in the codebase rolls those inflight instants back, which leaves them as orphan inflight compactions on the timeline forever and will block future compaction scheduling on the same file groups. Either (a) validate the plan before marking it inflight, or (b) explicitly rollback invalid plans here via `table.rollbackInflightCompaction(...)` before filtering them out. ########## fluss-lake/fluss-lake-hudi/src/main/java/org/apache/fluss/lake/hudi/tiering/HudiCompactionService.java: ########## @@ -0,0 +1,465 @@ +/* + * 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. + */ + +package org.apache.fluss.lake.hudi.tiering; + +import org.apache.fluss.metadata.TableBucket; + +import org.apache.flink.configuration.Configuration; +import org.apache.hudi.avro.model.HoodieCompactionOperation; +import org.apache.hudi.avro.model.HoodieCompactionPlan; +import org.apache.hudi.client.HoodieFlinkWriteClient; +import org.apache.hudi.client.WriteStatus; +import org.apache.hudi.common.data.HoodieListData; +import org.apache.hudi.common.engine.HoodieReaderContext; +import org.apache.hudi.common.model.CompactionOperation; +import org.apache.hudi.common.model.HoodieCommitMetadata; +import org.apache.hudi.common.model.HoodieWriteStat; +import org.apache.hudi.common.table.HoodieTableMetaClient; +import org.apache.hudi.common.table.timeline.HoodieInstant; +import org.apache.hudi.common.table.timeline.HoodieTimeline; +import org.apache.hudi.common.table.timeline.InstantGenerator; +import org.apache.hudi.common.util.CompactionUtils; +import org.apache.hudi.common.util.Option; +import org.apache.hudi.common.util.collection.Pair; +import org.apache.hudi.config.HoodieWriteConfig; +import org.apache.hudi.configuration.FlinkOptions; +import org.apache.hudi.exception.HoodieException; +import org.apache.hudi.index.bucket.BucketIdentifier; +import org.apache.hudi.sink.compact.FlinkCompactionConfig; +import org.apache.hudi.sink.compact.strategy.CompactionPlanStrategies; +import org.apache.hudi.storage.StorageConfiguration; +import org.apache.hudi.table.HoodieFlinkTable; +import org.apache.hudi.table.action.compact.CompactHelpers; +import org.apache.hudi.table.action.compact.HoodieFlinkMergeOnReadTableCompactor; +import org.apache.hudi.table.format.FlinkRowDataReaderContext; +import org.apache.hudi.table.format.InternalSchemaManager; +import org.apache.hudi.util.CompactionUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nullable; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.function.Supplier; +import java.util.stream.Collectors; + +import static org.apache.fluss.utils.Preconditions.checkNotNull; + +/** Coordinates Hudi compaction scheduling, execution, and commit for tiering writers. */ +public class HudiCompactionService { + + private static final Logger LOG = LoggerFactory.getLogger(HudiCompactionService.class); + + private final HudiWriteTableInfo hudiTableInfo; + private final HoodieFlinkWriteClient<?> writeClient; + private final HoodieTableMetaClient metaClient; + private final HoodieFlinkTable<?> table; + private final Configuration conf; + @Nullable private final TableBucket tableBucket; + @Nullable private final String partition; + + private InternalSchemaManager internalSchemaManager; + + public HudiCompactionService( + HudiWriteTableInfo hudiTableInfo, + @Nullable TableBucket tableBucket, + @Nullable String partition) { + this.hudiTableInfo = checkNotNull(hudiTableInfo, "Hudi write table info must not be null."); + this.writeClient = hudiTableInfo.getWriteClient(); + this.metaClient = hudiTableInfo.getMetaClient(); + this.table = writeClient.getHoodieTable(); + this.conf = hudiTableInfo.getFlinkConfig(); + this.tableBucket = tableBucket; + this.partition = partition; + } + + public boolean scheduleCompaction() { + LOG.info("Scheduling Hudi compaction for table {}.", hudiTableInfo.getTablePath()); + metaClient.reloadActiveTimeline(); + try { + boolean scheduled = writeClient.scheduleCompaction(Option.empty()).isPresent(); + metaClient.reloadActiveTimeline(); + if (!scheduled) { + LOG.info( + "No Hudi compaction plan was scheduled for table {}.", + hudiTableInfo.getTablePath()); + } + return scheduled; + } catch (Exception e) { + LOG.warn( + "Failed to schedule Hudi compaction for table {}.", + hudiTableInfo.getTablePath(), + e); + return false; + } + } + + public void markSelectedCompactionsInflight() { + List<String> compactionInstantTimes = getSelectedCompactionInstantTimes(); + if (compactionInstantTimes.isEmpty()) { + return; + } + + HoodieTimeline pendingCompactionTimeline = + table.getActiveTimeline().filterPendingCompactionTimeline(); + InstantGenerator instantGenerator = table.getInstantGenerator(); + + for (String timestamp : compactionInstantTimes) { + HoodieInstant inflightInstant = + instantGenerator.getCompactionInflightInstant(timestamp); + if (pendingCompactionTimeline.containsInstant(inflightInstant)) { + LOG.info("Rollback stale inflight Hudi compaction instant {}.", timestamp); + table.rollbackInflightCompaction( + inflightInstant, writeClient.getTransactionManager()); + metaClient.reloadActiveTimeline(); + } + } + + pendingCompactionTimeline = table.getActiveTimeline().filterPendingCompactionTimeline(); + for (String timestamp : compactionInstantTimes) { + HoodieInstant requestedInstant = + instantGenerator.getCompactionRequestedInstant(timestamp); + if (pendingCompactionTimeline.containsInstant(requestedInstant)) { + table.getActiveTimeline().transitionCompactionRequestedToInflight(requestedInstant); + LOG.info("Marked Hudi compaction instant {} as inflight.", timestamp); + } + } + metaClient.reloadActiveTimeline(); + } + + public List<String> getInflightCompactionInstantTimes() { + List<String> compactionInstantTimes = getSelectedCompactionInstantTimes(); + if (compactionInstantTimes.isEmpty()) { + return Collections.emptyList(); + } + + HoodieTimeline pendingCompactionTimeline = + metaClient.getActiveTimeline().filterPendingCompactionTimeline(); + InstantGenerator instantGenerator = table.getInstantGenerator(); + + List<String> inflightCompactionInstantTimes = new ArrayList<>(); + for (String timestamp : compactionInstantTimes) { + HoodieInstant inflightInstant = + instantGenerator.getCompactionInflightInstant(timestamp); + if (pendingCompactionTimeline.containsInstant(inflightInstant)) { + inflightCompactionInstantTimes.add(inflightInstant.requestedTime()); + } + } + LOG.info( + "Found {} inflight Hudi compaction instants for table {}.", + inflightCompactionInstantTimes.size(), + hudiTableInfo.getTablePath()); + return inflightCompactionInstantTimes; + } + + public List<Pair<String, HoodieCompactionPlan>> getCompactionPlans( + List<String> compactionInstantTimes) { + if (compactionInstantTimes == null || compactionInstantTimes.isEmpty()) { + return Collections.emptyList(); + } + + List<Pair<String, HoodieCompactionPlan>> compactionPlans = + compactionInstantTimes.stream() + .map( + timestamp -> { + try { + return Pair.of( + timestamp, + CompactionUtils.getCompactionPlan( + metaClient, timestamp)); + } catch (Exception e) { + throw new HoodieException( + "Failed to get Hudi compaction plan " + timestamp, + e); + } + }) + .filter(pair -> isValidCompactionPlan(pair.getRight())) + .collect(Collectors.toList()); + + LOG.info( + "Loaded {} Hudi compaction plans for table {}.", + compactionPlans.size(), + hudiTableInfo.getTablePath()); + return compactionPlans; + } + + public Map<String, List<WriteStatus>> executeCompaction( + List<Pair<String, HoodieCompactionPlan>> compactionPlans) throws IOException { + if (compactionPlans == null || compactionPlans.isEmpty()) { + return Collections.emptyMap(); + } + TableBucket currentBucket = + checkNotNull(tableBucket, "Hudi compaction execution requires a table bucket."); + + Map<String, List<WriteStatus>> writeStatusesByInstant = new HashMap<>(); + HoodieWriteConfig writeConfig = writeClient.getConfig(); + String hudiPartitionPath = toHudiPartitionPath(partition); + + for (Pair<String, HoodieCompactionPlan> planPair : compactionPlans) { + String instantTime = planPair.getLeft(); + HoodieCompactionPlan compactionPlan = planPair.getRight(); + for (HoodieCompactionOperation operation : compactionPlan.getOperations()) { + if (!belongsToCurrentWriter(operation, hudiPartitionPath, currentBucket)) { + continue; + } + + HoodieFlinkMergeOnReadTableCompactor<?> compactor = + new HoodieFlinkMergeOnReadTableCompactor<>(); + CompactionOperation compactionOperation = + CompactionOperation.convertFromAvroRecordInstance(operation); + + metaClient.reload(); Review Comment: `metaClient.reload()` (and `setAvroSchema` on the next line) are called once per compaction operation. A single plan typically contains many operations across the same file groups, so reloading the metaClient inside the inner loop is wasted work. Move `reload()` and `setAvroSchema()` out to the per-plan loop (around line 219), they only need to run once per instant. -- 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]
