use of org.apache.pulsar.common.policies.data.stats.PartitionedTopicStatsImpl in project pulsar by apache.
the class PersistentTopicsBase method internalGetPartitionedStats.
protected void internalGetPartitionedStats(AsyncResponse asyncResponse, boolean authoritative, boolean perPartition, boolean getPreciseBacklog, boolean subscriptionBacklogSize, boolean getEarliestTimeInBacklog) {
CompletableFuture<Void> future;
if (topicName.isGlobal()) {
future = validateGlobalNamespaceOwnershipAsync(namespaceName);
} else {
future = CompletableFuture.completedFuture(null);
}
future.thenCompose(__ -> getPartitionedTopicMetadataAsync(topicName, authoritative, false)).thenAccept(partitionMetadata -> {
if (partitionMetadata.partitions == 0) {
asyncResponse.resume(new RestException(Status.NOT_FOUND, "Partitioned Topic not found"));
return;
}
PartitionedTopicStatsImpl stats = new PartitionedTopicStatsImpl(partitionMetadata);
List<CompletableFuture<TopicStats>> topicStatsFutureList = Lists.newArrayList();
for (int i = 0; i < partitionMetadata.partitions; i++) {
try {
topicStatsFutureList.add(pulsar().getAdminClient().topics().getStatsAsync((topicName.getPartition(i).toString()), getPreciseBacklog, subscriptionBacklogSize, getEarliestTimeInBacklog));
} catch (PulsarServerException e) {
asyncResponse.resume(new RestException(e));
return;
}
}
FutureUtil.waitForAll(topicStatsFutureList).handle((result, exception) -> {
CompletableFuture<TopicStats> statFuture = null;
for (int i = 0; i < topicStatsFutureList.size(); i++) {
statFuture = topicStatsFutureList.get(i);
if (statFuture.isDone() && !statFuture.isCompletedExceptionally()) {
try {
stats.add(statFuture.get());
if (perPartition) {
stats.getPartitions().put(topicName.getPartition(i).toString(), (TopicStatsImpl) statFuture.get());
}
} catch (Exception e) {
asyncResponse.resume(new RestException(e));
return null;
}
}
}
if (perPartition && stats.partitions.isEmpty()) {
try {
boolean pathExists = namespaceResources().getPartitionedTopicResources().partitionedTopicExists(topicName);
if (pathExists) {
stats.partitions.put(topicName.toString(), new TopicStatsImpl());
} else {
asyncResponse.resume(new RestException(Status.NOT_FOUND, "Internal topics have not been generated yet"));
return null;
}
} catch (Exception e) {
asyncResponse.resume(new RestException(e));
return null;
}
}
asyncResponse.resume(stats);
return null;
});
}).exceptionally(ex -> {
// If the exception is not redirect exception we need to log it.
if (!isRedirectException(ex)) {
log.error("[{}] Failed to get partitioned internal stats for {}", clientAppId(), topicName, ex);
}
resumeAsyncResponseExceptionally(asyncResponse, ex);
return null;
});
}
use of org.apache.pulsar.common.policies.data.stats.PartitionedTopicStatsImpl in project pulsar by apache.
the class PartitionedTopicStatsTest method testPartitionedTopicStats.
@Test
public void testPartitionedTopicStats() {
PartitionedTopicStatsImpl partitionedTopicStats = new PartitionedTopicStatsImpl();
partitionedTopicStats.msgRateIn = 1;
partitionedTopicStats.msgThroughputIn = 1;
partitionedTopicStats.msgRateOut = 1;
partitionedTopicStats.msgThroughputOut = 1;
partitionedTopicStats.averageMsgSize = 1;
partitionedTopicStats.storageSize = 1;
partitionedTopicStats.addPublisher((new PublisherStatsImpl()));
partitionedTopicStats.subscriptions.put("test_ns", new SubscriptionStatsImpl());
partitionedTopicStats.replication.put("test_ns", new ReplicatorStatsImpl());
partitionedTopicStats.metadata.partitions = 1;
partitionedTopicStats.partitions.put("test", partitionedTopicStats);
partitionedTopicStats.reset();
assertEquals(partitionedTopicStats.msgRateIn, 0.0);
assertEquals(partitionedTopicStats.msgThroughputIn, 0.0);
assertEquals(partitionedTopicStats.msgRateOut, 0.0);
assertEquals(partitionedTopicStats.msgThroughputOut, 0.0);
assertEquals(partitionedTopicStats.averageMsgSize, 0.0);
assertEquals(partitionedTopicStats.storageSize, 0);
assertEquals(partitionedTopicStats.getPublishers().size(), 0);
assertEquals(partitionedTopicStats.subscriptions.size(), 0);
assertEquals(partitionedTopicStats.replication.size(), 0);
assertEquals(partitionedTopicStats.metadata.partitions, 0);
assertEquals(partitionedTopicStats.partitions.size(), 0);
}
Aggregations