JingsongLi commented on code in PR #1256: URL: https://github.com/apache/incubator-paimon/pull/1256#discussion_r1209741514
########## paimon-core/src/main/java/org/apache/paimon/append/AppendOnlyTableCompactionCoordinator.java: ########## @@ -0,0 +1,270 @@ +/* + * 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.append; + +import org.apache.paimon.data.BinaryRow; +import org.apache.paimon.io.DataFileMeta; +import org.apache.paimon.manifest.FileKind; +import org.apache.paimon.manifest.ManifestEntry; +import org.apache.paimon.operation.AppendOnlyFileStoreScan; +import org.apache.paimon.operation.ScanKind; +import org.apache.paimon.table.AppendOnlyFileStoreTable; +import org.apache.paimon.table.sink.CommitMessage; +import org.apache.paimon.table.sink.CommitMessageImpl; +import org.apache.paimon.utils.SnapshotManager; + +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Queue; +import java.util.stream.Collectors; + +/** {@link AppendOnlyFileStoreTable} compact coordinator. */ +public class AppendOnlyTableCompactionCoordinator { + private final SnapshotManager snapshotManager; + private final AppendOnlyFileStoreScan scan; + private Long snapshotId; + + private final long targetFileSize; + private final int minFileNum; + private final int maxFileNum; + + private final Map<BinaryRow, PartitionCompactCoordinator> partitionCompactCoordinators = + new HashMap<>(); + + public AppendOnlyTableCompactionCoordinator( + AppendOnlyFileStoreTable table, long targetFileSize, int minFileNum, int maxFileNum) { + scan = table.store().newScan(); + snapshotManager = table.snapshotManager(); + this.targetFileSize = targetFileSize; + this.minFileNum = minFileNum; + this.maxFileNum = maxFileNum; + snapshotId = snapshotManager.latestSnapshotId(); + + if (snapshotId != null) { + updateFull(); + } + } + + public void updateRestore() { + Long latestSnapshotId = snapshotManager.latestSnapshotId(); + if (latestSnapshotId == null || latestSnapshotId.equals(snapshotId)) { + return; + } + + if (snapshotId == null) { + snapshotId = latestSnapshotId; + updateFull(); + } else { + updateDelta(latestSnapshotId); + snapshotId = latestSnapshotId; + } + } + + private void updateFull() { + scan.withSnapshot(snapshotId).plan().files().stream() + .collect(Collectors.groupingBy(ManifestEntry::partition)) + .forEach( + (k, v) -> + partitionCompactCoordinators + .computeIfAbsent(k, PartitionCompactCoordinator::new) + .addRestoredFiles( + v.stream() + .map(ManifestEntry::file) + .filter( + dataFileMeta -> + dataFileMeta.fileSize() + < targetFileSize) + .collect(Collectors.toList()))); + } + + private void updateDelta(Long targetSnapshotId) { Review Comment: We don't need to `updateDelta`, just update full and store delta files from writers. -- 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]
