This is an automated email from the ASF dual-hosted git repository. bhliva pushed a commit to branch develop in repository https://gitbox.apache.org/repos/asf/incubator-dlab.git
commit 5b7ab4235ecdf7569f572502a1ea1d41ff985365 Author: Oleh Fuks <olegfuk...@gmail.com> AuthorDate: Tue Nov 12 14:13:28 2019 +0200 [DLAB-1256] Fixed issue with stopping notebook --- .../schedulers/CheckUserQuoteScheduler.java | 2 +- .../backendapi/service/EnvironmentService.java | 2 ++ .../dlab/backendapi/service/SecurityService.java | 4 ++- .../backendapi/service/SecurityServiceImpl.java | 5 ++-- .../service/impl/EnvironmentServiceImpl.java | 20 ++++++++++++-- .../service/impl/SchedulerJobServiceImpl.java | 10 +++---- .../schedulers/CheckUserQuoteSchedulerTest.java | 4 +-- .../service/impl/EnvironmentServiceImplTest.java | 17 +++++------- .../service/impl/SchedulerJobServiceImplTest.java | 32 +++++++++++----------- 9 files changed, 56 insertions(+), 40 deletions(-) diff --git a/services/self-service/src/main/java/com/epam/dlab/backendapi/schedulers/CheckUserQuoteScheduler.java b/services/self-service/src/main/java/com/epam/dlab/backendapi/schedulers/CheckUserQuoteScheduler.java index ce3b009..1e2e7f4 100644 --- a/services/self-service/src/main/java/com/epam/dlab/backendapi/schedulers/CheckUserQuoteScheduler.java +++ b/services/self-service/src/main/java/com/epam/dlab/backendapi/schedulers/CheckUserQuoteScheduler.java @@ -44,6 +44,6 @@ public class CheckUserQuoteScheduler implements Job { .map(UserDTO::getName) .filter(billingDAO::isUserQuoteReached) .peek(u -> log.warn("Stopping {} user env because of reaching user billing quote", u)) - .forEach(environmentService::stopEnvironment); + .forEach(environmentService::stopEnvironmentWithServiceAccount); } } diff --git a/services/self-service/src/main/java/com/epam/dlab/backendapi/service/EnvironmentService.java b/services/self-service/src/main/java/com/epam/dlab/backendapi/service/EnvironmentService.java index e36d936..65953cd 100644 --- a/services/self-service/src/main/java/com/epam/dlab/backendapi/service/EnvironmentService.java +++ b/services/self-service/src/main/java/com/epam/dlab/backendapi/service/EnvironmentService.java @@ -37,6 +37,8 @@ public interface EnvironmentService { void stopEnvironment(String user); + void stopEnvironmentWithServiceAccount(String user); + void stopProjectEnvironment(String project); void stopEdge(String user); diff --git a/services/self-service/src/main/java/com/epam/dlab/backendapi/service/SecurityService.java b/services/self-service/src/main/java/com/epam/dlab/backendapi/service/SecurityService.java index 9954cff..fb1179f 100644 --- a/services/self-service/src/main/java/com/epam/dlab/backendapi/service/SecurityService.java +++ b/services/self-service/src/main/java/com/epam/dlab/backendapi/service/SecurityService.java @@ -4,6 +4,8 @@ import com.epam.dlab.auth.UserInfo; public interface SecurityService { UserInfo getUserInfo(String code); + UserInfo getUserInfoOffline(String username); - UserInfo getServiceAccountInfo(String username); + + UserInfo getServiceAccountInfo(); } diff --git a/services/self-service/src/main/java/com/epam/dlab/backendapi/service/SecurityServiceImpl.java b/services/self-service/src/main/java/com/epam/dlab/backendapi/service/SecurityServiceImpl.java index b43128d..b62e3fc 100644 --- a/services/self-service/src/main/java/com/epam/dlab/backendapi/service/SecurityServiceImpl.java +++ b/services/self-service/src/main/java/com/epam/dlab/backendapi/service/SecurityServiceImpl.java @@ -38,8 +38,9 @@ public class SecurityServiceImpl implements SecurityService { } @Override - public UserInfo getServiceAccountInfo(String username) { + public UserInfo getServiceAccountInfo() { AccessTokenResponse accessTokenResponse = keycloakService.generateServiceAccountToken(); - return new UserInfo(username, accessTokenResponse.getToken()); + return new UserInfo(KeycloakUtil.parseToken(accessTokenResponse.getToken()).getPreferredUsername(), + accessTokenResponse.getToken()); } } diff --git a/services/self-service/src/main/java/com/epam/dlab/backendapi/service/impl/EnvironmentServiceImpl.java b/services/self-service/src/main/java/com/epam/dlab/backendapi/service/impl/EnvironmentServiceImpl.java index 5737497..f062c7f 100644 --- a/services/self-service/src/main/java/com/epam/dlab/backendapi/service/impl/EnvironmentServiceImpl.java +++ b/services/self-service/src/main/java/com/epam/dlab/backendapi/service/impl/EnvironmentServiceImpl.java @@ -122,15 +122,24 @@ public class EnvironmentServiceImpl implements EnvironmentService { } @Override + public void stopEnvironmentWithServiceAccount(String user) { + log.debug("Stopping environment for user {} by scheduler", user); + checkState(user, "stop"); + exploratoryDAO.fetchRunningExploratoryFields(user) + .forEach(this::stopNotebookWithServiceAccount); + stopEdge(user); + } + + @Override public void stopProjectEnvironment(String project) { log.debug("Stopping environment for project {}", project); checkProjectResourceConditions(project, "stop"); exploratoryDAO.fetchRunningExploratoryFieldsForProject(project) - .forEach(this::stopNotebook); + .forEach(this::stopNotebookWithServiceAccount); projectService.get(project).getEndpoints().stream() .filter(e -> UserInstanceStatus.RUNNING == e.getStatus()) - .forEach(endpoint -> projectService.stop(securityService.getServiceAccountInfo("admin"), + .forEach(endpoint -> projectService.stop(securityService.getServiceAccountInfo(), endpoint.getName(), project)); } @@ -196,7 +205,12 @@ public class EnvironmentServiceImpl implements EnvironmentService { } private void stopNotebook(UserInstanceDTO instance) { - final UserInfo userInfo = securityService.getServiceAccountInfo(instance.getUser()); + final UserInfo userInfo = securityService.getUserInfoOffline(instance.getUser()); + exploratoryService.stop(userInfo, instance.getExploratoryName()); + } + + private void stopNotebookWithServiceAccount(UserInstanceDTO instance) { + final UserInfo userInfo = securityService.getServiceAccountInfo(); exploratoryService.stop(userInfo, instance.getExploratoryName()); } diff --git a/services/self-service/src/main/java/com/epam/dlab/backendapi/service/impl/SchedulerJobServiceImpl.java b/services/self-service/src/main/java/com/epam/dlab/backendapi/service/impl/SchedulerJobServiceImpl.java index 7f3d3d8..2d67665 100644 --- a/services/self-service/src/main/java/com/epam/dlab/backendapi/service/impl/SchedulerJobServiceImpl.java +++ b/services/self-service/src/main/java/com/epam/dlab/backendapi/service/impl/SchedulerJobServiceImpl.java @@ -207,14 +207,14 @@ public class SchedulerJobServiceImpl implements SchedulerJobService { final String compName = job.getComputationalName(); final String user = job.getUser(); log.debug("Stopping exploratory {} computational {} for user {} by scheduler", expName, compName, user); - computationalService.stopSparkCluster(securityService.getServiceAccountInfo(job.getUser()), expName, compName); + computationalService.stopSparkCluster(securityService.getServiceAccountInfo(), expName, compName); } private void terminateComputational(SchedulerJobData job) { final String user = job.getUser(); final String expName = job.getExploratoryName(); final String compName = job.getComputationalName(); - final UserInfo userInfo = securityService.getServiceAccountInfo(user); + final UserInfo userInfo = securityService.getServiceAccountInfo(); log.debug("Terminating exploratory {} computational {} for user {} by scheduler", expName, compName, user); computationalService.terminateComputational(userInfo, expName, compName); } @@ -223,7 +223,7 @@ public class SchedulerJobServiceImpl implements SchedulerJobService { final String expName = job.getExploratoryName(); final String user = job.getUser(); log.debug("Stopping exploratory {} for user {} by scheduler", expName, user); - exploratoryService.stop(securityService.getServiceAccountInfo(user), expName); + exploratoryService.stop(securityService.getServiceAccountInfo(), expName); } private List<SchedulerJobData> getExploratorySchedulersForTerminating(OffsetDateTime now) { @@ -245,7 +245,7 @@ public class SchedulerJobServiceImpl implements SchedulerJobService { final String exploratoryName = schedulerJobData.getExploratoryName(); final String project = schedulerJobData.getProject(); log.debug("Starting exploratory {} for user {} by scheduler", exploratoryName, user); - exploratoryService.start(securityService.getServiceAccountInfo(user), exploratoryName, project); + exploratoryService.start(securityService.getServiceAccountInfo(), exploratoryName, project); if (schedulerJobData.getJobDTO().isSyncStartRequired()) { log.trace("Starting computational for exploratory {} for user {} by scheduler", exploratoryName, user); final DataEngineType sparkCluster = DataEngineType.SPARK_STANDALONE; @@ -268,7 +268,7 @@ public class SchedulerJobServiceImpl implements SchedulerJobService { private void startSpark(String user, String expName, String compName, String project) { log.debug("Starting exploratory {} computational {} for user {} by scheduler", expName, compName, user); - computationalService.startSparkCluster(securityService.getServiceAccountInfo(user), expName, compName, project); + computationalService.startSparkCluster(securityService.getServiceAccountInfo(), expName, compName, project); } private boolean shouldClusterBeStarted(DataEngineType sparkCluster, UserComputationalResource compResource) { diff --git a/services/self-service/src/test/java/com/epam/dlab/backendapi/schedulers/CheckUserQuoteSchedulerTest.java b/services/self-service/src/test/java/com/epam/dlab/backendapi/schedulers/CheckUserQuoteSchedulerTest.java index d858573..1c6937c 100644 --- a/services/self-service/src/test/java/com/epam/dlab/backendapi/schedulers/CheckUserQuoteSchedulerTest.java +++ b/services/self-service/src/test/java/com/epam/dlab/backendapi/schedulers/CheckUserQuoteSchedulerTest.java @@ -56,7 +56,7 @@ public class CheckUserQuoteSchedulerTest { verify(environmentService).getUsers(); verify(billingDAO).isUserQuoteReached(USER); - verify(environmentService).stopEnvironment(USER); + verify(environmentService).stopEnvironmentWithServiceAccount(USER); verifyNoMoreInteractions(environmentService, billingDAO); verifyZeroInteractions(jobExecutionContext); } @@ -70,7 +70,7 @@ public class CheckUserQuoteSchedulerTest { verify(environmentService).getUsers(); verify(billingDAO).isUserQuoteReached(USER); - verify(environmentService, never()).stopEnvironment(anyString()); + verify(environmentService, never()).stopEnvironmentWithServiceAccount(anyString()); verifyNoMoreInteractions(environmentService, billingDAO); verifyZeroInteractions(jobExecutionContext); } diff --git a/services/self-service/src/test/java/com/epam/dlab/backendapi/service/impl/EnvironmentServiceImplTest.java b/services/self-service/src/test/java/com/epam/dlab/backendapi/service/impl/EnvironmentServiceImplTest.java index 212cdf7..3344ad3 100644 --- a/services/self-service/src/test/java/com/epam/dlab/backendapi/service/impl/EnvironmentServiceImplTest.java +++ b/services/self-service/src/test/java/com/epam/dlab/backendapi/service/impl/EnvironmentServiceImplTest.java @@ -146,7 +146,6 @@ public class EnvironmentServiceImplTest { final UserInfo userInfo = getUserInfo(); when(exploratoryDAO.fetchRunningExploratoryFields(anyString())).thenReturn(getUserInstances()); when(securityService.getUserInfoOffline(anyString())).thenReturn(userInfo); - when(securityService.getServiceAccountInfo(anyString())).thenReturn(userInfo); when(exploratoryService.stop(any(UserInfo.class), anyString())).thenReturn(UUID); when(keyDAO.getEdgeStatus(anyString())).thenReturn(RUNNING_STATE); when(edgeService.stop(any(UserInfo.class))).thenReturn(UUID); @@ -154,8 +153,7 @@ public class EnvironmentServiceImplTest { environmentService.stopEnvironment(USER); verify(exploratoryDAO).fetchRunningExploratoryFields(USER); - verify(securityService, times(1)).getUserInfoOffline(USER); - verify(securityService, times(2)).getServiceAccountInfo(USER); + verify(securityService, times(3)).getUserInfoOffline(USER); verify(exploratoryService).stop(refEq(userInfo), eq(EXPLORATORY_NAME_1)); verify(exploratoryService).stop(refEq(userInfo), eq(EXPLORATORY_NAME_2)); verify(keyDAO, times(2)).getEdgeStatus(USER); @@ -189,7 +187,7 @@ public class EnvironmentServiceImplTest { public void stopEnvironmentWithoutEdge() { final UserInfo userInfo = getUserInfo(); when(exploratoryDAO.fetchRunningExploratoryFields(anyString())).thenReturn(getUserInstances()); - when(securityService.getServiceAccountInfo(anyString())).thenReturn(userInfo); + when(securityService.getUserInfoOffline(anyString())).thenReturn(userInfo); when(exploratoryService.stop(any(UserInfo.class), anyString())).thenReturn(UUID); when(keyDAO.getEdgeStatus(anyString())).thenReturn(STOPPED_STATE); when(edgeService.stop(any(UserInfo.class))).thenReturn(UUID); @@ -197,7 +195,7 @@ public class EnvironmentServiceImplTest { environmentService.stopEnvironment(USER); verify(exploratoryDAO).fetchRunningExploratoryFields(USER); - verify(securityService, times(2)).getServiceAccountInfo(USER); + verify(securityService, times(2)).getUserInfoOffline(USER); verify(exploratoryService).stop(refEq(userInfo), eq(EXPLORATORY_NAME_1)); verify(exploratoryService).stop(refEq(userInfo), eq(EXPLORATORY_NAME_2)); verify(keyDAO, times(2)).getEdgeStatus(USER); @@ -213,7 +211,7 @@ public class EnvironmentServiceImplTest { final UserInfo userInfo = getUserInfo(); final ProjectDTO projectDTO = getProjectDTO(); when(exploratoryDAO.fetchRunningExploratoryFieldsForProject(anyString())).thenReturn(getUserInstances()); - when(securityService.getServiceAccountInfo(anyString())).thenReturn(userInfo); + when(securityService.getServiceAccountInfo()).thenReturn(userInfo); when(exploratoryService.stop(any(UserInfo.class), anyString())).thenReturn(UUID); when(projectService.get(anyString())).thenReturn(projectDTO); doNothing().when(projectService).stop(any(UserInfo.class), anyString(), anyString()); @@ -223,8 +221,7 @@ public class EnvironmentServiceImplTest { verify(exploratoryDAO).fetchRunningExploratoryFieldsForProject(PROJECT_NAME); verify(exploratoryService).stop(refEq(userInfo), eq(EXPLORATORY_NAME_1)); verify(exploratoryService).stop(refEq(userInfo), eq(EXPLORATORY_NAME_2)); - verify(securityService, times(2)).getServiceAccountInfo(USER); - verify(securityService).getServiceAccountInfo(ADMIN); + verify(securityService, times(3)).getServiceAccountInfo(); verify(projectService).get(eq(PROJECT_NAME)); verify(projectService).stop(refEq(userInfo), eq(ENDPOINT_NAME), eq(PROJECT_NAME)); verify(exploratoryDAO).fetchProjectExploratoriesWhereStatusIn(PROJECT_NAME, Arrays.asList(UserInstanceStatus.CREATING, @@ -262,12 +259,12 @@ public class EnvironmentServiceImplTest { @Test public void stopExploratory() { final UserInfo userInfo = getUserInfo(); - when(securityService.getServiceAccountInfo(anyString())).thenReturn(userInfo); + when(securityService.getUserInfoOffline(anyString())).thenReturn(userInfo); when(exploratoryService.stop(any(UserInfo.class), anyString())).thenReturn(UUID); environmentService.stopExploratory(USER, EXPLORATORY_NAME_1); - verify(securityService).getServiceAccountInfo(USER); + verify(securityService).getUserInfoOffline(USER); verify(exploratoryService).stop(refEq(userInfo), eq(EXPLORATORY_NAME_1)); verifyNoMoreInteractions(securityService, exploratoryService); } diff --git a/services/self-service/src/test/java/com/epam/dlab/backendapi/service/impl/SchedulerJobServiceImplTest.java b/services/self-service/src/test/java/com/epam/dlab/backendapi/service/impl/SchedulerJobServiceImplTest.java index 8a7d8ec..0cd0cd3 100644 --- a/services/self-service/src/test/java/com/epam/dlab/backendapi/service/impl/SchedulerJobServiceImplTest.java +++ b/services/self-service/src/test/java/com/epam/dlab/backendapi/service/impl/SchedulerJobServiceImplTest.java @@ -392,11 +392,11 @@ public class SchedulerJobServiceImplTest { LocalDateTime.of(LocalDate.now(), LocalTime.now().truncatedTo(ChronoUnit.MINUTES)), false, USER, LocalTime.now().truncatedTo(ChronoUnit.MINUTES)))); - when(securityService.getServiceAccountInfo(anyString())).thenReturn(getUserInfo()); + when(securityService.getServiceAccountInfo()).thenReturn(getUserInfo()); schedulerJobService.startComputationalByScheduler(); - verify(securityService).getServiceAccountInfo(USER); + verify(securityService).getServiceAccountInfo(); verify(schedulerJobDAO) .getComputationalSchedulerDataWithOneOfStatus(RUNNING, DataEngineType.SPARK_STANDALONE, STOPPED); verify(computationalService).startSparkCluster(refEq(getUserInfo()), eq(EXPLORATORY_NAME), @@ -492,11 +492,11 @@ public class SchedulerJobServiceImplTest { LocalDateTime.of(LocalDate.now(), LocalTime.now().truncatedTo(ChronoUnit.MINUTES)), false, USER, LocalTime.now().truncatedTo(ChronoUnit.MINUTES)))); - when(securityService.getServiceAccountInfo(anyString())).thenReturn(getUserInfo()); + when(securityService.getServiceAccountInfo()).thenReturn(getUserInfo()); schedulerJobService.stopComputationalByScheduler(); - verify(securityService).getServiceAccountInfo(USER); + verify(securityService).getServiceAccountInfo(); verify(schedulerJobDAO) .getComputationalSchedulerDataWithOneOfStatus(RUNNING, DataEngineType.SPARK_STANDALONE, RUNNING); verify(computationalService).stopSparkCluster(refEq(getUserInfo()), eq(EXPLORATORY_NAME), @@ -590,11 +590,11 @@ public class SchedulerJobServiceImplTest { LocalDateTime.of(LocalDate.now(), LocalTime.now().truncatedTo(ChronoUnit.MINUTES)), false, USER, LocalTime.now().truncatedTo(ChronoUnit.MINUTES)))); - when(securityService.getServiceAccountInfo(anyString())).thenReturn(getUserInfo()); + when(securityService.getServiceAccountInfo()).thenReturn(getUserInfo()); schedulerJobService.stopExploratoryByScheduler(); - verify(securityService).getServiceAccountInfo(USER); + verify(securityService).getServiceAccountInfo(); verify(schedulerJobDAO).getExploratorySchedulerWithStatusAndClusterLastActivityLessThan(eq(RUNNING), any(Date.class)); verify(exploratoryService).stop(refEq(getUserInfo()), eq(EXPLORATORY_NAME)); @@ -686,11 +686,11 @@ public class SchedulerJobServiceImplTest { LocalTime.now().truncatedTo(ChronoUnit.MINUTES)), false, USER, LocalTime.now().truncatedTo(ChronoUnit.MINUTES) ))); - when(securityService.getServiceAccountInfo(anyString())).thenReturn(getUserInfo()); + when(securityService.getServiceAccountInfo()).thenReturn(getUserInfo()); schedulerJobService.startExploratoryByScheduler(); - verify(securityService).getServiceAccountInfo(USER); + verify(securityService).getServiceAccountInfo(); verify(schedulerJobDAO).getExploratorySchedulerDataWithStatus(STOPPED); verify(exploratoryService).start(refEq(getUserInfo()), eq(EXPLORATORY_NAME), eq(PROJECT)); verifyNoMoreInteractions(securityService, schedulerJobDAO, exploratoryService); @@ -709,14 +709,14 @@ public class SchedulerJobServiceImplTest { LocalTime.now().truncatedTo(ChronoUnit.MINUTES)), true, USER, LocalTime.now().truncatedTo(ChronoUnit.MINUTES) ))); - when(securityService.getServiceAccountInfo(anyString())).thenReturn(getUserInfo()); + when(securityService.getServiceAccountInfo()).thenReturn(getUserInfo()); when(computationalDAO.findComputationalResourcesWithStatus(anyString(), anyString(), any(UserInstanceStatus.class))).thenReturn(singletonList(getComputationalResource( DataEngineType.SPARK_STANDALONE, true))); schedulerJobService.startExploratoryByScheduler(); - verify(securityService, times(2)).getServiceAccountInfo(USER); + verify(securityService, times(2)).getServiceAccountInfo(); verify(schedulerJobDAO).getExploratorySchedulerDataWithStatus(STOPPED); verify(exploratoryService).start(refEq(getUserInfo()), eq(EXPLORATORY_NAME), eq(PROJECT)); verify(computationalDAO).findComputationalResourcesWithStatus(USER, EXPLORATORY_NAME, STOPPED); @@ -736,14 +736,14 @@ public class SchedulerJobServiceImplTest { LocalTime.now().truncatedTo(ChronoUnit.MINUTES)), true, USER, LocalTime.now().truncatedTo(ChronoUnit.MINUTES) ))); - when(securityService.getServiceAccountInfo(anyString())).thenReturn(getUserInfo()); + when(securityService.getServiceAccountInfo()).thenReturn(getUserInfo()); when(computationalDAO.findComputationalResourcesWithStatus(anyString(), anyString(), any(UserInstanceStatus.class))).thenReturn(singletonList(getComputationalResource( DataEngineType.CLOUD_SERVICE, true))); schedulerJobService.startExploratoryByScheduler(); - verify(securityService).getServiceAccountInfo(USER); + verify(securityService).getServiceAccountInfo(); verify(schedulerJobDAO).getExploratorySchedulerDataWithStatus(STOPPED); verify(exploratoryService).start(refEq(getUserInfo()), eq(EXPLORATORY_NAME), eq(PROJECT)); verify(computationalDAO).findComputationalResourcesWithStatus(USER, EXPLORATORY_NAME, STOPPED); @@ -761,14 +761,14 @@ public class SchedulerJobServiceImplTest { LocalTime.now().truncatedTo(ChronoUnit.MINUTES)), true, USER, LocalTime.now().truncatedTo(ChronoUnit.MINUTES) ))); - when(securityService.getServiceAccountInfo(anyString())).thenReturn(getUserInfo()); + when(securityService.getServiceAccountInfo()).thenReturn(getUserInfo()); when(computationalDAO.findComputationalResourcesWithStatus(anyString(), anyString(), any(UserInstanceStatus.class))).thenReturn(singletonList(getComputationalResource( DataEngineType.SPARK_STANDALONE, false))); schedulerJobService.startExploratoryByScheduler(); - verify(securityService).getServiceAccountInfo(USER); + verify(securityService).getServiceAccountInfo(); verify(schedulerJobDAO).getExploratorySchedulerDataWithStatus(STOPPED); verify(exploratoryService).start(refEq(getUserInfo()), eq(EXPLORATORY_NAME), eq(PROJECT)); verify(computationalDAO).findComputationalResourcesWithStatus(USER, EXPLORATORY_NAME, STOPPED); @@ -855,11 +855,11 @@ public class SchedulerJobServiceImplTest { ); when(schedulerJobDAO.getComputationalSchedulerDataWithOneOfStatus(any(UserInstanceStatus.class), anyVararg())).thenReturn(singletonList(schedulerJobData)); - when(securityService.getServiceAccountInfo(anyString())).thenReturn(getUserInfo()); + when(securityService.getServiceAccountInfo()).thenReturn(getUserInfo()); schedulerJobService.terminateComputationalByScheduler(); - verify(securityService).getServiceAccountInfo(USER); + verify(securityService).getServiceAccountInfo(); verify(schedulerJobDAO) .getComputationalSchedulerDataWithOneOfStatus(RUNNING, STOPPED, RUNNING); verify(computationalService).terminateComputational(refEq(getUserInfo()), eq(EXPLORATORY_NAME), --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@dlab.apache.org For additional commands, e-mail: commits-h...@dlab.apache.org