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