Search in sources :

Example 51 with RestException

use of com.yahoo.pulsar.broker.web.RestException in project pulsar by yahoo.

the class Clusters 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(com.yahoo.pulsar.broker.web.RestException) KeeperException(org.apache.zookeeper.KeeperException) RestException(com.yahoo.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 52 with RestException

use of com.yahoo.pulsar.broker.web.RestException in project pulsar by yahoo.

the class Clusters method deleteCluster.

@DELETE
@Path("/{cluster}")
@ApiOperation(value = "Delete an existing cluster")
@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"), @ApiResponse(code = 412, message = "Cluster is not empty") })
public void deleteCluster(@PathParam("cluster") String cluster) {
    validateSuperUserAccess();
    validatePoliciesReadOnlyAccess();
    // Check that the cluster is not used by any property (eg: no namespaces provisioned there)
    boolean isClusterUsed = false;
    try {
        for (String property : globalZk().getChildren(path("policies"), false)) {
            if (globalZk().exists(path("policies", property, cluster), false) == null) {
                continue;
            }
            if (!globalZk().getChildren(path("policies", property, cluster), false).isEmpty()) {
                // We found a property that has at least a namespace in this cluster
                isClusterUsed = true;
                break;
            }
        }
        // check the namespaceIsolationPolicies associated with the cluster
        String path = path("clusters", cluster, "namespaceIsolationPolicies");
        Optional<NamespaceIsolationPolicies> nsIsolationPolicies = namespaceIsolationPoliciesCache().get(path);
        // Need to delete the isolation policies if present
        if (nsIsolationPolicies.isPresent()) {
            if (nsIsolationPolicies.get().getPolicies().isEmpty()) {
                globalZk().delete(path, -1);
                namespaceIsolationPoliciesCache().invalidate(path);
            } else {
                isClusterUsed = true;
            }
        }
    } catch (Exception e) {
        log.error("[{}] Failed to get cluster usage {}", clientAppId(), cluster, e);
        throw new RestException(e);
    }
    if (isClusterUsed) {
        log.warn("[{}] Failed to delete cluster {} - Cluster not empty", clientAppId(), cluster);
        throw new RestException(Status.PRECONDITION_FAILED, "Cluster not empty");
    }
    try {
        String clusterPath = path("clusters", cluster);
        globalZk().delete(clusterPath, -1);
        globalZkCache().invalidate(clusterPath);
        log.info("[{}] Deleted cluster {}", clientAppId(), cluster);
    } catch (KeeperException.NoNodeException e) {
        log.warn("[{}] Failed to delete cluster {} - Does not exist", clientAppId(), cluster);
        throw new RestException(Status.NOT_FOUND, "Cluster does not exist");
    } catch (Exception e) {
        log.error("[{}] Failed to delete cluster {}", clientAppId(), cluster, e);
        throw new RestException(e);
    }
}
Also used : NamespaceIsolationPolicies(com.yahoo.pulsar.common.policies.impl.NamespaceIsolationPolicies) RestException(com.yahoo.pulsar.broker.web.RestException) RestException(com.yahoo.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) KeeperException(org.apache.zookeeper.KeeperException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 53 with RestException

use of com.yahoo.pulsar.broker.web.RestException in project pulsar by yahoo.

the class Namespaces method getNamespacesForCluster.

@GET
@Path("/{property}/{cluster}")
@ApiOperation(value = "Get the list of all the namespaces for a certain property on single cluster.", response = String.class, responseContainer = "Set")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 404, message = "Property or cluster doesn't exist") })
public List<String> getNamespacesForCluster(@PathParam("property") String property, @PathParam("cluster") String cluster) {
    validateAdminAccessOnProperty(property);
    List<String> namespaces = Lists.newArrayList();
    if (!clusters().contains(cluster)) {
        log.warn("[{}] Failed to get namespace list for property: {}/{} - Cluster does not exist", clientAppId(), property, cluster);
        throw new RestException(Status.NOT_FOUND, "Cluster does not exist");
    }
    try {
        for (String namespace : globalZk().getChildren(path("policies", property, cluster), false)) {
            namespaces.add(String.format("%s/%s/%s", property, cluster, namespace));
        }
    } catch (KeeperException.NoNodeException e) {
    // NoNode means there are no namespaces for this property on the specified cluster, returning empty list
    } catch (Exception e) {
        log.error("[{}] Failed to get namespaces list: {}", clientAppId(), e);
        throw new RestException(e);
    }
    namespaces.sort(null);
    return namespaces;
}
Also used : NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) RestException(com.yahoo.pulsar.broker.web.RestException) KeeperException(org.apache.zookeeper.KeeperException) RestException(com.yahoo.pulsar.broker.web.RestException) WebApplicationException(javax.ws.rs.WebApplicationException) SubscriptionBusyException(com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) KeeperException(org.apache.zookeeper.KeeperException) PulsarAdminException(com.yahoo.pulsar.client.admin.PulsarAdminException) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 54 with RestException

use of com.yahoo.pulsar.broker.web.RestException in project pulsar by yahoo.

the class Namespaces method deleteNamespaceBundle.

@DELETE
@Path("/{property}/{cluster}/{namespace}/{bundle}")
@ApiOperation(value = "Delete a namespace bundle and all the destinations under it.")
@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 = 409, message = "Namespace bundle is not empty") })
public void deleteNamespaceBundle(@PathParam("property") String property, @PathParam("cluster") String cluster, @PathParam("namespace") String namespace, @PathParam("bundle") String bundleRange, @QueryParam("authoritative") @DefaultValue("false") boolean authoritative) {
    NamespaceName nsName = new NamespaceName(property, cluster, namespace);
    validateAdminAccessOnProperty(property);
    validatePoliciesReadOnlyAccess();
    // ensure that non-global namespace is directed to the correct cluster
    validateClusterOwnership(cluster);
    Policies policies = getNamespacePolicies(property, cluster, namespace);
    // ensure the local cluster is the only cluster for the global namespace configuration
    try {
        if (cluster.equals(Namespaces.GLOBAL_CLUSTER)) {
            if (policies.replication_clusters.size() > 1) {
                // There are still more than one clusters configured for the global namespace
                throw new RestException(Status.PRECONDITION_FAILED, "Cannot delete the global namespace " + nsName + ". There are still more than one replication clusters configured.");
            }
            if (policies.replication_clusters.size() == 1 && !policies.replication_clusters.contains(config().getClusterName())) {
                // the only replication cluster is other cluster, redirect
                String replCluster = policies.replication_clusters.get(0);
                ClusterData replClusterData = clustersCache().get(AdminResource.path("clusters", replCluster)).orElseThrow(() -> new RestException(Status.NOT_FOUND, "Cluser " + replCluster + " does not exist"));
                URL replClusterUrl;
                if (!config().isTlsEnabled()) {
                    replClusterUrl = new URL(replClusterData.getServiceUrl());
                } else if (!replClusterData.getServiceUrlTls().isEmpty()) {
                    replClusterUrl = new URL(replClusterData.getServiceUrlTls());
                } else {
                    throw new RestException(Status.PRECONDITION_FAILED, "The replication cluster does not provide TLS encrypted service");
                }
                URI redirect = UriBuilder.fromUri(uri.getRequestUri()).host(replClusterUrl.getHost()).port(replClusterUrl.getPort()).replaceQueryParam("authoritative", false).build();
                log.debug("[{}] Redirecting the rest call to {}: cluster={}", clientAppId(), redirect, cluster);
                throw new WebApplicationException(Response.temporaryRedirect(redirect).build());
            }
        }
    } catch (WebApplicationException wae) {
        throw wae;
    } catch (Exception e) {
        throw new RestException(e);
    }
    NamespaceBundle bundle = validateNamespaceBundleOwnership(nsName, policies.bundles, bundleRange, authoritative, true);
    try {
        List<String> destinations = getDestinations(property, cluster, namespace);
        for (String destination : destinations) {
            NamespaceBundle destinationBundle = (NamespaceBundle) pulsar().getNamespaceService().getBundle(DestinationName.get(destination));
            if (bundle.equals(destinationBundle)) {
                throw new RestException(Status.CONFLICT, "Cannot delete non empty bundle");
            }
        }
        // remove from owned namespace map and ephemeral node from ZK
        pulsar().getNamespaceService().removeOwnedServiceUnit(bundle);
    } catch (WebApplicationException wae) {
        throw wae;
    } catch (Exception e) {
        log.error("[{}] Failed to remove namespace bundle {}/{}", clientAppId(), nsName.toString(), bundleRange, e);
        throw new RestException(e);
    }
}
Also used : NamespaceBundle(com.yahoo.pulsar.common.naming.NamespaceBundle) NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) Policies(com.yahoo.pulsar.common.policies.data.Policies) PersistencePolicies(com.yahoo.pulsar.common.policies.data.PersistencePolicies) RetentionPolicies(com.yahoo.pulsar.common.policies.data.RetentionPolicies) ClusterData(com.yahoo.pulsar.common.policies.data.ClusterData) WebApplicationException(javax.ws.rs.WebApplicationException) RestException(com.yahoo.pulsar.broker.web.RestException) URI(java.net.URI) URL(java.net.URL) RestException(com.yahoo.pulsar.broker.web.RestException) WebApplicationException(javax.ws.rs.WebApplicationException) SubscriptionBusyException(com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) KeeperException(org.apache.zookeeper.KeeperException) PulsarAdminException(com.yahoo.pulsar.client.admin.PulsarAdminException) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 55 with RestException

use of com.yahoo.pulsar.broker.web.RestException in project pulsar by yahoo.

the class ResourceQuotas method getNamespaceBundleResourceQuota.

@GET
@Path("/{property}/{cluster}/{namespace}/{bundle}")
@ApiOperation(value = "Get resource quota of a namespace bundle.")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 404, message = "Namespace does not exist") })
public ResourceQuota getNamespaceBundleResourceQuota(@PathParam("property") String property, @PathParam("cluster") String cluster, @PathParam("namespace") String namespace, @PathParam("bundle") String bundleRange) {
    validateSuperUserAccess();
    Policies policies = getNamespacePolicies(property, cluster, namespace);
    if (!cluster.equals(Namespaces.GLOBAL_CLUSTER)) {
        validateClusterOwnership(cluster);
        validateClusterForProperty(property, cluster);
    }
    NamespaceName fqnn = new NamespaceName(property, cluster, namespace);
    NamespaceBundle nsBundle = validateNamespaceBundleRange(fqnn, policies.bundles, bundleRange);
    try {
        return pulsar().getLocalZkCacheService().getResourceQuotaCache().getQuota(nsBundle);
    } catch (Exception e) {
        log.error("[{}] Failed to get resource quota for namespace bundle {}", clientAppId(), nsBundle.toString());
        throw new RestException(e);
    }
}
Also used : NamespaceBundle(com.yahoo.pulsar.common.naming.NamespaceBundle) NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) Policies(com.yahoo.pulsar.common.policies.data.Policies) RestException(com.yahoo.pulsar.broker.web.RestException) RestException(com.yahoo.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)

Aggregations

RestException (com.yahoo.pulsar.broker.web.RestException)79 KeeperException (org.apache.zookeeper.KeeperException)55 ApiOperation (io.swagger.annotations.ApiOperation)54 ApiResponses (io.swagger.annotations.ApiResponses)54 Path (javax.ws.rs.Path)54 WebApplicationException (javax.ws.rs.WebApplicationException)45 SubscriptionBusyException (com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)40 IOException (java.io.IOException)26 NamespaceName (com.yahoo.pulsar.common.naming.NamespaceName)25 PulsarAdminException (com.yahoo.pulsar.client.admin.PulsarAdminException)23 Policies (com.yahoo.pulsar.common.policies.data.Policies)23 PulsarClientException (com.yahoo.pulsar.client.api.PulsarClientException)22 PulsarServerException (com.yahoo.pulsar.broker.PulsarServerException)21 NotAllowedException (com.yahoo.pulsar.broker.service.BrokerServiceException.NotAllowedException)20 TopicBusyException (com.yahoo.pulsar.broker.service.BrokerServiceException.TopicBusyException)20 NotFoundException (com.yahoo.pulsar.client.admin.PulsarAdminException.NotFoundException)20 PreconditionFailedException (com.yahoo.pulsar.client.admin.PulsarAdminException.PreconditionFailedException)20 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)20 POST (javax.ws.rs.POST)19 DestinationName (com.yahoo.pulsar.common.naming.DestinationName)18