Managing persistent topics

Persistent helps to access topic which is a logical endpoint for publishing and consuming messages. Producers publish messages to the topic and consumers subscribe to the topic, to consume messages published to the topic.

In all of the instructions and commands below, the topic name structure is:

  1. persistent://tenant/namespace/topic

Persistent topics resources

List of topics

It provides a list of persistent topics exist under a given namespace.

pulsar-admin

List of topics can be fetched using list command.

  1. $ pulsar-admin persistent list \
  2. my-tenant/my-namespace

REST API

GET/admin/v2/persistent/:tenant/:namespace

Java

  1. String namespace = "my-tenant/my-namespace";
  2. admin.persistentTopics().getList(namespace);

Grant permission

It grants permissions on a client role to perform specific actions on a given topic.

pulsar-admin

Permission can be granted using grant-permission command.

  1. $ pulsar-admin persistent grant-permission \
  2. --actions produce,consume --role application1 \
  3. persistent://test-tenant/ns1/tp1 \

REST API

POST/admin/v2/persistent/:tenant/:namespace/:topic/permissions/:role

Java

  1. String topic = "persistent://my-tenant/my-namespace/my-topic";
  2. String role = "test-role";
  3. Set<AuthAction> actions = Sets.newHashSet(AuthAction.produce, AuthAction.consume);
  4. admin.persistentTopics().grantPermission(topic, role, actions);

Get permission

Permission can be fetched using permissions command.

pulsar-admin

  1. $ pulsar-admin persistent permissions \
  2. persistent://test-tenant/ns1/tp1 \
  3. {
  4. "application1": [
  5. "consume",
  6. "produce"
  7. ]
  8. }

REST API

GET/admin/v2/persistent/:tenant/:namespace/:topic/permissions

Java

  1. String topic = "persistent://my-tenant/my-namespace/my-topic";
  2. admin.persistentTopics().getPermissions(topic);

Revoke permission

It revokes a permission which was granted on a client role.

pulsar-admin

Permission can be revoked using revoke-permission command.

  1. $ pulsar-admin persistent revoke-permission \
  2. --role application1 \
  3. persistent://test-tenant/ns1/tp1 \
  4. {
  5. "application1": [
  6. "consume",
  7. "produce"
  8. ]
  9. }

REST API

DELETE/admin/v2/persistent/:tenant/:namespace/:topic/permissions/:role

Java

  1. String topic = "persistent://my-tenant/my-namespace/my-topic";
  2. String role = "test-role";
  3. admin.persistentTopics().revokePermissions(topic, role);

Delete topic

It deletes a topic. The topic cannot be deleted if there's any active subscription or producers connected to it.

pulsar-admin

Topic can be deleted using delete command.

  1. $ pulsar-admin persistent delete \
  2. persistent://test-tenant/ns1/tp1 \

REST API

DELETE/admin/v2/persistent/:tenant/:namespace/:topic

Java

  1. String topic = "persistent://my-tenant/my-namespace/my-topic";
  2. admin.persistentTopics().delete(topic);

Unload topic

It unloads a topic.

pulsar-admin

Topic can be unloaded using unload command.

  1. $ pulsar-admin persistent unload \
  2. persistent://test-tenant/ns1/tp1 \

REST API

PUT/admin/v2/persistent/:tenant/:namespace/:topic/unload

Java

  1. String topic = "persistent://my-tenant/my-namespace/my-topic";
  2. admin.persistentTopics().unload(topic);

Get stats

It shows current statistics of a given non-partitioned topic.

  • msgRateIn: The sum of all local and replication publishers' publish rates in messages per second

  • msgThroughputIn: Same as above, but in bytes per second instead of messages per second

  • msgRateOut: The sum of all local and replication consumers' dispatch rates in messages per second

  • msgThroughputOut: Same as above, but in bytes per second instead of messages per second

  • averageMsgSize: The average size in bytes of messages published within the last interval

  • storageSize: The sum of the ledgers' storage size for this topic. See

  • publishers: The list of all local publishers into the topic. There can be zero or thousands

  • averageMsgSize: Average message size in bytes from this publisher within the last interval

  • producerId: Internal identifier for this producer on this topic

  • producerName: Internal identifier for this producer, generated by the client library

  • address: IP address and source port for the connection of this producer

  • connectedSince: Timestamp this producer was created or last reconnected

  • subscriptions: The list of all local subscriptions to the topic

  • my-subscription: The name of this subscription (client defined)

  • msgBacklog: The count of messages in backlog for this subscription

  • type: This subscription type

  • msgRateExpired: The rate at which messages were discarded instead of dispatched from this subscription due to TTL

  • consumers: The list of connected consumers for this subscription

  • consumerName: Internal identifier for this consumer, generated by the client library

  • availablePermits: The number of messages this consumer has space for in the client library's listen queue. A value of 0 means the client library's queue is full and receive() isn't being called. A nonzero value means this consumer is ready to be dispatched messages.

  • replication: This section gives the stats for cross-colo replication of this topic

  • replicationBacklog: The outbound replication backlog in messages

  • connected: Whether the outbound replicator is connected

  • replicationDelayInSeconds: How long the oldest message has been waiting to be sent through the connection, if connected is true

  • inboundConnection: The IP and port of the broker in the remote cluster's publisher connection to this broker

  • inboundConnectedSince: The TCP connection being used to publish messages to the remote cluster. If there are no local publishers connected, this connection is automatically closed after a minute.

  1. {
  2. "msgRateIn": 4641.528542257553,
  3. "msgThroughputIn": 44663039.74947473,
  4. "msgRateOut": 0,
  5. "msgThroughputOut": 0,
  6. "averageMsgSize": 1232439.816728665,
  7. "storageSize": 135532389160,
  8. "publishers": [
  9. {
  10. "msgRateIn": 57.855383881403576,
  11. "msgThroughputIn": 558994.7078932219,
  12. "averageMsgSize": 613135,
  13. "producerId": 0,
  14. "producerName": null,
  15. "address": null,
  16. "connectedSince": null
  17. }
  18. ],
  19. "subscriptions": {
  20. "my-topic_subscription": {
  21. "msgRateOut": 0,
  22. "msgThroughputOut": 0,
  23. "msgBacklog": 116632,
  24. "type": null,
  25. "msgRateExpired": 36.98245516804671,
  26. "consumers": []
  27. }
  28. },
  29. "replication": {}
  30. }

pulsar-admin

Topic stats can be fetched using stats command.

  1. $ pulsar-admin persistent stats \
  2. persistent://test-tenant/ns1/tp1 \

REST API

GET/admin/v2/persistent/:tenant/:namespace/:topic/stats

Java

  1. String topic = "persistent://my-tenant/my-namespace/my-topic";
  2. admin.persistentTopics().getStats(topic);

Get internal stats

It shows detailed statistics of a topic.

  • entriesAddedCounter: Messages published since this broker loaded this topic

  • numberOfEntries: Total number of messages being tracked

  • totalSize: Total storage size in bytes of all messages

  • currentLedgerEntries: Count of messages written to the ledger currently open for writing

  • currentLedgerSize: Size in bytes of messages written to ledger currently open for writing

  • lastLedgerCreatedTimestamp: time when last ledger was created

  • lastLedgerCreationFailureTimestamp: time when last ledger was failed

  • waitingCursorsCount: How many cursors are "caught up" and waiting for a new message to be published

  • pendingAddEntriesCount: How many messages have (asynchronous) write requests we are waiting on completion

  • lastConfirmedEntry: The ledgerid:entryid of the last message successfully written. If the entryid is -1, then the ledger has been opened or is currently being opened but has no entries written yet.

  • state: The state of this ledger for writing. LedgerOpened means we have a ledger open for saving published messages.

  • ledgers: The ordered list of all ledgers for this topic holding its messages

  • cursors: The list of all cursors on this topic. There will be one for every subscription you saw in the topic stats.

  • markDeletePosition: The ack position: the last message the subscriber acknowledged receiving

  • readPosition: The latest position of subscriber for reading message

  • waitingReadOp: This is true when the subscription has read the latest message published to the topic and is waiting on new messages to be published.

  • pendingReadOps: The counter for how many outstanding read requests to the BookKeepers we have in progress

  • messagesConsumedCounter: Number of messages this cursor has acked since this broker loaded this topic

  • cursorLedger: The ledger being used to persistently store the current markDeletePosition

  • cursorLedgerLastEntry: The last entryid used to persistently store the current markDeletePosition

  • individuallyDeletedMessages: If Acks are being done out of order, shows the ranges of messages Acked between the markDeletePosition and the read-position

  • lastLedgerSwitchTimestamp: The last time the cursor ledger was rolled over

  • state: The state of the cursor ledger: Open means we have a cursor ledger for saving updates of the markDeletePosition.

  1. {
  2. "entriesAddedCounter": 20449518,
  3. "numberOfEntries": 3233,
  4. "totalSize": 331482,
  5. "currentLedgerEntries": 3233,
  6. "currentLedgerSize": 331482,
  7. "lastLedgerCreatedTimestamp": "2016-06-29 03:00:23.825",
  8. "lastLedgerCreationFailureTimestamp": null,
  9. "waitingCursorsCount": 1,
  10. "pendingAddEntriesCount": 0,
  11. "lastConfirmedEntry": "324711539:3232",
  12. "state": "LedgerOpened",
  13. "ledgers": [
  14. {
  15. "ledgerId": 324711539,
  16. "entries": 0,
  17. "size": 0
  18. }
  19. ],
  20. "cursors": {
  21. "my-subscription": {
  22. "markDeletePosition": "324711539:3133",
  23. "readPosition": "324711539:3233",
  24. "waitingReadOp": true,
  25. "pendingReadOps": 0,
  26. "messagesConsumedCounter": 20449501,
  27. "cursorLedger": 324702104,
  28. "cursorLedgerLastEntry": 21,
  29. "individuallyDeletedMessages": "[(324711539:3134‥324711539:3136], (324711539:3137‥324711539:3140], ]",
  30. "lastLedgerSwitchTimestamp": "2016-06-29 01:30:19.313",
  31. "state": "Open"
  32. }
  33. }
  34. }

pulsar-admin

Topic internal-stats can be fetched using stats-internal command.

  1. $ pulsar-admin persistent stats-internal \
  2. persistent://test-tenant/ns1/tp1 \

REST API

GET/admin/v2/persistent/:tenant/:namespace/:topic/internalStats

Java

  1. String topic = "persistent://my-tenant/my-namespace/my-topic";
  2. admin.persistentTopics().getInternalStats(topic);

Peek messages

It peeks N messages for a specific subscription of a given topic.

pulsar-admin

  1. $ pulsar-admin persistent peek-messages \
  2. --count 10 --subscription my-subscription \
  3. persistent://test-tenant/ns1/tp1 \
  4. Message ID: 315674752:0
  5. Properties: { "X-Pulsar-publish-time" : "2015-07-13 17:40:28.451" }
  6. msg-payload

REST API

GET/admin/v2/persistent/:tenant/:namespace/:topic/subscription/:subName/position/:messagePosition

Java

  1. String topic = "persistent://my-tenant/my-namespace/my-topic";
  2. String subName = "my-subscription";
  3. int numMessages = 1;
  4. admin.persistentTopics().peekMessages(topic, subName, numMessages);

Skip messages

It skips N messages for a specific subscription of a given topic.

pulsar-admin

  1. $ pulsar-admin persistent skip \
  2. --count 10 --subscription my-subscription \
  3. persistent://test-tenant/ns1/tp1 \

REST API

POST/admin/v2/persistent/:tenant/:namespace/:topic/subscription/:subName/skip/:numMessages

Java

  1. String topic = "persistent://my-tenant/my-namespace/my-topic";
  2. String subName = "my-subscription";
  3. int numMessages = 1;
  4. admin.persistentTopics().skipMessages(topic, subName, numMessages);

Skip all messages

It skips all old messages for a specific subscription of a given topic.

pulsar-admin

  1. $ pulsar-admin persistent skip-all \
  2. --subscription my-subscription \
  3. persistent://test-tenant/ns1/tp1 \

REST API

POST/admin/v2/persistent/:tenant/:namespace/:topic/subscription/:subName/skip_all

More info

Java

  1. String topic = "persistent://my-tenant/my-namespace/my-topic";
  2. String subName = "my-subscription";
  3. admin.persistentTopics().skipAllMessages(topic, subName);

Reset cursor

It resets a subscription’s cursor position back to the position which was recorded X minutes before. It essentially calculates time and position of cursor at X minutes before and resets it at that position.

pulsar-admin

  1. $ pulsar-admin persistent reset-cursor \
  2. --subscription my-subscription --time 10 \
  3. persistent://test-tenant/ns1/tp1 \

REST API

POST/admin/v2/persistent/:tenant/:namespace/:topic/subscription/:subName/resetcursor/:timestamp

Java

  1. String topic = "persistent://my-tenant/my-namespace/my-topic";
  2. String subName = "my-subscription";
  3. long timestamp = 2342343L;
  4. admin.persistentTopics().skipAllMessages(topic, subName, timestamp);

Lookup of topic

It locates broker url which is serving the given topic.

pulsar-admin

  1. $ pulsar-admin persistent lookup \
  2. persistent://test-tenant/ns1/tp1 \
  3. "pulsar://broker1.org.com:4480"

REST API

GET/lookup/v2/topic/persistent/:tenant:namespace/:topic

Java

  1. String topic = "persistent://my-tenant/my-namespace/my-topic";
  2. admin.lookup().lookupDestination(topic);

Get bundle

It gives range of the bundle which contains given topic

pulsar-admin

  1. $ pulsar-admin persistent bundle-range \
  2. persistent://test-tenant/ns1/tp1 \
  3. "0x00000000_0xffffffff"

REST API

GET/lookup/v2/topic/:topic_domain/:tenant/:namespace/:topic/bundle

Java

  1. String topic = "persistent://my-tenant/my-namespace/my-topic";
  2. admin.lookup().getBundleRange(topic);

Get subscriptions

It shows all subscription names for a given topic.

pulsar-admin

  1. $ pulsar-admin persistent subscriptions \
  2. persistent://test-tenant/ns1/tp1 \
  3. my-subscription

REST API

GET/admin/v2/persistent/:tenant/:namespace/:topic/subscriptions

Java

  1. String topic = "persistent://my-tenant/my-namespace/my-topic";
  2. admin.persistentTopics().getSubscriptions(topic);

Unsubscribe

It can also help to unsubscribe a subscription which is no more processing further messages.

pulsar-admin

  1. $ pulsar-admin persistent unsubscribe \
  2. --subscription my-subscription \
  3. persistent://test-tenant/ns1/tp1 \

REST API

DELETE/admin/v2/namespaces/:tenant/:namespace/:topic/subscription/:subscription

Java

  1. String topic = "persistent://my-tenant/my-namespace/my-topic";
  2. String subscriptionName = "my-subscription";
  3. admin.persistentTopics().deleteSubscription(topic, subscriptionName);

Last Message Id

It gives the last commited message ID for a persistent topic, and it will be available in 2.3.0.

  1. pulsar-admin topics last-message-id topic-name

REST API

{% endpoint Get /admin/v2/persistent/:tenant/:namespace/:topic/lastMessageId %}

Java

  1. String topic = "persistent://my-tenant/my-namespace/my-topic";
  2. admin.persistentTopics().getLastMessage(topic);