-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[HUDI-2207] Support independent flink hudi clustering function #3599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,8 +29,10 @@ | |
| import org.apache.hudi.common.model.HoodieRecord; | ||
| import org.apache.hudi.common.model.HoodieRecordLocation; | ||
| import org.apache.hudi.common.model.HoodieRecordPayload; | ||
| import org.apache.hudi.common.model.HoodieReplaceCommitMetadata; | ||
| import org.apache.hudi.common.model.HoodieTableType; | ||
| import org.apache.hudi.common.model.HoodieWriteStat; | ||
| import org.apache.hudi.common.model.TableServiceType; | ||
| import org.apache.hudi.common.model.WriteOperationType; | ||
| import org.apache.hudi.common.table.HoodieTableMetaClient; | ||
| import org.apache.hudi.common.table.HoodieTableVersion; | ||
|
|
@@ -39,6 +41,7 @@ | |
| import org.apache.hudi.common.table.timeline.HoodieTimeline; | ||
| import org.apache.hudi.common.util.Option; | ||
| import org.apache.hudi.config.HoodieWriteConfig; | ||
| import org.apache.hudi.exception.HoodieClusteringException; | ||
| import org.apache.hudi.exception.HoodieCommitException; | ||
| import org.apache.hudi.exception.HoodieNotSupportedException; | ||
| import org.apache.hudi.index.FlinkHoodieIndexFactory; | ||
|
|
@@ -68,6 +71,8 @@ | |
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.text.ParseException; | ||
| import java.util.HashMap; | ||
| import java.util.Iterator; | ||
|
|
@@ -399,6 +404,52 @@ public HoodieWriteMetadata<List<WriteStatus>> cluster(final String clusteringIns | |
| throw new HoodieNotSupportedException("Clustering is not supported yet"); | ||
| } | ||
|
|
||
| private void completeClustering( | ||
| HoodieReplaceCommitMetadata metadata, | ||
| HoodieTable<T, List<HoodieRecord<T>>, List<HoodieKey>, List<WriteStatus>> table, | ||
| String clusteringCommitTime) { | ||
| this.context.setJobStatus(this.getClass().getSimpleName(), "Collect clustering write status and commit clustering"); | ||
| HoodieInstant clusteringInstant = new HoodieInstant(HoodieInstant.State.INFLIGHT, HoodieTimeline.REPLACE_COMMIT_ACTION, clusteringCommitTime); | ||
| List<HoodieWriteStat> writeStats = metadata.getPartitionToWriteStats().entrySet().stream().flatMap(e -> | ||
| e.getValue().stream()).collect(Collectors.toList()); | ||
| if (writeStats.stream().mapToLong(HoodieWriteStat::getTotalWriteErrors).sum() > 0) { | ||
| throw new HoodieClusteringException("Clustering failed to write to files:" | ||
| + writeStats.stream().filter(s -> s.getTotalWriteErrors() > 0L).map(HoodieWriteStat::getFileId).collect(Collectors.joining(","))); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| try { | ||
| this.txnManager.beginTransaction(Option.of(clusteringInstant), Option.empty()); | ||
| finalizeWrite(table, clusteringCommitTime, writeStats); | ||
| // commit to data table after committing to metadata table. | ||
| // Do not do any conflict resolution here as we do with regular writes. We take the lock here to ensure all writes to metadata table happens within a | ||
| // single lock (single writer). Because more than one write to metadata table will result in conflicts since all of them updates the same partition. | ||
| writeTableMetadata(table, clusteringCommitTime, clusteringInstant.getAction(), metadata); | ||
| LOG.info("Committing Clustering {} finished with result {}.", clusteringCommitTime, metadata); | ||
| table.getActiveTimeline().transitionReplaceInflightToComplete( | ||
| HoodieTimeline.getReplaceCommitInflightInstant(clusteringCommitTime), | ||
| Option.of(metadata.toJsonString().getBytes(StandardCharsets.UTF_8))); | ||
| } catch (IOException e) { | ||
| throw new HoodieClusteringException( | ||
| "Failed to commit " + table.getMetaClient().getBasePath() + " at time " + clusteringCommitTime, e); | ||
| } finally { | ||
| this.txnManager.endTransaction(Option.of(clusteringInstant)); | ||
| } | ||
|
|
||
| WriteMarkersFactory.get(config.getMarkersType(), table, clusteringCommitTime) | ||
| .quietDeleteMarkerDir(context, config.getMarkersDeleteParallelism()); | ||
| if (clusteringTimer != null) { | ||
| long durationInMs = metrics.getDurationInMs(clusteringTimer.stop()); | ||
| try { | ||
| metrics.updateCommitMetrics(HoodieActiveTimeline.parseDateFromInstantTime(clusteringCommitTime).getTime(), | ||
| durationInMs, metadata, HoodieActiveTimeline.REPLACE_COMMIT_ACTION); | ||
| } catch (ParseException e) { | ||
| throw new HoodieCommitException("Commit time is not of valid format. Failed to commit compaction " | ||
| + config.getBasePath() + " at time " + clusteringCommitTime, e); | ||
| } | ||
| } | ||
| LOG.info("Clustering successfully on commit " + clusteringCommitTime); | ||
| } | ||
|
|
||
| @Override | ||
| protected HoodieTable doInitTable(HoodieTableMetaClient metaClient, Option<String> instantTime, boolean initialMetadataTableIfNecessary) { | ||
| // Create a Hoodie table which encapsulated the commits and files visible | ||
|
|
@@ -412,6 +463,23 @@ protected void tryUpgrade(HoodieTableMetaClient metaClient, Option<String> insta | |
| // no need to execute the upgrade/downgrade on each write in streaming. | ||
| } | ||
|
|
||
| public void completeTableService( | ||
| TableServiceType tableServiceType, | ||
| HoodieCommitMetadata metadata, | ||
| HoodieTable<T, List<HoodieRecord<T>>, List<HoodieKey>, List<WriteStatus>> table, | ||
| String commitInstant) { | ||
| switch (tableServiceType) { | ||
| case CLUSTER: | ||
| completeClustering((HoodieReplaceCommitMetadata) metadata, table, commitInstant); | ||
| break; | ||
| case COMPACT: | ||
| completeCompaction(metadata, table, commitInstant); | ||
| break; | ||
| default: | ||
| throw new IllegalArgumentException("This table service is not valid " + tableServiceType); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Upgrade downgrade the Hoodie table. | ||
| * | ||
|
|
||
65 changes: 65 additions & 0 deletions
65
...rg/apache/hudi/client/clustering/plan/strategy/FlinkRecentDaysClusteringPlanStrategy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * 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.hudi.client.clustering.plan.strategy; | ||
|
|
||
| import org.apache.hudi.client.common.HoodieFlinkEngineContext; | ||
| import org.apache.hudi.common.model.HoodieRecordPayload; | ||
| import org.apache.hudi.config.HoodieWriteConfig; | ||
| import org.apache.hudi.table.HoodieFlinkCopyOnWriteTable; | ||
| import org.apache.hudi.table.HoodieFlinkMergeOnReadTable; | ||
|
|
||
| import org.apache.log4j.LogManager; | ||
| import org.apache.log4j.Logger; | ||
|
|
||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * Clustering Strategy based on following. | ||
| * 1) Only looks at latest 'daybased.lookback.partitions' partitions. | ||
| * 2) Excludes files that are greater than 'small.file.limit' from clustering plan. | ||
| */ | ||
| public class FlinkRecentDaysClusteringPlanStrategy<T extends HoodieRecordPayload<T>> | ||
| extends FlinkSizeBasedClusteringPlanStrategy<T> { | ||
| private static final Logger LOG = LogManager.getLogger(FlinkRecentDaysClusteringPlanStrategy.class); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we extend from |
||
|
|
||
| public FlinkRecentDaysClusteringPlanStrategy(HoodieFlinkCopyOnWriteTable<T> table, | ||
| HoodieFlinkEngineContext engineContext, | ||
| HoodieWriteConfig writeConfig) { | ||
| super(table, engineContext, writeConfig); | ||
| } | ||
|
|
||
| public FlinkRecentDaysClusteringPlanStrategy(HoodieFlinkMergeOnReadTable<T> table, | ||
| HoodieFlinkEngineContext engineContext, | ||
| HoodieWriteConfig writeConfig) { | ||
| super(table, engineContext, writeConfig); | ||
| } | ||
|
|
||
| @Override | ||
| protected List<String> filterPartitionPaths(List<String> partitionPaths) { | ||
| int targetPartitionsForClustering = getWriteConfig().getTargetPartitionsForClustering(); | ||
| int skipPartitionsFromLatestForClustering = getWriteConfig().getSkipPartitionsFromLatestForClustering(); | ||
| return partitionPaths.stream() | ||
| .sorted(Comparator.reverseOrder()) | ||
| .skip(Math.max(skipPartitionsFromLatestForClustering, 0)) | ||
| .limit(targetPartitionsForClustering > 0 ? targetPartitionsForClustering : partitionPaths.size()) | ||
| .collect(Collectors.toList()); | ||
| } | ||
| } | ||
67 changes: 67 additions & 0 deletions
67
...e/hudi/client/clustering/plan/strategy/FlinkSelectedPartitionsClusteringPlanStrategy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| * 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.hudi.client.clustering.plan.strategy; | ||
|
|
||
| import org.apache.hudi.client.common.HoodieFlinkEngineContext; | ||
| import org.apache.hudi.common.model.HoodieRecordPayload; | ||
| import org.apache.hudi.config.HoodieWriteConfig; | ||
| import org.apache.hudi.table.HoodieFlinkCopyOnWriteTable; | ||
| import org.apache.hudi.table.HoodieFlinkMergeOnReadTable; | ||
|
|
||
| import org.apache.log4j.LogManager; | ||
| import org.apache.log4j.Logger; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import static org.apache.hudi.config.HoodieClusteringConfig.CLUSTERING_STRATEGY_PARAM_PREFIX; | ||
|
|
||
| /** | ||
| * Clustering Strategy to filter just specified partitions from [begin, end]. Note both begin and end are inclusive. | ||
| */ | ||
| public class FlinkSelectedPartitionsClusteringPlanStrategy<T extends HoodieRecordPayload<T>> | ||
| extends FlinkSizeBasedClusteringPlanStrategy<T> { | ||
| private static final Logger LOG = LogManager.getLogger(FlinkSelectedPartitionsClusteringPlanStrategy.class); | ||
|
|
||
| public static final String CONF_BEGIN_PARTITION = CLUSTERING_STRATEGY_PARAM_PREFIX + "cluster.begin.partition"; | ||
| public static final String CONF_END_PARTITION = CLUSTERING_STRATEGY_PARAM_PREFIX + "cluster.end.partition"; | ||
|
|
||
| public FlinkSelectedPartitionsClusteringPlanStrategy(HoodieFlinkCopyOnWriteTable<T> table, | ||
| HoodieFlinkEngineContext engineContext, | ||
| HoodieWriteConfig writeConfig) { | ||
| super(table, engineContext, writeConfig); | ||
| } | ||
|
|
||
| public FlinkSelectedPartitionsClusteringPlanStrategy(HoodieFlinkMergeOnReadTable<T> table, | ||
| HoodieFlinkEngineContext engineContext, | ||
| HoodieWriteConfig writeConfig) { | ||
| super(table, engineContext, writeConfig); | ||
| } | ||
|
|
||
| @Override | ||
| protected List<String> filterPartitionPaths(List<String> partitionPaths) { | ||
| String beginPartition = getWriteConfig().getProps().getProperty(CONF_BEGIN_PARTITION); | ||
| String endPartition = getWriteConfig().getProps().getProperty(CONF_END_PARTITION); | ||
| List<String> filteredPartitions = partitionPaths.stream() | ||
| .filter(path -> path.compareTo(beginPartition) >= 0 && path.compareTo(endPartition) <= 0) | ||
| .collect(Collectors.toList()); | ||
| LOG.info("Filtered to the following partitions: " + filteredPartitions); | ||
| return filteredPartitions; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Align the param documents.