Search in sources :

Example 1 with MLTransactionMetadataStore

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()));
    }
}
Also used : TransactionCoordinatorInternalStats(org.apache.pulsar.common.policies.data.TransactionCoordinatorInternalStats) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) RestException(org.apache.pulsar.broker.web.RestException) TransactionLogStats(org.apache.pulsar.common.policies.data.TransactionLogStats) TransactionMetadataStore(org.apache.pulsar.transaction.coordinator.TransactionMetadataStore) MLTransactionMetadataStore(org.apache.pulsar.transaction.coordinator.impl.MLTransactionMetadataStore) MLTransactionMetadataStore(org.apache.pulsar.transaction.coordinator.impl.MLTransactionMetadataStore) ServiceUnitNotReadyException(org.apache.pulsar.broker.service.BrokerServiceException.ServiceUnitNotReadyException) NotAllowedException(org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException) RestException(org.apache.pulsar.broker.web.RestException) CoordinatorNotFoundException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException.CoordinatorNotFoundException) SubscriptionNotFoundException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionNotFoundException) TransactionNotFoundException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException.TransactionNotFoundException) ExecutionException(java.util.concurrent.ExecutionException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) TopicName(org.apache.pulsar.common.naming.TopicName)

Example 2 with MLTransactionMetadataStore

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);
}
Also used : Field(java.lang.reflect.Field) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) Position(org.apache.bookkeeper.mledger.Position) ArrayList(java.util.ArrayList) List(java.util.List) TxnMeta(org.apache.pulsar.transaction.coordinator.TxnMeta) MLTransactionMetadataStore(org.apache.pulsar.transaction.coordinator.impl.MLTransactionMetadataStore) TimeoutException(java.util.concurrent.TimeoutException) CoordinatorException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException) ExecutionException(java.util.concurrent.ExecutionException) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.testng.annotations.Test)

Example 3 with MLTransactionMetadataStore

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);
}
Also used : TxnID(org.apache.pulsar.client.api.transaction.TxnID) TransactionSubscription(org.apache.pulsar.transaction.coordinator.TransactionSubscription) ArrayList(java.util.ArrayList) TxnMeta(org.apache.pulsar.transaction.coordinator.TxnMeta) MLTransactionMetadataStore(org.apache.pulsar.transaction.coordinator.impl.MLTransactionMetadataStore) TransactionMetadataStoreService(org.apache.pulsar.broker.TransactionMetadataStoreService) Test(org.testng.annotations.Test)

Example 4 with MLTransactionMetadataStore

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);
}
Also used : ManagedLedgerFactoryConfig(org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig) Position(org.apache.bookkeeper.mledger.Position) MLTransactionSequenceIdGenerator(org.apache.pulsar.transaction.coordinator.impl.MLTransactionSequenceIdGenerator) Cleanup(lombok.Cleanup) MLTransactionMetadataStore(org.apache.pulsar.transaction.coordinator.impl.MLTransactionMetadataStore) MLTransactionLogImpl(org.apache.pulsar.transaction.coordinator.impl.MLTransactionLogImpl) Field(java.lang.reflect.Field) ManagedLedgerImpl(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl) TxnID(org.apache.pulsar.client.api.transaction.TxnID) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) ManagedLedgerFactoryImpl(org.apache.bookkeeper.mledger.impl.ManagedLedgerFactoryImpl) Test(org.testng.annotations.Test)

Example 5 with MLTransactionMetadataStore

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();
}
Also used : ManagedLedgerFactoryConfig(org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig) MLTransactionSequenceIdGenerator(org.apache.pulsar.transaction.coordinator.impl.MLTransactionSequenceIdGenerator) Cleanup(lombok.Cleanup) MLTransactionMetadataStore(org.apache.pulsar.transaction.coordinator.impl.MLTransactionMetadataStore) MLTransactionLogImpl(org.apache.pulsar.transaction.coordinator.impl.MLTransactionLogImpl) AtomicReferenceFieldUpdater(java.util.concurrent.atomic.AtomicReferenceFieldUpdater) Field(java.lang.reflect.Field) ManagedLedgerImpl(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) ExecutionException(java.util.concurrent.ExecutionException) ManagedLedgerFactoryImpl(org.apache.bookkeeper.mledger.impl.ManagedLedgerFactoryImpl) Test(org.testng.annotations.Test)

Aggregations

MLTransactionMetadataStore (org.apache.pulsar.transaction.coordinator.impl.MLTransactionMetadataStore)17 Test (org.testng.annotations.Test)14 Field (java.lang.reflect.Field)9 ArrayList (java.util.ArrayList)9 TxnID (org.apache.pulsar.client.api.transaction.TxnID)8 ExecutionException (java.util.concurrent.ExecutionException)7 TxnMeta (org.apache.pulsar.transaction.coordinator.TxnMeta)7 MLTransactionLogImpl (org.apache.pulsar.transaction.coordinator.impl.MLTransactionLogImpl)7 MLTransactionSequenceIdGenerator (org.apache.pulsar.transaction.coordinator.impl.MLTransactionSequenceIdGenerator)7 Cleanup (lombok.Cleanup)6 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)6 ManagedLedgerFactory (org.apache.bookkeeper.mledger.ManagedLedgerFactory)6 ManagedLedgerFactoryConfig (org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig)6 ManagedLedgerFactoryImpl (org.apache.bookkeeper.mledger.impl.ManagedLedgerFactoryImpl)6 Position (org.apache.bookkeeper.mledger.Position)5 List (java.util.List)4 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)4 Pair (org.apache.commons.lang3.tuple.Pair)4 TimeoutException (java.util.concurrent.TimeoutException)3 CoordinatorException (org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException)3