stevenzwu commented on code in PR #11086: URL: https://github.com/apache/iceberg/pull/11086#discussion_r1747731139
########## core/src/test/java/org/apache/iceberg/TestSnapshotProducer.java: ########## @@ -0,0 +1,68 @@ +/* + * 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; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class TestSnapshotProducer { + @Test + public void testManifestFileGroupSize() { + assertEquals( + 1_000, + SnapshotProducer.manifestFileGroupSize(4 /* parallelism */, 1_000 /* file count */), + "Must return file count when it is small"); + + assertEquals( + 10_000, + SnapshotProducer.manifestFileGroupSize(4 /* parallelism */, 10_000 /* file count */), + "Must return file count when it matches the min group size"); + + assertEquals( + 10_001, + SnapshotProducer.manifestFileGroupSize(4 /* parallelism */, 10_001 /* file count */), + "Must return file count when it is slightly above min group size"); + + assertEquals( + 12_500, + SnapshotProducer.manifestFileGroupSize(4 /* parallelism */, 12_500 /* file count */), + "Must return file count when remainder is < 50% of min group size"); Review Comment: nit: `when remainder is < 50% of min group size` is less clear to me. maybe `when it is less than 1.5x of min group size`? ########## core/src/test/java/org/apache/iceberg/TestSnapshotProducer.java: ########## @@ -0,0 +1,68 @@ +/* + * 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; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class TestSnapshotProducer { + @Test + public void testManifestFileGroupSize() { + assertEquals( + 1_000, + SnapshotProducer.manifestFileGroupSize(4 /* parallelism */, 1_000 /* file count */), + "Must return file count when it is small"); + + assertEquals( + 10_000, + SnapshotProducer.manifestFileGroupSize(4 /* parallelism */, 10_000 /* file count */), + "Must return file count when it matches the min group size"); + + assertEquals( + 10_001, + SnapshotProducer.manifestFileGroupSize(4 /* parallelism */, 10_001 /* file count */), + "Must return file count when it is slightly above min group size"); + + assertEquals( + 12_500, + SnapshotProducer.manifestFileGroupSize(4 /* parallelism */, 12_500 /* file count */), + "Must return file count when remainder is < 50% of min group size"); + + assertEquals( + 7_500, + SnapshotProducer.manifestFileGroupSize(4 /* parallelism */, 15_000 /* file count */), + "Must split into 2 groups when remainder is >= 50% of min group size"); Review Comment: same as above ########## core/src/main/java/org/apache/iceberg/SnapshotProducer.java: ########## @@ -554,6 +562,84 @@ protected boolean cleanupAfterCommit() { return true; } + protected List<ManifestFile> writeDataManifests(List<DataFile> files, PartitionSpec spec) { + return writeDataManifests(files, null /* inherit data seq */, spec); + } + + protected List<ManifestFile> writeDataManifests( + List<DataFile> files, Long dataSeq, PartitionSpec spec) { + return writeManifests(files, group -> writeDataFileGroup(group, dataSeq, spec)); + } + + private List<ManifestFile> writeDataFileGroup( + List<DataFile> files, Long dataSeq, PartitionSpec spec) { + RollingManifestWriter<DataFile> writer = newRollingManifestWriter(spec); + + try (RollingManifestWriter<DataFile> closableWriter = writer) { + if (dataSeq != null) { + files.forEach(file -> closableWriter.add(file, dataSeq)); + } else { + files.forEach(closableWriter::add); + } + } catch (IOException e) { + throw new RuntimeIOException(e, "Failed to write data manifests"); + } + + return writer.toManifestFiles(); + } + + protected List<ManifestFile> writeDeleteManifests( + List<DeleteFileHolder> files, PartitionSpec spec) { + return writeManifests(files, group -> writeDeleteFileGroup(group, spec)); + } + + private List<ManifestFile> writeDeleteFileGroup( + List<DeleteFileHolder> files, PartitionSpec spec) { + RollingManifestWriter<DeleteFile> writer = newRollingDeleteManifestWriter(spec); + + try (RollingManifestWriter<DeleteFile> closableWriter = writer) { + for (DeleteFileHolder file : files) { + if (file.dataSequenceNumber() != null) { + closableWriter.add(file.deleteFile(), file.dataSequenceNumber()); + } else { + closableWriter.add(file.deleteFile()); + } + } + } catch (IOException e) { + throw new RuntimeIOException(e, "Failed to write delete manifests"); + } + + return writer.toManifestFiles(); + } + + private static <F> List<ManifestFile> writeManifests( + List<F> files, Function<List<F>, List<ManifestFile>> writeFunc) { + int groupSize = manifestFileGroupSize(ThreadPools.WORKER_THREAD_POOL_SIZE, files.size()); + List<List<F>> groups = Lists.partition(files, groupSize); + Queue<ManifestFile> manifests = Queues.newConcurrentLinkedQueue(); + Tasks.foreach(groups) Review Comment: nit: maybe the subject of this PR could be more specific like `Core: Parallelize manifest writing for many new files` ########## core/src/test/java/org/apache/iceberg/TestSnapshotProducer.java: ########## @@ -0,0 +1,68 @@ +/* + * 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; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class TestSnapshotProducer { + @Test + public void testManifestFileGroupSize() { + assertEquals( + 1_000, + SnapshotProducer.manifestFileGroupSize(4 /* parallelism */, 1_000 /* file count */), + "Must return file count when it is small"); + + assertEquals( + 10_000, + SnapshotProducer.manifestFileGroupSize(4 /* parallelism */, 10_000 /* file count */), + "Must return file count when it matches the min group size"); + + assertEquals( + 10_001, + SnapshotProducer.manifestFileGroupSize(4 /* parallelism */, 10_001 /* file count */), + "Must return file count when it is slightly above min group size"); + + assertEquals( + 12_500, + SnapshotProducer.manifestFileGroupSize(4 /* parallelism */, 12_500 /* file count */), + "Must return file count when remainder is < 50% of min group size"); + + assertEquals( + 7_500, + SnapshotProducer.manifestFileGroupSize(4 /* parallelism */, 15_000 /* file count */), + "Must split into 2 groups when remainder is >= 50% of min group size"); + + assertEquals( + 16_667, + SnapshotProducer.manifestFileGroupSize(3 /* parallelism */, 50_000 /* file count */), + "Must split into 3 groups when file count is large"); Review Comment: this test is essentially the same as the one below. note that min group size is 10_000. ########## core/src/main/java/org/apache/iceberg/SnapshotProducer.java: ########## @@ -554,6 +562,84 @@ protected boolean cleanupAfterCommit() { return true; } + protected List<ManifestFile> writeDataManifests(List<DataFile> files, PartitionSpec spec) { + return writeDataManifests(files, null /* inherit data seq */, spec); + } + + protected List<ManifestFile> writeDataManifests( + List<DataFile> files, Long dataSeq, PartitionSpec spec) { + return writeManifests(files, group -> writeDataFileGroup(group, dataSeq, spec)); + } + + private List<ManifestFile> writeDataFileGroup( + List<DataFile> files, Long dataSeq, PartitionSpec spec) { + RollingManifestWriter<DataFile> writer = newRollingManifestWriter(spec); + + try (RollingManifestWriter<DataFile> closableWriter = writer) { + if (dataSeq != null) { + files.forEach(file -> closableWriter.add(file, dataSeq)); + } else { + files.forEach(closableWriter::add); + } + } catch (IOException e) { + throw new RuntimeIOException(e, "Failed to write data manifests"); + } + + return writer.toManifestFiles(); + } + + protected List<ManifestFile> writeDeleteManifests( + List<DeleteFileHolder> files, PartitionSpec spec) { + return writeManifests(files, group -> writeDeleteFileGroup(group, spec)); + } + + private List<ManifestFile> writeDeleteFileGroup( + List<DeleteFileHolder> files, PartitionSpec spec) { + RollingManifestWriter<DeleteFile> writer = newRollingDeleteManifestWriter(spec); + + try (RollingManifestWriter<DeleteFile> closableWriter = writer) { + for (DeleteFileHolder file : files) { + if (file.dataSequenceNumber() != null) { + closableWriter.add(file.deleteFile(), file.dataSequenceNumber()); + } else { + closableWriter.add(file.deleteFile()); + } + } + } catch (IOException e) { + throw new RuntimeIOException(e, "Failed to write delete manifests"); + } + + return writer.toManifestFiles(); + } + + private static <F> List<ManifestFile> writeManifests( + List<F> files, Function<List<F>, List<ManifestFile>> writeFunc) { + int groupSize = manifestFileGroupSize(ThreadPools.WORKER_THREAD_POOL_SIZE, files.size()); + List<List<F>> groups = Lists.partition(files, groupSize); + Queue<ManifestFile> manifests = Queues.newConcurrentLinkedQueue(); + Tasks.foreach(groups) + .stopOnFailure() + .throwFailureWhenFinished() + .executeWith(ThreadPools.getWorkerPool()) + .run(group -> manifests.addAll(writeFunc.apply(group))); + return ImmutableList.copyOf(manifests); + } + + /** + * Calculates how many files can be processed concurrently depending on the provided parallelism + * without creating too small manifests. + * + * @param parallelism the max number of concurrent writers + * @param fileCount the total number of files to be written + * @return the size of each file group, adjusted to balance workload across available writers + */ + @VisibleForTesting + static int manifestFileGroupSize(int parallelism, int fileCount) { + int maxParallelism = IntMath.divide(fileCount, MIN_FILE_GROUP_SIZE, RoundingMode.HALF_UP); + int actualParallelism = Math.max(1, Math.min(parallelism, maxParallelism)); Review Comment: nit: is `Math.max(1, ...)` redundant? should we have assertion that `parallelism` and `fileCount` are `> 0` ########## core/src/test/java/org/apache/iceberg/TestFastAppend.java: ########## @@ -42,6 +42,25 @@ protected static List<Object> parameters() { return Arrays.asList(1, 2, 3); } + @TestTemplate + public void testAddManyFiles() { + assertThat(listManifestFiles()).as("Table should start empty").isEmpty(); + + List<DataFile> dataFiles = Lists.newArrayList(); + + for (int ordinal = 0; ordinal < 20_000; ordinal++) { Review Comment: nit: change `20_000` to `MIN_FILE_GROUP_SIZE * 2`. applicable to other tests. -- 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...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org