poorbarcode commented on code in PR #16758:
URL: https://github.com/apache/pulsar/pull/16758#discussion_r937563592


##########
pulsar-transaction/coordinator/src/main/java/org/apache/pulsar/transaction/coordinator/impl/TxnLogBufferedWriter.java:
##########
@@ -201,30 +213,93 @@ public void asyncAddData(T data, AddDataCallback 
callback, Object ctx){
         singleThreadExecutorForWrite.execute(() -> internalAsyncAddData(data, 
callback, ctx));
     }
 
+    /**
+     * Append data to queue, if reach {@link #batchedWriteMaxRecords} or 
{@link #batchedWriteMaxSize}, do flush. And if
+     * accept a request that {@param data} is too large (larger than {@link 
#batchedWriteMaxSize}), then two flushes
+     * are executed:
+     *    1. Write the data cached in the queue to BK.
+     *    2. Direct write the large data to BK.
+     * This ensures the sequential nature of multiple writes to BK.
+     */
     private void internalAsyncAddData(T data, AddDataCallback callback, Object 
ctx){
         if (state == State.CLOSING || state == State.CLOSED){
             callback.addFailed(BUFFERED_WRITER_CLOSED_EXCEPTION, ctx);
             return;
         }
+        // If param-data is too large.
         int len = dataSerializer.getSerializedSize(data);
         if (len >= batchedWriteMaxSize){
-            if (!flushContext.asyncAddArgsList.isEmpty()) {
-                doTrigFlush(true, false);
-            }
-            ByteBuf byteBuf = dataSerializer.serialize(data);
-            managedLedger.asyncAddEntry(byteBuf, 
DisabledBatchCallback.INSTANCE,
-                    AsyncAddArgs.newInstance(callback, ctx, 
System.currentTimeMillis(), byteBuf));
+            trigFlushByLargeSingleData(data, callback, ctx);
             return;
         }
-        // Add data.
+        // Append data to queue.
         this.dataArray.add(data);
         // Add callback info.
         AsyncAddArgs asyncAddArgs = AsyncAddArgs.newInstance(callback, ctx, 
System.currentTimeMillis());
         this.flushContext.asyncAddArgsList.add(asyncAddArgs);
         // Calculate bytes-size.
         this.bytesSize += len;
-        // trig flush.
-        doTrigFlush(false, false);
+        // trig flush by max records or max size.
+        trigFlushIfReachMaxRecordsOrMaxSize();
+    }
+
+    /**
+     * Change to IO thread and do flush, only called by {@link 
#timingFlushTask}.
+     */
+    private void trigFlushByTimingTask(){
+        singleThreadExecutorForWrite.execute(() -> {
+            if (flushContext.asyncAddArgsList.isEmpty()) {
+                return;
+            }
+            if (metrics != null) {
+                
metrics.triggerFlushByByMaxDelay(this.flushContext.asyncAddArgsList.size(), 
this.bytesSize,
+                        System.currentTimeMillis() - 
flushContext.asyncAddArgsList.get(0).addedTime);
+            }
+            doFlush();
+            // Start the next timing task.
+            nextTimingTrigger();
+        });
+    }
+
+    /**
+     * If reach the thresholds {@link #batchedWriteMaxRecords} or {@link 
#batchedWriteMaxSize}, do flush.
+     */
+    private void trigFlushIfReachMaxRecordsOrMaxSize(){
+        if (this.flushContext.asyncAddArgsList.size() >= 
batchedWriteMaxRecords) {
+            if (metrics != null) {
+                
metrics.triggerFlushByRecordsCount(this.flushContext.asyncAddArgsList.size(), 
this.bytesSize,
+                        System.currentTimeMillis() - 
flushContext.asyncAddArgsList.get(0).addedTime);
+            }
+            doFlush();
+            return;
+        }
+        if (this.bytesSize >= batchedWriteMaxSize) {
+            if (metrics != null) {
+                
metrics.triggerFlushByBytesSize(this.flushContext.asyncAddArgsList.size(), 
this.bytesSize,
+                        System.currentTimeMillis() - 
flushContext.asyncAddArgsList.get(0).addedTime);
+            }
+            doFlush();
+        }
+    }
+
+    /**

Review Comment:
   Already removed this comment.



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