Search in sources :

Example 16 with Topic

use of org.apache.pulsar.broker.service.Topic in project incubator-pulsar by apache.

the class NonPersistentTopic method publishMessage.

@Override
public void publishMessage(ByteBuf data, PublishContext callback) {
    AtomicInteger msgDeliveryCount = new AtomicInteger(2);
    ENTRIES_ADDED_COUNTER_UPDATER.incrementAndGet(this);
    // retain data for sub/replication because io-thread will release actual payload
    data.retain(2);
    this.executor.submitOrdered(topic, SafeRun.safeRun(() -> {
        subscriptions.forEach((name, subscription) -> {
            ByteBuf duplicateBuffer = data.retainedDuplicate();
            Entry entry = create(0L, 0L, duplicateBuffer);
            // entry internally retains data so, duplicateBuffer should be release here
            duplicateBuffer.release();
            if (subscription.getDispatcher() != null) {
                subscription.getDispatcher().sendMessages(Lists.newArrayList(entry));
            } else {
                // it happens when subscription is created but dispatcher is not created as consumer is not added
                // yet
                entry.release();
            }
        });
        data.release();
        if (msgDeliveryCount.decrementAndGet() == 0) {
            callback.completed(null, 0L, 0L);
        }
    }));
    this.executor.submitOrdered(topic, SafeRun.safeRun(() -> {
        replicators.forEach((name, replicator) -> {
            ByteBuf duplicateBuffer = data.retainedDuplicate();
            Entry entry = create(0L, 0L, duplicateBuffer);
            // entry internally retains data so, duplicateBuffer should be release here
            duplicateBuffer.release();
            ((NonPersistentReplicator) replicator).sendMessage(entry);
        });
        data.release();
        if (msgDeliveryCount.decrementAndGet() == 0) {
            callback.completed(null, 0L, 0L);
        }
    }));
}
Also used : NamingException(org.apache.pulsar.broker.service.BrokerServiceException.NamingException) Topic(org.apache.pulsar.broker.service.Topic) AdminResource(org.apache.pulsar.broker.admin.AdminResource) NotAllowedException(org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException) SchemaVersion(org.apache.pulsar.common.schema.SchemaVersion) UnsupportedVersionException(org.apache.pulsar.broker.service.BrokerServiceException.UnsupportedVersionException) LoggerFactory(org.slf4j.LoggerFactory) ObjectObjectHashMap(com.carrotsearch.hppc.ObjectObjectHashMap) ProducerBusyException(org.apache.pulsar.broker.service.BrokerServiceException.ProducerBusyException) EntryCacheManager.create(org.apache.bookkeeper.mledger.impl.EntryCacheManager.create) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) StatsOutputStream(org.apache.pulsar.utils.StatsOutputStream) OrderedScheduler(org.apache.bookkeeper.common.util.OrderedScheduler) NonPersistentPublisherStats(org.apache.pulsar.common.policies.data.NonPersistentPublisherStats) Set(java.util.Set) Position(org.apache.bookkeeper.mledger.Position) BrokerService(org.apache.pulsar.broker.service.BrokerService) Sets(com.google.common.collect.Sets) NamespaceStats(org.apache.pulsar.broker.stats.NamespaceStats) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) ServerCnx(org.apache.pulsar.broker.service.ServerCnx) List(java.util.List) FutureUtil(org.apache.pulsar.common.util.FutureUtil) ConcurrentOpenHashSet(org.apache.pulsar.common.util.collections.ConcurrentOpenHashSet) SafeRun(org.apache.bookkeeper.mledger.util.SafeRun) ConcurrentOpenHashMap(org.apache.pulsar.common.util.collections.ConcurrentOpenHashMap) SchemaData(org.apache.pulsar.common.schema.SchemaData) PersistentTopicInternalStats(org.apache.pulsar.common.policies.data.PersistentTopicInternalStats) NamespaceBundleStats(org.apache.pulsar.policies.data.loadbalancer.NamespaceBundleStats) NonPersistentTopicStats(org.apache.pulsar.common.policies.data.NonPersistentTopicStats) PublisherStats(org.apache.pulsar.common.policies.data.PublisherStats) NonPersistentReplicatorStats(org.apache.pulsar.common.policies.data.NonPersistentReplicatorStats) Subscription(org.apache.pulsar.broker.service.Subscription) TopicName(org.apache.pulsar.common.naming.TopicName) CursorStats(org.apache.pulsar.common.policies.data.PersistentTopicInternalStats.CursorStats) Entry(org.apache.bookkeeper.mledger.Entry) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BacklogQuota(org.apache.pulsar.common.policies.data.BacklogQuota) CompletableFuture(java.util.concurrent.CompletableFuture) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Consumer(org.apache.pulsar.broker.service.Consumer) NonPersistentSubscriptionStats(org.apache.pulsar.common.policies.data.NonPersistentSubscriptionStats) ServerMetadataException(org.apache.pulsar.broker.service.BrokerServiceException.ServerMetadataException) Lists(com.google.common.collect.Lists) ByteBuf(io.netty.buffer.ByteBuf) FastThreadLocal(io.netty.util.concurrent.FastThreadLocal) ClusterReplicationMetrics(org.apache.pulsar.broker.stats.ClusterReplicationMetrics) Logger(org.slf4j.Logger) SubType(org.apache.pulsar.common.api.proto.PulsarApi.CommandSubscribe.SubType) KeeperException(org.apache.zookeeper.KeeperException) BrokerServiceException(org.apache.pulsar.broker.service.BrokerServiceException) MoreObjects(com.google.common.base.MoreObjects) AtomicLongFieldUpdater(java.util.concurrent.atomic.AtomicLongFieldUpdater) Producer(org.apache.pulsar.broker.service.Producer) Maps(com.google.common.collect.Maps) TopicFencedException(org.apache.pulsar.broker.service.BrokerServiceException.TopicFencedException) 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) POLICIES(org.apache.pulsar.broker.cache.ConfigurationCacheService.POLICIES) ConsumerBusyException(org.apache.pulsar.broker.service.BrokerServiceException.ConsumerBusyException) Replicator(org.apache.pulsar.broker.service.Replicator) ConsumerStats(org.apache.pulsar.common.policies.data.ConsumerStats) InitialPosition(org.apache.pulsar.common.api.proto.PulsarApi.CommandSubscribe.InitialPosition) Collections(java.util.Collections) Entry(org.apache.bookkeeper.mledger.Entry) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ByteBuf(io.netty.buffer.ByteBuf)

Example 17 with Topic

use of org.apache.pulsar.broker.service.Topic in project incubator-pulsar by apache.

the class NamespacesBase method clearBacklog.

private void clearBacklog(NamespaceName nsName, String bundleRange, String subscription) {
    try {
        List<Topic> topicList = pulsar().getBrokerService().getAllTopicsFromNamespaceBundle(nsName.toString(), nsName.toString() + "/" + bundleRange);
        List<CompletableFuture<Void>> futures = Lists.newArrayList();
        if (subscription != null) {
            if (subscription.startsWith(pulsar().getConfiguration().getReplicatorPrefix())) {
                subscription = PersistentReplicator.getRemoteCluster(subscription);
            }
            for (Topic topic : topicList) {
                if (topic instanceof PersistentTopic) {
                    futures.add(((PersistentTopic) topic).clearBacklog(subscription));
                }
            }
        } else {
            for (Topic topic : topicList) {
                if (topic instanceof PersistentTopic) {
                    futures.add(((PersistentTopic) topic).clearBacklog());
                }
            }
        }
        FutureUtil.waitForAll(futures).get();
    } catch (Exception e) {
        log.error("[{}] Failed to clear backlog for namespace {}/{}, subscription: {}", clientAppId(), nsName.toString(), bundleRange, subscription, e);
        throw new RestException(e);
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) RestException(org.apache.pulsar.broker.web.RestException) Topic(org.apache.pulsar.broker.service.Topic) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) RestException(org.apache.pulsar.broker.web.RestException) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) WebApplicationException(javax.ws.rs.WebApplicationException) KeeperException(org.apache.zookeeper.KeeperException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) ExecutionException(java.util.concurrent.ExecutionException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException)

Example 18 with Topic

use of org.apache.pulsar.broker.service.Topic in project incubator-pulsar by apache.

the class PersistentTopicsBase method getTopicReference.

/**
 * Get the Topic object reference from the Pulsar broker
 */
private Topic getTopicReference(TopicName topicName) {
    try {
        Topic topic = pulsar().getBrokerService().getTopicReference(topicName.toString());
        checkNotNull(topic);
        return topic;
    } catch (Exception e) {
        throw new RestException(Status.NOT_FOUND, "Topic not found");
    }
}
Also used : RestException(org.apache.pulsar.broker.web.RestException) Topic(org.apache.pulsar.broker.service.Topic) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) NotAllowedException(org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) RestException(org.apache.pulsar.broker.web.RestException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) WebApplicationException(javax.ws.rs.WebApplicationException) KeeperException(org.apache.zookeeper.KeeperException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TopicBusyException(org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException)

Example 19 with Topic

use of org.apache.pulsar.broker.service.Topic in project incubator-pulsar by apache.

the class PersistentTopicsBase method internalGetInternalStats.

protected PersistentTopicInternalStats internalGetInternalStats(boolean authoritative) {
    validateAdminAndClientPermission();
    if (topicName.isGlobal()) {
        validateGlobalNamespaceOwnership(namespaceName);
    }
    validateTopicOwnership(topicName, authoritative);
    Topic topic = getTopicReference(topicName);
    return topic.getInternalStats();
}
Also used : Topic(org.apache.pulsar.broker.service.Topic) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic)

Example 20 with Topic

use of org.apache.pulsar.broker.service.Topic in project incubator-pulsar by apache.

the class PersistentTopicsBase method internalDeleteTopic.

protected void internalDeleteTopic(boolean authoritative) {
    validateAdminOperationOnTopic(authoritative);
    Topic topic = getTopicReference(topicName);
    // v2 topics have a global name so check if the topic is replicated.
    if (topic.isReplicated()) {
        // Delete is disallowed on global topic
        log.error("[{}] Delete topic is forbidden on global namespace {}", clientAppId(), topicName);
        throw new RestException(Status.FORBIDDEN, "Delete forbidden on global namespace");
    }
    try {
        topic.delete().get();
        log.info("[{}] Successfully removed topic {}", clientAppId(), topicName);
    } catch (Exception e) {
        Throwable t = e.getCause();
        log.error("[{}] Failed to get delete topic {}", clientAppId(), topicName, t);
        if (t instanceof TopicBusyException) {
            throw new RestException(Status.PRECONDITION_FAILED, "Topic has active producers/subscriptions");
        } else {
            throw new RestException(t);
        }
    }
}
Also used : TopicBusyException(org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException) RestException(org.apache.pulsar.broker.web.RestException) Topic(org.apache.pulsar.broker.service.Topic) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) NotAllowedException(org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) RestException(org.apache.pulsar.broker.web.RestException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) WebApplicationException(javax.ws.rs.WebApplicationException) KeeperException(org.apache.zookeeper.KeeperException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TopicBusyException(org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException)

Aggregations

Topic (org.apache.pulsar.broker.service.Topic)28 PersistentTopic (org.apache.pulsar.broker.service.persistent.PersistentTopic)15 KeeperException (org.apache.zookeeper.KeeperException)15 SubscriptionBusyException (org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)13 NotAllowedException (org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException)11 TopicBusyException (org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException)11 CompletableFuture (java.util.concurrent.CompletableFuture)10 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)10 RestException (org.apache.pulsar.broker.web.RestException)10 ExecutionException (java.util.concurrent.ExecutionException)9 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)9 WebApplicationException (javax.ws.rs.WebApplicationException)8 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)8 IOException (java.io.IOException)6 Subscription (org.apache.pulsar.broker.service.Subscription)6 ObjectObjectHashMap (com.carrotsearch.hppc.ObjectObjectHashMap)5 MoreObjects (com.google.common.base.MoreObjects)5 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)5 Lists (com.google.common.collect.Lists)5 Maps (com.google.common.collect.Maps)5