use of org.apache.pulsar.common.policies.data.TopicPolicies in project pulsar by apache.
the class NamespaceEventsSystemTopicServiceTest method testSendAndReceiveNamespaceEvents.
@Test
public void testSendAndReceiveNamespaceEvents() throws Exception {
TopicPoliciesSystemTopicClient systemTopicClientForNamespace1 = systemTopicFactory.createTopicPoliciesSystemTopicClient(NamespaceName.get(NAMESPACE1));
TopicPolicies policies = TopicPolicies.builder().maxProducerPerTopic(10).build();
PulsarEvent event = PulsarEvent.builder().eventType(EventType.TOPIC_POLICY).actionType(ActionType.INSERT).topicPoliciesEvent(TopicPoliciesEvent.builder().domain("persistent").tenant("system-topic").namespace(NamespaceName.get(NAMESPACE1).getLocalName()).topic("my-topic").policies(policies).build()).build();
systemTopicClientForNamespace1.newWriter().write(event);
SystemTopicClient.Reader reader = systemTopicClientForNamespace1.newReader();
Message<PulsarEvent> received = reader.readNext();
log.info("Receive pulsar event from system topic : {}", received.getValue());
// test event send and receive
Assert.assertEquals(received.getValue(), event);
Assert.assertEquals(systemTopicClientForNamespace1.getWriters().size(), 1);
Assert.assertEquals(systemTopicClientForNamespace1.getReaders().size(), 1);
// test new reader read
SystemTopicClient.Reader reader1 = systemTopicClientForNamespace1.newReader();
Message<PulsarEvent> received1 = reader1.readNext();
log.info("Receive pulsar event from system topic : {}", received1.getValue());
Assert.assertEquals(received1.getValue(), event);
// test writers and readers
Assert.assertEquals(systemTopicClientForNamespace1.getReaders().size(), 2);
SystemTopicClient.Writer writer = systemTopicClientForNamespace1.newWriter();
Assert.assertEquals(systemTopicClientForNamespace1.getWriters().size(), 2);
writer.close();
reader.close();
Assert.assertEquals(systemTopicClientForNamespace1.getWriters().size(), 1);
Assert.assertEquals(systemTopicClientForNamespace1.getReaders().size(), 1);
systemTopicClientForNamespace1.close();
Assert.assertEquals(systemTopicClientForNamespace1.getWriters().size(), 0);
Assert.assertEquals(systemTopicClientForNamespace1.getReaders().size(), 0);
}
use of org.apache.pulsar.common.policies.data.TopicPolicies in project pulsar by apache.
the class TransactionTest method testSubscriptionRecreateTopic.
@Test
public void testSubscriptionRecreateTopic() throws PulsarAdminException, NoSuchFieldException, IllegalAccessException, PulsarClientException {
String topic = "persistent://pulsar/system/testReCreateTopic";
String subName = "sub_testReCreateTopic";
int retentionSizeInMbSetTo = 5;
int retentionSizeInMbSetTopic = 6;
int retentionSizeInMinutesSetTo = 5;
int retentionSizeInMinutesSetTopic = 6;
admin.topics().createNonPartitionedTopic(topic);
PulsarService pulsarService = super.getPulsarServiceList().get(0);
pulsarService.getBrokerService().getTopics().clear();
ManagedLedgerFactory managedLedgerFactory = pulsarService.getBrokerService().getManagedLedgerFactory();
Field field = ManagedLedgerFactoryImpl.class.getDeclaredField("ledgers");
field.setAccessible(true);
ConcurrentHashMap<String, CompletableFuture<ManagedLedgerImpl>> ledgers = (ConcurrentHashMap<String, CompletableFuture<ManagedLedgerImpl>>) field.get(managedLedgerFactory);
ledgers.remove(TopicName.get(topic).getPersistenceNamingEncoding());
try {
admin.topics().createNonPartitionedTopic(topic);
Assert.fail();
} catch (PulsarAdminException.ConflictException e) {
log.info("Cann`t create topic again");
}
admin.topics().setRetention(topic, new RetentionPolicies(retentionSizeInMinutesSetTopic, retentionSizeInMbSetTopic));
pulsarClient.newConsumer().topic(topic).subscriptionName(subName).subscribe();
pulsarService.getBrokerService().getTopicIfExists(topic).thenAccept(option -> {
if (!option.isPresent()) {
log.error("Failed o get Topic named: {}", topic);
Assert.fail();
}
PersistentTopic originPersistentTopic = (PersistentTopic) option.get();
String pendingAckTopicName = MLPendingAckStore.getTransactionPendingAckStoreSuffix(originPersistentTopic.getName(), subName);
try {
admin.topics().setRetention(pendingAckTopicName, new RetentionPolicies(retentionSizeInMinutesSetTo, retentionSizeInMbSetTo));
} catch (PulsarAdminException e) {
log.error("Failed to get./setRetention of topic with Exception:" + e);
Assert.fail();
}
PersistentSubscription subscription = originPersistentTopic.getSubscription(subName);
subscription.getPendingAckManageLedger().thenAccept(managedLedger -> {
long retentionSize = managedLedger.getConfig().getRetentionSizeInMB();
if (!originPersistentTopic.getTopicPolicies().isPresent()) {
log.error("Failed to getTopicPolicies of :" + originPersistentTopic);
Assert.fail();
}
TopicPolicies topicPolicies = originPersistentTopic.getTopicPolicies().get();
Assert.assertEquals(retentionSizeInMbSetTopic, retentionSize);
MLPendingAckStoreProvider mlPendingAckStoreProvider = new MLPendingAckStoreProvider();
CompletableFuture<PendingAckStore> future = mlPendingAckStoreProvider.newPendingAckStore(subscription);
future.thenAccept(pendingAckStore -> {
((MLPendingAckStore) pendingAckStore).getManagedLedger().thenAccept(managedLedger1 -> {
Assert.assertEquals(managedLedger1.getConfig().getRetentionSizeInMB(), retentionSizeInMbSetTo);
});
});
});
});
}
use of org.apache.pulsar.common.policies.data.TopicPolicies in project pulsar by apache.
the class PersistentTopicsBase method internalSetDeduplicationSnapshotInterval.
protected CompletableFuture<Void> internalSetDeduplicationSnapshotInterval(Integer interval, boolean isGlobal) {
if (interval != null && interval < 0) {
throw new RestException(Status.PRECONDITION_FAILED, "interval must be 0 or more");
}
return getTopicPoliciesAsyncWithRetry(topicName, isGlobal).thenCompose(op -> {
TopicPolicies policies = op.orElseGet(TopicPolicies::new);
policies.setDeduplicationSnapshotIntervalSeconds(interval);
policies.setIsGlobal(isGlobal);
return pulsar().getTopicPoliciesService().updateTopicPoliciesAsync(topicName, policies);
});
}
use of org.apache.pulsar.common.policies.data.TopicPolicies in project pulsar by apache.
the class PersistentTopicsBase method internalRemoveSubscriptionDispatchRate.
protected CompletableFuture<Void> internalRemoveSubscriptionDispatchRate(boolean isGlobal) {
return getTopicPoliciesAsyncWithRetry(topicName, isGlobal).thenCompose(op -> {
if (!op.isPresent()) {
return CompletableFuture.completedFuture(null);
}
TopicPolicies topicPolicies = op.get();
topicPolicies.setSubscriptionDispatchRate(null);
topicPolicies.setIsGlobal(isGlobal);
return pulsar().getTopicPoliciesService().updateTopicPoliciesAsync(topicName, op.get());
});
}
use of org.apache.pulsar.common.policies.data.TopicPolicies in project pulsar by apache.
the class PersistentTopicsBase method internalSetBacklogQuota.
protected CompletableFuture<Void> internalSetBacklogQuota(BacklogQuota.BacklogQuotaType backlogQuotaType, BacklogQuotaImpl backlogQuota, boolean isGlobal) {
BacklogQuota.BacklogQuotaType finalBacklogQuotaType = backlogQuotaType == null ? BacklogQuota.BacklogQuotaType.destination_storage : backlogQuotaType;
return validateTopicPolicyOperationAsync(topicName, PolicyName.BACKLOG, PolicyOperation.WRITE).thenAccept(__ -> validatePoliciesReadOnlyAccess()).thenCompose(__ -> getTopicPoliciesAsyncWithRetry(topicName, isGlobal)).thenCompose(op -> {
TopicPolicies topicPolicies = op.orElseGet(TopicPolicies::new);
return getRetentionPoliciesAsync(topicName, topicPolicies).thenCompose(retentionPolicies -> {
if (!checkBacklogQuota(backlogQuota, retentionPolicies)) {
log.warn("[{}] Failed to update backlog configuration for topic {}: conflicts with" + " retention quota", clientAppId(), topicName);
return FutureUtil.failedFuture(new RestException(Status.PRECONDITION_FAILED, "Backlog Quota exceeds configured retention quota for topic. " + "Please increase retention quota and retry"));
}
if (backlogQuota != null) {
topicPolicies.getBackLogQuotaMap().put(finalBacklogQuotaType.name(), backlogQuota);
} else {
topicPolicies.getBackLogQuotaMap().remove(finalBacklogQuotaType.name());
}
Map<String, BacklogQuotaImpl> backLogQuotaMap = topicPolicies.getBackLogQuotaMap();
topicPolicies.setIsGlobal(isGlobal);
return pulsar().getTopicPoliciesService().updateTopicPoliciesAsync(topicName, topicPolicies).thenRun(() -> {
try {
log.info("[{}] Successfully updated backlog quota map: namespace={}, " + "topic={}, map={}", clientAppId(), namespaceName, topicName.getLocalName(), jsonMapper().writeValueAsString(backLogQuotaMap));
} catch (JsonProcessingException ignore) {
}
});
});
});
}
Aggregations