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