use of org.apache.pulsar.broker.web.RestException in project incubator-pulsar by apache.
the class PropertiesBase method getProperties.
@GET
@ApiOperation(value = "Get the list of properties.", response = String.class, responseContainer = "List")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 404, message = "Property doesn't exist") })
public List<String> getProperties() {
validateSuperUserAccess();
try {
List<String> properties = globalZk().getChildren(path(POLICIES), false);
properties.sort(null);
return properties;
} catch (Exception e) {
log.error("[{}] Failed to get properties list", clientAppId(), e);
throw new RestException(e);
}
}
use of org.apache.pulsar.broker.web.RestException in project incubator-pulsar by apache.
the class PropertiesBase method updateProperty.
@POST
@Path("/{property}")
@ApiOperation(value = "Update the admins for a property.", notes = "This operation requires Pulsar super-user privileges.")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 404, message = "Property does not exist"), @ApiResponse(code = 409, message = "Property already exist") })
public void updateProperty(@PathParam("property") String property, PropertyAdmin newPropertyAdmin) {
validateSuperUserAccess();
validatePoliciesReadOnlyAccess();
Stat nodeStat = new Stat();
try {
byte[] content = globalZk().getData(path(POLICIES, property), null, nodeStat);
PropertyAdmin oldPropertyAdmin = jsonMapper().readValue(content, PropertyAdmin.class);
List<String> clustersWithActiveNamespaces = Lists.newArrayList();
if (oldPropertyAdmin.getAllowedClusters().size() > newPropertyAdmin.getAllowedClusters().size()) {
// Get the colo(s) being removed from the list
oldPropertyAdmin.getAllowedClusters().removeAll(newPropertyAdmin.getAllowedClusters());
log.debug("Following clusters are being removed : [{}]", oldPropertyAdmin.getAllowedClusters());
for (String cluster : oldPropertyAdmin.getAllowedClusters()) {
List<String> activeNamespaces = Lists.newArrayList();
try {
activeNamespaces = globalZk().getChildren(path(POLICIES, property, cluster), false);
if (activeNamespaces.size() != 0) {
// There are active namespaces in this cluster
clustersWithActiveNamespaces.add(cluster);
}
} catch (KeeperException.NoNodeException nne) {
// Fine, some cluster does not have active namespace. Move on!
}
}
if (!clustersWithActiveNamespaces.isEmpty()) {
// Throw an exception because colos being removed are having active namespaces
String msg = String.format("Failed to update the property because active namespaces are present in colos %s. Please delete those namespaces first", clustersWithActiveNamespaces);
throw new RestException(Status.CONFLICT, msg);
}
}
String propertyPath = path(POLICIES, property);
globalZk().setData(propertyPath, jsonMapper().writeValueAsBytes(newPropertyAdmin), -1);
globalZkCache().invalidate(propertyPath);
log.info("[{}] updated property {}", clientAppId(), property);
} catch (RestException re) {
throw re;
} catch (KeeperException.NoNodeException e) {
log.warn("[{}] Failed to update property {}: does not exist", clientAppId(), property);
throw new RestException(Status.NOT_FOUND, "Property does not exist");
} catch (Exception e) {
log.error("[{}] Failed to update property {}", clientAppId(), property, e);
throw new RestException(e);
}
}
use of org.apache.pulsar.broker.web.RestException 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.broker.web.RestException in project incubator-pulsar by apache.
the class Namespaces method getTopics.
@GET
@Path("/{property}/{cluster}/{namespace}/destinations")
@ApiOperation(hidden = true, value = "Get the list of all the topics under a certain namespace.", response = String.class, responseContainer = "Set")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 404, message = "Property or cluster or namespace doesn't exist") })
public List<String> getTopics(@PathParam("property") String property, @PathParam("cluster") String cluster, @PathParam("namespace") String namespace) {
validateAdminAccessOnProperty(property);
validateNamespaceName(property, cluster, namespace);
// Validate that namespace exists, throws 404 if it doesn't exist
getNamespacePolicies(namespaceName);
try {
return pulsar().getNamespaceService().getListOfTopics(namespaceName);
} catch (Exception e) {
log.error("Failed to get topics list for namespace {}/{}/{}", property, cluster, namespace, e);
throw new RestException(e);
}
}
use of org.apache.pulsar.broker.web.RestException 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);
}
}
Aggregations