Search in sources :

Example 26 with SnapshotImpl

use of org.eclipse.che.api.machine.server.model.impl.SnapshotImpl in project che by eclipse.

the class WorkspaceManagerTest method shouldBeAbleToGetSnapshots.

@Test
public void shouldBeAbleToGetSnapshots() throws Exception {
    final WorkspaceImpl workspace = createAndMockWorkspace();
    final SnapshotImpl wsSnapshot = SnapshotImpl.builder().setDev(true).setEnvName("envName").setId("snap1").setMachineName("machine1").setWorkspaceId(workspace.getId()).build();
    when(snapshotDao.findSnapshots(workspace.getId())).thenReturn(singletonList(wsSnapshot));
    final List<SnapshotImpl> snapshots = workspaceManager.getSnapshot(workspace.getId());
    assertEquals(snapshots.size(), 1);
    assertEquals(snapshots.get(0), wsSnapshot);
}
Also used : WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) SnapshotImpl(org.eclipse.che.api.machine.server.model.impl.SnapshotImpl) Test(org.testng.annotations.Test)

Example 27 with SnapshotImpl

use of org.eclipse.che.api.machine.server.model.impl.SnapshotImpl in project che by eclipse.

the class WorkspaceManagerTest method mockSnapshots.

private List<SnapshotImpl> mockSnapshots(Workspace workspace, long creation) throws SnapshotException {
    SnapshotImpl.SnapshotBuilder snapshotBuilder = SnapshotImpl.builder().generateId().setCreationDate(creation).setEnvName(workspace.getConfig().getDefaultEnv()).setWorkspaceId(workspace.getId()).setType("docker").setMachineSource(new MachineSourceImpl("image"));
    SnapshotImpl snapshot1 = snapshotBuilder.build();
    SnapshotImpl snapshot2 = snapshotBuilder.generateId().setDev(false).setMachineName("machine2").build();
    List<SnapshotImpl> snapshots = asList(snapshot1, snapshot2);
    when(snapshotDao.findSnapshots(workspace.getId())).thenReturn(snapshots);
    return snapshots;
}
Also used : SnapshotImpl(org.eclipse.che.api.machine.server.model.impl.SnapshotImpl) MachineSourceImpl(org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl)

Example 28 with SnapshotImpl

use of org.eclipse.che.api.machine.server.model.impl.SnapshotImpl in project che by eclipse.

the class WorkspaceManager method removeSnapshots.

/**
     * Removes all snapshots of workspace machines,
     * continues to remove snapshots even when removal of some of them fails.
     *
     * <p>Note that snapshots binaries are removed asynchronously
     * while metadata removal is synchronous operation.
     *
     * @param workspaceId
     *         workspace id to remove machine snapshots
     * @throws NotFoundException
     *         when workspace with given id doesn't exists
     * @throws ServerException
     *         when any other error occurs
     */
public void removeSnapshots(String workspaceId) throws NotFoundException, ServerException {
    List<SnapshotImpl> snapshots = getSnapshot(workspaceId);
    List<SnapshotImpl> removed = new ArrayList<>(snapshots.size());
    for (SnapshotImpl snapshot : snapshots) {
        try {
            snapshotDao.removeSnapshot(snapshot.getId());
            removed.add(snapshot);
        } catch (Exception x) {
            LOG.error(format("Couldn't remove snapshot '%s' meta data, " + "binaries won't be removed either", snapshot.getId()), x);
        }
    }
    // binaries removal may take some time, do it asynchronously
    sharedPool.execute(() -> runtimes.removeBinaries(removed));
}
Also used : SnapshotImpl(org.eclipse.che.api.machine.server.model.impl.SnapshotImpl) ArrayList(java.util.ArrayList) ApiException(org.eclipse.che.api.core.ApiException) ConflictException(org.eclipse.che.api.core.ConflictException) SourceNotFoundException(org.eclipse.che.api.machine.server.exception.SourceNotFoundException) NotFoundException(org.eclipse.che.api.core.NotFoundException) EnvironmentException(org.eclipse.che.api.environment.server.exception.EnvironmentException) SnapshotException(org.eclipse.che.api.machine.server.exception.SnapshotException) BadRequestException(org.eclipse.che.api.core.BadRequestException) ServerException(org.eclipse.che.api.core.ServerException)

Example 29 with SnapshotImpl

use of org.eclipse.che.api.machine.server.model.impl.SnapshotImpl 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 30 with SnapshotImpl

use of org.eclipse.che.api.machine.server.model.impl.SnapshotImpl in project che by eclipse.

the class CheEnvironmentEngine method saveSnapshot.

/**
     * Saves machine into snapshot.
     *
     * @param workspaceId
     *         ID of workspace that owns environment
     * @param machineId
     *         ID of machine to save
     * @return snapshot
     * @throws EnvironmentNotRunningException
     *         if environment of machine is not running
     * @throws NotFoundException
     *         if machine is not running
     * @throws ServerException
     *         if another error occurs
     */
public SnapshotImpl saveSnapshot(String workspaceId, String machineId) throws ServerException, NotFoundException {
    EnvironmentHolder environmentHolder;
    SnapshotImpl snapshot = null;
    Instance instance = null;
    try (@SuppressWarnings("unused") Unlocker u = stripedLocks.readLock(workspaceId)) {
        environmentHolder = environments.get(workspaceId);
        if (environmentHolder == null || environmentHolder.status != EnvStatus.RUNNING) {
            throw new EnvironmentNotRunningException(format("Environment '%s' is not running", workspaceId));
        }
        for (Instance machine : environmentHolder.machines) {
            if (machine.getId().equals(machineId)) {
                instance = machine;
                snapshot = SnapshotImpl.builder().generateId().setType(machine.getConfig().getType()).setWorkspaceId(machine.getWorkspaceId()).setDescription(machine.getEnvName()).setDev(machine.getConfig().isDev()).setEnvName(machine.getEnvName()).setMachineName(machine.getConfig().getName()).useCurrentCreationDate().build();
            }
        }
    }
    if (instance == null) {
        throw new NotFoundException(format("Machine with id '%s' is not found in environment of workspace '%s'", machineId, workspaceId));
    }
    try {
        MachineSource machineSource = instance.saveToSnapshot();
        snapshot.setMachineSource(new MachineSourceImpl(machineSource));
        return snapshot;
    } catch (ServerException e) {
        try {
            instance.getLogger().writeLine("Snapshot storing failed. " + e.getLocalizedMessage());
        } catch (IOException ignore) {
        }
        throw e;
    }
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) SnapshotImpl(org.eclipse.che.api.machine.server.model.impl.SnapshotImpl) Instance(org.eclipse.che.api.machine.server.spi.Instance) Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker) EnvironmentNotRunningException(org.eclipse.che.api.environment.server.exception.EnvironmentNotRunningException) SourceNotFoundException(org.eclipse.che.api.machine.server.exception.SourceNotFoundException) NotFoundException(org.eclipse.che.api.core.NotFoundException) MachineSourceImpl(org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl) IOException(java.io.IOException) MachineSource(org.eclipse.che.api.core.model.machine.MachineSource)

Aggregations

SnapshotImpl (org.eclipse.che.api.machine.server.model.impl.SnapshotImpl)31 Test (org.testng.annotations.Test)22 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)13 MachineSourceImpl (org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl)10 NotFoundException (org.eclipse.che.api.core.NotFoundException)6 ServerException (org.eclipse.che.api.core.ServerException)5 SnapshotException (org.eclipse.che.api.machine.server.exception.SnapshotException)4 Instance (org.eclipse.che.api.machine.server.spi.Instance)4 ArrayList (java.util.ArrayList)3 ConflictException (org.eclipse.che.api.core.ConflictException)3 Workspace (org.eclipse.che.api.core.model.workspace.Workspace)3 EnvironmentException (org.eclipse.che.api.environment.server.exception.EnvironmentException)3 EnvironmentNotRunningException (org.eclipse.che.api.environment.server.exception.EnvironmentNotRunningException)3 SourceNotFoundException (org.eclipse.che.api.machine.server.exception.SourceNotFoundException)3 SnapshotDao (org.eclipse.che.api.machine.server.spi.SnapshotDao)3 EnvironmentImpl (org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl)3 Matchers.anyString (org.mockito.Matchers.anyString)3 TypeLiteral (com.google.inject.TypeLiteral)2 Transactional (com.google.inject.persist.Transactional)2 IOException (java.io.IOException)2