Search in sources :

Example 6 with NamespaceIsolationPolicies

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), "{}");
}
Also used : NamespaceIsolationPolicies(org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies) Test(org.testng.annotations.Test)

Example 7 with NamespaceIsolationPolicies

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);
}
Also used : NamespaceIsolationPolicies(org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies) NamespaceIsolationPolicyImpl(org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicyImpl) NamespaceIsolationPolicy(org.apache.pulsar.common.policies.NamespaceIsolationPolicy) Test(org.testng.annotations.Test)

Example 8 with NamespaceIsolationPolicies

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));
}
Also used : NamespaceIsolationPolicies(org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies) NamespaceIsolationPolicyImpl(org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicyImpl) NamespaceIsolationData(org.apache.pulsar.common.policies.data.NamespaceIsolationData) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Example 9 with NamespaceIsolationPolicies

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);
    }
}
Also used : NamespaceIsolationPolicies(org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies) RestException(org.apache.pulsar.broker.web.RestException) 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) 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 10 with NamespaceIsolationPolicies

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);
    }
}
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) DELETE(javax.ws.rs.DELETE) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

NamespaceIsolationPolicies (org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies)14 Test (org.testng.annotations.Test)7 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)4 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 ApiOperation (io.swagger.annotations.ApiOperation)4 ApiResponses (io.swagger.annotations.ApiResponses)4 IOException (java.io.IOException)4 Path (javax.ws.rs.Path)4 RestException (org.apache.pulsar.broker.web.RestException)4 NamespaceIsolationData (org.apache.pulsar.common.policies.data.NamespaceIsolationData)4 KeeperException (org.apache.zookeeper.KeeperException)4 NamespaceIsolationPolicy (org.apache.pulsar.common.policies.NamespaceIsolationPolicy)3 AutoFailoverPolicyData (org.apache.pulsar.common.policies.data.AutoFailoverPolicyData)3 NamespaceIsolationPolicyImpl (org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicyImpl)3 HashMap (java.util.HashMap)2 DELETE (javax.ws.rs.DELETE)2 ZooKeeper (org.apache.zookeeper.ZooKeeper)2 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1