codelipenghui commented on a change in pull request #12219:
URL: https://github.com/apache/pulsar/pull/12219#discussion_r727976752



##########
File path: 
pulsar-broker/src/main/java/org/apache/pulsar/broker/transaction/buffer/exceptions/TransactionBufferStatusException.java
##########
@@ -0,0 +1,34 @@
+/**
+ * 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.transaction.buffer.exceptions;
+
+
+import 
org.apache.pulsar.broker.transaction.buffer.impl.TopicTransactionBufferState;
+
+public class TransactionBufferStatusException extends 
TransactionBufferException{

Review comment:
       It can be an inner class of TransactionBufferException?

##########
File path: 
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentTopic.java
##########
@@ -2981,12 +2981,16 @@ public void publishTxnMessage(TxnID txnID, ByteBuf 
headersAndPayload, PublishCon
                                     // Message has been successfully persisted
                                     
messageDeduplication.recordMessagePersisted(publishContext,
                                             (PositionImpl) position);
-                                    publishContext.completed(null, 
((PositionImpl) position).getLedgerId(),
-                                            ((PositionImpl) 
position).getEntryId());
+                                    publishContext.completed(null, 
position.getLedgerId(),
+                                            position.getEntryId());
 
                                     decrementPendingWriteOpsAndCheck();
                                 })
                                 .exceptionally(throwable -> {
+                                    throwable = throwable.getCause();
+                                    if (!(throwable instanceof 
ManagedLedgerException)){
+                                        throwable = new 
ManagedLedgerException(throwable);
+                                    }

Review comment:
       Why need to covert all exceptions to ManagedLedgerException? And it's 
better to add error log here, so that we can debug more easier

##########
File path: 
pulsar-broker/src/main/java/org/apache/pulsar/broker/transaction/buffer/impl/TopicTransactionBufferState.java
##########
@@ -32,7 +32,8 @@
         None,
         Initializing,
         Ready,
-        Close
+        Close,
+        Unused

Review comment:
       Does `NoSnapshot` is more meaningful here?

##########
File path: 
pulsar-broker/src/main/java/org/apache/pulsar/broker/transaction/buffer/impl/TopicTransactionBuffer.java
##########
@@ -398,13 +451,14 @@ public void 
syncMaxReadPositionForNormalPublish(PositionImpl position) {
         synchronized (TopicTransactionBuffer.this) {
             if (ongoingTxns.isEmpty()) {
                 maxReadPosition = position;
+                changeMaxReadPositionAndAddAbortTimes.incrementAndGet();
             }
         }
     }
 
     @Override
     public PositionImpl getMaxReadPosition() {
-        if (checkIfReady()) {
+        if (checkIfReady() || checkIfUnused()) {

Review comment:
       If the state is unused, we should return the latest?

##########
File path: 
pulsar-broker/src/main/java/org/apache/pulsar/broker/transaction/buffer/impl/TopicTransactionBuffer.java
##########
@@ -163,6 +174,34 @@ public void recoverExceptionally(Exception e) {
     @Override
     public CompletableFuture<Position> appendBufferToTxn(TxnID txnId, long 
sequenceId, ByteBuf buffer) {
         CompletableFuture<Position> completableFuture = new 
CompletableFuture<>();
+        if (checkIfReady()){

Review comment:
       ```suggestion
           if (checkIfReady()) {
   ```

##########
File path: 
pulsar-broker/src/main/java/org/apache/pulsar/broker/transaction/buffer/impl/TopicTransactionBuffer.java
##########
@@ -163,6 +174,34 @@ public void recoverExceptionally(Exception e) {
     @Override
     public CompletableFuture<Position> appendBufferToTxn(TxnID txnId, long 
sequenceId, ByteBuf buffer) {
         CompletableFuture<Position> completableFuture = new 
CompletableFuture<>();
+        if (checkIfReady()){
+            addTxnEntry(completableFuture, txnId, buffer);
+        } else {
+            if (checkIfUnused() && changeToInitializingStateFromUnused()){
+                //`PulsarDecoder` will release this buffer  in `finally` and 
`takeSnapshot` is asynchronous
+                buffer.retain();
+                takeSnapshot().thenAccept(ignore -> {
+                    changeToReadyState();
+                    buffer.release();
+                    timer.newTimeout(TopicTransactionBuffer.this,
+                            takeSnapshotIntervalTime, TimeUnit.MILLISECONDS);
+                    log.info("Topic {} take snapshot successfully when uses 
TransactionBuffer at the first time",
+                            this.topic.getName());
+                }).exceptionally(exception -> {
+                    changeToUnUsedState();
+                    buffer.release();
+                    log.error("Topic {} fail to takeSnapshot before adding the 
first message with transaction",
+                            this.topic.getName(), exception);
+                    return null;
+                });
+            }
+        completableFuture.completeExceptionally(new 
TransactionBufferStatusException(this.topic.getName(),

Review comment:
       If we are taking the snapshot, all the messages publish with transaction 
will get exception here

##########
File path: 
pulsar-broker/src/main/java/org/apache/pulsar/broker/transaction/buffer/impl/TopicTransactionBuffer.java
##########
@@ -102,11 +103,12 @@ public TopicTransactionBuffer(PersistentTopic topic, 
CompletableFuture<Void> tra
                 
.getConfiguration().getTransactionBufferSnapshotMaxTransactionCount();
         this.takeSnapshotIntervalTime = topic.getBrokerService().getPulsar()
                 
.getConfiguration().getTransactionBufferSnapshotMinTimeInMillis();
+        this.maxReadPosition = (PositionImpl) 
topic.getManagedLedger().getLastConfirmedEntry();
         
this.topic.getBrokerService().getPulsar().getTransactionReplayExecutor()
                 .execute(new TopicTransactionBufferRecover(new 
TopicTransactionBufferRecoverCallBack() {
                     @Override
                     public void recoverComplete() {
-                        if (!changeToReadyState()) {
+                        if (!changeToReadyState()){

Review comment:
       ```suggestion
                           if (!changeToReadyState()) {
   ```

##########
File path: 
pulsar-broker/src/main/java/org/apache/pulsar/broker/transaction/buffer/impl/TopicTransactionBuffer.java
##########
@@ -163,6 +174,34 @@ public void recoverExceptionally(Exception e) {
     @Override
     public CompletableFuture<Position> appendBufferToTxn(TxnID txnId, long 
sequenceId, ByteBuf buffer) {
         CompletableFuture<Position> completableFuture = new 
CompletableFuture<>();
+        if (checkIfReady()){
+            addTxnEntry(completableFuture, txnId, buffer);
+        } else {
+            if (checkIfUnused() && changeToInitializingStateFromUnused()){
+                //`PulsarDecoder` will release this buffer  in `finally` and 
`takeSnapshot` is asynchronous
+                buffer.retain();
+                takeSnapshot().thenAccept(ignore -> {
+                    changeToReadyState();

Review comment:
       If the snapshot is created, we should add the txn entry? 




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