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);
}
}
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 ");
}
}
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;
}
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();
}
Aggregations