zhangbutao commented on code in PR #4897:
URL: https://github.com/apache/hive/pull/4897#discussion_r1405347851
##########
ql/src/java/org/apache/hadoop/hive/ql/parse/AlterTableExecuteSpec.java:
##########
@@ -270,4 +271,27 @@ public SearchArgument getSarg() {
return sarg;
}
}
+
+ /**
+ * Value object class, that stores the delete orphan files operation
specific parameters.
+ * <ul>
+ * <li>timestampMillis: the time before which files should be considered
to be deleted</li>
+ * </ul>
+ */
+ public static class DeleteOrphanFilesDesc {
+ private final long timestampMillis;
+
+ public DeleteOrphanFilesDesc(long timestampMillis) {
+ this.timestampMillis = timestampMillis;
Review Comment:
What will happen if timestampMillis is < 0?
##########
iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergDeleteOrphanFiles.java:
##########
@@ -0,0 +1,232 @@
+/*
+ * 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;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.net.URI;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.function.Consumer;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.LocatedFileStatus;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.fs.RemoteIterator;
+import org.apache.iceberg.DataTask;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.HasTableOperations;
+import org.apache.iceberg.ManifestFile;
+import org.apache.iceberg.MetadataTableUtils;
+import org.apache.iceberg.ReachableFileUtil;
+import org.apache.iceberg.Snapshot;
+import org.apache.iceberg.StructLike;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.TableScan;
+import org.apache.iceberg.actions.DeleteOrphanFiles;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.relocated.com.google.common.collect.Iterables;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
+import
org.apache.iceberg.relocated.com.google.common.util.concurrent.MoreExecutors;
+import org.apache.iceberg.util.Tasks;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.apache.iceberg.MetadataTableType.ALL_ENTRIES;
+
+public class HiveIcebergDeleteOrphanFiles implements DeleteOrphanFiles {
+
+ public static final String METADATA_FOLDER_NAME = "metadata";
+ public static final String DATA_FOLDER_NAME = "data";
+ private static final Logger LOG =
LoggerFactory.getLogger(HiveIcebergDeleteOrphanFiles.class);
+ private String tableLocation = null;
+ private long olderThanTimestamp = System.currentTimeMillis() -
TimeUnit.DAYS.toMillis(3);
+ private Consumer<String> deleteFunc;
+ private ExecutorService deleteExecutorService =
MoreExecutors.newDirectExecutorService();
+
+ private final Configuration conf;
+ private final Table table;
+
+ public HiveIcebergDeleteOrphanFiles(Configuration conf, Table table) {
+ this.conf = conf;
+ this.table = table;
+ this.deleteFunc = file -> table.io().deleteFile(file);
+ this.tableLocation = table.location();
+ }
+
+ @Override
+ public HiveIcebergDeleteOrphanFiles location(String location) {
+ this.tableLocation = location;
+ return this;
+ }
+
+ @Override
+ public HiveIcebergDeleteOrphanFiles olderThan(long newOlderThanTimestamp) {
+ this.olderThanTimestamp = newOlderThanTimestamp;
+ return this;
+ }
+
+ // TODO: Implement later, if there is any use case.
+ @Override
+ public HiveIcebergDeleteOrphanFiles deleteWith(Consumer<String>
newDeleteFunc) {
+ this.deleteFunc = newDeleteFunc;
+ return this;
+ }
+
+ @Override
+ public HiveIcebergDeleteOrphanFiles executeDeleteWith(ExecutorService
executorService) {
+ this.deleteExecutorService = executorService;
+ return this;
+ }
+
+ @Override
+ public Result execute() {
+ LOG.info("Cleaning orphan files for {}", table.name());
+ HiveIcebergDeleteOrphanFilesResult result = new
HiveIcebergDeleteOrphanFilesResult();
+ result.addDeletedFiles(cleanContentFiles(olderThanTimestamp));
+ result.addDeletedFiles(cleanMetadata(olderThanTimestamp));
+
+ LOG.debug("Deleting {} files while cleaning orphan files for {}",
result.deletedFiles, table.name());
+
Tasks.foreach(result.deletedFiles).executeWith(deleteExecutorService).retry(3)
+
.stopRetryOn(FileNotFoundException.class).suppressFailureWhenFinished().onFailure((file,
thrown) ->
+ LOG.warn("Delete failed for file: {}", file,
thrown)).run(deleteFunc::accept);
+ return result;
+ }
+
+ private Set<String> cleanContentFiles(long lastTime) {
+ Set<String> validFiles = Sets.union(getAllContentFilePath(),
getAllStatisticsFilePath(table));
+ LOG.debug("Valid content file for {} are {}", table.name(), validFiles);
+ try {
+ Path dataPath = new Path(tableLocation, DATA_FOLDER_NAME);
+ return getFilesToBeDeleted(lastTime, validFiles, dataPath);
+ } catch (IOException ex) {
+ throw new UncheckedIOException(ex);
+ }
+ }
+
+ public Set<String> getAllContentFilePath() {
+ Set<String> validFilesPath = Sets.newHashSet();
+ Table metadatTable = getMetadataTable();
+
+ TableScan tableScan = metadatTable.newScan();
+ CloseableIterable<FileScanTask> manifestFileScanTasks =
tableScan.planFiles();
+ CloseableIterable<StructLike> entries =
CloseableIterable.concat(entriesOfManifest(manifestFileScanTasks));
+
+ for (StructLike entry : entries) {
+ StructLike fileRecord = entry.get(4, StructLike.class);
+ String filePath = fileRecord.get(1, String.class);
+ validFilesPath.add(getUriPath(filePath));
+ }
+ return validFilesPath;
+ }
+
+ private Iterable<CloseableIterable<StructLike>> entriesOfManifest(
+ CloseableIterable<FileScanTask> fileScanTasks) {
+ return Iterables.transform(
+ fileScanTasks,
+ task -> {
+ assert task != null;
+ return ((DataTask) task).rows();
+ });
+ }
+
+ public static Set<String> getAllStatisticsFilePath(Table table) {
+ return
ReachableFileUtil.statisticsFilesLocations(table).stream().map(HiveIcebergDeleteOrphanFiles::getUriPath)
+ .collect(Collectors.toSet());
+ }
+
+ protected Set<String> cleanMetadata(long lastTime) {
+ LOG.info("{} start clean metadata files", table.name());
+ try {
+ Set<String> validFiles = getValidMetadataFiles(table);
+ LOG.debug("Valid metadata files for {} are {}", table.name(),
validFiles);
+ Path metadataLocation = new Path(tableLocation, METADATA_FOLDER_NAME);
+ return getFilesToBeDeleted(lastTime, validFiles, metadataLocation);
+ } catch (IOException ioe) {
+ throw new UncheckedIOException(ioe);
+ }
+ }
+
+ private Set<String> getFilesToBeDeleted(long lastTime, Set<String>
validFiles, Path location)
+ throws IOException {
+ Set<String> filesToDelete = Sets.newHashSet();
+ FileSystem fs = location.getFileSystem(conf);
+ RemoteIterator<LocatedFileStatus> metadataLocations =
fs.listFiles(location, true);
+ while (metadataLocations.hasNext()) {
+ LocatedFileStatus metadataFile = metadataLocations.next();
+ if (metadataFile.getModificationTime() < lastTime &&
!validFiles.contains(
+ getUriPath(metadataFile.getPath().toString()))) {
+ filesToDelete.add(metadataFile.getPath().toString());
+ }
+ }
+ return filesToDelete;
+ }
+
+ private Table getMetadataTable() {
+ return
MetadataTableUtils.createMetadataTableInstance(((HasTableOperations)
table).operations(), table.name(),
+ table.name() + "#" + ALL_ENTRIES.name(), ALL_ENTRIES);
+ }
+
+ private static Set<String> getValidMetadataFiles(Table internalTable) {
Review Comment:
What's the meaning of `internalTable`?
##########
iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergDeleteOrphanFiles.java:
##########
@@ -0,0 +1,232 @@
+/*
+ * 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;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.net.URI;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.function.Consumer;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.LocatedFileStatus;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.fs.RemoteIterator;
+import org.apache.iceberg.DataTask;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.HasTableOperations;
+import org.apache.iceberg.ManifestFile;
+import org.apache.iceberg.MetadataTableUtils;
+import org.apache.iceberg.ReachableFileUtil;
+import org.apache.iceberg.Snapshot;
+import org.apache.iceberg.StructLike;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.TableScan;
+import org.apache.iceberg.actions.DeleteOrphanFiles;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.relocated.com.google.common.collect.Iterables;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
+import
org.apache.iceberg.relocated.com.google.common.util.concurrent.MoreExecutors;
+import org.apache.iceberg.util.Tasks;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.apache.iceberg.MetadataTableType.ALL_ENTRIES;
+
+public class HiveIcebergDeleteOrphanFiles implements DeleteOrphanFiles {
Review Comment:
Should we use a new package for these iceberg action? such as
`org.apache.iceberg.mr.hive.actions`
Then we can add more actions in this package later.
##########
iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergDeleteOrphanFiles.java:
##########
@@ -0,0 +1,232 @@
+/*
+ * 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;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.net.URI;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.function.Consumer;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.LocatedFileStatus;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.fs.RemoteIterator;
+import org.apache.iceberg.DataTask;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.HasTableOperations;
+import org.apache.iceberg.ManifestFile;
+import org.apache.iceberg.MetadataTableUtils;
+import org.apache.iceberg.ReachableFileUtil;
+import org.apache.iceberg.Snapshot;
+import org.apache.iceberg.StructLike;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.TableScan;
+import org.apache.iceberg.actions.DeleteOrphanFiles;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.relocated.com.google.common.collect.Iterables;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
+import
org.apache.iceberg.relocated.com.google.common.util.concurrent.MoreExecutors;
+import org.apache.iceberg.util.Tasks;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.apache.iceberg.MetadataTableType.ALL_ENTRIES;
+
+public class HiveIcebergDeleteOrphanFiles implements DeleteOrphanFiles {
+
+ public static final String METADATA_FOLDER_NAME = "metadata";
+ public static final String DATA_FOLDER_NAME = "data";
+ private static final Logger LOG =
LoggerFactory.getLogger(HiveIcebergDeleteOrphanFiles.class);
+ private String tableLocation = null;
+ private long olderThanTimestamp = System.currentTimeMillis() -
TimeUnit.DAYS.toMillis(3);
+ private Consumer<String> deleteFunc;
+ private ExecutorService deleteExecutorService =
MoreExecutors.newDirectExecutorService();
+
+ private final Configuration conf;
+ private final Table table;
+
+ public HiveIcebergDeleteOrphanFiles(Configuration conf, Table table) {
+ this.conf = conf;
+ this.table = table;
+ this.deleteFunc = file -> table.io().deleteFile(file);
+ this.tableLocation = table.location();
+ }
+
+ @Override
+ public HiveIcebergDeleteOrphanFiles location(String location) {
+ this.tableLocation = location;
+ return this;
+ }
+
+ @Override
+ public HiveIcebergDeleteOrphanFiles olderThan(long newOlderThanTimestamp) {
+ this.olderThanTimestamp = newOlderThanTimestamp;
+ return this;
+ }
+
+ // TODO: Implement later, if there is any use case.
+ @Override
+ public HiveIcebergDeleteOrphanFiles deleteWith(Consumer<String>
newDeleteFunc) {
+ this.deleteFunc = newDeleteFunc;
+ return this;
+ }
+
+ @Override
+ public HiveIcebergDeleteOrphanFiles executeDeleteWith(ExecutorService
executorService) {
+ this.deleteExecutorService = executorService;
+ return this;
+ }
+
+ @Override
+ public Result execute() {
+ LOG.info("Cleaning orphan files for {}", table.name());
+ HiveIcebergDeleteOrphanFilesResult result = new
HiveIcebergDeleteOrphanFilesResult();
+ result.addDeletedFiles(cleanContentFiles(olderThanTimestamp));
+ result.addDeletedFiles(cleanMetadata(olderThanTimestamp));
+
+ LOG.debug("Deleting {} files while cleaning orphan files for {}",
result.deletedFiles, table.name());
+
Tasks.foreach(result.deletedFiles).executeWith(deleteExecutorService).retry(3)
+
.stopRetryOn(FileNotFoundException.class).suppressFailureWhenFinished().onFailure((file,
thrown) ->
+ LOG.warn("Delete failed for file: {}", file,
thrown)).run(deleteFunc::accept);
+ return result;
+ }
+
+ private Set<String> cleanContentFiles(long lastTime) {
+ Set<String> validFiles = Sets.union(getAllContentFilePath(),
getAllStatisticsFilePath(table));
+ LOG.debug("Valid content file for {} are {}", table.name(), validFiles);
Review Comment:
`validFiles` maybe a very long character if table has many files. Maybe we
can use its size instead?
##########
iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergDeleteOrphanFiles.java:
##########
@@ -0,0 +1,232 @@
+/*
+ * 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;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.net.URI;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.function.Consumer;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.LocatedFileStatus;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.fs.RemoteIterator;
+import org.apache.iceberg.DataTask;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.HasTableOperations;
+import org.apache.iceberg.ManifestFile;
+import org.apache.iceberg.MetadataTableUtils;
+import org.apache.iceberg.ReachableFileUtil;
+import org.apache.iceberg.Snapshot;
+import org.apache.iceberg.StructLike;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.TableScan;
+import org.apache.iceberg.actions.DeleteOrphanFiles;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.relocated.com.google.common.collect.Iterables;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
+import
org.apache.iceberg.relocated.com.google.common.util.concurrent.MoreExecutors;
+import org.apache.iceberg.util.Tasks;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.apache.iceberg.MetadataTableType.ALL_ENTRIES;
+
+public class HiveIcebergDeleteOrphanFiles implements DeleteOrphanFiles {
+
+ public static final String METADATA_FOLDER_NAME = "metadata";
+ public static final String DATA_FOLDER_NAME = "data";
+ private static final Logger LOG =
LoggerFactory.getLogger(HiveIcebergDeleteOrphanFiles.class);
+ private String tableLocation = null;
+ private long olderThanTimestamp = System.currentTimeMillis() -
TimeUnit.DAYS.toMillis(3);
+ private Consumer<String> deleteFunc;
+ private ExecutorService deleteExecutorService =
MoreExecutors.newDirectExecutorService();
+
+ private final Configuration conf;
+ private final Table table;
+
+ public HiveIcebergDeleteOrphanFiles(Configuration conf, Table table) {
+ this.conf = conf;
+ this.table = table;
+ this.deleteFunc = file -> table.io().deleteFile(file);
+ this.tableLocation = table.location();
+ }
+
+ @Override
+ public HiveIcebergDeleteOrphanFiles location(String location) {
+ this.tableLocation = location;
+ return this;
+ }
+
+ @Override
+ public HiveIcebergDeleteOrphanFiles olderThan(long newOlderThanTimestamp) {
+ this.olderThanTimestamp = newOlderThanTimestamp;
+ return this;
+ }
+
+ // TODO: Implement later, if there is any use case.
+ @Override
+ public HiveIcebergDeleteOrphanFiles deleteWith(Consumer<String>
newDeleteFunc) {
+ this.deleteFunc = newDeleteFunc;
+ return this;
+ }
+
+ @Override
+ public HiveIcebergDeleteOrphanFiles executeDeleteWith(ExecutorService
executorService) {
+ this.deleteExecutorService = executorService;
+ return this;
+ }
+
+ @Override
+ public Result execute() {
+ LOG.info("Cleaning orphan files for {}", table.name());
+ HiveIcebergDeleteOrphanFilesResult result = new
HiveIcebergDeleteOrphanFilesResult();
+ result.addDeletedFiles(cleanContentFiles(olderThanTimestamp));
+ result.addDeletedFiles(cleanMetadata(olderThanTimestamp));
+
+ LOG.debug("Deleting {} files while cleaning orphan files for {}",
result.deletedFiles, table.name());
Review Comment:
`result.deletedFiles` maybe a very long character if table has many orphan
files. Maybe we can use its size instead?
--
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]