Search in sources :

Example 1 with AutoSubscriptionCreationOverride

use of org.apache.pulsar.common.policies.data.AutoSubscriptionCreationOverride in project pulsar by apache.

the class BrokerServiceAutoSubscriptionCreationTest method testAutoSubscriptionCreationNamespaceAllowOverridesBroker.

@Test
public void testAutoSubscriptionCreationNamespaceAllowOverridesBroker() throws Exception {
    final String topic = "persistent://prop/ns-abc/test-subtopic-" + testId.getAndIncrement();
    final String subscriptionName = "test-subtopic-sub-2";
    final TopicName topicName = TopicName.get(topic);
    admin.topics().createNonPartitionedTopic(topicName.toString());
    pulsar.getConfiguration().setAllowAutoSubscriptionCreation(false);
    AutoSubscriptionCreationOverride autoSubscriptionCreationOverride = AutoSubscriptionCreationOverride.builder().allowAutoSubscriptionCreation(true).build();
    pulsar.getAdminClient().namespaces().setAutoSubscriptionCreation(topicName.getNamespace(), autoSubscriptionCreationOverride);
    Assert.assertEquals(pulsar.getAdminClient().namespaces().getAutoSubscriptionCreation(topicName.getNamespace()), autoSubscriptionCreationOverride);
    // Subscribe operation should be successful
    pulsarClient.newConsumer().topic(topicName.toString()).subscriptionName(subscriptionName).subscribe();
    assertTrue(admin.topics().getSubscriptions(topicName.toString()).contains(subscriptionName));
}
Also used : AutoSubscriptionCreationOverride(org.apache.pulsar.common.policies.data.AutoSubscriptionCreationOverride) TopicName(org.apache.pulsar.common.naming.TopicName) Test(org.testng.annotations.Test)

Example 2 with AutoSubscriptionCreationOverride

use of org.apache.pulsar.common.policies.data.AutoSubscriptionCreationOverride in project pulsar by apache.

the class BrokerServiceAutoSubscriptionCreationTest method testAutoSubscriptionCreationNamespaceDisallowOverridesBroker.

@Test
public void testAutoSubscriptionCreationNamespaceDisallowOverridesBroker() throws Exception {
    final String topic = "persistent://prop/ns-abc/test-subtopic-" + testId.getAndIncrement();
    final String subscriptionName = "test-subtopic-sub-3";
    final TopicName topicName = TopicName.get(topic);
    admin.topics().createNonPartitionedTopic(topicName.toString());
    pulsar.getConfiguration().setAllowAutoSubscriptionCreation(true);
    AutoSubscriptionCreationOverride autoSubscriptionCreationOverride = AutoSubscriptionCreationOverride.builder().allowAutoSubscriptionCreation(false).build();
    pulsar.getAdminClient().namespaces().setAutoSubscriptionCreation(topicName.getNamespace(), autoSubscriptionCreationOverride);
    Assert.assertEquals(pulsar.getAdminClient().namespaces().getAutoSubscriptionCreation(topicName.getNamespace()), autoSubscriptionCreationOverride);
    try {
        pulsarClient.newConsumer().topic(topicName.toString()).subscriptionName(subscriptionName).subscribe();
        fail("Subscribe operation should have failed");
    } catch (Exception e) {
        assertTrue(e instanceof PulsarClientException);
    }
    assertFalse(admin.topics().getSubscriptions(topicName.toString()).contains(subscriptionName));
}
Also used : PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) AutoSubscriptionCreationOverride(org.apache.pulsar.common.policies.data.AutoSubscriptionCreationOverride) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) TopicName(org.apache.pulsar.common.naming.TopicName) Test(org.testng.annotations.Test)

Example 3 with AutoSubscriptionCreationOverride

use of org.apache.pulsar.common.policies.data.AutoSubscriptionCreationOverride in project pulsar by apache.

the class NamespacesBase method internalSetAutoSubscriptionCreation.

protected void internalSetAutoSubscriptionCreation(AsyncResponse asyncResponse, AutoSubscriptionCreationOverride autoSubscriptionCreationOverride) {
    validateNamespacePolicyOperation(namespaceName, PolicyName.AUTO_SUBSCRIPTION_CREATION, PolicyOperation.WRITE);
    validatePoliciesReadOnlyAccess();
    // Force to read the data s.t. the watch to the cache content is setup.
    namespaceResources().setPoliciesAsync(namespaceName, policies -> {
        policies.autoSubscriptionCreationOverride = autoSubscriptionCreationOverride;
        return policies;
    }).thenApply(r -> {
        if (autoSubscriptionCreationOverride != null) {
            String autoOverride = autoSubscriptionCreationOverride.isAllowAutoSubscriptionCreation() ? "enabled" : "disabled";
            log.info("[{}] Successfully {} autoSubscriptionCreation on namespace {}", clientAppId(), autoOverride != null ? autoOverride : "removed", namespaceName);
        }
        asyncResponse.resume(Response.noContent().build());
        return null;
    }).exceptionally(e -> {
        log.error("[{}] Failed to modify autoSubscriptionCreation status on namespace {}", clientAppId(), namespaceName, e.getCause());
        if (e.getCause() instanceof NotFoundException) {
            asyncResponse.resume(new RestException(Status.NOT_FOUND, "Namespace does not exist"));
            return null;
        }
        asyncResponse.resume(new RestException(e.getCause()));
        return null;
    });
}
Also used : SortedSet(java.util.SortedSet) Topic(org.apache.pulsar.broker.service.Topic) MutableObject(org.apache.commons.lang.mutable.MutableObject) URL(java.net.URL) AdminResource(org.apache.pulsar.broker.admin.AdminResource) BundleType(org.apache.pulsar.common.policies.data.Policies.BundleType) LoggerFactory(org.slf4j.LoggerFactory) TopicHashPositions(org.apache.pulsar.common.policies.data.TopicHashPositions) DelayedDeliveryPolicies(org.apache.pulsar.common.policies.data.DelayedDeliveryPolicies) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) StringUtils(org.apache.commons.lang3.StringUtils) NamespaceBundleSplitAlgorithm(org.apache.pulsar.common.naming.NamespaceBundleSplitAlgorithm) SubscribeRate(org.apache.pulsar.common.policies.data.SubscribeRate) NamespaceBundles(org.apache.pulsar.common.naming.NamespaceBundles) PoliciesUtil.defaultBundle(org.apache.pulsar.common.policies.data.PoliciesUtil.defaultBundle) PoliciesUtil.getBundles(org.apache.pulsar.common.policies.data.PoliciesUtil.getBundles) BacklogQuotaType(org.apache.pulsar.common.policies.data.BacklogQuota.BacklogQuotaType) AuthAction(org.apache.pulsar.common.policies.data.AuthAction) PolicyOperation(org.apache.pulsar.common.policies.data.PolicyOperation) Map(java.util.Map) RestException(org.apache.pulsar.broker.web.RestException) NamespaceOperation(org.apache.pulsar.common.policies.data.NamespaceOperation) NamedEntity(org.apache.pulsar.common.naming.NamedEntity) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) DispatchRateImpl(org.apache.pulsar.common.policies.data.impl.DispatchRateImpl) UriBuilder(javax.ws.rs.core.UriBuilder) BookieAffinityGroupData(org.apache.pulsar.common.policies.data.BookieAffinityGroupData) URI(java.net.URI) InactiveTopicPolicies(org.apache.pulsar.common.policies.data.InactiveTopicPolicies) MetadataStoreException(org.apache.pulsar.metadata.api.MetadataStoreException) SchemaCompatibilityStrategy(org.apache.pulsar.common.policies.data.SchemaCompatibilityStrategy) PersistencePolicies(org.apache.pulsar.common.policies.data.PersistencePolicies) AsyncResponse(javax.ws.rs.container.AsyncResponse) OffloadPoliciesImpl(org.apache.pulsar.common.policies.data.OffloadPoliciesImpl) Set(java.util.Set) LocalPolicies(org.apache.pulsar.common.policies.data.LocalPolicies) PublishRate(org.apache.pulsar.common.policies.data.PublishRate) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) NamespaceBundleFactory(org.apache.pulsar.common.naming.NamespaceBundleFactory) List(java.util.List) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) FutureUtil(org.apache.pulsar.common.util.FutureUtil) Response(javax.ws.rs.core.Response) NotFoundException(org.apache.pulsar.metadata.api.MetadataStoreException.NotFoundException) PolicyName(org.apache.pulsar.common.policies.data.PolicyName) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) TenantOperation(org.apache.pulsar.common.policies.data.TenantOperation) Subscription(org.apache.pulsar.broker.service.Subscription) TopicName(org.apache.pulsar.common.naming.TopicName) BacklogQuota(org.apache.pulsar.common.policies.data.BacklogQuota) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) AutoTopicCreationOverride(org.apache.pulsar.common.policies.data.AutoTopicCreationOverride) AutoTopicCreationOverrideImpl(org.apache.pulsar.common.policies.data.impl.AutoTopicCreationOverrideImpl) BundlesData(org.apache.pulsar.common.policies.data.BundlesData) NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) TopicDomain(org.apache.pulsar.common.naming.TopicDomain) Status(javax.ws.rs.core.Response.Status) DispatchRate(org.apache.pulsar.common.policies.data.DispatchRate) RetentionPolicies(org.apache.pulsar.common.policies.data.RetentionPolicies) PersistentReplicator(org.apache.pulsar.broker.service.persistent.PersistentReplicator) Logger(org.slf4j.Logger) AlreadyExistsException(org.apache.pulsar.metadata.api.MetadataStoreException.AlreadyExistsException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) AutoSubscriptionCreationOverride(org.apache.pulsar.common.policies.data.AutoSubscriptionCreationOverride) Field(java.lang.reflect.Field) SubscriptionType(org.apache.pulsar.client.api.SubscriptionType) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) SystemTopicClient(org.apache.pulsar.broker.systopic.SystemTopicClient) Policies(org.apache.pulsar.common.policies.data.Policies) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) SchemaAutoUpdateCompatibilityStrategy(org.apache.pulsar.common.policies.data.SchemaAutoUpdateCompatibilityStrategy) SubscriptionAuthMode(org.apache.pulsar.common.policies.data.SubscriptionAuthMode) Codec(org.apache.pulsar.common.util.Codec) BadVersionException(org.apache.pulsar.metadata.api.MetadataStoreException.BadVersionException) AuthorizationService(org.apache.pulsar.broker.authorization.AuthorizationService) Collections(java.util.Collections) RestException(org.apache.pulsar.broker.web.RestException) NotFoundException(org.apache.pulsar.metadata.api.MetadataStoreException.NotFoundException)

Example 4 with AutoSubscriptionCreationOverride

use of org.apache.pulsar.common.policies.data.AutoSubscriptionCreationOverride in project pulsar by yahoo.

the class BrokerServiceAutoSubscriptionCreationTest method testAutoSubscriptionCreationNamespaceDisallowOverridesBroker.

@Test
public void testAutoSubscriptionCreationNamespaceDisallowOverridesBroker() throws Exception {
    final String topic = "persistent://prop/ns-abc/test-subtopic-" + testId.getAndIncrement();
    final String subscriptionName = "test-subtopic-sub-3";
    final TopicName topicName = TopicName.get(topic);
    admin.topics().createNonPartitionedTopic(topicName.toString());
    pulsar.getConfiguration().setAllowAutoSubscriptionCreation(true);
    AutoSubscriptionCreationOverride autoSubscriptionCreationOverride = AutoSubscriptionCreationOverride.builder().allowAutoSubscriptionCreation(false).build();
    pulsar.getAdminClient().namespaces().setAutoSubscriptionCreation(topicName.getNamespace(), autoSubscriptionCreationOverride);
    Assert.assertEquals(pulsar.getAdminClient().namespaces().getAutoSubscriptionCreation(topicName.getNamespace()), autoSubscriptionCreationOverride);
    try {
        pulsarClient.newConsumer().topic(topicName.toString()).subscriptionName(subscriptionName).subscribe();
        fail("Subscribe operation should have failed");
    } catch (Exception e) {
        assertTrue(e instanceof PulsarClientException);
    }
    assertFalse(admin.topics().getSubscriptions(topicName.toString()).contains(subscriptionName));
}
Also used : PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) AutoSubscriptionCreationOverride(org.apache.pulsar.common.policies.data.AutoSubscriptionCreationOverride) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) TopicName(org.apache.pulsar.common.naming.TopicName) Test(org.testng.annotations.Test)

Example 5 with AutoSubscriptionCreationOverride

use of org.apache.pulsar.common.policies.data.AutoSubscriptionCreationOverride in project pulsar by yahoo.

the class NamespacesBase method internalSetAutoSubscriptionCreation.

protected void internalSetAutoSubscriptionCreation(AsyncResponse asyncResponse, AutoSubscriptionCreationOverride autoSubscriptionCreationOverride) {
    validateNamespacePolicyOperation(namespaceName, PolicyName.AUTO_SUBSCRIPTION_CREATION, PolicyOperation.WRITE);
    validatePoliciesReadOnlyAccess();
    // Force to read the data s.t. the watch to the cache content is setup.
    namespaceResources().setPoliciesAsync(namespaceName, policies -> {
        policies.autoSubscriptionCreationOverride = autoSubscriptionCreationOverride;
        return policies;
    }).thenApply(r -> {
        if (autoSubscriptionCreationOverride != null) {
            String autoOverride = autoSubscriptionCreationOverride.isAllowAutoSubscriptionCreation() ? "enabled" : "disabled";
            log.info("[{}] Successfully {} autoSubscriptionCreation on namespace {}", clientAppId(), autoOverride != null ? autoOverride : "removed", namespaceName);
        }
        asyncResponse.resume(Response.noContent().build());
        return null;
    }).exceptionally(e -> {
        log.error("[{}] Failed to modify autoSubscriptionCreation status on namespace {}", clientAppId(), namespaceName, e.getCause());
        if (e.getCause() instanceof NotFoundException) {
            asyncResponse.resume(new RestException(Status.NOT_FOUND, "Namespace does not exist"));
            return null;
        }
        asyncResponse.resume(new RestException(e.getCause()));
        return null;
    });
}
Also used : SortedSet(java.util.SortedSet) Topic(org.apache.pulsar.broker.service.Topic) MutableObject(org.apache.commons.lang.mutable.MutableObject) URL(java.net.URL) AdminResource(org.apache.pulsar.broker.admin.AdminResource) BundleType(org.apache.pulsar.common.policies.data.Policies.BundleType) LoggerFactory(org.slf4j.LoggerFactory) TopicHashPositions(org.apache.pulsar.common.policies.data.TopicHashPositions) LedgerOffloader(org.apache.bookkeeper.mledger.LedgerOffloader) DelayedDeliveryPolicies(org.apache.pulsar.common.policies.data.DelayedDeliveryPolicies) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) StringUtils(org.apache.commons.lang3.StringUtils) NamespaceBundleSplitAlgorithm(org.apache.pulsar.common.naming.NamespaceBundleSplitAlgorithm) SubscribeRate(org.apache.pulsar.common.policies.data.SubscribeRate) NamespaceBundles(org.apache.pulsar.common.naming.NamespaceBundles) PoliciesUtil.defaultBundle(org.apache.pulsar.common.policies.data.PoliciesUtil.defaultBundle) PoliciesUtil.getBundles(org.apache.pulsar.common.policies.data.PoliciesUtil.getBundles) BacklogQuotaType(org.apache.pulsar.common.policies.data.BacklogQuota.BacklogQuotaType) AuthAction(org.apache.pulsar.common.policies.data.AuthAction) PolicyOperation(org.apache.pulsar.common.policies.data.PolicyOperation) Map(java.util.Map) RestException(org.apache.pulsar.broker.web.RestException) NamespaceOperation(org.apache.pulsar.common.policies.data.NamespaceOperation) NamedEntity(org.apache.pulsar.common.naming.NamedEntity) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) DispatchRateImpl(org.apache.pulsar.common.policies.data.impl.DispatchRateImpl) UriBuilder(javax.ws.rs.core.UriBuilder) BookieAffinityGroupData(org.apache.pulsar.common.policies.data.BookieAffinityGroupData) URI(java.net.URI) InactiveTopicPolicies(org.apache.pulsar.common.policies.data.InactiveTopicPolicies) MetadataStoreException(org.apache.pulsar.metadata.api.MetadataStoreException) SchemaCompatibilityStrategy(org.apache.pulsar.common.policies.data.SchemaCompatibilityStrategy) PersistencePolicies(org.apache.pulsar.common.policies.data.PersistencePolicies) AsyncResponse(javax.ws.rs.container.AsyncResponse) OffloadPoliciesImpl(org.apache.pulsar.common.policies.data.OffloadPoliciesImpl) Set(java.util.Set) LocalPolicies(org.apache.pulsar.common.policies.data.LocalPolicies) PublishRate(org.apache.pulsar.common.policies.data.PublishRate) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) NamespaceBundleFactory(org.apache.pulsar.common.naming.NamespaceBundleFactory) List(java.util.List) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) FutureUtil(org.apache.pulsar.common.util.FutureUtil) Response(javax.ws.rs.core.Response) NotFoundException(org.apache.pulsar.metadata.api.MetadataStoreException.NotFoundException) PolicyName(org.apache.pulsar.common.policies.data.PolicyName) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) TenantOperation(org.apache.pulsar.common.policies.data.TenantOperation) Subscription(org.apache.pulsar.broker.service.Subscription) TopicName(org.apache.pulsar.common.naming.TopicName) BacklogQuota(org.apache.pulsar.common.policies.data.BacklogQuota) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) TopicType(org.apache.pulsar.common.policies.data.TopicType) Lists(com.google.common.collect.Lists) AutoTopicCreationOverride(org.apache.pulsar.common.policies.data.AutoTopicCreationOverride) AutoTopicCreationOverrideImpl(org.apache.pulsar.common.policies.data.impl.AutoTopicCreationOverrideImpl) BundlesData(org.apache.pulsar.common.policies.data.BundlesData) NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) TopicDomain(org.apache.pulsar.common.naming.TopicDomain) Status(javax.ws.rs.core.Response.Status) DispatchRate(org.apache.pulsar.common.policies.data.DispatchRate) RetentionPolicies(org.apache.pulsar.common.policies.data.RetentionPolicies) PersistentReplicator(org.apache.pulsar.broker.service.persistent.PersistentReplicator) Logger(org.slf4j.Logger) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) AutoSubscriptionCreationOverride(org.apache.pulsar.common.policies.data.AutoSubscriptionCreationOverride) Field(java.lang.reflect.Field) SubscriptionType(org.apache.pulsar.client.api.SubscriptionType) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Policies(org.apache.pulsar.common.policies.data.Policies) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) SchemaAutoUpdateCompatibilityStrategy(org.apache.pulsar.common.policies.data.SchemaAutoUpdateCompatibilityStrategy) SubscriptionAuthMode(org.apache.pulsar.common.policies.data.SubscriptionAuthMode) ValidateResult(org.apache.pulsar.common.policies.data.ValidateResult) Codec(org.apache.pulsar.common.util.Codec) BadVersionException(org.apache.pulsar.metadata.api.MetadataStoreException.BadVersionException) AuthorizationService(org.apache.pulsar.broker.authorization.AuthorizationService) Collections(java.util.Collections) RestException(org.apache.pulsar.broker.web.RestException) NotFoundException(org.apache.pulsar.metadata.api.MetadataStoreException.NotFoundException)

Aggregations

AutoSubscriptionCreationOverride (org.apache.pulsar.common.policies.data.AutoSubscriptionCreationOverride)15 NamespaceName (org.apache.pulsar.common.naming.NamespaceName)9 TopicName (org.apache.pulsar.common.naming.TopicName)9 AutoTopicCreationOverride (org.apache.pulsar.common.policies.data.AutoTopicCreationOverride)9 CompletableFuture (java.util.concurrent.CompletableFuture)6 Test (org.testng.annotations.Test)6 WebTarget (javax.ws.rs.client.WebTarget)5 Lists (com.google.common.collect.Lists)3 Sets (com.google.common.collect.Sets)3 Field (java.lang.reflect.Field)3 URI (java.net.URI)3 URL (java.net.URL)3 Collections (java.util.Collections)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Map (java.util.Map)3 Objects (java.util.Objects)3 Optional (java.util.Optional)3 Set (java.util.Set)3