use of org.apache.pulsar.common.policies.data.SubscriptionStats in project incubator-pulsar by apache.
the class PersistentTopicStatsTest method testPersistentTopicStats.
@Test
public void testPersistentTopicStats() {
PersistentTopicStats persistentTopicStats = new PersistentTopicStats();
persistentTopicStats.msgRateIn = 1;
persistentTopicStats.msgThroughputIn = 1;
persistentTopicStats.msgRateOut = 1;
persistentTopicStats.msgThroughputOut = 1;
persistentTopicStats.averageMsgSize = 1;
persistentTopicStats.storageSize = 1;
persistentTopicStats.publishers.add(new PublisherStats());
persistentTopicStats.subscriptions.put("test_ns", new SubscriptionStats());
persistentTopicStats.replication.put("test_ns", new ReplicatorStats());
PersistentTopicStats target = new PersistentTopicStats();
target.add(persistentTopicStats);
assertEquals(persistentTopicStats.msgRateIn, 1.0);
assertEquals(persistentTopicStats.msgThroughputIn, 1.0);
assertEquals(persistentTopicStats.msgRateOut, 1.0);
assertEquals(persistentTopicStats.msgThroughputOut, 1.0);
assertEquals(persistentTopicStats.averageMsgSize, 1.0);
assertEquals(persistentTopicStats.storageSize, 1);
assertEquals(persistentTopicStats.publishers.size(), 1);
assertEquals(persistentTopicStats.subscriptions.size(), 1);
assertEquals(persistentTopicStats.replication.size(), 1);
persistentTopicStats.reset();
assertEquals(persistentTopicStats.msgRateIn, 0.0);
assertEquals(persistentTopicStats.msgThroughputIn, 0.0);
assertEquals(persistentTopicStats.msgRateOut, 0.0);
assertEquals(persistentTopicStats.msgThroughputOut, 0.0);
assertEquals(persistentTopicStats.averageMsgSize, 0.0);
assertEquals(persistentTopicStats.storageSize, 0);
assertEquals(persistentTopicStats.publishers.size(), 0);
assertEquals(persistentTopicStats.subscriptions.size(), 0);
assertEquals(persistentTopicStats.replication.size(), 0);
}
use of org.apache.pulsar.common.policies.data.SubscriptionStats in project incubator-pulsar by apache.
the class BrokerServiceTest method testBrokerServicePersistentRedeliverTopicStats.
@Test
public void testBrokerServicePersistentRedeliverTopicStats() throws Exception {
final String topicName = "persistent://prop/use/ns-abc/successSharedTopic";
final String subName = "successSharedSub";
PersistentTopicStats stats;
SubscriptionStats subStats;
Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName).subscriptionType(SubscriptionType.Shared).subscribe();
Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
assertNotNull(topicRef);
rolloverPerIntervalStats();
stats = topicRef.getStats();
subStats = stats.subscriptions.values().iterator().next();
// subscription stats
assertEquals(stats.subscriptions.keySet().size(), 1);
assertEquals(subStats.msgBacklog, 0);
assertEquals(subStats.consumers.size(), 1);
Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create();
Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
for (int i = 0; i < 10; i++) {
String message = "my-message-" + i;
producer.send(message.getBytes());
}
Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
rolloverPerIntervalStats();
stats = topicRef.getStats();
subStats = stats.subscriptions.values().iterator().next();
// publisher stats
assertEquals(subStats.msgBacklog, 10);
assertEquals(stats.publishers.size(), 1);
assertTrue(stats.publishers.get(0).msgRateIn > 0.0);
assertTrue(stats.publishers.get(0).msgThroughputIn > 0.0);
assertTrue(stats.publishers.get(0).averageMsgSize > 0.0);
// aggregated publish stats
assertEquals(stats.msgRateIn, stats.publishers.get(0).msgRateIn);
assertEquals(stats.msgThroughputIn, stats.publishers.get(0).msgThroughputIn);
double diff = stats.averageMsgSize - stats.publishers.get(0).averageMsgSize;
assertTrue(Math.abs(diff) < 0.000001);
// consumer stats
assertTrue(subStats.consumers.get(0).msgRateOut > 0.0);
assertTrue(subStats.consumers.get(0).msgThroughputOut > 0.0);
assertEquals(subStats.msgRateRedeliver, 0.0);
assertEquals(subStats.consumers.get(0).unackedMessages, 10);
// aggregated consumer stats
assertEquals(subStats.msgRateOut, subStats.consumers.get(0).msgRateOut);
assertEquals(subStats.msgThroughputOut, subStats.consumers.get(0).msgThroughputOut);
assertEquals(subStats.msgRateRedeliver, subStats.consumers.get(0).msgRateRedeliver);
assertEquals(stats.msgRateOut, subStats.consumers.get(0).msgRateOut);
assertEquals(stats.msgThroughputOut, subStats.consumers.get(0).msgThroughputOut);
assertEquals(subStats.msgRateRedeliver, subStats.consumers.get(0).msgRateRedeliver);
assertEquals(subStats.unackedMessages, subStats.consumers.get(0).unackedMessages);
consumer.redeliverUnacknowledgedMessages();
Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
rolloverPerIntervalStats();
stats = topicRef.getStats();
subStats = stats.subscriptions.values().iterator().next();
assertTrue(subStats.msgRateRedeliver > 0.0);
assertEquals(subStats.msgRateRedeliver, subStats.consumers.get(0).msgRateRedeliver);
Message<byte[]> msg;
for (int i = 0; i < 10; i++) {
msg = consumer.receive();
consumer.acknowledge(msg);
}
consumer.close();
Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
rolloverPerIntervalStats();
stats = topicRef.getStats();
subStats = stats.subscriptions.values().iterator().next();
assertEquals(subStats.msgBacklog, 0);
}
use of org.apache.pulsar.common.policies.data.SubscriptionStats in project incubator-pulsar by apache.
the class DispatcherBlockConsumerTest method testBlockDispatcherStats.
@Test
public void testBlockDispatcherStats() throws Exception {
int orginalDispatcherLimit = conf.getMaxUnackedMessagesPerSubscription();
try {
final String topicName = "persistent://prop/use/ns-abc/blockDispatch";
final String subName = "blockDispatch";
final int timeWaitToSync = 100;
PersistentTopicStats stats;
SubscriptionStats subStats;
// configure maxUnackMessagePerDispatcher then restart broker to get this change
conf.setMaxUnackedMessagesPerSubscription(10);
stopBroker();
startBroker();
Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName).subscriptionType(SubscriptionType.Shared).subscribe();
Thread.sleep(timeWaitToSync);
PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
assertNotNull(topicRef);
rolloverPerIntervalStats();
stats = topicRef.getStats();
subStats = stats.subscriptions.values().iterator().next();
// subscription stats
assertEquals(stats.subscriptions.keySet().size(), 1);
assertEquals(subStats.msgBacklog, 0);
assertEquals(subStats.consumers.size(), 1);
Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create();
Thread.sleep(timeWaitToSync);
for (int i = 0; i < 100; i++) {
String message = "my-message-" + i;
producer.send(message.getBytes());
}
Thread.sleep(timeWaitToSync);
rolloverPerIntervalStats();
stats = topicRef.getStats();
subStats = stats.subscriptions.values().iterator().next();
assertTrue(subStats.msgBacklog > 0);
assertTrue(subStats.unackedMessages > 0);
assertTrue(subStats.blockedSubscriptionOnUnackedMsgs);
assertEquals(subStats.consumers.get(0).unackedMessages, subStats.unackedMessages);
// consumer stats
assertTrue(subStats.consumers.get(0).msgRateOut > 0.0);
assertTrue(subStats.consumers.get(0).msgThroughputOut > 0.0);
assertEquals(subStats.msgRateRedeliver, 0.0);
producer.close();
consumer.close();
} finally {
conf.setMaxUnackedMessagesPerSubscription(orginalDispatcherLimit);
}
}
use of org.apache.pulsar.common.policies.data.SubscriptionStats in project incubator-pulsar by apache.
the class NonPersistentTopicTest method testReplicator.
/**
* verifies that non-persistent topic replicates using replicator
*/
@Test
public void testReplicator() throws Exception {
ReplicationClusterManager replication = new ReplicationClusterManager();
replication.setupReplicationCluster();
try {
final String globalTopicName = "non-persistent://pulsar/global/ns/nonPersistentTopic";
final int timeWaitToSync = 100;
NonPersistentTopicStats stats;
SubscriptionStats subStats;
PulsarClient client1 = PulsarClient.builder().serviceUrl(replication.url1.toString()).build();
PulsarClient client2 = PulsarClient.builder().serviceUrl(replication.url2.toString()).build();
PulsarClient client3 = PulsarClient.builder().serviceUrl(replication.url3.toString()).build();
ConsumerImpl<byte[]> consumer1 = (ConsumerImpl<byte[]>) client1.newConsumer().topic(globalTopicName).subscriptionName("subscriber-1").subscribe();
ConsumerImpl<byte[]> consumer2 = (ConsumerImpl<byte[]>) client1.newConsumer().topic(globalTopicName).subscriptionName("subscriber-2").subscribe();
ConsumerImpl<byte[]> repl2Consumer = (ConsumerImpl<byte[]>) client2.newConsumer().topic(globalTopicName).subscriptionName("subscriber-1").subscribe();
ConsumerImpl<byte[]> repl3Consumer = (ConsumerImpl<byte[]>) client3.newConsumer().topic(globalTopicName).subscriptionName("subscriber-1").subscribe();
Producer<byte[]> producer = client1.newProducer().topic(globalTopicName).create();
Thread.sleep(timeWaitToSync);
PulsarService replicationPulasr = replication.pulsar1;
// Replicator for r1 -> r2,r3
NonPersistentTopic topicRef = (NonPersistentTopic) replication.pulsar1.getBrokerService().getTopicReference(globalTopicName);
NonPersistentReplicator replicatorR2 = (NonPersistentReplicator) topicRef.getPersistentReplicator("r2");
NonPersistentReplicator replicatorR3 = (NonPersistentReplicator) topicRef.getPersistentReplicator("r3");
assertNotNull(topicRef);
assertNotNull(replicatorR2);
assertNotNull(replicatorR3);
rolloverPerIntervalStats(replicationPulasr);
stats = topicRef.getStats();
subStats = stats.getSubscriptions().values().iterator().next();
// subscription stats
assertEquals(stats.getSubscriptions().keySet().size(), 2);
assertEquals(subStats.consumers.size(), 1);
Thread.sleep(timeWaitToSync);
int totalProducedMessages = 100;
for (int i = 0; i < totalProducedMessages; i++) {
String message = "my-message-" + i;
producer.send(message.getBytes());
}
// (1) consume by consumer1
Message<?> msg = null;
Set<String> messageSet = Sets.newHashSet();
for (int i = 0; i < totalProducedMessages; i++) {
msg = consumer1.receive(300, TimeUnit.MILLISECONDS);
if (msg != null) {
String receivedMessage = new String(msg.getData());
testMessageOrderAndDuplicates(messageSet, receivedMessage, "my-message-" + i);
} else {
break;
}
}
assertEquals(messageSet.size(), totalProducedMessages);
// (2) consume by consumer2
messageSet.clear();
for (int i = 0; i < totalProducedMessages; i++) {
msg = consumer2.receive(300, TimeUnit.MILLISECONDS);
if (msg != null) {
String receivedMessage = new String(msg.getData());
testMessageOrderAndDuplicates(messageSet, receivedMessage, "my-message-" + i);
} else {
break;
}
}
assertEquals(messageSet.size(), totalProducedMessages);
// (3) consume by repl2consumer
messageSet.clear();
for (int i = 0; i < totalProducedMessages; i++) {
msg = repl2Consumer.receive(300, TimeUnit.MILLISECONDS);
if (msg != null) {
String receivedMessage = new String(msg.getData());
testMessageOrderAndDuplicates(messageSet, receivedMessage, "my-message-" + i);
} else {
break;
}
}
assertEquals(messageSet.size(), totalProducedMessages);
// (4) consume by repl3consumer
messageSet.clear();
for (int i = 0; i < totalProducedMessages; i++) {
msg = repl3Consumer.receive(300, TimeUnit.MILLISECONDS);
if (msg != null) {
String receivedMessage = new String(msg.getData());
testMessageOrderAndDuplicates(messageSet, receivedMessage, "my-message-" + i);
} else {
break;
}
}
assertEquals(messageSet.size(), totalProducedMessages);
Thread.sleep(timeWaitToSync);
rolloverPerIntervalStats(replicationPulasr);
stats = topicRef.getStats();
subStats = stats.getSubscriptions().values().iterator().next();
assertTrue(subStats.msgRateOut > 0);
assertEquals(subStats.consumers.size(), 1);
assertTrue(subStats.msgThroughputOut > 0);
// consumer stats
assertTrue(subStats.consumers.get(0).msgRateOut > 0.0);
assertTrue(subStats.consumers.get(0).msgThroughputOut > 0.0);
assertEquals(subStats.msgRateRedeliver, 0.0);
producer.close();
consumer1.close();
repl2Consumer.close();
repl3Consumer.close();
client1.close();
client2.close();
client3.close();
} finally {
replication.shutdownReplicationCluster();
}
}
use of org.apache.pulsar.common.policies.data.SubscriptionStats in project incubator-pulsar by apache.
the class PersistentTopic method getStats.
public PersistentTopicStats getStats() {
PersistentTopicStats stats = new PersistentTopicStats();
ObjectObjectHashMap<String, PublisherStats> remotePublishersStats = new ObjectObjectHashMap<String, PublisherStats>();
producers.forEach(producer -> {
PublisherStats publisherStats = producer.getStats();
stats.msgRateIn += publisherStats.msgRateIn;
stats.msgThroughputIn += publisherStats.msgThroughputIn;
if (producer.isRemote()) {
remotePublishersStats.put(producer.getRemoteCluster(), publisherStats);
} else {
stats.publishers.add(publisherStats);
}
});
stats.averageMsgSize = stats.msgRateIn == 0.0 ? 0.0 : (stats.msgThroughputIn / stats.msgRateIn);
subscriptions.forEach((name, subscription) -> {
SubscriptionStats subStats = subscription.getStats();
stats.msgRateOut += subStats.msgRateOut;
stats.msgThroughputOut += subStats.msgThroughputOut;
stats.subscriptions.put(name, subStats);
});
replicators.forEach((cluster, replicator) -> {
ReplicatorStats replicatorStats = replicator.getStats();
// Add incoming msg rates
PublisherStats pubStats = remotePublishersStats.get(replicator.getRemoteCluster());
if (pubStats != null) {
replicatorStats.msgRateIn = pubStats.msgRateIn;
replicatorStats.msgThroughputIn = pubStats.msgThroughputIn;
replicatorStats.inboundConnection = pubStats.address;
replicatorStats.inboundConnectedSince = pubStats.connectedSince;
}
stats.msgRateOut += replicatorStats.msgRateOut;
stats.msgThroughputOut += replicatorStats.msgThroughputOut;
stats.replication.put(replicator.getRemoteCluster(), replicatorStats);
});
stats.storageSize = ledger.getEstimatedBacklogSize();
stats.deduplicationStatus = messageDeduplication.getStatus().toString();
return stats;
}
Aggregations