zhijiangW commented on a change in pull request #11877:
URL: https://github.com/apache/flink/pull/11877#discussion_r424860940



##########
File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/consumer/BufferManager.java
##########
@@ -151,23 +150,43 @@ int requestFloatingBuffers(int numRequired) throws 
IOException {
                                return numRequestedBuffers;
                        }
 
-                       numRequiredBuffers = numRequired;
+                       numRequiredBuffers += numRequired;
+                       numRequestedBuffers = 
internalRequestFloatingBuffers(numRequiredBuffers);
+                       numRequiredBuffers -= numRequestedBuffers;
+               }
+               return numRequestedBuffers;
+       }
 
-                       while (bufferQueue.getAvailableBufferSize() < 
numRequiredBuffers && !isWaitingForFloatingBuffers) {
-                               BufferPool bufferPool = 
inputChannel.inputGate.getBufferPool();
-                               Buffer buffer = bufferPool.requestBuffer();
-                               if (buffer != null) {
-                                       bufferQueue.addFloatingBuffer(buffer);
-                                       numRequestedBuffers++;
-                               } else if (bufferPool.addBufferListener(this)) {
-                                       isWaitingForFloatingBuffers = true;
-                                       break;
-                               }
+       private int internalRequestFloatingBuffers(int numBuffersToRequest) 
throws IOException {
+               assert Thread.holdsLock(bufferQueue);
+
+               int numRequestedBuffers = 0;
+               while (numRequestedBuffers < numBuffersToRequest && 
!isWaitingForFloatingBuffers) {
+                       BufferPool bufferPool = 
inputChannel.inputGate.getBufferPool();
+                       Buffer buffer = bufferPool.requestBuffer();
+                       if (buffer != null) {
+                               bufferQueue.addFloatingBuffer(buffer);
+                               numRequestedBuffers++;
+                       } else if (bufferPool.addBufferListener(this)) {
+                               isWaitingForFloatingBuffers = true;
+                               break;
                        }
                }
                return numRequestedBuffers;
        }
 
+       public void unregisterBufferListenerAndReleaseFloatingBuffers() {
+               synchronized (bufferQueue) {
+                       if (isWaitingForFloatingBuffers) {
+                               
inputChannel.inputGate.getBufferPool().removeBufferListener(this);
+                               isWaitingForFloatingBuffers = false;
+                       }
+
+                       int bufferReleased = 
bufferQueue.releaseFloatingBuffers();

Review comment:
       nit: numReleasedBuffers




----------------------------------------------------------------
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:
[email protected]


Reply via email to