reswqa commented on code in PR #24688:
URL: https://github.com/apache/flink/pull/24688#discussion_r1572050446


##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/storage/SortBufferAccumulator.java:
##########
@@ -338,11 +365,22 @@ private Buffer requestBuffer() {
     }
 
     private void releaseFreeBuffers() {
-        freeSegments.forEach(this::recycleBuffer);
-        freeSegments.clear();
+        synchronized (lock) {
+            isDataBufferReleased = true;
+            freeSegments.forEach(this::recycleBuffer);
+            freeSegments.clear();
+        }
     }
 
     private void recycleBuffer(MemorySegment memorySegment) {
-        checkNotNull(bufferRecycler).recycle(memorySegment);
+        synchronized (lock) {
+            if (!isDataBufferReleased
+                    && currentDataBuffer != null
+                    && !currentDataBuffer.isReleased()) {
+                freeSegments.add(memorySegment);

Review Comment:
   `freeSegments` should be marked as `GuardedBy("lock")` now.



##########
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/storage/SortBufferAccumulator.java:
##########
@@ -242,46 +260,53 @@ private void requestBuffers() {
     }
 
     private synchronized void flushDataBuffer() {
-        if (currentDataBuffer == null
-                || currentDataBuffer.isReleased()
-                || !currentDataBuffer.hasRemaining()) {
-            return;
-        }
-        currentDataBuffer.finish();
-
-        while (currentDataBuffer.hasRemaining()) {
-            MemorySegment freeSegment = getFreeSegment();
-            BufferWithSubpartition bufferWithSubpartition =
-                    currentDataBuffer.getNextBuffer(freeSegment);
-            if (bufferWithSubpartition == null) {
-                break;
+        synchronized (lock) {
+            if (currentDataBuffer == null
+                    || currentDataBuffer.isReleased()
+                    || !currentDataBuffer.hasRemaining()) {
+                return;
+            }
+            currentDataBuffer.finish();
+
+            while (currentDataBuffer.hasRemaining()) {
+                MemorySegment freeSegment = getFreeSegment();
+                BufferWithSubpartition bufferWithSubpartition =
+                        currentDataBuffer.getNextBuffer(freeSegment);
+                if (bufferWithSubpartition == null) {
+                    break;
+                }
+                int numRemainingConsecutiveBuffers =
+                        (int)
+                                Math.ceil(
+                                        ((double) 
currentDataBuffer.getRecordRemainingBytes())
+                                                / bufferSizeBytes);
+                flushBuffer(bufferWithSubpartition, 
numRemainingConsecutiveBuffers);
             }
-            int numRemainingConsecutiveBuffers =
-                    (int)
-                            Math.ceil(
-                                    ((double) 
currentDataBuffer.getRecordRemainingBytes())
-                                            / bufferSizeBytes);
-            flushBuffer(bufferWithSubpartition, 
numRemainingConsecutiveBuffers);
-        }
 
-        releaseFreeBuffers();
-        currentDataBuffer.release();
+            isDataBufferReleased = true;
+            releaseFreeBuffers();
+            currentDataBuffer.release();
+        }
     }
 
     private synchronized void flushCurrentDataBuffer() {
-        if (currentDataBuffer != null) {
-            flushDataBuffer();
-            currentDataBuffer = null;
+        synchronized (lock) {
+            if (currentDataBuffer != null) {
+                flushDataBuffer();
+                currentDataBuffer = null;
+            }
         }
     }
 
     private synchronized boolean returnFreeSegments(int numSegments) {

Review Comment:
   Not checked, but could we remove the method level `synchronized` now?
   
   We have some methods grab two locks, and it's worth taking a closer look to 
see if it's the right time to refactor 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]

Reply via email to