use of org.apache.pulsar.common.policies.data.BrokerNamespaceIsolationData in project pulsar by apache.
the class ClustersBase method getBrokersWithNamespaceIsolationPolicy.
@GET
@Path("/{cluster}/namespaceIsolationPolicies/brokers")
@ApiOperation(value = "Get list of brokers with namespace-isolation policies attached to them.", response = BrokerNamespaceIsolationDataImpl.class, responseContainer = "set", notes = "This operation requires Pulsar superuser privileges.")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission."), @ApiResponse(code = 404, message = "Namespace-isolation policies not found."), @ApiResponse(code = 412, message = "Cluster doesn't exist."), @ApiResponse(code = 500, message = "Internal server error.") })
public List<BrokerNamespaceIsolationData> getBrokersWithNamespaceIsolationPolicy(@ApiParam(value = "The cluster name", required = true) @PathParam("cluster") String cluster) {
validateSuperUserAccess();
validateClusterExists(cluster);
Set<String> availableBrokers;
Map<String, ? extends NamespaceIsolationData> nsPolicies;
try {
availableBrokers = pulsar().getLoadManager().get().getAvailableBrokers();
} catch (Exception e) {
log.error("[{}] Failed to get list of brokers in cluster {}", clientAppId(), cluster, e);
throw new RestException(e);
}
try {
Optional<NamespaceIsolationPolicies> nsPoliciesResult = namespaceIsolationPolicies().getIsolationDataPolicies(cluster);
if (!nsPoliciesResult.isPresent()) {
throw new RestException(Status.NOT_FOUND, "namespace-isolation policies not found for " + cluster);
}
nsPolicies = nsPoliciesResult.get().getPolicies();
} catch (Exception e) {
log.error("[{}] Failed to get namespace isolation-policies {}", clientAppId(), cluster, e);
throw new RestException(e);
}
return availableBrokers.stream().map(broker -> {
BrokerNamespaceIsolationData.Builder brokerIsolationData = BrokerNamespaceIsolationData.builder().brokerName(broker);
if (nsPolicies != null) {
List<String> namespaceRegexes = new ArrayList<>();
nsPolicies.forEach((name, policyData) -> {
NamespaceIsolationPolicyImpl nsPolicyImpl = new NamespaceIsolationPolicyImpl(policyData);
if (nsPolicyImpl.isPrimaryBroker(broker) || nsPolicyImpl.isSecondaryBroker(broker)) {
namespaceRegexes.addAll(policyData.getNamespaces());
if (nsPolicyImpl.isPrimaryBroker(broker)) {
brokerIsolationData.primary(true);
}
}
});
brokerIsolationData.namespaceRegex(namespaceRegexes);
}
return brokerIsolationData.build();
}).collect(Collectors.toList());
}
use of org.apache.pulsar.common.policies.data.BrokerNamespaceIsolationData in project pulsar by apache.
the class V1_AdminApiTest method clusterNamespaceIsolationPolicies.
@Test
public void clusterNamespaceIsolationPolicies() throws PulsarAdminException {
try {
// create
String policyName1 = "policy-1";
Map<String, String> parameters1 = new HashMap<>();
parameters1.put("min_limit", "1");
parameters1.put("usage_threshold", "100");
NamespaceIsolationData nsPolicyData1 = NamespaceIsolationData.builder().namespaces(Lists.newArrayList("other/use/other.*")).primary(// match all broker. make it easy to verify `getBrokersWithNamespaceIsolationPolicy` later
Lists.newArrayList(".*")).secondary(Lists.newArrayList("prod1-broker.*.messaging.use.example.com")).autoFailoverPolicy(AutoFailoverPolicyData.builder().policyType(AutoFailoverPolicyType.min_available).parameters(parameters1).build()).build();
admin.clusters().createNamespaceIsolationPolicy("use", policyName1, nsPolicyData1);
String policyName2 = "policy-2";
Map<String, String> parameters2 = new HashMap<>();
parameters2.put("min_limit", "1");
parameters2.put("usage_threshold", "100");
NamespaceIsolationData nsPolicyData2 = NamespaceIsolationData.builder().namespaces(Lists.newArrayList("other/use/other.*")).primary(Lists.newArrayList("prod1-broker[4-6].messaging.use.example.com")).secondary(Lists.newArrayList("prod1-broker.*.messaging.use.example.com")).autoFailoverPolicy(AutoFailoverPolicyData.builder().policyType(AutoFailoverPolicyType.min_available).parameters(parameters1).build()).build();
admin.clusters().createNamespaceIsolationPolicy("use", policyName2, nsPolicyData2);
// verify create indirectly with get
Map<String, ? extends NamespaceIsolationData> policiesMap = admin.clusters().getNamespaceIsolationPolicies("use");
assertEquals(policiesMap.get(policyName1), nsPolicyData1);
assertEquals(policiesMap.get(policyName2), nsPolicyData2);
// verify local broker get matched.
List<BrokerNamespaceIsolationData> isoList = admin.clusters().getBrokersWithNamespaceIsolationPolicy("use");
assertEquals(isoList.size(), 1);
assertTrue(isoList.get(0).isPrimary());
// verify update of primary
nsPolicyData1.getPrimary().remove(0);
nsPolicyData1.getPrimary().add("prod1-broker[1-2].messaging.use.example.com");
admin.clusters().updateNamespaceIsolationPolicy("use", policyName1, nsPolicyData1);
// verify primary change
policiesMap = admin.clusters().getNamespaceIsolationPolicies("use");
assertEquals(policiesMap.get(policyName1), nsPolicyData1);
// verify update of secondary
nsPolicyData1.getSecondary().remove(0);
nsPolicyData1.getSecondary().add("prod1-broker[3-4].messaging.use.example.com");
admin.clusters().updateNamespaceIsolationPolicy("use", policyName1, nsPolicyData1);
// verify secondary change
policiesMap = admin.clusters().getNamespaceIsolationPolicies("use");
assertEquals(policiesMap.get(policyName1), nsPolicyData1);
// verify update of failover policy limit
nsPolicyData1.getAutoFailoverPolicy().getParameters().put("min_limit", "10");
admin.clusters().updateNamespaceIsolationPolicy("use", policyName1, nsPolicyData1);
// verify min_limit change
policiesMap = admin.clusters().getNamespaceIsolationPolicies("use");
assertEquals(policiesMap.get(policyName1), nsPolicyData1);
// verify update of failover usage_threshold limit
nsPolicyData1.getAutoFailoverPolicy().getParameters().put("usage_threshold", "80");
admin.clusters().updateNamespaceIsolationPolicy("use", policyName1, nsPolicyData1);
// verify usage_threshold change
policiesMap = admin.clusters().getNamespaceIsolationPolicies("use");
assertEquals(policiesMap.get(policyName1), nsPolicyData1);
// verify single get
NamespaceIsolationDataImpl policy1Data = (NamespaceIsolationDataImpl) admin.clusters().getNamespaceIsolationPolicy("use", policyName1);
assertEquals(policy1Data, nsPolicyData1);
// verify creation of more than one policy
admin.clusters().createNamespaceIsolationPolicy("use", policyName2, nsPolicyData1);
try {
admin.clusters().getNamespaceIsolationPolicy("use", "no-such-policy");
fail("should have raised exception");
} catch (PulsarAdminException e) {
assertTrue(e instanceof NotFoundException);
}
// verify delete cluster failed
try {
admin.clusters().deleteCluster("use");
fail("should have raised exception");
} catch (PulsarAdminException e) {
assertTrue(e instanceof PreconditionFailedException);
}
// verify delete
admin.clusters().deleteNamespaceIsolationPolicy("use", policyName1);
admin.clusters().deleteNamespaceIsolationPolicy("use", policyName2);
try {
admin.clusters().getNamespaceIsolationPolicy("use", policyName1);
fail("should have raised exception");
} catch (PulsarAdminException e) {
assertTrue(e instanceof NotFoundException);
}
try {
admin.clusters().getNamespaceIsolationPolicy("use", policyName2);
fail("should have raised exception");
} catch (PulsarAdminException e) {
assertTrue(e instanceof NotFoundException);
}
try {
admin.clusters().getNamespaceIsolationPolicies("usc");
fail("should have raised exception");
} catch (PulsarAdminException e) {
assertTrue(e instanceof NotFoundException);
}
try {
admin.clusters().getNamespaceIsolationPolicy("usc", "no-such-cluster");
fail("should have raised exception");
} catch (PulsarAdminException e) {
assertTrue(e instanceof PreconditionFailedException);
}
try {
admin.clusters().createNamespaceIsolationPolicy("usc", "no-such-cluster", nsPolicyData1);
fail("should have raised exception");
} catch (PulsarAdminException e) {
assertTrue(e instanceof PreconditionFailedException);
}
try {
admin.clusters().updateNamespaceIsolationPolicy("usc", "no-such-cluster", policy1Data);
fail("should have raised exception");
} catch (PulsarAdminException e) {
assertTrue(e instanceof PreconditionFailedException);
}
} catch (PulsarAdminException e) {
LOG.warn("TEST FAILED [{}]", e.getMessage());
throw e;
}
}
use of org.apache.pulsar.common.policies.data.BrokerNamespaceIsolationData in project pulsar by apache.
the class ClustersImpl method getBrokerWithNamespaceIsolationPolicyAsync.
@Override
public CompletableFuture<BrokerNamespaceIsolationData> getBrokerWithNamespaceIsolationPolicyAsync(String cluster, String broker) {
WebTarget path = adminClusters.path(cluster).path("namespaceIsolationPolicies").path("brokers").path(broker);
final CompletableFuture<BrokerNamespaceIsolationData> future = new CompletableFuture<>();
asyncGetRequest(path, new InvocationCallback<BrokerNamespaceIsolationDataImpl>() {
@Override
public void completed(BrokerNamespaceIsolationDataImpl brokerNamespaceIsolationData) {
future.complete(brokerNamespaceIsolationData);
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
use of org.apache.pulsar.common.policies.data.BrokerNamespaceIsolationData in project pulsar by yahoo.
the class ClustersImpl method getBrokerWithNamespaceIsolationPolicyAsync.
@Override
public CompletableFuture<BrokerNamespaceIsolationData> getBrokerWithNamespaceIsolationPolicyAsync(String cluster, String broker) {
WebTarget path = adminClusters.path(cluster).path("namespaceIsolationPolicies").path("brokers").path(broker);
final CompletableFuture<BrokerNamespaceIsolationData> future = new CompletableFuture<>();
asyncGetRequest(path, new InvocationCallback<BrokerNamespaceIsolationDataImpl>() {
@Override
public void completed(BrokerNamespaceIsolationDataImpl brokerNamespaceIsolationData) {
future.complete(brokerNamespaceIsolationData);
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
use of org.apache.pulsar.common.policies.data.BrokerNamespaceIsolationData in project incubator-pulsar by apache.
the class AdminApi2Test method brokerNamespaceIsolationPolicies.
@Test
public void brokerNamespaceIsolationPolicies() throws Exception {
// create
String policyName1 = "policy-1";
String cluster = pulsar.getConfiguration().getClusterName();
String namespaceRegex = "other/" + cluster + "/other.*";
String brokerName = pulsar.getAdvertisedAddress();
String brokerAddress = brokerName + ":" + pulsar.getConfiguration().getWebServicePort().get();
Map<String, String> parameters1 = new HashMap<>();
parameters1.put("min_limit", "1");
parameters1.put("usage_threshold", "100");
NamespaceIsolationData nsPolicyData1 = NamespaceIsolationData.builder().namespaces(Collections.singletonList(namespaceRegex)).primary(Collections.singletonList(brokerName + ":[0-9]*")).secondary(Collections.singletonList(brokerName + ".*")).autoFailoverPolicy(AutoFailoverPolicyData.builder().policyType(AutoFailoverPolicyType.min_available).parameters(parameters1).build()).build();
admin.clusters().createNamespaceIsolationPolicy(cluster, policyName1, nsPolicyData1);
List<BrokerNamespaceIsolationData> brokerIsolationDataList = admin.clusters().getBrokersWithNamespaceIsolationPolicy(cluster);
assertEquals(brokerIsolationDataList.size(), 1);
assertEquals(brokerIsolationDataList.get(0).getBrokerName(), brokerAddress);
assertEquals(brokerIsolationDataList.get(0).getNamespaceRegex().size(), 1);
assertEquals(brokerIsolationDataList.get(0).getNamespaceRegex().get(0), namespaceRegex);
assertEquals(brokerIsolationDataList.get(0).getPolicyName(), policyName1);
BrokerNamespaceIsolationDataImpl brokerIsolationData = (BrokerNamespaceIsolationDataImpl) admin.clusters().getBrokerWithNamespaceIsolationPolicy(cluster, brokerAddress);
assertEquals(brokerIsolationData.getBrokerName(), brokerAddress);
assertEquals(brokerIsolationData.getNamespaceRegex().size(), 1);
assertEquals(brokerIsolationData.getNamespaceRegex().get(0), namespaceRegex);
BrokerNamespaceIsolationDataImpl isolationData = (BrokerNamespaceIsolationDataImpl) admin.clusters().getBrokerWithNamespaceIsolationPolicy(cluster, "invalid-broker");
assertFalse(isolationData.isPrimary());
}
Aggregations