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


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/validator/TransactionBatchedWriteValidator.java:
##########
@@ -0,0 +1,55 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.broker.validator;
+
+import org.apache.pulsar.broker.ServiceConfiguration;
+
+public class TransactionBatchedWriteValidator {
+
+    public static void validate(ServiceConfiguration configuration){
+        if (configuration.isTransactionPendingAckBatchedWriteEnabled()){
+            if (configuration.getTransactionPendingAckBatchedWriteMaxRecords() 
< 10){
+                throw new IllegalArgumentException("Configuration field "
+                        + "'transactionPendingAckBatchedWriteMaxRecords' value 
must be greater than 10");

Review Comment:
   >=10? Sorry missed it the first time, must be greater than or equal to 10.



##########
pulsar-transaction/coordinator/src/main/java/org/apache/pulsar/transaction/coordinator/impl/TxnLogBufferedWriter.java:
##########
@@ -239,33 +239,35 @@ public void asyncAddData(T data, AddDataCallback 
callback, Object ctx){
      */
     private void internalAsyncAddData(T data, AddDataCallback callback, Object 
ctx){
         // Avoid missing callback, do failed callback when error occur before 
add data to the array.
-        boolean shouldCompensateCallBackWhenWriteFail = true;
+        if (state == State.CLOSING || state == State.CLOSED){
+            callback.addFailed(BUFFERED_WRITER_CLOSED_EXCEPTION, ctx);
+            return;
+        }
+        int dataLength;
         try {
-            if (state == State.CLOSING || state == State.CLOSED){
-                callback.addFailed(BUFFERED_WRITER_CLOSED_EXCEPTION, ctx);
-                return;
-            }
-            int dataLength = dataSerializer.getSerializedSize(data);
-            if (dataLength >= batchedWriteMaxSize){
-                trigFlushByLargeSingleData();
-                ByteBuf byteBuf = dataSerializer.serialize(data);
-                shouldCompensateCallBackWhenWriteFail = false;
-                managedLedger.asyncAddEntry(byteBuf, 
DisabledBatchCallback.INSTANCE,
-                        AsyncAddArgs.newInstance(callback, ctx, 
System.currentTimeMillis(), byteBuf));
-                return;
-            }
-            dataArray.add(data);
+            dataLength = dataSerializer.getSerializedSize(data);
+        } catch (Exception e){
+            callback.addFailed(new ManagedLedgerInterceptException(e), ctx);
+            return;
+        }
+        if (dataLength >= batchedWriteMaxSize){
+            trigFlushByLargeSingleData();
+            ByteBuf byteBuf = dataSerializer.serialize(data);
+            managedLedger.asyncAddEntry(byteBuf, 
DisabledBatchCallback.INSTANCE,
+                    AsyncAddArgs.newInstance(callback, ctx, 
System.currentTimeMillis(), byteBuf));
+            return;
+        }
+        try {
+            // Why should try-catch here?
+            // If the recycle mechanism is not executed as expected, exception 
occurs.
             flushContext.addCallback(callback, ctx);
-            bytesSize += dataLength;
-            shouldCompensateCallBackWhenWriteFail = false;
-            trigFlushIfReachMaxRecordsOrMaxSize();
         } catch (Exception e){
-            if (shouldCompensateCallBackWhenWriteFail){
-                callback.addFailed(new ManagedLedgerInterceptException(e), 
ctx);
-            } else {
-                log.error("Failed to add data asynchronously", e);
-            }
+            callback.addFailed(new ManagedLedgerInterceptException(e), ctx);
+            return;
         }
+        dataArray.add(data);
+        bytesSize += dataLength;
+        trigFlushIfReachMaxRecordsOrMaxSize();

Review Comment:
   💯 This is *so* much better!



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