use of org.apache.pulsar.transaction.coordinator.TransactionSubscription 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.TransactionSubscription in project pulsar by apache.
the class MLTransactionMetadataStore method subscriptionToTxnSubscription.
public static List<TransactionSubscription> subscriptionToTxnSubscription(List<Subscription> subscriptions) {
List<TransactionSubscription> transactionSubscriptions = new ArrayList<>(subscriptions.size());
for (Subscription subscription : subscriptions) {
TransactionSubscription.TransactionSubscriptionBuilder transactionSubscriptionBuilder = TransactionSubscription.builder();
transactionSubscriptionBuilder.subscription(subscription.getSubscription());
transactionSubscriptionBuilder.topic(subscription.getTopic());
transactionSubscriptions.add(transactionSubscriptionBuilder.build());
}
return transactionSubscriptions;
}
use of org.apache.pulsar.transaction.coordinator.TransactionSubscription in project pulsar by yahoo.
the class TransactionEndToEndTest method oneTransactionOneTopicWithMultiSubTest.
@Test
public void oneTransactionOneTopicWithMultiSubTest() throws Exception {
String topic = NAMESPACE1 + "/oneTransactionOneTopicWithMultiSubTest";
final String subName1 = "test1";
final String subName2 = "test2";
@Cleanup Consumer<byte[]> consumer1 = pulsarClient.newConsumer().topic(topic).subscriptionName(subName1).acknowledgmentGroupTime(0, TimeUnit.MILLISECONDS).subscribe();
Awaitility.await().until(consumer1::isConnected);
@Cleanup Consumer<byte[]> consumer2 = pulsarClient.newConsumer().topic(topic).subscriptionName(subName2).acknowledgmentGroupTime(0, TimeUnit.MILLISECONDS).subscribe();
Awaitility.await().until(consumer2::isConnected);
@Cleanup Producer<byte[]> producer = pulsarClient.newProducer().topic(topic).sendTimeout(0, TimeUnit.SECONDS).enableBatching(false).create();
MessageId messageId = producer.send(("Hello Pulsar").getBytes(UTF_8));
TransactionImpl txn = (TransactionImpl) getTxn();
consumer1.acknowledgeAsync(messageId, txn).get();
consumer2.acknowledgeAsync(messageId, txn).get();
boolean flag = false;
for (int i = 0; i < getPulsarServiceList().size(); i++) {
TransactionMetadataStoreService transactionMetadataStoreService = getPulsarServiceList().get(i).getTransactionMetadataStoreService();
if (transactionMetadataStoreService.getStores().containsKey(TransactionCoordinatorID.get(txn.getTxnIdMostBits()))) {
List<TransactionSubscription> list = transactionMetadataStoreService.getTxnMeta(new TxnID(txn.getTxnIdMostBits(), txn.getTxnIdLeastBits())).get().ackedPartitions();
flag = true;
assertEquals(list.size(), 2);
if (list.get(0).getSubscription().equals(subName1)) {
assertEquals(list.get(1).getSubscription(), subName2);
} else {
assertEquals(list.get(0).getSubscription(), subName2);
assertEquals(list.get(1).getSubscription(), subName1);
}
}
}
assertTrue(flag);
}
use of org.apache.pulsar.transaction.coordinator.TransactionSubscription in project pulsar by yahoo.
the class MLTransactionMetadataStore method addAckedPartitionToTxn.
@Override
public CompletableFuture<Void> addAckedPartitionToTxn(TxnID txnID, List<TransactionSubscription> txnSubscriptions) {
CompletableFuture<Void> promise = new CompletableFuture<>();
internalPinnedExecutor.execute(() -> {
if (!checkIfReady()) {
promise.completeExceptionally(new CoordinatorException.TransactionMetadataStoreStateException(tcID, State.Ready, getState(), "add acked partition"));
return;
}
getTxnPositionPair(txnID).thenCompose(txnMetaListPair -> {
TransactionMetadataEntry transactionMetadataEntry = new TransactionMetadataEntry().setTxnidMostBits(txnID.getMostSigBits()).setTxnidLeastBits(txnID.getLeastSigBits()).setMetadataOp(TransactionMetadataOp.ADD_SUBSCRIPTION).addAllSubscriptions(txnSubscriptionToSubscription(txnSubscriptions)).setLastModificationTime(System.currentTimeMillis()).setMaxLocalTxnId(sequenceIdGenerator.getCurrentSequenceId());
return transactionLog.append(transactionMetadataEntry).thenAccept(position -> {
appendLogCount.increment();
try {
synchronized (txnMetaListPair.getLeft()) {
txnMetaListPair.getLeft().addAckedPartitions(txnSubscriptions);
txnMetaMap.get(txnID.getLeastSigBits()).getRight().add(position);
}
promise.complete(null);
} catch (InvalidTxnStatusException e) {
transactionLog.deletePosition(Collections.singletonList(position));
log.error("TxnID : " + txnMetaListPair.getLeft().id().toString() + " add acked subscription error with TxnStatus : " + txnMetaListPair.getLeft().status().name(), e);
promise.completeExceptionally(e);
}
});
}).exceptionally(ex -> {
promise.completeExceptionally(ex);
return null;
});
});
return promise;
}
use of org.apache.pulsar.transaction.coordinator.TransactionSubscription in project pulsar by yahoo.
the class MLTransactionMetadataStore method txnSubscriptionToSubscription.
public static List<Subscription> txnSubscriptionToSubscription(List<TransactionSubscription> tnxSubscriptions) {
List<Subscription> subscriptions = new ArrayList<>(tnxSubscriptions.size());
for (TransactionSubscription transactionSubscription : tnxSubscriptions) {
Subscription subscription = new Subscription().setSubscription(transactionSubscription.getSubscription()).setTopic(transactionSubscription.getTopic());
subscriptions.add(subscription);
}
return subscriptions;
}
Aggregations