wwj6591812 commented on code in PR #3287: URL: https://github.com/apache/paimon/pull/3287#discussion_r1598408822
########## paimon-core/src/main/java/org/apache/paimon/operation/PickFilesUtil.java: ########## @@ -0,0 +1,184 @@ +/* + * 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.paimon.operation; + +import org.apache.paimon.Snapshot; +import org.apache.paimon.fs.Path; +import org.apache.paimon.index.IndexFileHandler; +import org.apache.paimon.manifest.IndexManifestEntry; +import org.apache.paimon.manifest.ManifestFileMeta; +import org.apache.paimon.manifest.ManifestList; +import org.apache.paimon.schema.SchemaManager; +import org.apache.paimon.utils.FileStorePathFactory; +import org.apache.paimon.utils.SnapshotManager; + +import javax.annotation.Nullable; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +/** PickFilesUtil. */ +public class PickFilesUtil { + + private static final int READ_FILE_RETRY_NUM = 3; + private static final int READ_FILE_RETRY_INTERVAL = 5; + + public static List<Path> getUsedFilesForSnapshot( + Snapshot snapshot, + SnapshotManager snapshotManager, + FileStorePathFactory pathFactory, + FileStoreScan scan, + ManifestList manifestList, + SchemaManager schemaManager, + IndexFileHandler indexFileHandler) { + List<Path> files = new ArrayList<>(); + if (snapshot != null) { + files.add(snapshotManager.snapshotPath(snapshot.id())); + files.addAll( + getUsedFilesInternal( + snapshot, pathFactory, scan, manifestList, indexFileHandler)); + } + for (long id : schemaManager.listAllIds()) { + files.add(schemaManager.toSchemaPath(id)); + } + return files; + } + + private static List<Path> getUsedFilesInternal( + Snapshot snapshot, + FileStorePathFactory pathFactory, + FileStoreScan scan, + ManifestList manifestList, + IndexFileHandler indexFileHandler) { + List<Path> files = new ArrayList<>(); + addManifestList(files, snapshot, pathFactory); + + try { + // try to read manifests + List<ManifestFileMeta> manifestFileMetas = + retryReadingFiles( + () -> readAllManifestsWithIOException(snapshot, manifestList)); + if (manifestFileMetas == null) { + return Collections.emptyList(); + } + List<String> manifestFileName = + manifestFileMetas.stream() + .map(ManifestFileMeta::fileName) + .collect(Collectors.toList()); + files.addAll( + manifestFileName.stream() + .map(pathFactory::toManifestFilePath) + .collect(Collectors.toList())); + + // try to read data files + List<Path> dataFiles = + scan.withSnapshot(snapshot).readSimpleEntries().stream() + .map( + f -> + pathFactory + .createDataFilePathFactory( + f.partition(), f.bucket()) + .toPath(f.fileName())) + .collect(Collectors.toList()); + Collections.reverse(dataFiles); + files.addAll(dataFiles); + + // try to read index files + String indexManifest = snapshot.indexManifest(); + if (indexManifest != null && indexFileHandler.existsManifest(indexManifest)) { + files.add(pathFactory.indexManifestFileFactory().toPath(indexManifest)); + + List<IndexManifestEntry> indexManifestEntries = + retryReadingFiles( + () -> indexFileHandler.readManifestWithIOException(indexManifest)); + if (indexManifestEntries == null) { + return Collections.emptyList(); + } + + indexManifestEntries.stream() + .map(IndexManifestEntry::indexFile) + .map(indexFileHandler::filePath) + .forEach(files::add); + } + + // add statistic file + if (snapshot.statistics() != null) { + files.add(pathFactory.statsFileFactory().toPath(snapshot.statistics())); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + + return files; + } + + private static void addManifestList( + List<Path> used, Snapshot snapshot, FileStorePathFactory pathFactory) { + used.add(pathFactory.toManifestListPath(snapshot.baseManifestList())); + used.add(pathFactory.toManifestListPath(snapshot.deltaManifestList())); + String changelogManifestList = snapshot.changelogManifestList(); + if (changelogManifestList != null) { + used.add(pathFactory.toManifestListPath(changelogManifestList)); + } + } + + private static List<ManifestFileMeta> readAllManifestsWithIOException( + Snapshot snapshot, ManifestList manifestList) throws IOException { + List<ManifestFileMeta> result = new ArrayList<>(); + + result.addAll(manifestList.readWithIOException(snapshot.baseManifestList())); + result.addAll(manifestList.readWithIOException(snapshot.deltaManifestList())); + + String changelogManifestList = snapshot.changelogManifestList(); + if (changelogManifestList != null) { + result.addAll(manifestList.readWithIOException(changelogManifestList)); + } + + return result; + } + + @Nullable + private static <T> T retryReadingFiles(OrphanFilesClean.ReaderWithIOException<T> reader) Review Comment: done ########## paimon-core/src/main/java/org/apache/paimon/clone/CopyFileUtils.java: ########## @@ -0,0 +1,61 @@ +/* + * 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.paimon.clone; + +import org.apache.paimon.fs.FileIO; +import org.apache.paimon.fs.Path; +import org.apache.paimon.utils.IOUtils; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; + +/** Utility class for copy file. */ +public class CopyFileUtils { + + private static final Logger LOG = LoggerFactory.getLogger(CopyFileUtils.class); + + public static void copyFile( Review Comment: done -- 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: issues-unsubscr...@paimon.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org