Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,18 @@ public void updateRates() {
stats.chuckedMessageRate = chuckedMessageRate.getRate();
}

public void updateStats(ConsumerStats consumerStats) {
msgOutCounter.add(consumerStats.msgOutCounter);
bytesOutCounter.add(consumerStats.bytesOutCounter);
msgOut.recordMultipleEvents(consumerStats.msgOutCounter, consumerStats.bytesOutCounter);
lastAckedTimestamp = consumerStats.lastAckedTimestamp;
lastConsumedTimestamp = consumerStats.lastConsumedTimestamp;
MESSAGE_PERMITS_UPDATER.set(this, consumerStats.availablePermits);
unackedMessages = consumerStats.unackedMessages;
blockedConsumerOnUnackedMsgs = consumerStats.blockedConsumerOnUnackedMsgs;
AVG_MESSAGES_PER_ENTRY.set(this, consumerStats.avgMessagesPerEntry);
}

public ConsumerStats getStats() {
stats.msgOutCounter = msgOutCounter.longValue();
stats.bytesOutCounter = bytesOutCounter.longValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@
import org.apache.pulsar.client.api.PulsarClientException;
import org.apache.pulsar.client.api.SubscriptionType;
import org.apache.pulsar.common.policies.data.TopicStats;
import org.apache.pulsar.broker.service.persistent.PersistentTopic;
import org.apache.pulsar.common.policies.data.ConsumerStats;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
Expand Down Expand Up @@ -140,4 +143,28 @@ public void testAckStatsOnPartitionedTopicForExclusiveSubscription() throws Puls
}
}

@Test
public void testUpdateStatsForActiveConsumerAndSubscription() throws Exception {
final String topicName = "persistent://prop/use/ns-abc/testUpdateStatsForActiveConsumerAndSubscription";
pulsarClient.newConsumer()
.topic(topicName)
.subscriptionType(SubscriptionType.Shared)
.subscriptionName("my-subscription")
.subscribe();

PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName).get();
Assert.assertNotNull(topicRef);
Assert.assertEquals(topicRef.getSubscriptions().size(), 1);
List<org.apache.pulsar.broker.service.Consumer> consumers = topicRef.getSubscriptions()
.get("my-subscription").getConsumers();
Assert.assertEquals(consumers.size(), 1);
ConsumerStats consumerStats = new ConsumerStats();
consumerStats.msgOutCounter = 10;
consumerStats.bytesOutCounter = 1280;
consumers.get(0).updateStats(consumerStats);
ConsumerStats updatedStats = consumers.get(0).getStats();

Assert.assertEquals(updatedStats.msgOutCounter, 10);
Assert.assertEquals(updatedStats.bytesOutCounter, 1280);
}
}