use of org.apache.pulsar.transaction.coordinator.impl.MLTransactionMetadataStore in project pulsar by apache.
the class TransactionsBase method internalGetCoordinatorInternalStats.
protected void internalGetCoordinatorInternalStats(AsyncResponse asyncResponse, boolean authoritative, boolean metadata, int coordinatorId) {
try {
if (pulsar().getConfig().isTransactionCoordinatorEnabled()) {
TopicName topicName = TopicName.TRANSACTION_COORDINATOR_ASSIGN.getPartition(coordinatorId);
validateTopicOwnership(topicName, authoritative);
TransactionMetadataStore metadataStore = pulsar().getTransactionMetadataStoreService().getStores().get(TransactionCoordinatorID.get(coordinatorId));
if (metadataStore == null) {
asyncResponse.resume(new RestException(NOT_FOUND, "Transaction coordinator not found! coordinator id : " + coordinatorId));
return;
}
if (metadataStore instanceof MLTransactionMetadataStore) {
ManagedLedger managedLedger = ((MLTransactionMetadataStore) metadataStore).getManagedLedger();
TransactionCoordinatorInternalStats transactionCoordinatorInternalStats = new TransactionCoordinatorInternalStats();
TransactionLogStats transactionLogStats = new TransactionLogStats();
transactionLogStats.managedLedgerName = managedLedger.getName();
transactionLogStats.managedLedgerInternalStats = managedLedger.getManagedLedgerInternalStats(metadata).get();
transactionCoordinatorInternalStats.transactionLogStats = transactionLogStats;
asyncResponse.resume(transactionCoordinatorInternalStats);
} else {
asyncResponse.resume(new RestException(METHOD_NOT_ALLOWED, "Broker don't use MLTransactionMetadataStore!"));
}
} else {
asyncResponse.resume(new RestException(SERVICE_UNAVAILABLE, "This Broker is not configured with transactionCoordinatorEnabled=true."));
}
} catch (Exception e) {
asyncResponse.resume(new RestException(e.getCause()));
}
}
use of org.apache.pulsar.transaction.coordinator.impl.MLTransactionMetadataStore in project pulsar by apache.
the class TransactionMetadataStoreServiceTest method testTimeoutTrackerMultiThreading.
@Test
public void testTimeoutTrackerMultiThreading() throws Exception {
pulsar.getTransactionMetadataStoreService().handleTcClientConnect(TransactionCoordinatorID.get(0));
Awaitility.await().until(() -> pulsar.getTransactionMetadataStoreService().getStores().get(TransactionCoordinatorID.get(0)) != null);
MLTransactionMetadataStore transactionMetadataStore = (MLTransactionMetadataStore) pulsar.getTransactionMetadataStoreService().getStores().get(TransactionCoordinatorID.get(0));
checkTransactionMetadataStoreReady(transactionMetadataStore);
Field field = MLTransactionMetadataStore.class.getDeclaredField("txnMetaMap");
field.setAccessible(true);
ConcurrentSkipListMap<Long, Pair<TxnMeta, List<Position>>> txnMap = (ConcurrentSkipListMap<Long, Pair<TxnMeta, List<Position>>>) field.get(transactionMetadataStore);
new Thread(() -> {
int i = -1;
while (++i < 100) {
try {
transactionMetadataStore.newTransaction(1000);
} catch (Exception e) {
// no operation
}
}
}).start();
new Thread(() -> {
int i = -1;
while (++i < 100) {
try {
transactionMetadataStore.newTransaction(2000);
} catch (Exception e) {
// no operation
}
}
}).start();
new Thread(() -> {
int i = -1;
while (++i < 100) {
try {
transactionMetadataStore.newTransaction(3000);
} catch (Exception e) {
// no operation
}
}
}).start();
new Thread(() -> {
int i = -1;
while (++i < 100) {
try {
transactionMetadataStore.newTransaction(4000);
} catch (Exception e) {
// no operation
}
}
}).start();
checkoutTimeout(txnMap, 300);
checkoutTimeout(txnMap, 200);
checkoutTimeout(txnMap, 100);
checkoutTimeout(txnMap, 0);
}
use of org.apache.pulsar.transaction.coordinator.impl.MLTransactionMetadataStore in project pulsar by apache.
the class TransactionMetadataStoreServiceTest method testAddAckedPartitionToTxn.
@Test
public void testAddAckedPartitionToTxn() throws Exception {
TransactionMetadataStoreService transactionMetadataStoreService = pulsar.getTransactionMetadataStoreService();
transactionMetadataStoreService.handleTcClientConnect(TransactionCoordinatorID.get(0)).get();
Awaitility.await().until(() -> transactionMetadataStoreService.getStores().size() == 1);
MLTransactionMetadataStore transactionMetadataStore = (MLTransactionMetadataStore) pulsar.getTransactionMetadataStoreService().getStores().get(TransactionCoordinatorID.get(0));
checkTransactionMetadataStoreReady(transactionMetadataStore);
TxnID txnID = transactionMetadataStoreService.newTransaction(TransactionCoordinatorID.get(0), 5000).get();
List<TransactionSubscription> partitions = new ArrayList<>();
partitions.add(TransactionSubscription.builder().topic("ptn-1").subscription("sub-1").build());
partitions.add(TransactionSubscription.builder().topic("ptn-2").subscription("sub-1").build());
partitions.add(TransactionSubscription.builder().topic("ptn-3").subscription("sub-1").build());
transactionMetadataStoreService.addAckedPartitionToTxn(txnID, partitions);
TxnMeta txn = transactionMetadataStoreService.getTxnMeta(txnID).get();
assertEquals(txn.status(), TxnStatus.OPEN);
transactionMetadataStoreService.removeTransactionMetadataStore(TransactionCoordinatorID.get(0));
Assert.assertEquals(transactionMetadataStoreService.getStores().size(), 0);
}
use of org.apache.pulsar.transaction.coordinator.impl.MLTransactionMetadataStore in project pulsar by apache.
the class MLTransactionMetadataStoreTest method testRecoverSequenceId.
@Test(dataProvider = "isUseManagedLedgerProperties")
public void testRecoverSequenceId(boolean isUseManagedLedgerProperties) throws Exception {
ManagedLedgerFactoryConfig factoryConf = new ManagedLedgerFactoryConfig();
factoryConf.setMaxCacheSize(0);
@Cleanup("shutdown") ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(metadataStore, bkc, factoryConf);
TransactionCoordinatorID transactionCoordinatorID = new TransactionCoordinatorID(1);
ManagedLedgerConfig managedLedgerConfig = new ManagedLedgerConfig();
MLTransactionSequenceIdGenerator mlTransactionSequenceIdGenerator = new MLTransactionSequenceIdGenerator();
managedLedgerConfig.setManagedLedgerInterceptor(mlTransactionSequenceIdGenerator);
managedLedgerConfig.setMaxEntriesPerLedger(3);
MLTransactionLogImpl mlTransactionLog = new MLTransactionLogImpl(transactionCoordinatorID, factory, managedLedgerConfig);
mlTransactionLog.initialize().join();
MLTransactionMetadataStore transactionMetadataStore = new MLTransactionMetadataStore(transactionCoordinatorID, mlTransactionLog, new TransactionTimeoutTrackerImpl(), new TransactionRecoverTrackerImpl(), mlTransactionSequenceIdGenerator);
Awaitility.await().until(transactionMetadataStore::checkIfReady);
TxnID txnID = transactionMetadataStore.newTransaction(20000).get();
transactionMetadataStore.updateTxnStatus(txnID, TxnStatus.COMMITTING, TxnStatus.OPEN, false).get();
if (isUseManagedLedgerProperties) {
transactionMetadataStore.updateTxnStatus(txnID, TxnStatus.COMMITTED, TxnStatus.COMMITTING, false).get();
}
assertEquals(txnID.getLeastSigBits(), 0);
Field field = MLTransactionLogImpl.class.getDeclaredField("managedLedger");
field.setAccessible(true);
ManagedLedgerImpl managedLedger = (ManagedLedgerImpl) field.get(mlTransactionLog);
Position position = managedLedger.getLastConfirmedEntry();
if (isUseManagedLedgerProperties) {
Awaitility.await().until(() -> {
managedLedger.rollCurrentLedgerIfFull();
return !managedLedger.ledgerExists(position.getLedgerId());
});
}
mlTransactionLog.closeAsync().get();
mlTransactionLog = new MLTransactionLogImpl(transactionCoordinatorID, factory, managedLedgerConfig);
mlTransactionLog.initialize().join();
transactionMetadataStore = new MLTransactionMetadataStore(transactionCoordinatorID, mlTransactionLog, new TransactionTimeoutTrackerImpl(), new TransactionRecoverTrackerImpl(), mlTransactionSequenceIdGenerator);
Awaitility.await().until(transactionMetadataStore::checkIfReady);
txnID = transactionMetadataStore.newTransaction(100000).get();
assertEquals(txnID.getLeastSigBits(), 1);
}
use of org.apache.pulsar.transaction.coordinator.impl.MLTransactionMetadataStore in project pulsar by apache.
the class MLTransactionMetadataStoreTest method testManageLedgerWriteFailState.
@Test
public void testManageLedgerWriteFailState() throws Exception {
ManagedLedgerFactoryConfig factoryConf = new ManagedLedgerFactoryConfig();
factoryConf.setMaxCacheSize(0);
@Cleanup("shutdown") ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(metadataStore, bkc, factoryConf);
TransactionCoordinatorID transactionCoordinatorID = new TransactionCoordinatorID(1);
ManagedLedgerConfig managedLedgerConfig = new ManagedLedgerConfig();
MLTransactionSequenceIdGenerator mlTransactionSequenceIdGenerator = new MLTransactionSequenceIdGenerator();
managedLedgerConfig.setManagedLedgerInterceptor(mlTransactionSequenceIdGenerator);
MLTransactionLogImpl mlTransactionLog = new MLTransactionLogImpl(transactionCoordinatorID, factory, managedLedgerConfig);
mlTransactionLog.initialize().join();
MLTransactionMetadataStore transactionMetadataStore = new MLTransactionMetadataStore(transactionCoordinatorID, mlTransactionLog, new TransactionTimeoutTrackerImpl(), new TransactionRecoverTrackerImpl(), mlTransactionSequenceIdGenerator);
Awaitility.await().until(transactionMetadataStore::checkIfReady);
transactionMetadataStore.newTransaction(5000).get();
Field field = MLTransactionLogImpl.class.getDeclaredField("managedLedger");
field.setAccessible(true);
ManagedLedgerImpl managedLedger = (ManagedLedgerImpl) field.get(mlTransactionLog);
field = ManagedLedgerImpl.class.getDeclaredField("STATE_UPDATER");
field.setAccessible(true);
AtomicReferenceFieldUpdater state = (AtomicReferenceFieldUpdater) field.get(managedLedger);
state.set(managedLedger, WriteFailed);
try {
transactionMetadataStore.newTransaction(5000).get();
fail();
} catch (ExecutionException e) {
assertTrue(e.getCause() instanceof ManagedLedgerException.ManagedLedgerAlreadyClosedException);
}
transactionMetadataStore.newTransaction(5000).get();
}
Aggregations