Search in sources :

Example 1 with NamespaceIsolationData

use of org.apache.pulsar.common.policies.data.NamespaceIsolationData in project incubator-pulsar by apache.

the class CmdNamespaceIsolationPolicy method createNamespaceIsolationData.

private NamespaceIsolationData createNamespaceIsolationData(List<String> namespaces, List<String> primary, List<String> secondary, String autoFailoverPolicyTypeName, Map<String, String> autoFailoverPolicyParams) {
    // validate
    namespaces = validateList(namespaces);
    if (namespaces.isEmpty()) {
        throw new ParameterException("unable to parse namespaces parameter list: " + namespaces);
    }
    primary = validateList(primary);
    if (primary.isEmpty()) {
        throw new ParameterException("unable to parse primary parameter list: " + namespaces);
    }
    secondary = validateList(secondary);
    // System.out.println("namespaces = " + namespaces);
    // System.out.println("primary = " + primary);
    // System.out.println("secondary = " + secondary);
    // System.out.println("autoFailoverPolicyTypeName = " + autoFailoverPolicyTypeName);
    // System.out.println("autoFailoverPolicyParams = " + autoFailoverPolicyParams);
    NamespaceIsolationData nsIsolationData = new NamespaceIsolationData();
    if (namespaces != null) {
        nsIsolationData.namespaces = namespaces;
    }
    if (primary != null) {
        nsIsolationData.primary = primary;
    }
    if (secondary != null) {
        nsIsolationData.secondary = secondary;
    }
    nsIsolationData.auto_failover_policy = new AutoFailoverPolicyData();
    nsIsolationData.auto_failover_policy.policy_type = AutoFailoverPolicyType.fromString(autoFailoverPolicyTypeName);
    nsIsolationData.auto_failover_policy.parameters = autoFailoverPolicyParams;
    // validation if necessary
    if (nsIsolationData.auto_failover_policy.policy_type == AutoFailoverPolicyType.min_available) {
        // ignore
        boolean error = true;
        String[] expectParamKeys = { "min_limit", "usage_threshold" };
        if (autoFailoverPolicyParams.size() == expectParamKeys.length) {
            for (String paramKey : expectParamKeys) {
                if (!autoFailoverPolicyParams.containsKey(paramKey)) {
                    break;
                }
            }
            error = false;
        }
        if (error) {
            throw new ParameterException("Unknown auto failover policy params specified : " + autoFailoverPolicyParams);
        }
    } else {
        // either we don't handle the new type or user has specified a bad type
        throw new ParameterException("Unknown auto failover policy type specified : " + autoFailoverPolicyTypeName);
    }
    return nsIsolationData;
}
Also used : AutoFailoverPolicyData(org.apache.pulsar.common.policies.data.AutoFailoverPolicyData) NamespaceIsolationData(org.apache.pulsar.common.policies.data.NamespaceIsolationData) ParameterException(com.beust.jcommander.ParameterException)

Example 2 with NamespaceIsolationData

use of org.apache.pulsar.common.policies.data.NamespaceIsolationData in project incubator-pulsar by apache.

the class NamespaceIsolationDataTest method testNamespaceIsolationData.

@Test
public void testNamespaceIsolationData() {
    NamespaceIsolationData n0 = new NamespaceIsolationData();
    NamespaceIsolationData n1 = new NamespaceIsolationData();
    assertFalse(n0.equals(new OldPolicies()));
    n0.namespaces = new ArrayList<>();
    n0.primary = new ArrayList<>();
    n0.secondary = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        n0.namespaces.add(String.format("ns%d", i));
        n0.primary.add(String.format("p%d", i));
        n0.secondary.add(String.format("s%d", i));
    }
    assertFalse(n0.equals(new NamespaceIsolationData()));
    n1.namespaces = n0.namespaces;
    n1.primary = n0.primary;
    n1.secondary = n0.secondary;
    assertTrue(n0.equals(n1));
    try {
        n0.validate();
        n1.validate();
        fail("Should not happen");
    } catch (Exception e) {
    // pass
    }
    AutoFailoverPolicyData policy0 = new AutoFailoverPolicyData();
    AutoFailoverPolicyData policy1 = new AutoFailoverPolicyData();
    policy0.policy_type = AutoFailoverPolicyType.min_available;
    policy0.parameters = new HashMap<>();
    policy0.parameters.put("min_limit", "3");
    policy0.parameters.put("usage_threshold", "10");
    policy1.policy_type = AutoFailoverPolicyType.min_available;
    policy1.parameters = new HashMap<>(policy0.parameters);
    n0.auto_failover_policy = policy0;
    n1.auto_failover_policy = policy1;
    try {
        n0.validate();
        n1.validate();
    } catch (Exception e) {
        fail("Should not happen");
    }
}
Also used : AutoFailoverPolicyData(org.apache.pulsar.common.policies.data.AutoFailoverPolicyData) NamespaceIsolationData(org.apache.pulsar.common.policies.data.NamespaceIsolationData) Test(org.testng.annotations.Test)

Example 3 with NamespaceIsolationData

use of org.apache.pulsar.common.policies.data.NamespaceIsolationData in project incubator-pulsar by apache.

the class AdminTest method clusters.

@Test
void clusters() throws Exception {
    assertEquals(clusters.getClusters(), Lists.newArrayList());
    verify(clusters, never()).validateSuperUserAccess();
    clusters.createCluster("use", new ClusterData("http://broker.messaging.use.example.com"));
    verify(clusters, times(1)).validateSuperUserAccess();
    // ensure to read from ZooKeeper directly
    clusters.clustersListCache().clear();
    assertEquals(clusters.getClusters(), Lists.newArrayList("use"));
    // Check creating existing cluster
    try {
        clusters.createCluster("use", new ClusterData("http://broker.messaging.use.example.com"));
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.CONFLICT.getStatusCode());
    }
    // Check deleting non-existing cluster
    try {
        clusters.deleteCluster("usc");
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.NOT_FOUND.getStatusCode());
    }
    assertEquals(clusters.getCluster("use"), new ClusterData("http://broker.messaging.use.example.com"));
    verify(clusters, times(4)).validateSuperUserAccess();
    clusters.updateCluster("use", new ClusterData("http://new-broker.messaging.use.example.com"));
    verify(clusters, times(5)).validateSuperUserAccess();
    assertEquals(clusters.getCluster("use"), new ClusterData("http://new-broker.messaging.use.example.com"));
    verify(clusters, times(6)).validateSuperUserAccess();
    try {
        clusters.getNamespaceIsolationPolicies("use");
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), 404);
    }
    NamespaceIsolationData policyData = new NamespaceIsolationData();
    policyData.namespaces = new ArrayList<String>();
    policyData.namespaces.add("dummy/colo/ns");
    policyData.primary = new ArrayList<String>();
    policyData.primary.add("localhost" + ":" + BROKER_WEBSERVICE_PORT);
    policyData.secondary = new ArrayList<String>();
    policyData.auto_failover_policy = new AutoFailoverPolicyData();
    policyData.auto_failover_policy.policy_type = AutoFailoverPolicyType.min_available;
    policyData.auto_failover_policy.parameters = new HashMap<String, String>();
    policyData.auto_failover_policy.parameters.put("min_limit", "1");
    policyData.auto_failover_policy.parameters.put("usage_threshold", "90");
    clusters.setNamespaceIsolationPolicy("use", "policy1", policyData);
    clusters.getNamespaceIsolationPolicies("use");
    try {
        clusters.deleteCluster("use");
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), 412);
    }
    clusters.deleteNamespaceIsolationPolicy("use", "policy1");
    assertTrue(clusters.getNamespaceIsolationPolicies("use").isEmpty());
    clusters.deleteCluster("use");
    verify(clusters, times(13)).validateSuperUserAccess();
    assertEquals(clusters.getClusters(), Lists.newArrayList());
    try {
        clusters.getCluster("use");
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), 404);
    }
    try {
        clusters.updateCluster("use", new ClusterData());
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), 404);
    }
    try {
        clusters.getNamespaceIsolationPolicies("use");
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), 404);
    }
    // Test zk failures
    mockZookKeeper.failNow(Code.SESSIONEXPIRED);
    configurationCache.clustersListCache().clear();
    try {
        clusters.getClusters();
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
    mockZookKeeper.failNow(Code.SESSIONEXPIRED);
    try {
        clusters.createCluster("test", new ClusterData("http://broker.messaging.test.example.com"));
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
    mockZookKeeper.failNow(Code.SESSIONEXPIRED);
    try {
        clusters.updateCluster("test", new ClusterData("http://broker.messaging.test.example.com"));
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
    mockZookKeeper.failNow(Code.SESSIONEXPIRED);
    try {
        clusters.getCluster("test");
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
    mockZookKeeper.failAfter(0, Code.SESSIONEXPIRED);
    try {
        clusters.deleteCluster("use");
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
    mockZookKeeper.failAfter(1, Code.SESSIONEXPIRED);
    try {
        clusters.deleteCluster("use");
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
    // Check name validations
    try {
        clusters.createCluster("bf@", new ClusterData("http://dummy.messaging.example.com"));
        fail("should have filed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.PRECONDITION_FAILED.getStatusCode());
    }
}
Also used : ClusterData(org.apache.pulsar.common.policies.data.ClusterData) AutoFailoverPolicyData(org.apache.pulsar.common.policies.data.AutoFailoverPolicyData) NamespaceIsolationData(org.apache.pulsar.common.policies.data.NamespaceIsolationData) RestException(org.apache.pulsar.broker.web.RestException) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 4 with NamespaceIsolationData

use of org.apache.pulsar.common.policies.data.NamespaceIsolationData 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 5 with NamespaceIsolationData

use of org.apache.pulsar.common.policies.data.NamespaceIsolationData in project incubator-pulsar by apache.

the class NamespaceIsolationPolicyImplTest method testConstructor.

@Test
public void testConstructor() throws Exception {
    NamespaceIsolationPolicyImpl defaultPolicy = this.getDefaultPolicy();
    NamespaceIsolationData policyData = new NamespaceIsolationData();
    policyData.namespaces = new ArrayList<String>();
    policyData.namespaces.add("pulsar/use/test.*");
    policyData.primary = new ArrayList<String>();
    policyData.primary.add("prod1-broker[1-3].messaging.use.example.com");
    policyData.secondary = new ArrayList<String>();
    policyData.secondary.add("prod1-broker.*.use.example.com");
    policyData.auto_failover_policy = new AutoFailoverPolicyData();
    policyData.auto_failover_policy.policy_type = AutoFailoverPolicyType.min_available;
    policyData.auto_failover_policy.parameters = new HashMap<String, String>();
    policyData.auto_failover_policy.parameters.put("min_limit", "3");
    policyData.auto_failover_policy.parameters.put("usage_threshold", "90");
    NamespaceIsolationPolicyImpl newPolicy = new NamespaceIsolationPolicyImpl(policyData);
    assertTrue(defaultPolicy.equals(newPolicy));
    policyData.auto_failover_policy.parameters.put("usage_threshold", "80");
    newPolicy = new NamespaceIsolationPolicyImpl(policyData);
    assertTrue(!defaultPolicy.equals(newPolicy));
    assertTrue(!newPolicy.equals(new OldPolicies()));
}
Also used : NamespaceIsolationPolicyImpl(org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicyImpl) AutoFailoverPolicyData(org.apache.pulsar.common.policies.data.AutoFailoverPolicyData) NamespaceIsolationData(org.apache.pulsar.common.policies.data.NamespaceIsolationData) OldPolicies(org.apache.pulsar.common.policies.data.OldPolicies) Test(org.testng.annotations.Test)

Aggregations

NamespaceIsolationData (org.apache.pulsar.common.policies.data.NamespaceIsolationData)10 AutoFailoverPolicyData (org.apache.pulsar.common.policies.data.AutoFailoverPolicyData)8 Test (org.testng.annotations.Test)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 NamespaceIsolationPolicies (org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies)4 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)2 ClusterData (org.apache.pulsar.common.policies.data.ClusterData)2 NamespaceIsolationPolicyImpl (org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicyImpl)2 ZooKeeper (org.apache.zookeeper.ZooKeeper)2 ParameterException (com.beust.jcommander.ParameterException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 BrokerTopicLoadingPredicate (org.apache.pulsar.broker.loadbalance.impl.LoadManagerShared.BrokerTopicLoadingPredicate)1 SimpleResourceAllocationPolicies (org.apache.pulsar.broker.loadbalance.impl.SimpleResourceAllocationPolicies)1 RestException (org.apache.pulsar.broker.web.RestException)1 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)1 NotFoundException (org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException)1 PreconditionFailedException (org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException)1 ServiceUnitId (org.apache.pulsar.common.naming.ServiceUnitId)1 NamespaceIsolationPolicy (org.apache.pulsar.common.policies.NamespaceIsolationPolicy)1