Search in sources :

Example 21 with RuntimeIdentity

use of org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity in project che-server by eclipse-che.

the class KubernetesRuntimeStateCacheTest method shouldDoNothingIfStateIsAlreadyRemove.

@Test(dependsOnMethods = "shouldReturnEmptyOptionalIfRuntimeStateIsNotFound")
public void shouldDoNothingIfStateIsAlreadyRemove() throws Exception {
    // given
    KubernetesRuntimeState runtimeState = createRuntimeState(workspaces[2]);
    RuntimeIdentity toRemove = runtimeState.getRuntimeId();
    // when
    runtimesStatesCache.remove(toRemove);
    // then
    assertFalse(runtimesStatesCache.get(toRemove).isPresent());
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) KubernetesRuntimeState(org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesRuntimeState) Test(org.testng.annotations.Test)

Example 22 with RuntimeIdentity

use of org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity in project che-server by eclipse-che.

the class BrokerService method handle.

private void handle(BrokerStatusChangedEvent event) {
    String encodedTooling = event.getTooling();
    RuntimeIdentity runtimeId = event.getRuntimeId();
    if (event.getStatus() == null || runtimeId == null || runtimeId.getWorkspaceId() == null) {
        LOG.error("Broker event skipped due to illegal content: {}", event);
        return;
    }
    eventService.publish(new BrokerEvent(event, parseTooling(encodedTooling)));
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity)

Example 23 with RuntimeIdentity

use of org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity in project che-server by eclipse-che.

the class WorkspaceRuntimes method getInternalRuntime.

/**
 * Returns {@link InternalRuntime} implementation for workspace with the specified id.
 *
 * <p>If memory-storage does not contain internal runtime, then runtime will be recovered if it is
 * active. Otherwise, an exception will be thrown.
 *
 * @param workspaceId identifier of workspace to fetch runtime
 * @return {@link InternalRuntime} implementation for workspace with the specified id.
 * @throws InfrastructureException if any infrastructure exception occurs
 * @throws ServerException if there is no active runtime for the specified workspace
 * @throws ServerException if any other exception occurs
 */
public InternalRuntime<?> getInternalRuntime(String workspaceId) throws InfrastructureException, ServerException {
    try (Unlocker ignored = lockService.writeLock(workspaceId)) {
        InternalRuntime<?> runtime = runtimes.get(workspaceId);
        if (runtime == null) {
            try {
                final Optional<RuntimeIdentity> runtimeIdentity = infrastructure.getIdentities().stream().filter(id -> id.getWorkspaceId().equals(workspaceId)).findAny();
                if (runtimeIdentity.isPresent()) {
                    LOG.info("Runtime for workspace '{}' is requested but there is no cached one. Recovering it.", workspaceId);
                    runtime = recoverOne(infrastructure, runtimeIdentity.get());
                } else {
                    // runtime is not considered by Infrastructure as active
                    throw new ServerException("No active runtime is found");
                }
            } catch (ServerException e) {
                statuses.remove(workspaceId);
                throw e;
            } catch (UnsupportedOperationException | ConflictException e) {
                statuses.remove(workspaceId);
                throw new ServerException(e.getMessage(), e);
            }
        }
        return runtime;
    }
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) NamespaceResolutionContext(org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext) STOPPED_ATTRIBUTE_NAME(org.eclipse.che.api.workspace.shared.Constants.STOPPED_ATTRIBUTE_NAME) ThreadLocalPropagateContext(org.eclipse.che.commons.lang.concurrent.ThreadLocalPropagateContext) LoggerFactory(org.slf4j.LoggerFactory) PostConstruct(jakarta.annotation.PostConstruct) WorkspaceConfigImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl) RUNNING(org.eclipse.che.api.core.model.workspace.WorkspaceStatus.RUNNING) RuntimeInfrastructure(org.eclipse.che.api.workspace.server.spi.RuntimeInfrastructure) Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker) InternalEnvironmentFactory(org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironmentFactory) WorkspaceStatusEvent(org.eclipse.che.api.workspace.shared.dto.event.WorkspaceStatusEvent) InternalEnvironment(org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment) Map(java.util.Map) DevfileConverter(org.eclipse.che.api.workspace.server.devfile.convert.DevfileConverter) WorkspaceDao(org.eclipse.che.api.workspace.server.spi.WorkspaceDao) Collectors.toSet(java.util.stream.Collectors.toSet) NameGenerator(org.eclipse.che.commons.lang.NameGenerator) EventService(org.eclipse.che.api.core.notification.EventService) Command(org.eclipse.che.api.core.model.workspace.config.Command) ImmutableSet(com.google.common.collect.ImmutableSet) RuntimeContext(org.eclipse.che.api.workspace.server.spi.RuntimeContext) RuntimeAbnormalStoppingEvent(org.eclipse.che.api.workspace.server.event.RuntimeAbnormalStoppingEvent) ImmutableMap(com.google.common.collect.ImmutableMap) Collections.emptyList(java.util.Collections.emptyList) EventSubscriber(org.eclipse.che.api.core.notification.EventSubscriber) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) String.format(java.lang.String.format) STOPPED_ABNORMALLY_ATTRIBUTE_NAME(org.eclipse.che.api.workspace.shared.Constants.STOPPED_ABNORMALLY_ATTRIBUTE_NAME) Sets(com.google.common.collect.Sets) RuntimeImpl(org.eclipse.che.api.workspace.server.model.impl.RuntimeImpl) Nullable(org.eclipse.che.commons.annotation.Nullable) RuntimeStartInterruptedException(org.eclipse.che.api.workspace.server.spi.RuntimeStartInterruptedException) STARTING(org.eclipse.che.api.core.model.workspace.WorkspaceStatus.STARTING) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) List(java.util.List) RuntimeIdentityImpl(org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl) TracingTags(org.eclipse.che.commons.tracing.TracingTags) RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE(org.eclipse.che.api.workspace.shared.Constants.WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE) Entry(java.util.Map.Entry) Optional(java.util.Optional) MoreObjects.firstNonNull(com.google.common.base.MoreObjects.firstNonNull) WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) STOPPED(org.eclipse.che.api.core.model.workspace.WorkspaceStatus.STOPPED) Workspace(org.eclipse.che.api.core.model.workspace.Workspace) STOPPING(org.eclipse.che.api.core.model.workspace.WorkspaceStatus.STOPPING) System.currentTimeMillis(java.lang.System.currentTimeMillis) CommandImpl(org.eclipse.che.api.workspace.server.model.impl.CommandImpl) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Singleton(javax.inject.Singleton) Traced(org.eclipse.che.commons.annotation.Traced) ValidationException(org.eclipse.che.api.core.ValidationException) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) Inject(javax.inject.Inject) CHE_DEVWORKSPACES_ENABLED_PROPERTY(org.eclipse.che.api.workspace.shared.Constants.CHE_DEVWORKSPACES_ENABLED_PROPERTY) WORKSPACE_STOPPED_BY(org.eclipse.che.api.workspace.shared.Constants.WORKSPACE_STOPPED_BY) InternalInfrastructureException(org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException) EnvironmentContext(org.eclipse.che.commons.env.EnvironmentContext) ERROR_MESSAGE_ATTRIBUTE_NAME(org.eclipse.che.api.workspace.shared.Constants.ERROR_MESSAGE_ATTRIBUTE_NAME) Subject(org.eclipse.che.commons.subject.Subject) ConflictException(org.eclipse.che.api.core.ConflictException) DevfileImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl) Named(javax.inject.Named) DBInitializer(org.eclipse.che.core.db.DBInitializer) DtoFactory(org.eclipse.che.dto.server.DtoFactory) ProbeScheduler(org.eclipse.che.api.workspace.server.hc.probe.ProbeScheduler) InternalRuntime(org.eclipse.che.api.workspace.server.spi.InternalRuntime) Collections.emptyMap(java.util.Collections.emptyMap) Logger(org.slf4j.Logger) RuntimeAbnormalStoppedEvent(org.eclipse.che.api.workspace.server.event.RuntimeAbnormalStoppedEvent) Constants(org.eclipse.che.api.workspace.shared.Constants) WorkspaceStatus(org.eclipse.che.api.core.model.workspace.WorkspaceStatus) SetView(com.google.common.collect.Sets.SetView) WORKSPACE_STOP_REASON(org.eclipse.che.api.workspace.shared.Constants.WORKSPACE_STOP_REASON) Environment(org.eclipse.che.api.core.model.workspace.config.Environment) NotFoundException(org.eclipse.che.api.core.NotFoundException) TimeUnit(java.util.concurrent.TimeUnit) Collectors.toList(java.util.stream.Collectors.toList) WORKSPACE_RUNTIMES_ID_ATTRIBUTE(org.eclipse.che.api.workspace.shared.Constants.WORKSPACE_RUNTIMES_ID_ATTRIBUTE) ServerException(org.eclipse.che.api.core.ServerException) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ServerException(org.eclipse.che.api.core.ServerException) ConflictException(org.eclipse.che.api.core.ConflictException) Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker)

Example 24 with RuntimeIdentity

use of org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity in project che-server by eclipse-che.

the class WorkspaceRuntimes method recover.

@VisibleForTesting
void recover() {
    if (isStartRefused.get()) {
        LOG.warn("Recovery of the workspaces is rejected.");
        return;
    }
    Set<RuntimeIdentity> identities;
    try {
        identities = infrastructure.getIdentities();
    } catch (UnsupportedOperationException e) {
        LOG.warn("Not recoverable infrastructure: '{}'", infrastructure.getName());
        return;
    } catch (InfrastructureException e) {
        LOG.error("An error occurred while attempting to get runtime identities for infrastructure '{}'. Reason: '{}'", infrastructure.getName(), e.getMessage());
        return;
    }
    LOG.info("Infrastructure is tracking {} active runtimes", identities.size());
    if (identities.isEmpty()) {
        return;
    }
    for (RuntimeIdentity identity : identities) {
        String workspaceId = identity.getWorkspaceId();
        try (Unlocker ignored = lockService.writeLock(workspaceId)) {
            statuses.putIfAbsent(workspaceId, STARTING);
        }
    }
    sharedPool.execute(new RecoverRuntimesTask(identities));
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) InternalInfrastructureException(org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 25 with RuntimeIdentity

use of org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity in project che-server by eclipse-che.

the class WorkspaceRuntimes method stop.

void stop() {
    Set<RuntimeIdentity> identities;
    try {
        identities = infrastructure.getIdentities();
    } catch (UnsupportedOperationException e) {
        LOG.warn("Not recoverable infrastructure: '{}'", infrastructure.getName());
        return;
    } catch (InfrastructureException e) {
        LOG.error("An error occurred while attempting to get runtime identities for infrastructure '{}'. Reason: '{}'", infrastructure.getName(), e.getMessage());
        return;
    }
    LOG.info("Infrastructure is tracking {} active runtimes that need to be stopped", identities.size());
    if (identities.isEmpty()) {
        return;
    }
    for (RuntimeIdentity identity : identities) {
        try {
            String workspaceId = identity.getWorkspaceId();
            WorkspaceImpl workspace = workspaceDao.get(workspaceId);
            try (Unlocker ignored = lockService.writeLock(workspaceId)) {
                statuses.putIfAbsent(workspaceId, STARTING);
            }
            String namespace = workspace.getAttributes().get(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE);
            stopAsync(workspace, emptyMap()).whenComplete((aVoid, throwable) -> {
                LOG.info("Workspace '{}' owned by '{}' has been stopped in namespace '{}'", workspaceId, namespace);
            });
        } catch (Exception e) {
            LOG.error("An error occurred while attempting to stop runtime '{}' using infrastructure '{}'. Reason: '{}'", workspaceRuntimesId, infrastructure.getName(), e.getMessage(), e);
        }
    }
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) InternalInfrastructureException(org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException) 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) ConflictException(org.eclipse.che.api.core.ConflictException) NotFoundException(org.eclipse.che.api.core.NotFoundException) ServerException(org.eclipse.che.api.core.ServerException)

Aggregations

RuntimeIdentity (org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity)128 Test (org.testng.annotations.Test)88 RuntimeIdentityImpl (org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl)70 InfrastructureException (org.eclipse.che.api.workspace.server.spi.InfrastructureException)40 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)28 InternalEnvironment (org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment)24 RuntimeContext (org.eclipse.che.api.workspace.server.spi.RuntimeContext)22 Map (java.util.Map)20 MixedOperation (io.fabric8.kubernetes.client.dsl.MixedOperation)18 List (java.util.List)16 Set (java.util.Set)14 ServerException (org.eclipse.che.api.core.ServerException)14 InternalInfrastructureException (org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException)14 Traced (org.eclipse.che.commons.annotation.Traced)14 KubernetesMachineImpl (org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesMachineImpl)14 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)12 Secret (io.fabric8.kubernetes.api.model.Secret)12 String.format (java.lang.String.format)12 Collectors (java.util.stream.Collectors)12 Named (javax.inject.Named)12