Search in sources :

Example 16 with WorkspaceRuntimeImpl

use of org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl in project che by eclipse.

the class WorkspaceManagerTest method mockRuntime.

private WorkspaceRuntimeImpl mockRuntime(WorkspaceImpl workspace, WorkspaceStatus status) {
    when(runtimes.getStatus(workspace.getId())).thenReturn(status);
    MachineImpl machine1 = spy(createMachine(workspace.getId(), workspace.getConfig().getDefaultEnv(), true));
    MachineImpl machine2 = spy(createMachine(workspace.getId(), workspace.getConfig().getDefaultEnv(), false));
    Map<String, MachineImpl> machines = new HashMap<>();
    machines.put(machine1.getId(), machine1);
    machines.put(machine2.getId(), machine2);
    WorkspaceRuntimeImpl runtime = new WorkspaceRuntimeImpl(workspace.getConfig().getDefaultEnv(), machines.values());
    doAnswer(inv -> {
        workspace.setStatus(status);
        workspace.setRuntime(runtime);
        return null;
    }).when(runtimes).injectRuntime(workspace);
    when(runtimes.isAnyRunning()).thenReturn(true);
    return runtime;
}
Also used : ExtendedMachineImpl(org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl) MachineImpl(org.eclipse.che.api.machine.server.model.impl.MachineImpl) HashMap(java.util.HashMap) WorkspaceRuntimeImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl) Matchers.anyString(org.mockito.Matchers.anyString)

Example 17 with WorkspaceRuntimeImpl

use of org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl in project che by eclipse.

the class WorkspaceManagerTest method shouldBeAbleToGetMachineInstanceIfWorkspaceIsStarting.

@Test
public void shouldBeAbleToGetMachineInstanceIfWorkspaceIsStarting() throws Exception {
    // given
    final WorkspaceImpl workspace = createAndMockWorkspace();
    WorkspaceRuntimeImpl runtime = mockRuntime(workspace, STARTING);
    MachineImpl machine = runtime.getMachines().get(0);
    // when
    workspaceManager.getMachineInstance(workspace.getId(), machine.getId());
    // then
    verify(runtimes).getMachine(workspace.getId(), machine.getId());
}
Also used : WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) ExtendedMachineImpl(org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl) MachineImpl(org.eclipse.che.api.machine.server.model.impl.MachineImpl) WorkspaceRuntimeImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl) Test(org.testng.annotations.Test)

Example 18 with WorkspaceRuntimeImpl

use of org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl in project che by eclipse.

the class WorkspaceRuntimes method snapshotAndUpdateStatus.

/** Creates a snapshot and changes status SNAPSHOTTING -> RUNNING. */
private void snapshotAndUpdateStatus(String workspaceId) throws NotFoundException, ConflictException, ServerException {
    eventsService.publish(DtoFactory.newDto(WorkspaceStatusEvent.class).withWorkspaceId(workspaceId).withStatus(WorkspaceStatus.SNAPSHOTTING).withEventType(EventType.SNAPSHOT_CREATING).withPrevStatus(WorkspaceStatus.RUNNING));
    WorkspaceRuntimeImpl runtime = getRuntime(workspaceId);
    List<MachineImpl> machines = runtime.getMachines();
    machines.sort(comparing(m -> !m.getConfig().isDev(), Boolean::compare));
    LOG.info("Creating snapshot of workspace '{}', machines to snapshot: '{}'", workspaceId, machines.size());
    List<SnapshotImpl> newSnapshots = new ArrayList<>(machines.size());
    for (MachineImpl machine : machines) {
        try {
            newSnapshots.add(envEngine.saveSnapshot(workspaceId, machine.getId()));
        } catch (ServerException | NotFoundException x) {
            if (machine.getConfig().isDev()) {
                compareAndSetStatus(workspaceId, WorkspaceStatus.SNAPSHOTTING, WorkspaceStatus.RUNNING);
                eventsService.publish(DtoFactory.newDto(WorkspaceStatusEvent.class).withWorkspaceId(workspaceId).withStatus(WorkspaceStatus.RUNNING).withEventType(EventType.SNAPSHOT_CREATION_ERROR).withPrevStatus(WorkspaceStatus.SNAPSHOTTING).withError(x.getMessage()));
                throw x;
            }
            LOG.warn(format("Couldn't create snapshot of machine '%s:%s' in workspace '%s'", machine.getEnvName(), machine.getConfig().getName(), workspaceId));
        }
    }
    LOG.info("Saving new snapshots metadata, workspace id '{}'", workspaceId);
    try {
        List<SnapshotImpl> removed = snapshotDao.replaceSnapshots(workspaceId, runtime.getActiveEnv(), newSnapshots);
        if (!removed.isEmpty()) {
            LOG.info("Removing old snapshots binaries, workspace id '{}', snapshots to remove '{}'", workspaceId, removed.size());
            removeBinaries(removed);
        }
    } catch (SnapshotException x) {
        LOG.error(format("Couldn't remove existing snapshots metadata for workspace '%s'", workspaceId), x);
        LOG.info("Removing newly created snapshots, workspace id '{}', snapshots to remove '{}'", workspaceId, newSnapshots.size());
        removeBinaries(newSnapshots);
        compareAndSetStatus(workspaceId, WorkspaceStatus.SNAPSHOTTING, WorkspaceStatus.RUNNING);
        eventsService.publish(DtoFactory.newDto(WorkspaceStatusEvent.class).withWorkspaceId(workspaceId).withStatus(WorkspaceStatus.RUNNING).withEventType(EventType.SNAPSHOT_CREATION_ERROR).withPrevStatus(WorkspaceStatus.SNAPSHOTTING).withError(x.getMessage()));
        throw x;
    }
    compareAndSetStatus(workspaceId, WorkspaceStatus.SNAPSHOTTING, WorkspaceStatus.RUNNING);
    eventsService.publish(DtoFactory.newDto(WorkspaceStatusEvent.class).withStatus(WorkspaceStatus.RUNNING).withWorkspaceId(workspaceId).withEventType(EventType.SNAPSHOT_CREATED).withPrevStatus(WorkspaceStatus.SNAPSHOTTING));
}
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) MachineImpl(org.eclipse.che.api.machine.server.model.impl.MachineImpl) ServerException(org.eclipse.che.api.core.ServerException) SnapshotImpl(org.eclipse.che.api.machine.server.model.impl.SnapshotImpl) WorkspaceStatusEvent(org.eclipse.che.api.workspace.shared.dto.event.WorkspaceStatusEvent) ArrayList(java.util.ArrayList) NotFoundException(org.eclipse.che.api.core.NotFoundException) WorkspaceRuntimeImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl) SnapshotException(org.eclipse.che.api.machine.server.exception.SnapshotException)

Example 19 with WorkspaceRuntimeImpl

use of org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl in project che by eclipse.

the class WorkspaceRuntimesTest method failsWorkspaceStartWhenEnvironmentStartIsFailed.

@Test
public void failsWorkspaceStartWhenEnvironmentStartIsFailed() throws Exception {
    WorkspaceImpl workspace = newWorkspace("workspace", "env-name");
    rejectEnvironmentStart(workspace, "env-name", new EnvironmentException("no no no!"));
    CompletableFuture<WorkspaceRuntimeImpl> cmpFuture = runtimes.startAsync(workspace, "env-name", false);
    try {
        captureAsyncTaskAndExecuteSynchronously();
    } catch (EnvironmentException x) {
        assertEquals(x.getMessage(), "no no no!");
        verifyCompletionException(cmpFuture, EnvironmentException.class, "no no no!");
    }
    assertFalse(runtimes.hasRuntime(workspace.getId()));
    verifyEventsSequence(event("workspace", WorkspaceStatus.STOPPED, WorkspaceStatus.STARTING, EventType.STARTING, null), event("workspace", WorkspaceStatus.STARTING, WorkspaceStatus.STOPPED, EventType.ERROR, "Start of environment 'env-name' failed. Error: no no no!"));
}
Also used : WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) WorkspaceRuntimeImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl) EnvironmentException(org.eclipse.che.api.environment.server.exception.EnvironmentException) Test(org.testng.annotations.Test)

Example 20 with WorkspaceRuntimeImpl

use of org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl in project che by eclipse.

the class WorkspaceRuntimesTest method cancelsWorkspaceStartIfEnvironmentStartIsInterrupted.

@Test
public void cancelsWorkspaceStartIfEnvironmentStartIsInterrupted() throws Exception {
    WorkspaceImpl workspace = newWorkspace("workspace", "env-name");
    rejectEnvironmentStart(workspace, "env-name", new EnvironmentStartInterruptedException(workspace.getId(), "env-name"));
    CompletableFuture<WorkspaceRuntimeImpl> cmpFuture = runtimes.startAsync(workspace, "env-name", false);
    captureAndVerifyRuntimeStateAfterInterruption(workspace, cmpFuture);
}
Also used : WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) EnvironmentStartInterruptedException(org.eclipse.che.api.environment.server.exception.EnvironmentStartInterruptedException) WorkspaceRuntimeImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl) Test(org.testng.annotations.Test)

Aggregations

WorkspaceRuntimeImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl)21 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)16 Test (org.testng.annotations.Test)16 MachineImpl (org.eclipse.che.api.machine.server.model.impl.MachineImpl)7 ExtendedMachineImpl (org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl)6 EnvironmentStartInterruptedException (org.eclipse.che.api.environment.server.exception.EnvironmentStartInterruptedException)4 CancellationException (java.util.concurrent.CancellationException)3 NoOpMachineInstance (org.eclipse.che.api.environment.server.NoOpMachineInstance)3 EnvironmentException (org.eclipse.che.api.environment.server.exception.EnvironmentException)3 MachineConfigImpl (org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl)3 Instance (org.eclipse.che.api.machine.server.spi.Instance)3 EnvironmentImpl (org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl)3 Unlocker (org.eclipse.che.commons.lang.concurrent.Unlocker)3 Response (com.jayway.restassured.response.Response)2 HashSet (java.util.HashSet)2 AgentException (org.eclipse.che.api.agent.server.exception.AgentException)2 ConflictException (org.eclipse.che.api.core.ConflictException)2 ServerException (org.eclipse.che.api.core.ServerException)2 Matchers.anyString (org.mockito.Matchers.anyString)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1