use of org.apache.pulsar.broker.web.RestException in project incubator-pulsar by apache.
the class NamespacesTest method testSplitBundleWithUnDividedRange.
@Test
public void testSplitBundleWithUnDividedRange() throws Exception {
URL localWebServiceUrl = new URL(pulsar.getWebServiceAddress());
String bundledNsLocal = "test-bundled-namespace-1";
BundlesData bundleData = new BundlesData(Lists.newArrayList("0x00000000", "0x08375b1a", "0x08375b1b", "0xffffffff"));
createBundledTestNamespaces(this.testProperty, this.testLocalCluster, bundledNsLocal, bundleData);
final NamespaceName testNs = NamespaceName.get(this.testProperty, this.testLocalCluster, bundledNsLocal);
OwnershipCache MockOwnershipCache = spy(pulsar.getNamespaceService().getOwnershipCache());
doNothing().when(MockOwnershipCache).disableOwnership(any(NamespaceBundle.class));
Field ownership = NamespaceService.class.getDeclaredField("ownershipCache");
ownership.setAccessible(true);
ownership.set(pulsar.getNamespaceService(), MockOwnershipCache);
mockWebUrl(localWebServiceUrl, testNs);
// split bundles
try {
namespaces.splitNamespaceBundle(testProperty, testLocalCluster, bundledNsLocal, "0x08375b1a_0x08375b1b", false, false);
} catch (RestException re) {
assertEquals(re.getResponse().getStatus(), Status.PRECONDITION_FAILED.getStatusCode());
}
}
use of org.apache.pulsar.broker.web.RestException in project incubator-pulsar by apache.
the class NamespacesTest method testValidateTopicOwnership.
@Test
public void testValidateTopicOwnership() throws Exception {
try {
URL localWebServiceUrl = new URL(pulsar.getWebServiceAddress());
String bundledNsLocal = "test-bundled-namespace-1";
BundlesData bundleData = new BundlesData(Lists.newArrayList("0x00000000", "0xffffffff"));
createBundledTestNamespaces(this.testProperty, this.testLocalCluster, bundledNsLocal, bundleData);
final NamespaceName testNs = NamespaceName.get(this.testProperty, this.testLocalCluster, bundledNsLocal);
OwnershipCache MockOwnershipCache = spy(pulsar.getNamespaceService().getOwnershipCache());
doNothing().when(MockOwnershipCache).disableOwnership(any(NamespaceBundle.class));
Field ownership = NamespaceService.class.getDeclaredField("ownershipCache");
ownership.setAccessible(true);
ownership.set(pulsar.getNamespaceService(), MockOwnershipCache);
TopicName topicName = TopicName.get(testNs.getPersistentTopicName("my-topic"));
PersistentTopics topics = spy(new PersistentTopics());
topics.setServletContext(new MockServletContext());
topics.setPulsar(pulsar);
doReturn(false).when(topics).isRequestHttps();
doReturn("test").when(topics).clientAppId();
mockWebUrl(localWebServiceUrl, testNs);
doReturn("persistent").when(topics).domain();
try {
topics.validateTopicName(topicName.getProperty(), topicName.getCluster(), topicName.getNamespacePortion(), topicName.getEncodedLocalName());
topics.validateAdminOperationOnTopic(false);
} catch (RestException e) {
fail("validateAdminAccessOnProperty failed");
}
} catch (RestException e) {
fail("validateAdminAccessOnProperty failed");
}
}
use of org.apache.pulsar.broker.web.RestException in project incubator-pulsar by apache.
the class ClustersBase method setPeerClusterNames.
@POST
@Path("/{cluster}/peers")
@ApiOperation(value = "Update peer-cluster-list 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 = 412, message = "Peer cluster doesn't exist"), @ApiResponse(code = 404, message = "Cluster doesn't exist") })
public void setPeerClusterNames(@PathParam("cluster") String cluster, LinkedHashSet<String> peerClusterNames) {
validateSuperUserAccess();
validatePoliciesReadOnlyAccess();
// validate if peer-cluster exist
if (peerClusterNames != null && !peerClusterNames.isEmpty()) {
for (String peerCluster : peerClusterNames) {
try {
if (cluster.equalsIgnoreCase(peerCluster)) {
throw new RestException(Status.PRECONDITION_FAILED, cluster + " itself can't be part of peer-list");
}
clustersCache().get(path("clusters", peerCluster)).orElseThrow(() -> new RestException(Status.PRECONDITION_FAILED, "Peer cluster " + peerCluster + " does not exist"));
} catch (RestException e) {
log.warn("[{}] Peer cluster doesn't exist from {}, {}", clientAppId(), peerClusterNames, e.getMessage());
throw e;
} catch (Exception e) {
log.warn("[{}] Failed to validate peer-cluster list {}, {}", clientAppId(), peerClusterNames, e.getMessage());
throw new RestException(e);
}
}
}
try {
String clusterPath = path("clusters", cluster);
Stat nodeStat = new Stat();
byte[] content = globalZk().getData(clusterPath, null, nodeStat);
ClusterData currentClusterData = jsonMapper().readValue(content, ClusterData.class);
currentClusterData.setPeerClusterNames(peerClusterNames);
// Write back the new updated ClusterData into zookeeper
globalZk().setData(clusterPath, jsonMapper().writeValueAsBytes(currentClusterData), nodeStat.getVersion());
globalZkCache().invalidate(clusterPath);
log.info("[{}] Successfully added peer-cluster {} for {}", clientAppId(), peerClusterNames, 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 deleteFailureDomain.
private void deleteFailureDomain(String clusterPath) {
try {
String failureDomain = joinPath(clusterPath, ConfigurationCacheService.FAILURE_DOMAIN);
if (globalZk().exists(failureDomain, false) == null) {
return;
}
for (String domain : globalZk().getChildren(failureDomain, false)) {
String domainPath = joinPath(failureDomain, domain);
globalZk().delete(domainPath, -1);
}
globalZk().delete(failureDomain, -1);
failureDomainCache().clear();
failureDomainListCache().clear();
} catch (Exception e) {
log.warn("Failed to delete failure-domain under cluster {}", clusterPath);
throw new RestException(e);
}
}
use of org.apache.pulsar.broker.web.RestException in project incubator-pulsar by apache.
the class ClustersBase 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);
deleteFailureDomain(clusterPath);
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);
}
}
Aggregations