duongkame commented on code in PR #6777:
URL: https://github.com/apache/ozone/pull/6777#discussion_r1633608338


##########
hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/BlockOutputStream.java:
##########
@@ -535,7 +572,20 @@ private void writeChunk(ChunkBuffer buffer)
       bufferList = new ArrayList<>();
     }
     bufferList.add(buffer);
-    writeChunkToContainer(buffer.duplicate(0, buffer.position()));
+    writeChunkToContainer(buffer.duplicate(0, buffer.position()));  // What 
bad things can happen if we don't duplicate the buffer here: 
java.lang.NullPointerException: Future not found for 16384: existing = [1]
+  }
+
+  /**
+   * Async version of handleFlush.
+   */
+  protected CompletableFuture<Void> asyncHandleFlush(boolean close) {
+    return CompletableFuture.runAsync(() -> {

Review Comment:
   Starting an asynchronous path here probably wouldn't yield any parallel 
gain, because it content `handleFlush` is mostly synchronized. It would be 
better to use the calling thread. The asynchronous should start only from 
waiting for `WriteChunk` response and the subsequent logic (`putBlock`, 
`watchForCommit`). 



##########
hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/BlockOutputStream.java:
##########
@@ -563,22 +613,24 @@ protected void handleFlush(boolean close) throws 
IOException {
   private void handleFlushInternal(boolean close)
       throws IOException, InterruptedException, ExecutionException {
     checkOpen();
-    // flush the last chunk data residing on the currentBuffer
-    if (totalDataFlushedLength < writtenDataLength) {
-      refreshCurrentBuffer();
-      Preconditions.checkArgument(currentBuffer.position() > 0);
-      if (currentBuffer.hasRemaining()) {
-        writeChunk(currentBuffer);
+    synchronized (this) {
+      // flush the last chunk data residing on the currentBuffer
+      if (totalDataFlushedLength < writtenDataLength) {
+        refreshCurrentBuffer();
+        Preconditions.checkArgument(currentBuffer.position() > 0);
+        if (currentBuffer.hasRemaining()) {
+          writeChunk(currentBuffer);
+        }
+        // This can be a partially filled chunk. Since we are flushing the 
buffer
+        // here, we just limit this buffer to the current position. So that 
next
+        // write will happen in new buffer
+        updateFlushLength();
+        executePutBlock(close, false);
+      } else if (close) {
+        // forcing an "empty" putBlock if stream is being closed without new
+        // data since latest flush - we need to send the "EOF" flag
+        executePutBlock(true, true);
       }
-      // This can be a partially filled chunk. Since we are flushing the buffer
-      // here, we just limit this buffer to the current position. So that next
-      // write will happen in new buffer
-      updateFlushLength();
-      executePutBlock(close, false);
-    } else if (close) {
-      // forcing an "empty" putBlock if stream is being closed without new
-      // data since latest flush - we need to send the "EOF" flag
-      executePutBlock(true, true);
     }
     waitOnFlushFutures();
     watchForCommit(false);

Review Comment:
   This is where the logic is getting wrong with simultaneous computation. 
   Previously, `watchForCommit(false)` with send watchCommit on the lastIndex 
in the commitWatcher state. 
   ```
   XceiverClientReply sendWatchForCommit(boolean bufferFull) throws IOException 
{
       return bufferFull ? commitWatcher.watchOnFirstIndex()
           : commitWatcher.watchOnLastIndex();
     }
   ```
   
   This is only correct when the KeyOutputStream is fully synchronized. This 
logic becomes wrong when we send multiple writeChunk/putBlock requests in 
parallel and perform `watchForCommit `asynchronously. A watchIndex must be 
captured in the future context when calling putBlock (see the 
`updateCommitInfo` call in BlockOutputStream#executePutBlock). This index 
should be used for this `watchForCommit()` call (see 
AbstractCommitWatcher#watchForCommit). Logically, this means, each thread 
should watch for the commit associated with the chunk it sends, not the last or 
the first one. 
   



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to