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();
}
}
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();
}
}
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();
}
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);
}
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);
}
Aggregations