use of org.apache.pulsar.common.policies.data.Policies in project incubator-pulsar by apache.
the class ResourceQuotasBase method internalSetNamespaceBundleResourceQuota.
@SuppressWarnings("deprecation")
protected void internalSetNamespaceBundleResourceQuota(String bundleRange, ResourceQuota quota) {
validateSuperUserAccess();
validatePoliciesReadOnlyAccess();
Policies policies = getNamespacePolicies(namespaceName);
if (!namespaceName.isGlobal()) {
validateClusterOwnership(namespaceName.getCluster());
validateClusterForProperty(namespaceName.getProperty(), namespaceName.getCluster());
}
NamespaceBundle nsBundle = validateNamespaceBundleRange(namespaceName, policies.bundles, bundleRange);
try {
pulsar().getLocalZkCacheService().getResourceQuotaCache().setQuota(nsBundle, quota);
log.info("[{}] Successfully set resource quota for namespace bundle {}", clientAppId(), nsBundle.toString());
} catch (KeeperException.NoNodeException e) {
log.warn("[{}] Failed to set resource quota for namespace bundle {}: concurrent modification", clientAppId(), nsBundle.toString());
throw new RestException(Status.CONFLICT, "Cuncurrent modification on namespace bundle quota");
} catch (Exception e) {
log.error("[{}] Failed to set resource quota for namespace bundle {}", clientAppId(), nsBundle.toString());
throw new RestException(e);
}
}
use of org.apache.pulsar.common.policies.data.Policies in project incubator-pulsar by apache.
the class Namespaces method setNamespaceAntiAffinityGroup.
@POST
@Path("/{property}/{cluster}/{namespace}/antiAffinity")
@ApiOperation(value = "Set anti-affinity group for a namespace")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 404, message = "Property or cluster or namespace doesn't exist"), @ApiResponse(code = 412, message = "Invalid antiAffinityGroup") })
public void setNamespaceAntiAffinityGroup(@PathParam("property") String property, @PathParam("cluster") String cluster, @PathParam("namespace") String namespace, String antiAffinityGroup) {
validateAdminAccessOnProperty(property);
validatePoliciesReadOnlyAccess();
log.info("[{}] Setting anti-affinity group {} for {}/{}/{}", clientAppId(), antiAffinityGroup, property, cluster, namespace);
if (isBlank(antiAffinityGroup)) {
throw new RestException(Status.PRECONDITION_FAILED, "antiAffinityGroup can't be empty");
}
NamespaceName nsName = NamespaceName.get(property, cluster, namespace);
Map.Entry<Policies, Stat> policiesNode = null;
try {
// Force to read the data s.t. the watch to the cache content is setup.
policiesNode = policiesCache().getWithStat(path(POLICIES, property, cluster, namespace)).orElseThrow(() -> new RestException(Status.NOT_FOUND, "Namespace " + nsName + " does not exist"));
policiesNode.getKey().antiAffinityGroup = antiAffinityGroup;
// Write back the new policies into zookeeper
globalZk().setData(path(POLICIES, property, cluster, namespace), jsonMapper().writeValueAsBytes(policiesNode.getKey()), policiesNode.getValue().getVersion());
policiesCache().invalidate(path(POLICIES, property, cluster, namespace));
log.info("[{}] Successfully updated the antiAffinityGroup {} on namespace {}/{}/{}", clientAppId(), antiAffinityGroup, property, cluster, namespace);
} catch (KeeperException.NoNodeException e) {
log.warn("[{}] Failed to update the antiAffinityGroup for namespace {}/{}/{}: does not exist", clientAppId(), property, cluster, namespace);
throw new RestException(Status.NOT_FOUND, "Namespace does not exist");
} catch (KeeperException.BadVersionException e) {
log.warn("[{}] Failed to update the antiAffinityGroup on namespace {}/{}/{} expected policy node version={} : concurrent modification", clientAppId(), property, cluster, namespace, policiesNode.getValue().getVersion());
throw new RestException(Status.CONFLICT, "Concurrent modification");
} catch (Exception e) {
log.error("[{}] Failed to update the antiAffinityGroup on namespace {}/{}/{}", clientAppId(), property, cluster, namespace, e);
throw new RestException(e);
}
}
use of org.apache.pulsar.common.policies.data.Policies in project incubator-pulsar by apache.
the class Namespaces method getNamespaceMessageTTL.
@GET
@Path("/{property}/{cluster}/{namespace}/messageTTL")
@ApiOperation(hidden = true, value = "Get the message TTL for the namespace")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 404, message = "Property or cluster or namespace doesn't exist") })
public int getNamespaceMessageTTL(@PathParam("property") String property, @PathParam("cluster") String cluster, @PathParam("namespace") String namespace) {
validateAdminAccessOnProperty(property);
validateNamespaceName(property, cluster, namespace);
Policies policies = getNamespacePolicies(namespaceName);
return policies.message_ttl_in_seconds;
}
use of org.apache.pulsar.common.policies.data.Policies in project incubator-pulsar by apache.
the class AdminResource method getNamespacePolicies.
protected Policies getNamespacePolicies(String property, String cluster, String namespace) {
try {
Policies policies = policiesCache().get(AdminResource.path(POLICIES, property, cluster, namespace)).orElseThrow(() -> new RestException(Status.NOT_FOUND, "Namespace does not exist"));
// fetch bundles from LocalZK-policies
NamespaceBundles bundles = pulsar().getNamespaceService().getNamespaceBundleFactory().getBundles(NamespaceName.get(property, cluster, namespace));
BundlesData bundleData = NamespaceBundleFactory.getBundlesData(bundles);
policies.bundles = bundleData != null ? bundleData : policies.bundles;
return policies;
} catch (RestException re) {
throw re;
} catch (Exception e) {
log.error("[{}] Failed to get namespace policies {}/{}/{}", clientAppId(), property, cluster, namespace, e);
throw new RestException(e);
}
}
use of org.apache.pulsar.common.policies.data.Policies in project incubator-pulsar by apache.
the class NamespacesBase method internalSetDispatchRate.
protected void internalSetDispatchRate(DispatchRate dispatchRate) {
log.info("[{}] Set namespace dispatch-rate {}/{}", clientAppId(), namespaceName, dispatchRate);
validateSuperUserAccess();
Entry<Policies, Stat> policiesNode = null;
try {
final String path = path(POLICIES, namespaceName.toString());
// Force to read the data s.t. the watch to the cache content is setup.
policiesNode = policiesCache().getWithStat(path).orElseThrow(() -> new RestException(Status.NOT_FOUND, "Namespace " + namespaceName + " does not exist"));
policiesNode.getKey().clusterDispatchRate.put(pulsar().getConfiguration().getClusterName(), dispatchRate);
// Write back the new policies into zookeeper
globalZk().setData(path, jsonMapper().writeValueAsBytes(policiesNode.getKey()), policiesNode.getValue().getVersion());
policiesCache().invalidate(path);
log.info("[{}] Successfully updated the dispatchRate for cluster on namespace {}", clientAppId(), namespaceName);
} catch (KeeperException.NoNodeException e) {
log.warn("[{}] Failed to update the dispatchRate for cluster on namespace {}: does not exist", clientAppId(), namespaceName);
throw new RestException(Status.NOT_FOUND, "Namespace does not exist");
} catch (KeeperException.BadVersionException e) {
log.warn("[{}] Failed to update the dispatchRate for cluster on namespace {} expected policy node version={} : concurrent modification", clientAppId(), namespaceName, policiesNode.getValue().getVersion());
throw new RestException(Status.CONFLICT, "Concurrent modification");
} catch (Exception e) {
log.error("[{}] Failed to update the dispatchRate for cluster on namespace {}", clientAppId(), namespaceName, e);
throw new RestException(e);
}
}
Aggregations