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));
}
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);
}
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");
}
}
}
Aggregations