Search in sources :

Example 1 with NamespaceIsolationPolicies

use of org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies 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);
    }
}
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) POST(javax.ws.rs.POST) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with NamespaceIsolationPolicies

use of org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies in project incubator-pulsar by apache.

the class NamespaceIsolationPoliciesTest method testDeletePolicy.

@Test
public void testDeletePolicy() throws Exception {
    NamespaceIsolationPolicies policies = this.getDefaultTestPolicies();
    policies.deletePolicy("non-existing-policy");
    assertTrue(!policies.getPolicies().isEmpty());
    policies.deletePolicy("policy1");
    assertTrue(policies.getPolicies().isEmpty());
}
Also used : NamespaceIsolationPolicies(org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies) Test(org.testng.annotations.Test)

Example 3 with NamespaceIsolationPolicies

use of org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies in project incubator-pulsar by apache.

the class NamespaceIsolationPoliciesTest method testJsonSerialization.

@Test
public void testJsonSerialization() throws Exception {
    // deserialize JSON string
    NamespaceIsolationPolicies policies = this.getDefaultTestPolicies();
    // serialize the object to JSON string
    ObjectMapper jsonMapperForWriter = ObjectMapperFactory.create();
    NamespaceIsolationPolicy nsPolicy = policies.getPolicyByName("policy1");
    assertNotNull(nsPolicy);
    List<String> primaryBrokers = nsPolicy.getPrimaryBrokers();
    byte[] primaryBrokersJson = jsonMapperForWriter.writeValueAsBytes(primaryBrokers);
    assertEquals(new String(primaryBrokersJson), "[\"prod1-broker[1-3].messaging.use.example.com\"]");
    List<String> secondaryBrokers = nsPolicy.getSecondaryBrokers();
    byte[] secondaryBrokersJson = jsonMapperForWriter.writeValueAsBytes(secondaryBrokers);
    assertEquals(new String(secondaryBrokersJson), "[\"prod1-broker.*.use.example.com\"]");
    byte[] outJson = jsonMapperForWriter.writeValueAsBytes(policies.getPolicies());
    assertEquals(new String(outJson), this.defaultJson);
    NamespaceIsolationData nsPolicyData = new NamespaceIsolationData();
    nsPolicyData.namespaces = new ArrayList<String>();
    nsPolicyData.namespaces.add("other/use/other.*");
    nsPolicyData.primary = new ArrayList<String>();
    nsPolicyData.primary.add("prod1-broker[4-6].messaging.use.example.com");
    nsPolicyData.secondary = new ArrayList<String>();
    nsPolicyData.secondary.add("prod1-broker.*.messaging.use.example.com");
    nsPolicyData.auto_failover_policy = new AutoFailoverPolicyData();
    nsPolicyData.auto_failover_policy.policy_type = AutoFailoverPolicyType.min_available;
    nsPolicyData.auto_failover_policy.parameters = new HashMap<String, String>();
    nsPolicyData.auto_failover_policy.parameters.put("min_limit", "1");
    nsPolicyData.auto_failover_policy.parameters.put("usage_threshold", "100");
    policies.setPolicy("otherPolicy", nsPolicyData);
    byte[] morePolicyJson = jsonMapperForWriter.writeValueAsBytes(policies.getPolicies());
    ObjectMapper jsonParser = ObjectMapperFactory.create();
    Map<String, NamespaceIsolationData> policiesMap = jsonParser.readValue(morePolicyJson, new TypeReference<Map<String, NamespaceIsolationData>>() {
    });
    assertEquals(policiesMap.size(), 2);
}
Also used : NamespaceIsolationPolicy(org.apache.pulsar.common.policies.NamespaceIsolationPolicy) AutoFailoverPolicyData(org.apache.pulsar.common.policies.data.AutoFailoverPolicyData) NamespaceIsolationData(org.apache.pulsar.common.policies.data.NamespaceIsolationData) NamespaceIsolationPolicies(org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies) HashMap(java.util.HashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Example 4 with NamespaceIsolationPolicies

use of org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies in project incubator-pulsar by apache.

the class NamespaceIsolationPoliciesTest method testBrokerAssignment.

@Test
public void testBrokerAssignment() throws Exception {
    NamespaceIsolationPolicies policies = this.getDefaultTestPolicies();
    NamespaceName ns = NamespaceName.get("pulsar/use/testns-1");
    SortedSet<BrokerStatus> primaryCandidates = new TreeSet<>();
    BrokerStatus primary = new BrokerStatus("prod1-broker1.messaging.use.example.com", true, 0);
    BrokerStatus secondary = new BrokerStatus("prod1-broker4.use.example.com", true, 0);
    BrokerStatus shared = new BrokerStatus("use.example.com", true, 0);
    SortedSet<BrokerStatus> secondaryCandidates = new TreeSet<>();
    SortedSet<BrokerStatus> sharedCandidates = new TreeSet<>();
    policies.assignBroker(ns, primary, primaryCandidates, secondaryCandidates, sharedCandidates);
    assertEquals(primaryCandidates.size(), 1);
    assertEquals(secondaryCandidates.size(), 0);
    assertEquals(sharedCandidates.size(), 0);
    assertTrue(primaryCandidates.first().equals(primary));
    policies.assignBroker(ns, secondary, primaryCandidates, secondaryCandidates, sharedCandidates);
    assertEquals(primaryCandidates.size(), 1);
    assertEquals(secondaryCandidates.size(), 1);
    assertEquals(sharedCandidates.size(), 0);
    assertTrue(secondaryCandidates.first().equals(secondary));
    policies.assignBroker(NamespaceName.get("pulsar/use1/testns-1"), shared, primaryCandidates, secondaryCandidates, sharedCandidates);
    assertEquals(primaryCandidates.size(), 1);
    assertEquals(secondaryCandidates.size(), 1);
    assertEquals(sharedCandidates.size(), 1);
    assertTrue(sharedCandidates.first().equals(shared));
}
Also used : NamespaceIsolationPolicies(org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) TreeSet(java.util.TreeSet) BrokerStatus(org.apache.pulsar.common.policies.data.BrokerStatus) Test(org.testng.annotations.Test)

Example 5 with NamespaceIsolationPolicies

use of org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies in project incubator-pulsar by apache.

the class NamespaceIsolationPoliciesTest method testGetNamespaceIsolationPolicyByNamespace.

@Test
public void testGetNamespaceIsolationPolicyByNamespace() throws Exception {
    NamespaceIsolationPolicies policies = this.getDefaultTestPolicies();
    NamespaceIsolationPolicy nsPolicy = policies.getPolicyByNamespace(NamespaceName.get("no/such/namespace"));
    assertTrue(nsPolicy == null);
    nsPolicy = policies.getPolicyByNamespace(NamespaceName.get("pulsar/use/testns-1"));
    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)

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