Search in sources :

Example 1 with NamespaceIsolationPolicy

use of com.yahoo.pulsar.common.policies.NamespaceIsolationPolicy in project pulsar by yahoo.

the class NamespaceIsolationPoliciesTest method testGetNamespaceIsolationPolicyByName.

@Test
public void testGetNamespaceIsolationPolicyByName() throws Exception {
    NamespaceIsolationPolicies policies = this.getDefaultTestPolicies();
    NamespaceIsolationPolicy nsPolicy = policies.getPolicyByName("non-existing-policy");
    assertTrue(nsPolicy == null);
    nsPolicy = policies.getPolicyByName("policy1");
    assertNotNull(nsPolicy);
    assertEquals(new NamespaceIsolationPolicyImpl(policies.getPolicies().get("policy1")), nsPolicy);
}
Also used : NamespaceIsolationPolicy(com.yahoo.pulsar.common.policies.NamespaceIsolationPolicy) Test(org.testng.annotations.Test)

Example 2 with NamespaceIsolationPolicy

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

use of com.yahoo.pulsar.common.policies.NamespaceIsolationPolicy in project pulsar by yahoo.

the class SimpleResourceAllocationPolicies method getNamespaceIsolationPolicy.

private NamespaceIsolationPolicy getNamespaceIsolationPolicy(NamespaceName namespace) {
    NamespaceIsolationPolicies policies = null;
    NamespaceIsolationPolicy policy = null;
    try {
        policies = getIsolationPolicies(pulsar.getConfiguration().getClusterName());
        policy = policies.getPolicyByNamespace(namespace);
    } catch (Exception e) {
        LOG.warn("Unable to get the namespaceIsolationPolicies [{}]", e);
    }
    return policy;
}
Also used : NamespaceIsolationPolicies(com.yahoo.pulsar.common.policies.impl.NamespaceIsolationPolicies) NamespaceIsolationPolicy(com.yahoo.pulsar.common.policies.NamespaceIsolationPolicy)

Example 4 with NamespaceIsolationPolicy

use of com.yahoo.pulsar.common.policies.NamespaceIsolationPolicy in project pulsar by yahoo.

the class NamespaceIsolationPoliciesTest method testGetNamespaceIsolationPolicyByNamespace.

@Test
public void testGetNamespaceIsolationPolicyByNamespace() throws Exception {
    NamespaceIsolationPolicies policies = this.getDefaultTestPolicies();
    NamespaceIsolationPolicy nsPolicy = policies.getPolicyByNamespace(new NamespaceName("no/such/namespace"));
    assertTrue(nsPolicy == null);
    nsPolicy = policies.getPolicyByNamespace(new NamespaceName("pulsar/use/testns-1"));
    assertNotNull(nsPolicy);
    assertEquals(new NamespaceIsolationPolicyImpl(policies.getPolicies().get("policy1")), nsPolicy);
}
Also used : NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) NamespaceIsolationPolicy(com.yahoo.pulsar.common.policies.NamespaceIsolationPolicy) Test(org.testng.annotations.Test)

Example 5 with NamespaceIsolationPolicy

use of com.yahoo.pulsar.common.policies.NamespaceIsolationPolicy in project pulsar by yahoo.

the class NamespaceIsolationPolicies method assignBroker.

public void assignBroker(NamespaceName nsname, BrokerStatus brkStatus, SortedSet<BrokerStatus> primaryCandidates, SortedSet<BrokerStatus> secondaryCandidates, SortedSet<BrokerStatus> sharedCandidates) {
    NamespaceIsolationPolicy nsPolicy = this.getPolicyByNamespace(nsname);
    BrokerAssignment brokerAssignment = this.getBrokerAssignment(nsPolicy, brkStatus.getBrokerAddress());
    if (brokerAssignment == BrokerAssignment.primary) {
        // Only add to candidates if allowed by policy
        if (nsPolicy.isPrimaryBrokerAvailable(brkStatus)) {
            primaryCandidates.add(brkStatus);
        }
    } else if (brokerAssignment == BrokerAssignment.secondary) {
        secondaryCandidates.add(brkStatus);
    } else if (brokerAssignment == BrokerAssignment.shared) {
        sharedCandidates.add(brkStatus);
    }
}
Also used : NamespaceIsolationPolicy(com.yahoo.pulsar.common.policies.NamespaceIsolationPolicy) BrokerAssignment(com.yahoo.pulsar.common.policies.data.BrokerAssignment)

Aggregations

NamespaceIsolationPolicy (com.yahoo.pulsar.common.policies.NamespaceIsolationPolicy)5 Test (org.testng.annotations.Test)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 NamespaceName (com.yahoo.pulsar.common.naming.NamespaceName)1 AutoFailoverPolicyData (com.yahoo.pulsar.common.policies.data.AutoFailoverPolicyData)1 BrokerAssignment (com.yahoo.pulsar.common.policies.data.BrokerAssignment)1 NamespaceIsolationData (com.yahoo.pulsar.common.policies.data.NamespaceIsolationData)1 NamespaceIsolationPolicies (com.yahoo.pulsar.common.policies.impl.NamespaceIsolationPolicies)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1