Search in sources :

Example 1 with PartitionedTopicInternalStats

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);
    }
}
Also used : RetentionPolicies(org.apache.pulsar.common.policies.data.RetentionPolicies) PartitionedTopicInternalStats(org.apache.pulsar.common.policies.data.PartitionedTopicInternalStats) PersistentTopicInternalStats(org.apache.pulsar.common.policies.data.PersistentTopicInternalStats) Map(java.util.Map) HashMap(java.util.HashMap) MessageId(org.apache.pulsar.client.api.MessageId) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 2 with PartitionedTopicInternalStats

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);
}
Also used : PartitionedTopicInternalStats(org.apache.pulsar.common.policies.data.PartitionedTopicInternalStats) PersistentTopicInternalStats(org.apache.pulsar.common.policies.data.PersistentTopicInternalStats) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 3 with PartitionedTopicInternalStats

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;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) PartitionedTopicInternalStats(org.apache.pulsar.common.policies.data.PartitionedTopicInternalStats) WebTarget(javax.ws.rs.client.WebTarget) TopicName(org.apache.pulsar.common.naming.TopicName)

Example 4 with PartitionedTopicInternalStats

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;
    });
}
Also used : Version(com.github.zafarkhaja.semver.Version) SubscriptionStats(org.apache.pulsar.common.policies.data.SubscriptionStats) ManagedLedgerOfflineBacklog(org.apache.bookkeeper.mledger.impl.ManagedLedgerOfflineBacklog) Topic(org.apache.pulsar.broker.service.Topic) NotAllowedException(org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) LedgerOffloader(org.apache.bookkeeper.mledger.LedgerOffloader) DelayedDeliveryPolicies(org.apache.pulsar.common.policies.data.DelayedDeliveryPolicies) StringUtils(org.apache.commons.lang3.StringUtils) SubscribeRate(org.apache.pulsar.common.policies.data.SubscribeRate) AlreadyRunningException(org.apache.pulsar.broker.service.BrokerServiceException.AlreadyRunningException) TopicOperation(org.apache.pulsar.common.policies.data.TopicOperation) PolicyOperation(org.apache.pulsar.common.policies.data.PolicyOperation) Map(java.util.Map) RestException(org.apache.pulsar.broker.web.RestException) NamespaceOperation(org.apache.pulsar.common.policies.data.NamespaceOperation) DispatchRateImpl(org.apache.pulsar.common.policies.data.impl.DispatchRateImpl) PersistentOfflineTopicStats(org.apache.pulsar.common.policies.data.PersistentOfflineTopicStats) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) MetadataStoreException(org.apache.pulsar.metadata.api.MetadataStoreException) PersistencePolicies(org.apache.pulsar.common.policies.data.PersistencePolicies) AsyncResponse(javax.ws.rs.container.AsyncResponse) EventsTopicNames.checkTopicIsTransactionCoordinatorAssign(org.apache.pulsar.common.events.EventsTopicNames.checkTopicIsTransactionCoordinatorAssign) Set(java.util.Set) PublishRate(org.apache.pulsar.common.policies.data.PublishRate) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) OffloadProcessStatus(org.apache.pulsar.client.admin.OffloadProcessStatus) MessageMetadata(org.apache.pulsar.common.api.proto.MessageMetadata) WebApplicationException(javax.ws.rs.WebApplicationException) PersistentTopicInternalStats(org.apache.pulsar.common.policies.data.PersistentTopicInternalStats) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) Subscription(org.apache.pulsar.broker.service.Subscription) TopicStats(org.apache.pulsar.common.policies.data.TopicStats) PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) ArrayList(java.util.ArrayList) Commands(org.apache.pulsar.common.protocol.Commands) SubType(org.apache.pulsar.common.api.proto.CommandSubscribe.SubType) Lists(com.google.common.collect.Lists) DateFormatter(org.apache.pulsar.common.util.DateFormatter) BiConsumer(java.util.function.BiConsumer) NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) DispatchRate(org.apache.pulsar.common.policies.data.DispatchRate) InitialPosition(org.apache.pulsar.common.api.proto.CommandSubscribe.InitialPosition) ManagedLedgerInfo(org.apache.bookkeeper.mledger.ManagedLedgerInfo) IOException(java.io.IOException) PulsarService(org.apache.pulsar.broker.PulsarService) SubscriptionType(org.apache.pulsar.client.api.SubscriptionType) ExecutionException(java.util.concurrent.ExecutionException) PulsarService.isTransactionInternalName(org.apache.pulsar.broker.PulsarService.isTransactionInternalName) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) SubscriptionInvalidCursorPosition(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionInvalidCursorPosition) AuthorizationService(org.apache.pulsar.broker.authorization.AuthorizationService) DEFAULT_OPERATION_TIMEOUT_SEC(org.apache.pulsar.broker.resources.PulsarResources.DEFAULT_OPERATION_TIMEOUT_SEC) AdminResource(org.apache.pulsar.broker.admin.AdminResource) LoggerFactory(org.slf4j.LoggerFactory) KeyValue(org.apache.pulsar.common.api.proto.KeyValue) TopicStatsImpl(org.apache.pulsar.common.policies.data.stats.TopicStatsImpl) ManagedLedgerFactoryImpl(org.apache.bookkeeper.mledger.impl.ManagedLedgerFactoryImpl) MessageImpl(org.apache.pulsar.client.impl.MessageImpl) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) AuthAction(org.apache.pulsar.common.policies.data.AuthAction) AuthPolicies(org.apache.pulsar.common.policies.data.AuthPolicies) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) BrokerEntryMetadata(org.apache.pulsar.common.api.proto.BrokerEntryMetadata) InactiveTopicPolicies(org.apache.pulsar.common.policies.data.InactiveTopicPolicies) PartitionedTopicInternalStats(org.apache.pulsar.common.policies.data.PartitionedTopicInternalStats) SchemaCompatibilityStrategy(org.apache.pulsar.common.policies.data.SchemaCompatibilityStrategy) OffloadPoliciesImpl(org.apache.pulsar.common.policies.data.OffloadPoliciesImpl) BitSetRecyclable(org.apache.pulsar.common.util.collections.BitSetRecyclable) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CompletionException(java.util.concurrent.CompletionException) StreamingOutput(javax.ws.rs.core.StreamingOutput) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) AuthenticationDataSource(org.apache.pulsar.broker.authentication.AuthenticationDataSource) PulsarByteBufAllocator(org.apache.pulsar.common.allocator.PulsarByteBufAllocator) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) MessageIdImpl(org.apache.pulsar.client.impl.MessageIdImpl) BacklogQuotaImpl(org.apache.pulsar.common.policies.data.impl.BacklogQuotaImpl) Base64(java.util.Base64) List(java.util.List) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) FutureUtil(org.apache.pulsar.common.util.FutureUtil) Response(javax.ws.rs.core.Response) EncryptionKeys(org.apache.pulsar.common.api.proto.EncryptionKeys) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription) PolicyName(org.apache.pulsar.common.policies.data.PolicyName) PartitionedTopicStatsImpl(org.apache.pulsar.common.policies.data.stats.PartitionedTopicStatsImpl) Optional(java.util.Optional) IntStream(java.util.stream.IntStream) CompressionCodec(org.apache.pulsar.common.compression.CompressionCodec) TopicName(org.apache.pulsar.common.naming.TopicName) Entry(org.apache.bookkeeper.mledger.Entry) ManagedLedgerInfoCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ManagedLedgerInfoCallback) BacklogQuota(org.apache.pulsar.common.policies.data.BacklogQuota) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuf(io.netty.buffer.ByteBuf) LongRunningProcessStatus(org.apache.pulsar.client.admin.LongRunningProcessStatus) CompressionCodecProvider(org.apache.pulsar.common.compression.CompressionCodecProvider) TopicDomain(org.apache.pulsar.common.naming.TopicDomain) Status(javax.ws.rs.core.Response.Status) PartitionedManagedLedgerInfo(org.apache.pulsar.common.naming.PartitionedManagedLedgerInfo) ManagedLedgerImpl(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl) RetentionPolicies(org.apache.pulsar.common.policies.data.RetentionPolicies) PersistentReplicator(org.apache.pulsar.broker.service.persistent.PersistentReplicator) Logger(org.slf4j.Logger) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) TopicPolicies(org.apache.pulsar.common.policies.data.TopicPolicies) Maps(com.google.common.collect.Maps) TimeUnit(java.util.concurrent.TimeUnit) Policies(org.apache.pulsar.common.policies.data.Policies) TopicBusyException(org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException) MessageId(org.apache.pulsar.client.api.MessageId) TransactionCoordinatorID(org.apache.pulsar.transaction.coordinator.TransactionCoordinatorID) Collections(java.util.Collections) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) CompletableFuture(java.util.concurrent.CompletableFuture) PartitionedTopicInternalStats(org.apache.pulsar.common.policies.data.PartitionedTopicInternalStats) RestException(org.apache.pulsar.broker.web.RestException) PersistentTopicInternalStats(org.apache.pulsar.common.policies.data.PersistentTopicInternalStats) ArrayList(java.util.ArrayList) List(java.util.List) NotAllowedException(org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) AlreadyRunningException(org.apache.pulsar.broker.service.BrokerServiceException.AlreadyRunningException) RestException(org.apache.pulsar.broker.web.RestException) MetadataStoreException(org.apache.pulsar.metadata.api.MetadataStoreException) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) WebApplicationException(javax.ws.rs.WebApplicationException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) CompletionException(java.util.concurrent.CompletionException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) TopicBusyException(org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException)

Aggregations

PartitionedTopicInternalStats (org.apache.pulsar.common.policies.data.PartitionedTopicInternalStats)4 PersistentTopicInternalStats (org.apache.pulsar.common.policies.data.PersistentTopicInternalStats)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)2 MessageId (org.apache.pulsar.client.api.MessageId)2 TopicName (org.apache.pulsar.common.naming.TopicName)2 PartitionedTopicMetadata (org.apache.pulsar.common.partition.PartitionedTopicMetadata)2 RetentionPolicies (org.apache.pulsar.common.policies.data.RetentionPolicies)2 Test (org.testng.annotations.Test)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Version (com.github.zafarkhaja.semver.Version)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 Sets (com.google.common.collect.Sets)1 ByteBuf (io.netty.buffer.ByteBuf)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Base64 (java.util.Base64)1