diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/TransactionMetadataStoreService.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/TransactionMetadataStoreService.java index 3d9e6924d1168..d0cf22a86533a 100644 --- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/TransactionMetadataStoreService.java +++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/TransactionMetadataStoreService.java @@ -133,54 +133,66 @@ public CompletableFuture handleTcClientConnect(TransactionCoordinatorID tc return; } - openTransactionMetadataStore(tcId).thenAccept((store) -> internalPinnedExecutor.execute(() -> { - stores.put(tcId, store); - LOG.info("Added new transaction meta store {}", tcId); - long endTime = System.currentTimeMillis() + HANDLE_PENDING_CONNECT_TIME_OUT; - while (true) { - // prevent thread in a busy loop. - if (System.currentTimeMillis() < endTime) { - CompletableFuture future = deque.poll(); - if (future != null) { - // complete queue request future - future.complete(null); - } else { - break; - } - } else { - deque.clear(); - break; - } - } - - completableFuture.complete(null); - tcLoadSemaphore.release(); - })).exceptionally(e -> { - internalPinnedExecutor.execute(() -> { - completableFuture.completeExceptionally(e.getCause()); - // release before handle request queue, - //in order to client reconnect infinite loop - tcLoadSemaphore.release(); - long endTime = System.currentTimeMillis() + HANDLE_PENDING_CONNECT_TIME_OUT; - while (true) { - // prevent thread in a busy loop. - if (System.currentTimeMillis() < endTime) { - CompletableFuture future = deque.poll(); - if (future != null) { - // this means that this tc client connection connect fail - future.completeExceptionally(e); - } else { - break; - } + TransactionTimeoutTracker timeoutTracker = timeoutTrackerFactory.newTracker(tcId); + TransactionRecoverTracker recoverTracker = + new TransactionRecoverTrackerImpl(TransactionMetadataStoreService.this, + timeoutTracker, tcId.getId()); + openTransactionMetadataStore(tcId, timeoutTracker, recoverTracker).thenAccept( + store -> internalPinnedExecutor.execute(() -> { + // TransactionMetadataStore initialization + // need to use TransactionMetadataStore itself. + // we need to put store into stores map before + // handle committing and aborting transaction. + stores.put(tcId, store); + LOG.info("Added new transaction meta store {}", tcId); + recoverTracker.handleCommittingAndAbortingTransaction(); + timeoutTracker.start(); + + long endTime = System.currentTimeMillis() + HANDLE_PENDING_CONNECT_TIME_OUT; + while (true) { + // prevent thread in a busy loop. + if (System.currentTimeMillis() < endTime) { + CompletableFuture future = deque.poll(); + if (future != null) { + // complete queue request future + future.complete(null); } else { - deque.clear(); break; } + } else { + deque.clear(); + break; } - LOG.error("Add transaction metadata store with id {} error", tcId.getId(), e); - }); - return null; - }); + } + + completableFuture.complete(null); + tcLoadSemaphore.release(); + })).exceptionally(e -> { + internalPinnedExecutor.execute(() -> { + completableFuture.completeExceptionally(e.getCause()); + // release before handle request queue, + //in order to client reconnect infinite loop + tcLoadSemaphore.release(); + long endTime = System.currentTimeMillis() + HANDLE_PENDING_CONNECT_TIME_OUT; + while (true) { + // prevent thread in a busy loop. + if (System.currentTimeMillis() < endTime) { + CompletableFuture future = deque.poll(); + if (future != null) { + // this means that this tc client connection connect fail + future.completeExceptionally(e); + } else { + break; + } + } else { + deque.clear(); + break; + } + } + LOG.error("Add transaction metadata store with id {} error", tcId.getId(), e); + }); + return null; + }); } else { // only one command can open transaction metadata store, // other will be added to the deque, when the op of openTransactionMetadataStore finished @@ -200,9 +212,11 @@ public CompletableFuture handleTcClientConnect(TransactionCoordinatorID tc return completableFuture; } - public CompletableFuture openTransactionMetadataStore(TransactionCoordinatorID tcId) { - final Timer brokerClientSharedTimer = - pulsarService.getBrokerClientSharedTimer(); + public CompletableFuture + openTransactionMetadataStore(TransactionCoordinatorID tcId, + TransactionTimeoutTracker timeoutTracker, + TransactionRecoverTracker recoverTracker) { + final Timer brokerClientSharedTimer = pulsarService.getBrokerClientSharedTimer(); final ServiceConfiguration serviceConfiguration = pulsarService.getConfiguration(); final TxnLogBufferedWriterConfig txnLogBufferedWriterConfig = new TxnLogBufferedWriterConfig(); txnLogBufferedWriterConfig.setBatchEnabled(serviceConfiguration.isTransactionLogBatchedWriteEnabled()); @@ -212,18 +226,11 @@ public CompletableFuture openTransactionMetadataStore( txnLogBufferedWriterConfig .setBatchedWriteMaxDelayInMillis(serviceConfiguration.getTransactionLogBatchedWriteMaxDelayInMillis()); - return pulsarService.getBrokerService() - .getManagedLedgerConfig(getMLTransactionLogName(tcId)).thenCompose(v -> { - TransactionTimeoutTracker timeoutTracker = timeoutTrackerFactory.newTracker(tcId); - TransactionRecoverTracker recoverTracker = - new TransactionRecoverTrackerImpl(TransactionMetadataStoreService.this, - timeoutTracker, tcId.getId()); - return transactionMetadataStoreProvider - .openStore(tcId, pulsarService.getManagedLedgerFactory(), v, - timeoutTracker, recoverTracker, - pulsarService.getConfig().getMaxActiveTransactionsPerCoordinator(), - txnLogBufferedWriterConfig, brokerClientSharedTimer); - }); + return pulsarService.getBrokerService().getManagedLedgerConfig(getMLTransactionLogName(tcId)).thenCompose( + v -> transactionMetadataStoreProvider.openStore(tcId, pulsarService.getManagedLedgerFactory(), v, + timeoutTracker, recoverTracker, + pulsarService.getConfig().getMaxActiveTransactionsPerCoordinator(), txnLogBufferedWriterConfig, + brokerClientSharedTimer)); } public CompletableFuture removeTransactionMetadataStore(TransactionCoordinatorID tcId) { diff --git a/pulsar-transaction/coordinator/src/main/java/org/apache/pulsar/transaction/coordinator/impl/MLTransactionMetadataStore.java b/pulsar-transaction/coordinator/src/main/java/org/apache/pulsar/transaction/coordinator/impl/MLTransactionMetadataStore.java index 53a515ff99164..2db278f1341be 100644 --- a/pulsar-transaction/coordinator/src/main/java/org/apache/pulsar/transaction/coordinator/impl/MLTransactionMetadataStore.java +++ b/pulsar-transaction/coordinator/src/main/java/org/apache/pulsar/transaction/coordinator/impl/MLTransactionMetadataStore.java @@ -128,8 +128,6 @@ public void replayComplete() { } else { completableFuture.complete(MLTransactionMetadataStore.this); - recoverTracker.handleCommittingAndAbortingTransaction(); - timeoutTracker.start(); recoverTime.setRecoverEndTime(System.currentTimeMillis()); } }