Search in sources :

Example 1 with AutoFailoverPolicyData

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

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

the class AutoFailoverPolicyFactoryTest method testAutoFailoverPolicyFactory.

@Test
public void testAutoFailoverPolicyFactory() {
    try {
        AutoFailoverPolicyFactory.create(new AutoFailoverPolicyData());
        Assert.fail("");
    } catch (IllegalArgumentException e) {
    // Pass
    }
    try {
        AutoFailoverPolicyData afopd = new AutoFailoverPolicyData();
        afopd.policy_type = AutoFailoverPolicyType.min_available;
        afopd.parameters = new HashMap<>();
        afopd.parameters.put("min_limit", "3");
        afopd.parameters.put("usage_threshold", "10");
        AutoFailoverPolicyFactory.create(afopd);
    // Pass
    } catch (IllegalArgumentException e) {
        Assert.fail("Should not happen");
    }
}
Also used : AutoFailoverPolicyData(com.yahoo.pulsar.common.policies.data.AutoFailoverPolicyData) Test(org.testng.annotations.Test)

Example 3 with AutoFailoverPolicyData

use of com.yahoo.pulsar.common.policies.data.AutoFailoverPolicyData 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 4 with AutoFailoverPolicyData

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

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

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(com.yahoo.pulsar.common.policies.data.AutoFailoverPolicyData) NamespaceIsolationData(com.yahoo.pulsar.common.policies.data.NamespaceIsolationData) ParameterException(com.beust.jcommander.ParameterException)

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