gaoyunhaii commented on a change in pull request #15199: URL: https://github.com/apache/flink/pull/15199#discussion_r597604195
########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/disk/BatchShuffleReadBufferPool.java ########## @@ -0,0 +1,353 @@ +/* + * 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.flink.runtime.io.disk; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.configuration.TaskManagerOptions; +import org.apache.flink.core.memory.MemorySegment; +import org.apache.flink.core.memory.MemorySegmentFactory; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.concurrent.GuardedBy; + +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Queue; +import java.util.concurrent.TimeoutException; + +import static org.apache.flink.util.Preconditions.checkArgument; +import static org.apache.flink.util.Preconditions.checkNotNull; +import static org.apache.flink.util.Preconditions.checkState; + +/** + * A fixed-size {@link MemorySegment} pool used by batch shuffle for shuffle data read (currently + * only used by sort-merge blocking shuffle). + */ +@Internal +public class BatchShuffleReadBufferPool { + + private static final Logger LOG = LoggerFactory.getLogger(BatchShuffleReadBufferPool.class); + + /** Minimum total memory size in bytes of this buffer pool. */ + public static final int MIN_TOTAL_BYTES = 32 * 1024 * 1024; + + /** + * Memory size in bytes can be allocated from this buffer pool for a single request (8M is for + * better sequential read). + */ + public static final int NUM_BYTES_PER_REQUEST = 8 * 1024 * 1024; + + /** Total direct memory size in bytes can can be allocated and used by this buffer pool. */ + private final long totalBytes; + + /** + * Maximum time to wait in milliseconds when requesting read buffers from this buffer pool + * before throwing an exception. + */ + private final long requestTimeout; + + /** The number of total buffers in this buffer pool. */ + private final int numTotalBuffers; + + /** Size of each buffer in bytes in this buffer pool. */ + private final int bufferSize; + + /** The number of buffers to be returned for a single request. */ + private final int numBuffersPerRequest; + + /** + * The maximum number of buffers can be allocated from this buffer pool for a single buffer + * requester. + */ + private final int maxBuffersPerRequester; + + /** All available buffers in this buffer pool currently. */ + @GuardedBy("buffers") + private final Queue<MemorySegment> buffers = new ArrayDeque<>(); + + /** Account for all the buffers requested per requester. */ + @GuardedBy("buffers") + private final Map<Object, Counter> numBuffersAllocated = new HashMap<>(); + + /** Whether this buffer pool has been destroyed or not. */ + @GuardedBy("buffers") + private boolean destroyed; + + /** Whether this buffer pool has been initialized or not. */ + @GuardedBy("buffers") + private boolean initialized; + + public BatchShuffleReadBufferPool(long totalBytes, int bufferSize) { + // 5 min default buffer request timeout + this(totalBytes, bufferSize, 5 * 60 * 1000); + } + + public BatchShuffleReadBufferPool(long totalBytes, int bufferSize, long requestTimeout) { + checkArgument(totalBytes > 0, "Total memory size must be positive."); + checkArgument(bufferSize > 0, "Size of buffer must be positive."); + checkArgument(requestTimeout > 0, "Request timeout must be positive."); + + this.totalBytes = totalBytes; + this.bufferSize = bufferSize; + this.requestTimeout = requestTimeout; + + this.numTotalBuffers = (int) Math.min(totalBytes / bufferSize, Integer.MAX_VALUE); Review comment: Do we need to check whether totalBytes could be divided by bufferSize without remainder ? ########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/disk/BatchShuffleReadIOExecutor.java ########## @@ -0,0 +1,127 @@ +/* + * 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.flink.runtime.io.disk; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.runtime.util.ExecutorThreadFactory; +import org.apache.flink.runtime.util.Hardware; + +import javax.annotation.Nonnull; +import javax.annotation.concurrent.GuardedBy; + +import java.util.concurrent.Executor; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ThreadFactory; + +import static org.apache.flink.util.Preconditions.checkArgument; +import static org.apache.flink.util.Preconditions.checkState; + +/** + * A fixed-size {@link Executor} pool used by batch shuffle for shuffle data read (currently only + * used by sort-merge blocking shuffle. + */ +@Internal +public class BatchShuffleReadIOExecutor implements Executor { Review comment: +1 for this one, it seems `BatchShuffleReadIOExecutor` is a direct wrapper of the fixed thread pool ? ########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/disk/BatchShuffleReadBufferPool.java ########## @@ -0,0 +1,353 @@ +/* + * 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.flink.runtime.io.disk; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.configuration.TaskManagerOptions; +import org.apache.flink.core.memory.MemorySegment; +import org.apache.flink.core.memory.MemorySegmentFactory; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.concurrent.GuardedBy; + +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Queue; +import java.util.concurrent.TimeoutException; + +import static org.apache.flink.util.Preconditions.checkArgument; +import static org.apache.flink.util.Preconditions.checkNotNull; +import static org.apache.flink.util.Preconditions.checkState; + +/** + * A fixed-size {@link MemorySegment} pool used by batch shuffle for shuffle data read (currently + * only used by sort-merge blocking shuffle). + */ +@Internal +public class BatchShuffleReadBufferPool { + + private static final Logger LOG = LoggerFactory.getLogger(BatchShuffleReadBufferPool.class); + + /** Minimum total memory size in bytes of this buffer pool. */ + public static final int MIN_TOTAL_BYTES = 32 * 1024 * 1024; + + /** + * Memory size in bytes can be allocated from this buffer pool for a single request (8M is for + * better sequential read). + */ + public static final int NUM_BYTES_PER_REQUEST = 8 * 1024 * 1024; + + /** Total direct memory size in bytes can can be allocated and used by this buffer pool. */ + private final long totalBytes; + + /** + * Maximum time to wait in milliseconds when requesting read buffers from this buffer pool + * before throwing an exception. + */ + private final long requestTimeout; + + /** The number of total buffers in this buffer pool. */ + private final int numTotalBuffers; + + /** Size of each buffer in bytes in this buffer pool. */ + private final int bufferSize; + + /** The number of buffers to be returned for a single request. */ + private final int numBuffersPerRequest; + + /** + * The maximum number of buffers can be allocated from this buffer pool for a single buffer + * requester. + */ + private final int maxBuffersPerRequester; + + /** All available buffers in this buffer pool currently. */ + @GuardedBy("buffers") + private final Queue<MemorySegment> buffers = new ArrayDeque<>(); + + /** Account for all the buffers requested per requester. */ + @GuardedBy("buffers") + private final Map<Object, Counter> numBuffersAllocated = new HashMap<>(); Review comment: Why we need use Counter instead of Map<Object, Integer> ? -- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org