Search in sources :

Example 1 with Unlocker

use of org.eclipse.che.commons.lang.concurrent.Unlocker in project che by eclipse.

the class CheEnvironmentEngine method startEnvironmentQueue.

/**
     * Starts all machine from machine queue of environment.
     */
private void startEnvironmentQueue(String namespace, String workspaceId, String devMachineName, String networkId, boolean recover, MachineStartedHandler startedHandler) throws ServerException, EnvironmentException {
    // Starting all machines in environment one by one by getting configs
    // from the corresponding starting queue.
    // Config will be null only if there are no machines left in the queue
    String envName;
    MessageConsumer<MachineLogMessage> envLogger;
    String creator = EnvironmentContext.getCurrent().getSubject().getUserId();
    try (@SuppressWarnings("unused") Unlocker u = stripedLocks.readLock(workspaceId)) {
        EnvironmentHolder environmentHolder = environments.get(workspaceId);
        if (environmentHolder == null) {
            throw new ServerException("Environment start is interrupted.");
        }
        envName = environmentHolder.name;
        envLogger = environmentHolder.logger;
    }
    try {
        machineProvider.createNetwork(networkId);
        String machineName = queuePeekOrFail(workspaceId);
        while (machineName != null) {
            boolean isDev = devMachineName.equals(machineName);
            // Environment start is failed when any machine start is failed, so if any error
            // occurs during machine creation then environment start fail is reported and
            // start resources such as queue and descriptor must be cleaned up
            CheServiceImpl service;
            @Nullable ExtendedMachine extendedMachine;
            try (@SuppressWarnings("unused") Unlocker u = stripedLocks.readLock(workspaceId)) {
                EnvironmentHolder environmentHolder = environments.get(workspaceId);
                if (environmentHolder == null) {
                    throw new ServerException("Environment start is interrupted.");
                }
                service = environmentHolder.environment.getServices().get(machineName);
                extendedMachine = environmentHolder.environmentConfig.getMachines().get(machineName);
            }
            // should not happen
            if (service == null) {
                LOG.error("Start of machine with name {} in workspace {} failed. Machine not found in start queue", machineName, workspaceId);
                throw new ServerException(format("Environment of workspace with ID '%s' failed due to internal error", workspaceId));
            }
            final String finalMachineName = machineName;
            // needed to reuse startInstance method and
            // create machine instances by different implementation-specific providers
            MachineStarter machineStarter = (machineLogger, machineSource) -> {
                CheServiceImpl serviceWithNormalizedSource = normalizeServiceSource(service, machineSource);
                return machineProvider.startService(namespace, workspaceId, envName, finalMachineName, isDev, networkId, serviceWithNormalizedSource, machineLogger);
            };
            MachineImpl machine = MachineImpl.builder().setConfig(MachineConfigImpl.builder().setDev(isDev).setLimits(new MachineLimitsImpl(bytesToMB(service.getMemLimit()))).setType("docker").setName(machineName).setEnvVariables(service.getEnvironment()).build()).setId(service.getId()).setWorkspaceId(workspaceId).setStatus(MachineStatus.CREATING).setEnvName(envName).setOwner(creator).build();
            checkInterruption(workspaceId, envName);
            Instance instance = startInstance(recover, envLogger, machine, machineStarter);
            checkInterruption(workspaceId, envName);
            startedHandler.started(instance, extendedMachine);
            checkInterruption(workspaceId, envName);
            // Machine destroying is an expensive operation which must be
            // performed outside of the lock, this section checks if
            // the environment wasn't stopped while it is starting and sets
            // polled flag to true if the environment wasn't stopped.
            // Also polls the proceeded machine configuration from the queue
            boolean queuePolled = false;
            try (@SuppressWarnings("unused") Unlocker u = stripedLocks.writeLock(workspaceId)) {
                ensurePreDestroyIsNotExecuted();
                EnvironmentHolder environmentHolder = environments.get(workspaceId);
                if (environmentHolder != null) {
                    final Queue<String> queue = environmentHolder.startQueue;
                    if (queue != null) {
                        queue.poll();
                        queuePolled = true;
                    }
                }
            }
            // must be destroyed
            if (!queuePolled) {
                try {
                    eventService.publish(newDto(MachineStatusEvent.class).withEventType(MachineStatusEvent.EventType.DESTROYING).withDev(isDev).withMachineName(machineName).withMachineId(instance.getId()).withWorkspaceId(workspaceId));
                    instance.destroy();
                    removeMachine(workspaceId, instance.getId());
                    eventService.publish(newDto(MachineStatusEvent.class).withEventType(MachineStatusEvent.EventType.DESTROYED).withDev(isDev).withMachineName(machineName).withMachineId(instance.getId()).withWorkspaceId(workspaceId));
                } catch (MachineException e) {
                    LOG.error(e.getLocalizedMessage(), e);
                }
                throw new ServerException("Workspace '" + workspaceId + "' start interrupted. Workspace stopped before all its machines started");
            }
            machineName = queuePeekOrFail(workspaceId);
        }
    } catch (RuntimeException | ServerException | EnvironmentStartInterruptedException e) {
        boolean interrupted = Thread.interrupted();
        EnvironmentHolder env;
        try (@SuppressWarnings("unused") Unlocker u = stripedLocks.writeLock(workspaceId)) {
            env = environments.remove(workspaceId);
        }
        try {
            destroyEnvironment(env.networkId, env.machines);
        } catch (Exception remEx) {
            LOG.error(remEx.getLocalizedMessage(), remEx);
        }
        if (interrupted) {
            throw new EnvironmentStartInterruptedException(workspaceId, envName);
        }
        try {
            throw e;
        } catch (ServerException | EnvironmentStartInterruptedException rethrow) {
            throw rethrow;
        } catch (Exception wrap) {
            throw new ServerException(wrap.getMessage(), wrap);
        }
    }
}
Also used : MachineStatusEvent(org.eclipse.che.api.machine.shared.dto.event.MachineStatusEvent) MachineStatus(org.eclipse.che.api.core.model.machine.MachineStatus) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) MachineLimitsImpl(org.eclipse.che.api.machine.server.model.impl.MachineLimitsImpl) AgentImpl(org.eclipse.che.api.agent.shared.model.impl.AgentImpl) StripedLocks(org.eclipse.che.commons.lang.concurrent.StripedLocks) WorkspaceSharedPool(org.eclipse.che.api.workspace.server.WorkspaceSharedPool) AgentException(org.eclipse.che.api.agent.server.exception.AgentException) Utils.getDevMachineName(org.eclipse.che.api.workspace.shared.Utils.getDevMachineName) PreDestroy(javax.annotation.PreDestroy) Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker) EnvironmentStartInterruptedException(org.eclipse.che.api.environment.server.exception.EnvironmentStartInterruptedException) Map(java.util.Map) EnvironmentNotRunningException(org.eclipse.che.api.environment.server.exception.EnvironmentNotRunningException) MessageConsumer(org.eclipse.che.api.core.util.MessageConsumer) CheServiceBuildContextImpl(org.eclipse.che.api.environment.server.model.CheServiceBuildContextImpl) NameGenerator(org.eclipse.che.commons.lang.NameGenerator) EventService(org.eclipse.che.api.core.notification.EventService) ConcurrentCompositeLineConsumer(org.eclipse.che.api.core.util.lineconsumer.ConcurrentCompositeLineConsumer) EventSubscriber(org.eclipse.che.api.core.notification.EventSubscriber) Collections.emptyList(java.util.Collections.emptyList) MachineConfig(org.eclipse.che.api.core.model.machine.MachineConfig) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DtoFactory.newDto(org.eclipse.che.dto.server.DtoFactory.newDto) SourceNotFoundException(org.eclipse.che.api.machine.server.exception.SourceNotFoundException) MachineSource(org.eclipse.che.api.core.model.machine.MachineSource) MachineLogMessageImpl(org.eclipse.che.api.machine.server.model.impl.MachineLogMessageImpl) Nullable(org.eclipse.che.commons.annotation.Nullable) String.format(java.lang.String.format) IoUtil(org.eclipse.che.commons.lang.IoUtil) Objects(java.util.Objects) EnvironmentException(org.eclipse.che.api.environment.server.exception.EnvironmentException) List(java.util.List) Environment(org.eclipse.che.api.core.model.workspace.Environment) PostConstruct(javax.annotation.PostConstruct) ServerConfImpl(org.eclipse.che.api.machine.server.model.impl.ServerConfImpl) Queue(java.util.Queue) Pattern(java.util.regex.Pattern) CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) AgentRegistry(org.eclipse.che.api.agent.server.AgentRegistry) ConcurrentFileLineConsumer(org.eclipse.che.api.core.util.lineconsumer.ConcurrentFileLineConsumer) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) MachineInstanceProviders(org.eclipse.che.api.machine.server.MachineInstanceProviders) AgentKeyImpl(org.eclipse.che.api.agent.shared.model.impl.AgentKeyImpl) Size(org.eclipse.che.commons.lang.Size) LineConsumer(org.eclipse.che.api.core.util.LineConsumer) AbstractLineConsumer(org.eclipse.che.api.core.util.AbstractLineConsumer) ExtendedMachineImpl(org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl) Singleton(javax.inject.Singleton) OOM(org.eclipse.che.api.machine.server.event.InstanceStateEvent.Type.OOM) CheServicesEnvironmentImpl(org.eclipse.che.api.environment.server.model.CheServicesEnvironmentImpl) SnapshotImpl(org.eclipse.che.api.machine.server.model.impl.SnapshotImpl) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) EnvironmentContext(org.eclipse.che.commons.env.EnvironmentContext) InstanceProvider(org.eclipse.che.api.machine.server.spi.InstanceProvider) ApiException(org.eclipse.che.api.core.ApiException) ServerConf2(org.eclipse.che.api.core.model.workspace.ServerConf2) ConflictException(org.eclipse.che.api.core.ConflictException) InstanceStateEvent(org.eclipse.che.api.machine.server.event.InstanceStateEvent) Named(javax.inject.Named) Instance(org.eclipse.che.api.machine.server.spi.Instance) SnapshotDao(org.eclipse.che.api.machine.server.spi.SnapshotDao) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) Logger(org.slf4j.Logger) ExtendedMachine(org.eclipse.che.api.core.model.workspace.ExtendedMachine) ServerConf(org.eclipse.che.api.core.model.machine.ServerConf) IOException(java.io.IOException) MachineLogMessage(org.eclipse.che.api.core.model.machine.MachineLogMessage) NotFoundException(org.eclipse.che.api.core.NotFoundException) File(java.io.File) Machine(org.eclipse.che.api.core.model.machine.Machine) MachineSourceImpl(org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl) Collectors.toList(java.util.stream.Collectors.toList) MachineImpl(org.eclipse.che.api.machine.server.model.impl.MachineImpl) RecipeDownloader(org.eclipse.che.api.machine.server.util.RecipeDownloader) ServerException(org.eclipse.che.api.core.ServerException) MachineConfigImpl(org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ArrayDeque(java.util.ArrayDeque) DIE(org.eclipse.che.api.machine.server.event.InstanceStateEvent.Type.DIE) ServerException(org.eclipse.che.api.core.ServerException) ExtendedMachineImpl(org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl) MachineImpl(org.eclipse.che.api.machine.server.model.impl.MachineImpl) CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) Instance(org.eclipse.che.api.machine.server.spi.Instance) EnvironmentStartInterruptedException(org.eclipse.che.api.environment.server.exception.EnvironmentStartInterruptedException) MachineLimitsImpl(org.eclipse.che.api.machine.server.model.impl.MachineLimitsImpl) ExtendedMachine(org.eclipse.che.api.core.model.workspace.ExtendedMachine) AgentException(org.eclipse.che.api.agent.server.exception.AgentException) EnvironmentStartInterruptedException(org.eclipse.che.api.environment.server.exception.EnvironmentStartInterruptedException) EnvironmentNotRunningException(org.eclipse.che.api.environment.server.exception.EnvironmentNotRunningException) SourceNotFoundException(org.eclipse.che.api.machine.server.exception.SourceNotFoundException) EnvironmentException(org.eclipse.che.api.environment.server.exception.EnvironmentException) ApiException(org.eclipse.che.api.core.ApiException) ConflictException(org.eclipse.che.api.core.ConflictException) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) IOException(java.io.IOException) NotFoundException(org.eclipse.che.api.core.NotFoundException) ServerException(org.eclipse.che.api.core.ServerException) Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) MachineLogMessage(org.eclipse.che.api.core.model.machine.MachineLogMessage) Nullable(org.eclipse.che.commons.annotation.Nullable)

Example 2 with Unlocker

use of org.eclipse.che.commons.lang.concurrent.Unlocker in project che by eclipse.

the class CheEnvironmentEngine method initializeEnvironment.

private void initializeEnvironment(String namespace, String workspaceId, String envName, EnvironmentImpl envConfig, String networkId, MessageConsumer<MachineLogMessage> messageConsumer) throws ServerException, ConflictException, EnvironmentException {
    CheServicesEnvironmentImpl internalEnv = environmentParser.parse(envConfig);
    internalEnv.setWorkspaceId(workspaceId);
    infrastructureProvisioner.provision(envConfig, internalEnv);
    normalize(namespace, workspaceId, internalEnv);
    List<String> servicesOrder = startStrategy.order(internalEnv);
    normalizeNames(internalEnv);
    EnvironmentHolder environmentHolder = new EnvironmentHolder(servicesOrder, internalEnv, envConfig, messageConsumer, EnvStatus.STARTING, envName, networkId);
    try (@SuppressWarnings("unused") Unlocker u = stripedLocks.writeLock(workspaceId)) {
        if (environments.putIfAbsent(workspaceId, environmentHolder) != null) {
            throw new ConflictException(format("Environment of workspace '%s' already exists", workspaceId));
        }
    }
}
Also used : ConflictException(org.eclipse.che.api.core.ConflictException) Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker) CheServicesEnvironmentImpl(org.eclipse.che.api.environment.server.model.CheServicesEnvironmentImpl)

Example 3 with Unlocker

use of org.eclipse.che.commons.lang.concurrent.Unlocker in project che by eclipse.

the class CheEnvironmentEngine method start.

/**
     * Starts provided environment.
     *
     * <p>Environment starts if and only all machines in environment definition start successfully.<br/>
     * Otherwise exception is thrown by this method.<br/>
     * It is not defined whether environment start fails right after first failure or in the end of the process.<br/>
     * Starting order of machines is not guarantied. Machines can start sequentially or in parallel.
     *
     * @param workspaceId
     *         ID of workspace that owns provided environment
     * @param envName
     *         name of environment
     * @param env
     *         environment to start
     * @param recover
     *         whether machines from environment should be recovered or not
     * @param messageConsumer
     *         consumer of log messages from machines in the environment
     * @param startedHandler
     *         handler for started machines
     * @return list of running machines of this environment
     * @throws ServerException
     *         if other error occurs
     */
public List<Instance> start(String workspaceId, String envName, Environment env, boolean recover, MessageConsumer<MachineLogMessage> messageConsumer, MachineStartedHandler startedHandler) throws ServerException, EnvironmentException, ConflictException {
    EnvironmentImpl environment = new EnvironmentImpl(env);
    // TODO move to machines provider
    // add random chars to ensure that old environments that weren't removed by some reason won't prevent start
    String networkId = NameGenerator.generate(workspaceId + "_", 16);
    String namespace = EnvironmentContext.getCurrent().getSubject().getUserName();
    initializeEnvironment(namespace, workspaceId, envName, environment, networkId, messageConsumer);
    String devMachineName = getDevMachineName(environment);
    if (devMachineName == null) {
        throw new ServerException("Agent 'org.eclipse.che.ws-agent' is not found in any of environment machines");
    }
    startEnvironmentQueue(namespace, workspaceId, devMachineName, networkId, recover, startedHandler);
    try (@SuppressWarnings("unused") Unlocker u = stripedLocks.writeLock(workspaceId)) {
        EnvironmentHolder environmentHolder = environments.get(workspaceId);
        // possible only if environment was stopped during its start
        if (environmentHolder == null) {
            throw new ServerException("Environment start was interrupted by environment stopping");
        }
        environmentHolder.status = EnvStatus.RUNNING;
        // prevent list modification
        return new ArrayList<>(environmentHolder.machines);
    }
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) CheServicesEnvironmentImpl(org.eclipse.che.api.environment.server.model.CheServicesEnvironmentImpl)

Example 4 with Unlocker

use of org.eclipse.che.commons.lang.concurrent.Unlocker in project che by eclipse.

the class WorkspaceRuntimes method shutdown.

/**
     * Terminates workspace runtimes service, so no more workspaces are allowed to start
     * or to be stopped directly, all the running workspaces are going to be stopped,
     * all the starting tasks will be eventually interrupted.
     *
     * @throws IllegalStateException
     *         if component shutdown is already called
     */
public void shutdown() throws InterruptedException {
    if (!isShutdown.compareAndSet(false, true)) {
        throw new IllegalStateException("Workspace runtimes service shutdown has been already called");
    }
    List<String> idsToStop;
    try (@SuppressWarnings("unused") Unlocker u = locks.writeAllLock()) {
        idsToStop = states.entrySet().stream().filter(e -> e.getValue().status != WorkspaceStatus.STOPPING).map(Map.Entry::getKey).collect(Collectors.toList());
        states.clear();
    }
    if (!idsToStop.isEmpty()) {
        LOG.info("Shutdown running environments, environments to stop: '{}'", idsToStop.size());
        ExecutorService executor = Executors.newFixedThreadPool(2 * Runtime.getRuntime().availableProcessors(), new ThreadFactoryBuilder().setNameFormat("StopEnvironmentsPool-%d").setDaemon(false).build());
        for (String id : idsToStop) {
            executor.execute(() -> {
                try {
                    envEngine.stop(id);
                } catch (EnvironmentNotRunningException ignored) {
                // might be already stopped
                } catch (Exception x) {
                    LOG.error(x.getMessage(), x);
                }
            });
        }
        executor.shutdown();
        try {
            if (!executor.awaitTermination(30, TimeUnit.SECONDS)) {
                executor.shutdownNow();
                if (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
                    LOG.error("Unable to stop runtimes termination pool");
                }
            }
        } catch (InterruptedException e) {
            executor.shutdownNow();
            Thread.currentThread().interrupt();
        }
    }
}
Also used : ENVIRONMENT_OUTPUT_CHANNEL_TEMPLATE(org.eclipse.che.api.machine.shared.Constants.ENVIRONMENT_OUTPUT_CHANNEL_TEMPLATE) AgentLauncher(org.eclipse.che.api.agent.server.launcher.AgentLauncher) Agent(org.eclipse.che.api.agent.shared.model.Agent) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) StripedLocks(org.eclipse.che.commons.lang.concurrent.StripedLocks) AgentException(org.eclipse.che.api.agent.server.exception.AgentException) RUNNING(org.eclipse.che.api.core.model.workspace.WorkspaceStatus.RUNNING) Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker) Future(java.util.concurrent.Future) WorkspaceStatusEvent(org.eclipse.che.api.workspace.shared.dto.event.WorkspaceStatusEvent) EnvironmentStartInterruptedException(org.eclipse.che.api.environment.server.exception.EnvironmentStartInterruptedException) Map(java.util.Map) EnvironmentNotRunningException(org.eclipse.che.api.environment.server.exception.EnvironmentNotRunningException) EventService(org.eclipse.che.api.core.notification.EventService) CancellationException(java.util.concurrent.CancellationException) MachineConfig(org.eclipse.che.api.core.model.machine.MachineConfig) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Collectors(java.util.stream.Collectors) Nullable(org.eclipse.che.commons.annotation.Nullable) Executors(java.util.concurrent.Executors) String.format(java.lang.String.format) CountDownLatch(java.util.concurrent.CountDownLatch) STARTING(org.eclipse.che.api.core.model.workspace.WorkspaceStatus.STARTING) EnvironmentException(org.eclipse.che.api.environment.server.exception.EnvironmentException) List(java.util.List) Environment(org.eclipse.che.api.core.model.workspace.Environment) CheEnvironmentEngine(org.eclipse.che.api.environment.server.CheEnvironmentEngine) AgentKey(org.eclipse.che.api.agent.shared.model.AgentKey) EventType(org.eclipse.che.api.workspace.shared.dto.event.WorkspaceStatusEvent.EventType) AgentLauncherFactory(org.eclipse.che.api.agent.server.launcher.AgentLauncherFactory) WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) AgentRegistry(org.eclipse.che.api.agent.server.AgentRegistry) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Workspace(org.eclipse.che.api.core.model.workspace.Workspace) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Callable(java.util.concurrent.Callable) CompletableFuture(java.util.concurrent.CompletableFuture) Singleton(javax.inject.Singleton) SnapshotImpl(org.eclipse.che.api.machine.server.model.impl.SnapshotImpl) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) Inject(javax.inject.Inject) HashSet(java.util.HashSet) WebsocketMessageConsumer(org.eclipse.che.api.core.util.WebsocketMessageConsumer) WorkspaceRuntimeImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl) Objects.requireNonNull(java.util.Objects.requireNonNull) ConflictException(org.eclipse.che.api.core.ConflictException) Comparator.comparing(java.util.Comparator.comparing) Instance(org.eclipse.che.api.machine.server.spi.Instance) SnapshotDao(org.eclipse.che.api.machine.server.spi.SnapshotDao) DtoFactory(org.eclipse.che.dto.server.DtoFactory) ExecutorService(java.util.concurrent.ExecutorService) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) Logger(org.slf4j.Logger) ExtendedMachine(org.eclipse.che.api.core.model.workspace.ExtendedMachine) SNAPSHOTTING(org.eclipse.che.api.core.model.workspace.WorkspaceStatus.SNAPSHOTTING) WorkspaceStatus(org.eclipse.che.api.core.model.workspace.WorkspaceStatus) NotFoundException(org.eclipse.che.api.core.NotFoundException) TimeUnit(java.util.concurrent.TimeUnit) SnapshotException(org.eclipse.che.api.machine.server.exception.SnapshotException) MachineImpl(org.eclipse.che.api.machine.server.model.impl.MachineImpl) AgentSorter(org.eclipse.che.api.agent.server.impl.AgentSorter) ServerException(org.eclipse.che.api.core.ServerException) MachineConfigImpl(org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) VisibleForTesting(com.google.common.annotations.VisibleForTesting) MachineStartedHandler(org.eclipse.che.api.environment.server.MachineStartedHandler) Collections(java.util.Collections) Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker) ExecutorService(java.util.concurrent.ExecutorService) EnvironmentNotRunningException(org.eclipse.che.api.environment.server.exception.EnvironmentNotRunningException) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) EnvironmentStartInterruptedException(org.eclipse.che.api.environment.server.exception.EnvironmentStartInterruptedException) AgentException(org.eclipse.che.api.agent.server.exception.AgentException) EnvironmentStartInterruptedException(org.eclipse.che.api.environment.server.exception.EnvironmentStartInterruptedException) EnvironmentNotRunningException(org.eclipse.che.api.environment.server.exception.EnvironmentNotRunningException) CancellationException(java.util.concurrent.CancellationException) EnvironmentException(org.eclipse.che.api.environment.server.exception.EnvironmentException) ConflictException(org.eclipse.che.api.core.ConflictException) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) NotFoundException(org.eclipse.che.api.core.NotFoundException) SnapshotException(org.eclipse.che.api.machine.server.exception.SnapshotException) ServerException(org.eclipse.che.api.core.ServerException)

Example 5 with Unlocker

use of org.eclipse.che.commons.lang.concurrent.Unlocker in project che by eclipse.

the class WorkspaceRuntimes method injectRuntime.

/**
     * Injects runtime information such as status and {@link WorkspaceRuntimeImpl}
     * into the workspace object, if the workspace doesn't have runtime sets the
     * status to {@link WorkspaceStatus#STOPPED}.
     *
     * @param workspace
     *         the workspace to inject runtime into
     */
public void injectRuntime(WorkspaceImpl workspace) {
    requireNonNull(workspace, "Required non-null workspace");
    RuntimeState state = null;
    try (@SuppressWarnings("unused") Unlocker u = locks.readLock(workspace.getId())) {
        if (states.containsKey(workspace.getId())) {
            state = new RuntimeState(states.get(workspace.getId()));
        }
    }
    if (state == null) {
        workspace.setStatus(WorkspaceStatus.STOPPED);
    } else {
        workspace.setStatus(state.status);
        try {
            workspace.setRuntime(new WorkspaceRuntimeImpl(state.envName, envEngine.getMachines(workspace.getId())));
        } catch (Exception x) {
            workspace.setRuntime(new WorkspaceRuntimeImpl(state.envName, Collections.emptyList()));
        }
    }
}
Also used : Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker) WorkspaceRuntimeImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl) AgentException(org.eclipse.che.api.agent.server.exception.AgentException) EnvironmentStartInterruptedException(org.eclipse.che.api.environment.server.exception.EnvironmentStartInterruptedException) EnvironmentNotRunningException(org.eclipse.che.api.environment.server.exception.EnvironmentNotRunningException) CancellationException(java.util.concurrent.CancellationException) EnvironmentException(org.eclipse.che.api.environment.server.exception.EnvironmentException) ConflictException(org.eclipse.che.api.core.ConflictException) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) NotFoundException(org.eclipse.che.api.core.NotFoundException) SnapshotException(org.eclipse.che.api.machine.server.exception.SnapshotException) ServerException(org.eclipse.che.api.core.ServerException)

Aggregations

Unlocker (org.eclipse.che.commons.lang.concurrent.Unlocker)18 ServerException (org.eclipse.che.api.core.ServerException)11 ConflictException (org.eclipse.che.api.core.ConflictException)10 NotFoundException (org.eclipse.che.api.core.NotFoundException)8 EnvironmentNotRunningException (org.eclipse.che.api.environment.server.exception.EnvironmentNotRunningException)8 Instance (org.eclipse.che.api.machine.server.spi.Instance)8 EnvironmentStartInterruptedException (org.eclipse.che.api.environment.server.exception.EnvironmentStartInterruptedException)5 MachineException (org.eclipse.che.api.machine.server.exception.MachineException)5 AgentException (org.eclipse.che.api.agent.server.exception.AgentException)4 EnvironmentException (org.eclipse.che.api.environment.server.exception.EnvironmentException)4 MachineConfigImpl (org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl)4 EnvironmentImpl (org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl)4 WorkspaceRuntimeImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl)4 ArrayList (java.util.ArrayList)3 CancellationException (java.util.concurrent.CancellationException)3 MachineConfig (org.eclipse.che.api.core.model.machine.MachineConfig)3 ExtendedMachine (org.eclipse.che.api.core.model.workspace.ExtendedMachine)3 CheServicesEnvironmentImpl (org.eclipse.che.api.environment.server.model.CheServicesEnvironmentImpl)3 SnapshotException (org.eclipse.che.api.machine.server.exception.SnapshotException)3 SourceNotFoundException (org.eclipse.che.api.machine.server.exception.SourceNotFoundException)3