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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations