wsry commented on a change in pull request #13924:
URL: https://github.com/apache/flink/pull/13924#discussion_r601228076



##########
File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/SortMergeSubpartitionReader.java
##########
@@ -19,154 +19,180 @@
 package org.apache.flink.runtime.io.network.partition;
 
 import org.apache.flink.core.memory.MemorySegment;
-import org.apache.flink.core.memory.MemorySegmentFactory;
 import org.apache.flink.runtime.io.network.buffer.Buffer;
 import org.apache.flink.runtime.io.network.buffer.BufferRecycler;
 import 
org.apache.flink.runtime.io.network.partition.ResultSubpartition.BufferAndBacklog;
-import org.apache.flink.util.ExceptionUtils;
-import org.apache.flink.util.IOUtils;
 
 import javax.annotation.Nullable;
+import javax.annotation.concurrent.GuardedBy;
 
 import java.io.IOException;
 import java.util.ArrayDeque;
 import java.util.Queue;
 
+import static org.apache.flink.util.Preconditions.checkArgument;
 import static org.apache.flink.util.Preconditions.checkNotNull;
-import static org.apache.flink.util.Preconditions.checkState;
 
 /** Subpartition data reader for {@link SortMergeResultPartition}. */
-public class SortMergeSubpartitionReader implements ResultSubpartitionView, 
BufferRecycler {
+public class SortMergeSubpartitionReader
+        implements ResultSubpartitionView, 
Comparable<SortMergeSubpartitionReader> {
 
-    private static final int NUM_READ_BUFFERS = 2;
+    private final Object lock = new Object();
 
     /** Target {@link SortMergeResultPartition} to read data from. */
     private final SortMergeResultPartition partition;
 
     /** Listener to notify when data is available. */
     private final BufferAvailabilityListener availabilityListener;
 
-    /** Unmanaged memory used as read buffers. */
-    private final Queue<MemorySegment> readBuffers = new ArrayDeque<>();
-
-    /** Buffers read by the file reader. */
+    /** Buffers already read which can be consumed by netty thread. */
+    @GuardedBy("lock")
     private final Queue<Buffer> buffersRead = new ArrayDeque<>();
 
     /** File reader used to read buffer from. */
     private final PartitionedFileReader fileReader;
 
-    /** Number of remaining non-event buffers to read. */
+    /** Number of remaining non-event buffers in the buffer queue. */
+    @GuardedBy("lock")
     private int dataBufferBacklog;
 
     /** Whether this reader is released or not. */
+    @GuardedBy("lock")
     private boolean isReleased;
 
+    /** Cause of failure which should be propagated to the consumer. */
+    @GuardedBy("lock")
+    private Throwable failureCause;
+
     /** Sequence number of the next buffer to be sent to the consumer. */
     private int sequenceNumber;
 
     public SortMergeSubpartitionReader(
-            int subpartitionIndex,
-            int dataBufferBacklog,
-            int bufferSize,
             SortMergeResultPartition partition,
             BufferAvailabilityListener listener,
-            PartitionedFile partitionedFile)
-            throws IOException {
+            PartitionedFileReader fileReader) {
         this.partition = checkNotNull(partition);
         this.availabilityListener = checkNotNull(listener);
-        this.dataBufferBacklog = dataBufferBacklog;
-
-        // allocate two pieces of unmanaged segments for data reading
-        for (int i = 0; i < NUM_READ_BUFFERS; i++) {
-            this.readBuffers.add(
-                    
MemorySegmentFactory.allocateUnpooledOffHeapMemory(bufferSize, null));
-        }
-
-        this.fileReader = new PartitionedFileReader(partitionedFile, 
subpartitionIndex);
-        try {
-            readBuffers();
-        } catch (Throwable throwable) {
-            // ensure that the file reader is closed when any exception occurs
-            IOUtils.closeQuietly(fileReader);
-            throw throwable;
-        }
+        this.fileReader = checkNotNull(fileReader);
     }
 
     @Nullable
     @Override
     public BufferAndBacklog getNextBuffer() {
-        checkState(!isReleased, "Reader is already released.");
+        synchronized (lock) {
+            Buffer buffer = buffersRead.poll();
+            if (buffer == null) {
+                return null;
+            }
+
+            if (buffer.isBuffer()) {
+                --dataBufferBacklog;
+            }
 
-        Buffer buffer = buffersRead.poll();
-        if (buffer == null) {
-            return null;
+            Buffer lookAhead = buffersRead.peek();
+            return BufferAndBacklog.fromBufferAndLookahead(
+                    buffer,
+                    lookAhead == null ? Buffer.DataType.NONE : 
lookAhead.getDataType(),
+                    dataBufferBacklog,
+                    sequenceNumber++);
         }
+    }
 
-        if (buffer.isBuffer()) {
-            --dataBufferBacklog;
+    private void addBuffer(Buffer buffer) {
+        boolean notifyAvailable;
+        synchronized (lock) {
+            if (isReleased) {
+                buffer.recycleBuffer();
+                return;
+            }
+
+            notifyAvailable = buffersRead.isEmpty();
+
+            buffersRead.add(buffer);
+            if (buffer.isBuffer()) {
+                ++dataBufferBacklog;
+            }
         }
 
-        final Buffer lookAhead = buffersRead.peek();
+        if (notifyAvailable) {
+            notifyDataAvailable();
+        }
+    }
+
+    boolean readBuffers(Queue<MemorySegment> buffers, BufferRecycler recycler) 
throws IOException {
+        while (!buffers.isEmpty()) {

Review comment:
       You are right, we can do the optimization in the future as a separated 
improvement.




-- 
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


Reply via email to