leaves12138 commented on code in PR #1072: URL: https://github.com/apache/incubator-paimon/pull/1072#discussion_r1185820003
########## paimon-core/src/main/java/org/apache/paimon/utils/ParallellyExecuteUtils.java: ########## @@ -0,0 +1,116 @@ +/* + * 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.utils; + +import java.util.ArrayDeque; +import java.util.Iterator; +import java.util.List; +import java.util.Queue; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.function.Function; + +/** + * This class is a parallel execution util class, which mainly aim to process tasks parallelly with + * memory control. + */ +public class ParallellyExecuteUtils { + + // reduce memory usage by batch iterable process, the cached result in memory will be 2 * + // queueSize + public static <T, U> Iterable<T> parallelismBatchIterable( + Function<List<U>, List<T>> processor, List<U> input, int queueSize) { + if (queueSize <= 0) { + throw new NegativeArraySizeException("queueSize need to be positive"); + } + + Queue<List<U>> stack = new ArrayDeque<>(); + int size = input.size(); + int num = size / queueSize; + + for (int i = 0; i < num; i++) { + stack.add(input.subList(i * queueSize, (i + 1) * queueSize)); + } + + if (num * queueSize < size) { + stack.add(input.subList(num * queueSize, size)); + } + + return () -> + new Iterator<T>() { + CompletableFuture<List<T>> batch = null; + List<T> activeList = null; + private int index = 0; + private T next = null; + + // build the first batch + { + if (stack.size() > 0) { + batch = + CompletableFuture.supplyAsync( + () -> processor.apply(stack.poll()), + FileUtils.COMMON_IO_FORK_JOIN_POOL); + } + } + + @Override + public boolean hasNext() { + try { + // get active list for polling, and submit tasks meanwhile + if (activeList == null) { + if (batch == null) { + return false; + } + + activeList = batch.get(); + if (stack.size() > 0) { + batch = + CompletableFuture.supplyAsync( Review Comment: done this -- 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]
