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