-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HIVE-28600: Iceberg: Check whether the table/partition requires compa…
…ction before initiating one (Dmitriy Fingerman, reviewed by Denys Kuzmenko) Closes #5529
- Loading branch information
Showing
29 changed files
with
2,357 additions
and
84 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
97 changes: 97 additions & 0 deletions
97
...rg-handler/src/main/java/org/apache/iceberg/mr/hive/compaction/IcebergCompactionUtil.java
This file contains 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,97 @@ | ||
/* | ||
* 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.iceberg.mr.hive.compaction; | ||
|
||
import java.util.List; | ||
import org.apache.iceberg.ContentFile; | ||
import org.apache.iceberg.DataFile; | ||
import org.apache.iceberg.DeleteFile; | ||
import org.apache.iceberg.FileScanTask; | ||
import org.apache.iceberg.MetadataTableType; | ||
import org.apache.iceberg.MetadataTableUtils; | ||
import org.apache.iceberg.PositionDeletesScanTask; | ||
import org.apache.iceberg.ScanTask; | ||
import org.apache.iceberg.Table; | ||
import org.apache.iceberg.io.CloseableIterable; | ||
import org.apache.iceberg.relocated.com.google.common.collect.Lists; | ||
|
||
public class IcebergCompactionUtil { | ||
|
||
private IcebergCompactionUtil() { | ||
|
||
} | ||
|
||
/** | ||
* This method implements a common filter that is used in several places in Iceberg compaction code. | ||
* Its aim is to determine if the provided file needs to be handled when compacting a partition whose path is equal to | ||
* the provided partitionPath. Returns true when one of the following conditions is true, otherwise returns false: | ||
* 1. table is unpartitioned | ||
* 2. partitionPath is null and the file belongs to the non-latest partition spec | ||
* 3. partitionPath is not null and the file belongs to the partition whose path is the partitionPath | ||
* @param table the iceberg table | ||
* @param partitionPath partition path | ||
* @param file Data or Delete file | ||
*/ | ||
public static boolean shouldIncludeForCompaction(Table table, String partitionPath, ContentFile<?> file) { | ||
return !table.spec().isPartitioned() || | ||
partitionPath == null && file.specId() != table.spec().specId() || | ||
partitionPath != null && | ||
table.specs().get(file.specId()).partitionToPath(file.partition()).equals(partitionPath); | ||
} | ||
|
||
/** | ||
* Returns table's list of data files as following: | ||
* 1. If the table is unpartitioned, returns all data files. | ||
* 2. If partitionPath is not provided, returns all data files that belong to the non-latest partition spec. | ||
* 3. If partitionPath is provided, returns all data files that belong to the corresponding partition. | ||
* @param table the iceberg table | ||
* @param partitionPath partition path | ||
*/ | ||
public static List<DataFile> getDataFiles(Table table, String partitionPath) { | ||
CloseableIterable<FileScanTask> fileScanTasks = | ||
table.newScan().useSnapshot(table.currentSnapshot().snapshotId()).ignoreResiduals().planFiles(); | ||
CloseableIterable<FileScanTask> filteredFileScanTasks = | ||
CloseableIterable.filter(fileScanTasks, t -> { | ||
DataFile file = t.asFileScanTask().file(); | ||
return shouldIncludeForCompaction(table, partitionPath, file); | ||
}); | ||
return Lists.newArrayList(CloseableIterable.transform(filteredFileScanTasks, t -> t.file())); | ||
} | ||
|
||
/** | ||
* Returns table's list of delete files as following: | ||
* 1. If the table is unpartitioned, returns all delete files. | ||
* 2. If partitionPath is not provided, returns all delete files that belong to the non-latest partition spec. | ||
* 3. If partitionPath is provided, returns all delete files that belong to corresponding partition. | ||
* @param table the iceberg table | ||
* @param partitionPath partition path | ||
*/ | ||
public static List<DeleteFile> getDeleteFiles(Table table, String partitionPath) { | ||
Table deletesTable = | ||
MetadataTableUtils.createMetadataTableInstance(table, MetadataTableType.POSITION_DELETES); | ||
CloseableIterable<ScanTask> deletesScanTasks = deletesTable.newBatchScan().planFiles(); | ||
CloseableIterable<ScanTask> filteredDeletesScanTasks = | ||
CloseableIterable.filter(deletesScanTasks, t -> { | ||
DeleteFile file = ((PositionDeletesScanTask) t).file(); | ||
return shouldIncludeForCompaction(table, partitionPath, file); | ||
}); | ||
return Lists.newArrayList(CloseableIterable.transform(filteredDeletesScanTasks, | ||
t -> ((PositionDeletesScanTask) t).file())); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...ndler/src/main/java/org/apache/iceberg/mr/hive/compaction/evaluator/HiveTableRuntime.java
This file contains 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,29 @@ | ||
/* | ||
* 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.iceberg.mr.hive.compaction.evaluator; | ||
|
||
import org.apache.iceberg.mr.hive.compaction.evaluator.amoro.TableRuntime; | ||
import org.apache.iceberg.mr.hive.compaction.evaluator.amoro.TableRuntimeMeta; | ||
|
||
public class HiveTableRuntime extends TableRuntime { | ||
|
||
public HiveTableRuntime(TableRuntimeMeta tableRuntimeMeta) { | ||
super(tableRuntimeMeta); | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
...main/java/org/apache/iceberg/mr/hive/compaction/evaluator/IcebergCompactionEvaluator.java
This file contains 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,129 @@ | ||
/* | ||
* 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.iceberg.mr.hive.compaction.evaluator; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.util.Collections; | ||
import org.apache.hadoop.hive.conf.HiveConf; | ||
import org.apache.hadoop.hive.metastore.api.CompactionType; | ||
import org.apache.iceberg.DataFile; | ||
import org.apache.iceberg.PartitionSpec; | ||
import org.apache.iceberg.StructLike; | ||
import org.apache.iceberg.Table; | ||
import org.apache.iceberg.io.CloseableIterable; | ||
import org.apache.iceberg.mr.hive.compaction.IcebergCompactionUtil; | ||
import org.apache.iceberg.mr.hive.compaction.evaluator.amoro.CommonPartitionEvaluator; | ||
import org.apache.iceberg.mr.hive.compaction.evaluator.amoro.IcebergTableFileScanHelper; | ||
import org.apache.iceberg.mr.hive.compaction.evaluator.amoro.OptimizingConfig; | ||
import org.apache.iceberg.mr.hive.compaction.evaluator.amoro.TableConfiguration; | ||
import org.apache.iceberg.mr.hive.compaction.evaluator.amoro.TableFileScanHelper; | ||
import org.apache.iceberg.mr.hive.compaction.evaluator.amoro.TableFormat; | ||
import org.apache.iceberg.mr.hive.compaction.evaluator.amoro.TableRuntime; | ||
import org.apache.iceberg.mr.hive.compaction.evaluator.amoro.TableRuntimeMeta; | ||
import org.apache.iceberg.util.Pair; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class IcebergCompactionEvaluator { | ||
|
||
private static final long LAST_OPTIMIZE_TIME = 0; | ||
private static final int TRIGGER_INTERVAL = 0; | ||
|
||
private IcebergCompactionEvaluator() { | ||
|
||
} | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(IcebergCompactionEvaluator.class); | ||
|
||
public static boolean isEligibleForCompaction(Table icebergTable, String partitionPath, | ||
CompactionType compactionType, HiveConf conf) { | ||
|
||
if (icebergTable.currentSnapshot() == null) { | ||
LOG.info("Table {}{} doesn't require compaction because it is empty", icebergTable, | ||
partitionPath == null ? "" : " partition " + partitionPath); | ||
return false; | ||
} | ||
|
||
CommonPartitionEvaluator partitionEvaluator = createCommonPartitionEvaluator(icebergTable, partitionPath, conf); | ||
|
||
if (partitionEvaluator == null) { | ||
return false; | ||
} | ||
|
||
switch (compactionType) { | ||
case MINOR: | ||
return partitionEvaluator.isMinorNecessary(); | ||
case MAJOR: | ||
return partitionEvaluator.isFullNecessary() || partitionEvaluator.isMajorNecessary(); | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
private static TableRuntime createTableRuntime(Table icebergTable, HiveConf conf) { | ||
long targetFileSizeBytes = HiveConf.getSizeVar(conf, | ||
HiveConf.ConfVars.HIVE_ICEBERG_COMPACTION_TARGET_FILE_SIZE); | ||
|
||
OptimizingConfig optimizingConfig = OptimizingConfig.parse(Collections.emptyMap()); | ||
optimizingConfig.setTargetSize(targetFileSizeBytes); | ||
optimizingConfig.setFullTriggerInterval(TRIGGER_INTERVAL); | ||
optimizingConfig.setMinorLeastInterval(TRIGGER_INTERVAL); | ||
|
||
TableConfiguration tableConfig = new TableConfiguration(); | ||
tableConfig.setOptimizingConfig(optimizingConfig); | ||
|
||
TableRuntimeMeta tableRuntimeMeta = new TableRuntimeMeta(); | ||
tableRuntimeMeta.setTableName(icebergTable.name()); | ||
tableRuntimeMeta.setFormat(TableFormat.ICEBERG); | ||
tableRuntimeMeta.setLastFullOptimizingTime(LAST_OPTIMIZE_TIME); | ||
tableRuntimeMeta.setLastMinorOptimizingTime(LAST_OPTIMIZE_TIME); | ||
tableRuntimeMeta.setTableConfig(tableConfig); | ||
|
||
return new HiveTableRuntime(tableRuntimeMeta); | ||
} | ||
|
||
private static CommonPartitionEvaluator createCommonPartitionEvaluator(Table table, String partitionPath, | ||
HiveConf conf) { | ||
TableRuntime tableRuntime = createTableRuntime(table, conf); | ||
|
||
TableFileScanHelper tableFileScanHelper = new IcebergTableFileScanHelper(table, | ||
table.currentSnapshot().snapshotId()); | ||
CommonPartitionEvaluator evaluator = null; | ||
try (CloseableIterable<TableFileScanHelper.FileScanResult> results = | ||
tableFileScanHelper.scan()) { | ||
for (TableFileScanHelper.FileScanResult fileScanResult : results) { | ||
DataFile file = fileScanResult.file(); | ||
if (IcebergCompactionUtil.shouldIncludeForCompaction(table, partitionPath, file)) { | ||
PartitionSpec partitionSpec = table.specs().get(file.specId()); | ||
Pair<Integer, StructLike> partition = Pair.of(partitionSpec.specId(), fileScanResult.file().partition()); | ||
|
||
if (evaluator == null) { | ||
evaluator = new CommonPartitionEvaluator(tableRuntime, partition, System.currentTimeMillis()); | ||
} | ||
|
||
evaluator.addFile(fileScanResult.file(), fileScanResult.deleteFiles()); | ||
} | ||
} | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
return evaluator; | ||
} | ||
} |
Oops, something went wrong.