use of org.apache.pulsar.transaction.coordinator.TransactionMetadataStore in project pulsar by apache.
the class TransactionMetadataStoreService method getLowWaterMark.
public long getLowWaterMark(TxnID txnID) {
TransactionCoordinatorID tcId = getTcIdFromTxnId(txnID);
TransactionMetadataStore store = stores.get(tcId);
if (store == null) {
return 0;
}
return store.getLowWaterMark();
}
use of org.apache.pulsar.transaction.coordinator.TransactionMetadataStore in project pulsar by apache.
the class TransactionMetadataStoreService method removeTransactionMetadataStore.
public CompletableFuture<Void> removeTransactionMetadataStore(TransactionCoordinatorID tcId) {
final Semaphore tcLoadSemaphore = this.tcLoadSemaphores.computeIfAbsent(tcId.getId(), (id) -> new Semaphore(1));
if (tcLoadSemaphore.tryAcquire()) {
TransactionMetadataStore metadataStore = stores.remove(tcId);
if (metadataStore != null) {
metadataStore.closeAsync().whenComplete((v, ex) -> {
if (ex != null) {
LOG.error("Close transaction metadata store with id " + tcId, ex);
} else {
LOG.info("Removed and closed transaction meta store {}", tcId);
}
});
}
tcLoadSemaphore.release();
return CompletableFuture.completedFuture(null);
} else {
return FutureUtil.failedFuture(new ServiceUnitNotReadyException("Could not remove " + "TransactionMetadataStore, it is doing other operations!"));
}
}
use of org.apache.pulsar.transaction.coordinator.TransactionMetadataStore in project pulsar by apache.
the class TransactionMetadataStoreService method getTxnMeta.
public CompletableFuture<TxnMeta> getTxnMeta(TxnID txnId) {
TransactionCoordinatorID tcId = getTcIdFromTxnId(txnId);
TransactionMetadataStore store = stores.get(tcId);
if (store == null) {
return FutureUtil.failedFuture(new CoordinatorNotFoundException(tcId));
}
return store.getTxnMeta(txnId);
}
use of org.apache.pulsar.transaction.coordinator.TransactionMetadataStore in project pulsar by apache.
the class TransactionsBase method internalGetCoordinatorStats.
protected void internalGetCoordinatorStats(AsyncResponse asyncResponse, boolean authoritative, Integer coordinatorId) {
if (pulsar().getConfig().isTransactionCoordinatorEnabled()) {
if (coordinatorId != null) {
validateTopicOwnership(TopicName.TRANSACTION_COORDINATOR_ASSIGN.getPartition(coordinatorId), authoritative);
TransactionMetadataStore transactionMetadataStore = pulsar().getTransactionMetadataStoreService().getStores().get(TransactionCoordinatorID.get(coordinatorId));
if (transactionMetadataStore == null) {
asyncResponse.resume(new RestException(NOT_FOUND, "Transaction coordinator not found! coordinator id : " + coordinatorId));
return;
}
asyncResponse.resume(transactionMetadataStore.getCoordinatorStats());
} else {
getPartitionedTopicMetadataAsync(TopicName.TRANSACTION_COORDINATOR_ASSIGN, false, false).thenAccept(partitionMetadata -> {
if (partitionMetadata.partitions == 0) {
asyncResponse.resume(new RestException(Response.Status.NOT_FOUND, "Transaction coordinator not found"));
return;
}
List<CompletableFuture<TransactionCoordinatorStats>> transactionMetadataStoreInfoFutures = Lists.newArrayList();
for (int i = 0; i < partitionMetadata.partitions; i++) {
try {
transactionMetadataStoreInfoFutures.add(pulsar().getAdminClient().transactions().getCoordinatorStatsByIdAsync(i));
} catch (PulsarServerException e) {
asyncResponse.resume(new RestException(e));
return;
}
}
Map<Integer, TransactionCoordinatorStats> stats = new HashMap<>();
FutureUtil.waitForAll(transactionMetadataStoreInfoFutures).whenComplete((result, e) -> {
if (e != null) {
asyncResponse.resume(new RestException(e));
return;
}
for (int i = 0; i < transactionMetadataStoreInfoFutures.size(); i++) {
try {
stats.put(i, transactionMetadataStoreInfoFutures.get(i).get());
} catch (Exception exception) {
asyncResponse.resume(new RestException(exception.getCause()));
return;
}
}
asyncResponse.resume(stats);
});
}).exceptionally(ex -> {
log.error("[{}] Failed to get transaction coordinator state.", clientAppId(), ex);
resumeAsyncResponseExceptionally(asyncResponse, ex);
return null;
});
}
} else {
asyncResponse.resume(new RestException(SERVICE_UNAVAILABLE, "This Broker is not configured with transactionCoordinatorEnabled=true."));
}
}
use of org.apache.pulsar.transaction.coordinator.TransactionMetadataStore in project pulsar by apache.
the class TransactionsBase method internalGetSlowTransactions.
protected void internalGetSlowTransactions(AsyncResponse asyncResponse, boolean authoritative, long timeout, Integer coordinatorId) {
try {
if (pulsar().getConfig().isTransactionCoordinatorEnabled()) {
if (coordinatorId != null) {
validateTopicOwnership(TopicName.TRANSACTION_COORDINATOR_ASSIGN.getPartition(coordinatorId), authoritative);
TransactionMetadataStore transactionMetadataStore = pulsar().getTransactionMetadataStoreService().getStores().get(TransactionCoordinatorID.get(coordinatorId));
if (transactionMetadataStore == null) {
asyncResponse.resume(new RestException(NOT_FOUND, "Transaction coordinator not found! coordinator id : " + coordinatorId));
return;
}
List<TxnMeta> transactions = transactionMetadataStore.getSlowTransactions(timeout);
List<CompletableFuture<TransactionMetadata>> completableFutures = new ArrayList<>();
for (TxnMeta txnMeta : transactions) {
CompletableFuture<TransactionMetadata> completableFuture = new CompletableFuture<>();
getTransactionMetadata(txnMeta, completableFuture);
completableFutures.add(completableFuture);
}
FutureUtil.waitForAll(completableFutures).whenComplete((v, e) -> {
if (e != null) {
asyncResponse.resume(new RestException(e.getCause()));
return;
}
Map<String, TransactionMetadata> transactionMetadata = new HashMap<>();
for (CompletableFuture<TransactionMetadata> future : completableFutures) {
try {
transactionMetadata.put(future.get().txnId, future.get());
} catch (Exception exception) {
asyncResponse.resume(new RestException(exception.getCause()));
return;
}
}
asyncResponse.resume(transactionMetadata);
});
} else {
getPartitionedTopicMetadataAsync(TopicName.TRANSACTION_COORDINATOR_ASSIGN, false, false).thenAccept(partitionMetadata -> {
if (partitionMetadata.partitions == 0) {
asyncResponse.resume(new RestException(Response.Status.NOT_FOUND, "Transaction coordinator not found"));
return;
}
List<CompletableFuture<Map<String, TransactionMetadata>>> completableFutures = Lists.newArrayList();
for (int i = 0; i < partitionMetadata.partitions; i++) {
try {
completableFutures.add(pulsar().getAdminClient().transactions().getSlowTransactionsByCoordinatorIdAsync(i, timeout, TimeUnit.MILLISECONDS));
} catch (PulsarServerException e) {
asyncResponse.resume(new RestException(e));
return;
}
}
Map<String, TransactionMetadata> transactionMetadataMaps = new HashMap<>();
FutureUtil.waitForAll(completableFutures).whenComplete((result, e) -> {
if (e != null) {
asyncResponse.resume(new RestException(e));
return;
}
for (CompletableFuture<Map<String, TransactionMetadata>> transactionMetadataMap : completableFutures) {
try {
transactionMetadataMaps.putAll(transactionMetadataMap.get());
} catch (Exception exception) {
asyncResponse.resume(new RestException(exception.getCause()));
return;
}
}
asyncResponse.resume(transactionMetadataMaps);
});
}).exceptionally(ex -> {
log.error("[{}] Failed to get transaction coordinator state.", clientAppId(), ex);
resumeAsyncResponseExceptionally(asyncResponse, ex);
return null;
});
}
} else {
asyncResponse.resume(new RestException(SERVICE_UNAVAILABLE, "Broker don't support transaction!"));
}
} catch (Exception e) {
asyncResponse.resume(new RestException(e));
}
}
Aggregations