use of org.apache.pulsar.transaction.coordinator.TransactionCoordinatorID in project pulsar by apache.
the class TransactionMetadataStoreService method getLowWaterMark.
public long getLowWaterMark(TxnID txnID) {
TransactionCoordinatorID tcId = getTcIdFromTxnId(txnID);
TransactionMetadataStore store = stores.get(tcId);
if (store == null) {
return 0;
}
return store.getLowWaterMark();
}
use of org.apache.pulsar.transaction.coordinator.TransactionCoordinatorID in project pulsar by apache.
the class TransactionMetadataStoreService method getTxnMeta.
public CompletableFuture<TxnMeta> getTxnMeta(TxnID txnId) {
TransactionCoordinatorID tcId = getTcIdFromTxnId(txnId);
TransactionMetadataStore store = stores.get(tcId);
if (store == null) {
return FutureUtil.failedFuture(new CoordinatorNotFoundException(tcId));
}
return store.getTxnMeta(txnId);
}
use of org.apache.pulsar.transaction.coordinator.TransactionCoordinatorID in project pulsar by apache.
the class ServerCnx method handleAddSubscriptionToTxn.
@Override
protected void handleAddSubscriptionToTxn(CommandAddSubscriptionToTxn command) {
final TxnID txnID = new TxnID(command.getTxnidMostBits(), command.getTxnidLeastBits());
final long requestId = command.getRequestId();
if (log.isDebugEnabled()) {
log.debug("Receive add published partition to txn request {} from {} with txnId {}", requestId, remoteAddress, txnID);
}
final TransactionCoordinatorID tcId = TransactionCoordinatorID.get(command.getTxnidMostBits());
if (!checkTransactionEnableAndSendError(requestId)) {
return;
}
TransactionMetadataStoreService transactionMetadataStoreService = service.pulsar().getTransactionMetadataStoreService();
transactionMetadataStoreService.addAckedPartitionToTxn(txnID, MLTransactionMetadataStore.subscriptionToTxnSubscription(command.getSubscriptionsList())).whenComplete(((v, ex) -> {
if (ex == null) {
if (log.isDebugEnabled()) {
log.debug("Send response success for add published partition to txn request {}", requestId);
}
ctx.writeAndFlush(Commands.newAddSubscriptionToTxnResponse(requestId, txnID.getLeastSigBits(), txnID.getMostSigBits()));
} else {
ex = handleTxnException(ex, BaseCommand.Type.ADD_SUBSCRIPTION_TO_TXN.name(), requestId);
ctx.writeAndFlush(Commands.newAddSubscriptionToTxnResponse(requestId, txnID.getMostSigBits(), BrokerServiceException.getClientErrorCode(ex), ex.getMessage()));
transactionMetadataStoreService.handleOpFail(ex, tcId);
}
}));
}
use of org.apache.pulsar.transaction.coordinator.TransactionCoordinatorID in project pulsar by apache.
the class TransactionMetricsTest method testDuplicateMetricTypeDefinitions.
@Test
public void testDuplicateMetricTypeDefinitions() throws Exception {
admin.lookups().lookupTopic(TopicName.TRANSACTION_COORDINATOR_ASSIGN.toString());
TransactionCoordinatorID transactionCoordinatorIDOne = TransactionCoordinatorID.get(0);
TransactionCoordinatorID transactionCoordinatorIDTwo = TransactionCoordinatorID.get(1);
pulsar.getTransactionMetadataStoreService().handleTcClientConnect(transactionCoordinatorIDOne);
pulsar.getTransactionMetadataStoreService().handleTcClientConnect(transactionCoordinatorIDTwo);
Awaitility.await().until(() -> pulsar.getTransactionMetadataStoreService().getStores().size() == 2);
pulsarClient = PulsarClient.builder().serviceUrl(lookupUrl.toString()).enableTransaction(true).build();
Producer<byte[]> p1 = pulsarClient.newProducer().topic("persistent://my-property/use/my-ns/my-topic1").sendTimeout(0, TimeUnit.SECONDS).create();
Transaction transaction = pulsarClient.newTransaction().withTransactionTimeout(5, TimeUnit.SECONDS).build().get();
for (int i = 0; i < 10; i++) {
String message = "my-message-" + i;
p1.newMessage(transaction).value(message.getBytes()).send();
}
ByteArrayOutputStream statsOut = new ByteArrayOutputStream();
PrometheusMetricsGenerator.generate(pulsar, false, false, false, statsOut);
String metricsStr = statsOut.toString();
Map<String, String> typeDefs = new HashMap<>();
Map<String, String> metricNames = new HashMap<>();
Pattern typePattern = Pattern.compile("^#\\s+TYPE\\s+(\\w+)\\s+(\\w+)");
Splitter.on("\n").split(metricsStr).forEach(line -> {
if (line.isEmpty()) {
return;
}
if (line.startsWith("#")) {
// Check for duplicate type definitions
Matcher typeMatcher = typePattern.matcher(line);
checkArgument(typeMatcher.matches());
String metricName = typeMatcher.group(1);
String type = typeMatcher.group(2);
// "Only one TYPE line may exist for a given metric name."
if (!typeDefs.containsKey(metricName)) {
typeDefs.put(metricName, type);
} else {
log.warn(metricsStr);
fail("Duplicate type definition found for TYPE definition " + metricName);
}
// "The TYPE line for a metric name must appear before the first sample is reported for that metric name."
if (metricNames.containsKey(metricName)) {
log.info(metricsStr);
fail("TYPE definition for " + metricName + " appears after first sample");
}
}
});
}
use of org.apache.pulsar.transaction.coordinator.TransactionCoordinatorID in project pulsar by apache.
the class TransactionMetricsTest method testManagedLedgerMetricsWhenPendingAckNotInit.
@Test
public void testManagedLedgerMetricsWhenPendingAckNotInit() throws Exception {
String ns1 = "prop/ns-abc1";
admin.namespaces().createNamespace(ns1);
String topic = "persistent://" + ns1 + "/testManagedLedgerMetricsWhenPendingAckNotInit";
String subName = "test_managed_ledger_metrics";
String subName2 = "test_pending_ack_no_init";
admin.topics().createNonPartitionedTopic(topic);
admin.lookups().lookupTopic(TopicName.TRANSACTION_COORDINATOR_ASSIGN.toString());
TransactionCoordinatorID transactionCoordinatorIDOne = TransactionCoordinatorID.get(0);
pulsar.getTransactionMetadataStoreService().handleTcClientConnect(transactionCoordinatorIDOne).get();
admin.topics().createSubscription(topic, subName, MessageId.earliest);
admin.topics().createSubscription(topic, subName2, MessageId.earliest);
Awaitility.await().atMost(2000, TimeUnit.MILLISECONDS).until(() -> pulsar.getTransactionMetadataStoreService().getStores().size() == 1);
pulsarClient = PulsarClient.builder().serviceUrl(lookupUrl.toString()).enableTransaction(true).build();
Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topic).receiverQueueSize(10).subscriptionName(subName).subscriptionType(SubscriptionType.Key_Shared).subscribe();
Producer<byte[]> producer = pulsarClient.newProducer().topic(topic).create();
Transaction transaction = pulsarClient.newTransaction().withTransactionTimeout(10, TimeUnit.SECONDS).build().get();
producer.send("hello pulsar".getBytes());
consumer.acknowledgeAsync(consumer.receive().getMessageId(), transaction).get();
ByteArrayOutputStream statsOut = new ByteArrayOutputStream();
PrometheusMetricsGenerator.generate(pulsar, true, false, false, statsOut);
String metricsStr = statsOut.toString();
Multimap<String, PrometheusMetricsTest.Metric> metrics = parseMetrics(metricsStr);
Collection<PrometheusMetricsTest.Metric> metric = metrics.get("pulsar_storage_size");
checkManagedLedgerMetrics(subName, 32, metric);
// No statistics of the pendingAck are generated when the pendingAck is not initialized.
for (PrometheusMetricsTest.Metric metric1 : metric) {
if (metric1.tags.containsValue(subName2)) {
Assert.fail();
}
}
consumer = pulsarClient.newConsumer().topic(topic).receiverQueueSize(10).subscriptionName(subName2).subscriptionType(SubscriptionType.Key_Shared).subscribe();
transaction = pulsarClient.newTransaction().withTransactionTimeout(10, TimeUnit.SECONDS).build().get();
consumer.acknowledgeAsync(consumer.receive().getMessageId(), transaction).get();
statsOut = new ByteArrayOutputStream();
PrometheusMetricsGenerator.generate(pulsar, true, false, false, statsOut);
metricsStr = statsOut.toString();
metrics = parseMetrics(metricsStr);
metric = metrics.get("pulsar_storage_size");
checkManagedLedgerMetrics(subName2, 32, metric);
}
Aggregations