use of org.apache.pulsar.common.policies.data.PartitionedTopicInternalStats in project pulsar by apache.
the class AdminApiTest method testPartitionedTopicTruncate.
@Test(timeOut = 20000)
public void testPartitionedTopicTruncate() throws Exception {
final String topicName = "persistent://prop-xyz/ns1/testTruncateTopic-" + UUID.randomUUID().toString();
final String subName = "my-sub";
this.conf.setTopicLevelPoliciesEnabled(true);
this.conf.setSystemTopicEnabled(true);
admin.topics().createPartitionedTopic(topicName, 6);
admin.namespaces().setRetention("prop-xyz/ns1", new RetentionPolicies(60, 50));
List<MessageId> messageIds = publishMessagesOnPersistentTopic(topicName, 10);
admin.topics().createSubscription(topicName, subName, messageIds.get(0));
admin.topics().unload(topicName);
publishMessagesOnPersistentTopic(topicName, 10);
admin.topics().unload(topicName);
publishMessagesOnPersistentTopic(topicName, 10);
admin.topics().truncate(topicName);
PartitionedTopicInternalStats stats = admin.topics().getPartitionedInternalStats(topicName);
for (Map.Entry<String, PersistentTopicInternalStats> statsEntry : stats.partitions.entrySet()) {
assertTrue(statsEntry.getValue().ledgers.size() <= 2);
}
}
use of org.apache.pulsar.common.policies.data.PartitionedTopicInternalStats in project pulsar by apache.
the class AdminApiTest method testGetPartitionedStatsInternal.
@Test
public void testGetPartitionedStatsInternal() throws Exception {
String partitionedTopic = "my-topic" + UUID.randomUUID().toString();
String subName = "my-sub";
assertEquals(admin.topics().getPartitionedTopicList("prop-xyz/ns1"), Lists.newArrayList());
final String partitionedTopicName = "persistent://prop-xyz/ns1/" + partitionedTopic;
admin.topics().createPartitionedTopic(partitionedTopicName, 2);
assertEquals(admin.topics().getPartitionedTopicList("prop-xyz/ns1"), Lists.newArrayList(partitionedTopicName));
assertEquals(admin.topics().getPartitionedTopicMetadata(partitionedTopicName).partitions, 2);
// create consumer and subscription
pulsarClient.newConsumer().topic(partitionedTopicName).subscriptionName(subName).subscribe();
// publish several messages
publishMessagesOnPersistentTopic(partitionedTopicName, 10);
String partitionTopic0 = partitionedTopicName + "-partition-0";
String partitionTopic1 = partitionedTopicName + "-partition-1";
Thread.sleep(1000);
PersistentTopicInternalStats internalStats0 = admin.topics().getInternalStats(partitionTopic0, false);
assertEquals(internalStats0.cursors.keySet(), Sets.newTreeSet(Lists.newArrayList(Codec.encode(subName))));
PersistentTopicInternalStats internalStats1 = admin.topics().getInternalStats(partitionTopic1, false);
assertEquals(internalStats1.cursors.keySet(), Sets.newTreeSet(Lists.newArrayList(Codec.encode(subName))));
// expected internal stats
PartitionedTopicMetadata partitionedTopicMetadata = new PartitionedTopicMetadata(2);
PartitionedTopicInternalStats expectedInternalStats = new PartitionedTopicInternalStats(partitionedTopicMetadata);
expectedInternalStats.partitions.put(partitionTopic0, internalStats0);
expectedInternalStats.partitions.put(partitionTopic1, internalStats1);
// partitioned internal stats
PartitionedTopicInternalStats partitionedInternalStats = admin.topics().getPartitionedInternalStats(partitionedTopicName);
String expectedResult = ObjectMapperFactory.getThreadLocal().writeValueAsString(expectedInternalStats);
String result = ObjectMapperFactory.getThreadLocal().writeValueAsString(partitionedInternalStats);
assertEquals(result, expectedResult);
}
use of org.apache.pulsar.common.policies.data.PartitionedTopicInternalStats in project pulsar by apache.
the class TopicsImpl method getPartitionedInternalStatsAsync.
@Override
public CompletableFuture<PartitionedTopicInternalStats> getPartitionedInternalStatsAsync(String topic) {
TopicName tn = validateTopic(topic);
WebTarget path = topicPath(tn, "partitioned-internalStats");
final CompletableFuture<PartitionedTopicInternalStats> future = new CompletableFuture<>();
asyncGetRequest(path, new InvocationCallback<PartitionedTopicInternalStats>() {
@Override
public void completed(PartitionedTopicInternalStats response) {
future.complete(response);
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
use of org.apache.pulsar.common.policies.data.PartitionedTopicInternalStats in project pulsar by apache.
the class PersistentTopicsBase method internalGetPartitionedStatsInternal.
protected void internalGetPartitionedStatsInternal(AsyncResponse asyncResponse, boolean authoritative) {
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;
}
PartitionedTopicInternalStats stats = new PartitionedTopicInternalStats(partitionMetadata);
List<CompletableFuture<PersistentTopicInternalStats>> topicStatsFutureList = Lists.newArrayList();
for (int i = 0; i < partitionMetadata.partitions; i++) {
try {
topicStatsFutureList.add(pulsar().getAdminClient().topics().getInternalStatsAsync((topicName.getPartition(i).toString()), false));
} catch (PulsarServerException e) {
asyncResponse.resume(new RestException(e));
return;
}
}
FutureUtil.waitForAll(topicStatsFutureList).handle((result, exception) -> {
CompletableFuture<PersistentTopicInternalStats> statFuture = null;
for (int i = 0; i < topicStatsFutureList.size(); i++) {
statFuture = topicStatsFutureList.get(i);
if (statFuture.isDone() && !statFuture.isCompletedExceptionally()) {
try {
stats.partitions.put(topicName.getPartition(i).toString(), statFuture.get());
} catch (Exception e) {
asyncResponse.resume(new RestException(e));
return null;
}
}
}
asyncResponse.resume(!stats.partitions.isEmpty() ? stats : new RestException(Status.NOT_FOUND, "Internal topics have not been generated yet"));
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;
});
}
Aggregations