Search in sources :

Example 1 with CancellableCompletableFuture

use of org.jboss.pnc.common.monitor.CancellableCompletableFuture in project pnc by project-ncl.

the class OpenshiftStartedEnvironment method monitorInitialization.

/**
 * retries is decremented in retryPod in case of pod failing to start
 *
 * @param onComplete
 * @param onError
 * @param retries
 */
private void monitorInitialization(Consumer<RunningEnvironment> onComplete, Consumer<Exception> onError, int retries) {
    cancelHook = () -> onComplete.accept(null);
    CompletableFuture<Void> podFuture = creatingPod.thenComposeAsync(nul -> {
        CancellableCompletableFuture<Void> monitor = pollingMonitor.monitor(this::isPodRunning, pollingMonitorCheckInterval, pollingMonitorTimeout, TimeUnit.SECONDS);
        addFuture(monitor);
        return monitor;
    }, executor);
    CompletableFuture<Void> serviceFuture = creatingService.thenComposeAsync(nul -> {
        CancellableCompletableFuture<Void> monitor = pollingMonitor.monitor(this::isServiceRunning, pollingMonitorCheckInterval, pollingMonitorTimeout, TimeUnit.SECONDS);
        addFuture(monitor);
        return monitor;
    }, executor);
    CompletableFuture<Void> routeFuture;
    if (creatingRoute.isPresent()) {
        routeFuture = creatingRoute.get().thenComposeAsync(nul -> {
            CancellableCompletableFuture<Void> monitor = pollingMonitor.monitor(this::isRouteRunning, pollingMonitorCheckInterval, pollingMonitorTimeout, TimeUnit.SECONDS);
            addFuture(monitor);
            return monitor;
        }, executor);
    } else {
        routeFuture = CompletableFuture.completedFuture(null);
    }
    CompletableFuture<Void> openshiftDefinitionsError = new CompletableFuture<>();
    openshiftDefinitions.exceptionally(t -> {
        openshiftDefinitionsError.completeExceptionally(t);
        return null;
    });
    CancellableCompletableFuture<Void> isBuildAgentUpFuture = pollingMonitor.monitor(this::isInternalServletAvailable, pollingMonitorCheckInterval, pollingMonitorTimeout, TimeUnit.SECONDS);
    addFuture(isBuildAgentUpFuture);
    CompletableFuture<RunningEnvironment> runningEnvironmentFuture = CompletableFutureUtils.allOfOrException(podFuture, serviceFuture, routeFuture).thenComposeAsync(nul -> isBuildAgentUpFuture, executor).thenApplyAsync(nul -> RunningEnvironment.createInstance(pod.getMetadata().getName(), Integer.parseInt(environmentConfiguration.getContainerPort()), route.getSpec().getHost(), getPublicEndpointUrl(), getInternalEndpointUrl(), repositorySession, Paths.get(environmentConfiguration.getWorkingDirectory()), this::destroyEnvironment, debugData), executor);
    CompletableFuture.anyOf(runningEnvironmentFuture, openshiftDefinitionsError).handleAsync((runningEnvironment, throwable) -> {
        if (throwable != null) {
            logger.info("Error while trying to create an OpenShift environment... ", throwable);
            cancelAndClearMonitors();
            // no more retries, execute the onError consumer
            if (retries == 0) {
                logger.info("No more retries left, giving up!");
                onError.accept(new Exception(getPrettierErrorMessageFromThrowable(throwable, true), throwable));
            } else {
                PodFailedStartException podFailedStartExc = null;
                if (throwable instanceof PodFailedStartException) {
                    podFailedStartExc = (PodFailedStartException) throwable;
                } else if (throwable.getCause() instanceof PodFailedStartException) {
                    podFailedStartExc = (PodFailedStartException) throwable.getCause();
                }
                if (podFailedStartExc != null && !Arrays.asList(POD_RETRYABLE_STATUSES).contains(podFailedStartExc.getPodStatus())) {
                    logger.info("The detected pod error status '{}' is not considered among the ones to be retried, giving up!", podFailedStartExc.getPodStatus());
                    // the status is not to be retried
                    onError.accept(new Exception(getPrettierErrorMessageFromThrowable(throwable, false), throwable));
                } else {
                    if (!cancelRequested) {
                        logger.warn("Creating build environment failed with error '{}'! Retrying ({} retries left)...", throwable, retries);
                        retryEnvironment(onComplete, onError, retries);
                    } else {
                        logger.info("Build was cancelled, not retrying environment!");
                    }
                }
            }
        } else {
            logger.info("Environment successfully initialized. Pod [{}]; Service [{}].", pod.getMetadata().getName(), service.getMetadata().getName());
            // openshiftDefinitionsError
            onComplete.accept((RunningEnvironment) runningEnvironment);
        // completes only with error
        }
        gaugeMetric.ifPresent(g -> g.incrementMetric(METRICS_POD_STARTED_SUCCESS_KEY));
        return null;
    }, executor);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ConcurrentSet(org.jboss.util.collection.ConcurrentSet) Arrays(java.util.Arrays) URL(java.net.URL) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) GaugeMetric(org.jboss.pnc.pncmetrics.GaugeMetric) DefaultOpenShiftClient(io.fabric8.openshift.client.DefaultOpenShiftClient) MDCUtils(org.jboss.pnc.common.logging.MDCUtils) StringUtils(org.jboss.pnc.common.util.StringUtils) Route(io.fabric8.openshift.api.model.Route) OpenshiftBuildAgentConfig(org.jboss.pnc.common.json.moduleconfig.OpenshiftBuildAgentConfig) Map(java.util.Map) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) BuildConfigurationParameterKeys(org.jboss.pnc.api.constants.BuildConfigurationParameterKeys) OpenshiftEnvironmentDriverModuleConfig(org.jboss.pnc.common.json.moduleconfig.OpenshiftEnvironmentDriverModuleConfig) JsonStringEncoder(com.fasterxml.jackson.core.io.JsonStringEncoder) MetricsConfiguration(org.jboss.pnc.pncmetrics.MetricsConfiguration) Instant(java.time.Instant) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Config(io.fabric8.kubernetes.client.Config) RunningEnvironment(org.jboss.pnc.spi.environment.RunningEnvironment) RepositorySession(org.jboss.pnc.spi.repositorymanager.model.RepositorySession) DebugData(org.jboss.pnc.spi.builddriver.DebugData) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) RandomUtils(org.jboss.pnc.common.util.RandomUtils) Service(io.fabric8.kubernetes.api.model.Service) ExecutorService(java.util.concurrent.ExecutorService) RandomStringUtils(org.apache.commons.lang.RandomStringUtils) StringPropertyReplacer(org.jboss.util.StringPropertyReplacer) Logger(org.slf4j.Logger) Properties(java.util.Properties) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Pod(io.fabric8.kubernetes.api.model.Pod) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) PodFailedStartException(org.jboss.pnc.environment.openshift.exceptions.PodFailedStartException) IOException(java.io.IOException) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) PollingMonitor(org.jboss.pnc.common.monitor.PollingMonitor) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CancellableCompletableFuture(org.jboss.pnc.common.monitor.CancellableCompletableFuture) KubernetesResource(io.fabric8.kubernetes.api.model.KubernetesResource) StartedEnvironment(org.jboss.pnc.spi.environment.StartedEnvironment) Paths(java.nio.file.Paths) CompletableFutureUtils(org.jboss.pnc.common.util.CompletableFutureUtils) ConfigBuilder(io.fabric8.kubernetes.client.ConfigBuilder) CompletableFuture(java.util.concurrent.CompletableFuture) CancellableCompletableFuture(org.jboss.pnc.common.monitor.CancellableCompletableFuture) CancellableCompletableFuture(org.jboss.pnc.common.monitor.CancellableCompletableFuture) PodFailedStartException(org.jboss.pnc.environment.openshift.exceptions.PodFailedStartException) RunningEnvironment(org.jboss.pnc.spi.environment.RunningEnvironment) TimeoutException(java.util.concurrent.TimeoutException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) PodFailedStartException(org.jboss.pnc.environment.openshift.exceptions.PodFailedStartException) IOException(java.io.IOException)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonStringEncoder (com.fasterxml.jackson.core.io.JsonStringEncoder)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)1 KubernetesResource (io.fabric8.kubernetes.api.model.KubernetesResource)1 Pod (io.fabric8.kubernetes.api.model.Pod)1 Service (io.fabric8.kubernetes.api.model.Service)1 Config (io.fabric8.kubernetes.client.Config)1 ConfigBuilder (io.fabric8.kubernetes.client.ConfigBuilder)1 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)1 MixedOperation (io.fabric8.kubernetes.client.dsl.MixedOperation)1 Route (io.fabric8.openshift.api.model.Route)1 DefaultOpenShiftClient (io.fabric8.openshift.client.DefaultOpenShiftClient)1 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)1 IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 Paths (java.nio.file.Paths)1 Instant (java.time.Instant)1 Arrays (java.util.Arrays)1