Skip to content

Commit

Permalink
CHE-1117: Add parameters for auto-snapshot and auto-restore of the wo…
Browse files Browse the repository at this point in the history
…rkspace state
  • Loading branch information
akorneta committed May 19, 2016
1 parent 8ff0709 commit a8549b2
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
Expand Up @@ -143,3 +143,10 @@ machine.ssh.connection_timeout_ms=3000
# Suffix helps differentiate archive for different architectures/OSes
machine.server.terminal.path_to_archive.linux_amd64=${che.home}/lib/linux_amd64/terminal
machine.server.terminal.path_to_archive.linux_arm7=${che.home}/lib/linux_arm7/terminal

# During the stop of the workspace automatically creates a snapshot if the value is {true},
# otherwise just stops the workspace.
workspace.runtime.auto_snapshot=false
# During the start of the workspace automatically restored it from a snapshot if the value is {true},
# otherwise just creates the new workspace.
workspace.runtime.auto_restore=false
Expand Up @@ -44,6 +44,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Named;
import javax.inject.Singleton;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -88,6 +89,8 @@ public class WorkspaceManager {
private final ExecutorService executor;
private final MachineManager machineManager;
private final UserManager userManager;
private final boolean defaultAutoSnapshot;
private final boolean defaultAutoRestore;

private WorkspaceHooks hooks = new NoopWorkspaceHooks();

Expand All @@ -96,12 +99,16 @@ public WorkspaceManager(WorkspaceDao workspaceDao,
WorkspaceRuntimes workspaceRegistry,
EventService eventService,
MachineManager machineManager,
UserManager userManager) {
UserManager userManager,
@Named("workspace.runtime.auto_snapshot") boolean defaultAutoSnapshot,
@Named("workspace.runtime.auto_restore") boolean defaultAutoRestore) {
this.workspaceDao = workspaceDao;
this.runtimes = workspaceRegistry;
this.eventService = eventService;
this.machineManager = machineManager;
this.userManager = userManager;
this.defaultAutoSnapshot = defaultAutoSnapshot;
this.defaultAutoRestore = defaultAutoRestore;

executor = Executors.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("WorkspaceManager-%d")
.setDaemon(true)
Expand Down Expand Up @@ -343,9 +350,10 @@ public WorkspaceImpl startWorkspace(String workspaceId,
ConflictException {
requireNonNull(workspaceId, "Required non-null workspace id");
final WorkspaceImpl workspace = workspaceDao.get(workspaceId);
final boolean autoRestore = parseBoolean(workspace.getAttributes().get(AUTO_RESTORE_FROM_SNAPSHOT))
&& !getSnapshot(workspaceId).isEmpty();
return performAsyncStart(workspace, envName, autoRestore, accountId);
final String restoreAttr = workspace.getAttributes().get(AUTO_RESTORE_FROM_SNAPSHOT);
final boolean autoRestore = restoreAttr == null ? defaultAutoRestore : parseBoolean(restoreAttr);
final boolean snapshotExists = !getSnapshot(workspaceId).isEmpty();
return performAsyncStart(workspace, envName, snapshotExists && autoRestore, accountId);
}

/**
Expand Down Expand Up @@ -550,7 +558,8 @@ WorkspaceImpl performAsyncStart(WorkspaceImpl workspace,
*/
@VisibleForTesting
void performAsyncStop(WorkspaceImpl workspace) throws ConflictException {
final boolean createSnapshot = parseBoolean(workspace.getAttributes().get(AUTO_CREATE_SNAPSHOT));
final String autoSnapshotAttr = workspace.getAttributes().get(AUTO_CREATE_SNAPSHOT);
final boolean createSnapshot = autoSnapshotAttr == null ? defaultAutoSnapshot : parseBoolean(autoSnapshotAttr);
if (createSnapshot) {
checkWorkspaceBeforeCreatingSnapshot(workspace);
}
Expand Down
Expand Up @@ -109,7 +109,9 @@ public void setUp() throws Exception {
runtimes,
eventService,
machineManager,
userManager));
userManager,
false,
false));
workspaceManager.setHooks(workspaceHooks);

when(workspaceDao.create(any(WorkspaceImpl.class))).thenAnswer(invocation -> invocation.getArguments()[0]);
Expand Down Expand Up @@ -531,6 +533,45 @@ public void shouldBeAbleToGetSnapshots() throws Exception {
assertEquals(snapshots.size(), 1);
}

@Test
public void shouldCreateWorkspaceSnapshotUsingDefaultValueForAutoRestore() throws Exception {
workspaceManager = spy(new WorkspaceManager(workspaceDao,
runtimes,
eventService,
machineManager,
userManager,
true,
false));
final WorkspaceImpl workspace = workspaceManager.createWorkspace(createConfig(), "user123", "account");
when(workspaceDao.get(workspace.getId())).thenReturn(workspace);
final RuntimeDescriptor descriptor = createDescriptor(workspace, RUNNING);
when(runtimes.get(any())).thenReturn(descriptor);

workspaceManager.stopWorkspace(workspace.getId());

verify(workspaceManager, timeout(2000)).createSnapshotSync(workspace.getRuntime(), workspace.getNamespace(), workspace.getId());
verify(runtimes, timeout(2000)).stop(any());
}

@Test
public void shouldStartWorkspaceFromSnapshotUsingDefaultValueForAutoRestore() throws Exception {
workspaceManager = spy(new WorkspaceManager(workspaceDao,
runtimes,
eventService,
machineManager,
userManager,
false,
true));
final WorkspaceImpl workspace = workspaceManager.createWorkspace(createConfig(), "user123", "account");
when(machineManager.getSnapshots(any(), any())).thenReturn(singletonList(mock(SnapshotImpl.class)));
when(workspaceDao.get(workspace.getId())).thenReturn(workspace);
when(runtimes.get(any())).thenThrow(new NotFoundException(""));

workspaceManager.startWorkspace(workspace.getId(), workspace.getConfig().getDefaultEnv(), "account");

verify(runtimes, timeout(2000)).start(workspace, workspace.getConfig().getDefaultEnv(), true);
}

private RuntimeDescriptor createDescriptor(WorkspaceImpl workspace, WorkspaceStatus status) {
final WorkspaceRuntimeImpl runtime = new WorkspaceRuntimeImpl(workspace.getConfig().getDefaultEnv());
final String env = workspace.getConfig().getDefaultEnv();
Expand Down

0 comments on commit a8549b2

Please sign in to comment.