Search in sources :

Example 1 with NamespaceIsolationPolicy

use of org.apache.pulsar.common.policies.NamespaceIsolationPolicy 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 2 with NamespaceIsolationPolicy

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

the class NamespaceIsolationPoliciesTest method testGetNamespaceIsolationPolicyByNamespace.

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

Example 3 with NamespaceIsolationPolicy

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

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 : NamespaceIsolationPolicies(org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies) NamespaceIsolationPolicyImpl(org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicyImpl) NamespaceIsolationPolicy(org.apache.pulsar.common.policies.NamespaceIsolationPolicy) Test(org.testng.annotations.Test)

Example 4 with NamespaceIsolationPolicy

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

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 != null && 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(org.apache.pulsar.common.policies.NamespaceIsolationPolicy) BrokerAssignment(org.apache.pulsar.common.policies.data.BrokerAssignment)

Aggregations

NamespaceIsolationPolicy (org.apache.pulsar.common.policies.NamespaceIsolationPolicy)4 NamespaceIsolationPolicies (org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies)3 Test (org.testng.annotations.Test)3 NamespaceIsolationPolicyImpl (org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicyImpl)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AutoFailoverPolicyData (org.apache.pulsar.common.policies.data.AutoFailoverPolicyData)1 BrokerAssignment (org.apache.pulsar.common.policies.data.BrokerAssignment)1 NamespaceIsolationData (org.apache.pulsar.common.policies.data.NamespaceIsolationData)1