Search in sources :

Example 1 with RestException

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);
    }
}
Also used : RestException(org.apache.pulsar.broker.web.RestException) KeeperException(org.apache.zookeeper.KeeperException) RestException(org.apache.pulsar.broker.web.RestException) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with RestException

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);
    }
}
Also used : Stat(org.apache.zookeeper.data.Stat) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) RestException(org.apache.pulsar.broker.web.RestException) KeeperException(org.apache.zookeeper.KeeperException) KeeperException(org.apache.zookeeper.KeeperException) 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)

Example 3 with RestException

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);
    }
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) Policies(org.apache.pulsar.common.policies.data.Policies) RestException(org.apache.pulsar.broker.web.RestException) KeeperException(org.apache.zookeeper.KeeperException) KeeperException(org.apache.zookeeper.KeeperException) RestException(org.apache.pulsar.broker.web.RestException)

Example 4 with RestException

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);
    }
}
Also used : RestException(org.apache.pulsar.broker.web.RestException) RestException(org.apache.pulsar.broker.web.RestException) KeeperException(org.apache.zookeeper.KeeperException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 5 with RestException

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);
    }
}
Also used : NamespaceName(org.apache.pulsar.common.naming.NamespaceName) RetentionPolicies(org.apache.pulsar.common.policies.data.RetentionPolicies) PersistencePolicies(org.apache.pulsar.common.policies.data.PersistencePolicies) Policies(org.apache.pulsar.common.policies.data.Policies) Stat(org.apache.zookeeper.data.Stat) RestException(org.apache.pulsar.broker.web.RestException) Map(java.util.Map) KeeperException(org.apache.zookeeper.KeeperException) RestException(org.apache.pulsar.broker.web.RestException) KeeperException(org.apache.zookeeper.KeeperException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

RestException (org.apache.pulsar.broker.web.RestException)112 KeeperException (org.apache.zookeeper.KeeperException)81 WebApplicationException (javax.ws.rs.WebApplicationException)55 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)55 ExecutionException (java.util.concurrent.ExecutionException)53 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)51 SubscriptionBusyException (org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)49 IOException (java.io.IOException)36 Policies (org.apache.pulsar.common.policies.data.Policies)33 ApiResponses (io.swagger.annotations.ApiResponses)30 Path (javax.ws.rs.Path)29 ApiOperation (io.swagger.annotations.ApiOperation)27 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)25 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)24 NotAllowedException (org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException)24 TopicBusyException (org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException)24 NotFoundException (org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException)24 PreconditionFailedException (org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException)24 RetentionPolicies (org.apache.pulsar.common.policies.data.RetentionPolicies)24 PersistencePolicies (org.apache.pulsar.common.policies.data.PersistencePolicies)22