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



##########
File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/consumer/BufferManager.java
##########
@@ -151,23 +149,42 @@ 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() {

Review comment:
       This method should be placed into the below section `Buffer recycle`. 
   
   I think it is better to integrate this method with existing 
`#releaseFloatingBuffers` to provide a general one, otherwise it might bring 
confusing to understand the difference among them, especially for the different 
handle of `numRequiredBuffers`, to make them seem customized logic.
   
   The integration is as below
   
   ```
   void releaseFloatingBuffers(boolean isTemporaryRelease) {
                synchronized (bufferQueue) {
                        if (isWaitingForFloatingBuffers) {
                                
inputChannel.inputGate.getBufferPool().removeBufferListener(this);
                                isWaitingForFloatingBuffers = false;
                        }
   
                        int numReleasedBuffers = 
bufferQueue.releaseFloatingBuffers();
                        if (isTemporaryRelease) {
                                numRequiredBuffers += numReleasedBuffers;
                        } else {
                                numRequiredBuffers = 0;
                        }
                }
        }
   ```




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