Search in sources :

Example 6 with NamingException

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

the class ReplicatorTest method testReplicatorOnPartitionedTopic.

/**
 * It verifies that broker should not start replicator for partitioned-topic (topic without -partition postfix)
 *
 * @param isPartitionedTopic
 * @throws Exception
 */
@Test(dataProvider = "partitionedTopic")
public void testReplicatorOnPartitionedTopic(boolean isPartitionedTopic) throws Exception {
    log.info("--- Starting ReplicatorTest::{} --- ", methodName);
    final String namespace = "pulsar/global/partitionedNs-" + isPartitionedTopic;
    final String persistentTopicName = "persistent://" + namespace + "/partTopic-" + isPartitionedTopic;
    final String nonPersistentTopicName = "non-persistent://" + namespace + "/partTopic-" + isPartitionedTopic;
    BrokerService brokerService = pulsar1.getBrokerService();
    admin1.namespaces().createNamespace(namespace);
    admin1.namespaces().setNamespaceReplicationClusters(namespace, Lists.newArrayList("r1", "r2", "r3"));
    if (isPartitionedTopic) {
        admin1.persistentTopics().createPartitionedTopic(persistentTopicName, 5);
        admin1.nonPersistentTopics().createPartitionedTopic(nonPersistentTopicName, 5);
    }
    // load namespace with dummy topic on ns
    PulsarClient client = PulsarClient.builder().serviceUrl(url1.toString()).build();
    client.newProducer().topic("persistent://" + namespace + "/dummyTopic").create();
    // persistent topic test
    try {
        brokerService.getTopic(persistentTopicName).get();
        if (isPartitionedTopic) {
            fail("Topic creation fails with partitioned topic as replicator init fails");
        }
    } catch (Exception e) {
        if (!isPartitionedTopic) {
            fail("Topic creation should not fail without any partitioned topic");
        }
        assertTrue(e.getCause() instanceof NamingException);
    }
    // non-persistent topic test
    try {
        brokerService.getTopic(nonPersistentTopicName).get();
        if (isPartitionedTopic) {
            fail("Topic creation fails with partitioned topic as replicator init fails");
        }
    } catch (Exception e) {
        if (!isPartitionedTopic) {
            fail("Topic creation should not fail without any partitioned topic");
        }
        assertTrue(e.getCause() instanceof NamingException);
    }
}
Also used : NamingException(org.apache.pulsar.broker.service.BrokerServiceException.NamingException) PulsarClient(org.apache.pulsar.client.api.PulsarClient) NamingException(org.apache.pulsar.broker.service.BrokerServiceException.NamingException) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CursorAlreadyClosedException(org.apache.bookkeeper.mledger.ManagedLedgerException.CursorAlreadyClosedException) Test(org.testng.annotations.Test)

Example 7 with NamingException

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

the class AbstractReplicator method validatePartitionedTopic.

/**
 * Replication can't be started on root-partitioned-topic to avoid producer startup conflict.
 *
 * <pre>
 * eg:
 * if topic : persistent://prop/cluster/ns/my-topic is a partitioned topic with 2 partitions then
 * broker explicitly creates replicator producer for: "my-topic-partition-1" and "my-topic-partition-2".
 *
 * However, if broker tries to start producer with root topic "my-topic" then client-lib internally creates individual
 * producers for "my-topic-partition-1" and "my-topic-partition-2" which creates conflict with existing
 * replicator producers.
 * </pre>
 *
 * Therefore, replicator can't be started on root-partition topic which can internally create multiple partitioned
 * producers.
 *
 * @param topic
 * @param brokerService
 */
private void validatePartitionedTopic(String topic, BrokerService brokerService) throws NamingException {
    TopicName topicName = TopicName.get(topic);
    String partitionedTopicPath = path(AdminResource.PARTITIONED_TOPIC_PATH_ZNODE, topicName.getNamespace().toString(), topicName.getDomain().toString(), topicName.getEncodedLocalName());
    boolean isPartitionedTopic = false;
    try {
        isPartitionedTopic = brokerService.pulsar().getConfigurationCache().policiesCache().get(partitionedTopicPath).isPresent();
    } catch (Exception e) {
        log.warn("Failed to verify partitioned topic {}-{}", topicName, e.getMessage());
    }
    if (isPartitionedTopic) {
        throw new NamingException(topicName + " is a partitioned-topic and replication can't be started for partitioned-producer ");
    }
}
Also used : NamingException(org.apache.pulsar.broker.service.BrokerServiceException.NamingException) NamingException(org.apache.pulsar.broker.service.BrokerServiceException.NamingException) TopicBusyException(org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException) TopicName(org.apache.pulsar.common.naming.TopicName)

Example 8 with NamingException

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

the class NonPersistentTopic method subscribe.

@Override
public CompletableFuture<Consumer> subscribe(final ServerCnx cnx, String subscriptionName, long consumerId, SubType subType, int priorityLevel, String consumerName, boolean isDurable, MessageId startMessageId, Map<String, String> metadata, boolean readCompacted, InitialPosition initialPosition) {
    final CompletableFuture<Consumer> future = new CompletableFuture<>();
    if (hasBatchMessagePublished && !cnx.isBatchMessageCompatibleVersion()) {
        if (log.isDebugEnabled()) {
            log.debug("[{}] Consumer doesn't support batch-message {}", topic, subscriptionName);
        }
        future.completeExceptionally(new UnsupportedVersionException("Consumer doesn't support batch-message"));
        return future;
    }
    if (subscriptionName.startsWith(replicatorPrefix)) {
        log.warn("[{}] Failed to create subscription for {}", topic, subscriptionName);
        future.completeExceptionally(new NamingException("Subscription with reserved subscription name attempted"));
        return future;
    }
    if (readCompacted) {
        future.completeExceptionally(new NotAllowedException("readCompacted only valid on persistent topics"));
        return future;
    }
    lock.readLock().lock();
    try {
        if (isFenced) {
            log.warn("[{}] Attempting to subscribe to a fenced topic", topic);
            future.completeExceptionally(new TopicFencedException("Topic is temporarily unavailable"));
            return future;
        }
        USAGE_COUNT_UPDATER.incrementAndGet(this);
        if (log.isDebugEnabled()) {
            log.debug("[{}] [{}] [{}] Added consumer -- count: {}", topic, subscriptionName, consumerName, USAGE_COUNT_UPDATER.get(this));
        }
    } finally {
        lock.readLock().unlock();
    }
    NonPersistentSubscription subscription = subscriptions.computeIfAbsent(subscriptionName, name -> new NonPersistentSubscription(this, subscriptionName));
    try {
        Consumer consumer = new Consumer(subscription, subType, topic, consumerId, priorityLevel, consumerName, 0, cnx, cnx.getRole(), metadata, readCompacted, initialPosition);
        subscription.addConsumer(consumer);
        if (!cnx.isActive()) {
            consumer.close();
            if (log.isDebugEnabled()) {
                log.debug("[{}] [{}] [{}] Subscribe failed -- count: {}", topic, subscriptionName, consumer.consumerName(), USAGE_COUNT_UPDATER.get(NonPersistentTopic.this));
            }
            future.completeExceptionally(new BrokerServiceException("Connection was closed while the opening the cursor "));
        } else {
            log.info("[{}][{}] Created new subscription for {}", topic, subscriptionName, consumerId);
            future.complete(consumer);
        }
    } catch (BrokerServiceException e) {
        if (e instanceof ConsumerBusyException) {
            log.warn("[{}][{}] Consumer {} {} already connected", topic, subscriptionName, consumerId, consumerName);
        } else if (e instanceof SubscriptionBusyException) {
            log.warn("[{}][{}] {}", topic, subscriptionName, e.getMessage());
        }
        USAGE_COUNT_UPDATER.decrementAndGet(NonPersistentTopic.this);
        future.completeExceptionally(e);
    }
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ConsumerBusyException(org.apache.pulsar.broker.service.BrokerServiceException.ConsumerBusyException) TopicFencedException(org.apache.pulsar.broker.service.BrokerServiceException.TopicFencedException) Consumer(org.apache.pulsar.broker.service.Consumer) NotAllowedException(org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException) BrokerServiceException(org.apache.pulsar.broker.service.BrokerServiceException) NamingException(org.apache.pulsar.broker.service.BrokerServiceException.NamingException) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) UnsupportedVersionException(org.apache.pulsar.broker.service.BrokerServiceException.UnsupportedVersionException)

Example 9 with NamingException

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

the class NonPersistentTopic method addReplicationCluster.

protected boolean addReplicationCluster(String remoteCluster, NonPersistentTopic nonPersistentTopic, String localCluster) {
    AtomicBoolean isReplicatorStarted = new AtomicBoolean(true);
    replicators.computeIfAbsent(remoteCluster, r -> {
        try {
            return new NonPersistentReplicator(NonPersistentTopic.this, localCluster, remoteCluster, brokerService);
        } catch (NamingException e) {
            isReplicatorStarted.set(false);
            log.error("[{}] Replicator startup failed due to partitioned-topic {}", topic, remoteCluster);
        }
        return null;
    });
    // clean up replicator if startup is failed
    if (!isReplicatorStarted.get()) {
        replicators.remove(remoteCluster);
    }
    return isReplicatorStarted.get();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NamingException(org.apache.pulsar.broker.service.BrokerServiceException.NamingException)

Aggregations

NamingException (org.apache.pulsar.broker.service.BrokerServiceException.NamingException)9 CompletableFuture (java.util.concurrent.CompletableFuture)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)4 NotAllowedException (org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException)4 TopicName (org.apache.pulsar.common.naming.TopicName)4 BrokerServiceException (org.apache.pulsar.broker.service.BrokerServiceException)3 ConsumerBusyException (org.apache.pulsar.broker.service.BrokerServiceException.ConsumerBusyException)3 PersistenceException (org.apache.pulsar.broker.service.BrokerServiceException.PersistenceException)3 ServerMetadataException (org.apache.pulsar.broker.service.BrokerServiceException.ServerMetadataException)3 SubscriptionBusyException (org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)3 TopicBusyException (org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException)3 TopicFencedException (org.apache.pulsar.broker.service.BrokerServiceException.TopicFencedException)3 UnsupportedVersionException (org.apache.pulsar.broker.service.BrokerServiceException.UnsupportedVersionException)3 Policies (org.apache.pulsar.common.policies.data.Policies)3 KeeperException (org.apache.zookeeper.KeeperException)3 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2 Lists (com.google.common.collect.Lists)2 Maps (com.google.common.collect.Maps)2