use of org.apache.pulsar.common.policies.data.FailureDomainImpl in project pulsar by apache.
the class ClustersBase method getFailureDomains.
@GET
@Path("/{cluster}/failureDomains")
@ApiOperation(value = "Get the cluster failure domains.", response = FailureDomainImpl.class, responseContainer = "Map", notes = "This operation requires Pulsar superuser privileges.")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 500, message = "Internal server error") })
public Map<String, FailureDomainImpl> getFailureDomains(@ApiParam(value = "The cluster name", required = true) @PathParam("cluster") String cluster) throws Exception {
validateSuperUserAccess();
Map<String, FailureDomainImpl> domains = Maps.newHashMap();
try {
FailureDomainResources fdr = clusterResources().getFailureDomainResources();
for (String domainName : fdr.listFailureDomains(cluster)) {
try {
Optional<FailureDomainImpl> domain = fdr.getFailureDomain(cluster, domainName);
domain.ifPresent(failureDomain -> domains.put(domainName, failureDomain));
} catch (Exception e) {
log.warn("Failed to get domain {}", domainName, e);
}
}
} catch (NotFoundException e) {
log.warn("[{}] Failure-domain is not configured for cluster {}", clientAppId(), cluster, e);
return Collections.emptyMap();
} catch (Exception e) {
log.error("[{}] Failed to get failure-domains for cluster {}", clientAppId(), cluster, e);
throw new RestException(e);
}
return domains;
}
use of org.apache.pulsar.common.policies.data.FailureDomainImpl in project pulsar by apache.
the class ClustersImpl method getFailureDomainsAsync.
@Override
public CompletableFuture<Map<String, FailureDomain>> getFailureDomainsAsync(String cluster) {
WebTarget path = adminClusters.path(cluster).path("failureDomains");
final CompletableFuture<Map<String, FailureDomain>> future = new CompletableFuture<>();
asyncGetRequest(path, new InvocationCallback<Map<String, FailureDomainImpl>>() {
@Override
public void completed(Map<String, FailureDomainImpl> failureDomains) {
Map<String, FailureDomain> result = new HashMap<>(failureDomains);
future.complete(result);
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
use of org.apache.pulsar.common.policies.data.FailureDomainImpl in project pulsar by yahoo.
the class ClustersBase method validateBrokerExistsInOtherDomain.
private CompletableFuture<Void> validateBrokerExistsInOtherDomain(final String cluster, final String inputDomainName, final FailureDomainImpl inputDomain) {
if (inputDomain == null || inputDomain.brokers == null) {
return CompletableFuture.completedFuture(null);
}
return clusterResources().getFailureDomainResources().listFailureDomainsAsync(cluster).thenCompose(domainNames -> {
List<CompletableFuture<Void>> futures = domainNames.stream().filter(domainName -> !domainName.equals(inputDomainName)).map(domainName -> clusterResources().getFailureDomainResources().getFailureDomainAsync(cluster, domainName).thenAccept(failureDomainOpt -> {
if (failureDomainOpt.isPresent() && CollectionUtils.isNotEmpty(failureDomainOpt.get().getBrokers())) {
List<String> duplicateBrokers = failureDomainOpt.get().getBrokers().stream().parallel().filter(inputDomain.brokers::contains).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(duplicateBrokers)) {
throw new RestException(Status.CONFLICT, duplicateBrokers + " already exists in " + domainName);
}
}
}).exceptionally(ex -> {
Throwable realCause = FutureUtil.unwrapCompletionException(ex);
if (realCause instanceof WebApplicationException) {
throw FutureUtil.wrapToCompletionException(ex);
}
if (realCause instanceof NotFoundException) {
if (log.isDebugEnabled()) {
log.debug("[{}] Domain is not configured for cluster", clientAppId(), ex);
}
return null;
}
log.warn("Failed to get domain {}", domainName, ex);
return null;
})).collect(Collectors.toList());
return FutureUtil.waitForAll(futures);
});
}
use of org.apache.pulsar.common.policies.data.FailureDomainImpl in project incubator-pulsar by apache.
the class ClustersImpl method getFailureDomainAsync.
@Override
public CompletableFuture<FailureDomain> getFailureDomainAsync(String cluster, String domainName) {
WebTarget path = adminClusters.path(cluster).path("failureDomains").path(domainName);
final CompletableFuture<FailureDomain> future = new CompletableFuture<>();
asyncGetRequest(path, new InvocationCallback<FailureDomainImpl>() {
@Override
public void completed(FailureDomainImpl failureDomain) {
future.complete(failureDomain);
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
use of org.apache.pulsar.common.policies.data.FailureDomainImpl in project incubator-pulsar by apache.
the class ClustersBase method setFailureDomain.
@POST
@Path("/{cluster}/failureDomains/{domainName}")
@ApiOperation(value = "Set the failure domain of the cluster.", notes = "This operation requires Pulsar superuser privileges.")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission."), @ApiResponse(code = 404, message = "Failure domain doesn't exist."), @ApiResponse(code = 409, message = "Broker already exists in another domain."), @ApiResponse(code = 412, message = "Cluster doesn't exist."), @ApiResponse(code = 500, message = "Internal server error.") })
public void setFailureDomain(@Suspended AsyncResponse asyncResponse, @ApiParam(value = "The cluster name", required = true) @PathParam("cluster") String cluster, @ApiParam(value = "The failure domain name", required = true) @PathParam("domainName") String domainName, @ApiParam(value = "The configuration data of a failure domain", required = true) FailureDomainImpl domain) {
validateSuperUserAccessAsync().thenCompose(__ -> validateClusterExistAsync(cluster, PRECONDITION_FAILED)).thenCompose(__ -> validateBrokerExistsInOtherDomain(cluster, domainName, domain)).thenCompose(__ -> clusterResources().getFailureDomainResources().setFailureDomainWithCreateAsync(cluster, domainName, old -> domain)).thenAccept(__ -> {
log.info("[{}] Successful set failure domain {} for cluster {}", clientAppId(), domainName, cluster);
asyncResponse.resume(Response.noContent().build());
}).exceptionally(ex -> {
Throwable realCause = FutureUtil.unwrapCompletionException(ex);
if (realCause instanceof NotFoundException) {
log.warn("[{}] Failed to update domain {}. clusters {} Does not exist", clientAppId(), cluster, domainName);
asyncResponse.resume(new RestException(Status.NOT_FOUND, "Domain " + domainName + " for cluster " + cluster + " does not exist"));
return null;
}
log.error("[{}] Failed to update clusters/{}/domainName/{}", clientAppId(), cluster, domainName, ex);
resumeAsyncResponseExceptionally(asyncResponse, ex);
return null;
});
}
Aggregations