Search in sources :

Example 36 with Policies

use of org.apache.pulsar.common.policies.data.Policies in project incubator-pulsar by apache.

the class DispatchRateLimiter method getPoliciesDispatchRate.

/**
 * Gets configured dispatch-rate from namespace policies. Returns null if dispatch-rate is not configured
 *
 * @return
 */
public DispatchRate getPoliciesDispatchRate() {
    final NamespaceName namespace = TopicName.get(this.topicName).getNamespaceObject();
    final String cluster = brokerService.pulsar().getConfiguration().getClusterName();
    final String path = path(POLICIES, namespace.toString());
    Optional<Policies> policies = Optional.empty();
    try {
        policies = brokerService.pulsar().getConfigurationCache().policiesCache().getAsync(path).get(cacheTimeOutInSec, SECONDS);
    } catch (Exception e) {
        log.warn("Failed to get message-rate for {}", this.topicName, e);
    }
    // return policy-dispatch rate only if it's enabled in policies
    return policies.map(p -> {
        DispatchRate dispatchRate = p.clusterDispatchRate.get(cluster);
        return isDispatchRateEnabled(dispatchRate) ? dispatchRate : null;
    }).orElse(null);
}
Also used : DispatchRate(org.apache.pulsar.common.policies.data.DispatchRate) TopicName(org.apache.pulsar.common.naming.TopicName) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) PulsarWebResource.path(org.apache.pulsar.broker.web.PulsarWebResource.path) BrokerService(org.apache.pulsar.broker.service.BrokerService) RateLimiter(org.apache.pulsar.common.util.RateLimiter) TimeUnit(java.util.concurrent.TimeUnit) Policies(org.apache.pulsar.common.policies.data.Policies) POLICIES(org.apache.pulsar.broker.cache.ConfigurationCacheService.POLICIES) Optional(java.util.Optional) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) ZooKeeperCache.cacheTimeOutInSec(org.apache.pulsar.zookeeper.ZooKeeperCache.cacheTimeOutInSec) SECONDS(java.util.concurrent.TimeUnit.SECONDS) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) Policies(org.apache.pulsar.common.policies.data.Policies) DispatchRate(org.apache.pulsar.common.policies.data.DispatchRate)

Example 37 with Policies

use of org.apache.pulsar.common.policies.data.Policies in project incubator-pulsar by apache.

the class PersistentDispatcherMultipleConsumers method isConsumersExceededOnSubscription.

private boolean isConsumersExceededOnSubscription() {
    Policies policies;
    try {
        policies = topic.getBrokerService().pulsar().getConfigurationCache().policiesCache().get(AdminResource.path(POLICIES, TopicName.get(topic.getName()).getNamespace())).orElseGet(() -> new Policies());
    } catch (Exception e) {
        policies = new Policies();
    }
    final int maxConsumersPerSubscription = policies.max_consumers_per_subscription > 0 ? policies.max_consumers_per_subscription : serviceConfig.getMaxConsumersPerSubscription();
    if (maxConsumersPerSubscription > 0 && maxConsumersPerSubscription <= consumerList.size()) {
        return true;
    }
    return false;
}
Also used : Policies(org.apache.pulsar.common.policies.data.Policies) BrokerServiceException(org.apache.pulsar.broker.service.BrokerServiceException) TooManyRequestsException(org.apache.bookkeeper.mledger.ManagedLedgerException.TooManyRequestsException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ConsumerBusyException(org.apache.pulsar.broker.service.BrokerServiceException.ConsumerBusyException) NoMoreEntriesToReadException(org.apache.bookkeeper.mledger.ManagedLedgerException.NoMoreEntriesToReadException)

Example 38 with Policies

use of org.apache.pulsar.common.policies.data.Policies in project incubator-pulsar by apache.

the class PersistentDispatcherMultipleConsumers method isConsumersExceededOnTopic.

private boolean isConsumersExceededOnTopic() {
    Policies policies;
    try {
        policies = topic.getBrokerService().pulsar().getConfigurationCache().policiesCache().get(AdminResource.path(POLICIES, TopicName.get(topic.getName()).getNamespace())).orElseGet(() -> new Policies());
    } catch (Exception e) {
        policies = new Policies();
    }
    final int maxConsumersPerTopic = policies.max_consumers_per_topic > 0 ? policies.max_consumers_per_topic : serviceConfig.getMaxConsumersPerTopic();
    if (maxConsumersPerTopic > 0 && maxConsumersPerTopic <= topic.getNumberOfConsumers()) {
        return true;
    }
    return false;
}
Also used : Policies(org.apache.pulsar.common.policies.data.Policies) BrokerServiceException(org.apache.pulsar.broker.service.BrokerServiceException) TooManyRequestsException(org.apache.bookkeeper.mledger.ManagedLedgerException.TooManyRequestsException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ConsumerBusyException(org.apache.pulsar.broker.service.BrokerServiceException.ConsumerBusyException) NoMoreEntriesToReadException(org.apache.bookkeeper.mledger.ManagedLedgerException.NoMoreEntriesToReadException)

Example 39 with Policies

use of org.apache.pulsar.common.policies.data.Policies in project incubator-pulsar by apache.

the class PulsarWebResource method checkLocalOrGetPeerReplicationCluster.

protected static CompletableFuture<ClusterData> checkLocalOrGetPeerReplicationCluster(PulsarService pulsarService, NamespaceName namespace) {
    if (!namespace.isGlobal()) {
        return CompletableFuture.completedFuture(null);
    }
    final CompletableFuture<ClusterData> validationFuture = new CompletableFuture<>();
    final String localCluster = pulsarService.getConfiguration().getClusterName();
    final String path = AdminResource.path(POLICIES, namespace.toString());
    pulsarService.getConfigurationCache().policiesCache().getAsync(path).thenAccept(policiesResult -> {
        if (policiesResult.isPresent()) {
            Policies policies = policiesResult.get();
            if (policies.replication_clusters.isEmpty()) {
                String msg = String.format("Global namespace does not have any clusters configured : local_cluster=%s ns=%s", localCluster, namespace.toString());
                log.warn(msg);
                validationFuture.completeExceptionally(new RestException(Status.PRECONDITION_FAILED, msg));
            } else if (!policies.replication_clusters.contains(localCluster)) {
                ClusterData ownerPeerCluster = getOwnerFromPeerClusterList(pulsarService, policies.replication_clusters);
                if (ownerPeerCluster != null) {
                    // found a peer that own this namespace
                    validationFuture.complete(ownerPeerCluster);
                    return;
                }
                String msg = String.format("Global namespace missing local cluster name in replication list : local_cluster=%s ns=%s repl_clusters=%s", localCluster, namespace.toString(), policies.replication_clusters);
                log.warn(msg);
                validationFuture.completeExceptionally(new RestException(Status.PRECONDITION_FAILED, msg));
            } else {
                validationFuture.complete(null);
            }
        } else {
            String msg = String.format("Policies not found for %s namespace", namespace.toString());
            log.error(msg);
            validationFuture.completeExceptionally(new RestException(Status.NOT_FOUND, msg));
        }
    }).exceptionally(ex -> {
        String msg = String.format("Failed to validate global cluster configuration : cluster=%s ns=%s  emsg=%s", localCluster, namespace, ex.getMessage());
        log.error(msg);
        validationFuture.completeExceptionally(new RestException(ex));
        return null;
    });
    return validationFuture;
}
Also used : URL(java.net.URL) AdminResource(org.apache.pulsar.broker.admin.AdminResource) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) NamespaceService(org.apache.pulsar.broker.namespace.NamespaceService) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) StringUtils(org.apache.commons.lang3.StringUtils) org.apache.pulsar.common.naming(org.apache.pulsar.common.naming) AuthenticationDataHttps(org.apache.pulsar.broker.authentication.AuthenticationDataHttps) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) HttpServletRequest(javax.servlet.http.HttpServletRequest) BundlesData(org.apache.pulsar.common.policies.data.BundlesData) ZooKeeperCache.cacheTimeOutInSec(org.apache.pulsar.zookeeper.ZooKeeperCache.cacheTimeOutInSec) UriBuilder(javax.ws.rs.core.UriBuilder) URI(java.net.URI) Status(javax.ws.rs.core.Response.Status) Splitter(com.google.common.base.Splitter) Context(javax.ws.rs.core.Context) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) KeeperException(org.apache.zookeeper.KeeperException) MalformedURLException(java.net.MalformedURLException) ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) Range(com.google.common.collect.Range) Set(java.util.Set) PulsarService(org.apache.pulsar.broker.PulsarService) AuthenticationDataSource(org.apache.pulsar.broker.authentication.AuthenticationDataSource) Sets(com.google.common.collect.Sets) Policies(org.apache.pulsar.common.policies.data.Policies) List(java.util.List) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) POLICIES(org.apache.pulsar.broker.cache.ConfigurationCacheService.POLICIES) Response(javax.ws.rs.core.Response) BoundType(com.google.common.collect.BoundType) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) ServletContext(javax.servlet.ServletContext) UriInfo(javax.ws.rs.core.UriInfo) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Joiner(com.google.common.base.Joiner) CompletableFuture(java.util.concurrent.CompletableFuture) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) Policies(org.apache.pulsar.common.policies.data.Policies)

Example 40 with Policies

use of org.apache.pulsar.common.policies.data.Policies in project incubator-pulsar by apache.

the class PoliciesDataTest method policies.

@Test
void policies() {
    Policies policies = new Policies();
    assertEquals(policies, new Policies());
    policies.auth_policies.namespace_auth.put("my-role", EnumSet.of(AuthAction.consume));
    assertTrue(!policies.equals(new Policies()));
    assertTrue(!policies.equals(new Object()));
    policies.auth_policies.namespace_auth.clear();
    Map<String, Set<AuthAction>> permissions = Maps.newTreeMap();
    permissions.put("my-role", EnumSet.of(AuthAction.consume));
    policies.auth_policies.destination_auth.put("persistent://my-dest", permissions);
    assertTrue(!policies.equals(new Policies()));
}
Also used : Policies(org.apache.pulsar.common.policies.data.Policies) Set(java.util.Set) EnumSet(java.util.EnumSet) Test(org.testng.annotations.Test)

Aggregations

Policies (org.apache.pulsar.common.policies.data.Policies)93 KeeperException (org.apache.zookeeper.KeeperException)43 RetentionPolicies (org.apache.pulsar.common.policies.data.RetentionPolicies)40 PersistencePolicies (org.apache.pulsar.common.policies.data.PersistencePolicies)39 RestException (org.apache.pulsar.broker.web.RestException)34 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)30 SubscriptionBusyException (org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)28 Stat (org.apache.zookeeper.data.Stat)26 WebApplicationException (javax.ws.rs.WebApplicationException)24 ExecutionException (java.util.concurrent.ExecutionException)23 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)23 Test (org.testng.annotations.Test)21 NamespaceBundle (org.apache.pulsar.common.naming.NamespaceBundle)15 TopicName (org.apache.pulsar.common.naming.TopicName)14 ApiOperation (io.swagger.annotations.ApiOperation)13 ApiResponses (io.swagger.annotations.ApiResponses)13 Path (javax.ws.rs.Path)13 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)13 NotAllowedException (org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException)11 NamespaceName (org.apache.pulsar.common.naming.NamespaceName)11