Search in sources :

Example 96 with InfrastructureException

use of org.eclipse.che.api.workspace.server.spi.InfrastructureException in project devspaces-images by redhat-developer.

the class KubernetesInternalRuntime method internalStop.

@Traced
@Override
protected void internalStop(Map<String, String> stopOptions) throws InfrastructureException {
    RuntimeIdentity identity = getContext().getIdentity();
    TracingTags.WORKSPACE_ID.set(identity.getWorkspaceId());
    runtimeHangingDetector.stopTracking(getContext().getIdentity());
    if (startSynchronizer.interrupt()) {
        // runtime is STARTING. Need to wait until start will be interrupted properly
        try {
            if (!startSynchronizer.awaitInterruption(workspaceStartTimeoutMin, TimeUnit.MINUTES)) {
                // Runtime is not interrupted yet. It may occur when start was performing by another
                // Che Server that is crashed so start is hung up in STOPPING phase.
                // Need to clean up runtime resources
                probeScheduler.cancel(identity.getWorkspaceId());
                runtimeCleaner.cleanUp(namespace, identity.getWorkspaceId());
            }
        } catch (InterruptedException e) {
            throw new InfrastructureException("Interrupted while waiting for start task cancellation", e);
        }
    } else {
        // runtime is RUNNING. Clean up used resources
        // Cancels workspace servers probes if any
        probeScheduler.cancel(identity.getWorkspaceId());
        runtimeCleaner.cleanUp(namespace, identity.getWorkspaceId());
    }
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) RuntimeStartInterruptedException(org.eclipse.che.api.workspace.server.spi.RuntimeStartInterruptedException) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) InternalInfrastructureException(org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException) Traced(org.eclipse.che.commons.annotation.Traced)

Example 97 with InfrastructureException

use of org.eclipse.che.api.workspace.server.spi.InfrastructureException in project devspaces-images by redhat-developer.

the class KubernetesInternalRuntime method doStartMachine.

/**
 * Creates Kubernetes pods and resolves servers using the specified serverResolver.
 *
 * @param serverResolver server resolver that provide servers by container
 * @throws InfrastructureException when any error occurs while creating Kubernetes pods
 */
@Traced
protected void doStartMachine(ServerResolver serverResolver) throws InfrastructureException {
    final KubernetesEnvironment environment = getContext().getEnvironment();
    final Map<String, InternalMachineConfig> machineConfigs = environment.getMachines();
    final String workspaceId = getContext().getIdentity().getWorkspaceId();
    LOG.debug("Begin pods creation for workspace '{}'", workspaceId);
    PodMerger podMerger = new PodMerger();
    Map<String, Map<String, Pod>> injectablePods = environment.getInjectablePodsCopy();
    for (Pod toCreate : environment.getPodsCopy().values()) {
        ObjectMeta toCreateMeta = toCreate.getMetadata();
        List<PodData> injectables = getAllInjectablePods(toCreate, injectablePods);
        Pod createdPod;
        if (injectables.isEmpty()) {
            createdPod = namespace.deployments().deploy(toCreate);
        } else {
            try {
                injectables.add(new PodData(toCreate));
                Deployment merged = podMerger.merge(injectables);
                merged.getMetadata().setName(toCreate.getMetadata().getName());
                createdPod = namespace.deployments().deploy(merged);
            } catch (ValidationException e) {
                throw new InfrastructureException(e);
            }
        }
        LOG.debug("Creating pod '{}' in workspace '{}'", toCreateMeta.getName(), workspaceId);
        storeStartingMachine(createdPod, createdPod.getMetadata(), machineConfigs, serverResolver);
    }
    for (Deployment toCreate : environment.getDeploymentsCopy().values()) {
        PodTemplateSpec template = toCreate.getSpec().getTemplate();
        List<PodData> injectables = getAllInjectablePods(template.getMetadata(), template.getSpec().getContainers(), injectablePods);
        Pod createdPod;
        if (injectables.isEmpty()) {
            createdPod = namespace.deployments().deploy(toCreate);
        } else {
            try {
                injectables.add(new PodData(toCreate));
                Deployment deployment = podMerger.merge(injectables);
                deployment.getMetadata().setName(toCreate.getMetadata().getName());
                putAnnotations(deployment.getMetadata(), toCreate.getMetadata().getAnnotations());
                putLabels(deployment.getMetadata(), toCreate.getMetadata().getLabels());
                createdPod = namespace.deployments().deploy(deployment);
            } catch (ValidationException e) {
                throw new InfrastructureException(e);
            }
        }
        LOG.debug("Creating deployment '{}' in workspace '{}'", createdPod.getMetadata().getName(), workspaceId);
        storeStartingMachine(createdPod, createdPod.getMetadata(), machineConfigs, serverResolver);
    }
    LOG.debug("Pods creation finished in workspace '{}'", workspaceId);
}
Also used : PodMerger(org.eclipse.che.workspace.infrastructure.kubernetes.environment.PodMerger) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) ValidationException(org.eclipse.che.api.core.ValidationException) Pod(io.fabric8.kubernetes.api.model.Pod) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) InternalMachineConfig(org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) LinkedHashMap(java.util.LinkedHashMap) Collections.emptyMap(java.util.Collections.emptyMap) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) InternalInfrastructureException(org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException) Traced(org.eclipse.che.commons.annotation.Traced)

Example 98 with InfrastructureException

use of org.eclipse.che.api.workspace.server.spi.InfrastructureException in project devspaces-images by redhat-developer.

the class KubernetesInternalRuntime method internalStart.

@Override
protected void internalStart(Map<String, String> startOptions) throws InfrastructureException {
    KubernetesRuntimeContext<E> context = getContext();
    String workspaceId = context.getIdentity().getWorkspaceId();
    try {
        startSynchronizer.setStartThread();
        startSynchronizer.start();
        runtimeCleaner.cleanUp(namespace, workspaceId);
        provisionWorkspace(startOptions, context, workspaceId);
        volumesStrategy.prepare(context.getEnvironment(), context.getIdentity(), startSynchronizer.getStartTimeoutMillis(), startOptions);
        startSynchronizer.checkFailure();
        startMachines();
        watchLogsIfDebugEnabled(startOptions);
        previewUrlCommandProvisioner.provision(context.getEnvironment(), namespace);
        runtimeStates.updateCommands(context.getIdentity(), context.getEnvironment().getCommands());
        startSynchronizer.checkFailure();
        final Map<String, CompletableFuture<Void>> machinesFutures = new LinkedHashMap<>();
        // futures that must be cancelled explicitly
        final List<CompletableFuture<?>> toCancelFutures = new CopyOnWriteArrayList<>();
        final EnvironmentContext currentContext = EnvironmentContext.getCurrent();
        CompletableFuture<Void> startFailure = startSynchronizer.getStartFailure();
        Span waitRunningAsyncSpan = tracer.buildSpan(WAIT_MACHINES_START).start();
        try (Scope waitRunningAsyncScope = tracer.scopeManager().activate(waitRunningAsyncSpan)) {
            TracingTags.WORKSPACE_ID.set(waitRunningAsyncSpan, workspaceId);
            for (KubernetesMachineImpl machine : machines.getMachines(context.getIdentity()).values()) {
                String machineName = machine.getName();
                final CompletableFuture<Void> machineBootChain = waitRunningAsync(toCancelFutures, machine).thenComposeAsync(checkFailure(startFailure), executor).thenRun(publishRunningStatus(machineName)).thenCompose(checkFailure(startFailure)).thenCompose(setContext(currentContext, checkServers(toCancelFutures, machine))).exceptionally(publishFailedStatus(startFailure, machineName));
                machinesFutures.put(machineName, machineBootChain);
            }
            waitMachines(machinesFutures, toCancelFutures, startFailure);
        } finally {
            waitRunningAsyncSpan.finish();
        }
        startSynchronizer.complete();
    } catch (InfrastructureException | RuntimeException e) {
        Exception startFailureCause = startSynchronizer.getStartFailureNow();
        if (startFailureCause == null) {
            startFailureCause = e;
        }
        startSynchronizer.completeExceptionally(startFailureCause);
        LOG.warn("Failed to start Kubernetes runtime of workspace {}.", workspaceId, startFailureCause);
        boolean interrupted = Thread.interrupted() || startFailureCause instanceof RuntimeStartInterruptedException;
        // Cancels workspace servers probes if any
        probeScheduler.cancel(workspaceId);
        // stop watching before namespace cleaning up
        namespace.deployments().stopWatch(true);
        try {
            runtimeCleaner.cleanUp(namespace, workspaceId);
        } catch (InfrastructureException cleanUppingEx) {
            LOG.warn("Failed to clean up namespace after workspace '{}' start failing.", context.getIdentity().getWorkspaceId(), cleanUppingEx);
        }
        if (interrupted) {
            throw new RuntimeStartInterruptedException(getContext().getIdentity());
        }
        wrapAndRethrow(startFailureCause);
    } finally {
        namespace.deployments().stopWatch();
    }
}
Also used : Span(io.opentracing.Span) TimeoutException(java.util.concurrent.TimeoutException) StateException(org.eclipse.che.api.workspace.server.spi.StateException) RuntimeStartInterruptedException(org.eclipse.che.api.workspace.server.spi.RuntimeStartInterruptedException) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) ValidationException(org.eclipse.che.api.core.ValidationException) InternalInfrastructureException(org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException) ExecutionException(java.util.concurrent.ExecutionException) LinkedHashMap(java.util.LinkedHashMap) EnvironmentContext(org.eclipse.che.commons.env.EnvironmentContext) CompletableFuture(java.util.concurrent.CompletableFuture) Scope(io.opentracing.Scope) KubernetesMachineImpl(org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesMachineImpl) RuntimeStartInterruptedException(org.eclipse.che.api.workspace.server.spi.RuntimeStartInterruptedException) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) InternalInfrastructureException(org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 99 with InfrastructureException

use of org.eclipse.che.api.workspace.server.spi.InfrastructureException in project devspaces-images by redhat-developer.

the class RuntimeHangingDetector method handleHangingStoppingRuntime.

private void handleHangingStoppingRuntime(KubernetesInternalRuntime runtime) {
    RuntimeIdentity runtimeId = runtime.getContext().getIdentity();
    eventPublisher.sendAbnormalStoppingEvent(runtimeId, "Workspace is not stopped in time. Trying to stop it forcibly");
    try {
        LOG.info("Runtime '{}:{}:{}' is not stopped in time. Stopped it forcibly", runtimeId.getWorkspaceId(), runtimeId.getEnvName(), runtimeId.getOwnerId());
        runtime.internalStop(emptyMap());
    } catch (InfrastructureException e) {
        LOG.error("Error occurred during forcibly stopping of hanging runtime '{}:{}:{}'. Error: {}", runtimeId.getWorkspaceId(), runtimeId.getEnvName(), runtimeId.getOwnerId(), e.getMessage(), e);
    } finally {
        try {
            runtime.markStopped();
        } catch (InfrastructureException e) {
            LOG.error("Error occurred during marking hanging runtime as stopped '{}:{}:{}'. Error: {}", runtimeId.getWorkspaceId(), runtimeId.getEnvName(), runtimeId.getOwnerId(), e.getMessage(), e);
        }
        eventPublisher.sendAbnormalStoppedEvent(runtimeId, "Workspace stop reached timeout");
    }
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException)

Example 100 with InfrastructureException

use of org.eclipse.che.api.workspace.server.spi.InfrastructureException in project devspaces-images by redhat-developer.

the class RuntimeHangingDetector method handleHangingStartingRuntime.

private void handleHangingStartingRuntime(KubernetesInternalRuntime runtime) {
    RuntimeIdentity runtimeId = runtime.getContext().getIdentity();
    eventPublisher.sendAbnormalStoppingEvent(runtimeId, "Workspace is not started in time. Trying interrupt runtime start");
    try {
        runtime.stop(emptyMap());
        LOG.info("Start of hanging runtime '{}:{}:{}' is interrupted", runtimeId.getWorkspaceId(), runtimeId.getEnvName(), runtimeId.getOwnerId());
    } catch (InfrastructureException e) {
        LOG.error("Error occurred during start interruption of hanging runtime '{}:{}:{}'. Error: {}", runtimeId.getWorkspaceId(), runtimeId.getEnvName(), runtimeId.getOwnerId(), e.getMessage(), e);
    } finally {
        eventPublisher.sendAbnormalStoppedEvent(runtimeId, "Workspace start reached timeout");
    }
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException)

Aggregations

InfrastructureException (org.eclipse.che.api.workspace.server.spi.InfrastructureException)242 InternalInfrastructureException (org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException)64 Test (org.testng.annotations.Test)56 KubernetesInfrastructureException (org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException)44 RuntimeIdentity (org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity)42 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)38 CompletableFuture (java.util.concurrent.CompletableFuture)36 ExecutionException (java.util.concurrent.ExecutionException)36 TimeoutException (java.util.concurrent.TimeoutException)32 ServerException (org.eclipse.che.api.core.ServerException)32 Pod (io.fabric8.kubernetes.api.model.Pod)30 Map (java.util.Map)26 ValidationException (org.eclipse.che.api.core.ValidationException)22 Traced (org.eclipse.che.commons.annotation.Traced)20 Container (io.fabric8.kubernetes.api.model.Container)18 List (java.util.List)18 Set (java.util.Set)18 Inject (javax.inject.Inject)18 RuntimeStartInterruptedException (org.eclipse.che.api.workspace.server.spi.RuntimeStartInterruptedException)18 KubernetesEnvironment (org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment)18