Skip to content

Commit

Permalink
Add 'snapshotted_at' workspace attribyte
Browse files Browse the repository at this point in the history
  • Loading branch information
Yevhenii Voevodin committed Mar 7, 2017
1 parent 2b3f522 commit 588170b
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.eclipse.che.api.core.model.workspace.WorkspaceStatus;
import org.eclipse.che.api.core.notification.EventService;
import org.eclipse.che.api.environment.server.exception.EnvironmentException;
import org.eclipse.che.api.machine.server.exception.SnapshotException;
import org.eclipse.che.api.machine.server.exception.SourceNotFoundException;
import org.eclipse.che.api.machine.server.model.impl.SnapshotImpl;
import org.eclipse.che.api.machine.server.spi.Instance;
Expand Down Expand Up @@ -79,6 +80,9 @@ public class WorkspaceManager {
/** This attribute describes time when workspace was last updated or started/stopped/recovered. */
public static final String UPDATED_ATTRIBUTE_NAME = "updated";

/** Describes time when workspace was snapshotted. */
public static final String SNAPSHOTTED_AT_ATTRIBUTE_NAME = "snapshotted_at";

private final WorkspaceDao workspaceDao;
private final SnapshotDao snapshotDao;
private final WorkspaceRuntimes runtimes;
Expand Down Expand Up @@ -201,6 +205,7 @@ public WorkspaceImpl getWorkspace(String key) throws NotFoundException, ServerEx
requireNonNull(key, "Required non-null workspace key");
WorkspaceImpl workspace = getByKey(key);
runtimes.injectRuntime(workspace);
addExtraAttributes(workspace);
return workspace;
}

Expand All @@ -225,6 +230,7 @@ public WorkspaceImpl getWorkspace(String name, String namespace) throws NotFound
requireNonNull(namespace, "Required non-null workspace owner");
WorkspaceImpl workspace = workspaceDao.get(name, namespace);
runtimes.injectRuntime(workspace);
addExtraAttributes(workspace);
return workspace;
}

Expand Down Expand Up @@ -265,11 +271,7 @@ public List<WorkspaceImpl> getWorkspaces(String user) throws ServerException {
public List<WorkspaceImpl> getWorkspaces(String user, boolean includeRuntimes) throws ServerException {
requireNonNull(user, "Required non-null user id");
final List<WorkspaceImpl> workspaces = workspaceDao.getWorkspaces(user);
if (includeRuntimes) {
injectRuntimes(workspaces);
} else {
injectStatuses(workspaces);
}
injectRuntimeAndAttributes(workspaces, !includeRuntimes);
return workspaces;
}

Expand Down Expand Up @@ -310,11 +312,7 @@ public List<WorkspaceImpl> getByNamespace(String namespace) throws ServerExcepti
public List<WorkspaceImpl> getByNamespace(String namespace, boolean includeRuntimes) throws ServerException {
requireNonNull(namespace, "Required non-null namespace");
final List<WorkspaceImpl> workspaces = workspaceDao.getByNamespace(namespace);
if (includeRuntimes) {
injectRuntimes(workspaces);
} else {
injectStatuses(workspaces);
}
injectRuntimeAndAttributes(workspaces, !includeRuntimes);
return workspaces;
}

Expand Down Expand Up @@ -416,6 +414,7 @@ public WorkspaceImpl startWorkspace(String workspaceId,
final boolean autoRestore = restoreAttr == null ? defaultAutoRestore : parseBoolean(restoreAttr);
startAsync(workspace, envName, firstNonNull(restore, autoRestore) && !getSnapshot(workspaceId).isEmpty());
runtimes.injectRuntime(workspace);
addExtraAttributes(workspace);
return workspace;
}

Expand Down Expand Up @@ -924,16 +923,27 @@ private WorkspaceImpl getByKey(String key) throws NotFoundException, ServerExcep
return workspaceDao.get(wsName, namespace);
}


private void injectRuntimes(List<? extends WorkspaceImpl> workspaces) {
for (WorkspaceImpl workspace : workspaces) {
runtimes.injectRuntime(workspace);
/** Adds runtime data (whole or status only) and extra attributes to each of the given workspaces. */
private void injectRuntimeAndAttributes(List<WorkspaceImpl> workspaces, boolean statusOnly) throws SnapshotException {
if (statusOnly) {
for (WorkspaceImpl workspace : workspaces) {
workspace.setStatus(runtimes.getStatus(workspace.getId()));
addExtraAttributes(workspace);
}
} else {
for (WorkspaceImpl workspace : workspaces) {
runtimes.injectRuntime(workspace);
addExtraAttributes(workspace);
}
}
}

private void injectStatuses(List<? extends WorkspaceImpl> workspaces) {
for (WorkspaceImpl workspace : workspaces) {
workspace.setStatus(runtimes.getStatus(workspace.getId()));
/** Adds attributes that are not originally stored in workspace but should be published. */
private void addExtraAttributes(WorkspaceImpl workspace) throws SnapshotException {
// snapshotted_at
List<SnapshotImpl> snapshots = snapshotDao.findSnapshots(workspace.getId());
if (!snapshots.isEmpty()) {
workspace.getAttributes().put(SNAPSHOTTED_AT_ATTRIBUTE_NAME, Long.toString(snapshots.get(0).getCreationDate()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.eclipse.che.api.core.NotFoundException;
import org.eclipse.che.api.core.ServerException;
import org.eclipse.che.api.core.model.machine.MachineStatus;
import org.eclipse.che.api.core.model.workspace.ExtendedMachine;
import org.eclipse.che.api.core.model.workspace.Workspace;
import org.eclipse.che.api.core.model.workspace.WorkspaceConfig;
import org.eclipse.che.api.core.model.workspace.WorkspaceStatus;
Expand Down Expand Up @@ -48,6 +49,7 @@
import org.mockito.MockitoAnnotations;
import org.mockito.testng.MockitoTestNGListener;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

Expand All @@ -57,6 +59,7 @@
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

import static com.google.common.base.Strings.isNullOrEmpty;
import static java.util.Arrays.asList;
Expand All @@ -67,6 +70,7 @@
import static org.eclipse.che.api.core.model.workspace.WorkspaceStatus.STOPPED;
import static org.eclipse.che.api.core.model.workspace.WorkspaceStatus.STOPPING;
import static org.eclipse.che.api.workspace.server.WorkspaceManager.CREATED_ATTRIBUTE_NAME;
import static org.eclipse.che.api.workspace.server.WorkspaceManager.SNAPSHOTTED_AT_ATTRIBUTE_NAME;
import static org.eclipse.che.api.workspace.server.WorkspaceManager.UPDATED_ATTRIBUTE_NAME;
import static org.eclipse.che.api.workspace.shared.Constants.AUTO_CREATE_SNAPSHOT;
import static org.eclipse.che.api.workspace.shared.Constants.AUTO_RESTORE_FROM_SNAPSHOT;
Expand Down Expand Up @@ -235,7 +239,6 @@ public void shouldBeAbleToGetWorkspaceByKeyWithoutOwner() throws Exception {
}



@Test
public void shouldBeAbleToGetWorkspacesAvailableForUser() throws Exception {
// given
Expand Down Expand Up @@ -658,22 +661,7 @@ public void shouldStartWorkspaceFromSnapshotUsingDefaultValueForAutoRestore() th
sharedPool);
WorkspaceImpl workspace = createAndMockWorkspace();
mockStart(workspace);

SnapshotImpl.SnapshotBuilder snapshotBuilder = SnapshotImpl.builder()
.generateId()
.setEnvName("env")
.setDev(true)
.setMachineName("machine1")
.setWorkspaceId(workspace.getId())
.setType("docker")
.setMachineSource(new MachineSourceImpl("image"));
SnapshotImpl snapshot1 = snapshotBuilder.build();
SnapshotImpl snapshot2 = snapshotBuilder.generateId()
.setDev(false)
.setMachineName("machine2")
.build();
when(snapshotDao.findSnapshots(workspace.getId()))
.thenReturn(asList(snapshot1, snapshot2));
mockSnapshots(workspace, 12345L);

workspaceManager.startWorkspace(workspace.getId(), workspace.getConfig().getDefaultEnv(), null);

Expand Down Expand Up @@ -915,6 +903,76 @@ public void getsRunningWorkspacesIds() {
assertEquals(workspaceManager.getRunningWorkspacesIds(), ids);
}

@Test
public void snapshottedAtAttributeIncludedToWorkspaceWhenGettingByKey() throws Exception {
WorkspaceImpl workspace = createAndMockWorkspace();
mockSnapshots(workspace, 12345);

WorkspaceImpl result = workspaceManager.getWorkspace(workspace.getId());

assertEquals(result.getAttributes().get(SNAPSHOTTED_AT_ATTRIBUTE_NAME), "12345");
}

@Test
public void snapshottedAtAttributeIncludedToWorkspaceWhenGettingByNamespaceAndName() throws Exception {
WorkspaceImpl workspace = createAndMockWorkspace();
mockSnapshots(workspace, 12345);

WorkspaceImpl result = workspaceManager.getWorkspace(workspace.getConfig().getName(), workspace.getNamespace());

assertEquals(result.getAttributes().get(SNAPSHOTTED_AT_ATTRIBUTE_NAME), "12345");
}

@Test
public void snapshottedAtAttributeIncludedToWorkspaceWhenGettingByUserId() throws Exception {
WorkspaceImpl workspace = createAndMockWorkspace();
mockSnapshots(workspace, 12345);

List<WorkspaceImpl> workspaces = workspaceManager.getWorkspaces(USER_ID, false);

assertEquals(workspaces.get(0).getAttributes().get(SNAPSHOTTED_AT_ATTRIBUTE_NAME), "12345");
}

@Test
public void snapshottedAtAttributeIncludedToWorkspaceWhenGettingByNamespace() throws Exception {
WorkspaceImpl workspace = createAndMockWorkspace();
mockSnapshots(workspace, 12345);

List<WorkspaceImpl> workspaces = workspaceManager.getByNamespace(workspace.getNamespace(), false);

assertEquals(workspaces.get(0).getAttributes().get(SNAPSHOTTED_AT_ATTRIBUTE_NAME), "12345");
}

@Test
public void snapshottedAtAttributeIncludedToWorkspaceWhenStartingById() throws Exception {
WorkspaceImpl workspace = createAndMockWorkspace();
mockSnapshots(workspace, 12345);
mockStart(workspace);

Workspace result = workspaceManager.startWorkspace(workspace.getId(), workspace.getConfig().getDefaultEnv(), false);

assertEquals(result.getAttributes().get(SNAPSHOTTED_AT_ATTRIBUTE_NAME), "12345");
}

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;
}

private void captureRunAsyncCallsAndRunSynchronously() {
verify(sharedPool, atLeastOnce()).runAsync(taskCaptor.capture());
for (Runnable runnable : taskCaptor.getAllValues()) {
Expand Down Expand Up @@ -962,6 +1020,7 @@ private WorkspaceImpl createAndMockWorkspace(WorkspaceConfig cfg, String namespa
when(workspaceDao.get(workspace.getConfig().getName(), NAMESPACE)).thenReturn(workspace);
when(workspaceDao.getByNamespace(workspace.getNamespace())).thenReturn(singletonList(workspace));
when(workspaceDao.getByNamespace(NAMESPACE)).thenReturn(singletonList(workspace));
when(workspaceDao.getWorkspaces(USER_ID)).thenReturn(singletonList(workspace));
return workspace;
}

Expand Down

0 comments on commit 588170b

Please sign in to comment.