Search in sources :

Example 1 with AutoTopicCreationOverride

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

the class NamespacesBase method internalSetAutoTopicCreation.

protected void internalSetAutoTopicCreation(AsyncResponse asyncResponse, AutoTopicCreationOverride autoTopicCreationOverride) {
    final int maxPartitions = pulsar().getConfig().getMaxNumPartitionsPerPartitionedTopic();
    validateNamespacePolicyOperation(namespaceName, PolicyName.AUTO_TOPIC_CREATION, PolicyOperation.WRITE);
    validatePoliciesReadOnlyAccess();
    if (autoTopicCreationOverride != null) {
        if (!AutoTopicCreationOverrideImpl.isValidOverride(autoTopicCreationOverride)) {
            throw new RestException(Status.PRECONDITION_FAILED, "Invalid configuration for autoTopicCreationOverride");
        }
        if (maxPartitions > 0 && autoTopicCreationOverride.getDefaultNumPartitions() > maxPartitions) {
            throw new RestException(Status.NOT_ACCEPTABLE, "Number of partitions should be less than or equal to " + maxPartitions);
        }
    }
    // Force to read the data s.t. the watch to the cache content is setup.
    namespaceResources().setPoliciesAsync(namespaceName, policies -> {
        policies.autoTopicCreationOverride = autoTopicCreationOverride;
        return policies;
    }).thenApply(r -> {
        String autoOverride = (autoTopicCreationOverride != null && autoTopicCreationOverride.isAllowAutoTopicCreation()) ? "enabled" : "disabled";
        log.info("[{}] Successfully {} autoTopicCreation on namespace {}", clientAppId(), autoOverride != null ? autoOverride : "removed", namespaceName);
        asyncResponse.resume(Response.noContent().build());
        return null;
    }).exceptionally(e -> {
        log.error("[{}] Failed to modify autoTopicCreation 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 2 with AutoTopicCreationOverride

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

the class NamespacesImpl method getAutoTopicCreationAsync.

@Override
public CompletableFuture<AutoTopicCreationOverride> getAutoTopicCreationAsync(String namespace) {
    NamespaceName ns = NamespaceName.get(namespace);
    WebTarget path = namespacePath(ns, "autoTopicCreation");
    final CompletableFuture<AutoTopicCreationOverride> future = new CompletableFuture<>();
    asyncGetRequest(path, new InvocationCallback<AutoTopicCreationOverride>() {

        @Override
        public void completed(AutoTopicCreationOverride autoTopicCreationOverride) {
            future.complete(autoTopicCreationOverride);
        }

        @Override
        public void failed(Throwable throwable) {
            future.completeExceptionally(getApiException(throwable.getCause()));
        }
    });
    return future;
}
Also used : NamespaceName(org.apache.pulsar.common.naming.NamespaceName) CompletableFuture(java.util.concurrent.CompletableFuture) AutoTopicCreationOverride(org.apache.pulsar.common.policies.data.AutoTopicCreationOverride) WebTarget(javax.ws.rs.client.WebTarget) AutoTopicCreationOverride(org.apache.pulsar.common.policies.data.AutoTopicCreationOverride) AutoSubscriptionCreationOverride(org.apache.pulsar.common.policies.data.AutoSubscriptionCreationOverride) AutoTopicCreationOverride(org.apache.pulsar.common.policies.data.AutoTopicCreationOverride) AutoSubscriptionCreationOverride(org.apache.pulsar.common.policies.data.AutoSubscriptionCreationOverride)

Example 3 with AutoTopicCreationOverride

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

the class NamespacesImpl method setAutoTopicCreationAsync.

@Override
public CompletableFuture<Void> setAutoTopicCreationAsync(String namespace, AutoTopicCreationOverride autoTopicCreationOverride) {
    NamespaceName ns = NamespaceName.get(namespace);
    WebTarget path = namespacePath(ns, "autoTopicCreation");
    return asyncPostRequest(path, Entity.entity(autoTopicCreationOverride, MediaType.APPLICATION_JSON));
}
Also used : NamespaceName(org.apache.pulsar.common.naming.NamespaceName) WebTarget(javax.ws.rs.client.WebTarget) AutoTopicCreationOverride(org.apache.pulsar.common.policies.data.AutoTopicCreationOverride) AutoSubscriptionCreationOverride(org.apache.pulsar.common.policies.data.AutoSubscriptionCreationOverride)

Example 4 with AutoTopicCreationOverride

use of org.apache.pulsar.common.policies.data.AutoTopicCreationOverride in project mop by streamnative.

the class MQTT5TestTopicAutoCreation method testSubscribeAllowTopicCreationByNamespacePolicy.

@Test(timeOut = TIMEOUT)
public void testSubscribeAllowTopicCreationByNamespacePolicy() throws PulsarAdminException {
    AutoTopicCreationOverride param = AutoTopicCreationOverride.builder().topicType(TopicType.NON_PARTITIONED.toString()).allowAutoTopicCreation(true).build();
    admin.namespaces().setAutoTopicCreation("public/default", param);
    Mqtt5BlockingClient client = MQTT5ClientUtils.createMqtt5Client(getMqttBrokerPortList().get(0));
    client.connect();
    try {
        client.subscribeWith().topicFilter("topic-not-allow-auto-creation-subscribe").qos(MqttQos.AT_LEAST_ONCE).send();
    } catch (Mqtt5SubAckException ex) {
        Assert.fail("Unexpected result");
    }
    client.disconnect();
}
Also used : Mqtt5SubAckException(com.hivemq.client.mqtt.mqtt5.exceptions.Mqtt5SubAckException) AutoTopicCreationOverride(org.apache.pulsar.common.policies.data.AutoTopicCreationOverride) Mqtt5BlockingClient(com.hivemq.client.mqtt.mqtt5.Mqtt5BlockingClient) Test(org.testng.annotations.Test)

Example 5 with AutoTopicCreationOverride

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

the class Namespaces method setAutoTopicCreation.

@POST
@Path("/{tenant}/{namespace}/autoTopicCreation")
@ApiOperation(value = "Override broker's allowAutoTopicCreation setting for a namespace")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 404, message = "Tenant or cluster or namespace doesn't exist"), @ApiResponse(code = 406, message = "The number of partitions should be less than or" + " equal to maxNumPartitionsPerPartitionedTopic"), @ApiResponse(code = 400, message = "Invalid autoTopicCreation override") })
public void setAutoTopicCreation(@Suspended final AsyncResponse asyncResponse, @PathParam("tenant") String tenant, @PathParam("namespace") String namespace, @ApiParam(value = "Settings for automatic topic creation", required = true) AutoTopicCreationOverride autoTopicCreationOverride) {
    validateNamespaceName(tenant, namespace);
    internalSetAutoTopicCreationAsync(autoTopicCreationOverride).thenAccept(__ -> {
        String autoOverride = (autoTopicCreationOverride != null && autoTopicCreationOverride.isAllowAutoTopicCreation()) ? "enabled" : "disabled";
        log.info("[{}] Successfully {} autoTopicCreation on namespace {}", clientAppId(), autoOverride, namespaceName);
        asyncResponse.resume(Response.noContent().build());
    }).exceptionally(e -> {
        Throwable ex = FutureUtil.unwrapCompletionException(e);
        log.error("[{}] Failed to set autoTopicCreation status on namespace {}", clientAppId(), namespaceName, ex);
        if (ex instanceof MetadataStoreException.NotFoundException) {
            asyncResponse.resume(new RestException(Response.Status.NOT_FOUND, "Namespace does not exist"));
        } else {
            resumeAsyncResponseExceptionally(asyncResponse, ex);
        }
        return null;
    });
}
Also used : Produces(javax.ws.rs.Produces) ObjectMapperFactory(org.apache.pulsar.common.util.ObjectMapperFactory) Path(javax.ws.rs.Path) LoggerFactory(org.slf4j.LoggerFactory) ApiParam(io.swagger.annotations.ApiParam) NamespacesBase(org.apache.pulsar.broker.admin.impl.NamespacesBase) DelayedDeliveryPolicies(org.apache.pulsar.common.policies.data.DelayedDeliveryPolicies) SubscribeRate(org.apache.pulsar.common.policies.data.SubscribeRate) ApiOperation(io.swagger.annotations.ApiOperation) MediaType(javax.ws.rs.core.MediaType) PoliciesUtil.getBundles(org.apache.pulsar.common.policies.data.PoliciesUtil.getBundles) QueryParam(javax.ws.rs.QueryParam) Consumes(javax.ws.rs.Consumes) 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) DefaultValue(javax.ws.rs.DefaultValue) RestException(org.apache.pulsar.broker.web.RestException) NamespaceOperation(org.apache.pulsar.common.policies.data.NamespaceOperation) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) DispatchRateImpl(org.apache.pulsar.common.policies.data.impl.DispatchRateImpl) BookieAffinityGroupData(org.apache.pulsar.common.policies.data.BookieAffinityGroupData) DELETE(javax.ws.rs.DELETE) 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) StreamingOutput(javax.ws.rs.core.StreamingOutput) PublishRate(org.apache.pulsar.common.policies.data.PublishRate) OffloaderObjectsScannerUtils(org.apache.pulsar.broker.admin.impl.OffloaderObjectsScannerUtils) Suspended(javax.ws.rs.container.Suspended) Sets(com.google.common.collect.Sets) StandardCharsets(java.nio.charset.StandardCharsets) Mode(org.apache.pulsar.common.api.proto.CommandGetTopicsOfNamespace.Mode) List(java.util.List) FutureUtil(org.apache.pulsar.common.util.FutureUtil) Response(javax.ws.rs.core.Response) PolicyName(org.apache.pulsar.common.policies.data.PolicyName) WebApplicationException(javax.ws.rs.WebApplicationException) PathParam(javax.ws.rs.PathParam) GET(javax.ws.rs.GET) BacklogQuota(org.apache.pulsar.common.policies.data.BacklogQuota) ApiResponses(io.swagger.annotations.ApiResponses) AutoTopicCreationOverride(org.apache.pulsar.common.policies.data.AutoTopicCreationOverride) BundlesData(org.apache.pulsar.common.policies.data.BundlesData) OutputStreamWriter(java.io.OutputStreamWriter) Api(io.swagger.annotations.Api) DispatchRate(org.apache.pulsar.common.policies.data.DispatchRate) RetentionPolicies(org.apache.pulsar.common.policies.data.RetentionPolicies) POST(javax.ws.rs.POST) Logger(org.slf4j.Logger) AutoSubscriptionCreationOverride(org.apache.pulsar.common.policies.data.AutoSubscriptionCreationOverride) SubscriptionType(org.apache.pulsar.client.api.SubscriptionType) Policies(org.apache.pulsar.common.policies.data.Policies) ApiResponse(io.swagger.annotations.ApiResponse) SchemaAutoUpdateCompatibilityStrategy(org.apache.pulsar.common.policies.data.SchemaAutoUpdateCompatibilityStrategy) SubscriptionAuthMode(org.apache.pulsar.common.policies.data.SubscriptionAuthMode) PUT(javax.ws.rs.PUT) RestException(org.apache.pulsar.broker.web.RestException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

AutoTopicCreationOverride (org.apache.pulsar.common.policies.data.AutoTopicCreationOverride)26 Test (org.testng.annotations.Test)15 NamespaceName (org.apache.pulsar.common.naming.NamespaceName)13 TopicName (org.apache.pulsar.common.naming.TopicName)12 AutoSubscriptionCreationOverride (org.apache.pulsar.common.policies.data.AutoSubscriptionCreationOverride)11 CompletableFuture (java.util.concurrent.CompletableFuture)8 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)8 List (java.util.List)7 Map (java.util.Map)7 Set (java.util.Set)7 WebApplicationException (javax.ws.rs.WebApplicationException)7 AsyncResponse (javax.ws.rs.container.AsyncResponse)7 Response (javax.ws.rs.core.Response)7 RestException (org.apache.pulsar.broker.web.RestException)7 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)7 AuthAction (org.apache.pulsar.common.policies.data.AuthAction)7 BundlesData (org.apache.pulsar.common.policies.data.BundlesData)7 PersistencePolicies (org.apache.pulsar.common.policies.data.PersistencePolicies)7 Policies (org.apache.pulsar.common.policies.data.Policies)7 PolicyName (org.apache.pulsar.common.policies.data.PolicyName)7