Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix][client] TransactionCoordinatorClient support retry #23081

Merged
merged 4 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import lombok.Cleanup;
import org.apache.pulsar.broker.PulsarService;
import org.apache.pulsar.broker.TransactionMetadataStoreService;
import org.apache.pulsar.broker.transaction.buffer.impl.TransactionBufferClientImpl;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.PulsarClientException;
import org.apache.pulsar.client.api.transaction.TransactionBufferClient;
import org.apache.pulsar.client.api.transaction.TransactionCoordinatorClient;
import org.apache.pulsar.client.api.transaction.TransactionCoordinatorClient.State;
import org.apache.pulsar.client.api.transaction.TransactionCoordinatorClientException;
import org.apache.pulsar.client.api.transaction.TxnID;
import org.apache.pulsar.client.impl.transaction.TransactionCoordinatorClientImpl;
import org.mockito.Mockito;
import org.testng.Assert;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -107,4 +111,24 @@ public void testTransactionCoordinatorExceptionUnwrap() {
instanceof TransactionCoordinatorClientException.InvalidTxnStatusException);
}
}

@Test
public void testClientStartWithRetry() throws Exception{
String validBrokerServiceUrl = pulsarServices[0].getBrokerServiceUrl();
String invalidBrokerServiceUrl = "localhost:0";
String brokerServiceUrl = validBrokerServiceUrl + "," + invalidBrokerServiceUrl;

@Cleanup
PulsarClient pulsarClient = PulsarClient.builder().serviceUrl(brokerServiceUrl).build();
@Cleanup
TransactionCoordinatorClient transactionCoordinatorClient = new TransactionCoordinatorClientImpl(pulsarClient);

try {
transactionCoordinatorClient.start();
}catch (TransactionCoordinatorClientException e) {
Assert.fail("Shouldn't have exception at here", e);
}

Assert.assertEquals(transactionCoordinatorClient.getState(), State.READY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -88,6 +89,10 @@ public RequestTime(long creationTime, long requestId) {
private Timeout requestTimeout;

private final CompletableFuture<Void> connectFuture;
private final long lookupDeadline;
private final List<Throwable> previousExceptions = new CopyOnWriteArrayList<>();



public TransactionMetaStoreHandler(long transactionCoordinatorId, PulsarClientImpl pulsarClient, String topic,
CompletableFuture<Void> connectFuture) {
Expand All @@ -109,6 +114,7 @@ public TransactionMetaStoreHandler(long transactionCoordinatorId, PulsarClientIm
this.connectFuture = connectFuture;
this.internalPinnedExecutor = pulsarClient.getInternalExecutorService();
this.timer = pulsarClient.timer();
this.lookupDeadline = System.currentTimeMillis() + client.getConfiguration().getLookupTimeoutMs();
}

public void start() {
Expand All @@ -117,10 +123,22 @@ public void start() {

@Override
public void connectionFailed(PulsarClientException exception) {
LOG.error("Transaction meta handler with transaction coordinator id {} connection failed.",
transactionCoordinatorId, exception);
if (!this.connectFuture.isDone()) {
this.connectFuture.completeExceptionally(exception);
boolean nonRetriableError = !PulsarClientException.isRetriableError(exception);
boolean timeout = System.currentTimeMillis() > lookupDeadline;
if (nonRetriableError || timeout) {
exception.setPreviousExceptions(previousExceptions);
if (connectFuture.completeExceptionally(exception)) {
if (nonRetriableError) {
LOG.error("Transaction meta handler with transaction coordinator id {} connection failed.",
transactionCoordinatorId, exception);
} else {
LOG.error("Transaction meta handler with transaction coordinator id {} connection failed after "
+ "timeout", transactionCoordinatorId, exception);
}
setState(State.Failed);
}
} else {
previousExceptions.add(exception);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ public void start() throws TransactionCoordinatorClientException {
@Override
public CompletableFuture<Void> startAsync() {
if (STATE_UPDATER.compareAndSet(this, State.NONE, State.STARTING)) {
return pulsarClient.getLookup()
.getPartitionedTopicMetadata(SystemTopicNames.TRANSACTION_COORDINATOR_ASSIGN, true)
return pulsarClient.getPartitionedTopicMetadata(
SystemTopicNames.TRANSACTION_COORDINATOR_ASSIGN.getPartitionedTopicName(), true)
.thenCompose(partitionMeta -> {
List<CompletableFuture<Void>> connectFutureList = new ArrayList<>();
if (LOG.isDebugEnabled()) {
Expand Down
Loading