Search in sources :

Example 6 with AutoFailoverPolicyData

use of com.yahoo.pulsar.common.policies.data.AutoFailoverPolicyData in project pulsar by yahoo.

the class AdminApiTest method clusterNamespaceIsolationPolicies.

@Test
public void clusterNamespaceIsolationPolicies() throws PulsarAdminException {
    try {
        // create
        String policyName1 = "policy-1";
        NamespaceIsolationData nsPolicyData1 = new NamespaceIsolationData();
        nsPolicyData1.namespaces = new ArrayList<String>();
        nsPolicyData1.namespaces.add("other/use/other.*");
        nsPolicyData1.primary = new ArrayList<String>();
        nsPolicyData1.primary.add("prod1-broker[4-6].messaging.use.example.com");
        nsPolicyData1.secondary = new ArrayList<String>();
        nsPolicyData1.secondary.add("prod1-broker.*.messaging.use.example.com");
        nsPolicyData1.auto_failover_policy = new AutoFailoverPolicyData();
        nsPolicyData1.auto_failover_policy.policy_type = AutoFailoverPolicyType.min_available;
        nsPolicyData1.auto_failover_policy.parameters = new HashMap<String, String>();
        nsPolicyData1.auto_failover_policy.parameters.put("min_limit", "1");
        nsPolicyData1.auto_failover_policy.parameters.put("usage_threshold", "100");
        admin.clusters().createNamespaceIsolationPolicy("use", policyName1, nsPolicyData1);
        String policyName2 = "policy-2";
        NamespaceIsolationData nsPolicyData2 = new NamespaceIsolationData();
        nsPolicyData2.namespaces = new ArrayList<String>();
        nsPolicyData2.namespaces.add("other/use/other.*");
        nsPolicyData2.primary = new ArrayList<String>();
        nsPolicyData2.primary.add("prod1-broker[4-6].messaging.use.example.com");
        nsPolicyData2.secondary = new ArrayList<String>();
        nsPolicyData2.secondary.add("prod1-broker.*.messaging.use.example.com");
        nsPolicyData2.auto_failover_policy = new AutoFailoverPolicyData();
        nsPolicyData2.auto_failover_policy.policy_type = AutoFailoverPolicyType.min_available;
        nsPolicyData2.auto_failover_policy.parameters = new HashMap<String, String>();
        nsPolicyData2.auto_failover_policy.parameters.put("min_limit", "1");
        nsPolicyData2.auto_failover_policy.parameters.put("usage_threshold", "100");
        admin.clusters().createNamespaceIsolationPolicy("use", policyName2, nsPolicyData2);
        // verify create indirectly with get
        Map<String, NamespaceIsolationData> policiesMap = admin.clusters().getNamespaceIsolationPolicies("use");
        assertEquals(policiesMap.get(policyName1), nsPolicyData1);
        assertEquals(policiesMap.get(policyName2), nsPolicyData2);
        // verify update of primary
        nsPolicyData1.primary.remove(0);
        nsPolicyData1.primary.add("prod1-broker[1-2].messaging.use.example.com");
        admin.clusters().updateNamespaceIsolationPolicy("use", policyName1, nsPolicyData1);
        // verify primary change
        policiesMap = admin.clusters().getNamespaceIsolationPolicies("use");
        assertEquals(policiesMap.get(policyName1), nsPolicyData1);
        // verify update of secondary
        nsPolicyData1.secondary.remove(0);
        nsPolicyData1.secondary.add("prod1-broker[3-4].messaging.use.example.com");
        admin.clusters().updateNamespaceIsolationPolicy("use", policyName1, nsPolicyData1);
        // verify secondary change
        policiesMap = admin.clusters().getNamespaceIsolationPolicies("use");
        assertEquals(policiesMap.get(policyName1), nsPolicyData1);
        // verify update of failover policy limit
        nsPolicyData1.auto_failover_policy.parameters.put("min_limit", "10");
        admin.clusters().updateNamespaceIsolationPolicy("use", policyName1, nsPolicyData1);
        // verify min_limit change
        policiesMap = admin.clusters().getNamespaceIsolationPolicies("use");
        assertEquals(policiesMap.get(policyName1), nsPolicyData1);
        // verify update of failover usage_threshold limit
        nsPolicyData1.auto_failover_policy.parameters.put("usage_threshold", "80");
        admin.clusters().updateNamespaceIsolationPolicy("use", policyName1, nsPolicyData1);
        // verify usage_threshold change
        policiesMap = admin.clusters().getNamespaceIsolationPolicies("use");
        assertEquals(policiesMap.get(policyName1), nsPolicyData1);
        // verify single get
        NamespaceIsolationData policy1Data = admin.clusters().getNamespaceIsolationPolicy("use", policyName1);
        assertEquals(policy1Data, nsPolicyData1);
        // verify creation of more than one policy
        admin.clusters().createNamespaceIsolationPolicy("use", policyName2, nsPolicyData1);
        try {
            admin.clusters().getNamespaceIsolationPolicy("use", "no-such-policy");
            fail("should have raised exception");
        } catch (PulsarAdminException e) {
            assertTrue(e instanceof NotFoundException);
        }
        // verify delete cluster failed
        try {
            admin.clusters().deleteCluster("use");
            fail("should have raised exception");
        } catch (PulsarAdminException e) {
            assertTrue(e instanceof PreconditionFailedException);
        }
        // verify delete
        admin.clusters().deleteNamespaceIsolationPolicy("use", policyName1);
        admin.clusters().deleteNamespaceIsolationPolicy("use", policyName2);
        try {
            admin.clusters().getNamespaceIsolationPolicy("use", policyName1);
            fail("should have raised exception");
        } catch (PulsarAdminException e) {
            assertTrue(e instanceof NotFoundException);
        }
        try {
            admin.clusters().getNamespaceIsolationPolicy("use", policyName2);
            fail("should have raised exception");
        } catch (PulsarAdminException e) {
            assertTrue(e instanceof NotFoundException);
        }
        try {
            admin.clusters().getNamespaceIsolationPolicies("usc");
            fail("should have raised exception");
        } catch (PulsarAdminException e) {
            assertTrue(e instanceof NotFoundException);
        }
        try {
            admin.clusters().getNamespaceIsolationPolicy("usc", "no-such-cluster");
            fail("should have raised exception");
        } catch (PulsarAdminException e) {
            assertTrue(e instanceof PreconditionFailedException);
        }
        try {
            admin.clusters().createNamespaceIsolationPolicy("usc", "no-such-cluster", nsPolicyData1);
            fail("should have raised exception");
        } catch (PulsarAdminException e) {
            assertTrue(e instanceof PreconditionFailedException);
        }
        try {
            admin.clusters().updateNamespaceIsolationPolicy("usc", "no-such-cluster", policy1Data);
            fail("should have raised exception");
        } catch (PulsarAdminException e) {
            assertTrue(e instanceof PreconditionFailedException);
        }
    } catch (PulsarAdminException e) {
        LOG.warn("TEST FAILED [{}]", e.getMessage());
        throw e;
    }
}
Also used : AutoFailoverPolicyData(com.yahoo.pulsar.common.policies.data.AutoFailoverPolicyData) NamespaceIsolationData(com.yahoo.pulsar.common.policies.data.NamespaceIsolationData) NotFoundException(com.yahoo.pulsar.client.admin.PulsarAdminException.NotFoundException) PreconditionFailedException(com.yahoo.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) PulsarAdminException(com.yahoo.pulsar.client.admin.PulsarAdminException) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 7 with AutoFailoverPolicyData

use of com.yahoo.pulsar.common.policies.data.AutoFailoverPolicyData in project pulsar by yahoo.

the class LoadBalancerTest method createNamespacePolicies.

private void createNamespacePolicies(PulsarService pulsar) throws Exception {
    // // prepare three policies for the namespace isolation
    NamespaceIsolationPolicies policies = new NamespaceIsolationPolicies();
    // set up policy that use this broker as primary
    NamespaceIsolationData policyData = new NamespaceIsolationData();
    policyData.namespaces = new ArrayList<String>();
    policyData.namespaces.add("pulsar/use/primary-ns.*");
    policyData.primary = new ArrayList<String>();
    for (int i = 0; i < BROKER_COUNT; i++) {
        policyData.primary.add(lookupAddresses[i]);
    }
    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", "100");
    policies.setPolicy("primaryBrokerPolicy", policyData);
    // set up policy that use this broker as secondary
    policyData = new NamespaceIsolationData();
    policyData.namespaces = new ArrayList<String>();
    policyData.namespaces.add("pulsar/use/secondary-ns.*");
    policyData.primary = new ArrayList<String>();
    policyData.primary.add(lookupAddresses[0]);
    policyData.secondary = new ArrayList<String>();
    for (int i = 1; i < BROKER_COUNT; i++) {
        policyData.secondary.add(lookupAddresses[i]);
    }
    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", "100");
    policies.setPolicy("secondaryBrokerPolicy", policyData);
    // set up policy that do not use this broker (neither primary nor secondary)
    policyData = new NamespaceIsolationData();
    policyData.namespaces = new ArrayList<String>();
    policyData.namespaces.add("pulsar/use/shared-ns.*");
    policyData.primary = new ArrayList<String>();
    policyData.primary.add(lookupAddresses[0]);
    policyData.secondary = new ArrayList<String>();
    for (int i = 1; i < BROKER_COUNT; i++) {
        policyData.secondary.add(lookupAddresses[i]);
    }
    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", "100");
    policies.setPolicy("otherBrokerPolicy", policyData);
    ObjectMapper jsonMapper = ObjectMapperFactory.create();
    ZooKeeper globalZk = pulsar.getGlobalZkCache().getZooKeeper();
    ZkUtils.createFullPathOptimistic(globalZk, AdminResource.path("clusters", "use", "namespaceIsolationPolicies"), new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    byte[] content = jsonMapper.writeValueAsBytes(policies.getPolicies());
    globalZk.setData(AdminResource.path("clusters", "use", "namespaceIsolationPolicies"), content, -1);
}
Also used : NamespaceIsolationPolicies(com.yahoo.pulsar.common.policies.impl.NamespaceIsolationPolicies) ZooKeeper(org.apache.zookeeper.ZooKeeper) AutoFailoverPolicyData(com.yahoo.pulsar.common.policies.data.AutoFailoverPolicyData) NamespaceIsolationData(com.yahoo.pulsar.common.policies.data.NamespaceIsolationData) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 8 with AutoFailoverPolicyData

use of com.yahoo.pulsar.common.policies.data.AutoFailoverPolicyData in project pulsar by yahoo.

the class SimpleLoadManagerImplTest method createNamespacePolicies.

private void createNamespacePolicies(PulsarService pulsar) throws Exception {
    NamespaceIsolationPolicies policies = new NamespaceIsolationPolicies();
    // set up policy that use this broker as primary
    NamespaceIsolationData policyData = new NamespaceIsolationData();
    policyData.namespaces = new ArrayList<String>();
    policyData.namespaces.add("pulsar/use/primary-ns.*");
    policyData.primary = new ArrayList<String>();
    policyData.primary.add(pulsar1.getAdvertisedAddress() + "*");
    policyData.secondary = new ArrayList<String>();
    policyData.secondary.add("prod2-broker([78]).messaging.usw.example.co.*");
    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", "100");
    policies.setPolicy("primaryBrokerPolicy", policyData);
    ObjectMapper jsonMapper = ObjectMapperFactory.create();
    ZooKeeper globalZk = pulsar.getGlobalZkCache().getZooKeeper();
    ZkUtils.createFullPathOptimistic(globalZk, AdminResource.path("clusters", "use", "namespaceIsolationPolicies"), new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    byte[] content = jsonMapper.writeValueAsBytes(policies.getPolicies());
    globalZk.setData(AdminResource.path("clusters", "use", "namespaceIsolationPolicies"), content, -1);
}
Also used : NamespaceIsolationPolicies(com.yahoo.pulsar.common.policies.impl.NamespaceIsolationPolicies) ZooKeeper(org.apache.zookeeper.ZooKeeper) AutoFailoverPolicyData(com.yahoo.pulsar.common.policies.data.AutoFailoverPolicyData) NamespaceIsolationData(com.yahoo.pulsar.common.policies.data.NamespaceIsolationData) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

AutoFailoverPolicyData (com.yahoo.pulsar.common.policies.data.AutoFailoverPolicyData)8 NamespaceIsolationData (com.yahoo.pulsar.common.policies.data.NamespaceIsolationData)7 Test (org.testng.annotations.Test)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 MockedPulsarServiceBaseTest (com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)2 NamespaceIsolationPolicies (com.yahoo.pulsar.common.policies.impl.NamespaceIsolationPolicies)2 ZooKeeper (org.apache.zookeeper.ZooKeeper)2 ParameterException (com.beust.jcommander.ParameterException)1 RestException (com.yahoo.pulsar.broker.web.RestException)1 PulsarAdminException (com.yahoo.pulsar.client.admin.PulsarAdminException)1 NotFoundException (com.yahoo.pulsar.client.admin.PulsarAdminException.NotFoundException)1 PreconditionFailedException (com.yahoo.pulsar.client.admin.PulsarAdminException.PreconditionFailedException)1 NamespaceIsolationPolicy (com.yahoo.pulsar.common.policies.NamespaceIsolationPolicy)1 ClusterData (com.yahoo.pulsar.common.policies.data.ClusterData)1 OldPolicies (com.yahoo.pulsar.common.policies.data.OldPolicies)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1