Search in sources :

Example 1 with NamespaceIsolationData

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

the class AdminTest method clusters.

@Test
void clusters() throws Exception {
    assertEquals(clusters.getClusters(), new ArrayList<String>());
    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(com.yahoo.pulsar.common.policies.data.ClusterData) AutoFailoverPolicyData(com.yahoo.pulsar.common.policies.data.AutoFailoverPolicyData) NamespaceIsolationData(com.yahoo.pulsar.common.policies.data.NamespaceIsolationData) RestException(com.yahoo.pulsar.broker.web.RestException) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 2 with NamespaceIsolationData

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

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(com.yahoo.pulsar.common.policies.NamespaceIsolationPolicy) AutoFailoverPolicyData(com.yahoo.pulsar.common.policies.data.AutoFailoverPolicyData) NamespaceIsolationData(com.yahoo.pulsar.common.policies.data.NamespaceIsolationData) HashMap(java.util.HashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Example 3 with NamespaceIsolationData

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

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(new NamespaceName("pulsar/use/TESTNS.1")), new NamespaceIsolationPolicyImpl(nsPolicyData));
}
Also used : NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) NamespaceIsolationData(com.yahoo.pulsar.common.policies.data.NamespaceIsolationData) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Example 4 with NamespaceIsolationData

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

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 : AutoFailoverPolicyData(com.yahoo.pulsar.common.policies.data.AutoFailoverPolicyData) NamespaceIsolationData(com.yahoo.pulsar.common.policies.data.NamespaceIsolationData) OldPolicies(com.yahoo.pulsar.common.policies.data.OldPolicies) Test(org.testng.annotations.Test)

Example 5 with NamespaceIsolationData

use of com.yahoo.pulsar.common.policies.data.NamespaceIsolationData 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)

Aggregations

NamespaceIsolationData (com.yahoo.pulsar.common.policies.data.NamespaceIsolationData)8 AutoFailoverPolicyData (com.yahoo.pulsar.common.policies.data.AutoFailoverPolicyData)7 Test (org.testng.annotations.Test)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 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 NamespaceName (com.yahoo.pulsar.common.naming.NamespaceName)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