codelipenghui commented on a change in pull request #9776:
URL: https://github.com/apache/pulsar/pull/9776#discussion_r589366260
##########
File path:
pulsar-client/src/main/java/org/apache/pulsar/client/impl/transaction/TransactionImpl.java
##########
@@ -126,52 +143,76 @@ public synchronized void
registerCumulativeAckConsumer(ConsumerImpl<?> consumer)
@Override
public CompletableFuture<Void> commit() {
- CompletableFuture<Void> commitFuture = new CompletableFuture<>();
- allOpComplete().whenComplete((v, e) -> {
- if (e != null) {
- abort().whenComplete((vx, ex) ->
commitFuture.completeExceptionally(e));
- } else {
- tcClient.commitAsync(new TxnID(txnIdMostBits, txnIdLeastBits))
- .whenComplete((vx, ex) -> {
- if (ex != null) {
- commitFuture.completeExceptionally(ex);
- } else {
- commitFuture.complete(vx);
- }
- });
- }
+ return checkIfOpen().thenCompose((value) -> {
+ CompletableFuture<Void> commitFuture = new CompletableFuture<>();
+ this.state = State.COMMITTING;
+ allOpComplete().whenComplete((v, e) -> {
+ if (e != null) {
+ abort().whenComplete((vx, ex) ->
commitFuture.completeExceptionally(e));
+ } else {
+ tcClient.commitAsync(new TxnID(txnIdMostBits,
txnIdLeastBits))
+ .whenComplete((vx, ex) -> {
+ if (ex != null) {
+ if (ex instanceof
TransactionNotFoundException
+ || ex instanceof
InvalidTxnStatusException) {
+ this.state = State.ERROR;
+ }
+ commitFuture.completeExceptionally(ex);
+ } else {
+ this.state = State.COMMITTED;
+ commitFuture.complete(vx);
+ }
+ });
+ }
+ });
+ return commitFuture;
});
- return commitFuture;
}
@Override
public CompletableFuture<Void> abort() {
- CompletableFuture<Void> abortFuture = new CompletableFuture<>();
- allOpComplete().whenComplete((v, e) -> {
- if (e != null) {
- log.error(e.getMessage());
- }
- if (cumulativeAckConsumers != null) {
- cumulativeAckConsumers.forEach((consumer, integer) ->
- cumulativeAckConsumers
- .putIfAbsent(consumer,
consumer.clearIncomingMessagesAndGetMessageNumber()));
- }
- tcClient.abortAsync(new TxnID(txnIdMostBits,
txnIdLeastBits)).whenComplete((vx, ex) -> {
+ return checkIfOpen().thenCompose(value -> {
+ CompletableFuture<Void> abortFuture = new CompletableFuture<>();
Review comment:
Change to aborting first?
##########
File path:
pulsar-broker/src/test/java/org/apache/pulsar/client/impl/TransactionEndToEndTest.java
##########
@@ -676,4 +685,82 @@ public void produceTxnMessageOrderTest() throws Exception {
}
}
+ @Test
+ public void produceAndConsumeCloseStateTxnTest() throws Exception {
+ String topic = NAMESPACE1 + "/txn-close-state";
+
+ @Cleanup
+ Consumer<byte[]> consumer = pulsarClient.newConsumer()
+ .topic(topic)
+ .subscriptionName("test")
+ .subscribe();
+
+ @Cleanup
+ Producer<byte[]> producer = pulsarClient.newProducer()
+ .topic(topic)
+ .sendTimeout(0, TimeUnit.SECONDS)
+ .producerName("txn-close-state")
+ .create();
+
+ Transaction produceTxn = pulsarClient
+ .newTransaction()
+ .withTransactionTimeout(2, TimeUnit.SECONDS)
+ .build().get();
+
+ Transaction consumeTxn = pulsarClient
+ .newTransaction()
+ .withTransactionTimeout(2, TimeUnit.SECONDS)
+ .build().get();
+
+ producer.newMessage(produceTxn).value(("Hello
Pulsar!").getBytes()).sendAsync().get();
+ produceTxn.commit().get();
+ try {
+ producer.newMessage(produceTxn).value(("Hello
Pulsar!").getBytes()).sendAsync().get();
+ fail();
+ } catch (Exception e) {
+ assertTrue(e.getCause() instanceof
TransactionCoordinatorClientException.InvalidTxnStatusException);
+ }
+
+ try {
+ produceTxn.commit().get();
+ fail();
+ } catch (Exception e) {
+ assertTrue(e.getCause() instanceof
TransactionCoordinatorClientException.InvalidTxnStatusException);
+ }
+
+
+ Message<byte[]> message = consumer.receive(5, TimeUnit.SECONDS);
+ consumer.acknowledgeAsync(message.getMessageId(), consumeTxn).get();
+ consumeTxn.commit().get();
+ try {
+ consumer.acknowledgeAsync(message.getMessageId(),
consumeTxn).get();
+ fail();
+ } catch (Exception e) {
+ assertTrue(e.getCause() instanceof
TransactionCoordinatorClientException.InvalidTxnStatusException);
+ }
+
+ try {
+ consumeTxn.commit().get();
+ fail();
+ } catch (Exception e) {
+ assertTrue(e.getCause() instanceof
TransactionCoordinatorClientException.InvalidTxnStatusException);
+ }
+
+ Transaction timeoutTxn = pulsarClient
+ .newTransaction()
+ .withTransactionTimeout(1, TimeUnit.SECONDS)
+ .build().get();
+
+ Thread.sleep(2000);
Review comment:
Please avoid use sleep.
##########
File path:
pulsar-client/src/main/java/org/apache/pulsar/client/impl/transaction/TransactionImpl.java
##########
@@ -126,52 +143,76 @@ public synchronized void
registerCumulativeAckConsumer(ConsumerImpl<?> consumer)
@Override
public CompletableFuture<Void> commit() {
- CompletableFuture<Void> commitFuture = new CompletableFuture<>();
- allOpComplete().whenComplete((v, e) -> {
- if (e != null) {
- abort().whenComplete((vx, ex) ->
commitFuture.completeExceptionally(e));
- } else {
- tcClient.commitAsync(new TxnID(txnIdMostBits, txnIdLeastBits))
- .whenComplete((vx, ex) -> {
- if (ex != null) {
- commitFuture.completeExceptionally(ex);
- } else {
- commitFuture.complete(vx);
- }
- });
- }
+ return checkIfOpen().thenCompose((value) -> {
+ CompletableFuture<Void> commitFuture = new CompletableFuture<>();
+ this.state = State.COMMITTING;
+ allOpComplete().whenComplete((v, e) -> {
+ if (e != null) {
+ abort().whenComplete((vx, ex) ->
commitFuture.completeExceptionally(e));
+ } else {
+ tcClient.commitAsync(new TxnID(txnIdMostBits,
txnIdLeastBits))
+ .whenComplete((vx, ex) -> {
+ if (ex != null) {
+ if (ex instanceof
TransactionNotFoundException
+ || ex instanceof
InvalidTxnStatusException) {
+ this.state = State.ERROR;
+ }
+ commitFuture.completeExceptionally(ex);
+ } else {
+ this.state = State.COMMITTED;
+ commitFuture.complete(vx);
+ }
+ });
+ }
+ });
+ return commitFuture;
});
- return commitFuture;
}
@Override
public CompletableFuture<Void> abort() {
- CompletableFuture<Void> abortFuture = new CompletableFuture<>();
- allOpComplete().whenComplete((v, e) -> {
- if (e != null) {
- log.error(e.getMessage());
- }
- if (cumulativeAckConsumers != null) {
- cumulativeAckConsumers.forEach((consumer, integer) ->
- cumulativeAckConsumers
- .putIfAbsent(consumer,
consumer.clearIncomingMessagesAndGetMessageNumber()));
- }
- tcClient.abortAsync(new TxnID(txnIdMostBits,
txnIdLeastBits)).whenComplete((vx, ex) -> {
+ return checkIfOpen().thenCompose(value -> {
+ CompletableFuture<Void> abortFuture = new CompletableFuture<>();
+ allOpComplete().whenComplete((v, e) -> {
+ if (e != null) {
+ log.error(e.getMessage());
+ }
if (cumulativeAckConsumers != null) {
-
cumulativeAckConsumers.forEach(ConsumerImpl::increaseAvailablePermits);
- cumulativeAckConsumers.clear();
+ cumulativeAckConsumers.forEach((consumer, integer) ->
+ cumulativeAckConsumers
+ .putIfAbsent(consumer,
consumer.clearIncomingMessagesAndGetMessageNumber()));
}
+ tcClient.abortAsync(new TxnID(txnIdMostBits,
txnIdLeastBits)).whenComplete((vx, ex) -> {
+ if (cumulativeAckConsumers != null) {
+
cumulativeAckConsumers.forEach(ConsumerImpl::increaseAvailablePermits);
+ cumulativeAckConsumers.clear();
+ }
- if (ex != null) {
- abortFuture.completeExceptionally(ex);
- } else {
- abortFuture.complete(null);
- }
+ if (ex != null) {
+ if (ex instanceof TransactionNotFoundException
+ || ex instanceof InvalidTxnStatusException) {
+ this.state = State.ERROR;
+ }
+ abortFuture.completeExceptionally(ex);
+ } else {
+ this.state = State.ABORTED;
+ abortFuture.complete(null);
+ }
+ });
});
+
+ return abortFuture;
});
+ }
- return abortFuture;
+ private CompletableFuture<Void> checkIfOpen() {
+ if (state == State.OPEN) {
+ return CompletableFuture.completedFuture(null);
+ } else {
+ return FutureUtil.failedFuture(new InvalidTxnStatusException("[" +
txnIdMostBits + ":"
+ + txnIdLeastBits + "] Transaction in " + state.name() + "
state!"));
Review comment:
print the expected transaction state and the current transaction state?
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]