use of org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies in project incubator-pulsar by apache.
the class ClustersBase method setNamespaceIsolationPolicy.
@POST
@Path("/{cluster}/namespaceIsolationPolicies/{policyName}")
@ApiOperation(value = "Set namespace isolation policy")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission or plicy is read only"), @ApiResponse(code = 412, message = "Cluster doesn't exist") })
public void setNamespaceIsolationPolicy(@PathParam("cluster") String cluster, @PathParam("policyName") String policyName, NamespaceIsolationData policyData) throws Exception {
validateSuperUserAccess();
validateClusterExists(cluster);
validatePoliciesReadOnlyAccess();
try {
// validate the policy data before creating the node
policyData.validate();
String nsIsolationPolicyPath = path("clusters", cluster, "namespaceIsolationPolicies");
NamespaceIsolationPolicies nsIsolationPolicies = namespaceIsolationPoliciesCache().get(nsIsolationPolicyPath).orElseGet(() -> {
try {
this.createZnodeIfNotExist(nsIsolationPolicyPath, Optional.of(Collections.emptyMap()));
return new NamespaceIsolationPolicies();
} catch (KeeperException | InterruptedException e) {
throw new RestException(e);
}
});
nsIsolationPolicies.setPolicy(policyName, policyData);
globalZk().setData(nsIsolationPolicyPath, jsonMapper().writeValueAsBytes(nsIsolationPolicies.getPolicies()), -1);
// make sure that the cache content will be refreshed for the next read access
namespaceIsolationPoliciesCache().invalidate(nsIsolationPolicyPath);
} catch (IllegalArgumentException iae) {
log.info("[{}] Failed to update clusters/{}/namespaceIsolationPolicies/{}. Input data is invalid", clientAppId(), cluster, policyName, iae);
String jsonInput = ObjectMapperFactory.create().writeValueAsString(policyData);
throw new RestException(Status.BAD_REQUEST, "Invalid format of input policy data. policy: " + policyName + "; data: " + jsonInput);
} catch (KeeperException.NoNodeException nne) {
log.warn("[{}] Failed to update clusters/{}/namespaceIsolationPolicies: Does not exist", clientAppId(), cluster);
throw new RestException(Status.NOT_FOUND, "NamespaceIsolationPolicies for cluster " + cluster + " does not exist");
} catch (Exception e) {
log.error("[{}] Failed to update clusters/{}/namespaceIsolationPolicies/{}", clientAppId(), cluster, policyName, e);
throw new RestException(e);
}
}
use of org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies in project incubator-pulsar by apache.
the class NamespaceIsolationPoliciesTest method testDeletePolicy.
@Test
public void testDeletePolicy() throws Exception {
NamespaceIsolationPolicies policies = this.getDefaultTestPolicies();
policies.deletePolicy("non-existing-policy");
assertTrue(!policies.getPolicies().isEmpty());
policies.deletePolicy("policy1");
assertTrue(policies.getPolicies().isEmpty());
}
use of org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies 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);
}
use of org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies in project incubator-pulsar by apache.
the class NamespaceIsolationPoliciesTest method testBrokerAssignment.
@Test
public void testBrokerAssignment() throws Exception {
NamespaceIsolationPolicies policies = this.getDefaultTestPolicies();
NamespaceName ns = NamespaceName.get("pulsar/use/testns-1");
SortedSet<BrokerStatus> primaryCandidates = new TreeSet<>();
BrokerStatus primary = new BrokerStatus("prod1-broker1.messaging.use.example.com", true, 0);
BrokerStatus secondary = new BrokerStatus("prod1-broker4.use.example.com", true, 0);
BrokerStatus shared = new BrokerStatus("use.example.com", true, 0);
SortedSet<BrokerStatus> secondaryCandidates = new TreeSet<>();
SortedSet<BrokerStatus> sharedCandidates = new TreeSet<>();
policies.assignBroker(ns, primary, primaryCandidates, secondaryCandidates, sharedCandidates);
assertEquals(primaryCandidates.size(), 1);
assertEquals(secondaryCandidates.size(), 0);
assertEquals(sharedCandidates.size(), 0);
assertTrue(primaryCandidates.first().equals(primary));
policies.assignBroker(ns, secondary, primaryCandidates, secondaryCandidates, sharedCandidates);
assertEquals(primaryCandidates.size(), 1);
assertEquals(secondaryCandidates.size(), 1);
assertEquals(sharedCandidates.size(), 0);
assertTrue(secondaryCandidates.first().equals(secondary));
policies.assignBroker(NamespaceName.get("pulsar/use1/testns-1"), shared, primaryCandidates, secondaryCandidates, sharedCandidates);
assertEquals(primaryCandidates.size(), 1);
assertEquals(secondaryCandidates.size(), 1);
assertEquals(sharedCandidates.size(), 1);
assertTrue(sharedCandidates.first().equals(shared));
}
use of org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies 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);
}
Aggregations