Search in sources :

Example 11 with RestException

use of org.apache.pulsar.broker.web.RestException in project incubator-pulsar by apache.

the class ClustersBase method updateCluster.

@POST
@Path("/{cluster}")
@ApiOperation(value = "Update the configuration for a cluster.", notes = "This operation requires Pulsar super-user privileges.")
@ApiResponses(value = { @ApiResponse(code = 204, message = "Cluster has been updated"), @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 404, message = "Cluster doesn't exist") })
public void updateCluster(@PathParam("cluster") String cluster, ClusterData clusterData) {
    validateSuperUserAccess();
    validatePoliciesReadOnlyAccess();
    try {
        String clusterPath = path("clusters", cluster);
        Stat nodeStat = new Stat();
        byte[] content = globalZk().getData(clusterPath, null, nodeStat);
        ClusterData currentClusterData = null;
        if (content.length > 0) {
            currentClusterData = jsonMapper().readValue(content, ClusterData.class);
            // only update cluster-url-data and not overwrite other metadata such as peerClusterNames
            currentClusterData.update(clusterData);
        } else {
            currentClusterData = clusterData;
        }
        // Write back the new updated ClusterData into zookeeper
        globalZk().setData(clusterPath, jsonMapper().writeValueAsBytes(currentClusterData), nodeStat.getVersion());
        globalZkCache().invalidate(clusterPath);
        log.info("[{}] Updated cluster {}", clientAppId(), cluster);
    } catch (KeeperException.NoNodeException e) {
        log.warn("[{}] Failed to update cluster {}: Does not exist", clientAppId(), cluster);
        throw new RestException(Status.NOT_FOUND, "Cluster does not exist");
    } catch (Exception e) {
        log.error("[{}] Failed to update cluster {}", clientAppId(), cluster, e);
        throw new RestException(e);
    }
}
Also used : Stat(org.apache.zookeeper.data.Stat) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) RestException(org.apache.pulsar.broker.web.RestException) KeeperException(org.apache.zookeeper.KeeperException) RestException(org.apache.pulsar.broker.web.RestException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 12 with RestException

use of org.apache.pulsar.broker.web.RestException in project incubator-pulsar by apache.

the class ClustersBase method createCluster.

@PUT
@Path("/{cluster}")
@ApiOperation(value = "Provisions a new cluster. This operation requires Pulsar super-user privileges.", notes = "The name cannot contain '/' characters.")
@ApiResponses(value = { @ApiResponse(code = 204, message = "Cluster has been created"), @ApiResponse(code = 403, message = "You don't have admin permission to create the cluster"), @ApiResponse(code = 409, message = "Cluster already exists"), @ApiResponse(code = 412, message = "Cluster name is not valid") })
public void createCluster(@PathParam("cluster") String cluster, ClusterData clusterData) {
    validateSuperUserAccess();
    validatePoliciesReadOnlyAccess();
    try {
        NamedEntity.checkName(cluster);
        zkCreate(path("clusters", cluster), jsonMapper().writeValueAsBytes(clusterData));
        log.info("[{}] Created cluster {}", clientAppId(), cluster);
    } catch (KeeperException.NodeExistsException e) {
        log.warn("[{}] Failed to create already existing cluster {}", clientAppId(), cluster);
        throw new RestException(Status.CONFLICT, "Cluster already exist");
    } catch (IllegalArgumentException e) {
        log.warn("[{}] Failed to create cluster with invalid name {}", clientAppId(), cluster, e);
        throw new RestException(Status.PRECONDITION_FAILED, "Cluster name is not valid");
    } catch (Exception e) {
        log.error("[{}] Failed to create cluster {}", clientAppId(), cluster, 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) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Path(javax.ws.rs.Path) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Example 13 with RestException

use of org.apache.pulsar.broker.web.RestException in project incubator-pulsar by apache.

the class ClustersBase method getPeerCluster.

@GET
@Path("/{cluster}/peers")
@ApiOperation(value = "Get the peer-cluster data for the specified cluster.", response = Set.class)
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 404, message = "Cluster doesn't exist") })
public Set<String> getPeerCluster(@PathParam("cluster") String cluster) {
    validateSuperUserAccess();
    try {
        String clusterPath = path("clusters", cluster);
        byte[] content = globalZk().getData(clusterPath, null, null);
        ClusterData clusterData = jsonMapper().readValue(content, ClusterData.class);
        return clusterData.getPeerClusterNames();
    } catch (KeeperException.NoNodeException e) {
        log.warn("[{}] Failed to get cluster {}: Does not exist", clientAppId(), cluster);
        throw new RestException(Status.NOT_FOUND, "Cluster does not exist");
    } catch (Exception e) {
        log.error("[{}] Failed to get cluster {}", clientAppId(), cluster, e);
        throw new RestException(e);
    }
}
Also used : ClusterData(org.apache.pulsar.common.policies.data.ClusterData) RestException(org.apache.pulsar.broker.web.RestException) KeeperException(org.apache.zookeeper.KeeperException) RestException(org.apache.pulsar.broker.web.RestException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 14 with RestException

use of org.apache.pulsar.broker.web.RestException in project incubator-pulsar by apache.

the class ClustersBase method setNamespaceIsolationPolicy.

@POST
@Path("/{cluster}/namespaceIsolationPolicies/{policyName}")
@ApiOperation(value = "Set namespace isolation policy")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission or plicy is read only"), @ApiResponse(code = 412, message = "Cluster doesn't exist") })
public void setNamespaceIsolationPolicy(@PathParam("cluster") String cluster, @PathParam("policyName") String policyName, NamespaceIsolationData policyData) throws Exception {
    validateSuperUserAccess();
    validateClusterExists(cluster);
    validatePoliciesReadOnlyAccess();
    try {
        // validate the policy data before creating the node
        policyData.validate();
        String nsIsolationPolicyPath = path("clusters", cluster, "namespaceIsolationPolicies");
        NamespaceIsolationPolicies nsIsolationPolicies = namespaceIsolationPoliciesCache().get(nsIsolationPolicyPath).orElseGet(() -> {
            try {
                this.createZnodeIfNotExist(nsIsolationPolicyPath, Optional.of(Collections.emptyMap()));
                return new NamespaceIsolationPolicies();
            } catch (KeeperException | InterruptedException e) {
                throw new RestException(e);
            }
        });
        nsIsolationPolicies.setPolicy(policyName, policyData);
        globalZk().setData(nsIsolationPolicyPath, jsonMapper().writeValueAsBytes(nsIsolationPolicies.getPolicies()), -1);
        // make sure that the cache content will be refreshed for the next read access
        namespaceIsolationPoliciesCache().invalidate(nsIsolationPolicyPath);
    } catch (IllegalArgumentException iae) {
        log.info("[{}] Failed to update clusters/{}/namespaceIsolationPolicies/{}. Input data is invalid", clientAppId(), cluster, policyName, iae);
        String jsonInput = ObjectMapperFactory.create().writeValueAsString(policyData);
        throw new RestException(Status.BAD_REQUEST, "Invalid format of input policy data. policy: " + policyName + "; data: " + jsonInput);
    } catch (KeeperException.NoNodeException nne) {
        log.warn("[{}] Failed to update clusters/{}/namespaceIsolationPolicies: Does not exist", clientAppId(), cluster);
        throw new RestException(Status.NOT_FOUND, "NamespaceIsolationPolicies for cluster " + cluster + " does not exist");
    } catch (Exception e) {
        log.error("[{}] Failed to update clusters/{}/namespaceIsolationPolicies/{}", clientAppId(), cluster, policyName, e);
        throw new RestException(e);
    }
}
Also used : NamespaceIsolationPolicies(org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies) RestException(org.apache.pulsar.broker.web.RestException) KeeperException(org.apache.zookeeper.KeeperException) RestException(org.apache.pulsar.broker.web.RestException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 15 with RestException

use of org.apache.pulsar.broker.web.RestException in project incubator-pulsar by apache.

the class ClustersBase method setFailureDomain.

@POST
@Path("/{cluster}/failureDomains/{domainName}")
@ApiOperation(value = "Set cluster's failure Domain")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 409, message = "Broker already exist into other domain"), @ApiResponse(code = 404, message = "Cluster doesn't exist") })
public void setFailureDomain(@PathParam("cluster") String cluster, @PathParam("domainName") String domainName, FailureDomain domain) throws Exception {
    validateSuperUserAccess();
    validateClusterExists(cluster);
    validateBrokerExistsInOtherDomain(cluster, domainName, domain);
    try {
        String domainPath = joinPath(pulsar().getConfigurationCache().CLUSTER_FAILURE_DOMAIN_ROOT, domainName);
        if (this.createZnodeIfNotExist(domainPath, Optional.ofNullable(domain))) {
            // clear domains-children cache
            this.failureDomainListCache().clear();
        } else {
            globalZk().setData(domainPath, jsonMapper().writeValueAsBytes(domain), -1);
            // make sure that the domain-cache will be refreshed for the next read access
            failureDomainCache().invalidate(domainPath);
        }
    } catch (KeeperException.NoNodeException nne) {
        log.warn("[{}] Failed to update domain {}. clusters {}  Does not exist", clientAppId(), cluster, domainName);
        throw new RestException(Status.NOT_FOUND, "Domain " + domainName + " for cluster " + cluster + " does not exist");
    } catch (Exception e) {
        log.error("[{}] Failed to update clusters/{}/domainName/{}", clientAppId(), cluster, domainName, 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) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) 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