Search in sources :

Example 1 with PersistentTopicInternalStats

use of org.apache.pulsar.common.policies.data.PersistentTopicInternalStats in project incubator-pulsar by apache.

the class PersistentTopic method getInternalStats.

public PersistentTopicInternalStats getInternalStats() {
    PersistentTopicInternalStats stats = new PersistentTopicInternalStats();
    ManagedLedgerImpl ml = (ManagedLedgerImpl) ledger;
    stats.entriesAddedCounter = ml.getEntriesAddedCounter();
    stats.numberOfEntries = ml.getNumberOfEntries();
    stats.totalSize = ml.getTotalSize();
    stats.currentLedgerEntries = ml.getCurrentLedgerEntries();
    stats.currentLedgerSize = ml.getCurrentLedgerSize();
    stats.lastLedgerCreatedTimestamp = DateFormatter.format(ml.getLastLedgerCreatedTimestamp());
    if (ml.getLastLedgerCreationFailureTimestamp() != 0) {
        stats.lastLedgerCreationFailureTimestamp = DateFormatter.format(ml.getLastLedgerCreationFailureTimestamp());
    }
    stats.waitingCursorsCount = ml.getWaitingCursorsCount();
    stats.pendingAddEntriesCount = ml.getPendingAddEntriesCount();
    stats.lastConfirmedEntry = ml.getLastConfirmedEntry().toString();
    stats.state = ml.getState().toString();
    stats.ledgers = Lists.newArrayList();
    ml.getLedgersInfo().forEach((id, li) -> {
        LedgerInfo info = new LedgerInfo();
        info.ledgerId = li.getLedgerId();
        info.entries = li.getEntries();
        info.size = li.getSize();
        stats.ledgers.add(info);
    });
    stats.cursors = Maps.newTreeMap();
    ml.getCursors().forEach(c -> {
        ManagedCursorImpl cursor = (ManagedCursorImpl) c;
        CursorStats cs = new CursorStats();
        cs.markDeletePosition = cursor.getMarkDeletedPosition().toString();
        cs.readPosition = cursor.getReadPosition().toString();
        cs.waitingReadOp = cursor.hasPendingReadRequest();
        cs.pendingReadOps = cursor.getPendingReadOpsCount();
        cs.messagesConsumedCounter = cursor.getMessagesConsumedCounter();
        cs.cursorLedger = cursor.getCursorLedger();
        cs.cursorLedgerLastEntry = cursor.getCursorLedgerLastEntry();
        cs.individuallyDeletedMessages = cursor.getIndividuallyDeletedMessages();
        cs.lastLedgerSwitchTimestamp = DateFormatter.format(cursor.getLastLedgerSwitchTimestamp());
        cs.state = cursor.getState();
        cs.numberOfEntriesSinceFirstNotAckedMessage = cursor.getNumberOfEntriesSinceFirstNotAckedMessage();
        cs.totalNonContiguousDeletedMessagesRange = cursor.getTotalNonContiguousDeletedMessagesRange();
        cs.properties = cursor.getProperties();
        stats.cursors.put(cursor.getName(), cs);
    });
    return stats;
}
Also used : LedgerInfo(org.apache.pulsar.common.policies.data.PersistentTopicInternalStats.LedgerInfo) ManagedLedgerImpl(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl) ManagedCursorImpl(org.apache.bookkeeper.mledger.impl.ManagedCursorImpl) PersistentTopicInternalStats(org.apache.pulsar.common.policies.data.PersistentTopicInternalStats) CursorStats(org.apache.pulsar.common.policies.data.PersistentTopicInternalStats.CursorStats)

Example 2 with PersistentTopicInternalStats

use of org.apache.pulsar.common.policies.data.PersistentTopicInternalStats in project incubator-pulsar by apache.

the class AdminApiTest method persistentTopics.

@Test(dataProvider = "topicName")
public void persistentTopics(String topicName) throws Exception {
    assertEquals(admin.persistentTopics().getList("prop-xyz/use/ns1"), Lists.newArrayList());
    final String persistentTopicName = "persistent://prop-xyz/use/ns1/" + topicName;
    // Force to create a topic
    publishMessagesOnPersistentTopic("persistent://prop-xyz/use/ns1/" + topicName, 0);
    assertEquals(admin.persistentTopics().getList("prop-xyz/use/ns1"), Lists.newArrayList("persistent://prop-xyz/use/ns1/" + topicName));
    // create consumer and subscription
    URL pulsarUrl = new URL("http://127.0.0.1" + ":" + BROKER_WEBSERVICE_PORT);
    PulsarClient client = PulsarClient.builder().serviceUrl(pulsarUrl.toString()).statsInterval(0, TimeUnit.SECONDS).build();
    Consumer<byte[]> consumer = client.newConsumer().topic(persistentTopicName).subscriptionName("my-sub").subscriptionType(SubscriptionType.Exclusive).subscribe();
    assertEquals(admin.persistentTopics().getSubscriptions(persistentTopicName), Lists.newArrayList("my-sub"));
    publishMessagesOnPersistentTopic("persistent://prop-xyz/use/ns1/" + topicName, 10);
    PersistentTopicStats topicStats = admin.persistentTopics().getStats(persistentTopicName);
    assertEquals(topicStats.subscriptions.keySet(), Sets.newTreeSet(Lists.newArrayList("my-sub")));
    assertEquals(topicStats.subscriptions.get("my-sub").consumers.size(), 1);
    assertEquals(topicStats.subscriptions.get("my-sub").msgBacklog, 10);
    assertEquals(topicStats.publishers.size(), 0);
    PersistentTopicInternalStats internalStats = admin.persistentTopics().getInternalStats(persistentTopicName);
    assertEquals(internalStats.cursors.keySet(), Sets.newTreeSet(Lists.newArrayList("my-sub")));
    List<Message<byte[]>> messages = admin.persistentTopics().peekMessages(persistentTopicName, "my-sub", 3);
    assertEquals(messages.size(), 3);
    for (int i = 0; i < 3; i++) {
        String expectedMessage = "message-" + i;
        assertEquals(messages.get(i).getData(), expectedMessage.getBytes());
    }
    messages = admin.persistentTopics().peekMessages(persistentTopicName, "my-sub", 15);
    assertEquals(messages.size(), 10);
    for (int i = 0; i < 10; i++) {
        String expectedMessage = "message-" + i;
        assertEquals(messages.get(i).getData(), expectedMessage.getBytes());
    }
    admin.persistentTopics().skipMessages(persistentTopicName, "my-sub", 5);
    topicStats = admin.persistentTopics().getStats(persistentTopicName);
    assertEquals(topicStats.subscriptions.get("my-sub").msgBacklog, 5);
    admin.persistentTopics().skipAllMessages(persistentTopicName, "my-sub");
    topicStats = admin.persistentTopics().getStats(persistentTopicName);
    assertEquals(topicStats.subscriptions.get("my-sub").msgBacklog, 0);
    consumer.close();
    client.close();
    admin.persistentTopics().deleteSubscription(persistentTopicName, "my-sub");
    assertEquals(admin.persistentTopics().getSubscriptions(persistentTopicName), Lists.newArrayList());
    topicStats = admin.persistentTopics().getStats(persistentTopicName);
    assertEquals(topicStats.subscriptions.keySet(), Sets.newTreeSet());
    assertEquals(topicStats.publishers.size(), 0);
    try {
        admin.persistentTopics().skipAllMessages(persistentTopicName, "my-sub");
    } catch (NotFoundException e) {
    }
    admin.persistentTopics().delete(persistentTopicName);
    try {
        admin.persistentTopics().delete(persistentTopicName);
        fail("Should have received 404");
    } catch (NotFoundException e) {
    }
    assertEquals(admin.persistentTopics().getList("prop-xyz/use/ns1"), Lists.newArrayList());
}
Also used : Message(org.apache.pulsar.client.api.Message) PersistentTopicInternalStats(org.apache.pulsar.common.policies.data.PersistentTopicInternalStats) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) PulsarClient(org.apache.pulsar.client.api.PulsarClient) PersistentTopicStats(org.apache.pulsar.common.policies.data.PersistentTopicStats) URL(java.net.URL) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 3 with PersistentTopicInternalStats

use of org.apache.pulsar.common.policies.data.PersistentTopicInternalStats in project incubator-pulsar by apache.

the class NonPersistentTopicsImpl method getInternalStatsAsync.

@Override
public CompletableFuture<PersistentTopicInternalStats> getInternalStatsAsync(String topic) {
    TopicName topicName = validateTopic(topic);
    final CompletableFuture<PersistentTopicInternalStats> future = new CompletableFuture<>();
    WebTarget path = topicPath(topicName, "internalStats");
    asyncGetRequest(path, new InvocationCallback<PersistentTopicInternalStats>() {

        @Override
        public void completed(PersistentTopicInternalStats response) {
            future.complete(response);
        }

        @Override
        public void failed(Throwable throwable) {
            future.completeExceptionally(getApiException(throwable.getCause()));
        }
    });
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) PersistentTopicInternalStats(org.apache.pulsar.common.policies.data.PersistentTopicInternalStats) WebTarget(javax.ws.rs.client.WebTarget) TopicName(org.apache.pulsar.common.naming.TopicName)

Example 4 with PersistentTopicInternalStats

use of org.apache.pulsar.common.policies.data.PersistentTopicInternalStats in project incubator-pulsar by apache.

the class PersistentTopicsImpl method getInternalStatsAsync.

@Override
public CompletableFuture<PersistentTopicInternalStats> getInternalStatsAsync(String topic) {
    TopicName tn = validateTopic(topic);
    WebTarget path = topicPath(tn, "internalStats");
    final CompletableFuture<PersistentTopicInternalStats> future = new CompletableFuture<>();
    asyncGetRequest(path, new InvocationCallback<PersistentTopicInternalStats>() {

        @Override
        public void completed(PersistentTopicInternalStats response) {
            future.complete(response);
        }

        @Override
        public void failed(Throwable throwable) {
            future.completeExceptionally(getApiException(throwable.getCause()));
        }
    });
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) PersistentTopicInternalStats(org.apache.pulsar.common.policies.data.PersistentTopicInternalStats) WebTarget(javax.ws.rs.client.WebTarget) TopicName(org.apache.pulsar.common.naming.TopicName)

Example 5 with PersistentTopicInternalStats

use of org.apache.pulsar.common.policies.data.PersistentTopicInternalStats in project incubator-pulsar by apache.

the class PulsarBrokerStatsClientTest method testTopicInternalStats.

@Test
public void testTopicInternalStats() throws Exception {
    log.info("-- Starting {} test --", methodName);
    final String topicName = "persistent://my-property/use/my-ns/my-topic1";
    final String subscriptionName = "my-subscriber-name";
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subscriptionName).subscribe();
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create();
    final int numberOfMsgs = 1000;
    for (int i = 0; i < numberOfMsgs; i++) {
        String message = "my-message-" + i;
        producer.send(message.getBytes());
    }
    Message<byte[]> msg = null;
    int count = 0;
    for (int i = 0; i < numberOfMsgs; i++) {
        msg = consumer.receive(5, TimeUnit.SECONDS);
        if (msg != null && count++ % 2 == 0) {
            consumer.acknowledge(msg);
        }
    }
    PersistentTopic topic = (PersistentTopic) pulsar.getBrokerService().getTopic(topicName).get();
    PersistentTopicInternalStats internalStats = topic.getInternalStats();
    CursorStats cursor = internalStats.cursors.get(subscriptionName);
    assertEquals(cursor.numberOfEntriesSinceFirstNotAckedMessage, numberOfMsgs);
    assertTrue(cursor.totalNonContiguousDeletedMessagesRange > 0 && (cursor.totalNonContiguousDeletedMessagesRange) < numberOfMsgs / 2);
    producer.close();
    consumer.close();
    log.info("-- Exiting {} test --", methodName);
}
Also used : PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) PersistentTopicInternalStats(org.apache.pulsar.common.policies.data.PersistentTopicInternalStats) CursorStats(org.apache.pulsar.common.policies.data.PersistentTopicInternalStats.CursorStats) Test(org.testng.annotations.Test)

Aggregations

PersistentTopicInternalStats (org.apache.pulsar.common.policies.data.PersistentTopicInternalStats)7 CursorStats (org.apache.pulsar.common.policies.data.PersistentTopicInternalStats.CursorStats)3 Test (org.testng.annotations.Test)3 URL (java.net.URL)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 WebTarget (javax.ws.rs.client.WebTarget)2 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)2 PulsarClient (org.apache.pulsar.client.api.PulsarClient)2 TopicName (org.apache.pulsar.common.naming.TopicName)2 ManagedCursorImpl (org.apache.bookkeeper.mledger.impl.ManagedCursorImpl)1 ManagedLedgerImpl (org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl)1 PersistentTopic (org.apache.pulsar.broker.service.persistent.PersistentTopic)1 NotFoundException (org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException)1 Message (org.apache.pulsar.client.api.Message)1 NonPersistentTopicStats (org.apache.pulsar.common.policies.data.NonPersistentTopicStats)1 LedgerInfo (org.apache.pulsar.common.policies.data.PersistentTopicInternalStats.LedgerInfo)1 PersistentTopicStats (org.apache.pulsar.common.policies.data.PersistentTopicStats)1