Search in sources :

Example 11 with TxnMeta

use of org.apache.pulsar.transaction.coordinator.TxnMeta in project pulsar by yahoo.

the class TransactionMetadataStoreServiceTest method testTimeoutTracker.

@Test
public void testTimeoutTracker() 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);
    int i = -1;
    while (++i < 1000) {
        try {
            transactionMetadataStore.newTransaction(2000).get();
        } catch (Exception e) {
        // no operation
        }
    }
    txnMap.forEach((txnID, txnMetaListPair) -> Assert.assertEquals(txnMetaListPair.getLeft().status(), TxnStatus.OPEN));
    Awaitility.await().atLeast(1000, TimeUnit.MICROSECONDS).until(() -> txnMap.size() == 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 12 with TxnMeta

use of org.apache.pulsar.transaction.coordinator.TxnMeta in project pulsar by yahoo.

the class TransactionMetadataStoreServiceTest method transactionTimeoutRecoverTest.

@Test
public void transactionTimeoutRecoverTest() throws Exception {
    int timeout = 2000;
    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);
    transactionMetadataStore.newTransaction(timeout);
    pulsar.getTransactionMetadataStoreService().removeTransactionMetadataStore(TransactionCoordinatorID.get(0));
    pulsar.getTransactionMetadataStoreService().handleTcClientConnect(TransactionCoordinatorID.get(0));
    Awaitility.await().until(() -> pulsar.getTransactionMetadataStoreService().getStores().get(TransactionCoordinatorID.get(0)) != null);
    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);
    Awaitility.await().until(() -> txnMap.size() == 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) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.testng.annotations.Test)

Example 13 with TxnMeta

use of org.apache.pulsar.transaction.coordinator.TxnMeta in project pulsar by yahoo.

the class TransactionMetadataStoreServiceTest method testEndTransactionOpRetry.

@Test(dataProvider = "txnStatus")
public void testEndTransactionOpRetry(TxnStatus txnStatus) throws Exception {
    int timeOut = 3000;
    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);
    TxnID txnID = transactionMetadataStore.newTransaction(timeOut - 2000).get();
    TxnMeta txnMeta = transactionMetadataStore.getTxnMeta(txnID).get();
    txnMeta.updateTxnStatus(txnStatus, TxnStatus.OPEN);
    Field field = TransactionMetadataStoreState.class.getDeclaredField("state");
    field.setAccessible(true);
    field.set(transactionMetadataStore, TransactionMetadataStoreState.State.None);
    CompletableFuture<Void> completableFuture = null;
    try {
        completableFuture = pulsar.getTransactionMetadataStoreService().endTransaction(txnID, TxnAction.COMMIT.getValue(), false);
        completableFuture.get(5, TimeUnit.SECONDS);
        fail();
    } catch (Exception e) {
        if (txnStatus == TxnStatus.OPEN || txnStatus == TxnStatus.COMMITTING) {
            assertTrue(e instanceof TimeoutException);
        } else if (txnStatus == TxnStatus.ABORTING) {
            assertTrue(e.getCause() instanceof CoordinatorException.InvalidTxnStatusException);
        } else {
            fail();
        }
    }
    assertEquals(txnMeta.status(), txnStatus);
    field = TransactionMetadataStoreState.class.getDeclaredField("state");
    field.setAccessible(true);
    field.set(transactionMetadataStore, TransactionMetadataStoreState.State.Ready);
    if (txnStatus == TxnStatus.ABORTING) {
        pulsar.getTransactionMetadataStoreService().endTransaction(txnID, TxnAction.ABORT.getValue(), false).get();
    }
    Awaitility.await().atMost(timeOut, TimeUnit.MILLISECONDS).until(() -> {
        try {
            transactionMetadataStore.getTxnMeta(txnID).get();
            return false;
        } catch (ExecutionException e) {
            return e.getCause() instanceof CoordinatorException.TransactionNotFoundException;
        }
    });
}
Also used : 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) Field(java.lang.reflect.Field) TxnID(org.apache.pulsar.client.api.transaction.TxnID) CoordinatorException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException) ExecutionException(java.util.concurrent.ExecutionException) TransactionMetadataStoreState(org.apache.pulsar.transaction.coordinator.TransactionMetadataStoreState) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test)

Example 14 with TxnMeta

use of org.apache.pulsar.transaction.coordinator.TxnMeta in project pulsar by yahoo.

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 15 with TxnMeta

use of org.apache.pulsar.transaction.coordinator.TxnMeta in project incubator-pulsar by apache.

the class TransactionMetadataStoreServiceTest method transactionTimeoutRecoverTest.

@Test
public void transactionTimeoutRecoverTest() throws Exception {
    int timeout = 2000;
    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);
    transactionMetadataStore.newTransaction(timeout);
    pulsar.getTransactionMetadataStoreService().removeTransactionMetadataStore(TransactionCoordinatorID.get(0));
    pulsar.getTransactionMetadataStoreService().handleTcClientConnect(TransactionCoordinatorID.get(0));
    Awaitility.await().until(() -> pulsar.getTransactionMetadataStoreService().getStores().get(TransactionCoordinatorID.get(0)) != null);
    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);
    Awaitility.await().until(() -> txnMap.size() == 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) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.testng.annotations.Test)

Aggregations

TxnMeta (org.apache.pulsar.transaction.coordinator.TxnMeta)42 ArrayList (java.util.ArrayList)31 List (java.util.List)27 MLTransactionMetadataStore (org.apache.pulsar.transaction.coordinator.impl.MLTransactionMetadataStore)24 CompletableFuture (java.util.concurrent.CompletableFuture)21 TxnID (org.apache.pulsar.client.api.transaction.TxnID)21 Test (org.testng.annotations.Test)21 Position (org.apache.bookkeeper.mledger.Position)18 Field (java.lang.reflect.Field)15 ExecutionException (java.util.concurrent.ExecutionException)15 Pair (org.apache.commons.lang3.tuple.Pair)15 TransactionNotFoundException (org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException.TransactionNotFoundException)15 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)12 CoordinatorException (org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException)12 TimeoutException (java.util.concurrent.TimeoutException)9 CoordinatorNotFoundException (org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException.CoordinatorNotFoundException)9 Map (java.util.Map)6 TimeUnit (java.util.concurrent.TimeUnit)6 TransactionMetadataStoreService (org.apache.pulsar.broker.TransactionMetadataStoreService)6 FutureUtil (org.apache.pulsar.common.util.FutureUtil)6