use of org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies in project incubator-pulsar by apache.
the class NamespaceIsolationPoliciesTest method testDefaultConstructor.
@Test
public void testDefaultConstructor() throws Exception {
NamespaceIsolationPolicies policies = new NamespaceIsolationPolicies();
assertTrue(policies.getPolicies().isEmpty());
byte[] outJson = ObjectMapperFactory.create().writeValueAsBytes(policies.getPolicies());
assertEquals(new String(outJson), "{}");
}
use of org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies in project incubator-pulsar by apache.
the class NamespaceIsolationPoliciesTest method testGetNamespaceIsolationPolicyByName.
@Test
public void testGetNamespaceIsolationPolicyByName() throws Exception {
NamespaceIsolationPolicies policies = this.getDefaultTestPolicies();
NamespaceIsolationPolicy nsPolicy = policies.getPolicyByName("non-existing-policy");
assertTrue(nsPolicy == null);
nsPolicy = policies.getPolicyByName("policy1");
assertNotNull(nsPolicy);
assertEquals(new NamespaceIsolationPolicyImpl(policies.getPolicies().get("policy1")), nsPolicy);
}
use of org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies in project incubator-pulsar by apache.
the class NamespaceIsolationPoliciesTest method testSetPolicy.
@Test
public void testSetPolicy() throws Exception {
NamespaceIsolationPolicies policies = this.getDefaultTestPolicies();
// set a new policy
String newPolicyJson = "{\"namespaces\":[\"pulsar/use/TESTNS.*\"],\"primary\":[\"prod1-broker[45].messaging.use.example.com\"],\"secondary\":[\"prod1-broker.*.use.example.com\"],\"auto_failover_policy\":{\"policy_type\":\"min_available\",\"parameters\":{\"min_limit\":2,\"usage_threshold\":80}}}";
String newPolicyName = "policy2";
ObjectMapper jsonMapper = ObjectMapperFactory.create();
NamespaceIsolationData nsPolicyData = jsonMapper.readValue(newPolicyJson.getBytes(), NamespaceIsolationData.class);
policies.setPolicy(newPolicyName, nsPolicyData);
assertEquals(policies.getPolicies().size(), 2);
assertEquals(policies.getPolicyByName(newPolicyName), new NamespaceIsolationPolicyImpl(nsPolicyData));
assertTrue(!policies.getPolicyByName(newPolicyName).equals(policies.getPolicyByName("policy1")));
assertEquals(policies.getPolicyByNamespace(NamespaceName.get("pulsar/use/TESTNS.1")), new NamespaceIsolationPolicyImpl(nsPolicyData));
}
use of org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies 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);
}
}
use of org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies in project incubator-pulsar by apache.
the class ClustersBase method deleteNamespaceIsolationPolicy.
@DELETE
@Path("/{cluster}/namespaceIsolationPolicies/{policyName}")
@ApiOperation(value = "Delete 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 deleteNamespaceIsolationPolicy(@PathParam("cluster") String cluster, @PathParam("policyName") String policyName) throws Exception {
validateSuperUserAccess();
validateClusterExists(cluster);
validatePoliciesReadOnlyAccess();
try {
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.deletePolicy(policyName);
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 (KeeperException.NoNodeException nne) {
log.warn("[{}] Failed to update brokers/{}/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 brokers/{}/namespaceIsolationPolicies/{}", clientAppId(), cluster, policyName, e);
throw new RestException(e);
}
}
Aggregations