Search in sources :

Example 1 with FailureDomainImpl

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;
}
Also used : FailureDomainImpl(org.apache.pulsar.common.policies.data.FailureDomainImpl) RestException(org.apache.pulsar.broker.web.RestException) NotFoundException(org.apache.pulsar.metadata.api.MetadataStoreException.NotFoundException) FailureDomainResources(org.apache.pulsar.broker.resources.ClusterResources.FailureDomainResources) RestException(org.apache.pulsar.broker.web.RestException) NotFoundException(org.apache.pulsar.metadata.api.MetadataStoreException.NotFoundException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with FailureDomainImpl

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;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) FailureDomainImpl(org.apache.pulsar.common.policies.data.FailureDomainImpl) WebTarget(javax.ws.rs.client.WebTarget) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with FailureDomainImpl

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);
    });
}
Also used : Constants(org.apache.pulsar.common.naming.Constants) ClusterDataImpl(org.apache.pulsar.common.policies.data.ClusterDataImpl) JsonUtil(org.apache.bookkeeper.common.util.JsonUtil) AdminResource(org.apache.pulsar.broker.admin.AdminResource) Path(javax.ws.rs.Path) LoggerFactory(org.slf4j.LoggerFactory) ApiParam(io.swagger.annotations.ApiParam) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) ApiOperation(io.swagger.annotations.ApiOperation) NamespaceIsolationPolicies(org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies) MediaType(javax.ws.rs.core.MediaType) Pair(org.apache.commons.lang3.tuple.Pair) Example(io.swagger.annotations.Example) Map(java.util.Map) RestException(org.apache.pulsar.broker.web.RestException) NamedEntity(org.apache.pulsar.common.naming.NamedEntity) DELETE(javax.ws.rs.DELETE) BrokerNamespaceIsolationDataImpl(org.apache.pulsar.common.policies.data.BrokerNamespaceIsolationDataImpl) MetadataStoreException(org.apache.pulsar.metadata.api.MetadataStoreException) AsyncResponse(javax.ws.rs.container.AsyncResponse) Collectors(java.util.stream.Collectors) Suspended(javax.ws.rs.container.Suspended) Objects(java.util.Objects) List(java.util.List) FutureUtil(org.apache.pulsar.common.util.FutureUtil) Stream(java.util.stream.Stream) Response(javax.ws.rs.core.Response) NotFoundException(org.apache.pulsar.metadata.api.MetadataStoreException.NotFoundException) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) PathParam(javax.ws.rs.PathParam) NamespaceIsolationDataImpl(org.apache.pulsar.common.policies.data.NamespaceIsolationDataImpl) GET(javax.ws.rs.GET) CompletableFuture(java.util.concurrent.CompletableFuture) PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) ApiResponses(io.swagger.annotations.ApiResponses) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) NamespaceIsolationPolicyImpl(org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicyImpl) PRECONDITION_FAILED(javax.ws.rs.core.Response.Status.PRECONDITION_FAILED) Status(javax.ws.rs.core.Response.Status) LinkedHashSet(java.util.LinkedHashSet) BrokerNamespaceIsolationData(org.apache.pulsar.common.policies.data.BrokerNamespaceIsolationData) POST(javax.ws.rs.POST) Logger(org.slf4j.Logger) ApiResponse(io.swagger.annotations.ApiResponse) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) ExampleProperty(io.swagger.annotations.ExampleProperty) PUT(javax.ws.rs.PUT) FailureDomainImpl(org.apache.pulsar.common.policies.data.FailureDomainImpl) Collections(java.util.Collections) CompletableFuture(java.util.concurrent.CompletableFuture) WebApplicationException(javax.ws.rs.WebApplicationException) RestException(org.apache.pulsar.broker.web.RestException) NotFoundException(org.apache.pulsar.metadata.api.MetadataStoreException.NotFoundException) List(java.util.List) ArrayList(java.util.ArrayList)

Example 4 with FailureDomainImpl

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;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) FailureDomainImpl(org.apache.pulsar.common.policies.data.FailureDomainImpl) FailureDomain(org.apache.pulsar.common.policies.data.FailureDomain) WebTarget(javax.ws.rs.client.WebTarget)

Example 5 with FailureDomainImpl

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;
    });
}
Also used : Constants(org.apache.pulsar.common.naming.Constants) ClusterDataImpl(org.apache.pulsar.common.policies.data.ClusterDataImpl) JsonUtil(org.apache.bookkeeper.common.util.JsonUtil) AdminResource(org.apache.pulsar.broker.admin.AdminResource) Path(javax.ws.rs.Path) LoggerFactory(org.slf4j.LoggerFactory) ApiParam(io.swagger.annotations.ApiParam) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) ApiOperation(io.swagger.annotations.ApiOperation) NamespaceIsolationPolicies(org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies) MediaType(javax.ws.rs.core.MediaType) Pair(org.apache.commons.lang3.tuple.Pair) Example(io.swagger.annotations.Example) Map(java.util.Map) RestException(org.apache.pulsar.broker.web.RestException) NamedEntity(org.apache.pulsar.common.naming.NamedEntity) DELETE(javax.ws.rs.DELETE) BrokerNamespaceIsolationDataImpl(org.apache.pulsar.common.policies.data.BrokerNamespaceIsolationDataImpl) MetadataStoreException(org.apache.pulsar.metadata.api.MetadataStoreException) AsyncResponse(javax.ws.rs.container.AsyncResponse) Collectors(java.util.stream.Collectors) Suspended(javax.ws.rs.container.Suspended) Objects(java.util.Objects) List(java.util.List) FutureUtil(org.apache.pulsar.common.util.FutureUtil) Stream(java.util.stream.Stream) Response(javax.ws.rs.core.Response) NotFoundException(org.apache.pulsar.metadata.api.MetadataStoreException.NotFoundException) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) PathParam(javax.ws.rs.PathParam) NamespaceIsolationDataImpl(org.apache.pulsar.common.policies.data.NamespaceIsolationDataImpl) GET(javax.ws.rs.GET) CompletableFuture(java.util.concurrent.CompletableFuture) PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) ApiResponses(io.swagger.annotations.ApiResponses) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) NamespaceIsolationPolicyImpl(org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicyImpl) PRECONDITION_FAILED(javax.ws.rs.core.Response.Status.PRECONDITION_FAILED) Status(javax.ws.rs.core.Response.Status) LinkedHashSet(java.util.LinkedHashSet) BrokerNamespaceIsolationData(org.apache.pulsar.common.policies.data.BrokerNamespaceIsolationData) POST(javax.ws.rs.POST) Logger(org.slf4j.Logger) ApiResponse(io.swagger.annotations.ApiResponse) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) ExampleProperty(io.swagger.annotations.ExampleProperty) PUT(javax.ws.rs.PUT) FailureDomainImpl(org.apache.pulsar.common.policies.data.FailureDomainImpl) Collections(java.util.Collections) RestException(org.apache.pulsar.broker.web.RestException) NotFoundException(org.apache.pulsar.metadata.api.MetadataStoreException.NotFoundException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

FailureDomainImpl (org.apache.pulsar.common.policies.data.FailureDomainImpl)11 CompletableFuture (java.util.concurrent.CompletableFuture)10 Map (java.util.Map)7 WebTarget (javax.ws.rs.client.WebTarget)6 ApiOperation (io.swagger.annotations.ApiOperation)5 ApiResponses (io.swagger.annotations.ApiResponses)5 GET (javax.ws.rs.GET)5 Path (javax.ws.rs.Path)5 RestException (org.apache.pulsar.broker.web.RestException)5 ApiParam (io.swagger.annotations.ApiParam)4 ApiResponse (io.swagger.annotations.ApiResponse)4 Example (io.swagger.annotations.Example)4 ExampleProperty (io.swagger.annotations.ExampleProperty)4 ArrayList (java.util.ArrayList)4 Collections (java.util.Collections)4 LinkedHashSet (java.util.LinkedHashSet)4 List (java.util.List)4 Objects (java.util.Objects)4 Optional (java.util.Optional)4 Collectors (java.util.stream.Collectors)4