Search in sources :

Example 1 with TransactionImpl

use of org.apache.pulsar.client.impl.transaction.TransactionImpl in project pulsar by apache.

the class TransactionLowWaterMarkTest method testTransactionBufferLowWaterMark.

@Test
public void testTransactionBufferLowWaterMark() throws Exception {
    Transaction txn = pulsarClient.newTransaction().withTransactionTimeout(5, TimeUnit.SECONDS).build().get();
    Producer<byte[]> producer = pulsarClient.newProducer().topic(TOPIC).sendTimeout(0, TimeUnit.SECONDS).enableBatching(false).create();
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(TOPIC).subscriptionName("test").subscriptionInitialPosition(SubscriptionInitialPosition.Earliest).enableBatchIndexAcknowledgment(true).subscriptionType(SubscriptionType.Failover).subscribe();
    final String TEST1 = "test1";
    final String TEST2 = "test2";
    final String TEST3 = "test3";
    producer.newMessage(txn).value(TEST1.getBytes()).send();
    txn.commit().get();
    Message<byte[]> message = consumer.receive(2, TimeUnit.SECONDS);
    assertEquals(new String(message.getData()), TEST1);
    message = consumer.receive(2, TimeUnit.SECONDS);
    assertNull(message);
    Field field = TransactionImpl.class.getDeclaredField("state");
    field.setAccessible(true);
    field.set(txn, TransactionImpl.State.OPEN);
    producer.newMessage(txn).value(TEST2.getBytes()).send();
    try {
        txn.commit().get();
        Assert.fail("The commit operation should be failed.");
    } catch (Exception e) {
        Assert.assertTrue(e.getCause() instanceof TransactionCoordinatorClientException.TransactionNotFoundException);
    }
    PartitionedTopicMetadata partitionedTopicMetadata = ((PulsarClientImpl) pulsarClient).getLookup().getPartitionedTopicMetadata(TopicName.TRANSACTION_COORDINATOR_ASSIGN).get();
    Transaction lowWaterMarkTxn = null;
    for (int i = 0; i < partitionedTopicMetadata.partitions; i++) {
        lowWaterMarkTxn = pulsarClient.newTransaction().withTransactionTimeout(5, TimeUnit.SECONDS).build().get();
        if (((TransactionImpl) lowWaterMarkTxn).getTxnIdMostBits() == ((TransactionImpl) txn).getTxnIdMostBits()) {
            break;
        }
    }
    if (lowWaterMarkTxn != null && ((TransactionImpl) lowWaterMarkTxn).getTxnIdMostBits() == ((TransactionImpl) txn).getTxnIdMostBits()) {
        producer.newMessage(lowWaterMarkTxn).value(TEST3.getBytes()).send();
        message = consumer.receive(2, TimeUnit.SECONDS);
        assertNull(message);
        lowWaterMarkTxn.commit().get();
        message = consumer.receive();
        assertEquals(new String(message.getData()), TEST3);
    } else {
        fail();
    }
}
Also used : Field(java.lang.reflect.Field) Transaction(org.apache.pulsar.client.api.transaction.Transaction) PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl) TransactionImpl(org.apache.pulsar.client.impl.transaction.TransactionImpl) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) TransactionCoordinatorClientException(org.apache.pulsar.client.api.transaction.TransactionCoordinatorClientException) Test(org.testng.annotations.Test)

Example 2 with TransactionImpl

use of org.apache.pulsar.client.impl.transaction.TransactionImpl in project pulsar by apache.

the class TransactionLowWaterMarkTest method testPendingAckLowWaterMark.

@Test
public void testPendingAckLowWaterMark() throws Exception {
    String subName = "test";
    Transaction txn = pulsarClient.newTransaction().withTransactionTimeout(5, TimeUnit.SECONDS).build().get();
    @Cleanup Producer<byte[]> producer = pulsarClient.newProducer().topic(TOPIC).sendTimeout(0, TimeUnit.SECONDS).enableBatching(false).create();
    @Cleanup Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(TOPIC).subscriptionName(subName).subscriptionInitialPosition(SubscriptionInitialPosition.Earliest).enableBatchIndexAcknowledgment(true).subscriptionType(SubscriptionType.Failover).subscribe();
    final String TEST1 = "test1";
    final String TEST2 = "test2";
    final String TEST3 = "test3";
    producer.send(TEST1.getBytes());
    producer.send(TEST2.getBytes());
    producer.send(TEST3.getBytes());
    Message<byte[]> message = consumer.receive(2, TimeUnit.SECONDS);
    assertEquals(new String(message.getData()), TEST1);
    consumer.acknowledgeAsync(message.getMessageId(), txn).get();
    LinkedMap<TxnID, HashMap<PositionImpl, PositionImpl>> individualAckOfTransaction = null;
    for (int i = 0; i < getPulsarServiceList().size(); i++) {
        Field field = BrokerService.class.getDeclaredField("topics");
        field.setAccessible(true);
        ConcurrentOpenHashMap<String, CompletableFuture<Optional<Topic>>> topics = (ConcurrentOpenHashMap<String, CompletableFuture<Optional<Topic>>>) field.get(getPulsarServiceList().get(i).getBrokerService());
        CompletableFuture<Optional<Topic>> completableFuture = topics.get("persistent://" + TOPIC);
        if (completableFuture != null) {
            Optional<Topic> topic = completableFuture.get();
            if (topic.isPresent()) {
                PersistentSubscription persistentSubscription = (PersistentSubscription) topic.get().getSubscription(subName);
                field = PersistentSubscription.class.getDeclaredField("pendingAckHandle");
                field.setAccessible(true);
                PendingAckHandleImpl pendingAckHandle = (PendingAckHandleImpl) field.get(persistentSubscription);
                field = PendingAckHandleImpl.class.getDeclaredField("individualAckOfTransaction");
                field.setAccessible(true);
                individualAckOfTransaction = (LinkedMap<TxnID, HashMap<PositionImpl, PositionImpl>>) field.get(pendingAckHandle);
            }
        }
    }
    assertTrue(individualAckOfTransaction.containsKey(new TxnID(((TransactionImpl) txn).getTxnIdMostBits(), ((TransactionImpl) txn).getTxnIdLeastBits())));
    txn.commit().get();
    Field field = TransactionImpl.class.getDeclaredField("state");
    field.setAccessible(true);
    field.set(txn, TransactionImpl.State.OPEN);
    assertFalse(individualAckOfTransaction.containsKey(new TxnID(((TransactionImpl) txn).getTxnIdMostBits(), ((TransactionImpl) txn).getTxnIdLeastBits())));
    message = consumer.receive();
    assertEquals(new String(message.getData()), TEST2);
    consumer.acknowledgeAsync(message.getMessageId(), txn).get();
    assertTrue(individualAckOfTransaction.containsKey(new TxnID(((TransactionImpl) txn).getTxnIdMostBits(), ((TransactionImpl) txn).getTxnIdLeastBits())));
    PartitionedTopicMetadata partitionedTopicMetadata = ((PulsarClientImpl) pulsarClient).getLookup().getPartitionedTopicMetadata(TopicName.TRANSACTION_COORDINATOR_ASSIGN).get();
    Transaction lowWaterMarkTxn = null;
    for (int i = 0; i < partitionedTopicMetadata.partitions; i++) {
        lowWaterMarkTxn = pulsarClient.newTransaction().withTransactionTimeout(5, TimeUnit.SECONDS).build().get();
        if (((TransactionImpl) lowWaterMarkTxn).getTxnIdMostBits() == ((TransactionImpl) txn).getTxnIdMostBits()) {
            break;
        }
    }
    if (lowWaterMarkTxn != null && ((TransactionImpl) lowWaterMarkTxn).getTxnIdMostBits() == ((TransactionImpl) txn).getTxnIdMostBits()) {
        producer.newMessage(lowWaterMarkTxn).value(TEST3.getBytes()).send();
        message = consumer.receive(2, TimeUnit.SECONDS);
        assertEquals(new String(message.getData()), TEST3);
        consumer.acknowledgeAsync(message.getMessageId(), lowWaterMarkTxn).get();
        assertTrue(individualAckOfTransaction.containsKey(new TxnID(((TransactionImpl) txn).getTxnIdMostBits(), ((TransactionImpl) txn).getTxnIdLeastBits())));
        assertTrue(individualAckOfTransaction.containsKey(new TxnID(((TransactionImpl) lowWaterMarkTxn).getTxnIdMostBits(), ((TransactionImpl) lowWaterMarkTxn).getTxnIdLeastBits())));
        lowWaterMarkTxn.commit().get();
        assertFalse(individualAckOfTransaction.containsKey(new TxnID(((TransactionImpl) txn).getTxnIdMostBits(), ((TransactionImpl) txn).getTxnIdLeastBits())));
        assertFalse(individualAckOfTransaction.containsKey(new TxnID(((TransactionImpl) lowWaterMarkTxn).getTxnIdMostBits(), ((TransactionImpl) lowWaterMarkTxn).getTxnIdLeastBits())));
    } else {
        fail();
    }
}
Also used : ConcurrentOpenHashMap(org.apache.pulsar.common.util.collections.ConcurrentOpenHashMap) Optional(java.util.Optional) HashMap(java.util.HashMap) ConcurrentOpenHashMap(org.apache.pulsar.common.util.collections.ConcurrentOpenHashMap) PendingAckHandleImpl(org.apache.pulsar.broker.transaction.pendingack.impl.PendingAckHandleImpl) TransactionImpl(org.apache.pulsar.client.impl.transaction.TransactionImpl) Cleanup(lombok.Cleanup) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription) Field(java.lang.reflect.Field) TxnID(org.apache.pulsar.client.api.transaction.TxnID) CompletableFuture(java.util.concurrent.CompletableFuture) Transaction(org.apache.pulsar.client.api.transaction.Transaction) PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl) Topic(org.apache.pulsar.broker.service.Topic) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) Test(org.testng.annotations.Test)

Example 3 with TransactionImpl

use of org.apache.pulsar.client.impl.transaction.TransactionImpl in project pulsar by apache.

the class TransactionTest method testEndTxnWhenCommittingOrAborting.

@Test
public void testEndTxnWhenCommittingOrAborting() throws Exception {
    Transaction commitTxn = pulsarClient.newTransaction().withTransactionTimeout(5, TimeUnit.SECONDS).build().get();
    Transaction abortTxn = pulsarClient.newTransaction().withTransactionTimeout(5, TimeUnit.SECONDS).build().get();
    Class<TransactionImpl> transactionClass = TransactionImpl.class;
    Field field = transactionClass.getDeclaredField("state");
    field.setAccessible(true);
    field.set(commitTxn, TransactionImpl.State.COMMITTING);
    field.set(abortTxn, TransactionImpl.State.ABORTING);
    abortTxn.abort();
    commitTxn.commit();
}
Also used : Field(java.lang.reflect.Field) Transaction(org.apache.pulsar.client.api.transaction.Transaction) TransactionImpl(org.apache.pulsar.client.impl.transaction.TransactionImpl) Test(org.testng.annotations.Test)

Example 4 with TransactionImpl

use of org.apache.pulsar.client.impl.transaction.TransactionImpl in project pulsar by apache.

the class AdminApiTransactionTest method testGetTransactionInBufferStats.

@Test(timeOut = 20000)
public void testGetTransactionInBufferStats() throws Exception {
    initTransaction(2);
    TransactionImpl transaction = (TransactionImpl) getTransaction();
    final String topic = "persistent://public/default/testGetTransactionInBufferStats";
    admin.topics().createNonPartitionedTopic(topic);
    Producer<byte[]> producer = pulsarClient.newProducer(Schema.BYTES).topic(topic).sendTimeout(0, TimeUnit.SECONDS).create();
    MessageId messageId = producer.newMessage(transaction).value("Hello pulsar!".getBytes()).send();
    TransactionInBufferStats transactionInBufferStats = admin.transactions().getTransactionInBufferStatsAsync(new TxnID(transaction.getTxnIdMostBits(), transaction.getTxnIdLeastBits()), topic).get();
    PositionImpl position = PositionImpl.get(((MessageIdImpl) messageId).getLedgerId(), ((MessageIdImpl) messageId).getEntryId());
    assertEquals(transactionInBufferStats.startPosition, position.toString());
    assertFalse(transactionInBufferStats.aborted);
    transaction.abort().get();
    transactionInBufferStats = admin.transactions().getTransactionInBufferStatsAsync(new TxnID(transaction.getTxnIdMostBits(), transaction.getTxnIdLeastBits()), topic).get();
    assertNull(transactionInBufferStats.startPosition);
    assertTrue(transactionInBufferStats.aborted);
}
Also used : TxnID(org.apache.pulsar.client.api.transaction.TxnID) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) TransactionImpl(org.apache.pulsar.client.impl.transaction.TransactionImpl) TransactionInBufferStats(org.apache.pulsar.common.policies.data.TransactionInBufferStats) MessageId(org.apache.pulsar.client.api.MessageId) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 5 with TransactionImpl

use of org.apache.pulsar.client.impl.transaction.TransactionImpl in project pulsar by apache.

the class AdminApiTransactionTest method testGetSlowTransactions.

@Test(timeOut = 20000)
public void testGetSlowTransactions() throws Exception {
    initTransaction(2);
    TransactionImpl transaction1 = (TransactionImpl) pulsarClient.newTransaction().withTransactionTimeout(60, TimeUnit.SECONDS).build().get();
    TransactionImpl transaction2 = (TransactionImpl) pulsarClient.newTransaction().withTransactionTimeout(60, TimeUnit.SECONDS).build().get();
    pulsarClient.newTransaction().withTransactionTimeout(20, TimeUnit.SECONDS).build();
    pulsarClient.newTransaction().withTransactionTimeout(20, TimeUnit.SECONDS).build();
    Map<String, TransactionMetadata> transactionMetadataMap = admin.transactions().getSlowTransactionsAsync(30, TimeUnit.SECONDS).get();
    assertEquals(transactionMetadataMap.size(), 2);
    TxnID txnID1 = new TxnID(transaction1.getTxnIdMostBits(), transaction1.getTxnIdLeastBits());
    TxnID txnID2 = new TxnID(transaction2.getTxnIdMostBits(), transaction2.getTxnIdLeastBits());
    TransactionMetadata transactionMetadata = transactionMetadataMap.get(txnID1.toString());
    assertNotNull(transactionMetadata);
    assertEquals(transactionMetadata.timeoutAt, 60000);
    transactionMetadata = transactionMetadataMap.get(txnID2.toString());
    assertNotNull(transactionMetadata);
    assertEquals(transactionMetadata.timeoutAt, 60000);
}
Also used : TxnID(org.apache.pulsar.client.api.transaction.TxnID) TransactionImpl(org.apache.pulsar.client.impl.transaction.TransactionImpl) TransactionMetadata(org.apache.pulsar.common.policies.data.TransactionMetadata) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Aggregations

TransactionImpl (org.apache.pulsar.client.impl.transaction.TransactionImpl)60 Test (org.testng.annotations.Test)47 TxnID (org.apache.pulsar.client.api.transaction.TxnID)28 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)21 MessageId (org.apache.pulsar.client.api.MessageId)21 Cleanup (lombok.Cleanup)20 Transaction (org.apache.pulsar.client.api.transaction.Transaction)18 ExecutionException (java.util.concurrent.ExecutionException)13 Field (java.lang.reflect.Field)12 ArrayList (java.util.ArrayList)12 CompletableFuture (java.util.concurrent.CompletableFuture)12 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)10 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)10 PartitionedTopicMetadata (org.apache.pulsar.common.partition.PartitionedTopicMetadata)9 PulsarClient (org.apache.pulsar.client.api.PulsarClient)8 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)6 Map (java.util.Map)6 Optional (java.util.Optional)6 TransactionCoordinatorClientException (org.apache.pulsar.client.api.transaction.TransactionCoordinatorClientException)6