From 93fd6c6150fd4b4d2ce48ebb8750e4c489f3b489 Mon Sep 17 00:00:00 2001 From: Michail Kuznetsov Date: Tue, 10 Jan 2017 17:34:32 +0200 Subject: [PATCH] Do not fetch runtimes when getting list of all workspaces (#3557) * Do not fetch runtimes when getting list of all workspaces --- .../create-project.controller.ts | 2 +- .../workspace/server/WorkspaceManager.java | 4 +-- .../workspace/server/WorkspaceRuntimes.java | 17 ++++++++++ .../server/WorkspaceManagerTest.java | 5 +-- .../server/WorkspaceRuntimesTest.java | 33 +++++++++++++++++++ 5 files changed, 56 insertions(+), 5 deletions(-) diff --git a/dashboard/src/app/projects/create-project/create-project.controller.ts b/dashboard/src/app/projects/create-project/create-project.controller.ts index 93c11d80c42..ae2187c41bd 100755 --- a/dashboard/src/app/projects/create-project/create-project.controller.ts +++ b/dashboard/src/app/projects/create-project/create-project.controller.ts @@ -1046,7 +1046,7 @@ export class CreateProjectController { * @param workspace existing workspace */ checkExistingWorkspaceState(workspace: any): void { - if (workspace.runtime) { + if (workspace.status === 'RUNNING') { let websocketUrl = this.cheAPI.getWorkspace().getWebsocketUrl(workspace.id); // get bus let websocketStream = this.$websocket(websocketUrl); diff --git a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceManager.java b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceManager.java index 2af62993ea6..3f969082982 100644 --- a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceManager.java +++ b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceManager.java @@ -235,7 +235,7 @@ public List getWorkspaces(String user) throws ServerException { requireNonNull(user, "Required non-null user id"); final List workspaces = workspaceDao.getWorkspaces(user); for (WorkspaceImpl workspace : workspaces) { - normalizeState(workspace); + workspace.setStatus(runtimes.getStatus(workspace.getId())); } return workspaces; } @@ -258,7 +258,7 @@ public List getByNamespace(String namespace) throws ServerExcepti requireNonNull(namespace, "Required non-null namespace"); final List workspaces = workspaceDao.getByNamespace(namespace); for (WorkspaceImpl workspace : workspaces) { - normalizeState(workspace); + workspace.setStatus(runtimes.getStatus(workspace.getId())); } return workspaces; } diff --git a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceRuntimes.java b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceRuntimes.java index 6e104752bb7..40e53df8db1 100644 --- a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceRuntimes.java +++ b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceRuntimes.java @@ -536,6 +536,23 @@ public Map getWorkspaces() { return new HashMap<>(workspaces); } + /** + * Return status of the workspace. + * + * @param workspaceId + * ID of requested workspace + * @return workspace status + */ + public WorkspaceStatus getStatus(String workspaceId) { + try (@SuppressWarnings("unused") CloseableLock l = locks.acquireReadLock(workspaceId)) { + final WorkspaceState state = workspaces.get(workspaceId); + if (state == null) { + return WorkspaceStatus.STOPPED; + } + return state.status; + } + } + private MessageConsumer getEnvironmentLogger(String workspaceId) throws ServerException { WebsocketMessageConsumer envMessageConsumer = new WebsocketMessageConsumer<>(format(ENVIRONMENT_OUTPUT_CHANNEL_TEMPLATE, workspaceId)); diff --git a/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/workspace/server/WorkspaceManagerTest.java b/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/workspace/server/WorkspaceManagerTest.java index 1e766374091..2558e568619 100644 --- a/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/workspace/server/WorkspaceManagerTest.java +++ b/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/workspace/server/WorkspaceManagerTest.java @@ -214,8 +214,8 @@ public void shouldBeAbleToGetWorkspacesAvailableForUser() throws Exception { when(workspaceDao.getWorkspaces(NAMESPACE)).thenReturn(asList(workspace1, workspace2)); final RuntimeDescriptor descriptor = createDescriptor(workspace2, RUNNING); - when(runtimes.get(workspace2.getId())).thenReturn(descriptor); - when(runtimes.get(workspace1.getId())).thenThrow(new NotFoundException("no runtime")); + when(runtimes.getStatus(workspace2.getId())).thenReturn(RUNNING); + when(runtimes.getStatus(workspace1.getId())).thenReturn(STOPPED); // when final List result = workspaceManager.getWorkspaces(NAMESPACE); @@ -856,6 +856,7 @@ private RuntimeDescriptor createAndMockDescriptor(WorkspaceImpl workspace, Works throws ServerException, NotFoundException, ConflictException { RuntimeDescriptor descriptor = createDescriptor(workspace, status); when(runtimes.get(workspace.getId())).thenReturn(descriptor); + when(runtimes.getStatus(workspace.getId())).thenReturn(status); return descriptor; } diff --git a/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/workspace/server/WorkspaceRuntimesTest.java b/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/workspace/server/WorkspaceRuntimesTest.java index 83cf5402472..60b9a73bb26 100644 --- a/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/workspace/server/WorkspaceRuntimesTest.java +++ b/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/workspace/server/WorkspaceRuntimesTest.java @@ -63,6 +63,7 @@ import static java.util.Collections.singletonList; import static java.util.Collections.singletonMap; import static org.eclipse.che.api.core.model.workspace.WorkspaceStatus.RUNNING; +import static org.eclipse.che.api.core.model.workspace.WorkspaceStatus.STOPPED; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyBoolean; import static org.mockito.Matchers.anyString; @@ -519,6 +520,38 @@ public void shouldBeAbleToGetMachine() throws Exception { verify(envEngine).getMachine(WORKSPACE_ID, expected.getId()); } + @Test + public void shouldBeAbleToGetStatusOfRunningWorkspace() throws Exception { + // given + WorkspaceImpl workspace = createWorkspace(); + runtimes.start(workspace, + workspace.getConfig().getDefaultEnv(), + false); + + // when + WorkspaceStatus status = runtimes.getStatus(workspace.getId()); + + // then + assertEquals(status, RUNNING); + } + + + @Test + public void shouldBeAbleToGetStatusOfStoppedWorkspace() throws Exception { + // given + WorkspaceImpl workspace = createWorkspace(); + runtimes.start(workspace, + workspace.getConfig().getDefaultEnv(), + false); + runtimes.stop(workspace.getId()); + + // when + WorkspaceStatus status = runtimes.getStatus(workspace.getId()); + + // then + assertEquals(status, STOPPED); + } + @Test(expectedExceptions = NotFoundException.class, expectedExceptionsMessageRegExp = "test exception") public void shouldThrowExceptionIfGetMachineFromEnvEngineThrowsException() throws Exception {