yanghua commented on a change in pull request #2260: URL: https://github.com/apache/hudi/pull/2260#discussion_r559506139
########## File path: hudi-client/hudi-client-common/src/main/java/org/apache/hudi/table/action/compact/CompactType.java ########## @@ -0,0 +1,23 @@ +/* + * 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.table.action.compact; + +public enum CompactType { + COMMIT_NUM, TIME_ELAPSED, NUM_AND_TIME, NUM_OR_TIME Review comment: `COMMIT_NUM` and `NUM ` do not keep consistent. What about `NUM` or `COMMITS`? IMO, `CompactType` may make users confused. what about `CompactionTriggerStrategy` Or `CompactionScheduleStrategy` ########## File path: hudi-client/hudi-spark-client/src/main/java/org/apache/hudi/table/action/compact/SparkScheduleCompactionActionExecutor.java ########## @@ -58,36 +62,98 @@ public SparkScheduleCompactionActionExecutor(HoodieEngineContext context, @Override protected HoodieCompactionPlan scheduleCompaction() { LOG.info("Checking if compaction needs to be run on " + config.getBasePath()); + // judge if we need to compact according to num delta commits and time elapsed + boolean compactable = needCompact(config.getInlineCompactType()); + if (compactable) { + LOG.info("Generating compaction plan for merge on read table " + config.getBasePath()); + HoodieSparkMergeOnReadTableCompactor compactor = new HoodieSparkMergeOnReadTableCompactor(); + try { + SyncableFileSystemView fileSystemView = (SyncableFileSystemView) table.getSliceView(); + Set<HoodieFileGroupId> fgInPendingCompactionAndClustering = fileSystemView.getPendingCompactionOperations() + .map(instantTimeOpPair -> instantTimeOpPair.getValue().getFileGroupId()) + .collect(Collectors.toSet()); + // exclude files in pending clustering from compaction. + fgInPendingCompactionAndClustering.addAll(fileSystemView.getFileGroupsInPendingClustering().map(Pair::getLeft).collect(Collectors.toSet())); + return compactor.generateCompactionPlan(context, table, config, instantTime, fgInPendingCompactionAndClustering); + } catch (IOException e) { + throw new HoodieCompactionException("Could not schedule compaction " + config.getBasePath(), e); + } + } + + return new HoodieCompactionPlan(); + } + + public Tuple2<Integer, String> checkCompact(CompactType compactType) { Option<HoodieInstant> lastCompaction = table.getActiveTimeline().getCommitTimeline() - .filterCompletedInstants().lastInstant(); - String lastCompactionTs = "0"; + .filterCompletedInstants().lastInstant(); Review comment: Revert this change, please. ########## File path: hudi-client/hudi-spark-client/src/main/java/org/apache/hudi/table/action/compact/SparkScheduleCompactionActionExecutor.java ########## @@ -58,36 +62,98 @@ public SparkScheduleCompactionActionExecutor(HoodieEngineContext context, @Override protected HoodieCompactionPlan scheduleCompaction() { LOG.info("Checking if compaction needs to be run on " + config.getBasePath()); + // judge if we need to compact according to num delta commits and time elapsed + boolean compactable = needCompact(config.getInlineCompactType()); + if (compactable) { + LOG.info("Generating compaction plan for merge on read table " + config.getBasePath()); + HoodieSparkMergeOnReadTableCompactor compactor = new HoodieSparkMergeOnReadTableCompactor(); + try { + SyncableFileSystemView fileSystemView = (SyncableFileSystemView) table.getSliceView(); + Set<HoodieFileGroupId> fgInPendingCompactionAndClustering = fileSystemView.getPendingCompactionOperations() + .map(instantTimeOpPair -> instantTimeOpPair.getValue().getFileGroupId()) + .collect(Collectors.toSet()); + // exclude files in pending clustering from compaction. + fgInPendingCompactionAndClustering.addAll(fileSystemView.getFileGroupsInPendingClustering().map(Pair::getLeft).collect(Collectors.toSet())); + return compactor.generateCompactionPlan(context, table, config, instantTime, fgInPendingCompactionAndClustering); + } catch (IOException e) { + throw new HoodieCompactionException("Could not schedule compaction " + config.getBasePath(), e); + } + } + + return new HoodieCompactionPlan(); + } + + public Tuple2<Integer, String> checkCompact(CompactType compactType) { Review comment: IMO, `getLastDeltaCommitInfo` sounds better? Correct me, if it's not good for you. ########## File path: hudi-client/hudi-client-common/src/main/java/org/apache/hudi/config/HoodieWriteConfig.java ########## @@ -468,7 +477,7 @@ public String getClusteringExecutionStrategyClass() { public long getClusteringMaxBytesInGroup() { return Long.parseLong(props.getProperty(HoodieClusteringConfig.CLUSTERING_MAX_BYTES_PER_GROUP)); } - + Review comment: Still exists? ########## File path: hudi-client/hudi-client-common/src/main/java/org/apache/hudi/config/HoodieWriteConfig.java ########## @@ -484,7 +493,7 @@ public long getClusteringTargetFileMaxBytes() { public int getTargetPartitionsForClustering() { return Integer.parseInt(props.getProperty(HoodieClusteringConfig.CLUSTERING_TARGET_PARTITIONS)); } - + Review comment: ditto ########## File path: hudi-client/hudi-spark-client/src/main/java/org/apache/hudi/table/action/compact/SparkScheduleCompactionActionExecutor.java ########## @@ -58,36 +62,98 @@ public SparkScheduleCompactionActionExecutor(HoodieEngineContext context, @Override protected HoodieCompactionPlan scheduleCompaction() { LOG.info("Checking if compaction needs to be run on " + config.getBasePath()); + // judge if we need to compact according to num delta commits and time elapsed + boolean compactable = needCompact(config.getInlineCompactType()); + if (compactable) { + LOG.info("Generating compaction plan for merge on read table " + config.getBasePath()); + HoodieSparkMergeOnReadTableCompactor compactor = new HoodieSparkMergeOnReadTableCompactor(); + try { + SyncableFileSystemView fileSystemView = (SyncableFileSystemView) table.getSliceView(); + Set<HoodieFileGroupId> fgInPendingCompactionAndClustering = fileSystemView.getPendingCompactionOperations() + .map(instantTimeOpPair -> instantTimeOpPair.getValue().getFileGroupId()) + .collect(Collectors.toSet()); + // exclude files in pending clustering from compaction. + fgInPendingCompactionAndClustering.addAll(fileSystemView.getFileGroupsInPendingClustering().map(Pair::getLeft).collect(Collectors.toSet())); + return compactor.generateCompactionPlan(context, table, config, instantTime, fgInPendingCompactionAndClustering); + } catch (IOException e) { + throw new HoodieCompactionException("Could not schedule compaction " + config.getBasePath(), e); + } + } + + return new HoodieCompactionPlan(); + } + + public Tuple2<Integer, String> checkCompact(CompactType compactType) { Option<HoodieInstant> lastCompaction = table.getActiveTimeline().getCommitTimeline() - .filterCompletedInstants().lastInstant(); - String lastCompactionTs = "0"; + .filterCompletedInstants().lastInstant(); + HoodieTimeline deltaCommits = table.getActiveTimeline().getDeltaCommitTimeline(); + + String lastCompactionTs; + int deltaCommitsSinceLastCompaction = 0; if (lastCompaction.isPresent()) { lastCompactionTs = lastCompaction.get().getTimestamp(); + } else { + lastCompactionTs = deltaCommits.firstInstant().get().getTimestamp(); } - - int deltaCommitsSinceLastCompaction = table.getActiveTimeline().getDeltaCommitTimeline() - .findInstantsAfter(lastCompactionTs, Integer.MAX_VALUE).countInstants(); - if (config.getInlineCompactDeltaCommitMax() > deltaCommitsSinceLastCompaction) { - LOG.info("Not scheduling compaction as only " + deltaCommitsSinceLastCompaction - + " delta commits was found since last compaction " + lastCompactionTs + ". Waiting for " - + config.getInlineCompactDeltaCommitMax()); - return new HoodieCompactionPlan(); + if (compactType != CompactType.TIME_ELAPSED) { + if (lastCompaction.isPresent()) { + deltaCommitsSinceLastCompaction = deltaCommits.findInstantsAfter(lastCompactionTs, Integer.MAX_VALUE).countInstants(); + } else { + deltaCommitsSinceLastCompaction = deltaCommits.findInstantsAfterOrEquals(lastCompactionTs, Integer.MAX_VALUE).countInstants(); + } } + return new Tuple2(deltaCommitsSinceLastCompaction, lastCompactionTs); + } - LOG.info("Generating compaction plan for merge on read table " + config.getBasePath()); - HoodieSparkMergeOnReadTableCompactor compactor = new HoodieSparkMergeOnReadTableCompactor(); - try { - SyncableFileSystemView fileSystemView = (SyncableFileSystemView) table.getSliceView(); - Set<HoodieFileGroupId> fgInPendingCompactionAndClustering = fileSystemView.getPendingCompactionOperations() - .map(instantTimeOpPair -> instantTimeOpPair.getValue().getFileGroupId()) - .collect(Collectors.toSet()); - // exclude files in pending clustering from compaction. - fgInPendingCompactionAndClustering.addAll(fileSystemView.getFileGroupsInPendingClustering().map(Pair::getLeft).collect(Collectors.toSet())); - return compactor.generateCompactionPlan(context, table, config, instantTime, fgInPendingCompactionAndClustering); + public boolean needCompact(CompactType compactType) { + boolean compactable; + // return deltaCommitsSinceLastCompaction and lastCompactionTs + Tuple2<Integer, String> threshold = checkCompact(compactType); + int inlineCompactDeltaCommitMax = config.getInlineCompactDeltaCommitMax(); + int inlineCompactDeltaElapsedTimeMax = config.getInlineCompactDeltaElapsedTimeMax(); + switch (compactType) { + case COMMIT_NUM: + compactable = inlineCompactDeltaCommitMax <= threshold._1; + LOG.info(String.format("Trigger compaction when commit_num >=%s", inlineCompactDeltaCommitMax)); + break; + case TIME_ELAPSED: + compactable = parseToTimestamp(threshold._2) + inlineCompactDeltaElapsedTimeMax <= parseToTimestamp(instantTime); + LOG.info(String.format("Trigger compaction when elapsed_time >=%ss", inlineCompactDeltaElapsedTimeMax)); + break; + case NUM_OR_TIME: + compactable = inlineCompactDeltaCommitMax <= threshold._1 + || parseToTimestamp(threshold._2) + inlineCompactDeltaElapsedTimeMax <= parseToTimestamp(instantTime); + LOG.info(String.format("Trigger compaction when commit_num >=%s or elapsed_time >=%ss", inlineCompactDeltaCommitMax, + inlineCompactDeltaElapsedTimeMax)); + break; + case NUM_AND_TIME: + compactable = inlineCompactDeltaCommitMax <= threshold._1 + && parseToTimestamp(threshold._2) + inlineCompactDeltaElapsedTimeMax <= parseToTimestamp(instantTime); + LOG.info(String.format("Trigger compaction when commit_num >=%s and elapsed_time >=%ss", inlineCompactDeltaCommitMax, + inlineCompactDeltaElapsedTimeMax)); + break; + default: + throw new HoodieCompactionException("Unsupported compact type: " + config.getInlineCompactType()); + } - } catch (IOException e) { - throw new HoodieCompactionException("Could not schedule compaction " + config.getBasePath(), e); + if (compactable) { + LOG.info(String.format("Scheduling compaction: %s. Delta commits found: %s times, and last compaction time is %s.", + compactType.name(), threshold._1, threshold._2)); + } else { + LOG.info(String.format("Not scheduling compaction as only %s delta commits was found since last compaction %s." + + "Waiting for %s,or %sms elapsed time need since last compaction %s.", threshold._1, + threshold._2, config.getInlineCompactDeltaCommitMax(), config.getInlineCompactDeltaElapsedTimeMax(), threshold._2)); } + return compactable; } + public Long parseToTimestamp(String time) { Review comment: `Timestamp` is not clear, it means `second` or `mills` or something else? ########## File path: hudi-client/hudi-spark-client/src/main/java/org/apache/hudi/table/action/compact/SparkScheduleCompactionActionExecutor.java ########## @@ -58,36 +62,98 @@ public SparkScheduleCompactionActionExecutor(HoodieEngineContext context, @Override protected HoodieCompactionPlan scheduleCompaction() { LOG.info("Checking if compaction needs to be run on " + config.getBasePath()); + // judge if we need to compact according to num delta commits and time elapsed + boolean compactable = needCompact(config.getInlineCompactType()); + if (compactable) { + LOG.info("Generating compaction plan for merge on read table " + config.getBasePath()); + HoodieSparkMergeOnReadTableCompactor compactor = new HoodieSparkMergeOnReadTableCompactor(); + try { + SyncableFileSystemView fileSystemView = (SyncableFileSystemView) table.getSliceView(); + Set<HoodieFileGroupId> fgInPendingCompactionAndClustering = fileSystemView.getPendingCompactionOperations() + .map(instantTimeOpPair -> instantTimeOpPair.getValue().getFileGroupId()) + .collect(Collectors.toSet()); + // exclude files in pending clustering from compaction. + fgInPendingCompactionAndClustering.addAll(fileSystemView.getFileGroupsInPendingClustering().map(Pair::getLeft).collect(Collectors.toSet())); + return compactor.generateCompactionPlan(context, table, config, instantTime, fgInPendingCompactionAndClustering); + } catch (IOException e) { + throw new HoodieCompactionException("Could not schedule compaction " + config.getBasePath(), e); + } + } + + return new HoodieCompactionPlan(); + } + + public Tuple2<Integer, String> checkCompact(CompactType compactType) { Option<HoodieInstant> lastCompaction = table.getActiveTimeline().getCommitTimeline() - .filterCompletedInstants().lastInstant(); - String lastCompactionTs = "0"; + .filterCompletedInstants().lastInstant(); + HoodieTimeline deltaCommits = table.getActiveTimeline().getDeltaCommitTimeline(); + + String lastCompactionTs; + int deltaCommitsSinceLastCompaction = 0; if (lastCompaction.isPresent()) { lastCompactionTs = lastCompaction.get().getTimestamp(); + } else { + lastCompactionTs = deltaCommits.firstInstant().get().getTimestamp(); } - - int deltaCommitsSinceLastCompaction = table.getActiveTimeline().getDeltaCommitTimeline() - .findInstantsAfter(lastCompactionTs, Integer.MAX_VALUE).countInstants(); - if (config.getInlineCompactDeltaCommitMax() > deltaCommitsSinceLastCompaction) { - LOG.info("Not scheduling compaction as only " + deltaCommitsSinceLastCompaction - + " delta commits was found since last compaction " + lastCompactionTs + ". Waiting for " - + config.getInlineCompactDeltaCommitMax()); - return new HoodieCompactionPlan(); + if (compactType != CompactType.TIME_ELAPSED) { + if (lastCompaction.isPresent()) { + deltaCommitsSinceLastCompaction = deltaCommits.findInstantsAfter(lastCompactionTs, Integer.MAX_VALUE).countInstants(); + } else { + deltaCommitsSinceLastCompaction = deltaCommits.findInstantsAfterOrEquals(lastCompactionTs, Integer.MAX_VALUE).countInstants(); + } } + return new Tuple2(deltaCommitsSinceLastCompaction, lastCompactionTs); + } - LOG.info("Generating compaction plan for merge on read table " + config.getBasePath()); - HoodieSparkMergeOnReadTableCompactor compactor = new HoodieSparkMergeOnReadTableCompactor(); - try { - SyncableFileSystemView fileSystemView = (SyncableFileSystemView) table.getSliceView(); - Set<HoodieFileGroupId> fgInPendingCompactionAndClustering = fileSystemView.getPendingCompactionOperations() - .map(instantTimeOpPair -> instantTimeOpPair.getValue().getFileGroupId()) - .collect(Collectors.toSet()); - // exclude files in pending clustering from compaction. - fgInPendingCompactionAndClustering.addAll(fileSystemView.getFileGroupsInPendingClustering().map(Pair::getLeft).collect(Collectors.toSet())); - return compactor.generateCompactionPlan(context, table, config, instantTime, fgInPendingCompactionAndClustering); + public boolean needCompact(CompactType compactType) { + boolean compactable; + // return deltaCommitsSinceLastCompaction and lastCompactionTs + Tuple2<Integer, String> threshold = checkCompact(compactType); + int inlineCompactDeltaCommitMax = config.getInlineCompactDeltaCommitMax(); + int inlineCompactDeltaElapsedTimeMax = config.getInlineCompactDeltaElapsedTimeMax(); + switch (compactType) { + case COMMIT_NUM: + compactable = inlineCompactDeltaCommitMax <= threshold._1; + LOG.info(String.format("Trigger compaction when commit_num >=%s", inlineCompactDeltaCommitMax)); Review comment: `The commit number is larger than xxx, trigger compaction scheduler.` sounds better? ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected]
