Search in sources :

Example 6 with NamespaceIsolationPolicyImpl

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

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

Example 7 with NamespaceIsolationPolicyImpl

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

the class NamespaceIsolationPolicyImplTest method testShouldFailover.

@Test
public void testShouldFailover() throws Exception {
    NamespaceIsolationPolicyImpl defaultPolicy = this.getDefaultPolicy();
    SortedSet<BrokerStatus> brokerStatus = new TreeSet<BrokerStatus>();
    for (int i = 0; i < 10; i++) {
        BrokerStatus status = new BrokerStatus(String.format("broker-%d", i), true, i * 10);
        brokerStatus.add(status);
    }
    assertEquals(defaultPolicy.shouldFailover(brokerStatus), false);
    List<BrokerStatus> objList = new ArrayList<BrokerStatus>();
    objList.addAll(brokerStatus);
    for (int i = 0; i < 8; i++) {
        objList.get(i).setActive(false);
    }
    assertEquals(defaultPolicy.shouldFailover(brokerStatus), true);
    objList.get(7).setActive(true);
    assertEquals(defaultPolicy.shouldFailover(brokerStatus), true);
    objList.get(9).setLoadFactor(80);
    assertEquals(defaultPolicy.shouldFailover(brokerStatus), false);
    brokerStatus = new TreeSet<BrokerStatus>();
    for (int i = 0; i < 5; i++) {
        BrokerStatus status = new BrokerStatus(String.format("broker-%d", 2 * i), true, i * 20);
        brokerStatus.add(status);
        status = new BrokerStatus(String.format("broker-%d", 2 * i + 1), true, i * 20);
        brokerStatus.add(status);
    }
    assertEquals(brokerStatus.size(), 10);
}
Also used : NamespaceIsolationPolicyImpl(org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicyImpl) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) BrokerStatus(org.apache.pulsar.common.policies.data.BrokerStatus) Test(org.testng.annotations.Test)

Example 8 with NamespaceIsolationPolicyImpl

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

the class NamespaceIsolationPolicyImplTest method testGetAvailablePrimaryBrokers.

@Test
public void testGetAvailablePrimaryBrokers() throws Exception {
    NamespaceIsolationPolicyImpl defaultPolicy = this.getDefaultPolicy();
    SortedSet<BrokerStatus> brokerStatus = new TreeSet<BrokerStatus>();
    SortedSet<BrokerStatus> expectedAvailablePrimaries = new TreeSet<BrokerStatus>();
    for (int i = 0; i < 10; i++) {
        BrokerStatus status = new BrokerStatus(String.format("prod1-broker%d.messaging.use.example.com", i), i % 2 == 0, i * 10);
        brokerStatus.add(status);
        if (i % 2 == 0) {
            expectedAvailablePrimaries.add(status);
        }
    }
    SortedSet<BrokerStatus> availablePrimaries = defaultPolicy.getAvailablePrimaryBrokers(brokerStatus);
    assertEquals(expectedAvailablePrimaries.size(), availablePrimaries.size());
    for (BrokerStatus bs : availablePrimaries) {
        if (!expectedAvailablePrimaries.contains(bs)) {
            fail("Should not happen");
        }
    }
}
Also used : NamespaceIsolationPolicyImpl(org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicyImpl) TreeSet(java.util.TreeSet) BrokerStatus(org.apache.pulsar.common.policies.data.BrokerStatus) Test(org.testng.annotations.Test)

Aggregations

NamespaceIsolationPolicyImpl (org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicyImpl)8 Test (org.testng.annotations.Test)8 NamespaceIsolationPolicies (org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies)3 ArrayList (java.util.ArrayList)2 TreeSet (java.util.TreeSet)2 NamespaceIsolationPolicy (org.apache.pulsar.common.policies.NamespaceIsolationPolicy)2 BrokerStatus (org.apache.pulsar.common.policies.data.BrokerStatus)2 NamespaceIsolationData (org.apache.pulsar.common.policies.data.NamespaceIsolationData)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 URL (java.net.URL)1 AutoFailoverPolicyData (org.apache.pulsar.common.policies.data.AutoFailoverPolicyData)1 OldPolicies (org.apache.pulsar.common.policies.data.OldPolicies)1