Skip to content

Commit

Permalink
remove redundant async threads in migration process
Browse files Browse the repository at this point in the history
  • Loading branch information
WangLiNaruto committed Jan 23, 2024
1 parent 4523c34 commit 2029878
Show file tree
Hide file tree
Showing 36 changed files with 1,182 additions and 565 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -35,11 +33,8 @@
import org.eclipse.xpanse.modules.models.service.view.DeployedService;
import org.eclipse.xpanse.modules.models.service.view.DeployedServiceDetails;
import org.eclipse.xpanse.modules.models.service.view.VendorHostedDeployedServiceDetails;
import org.eclipse.xpanse.modules.models.workflow.migrate.MigrateRequest;
import org.eclipse.xpanse.modules.orchestrator.deployment.DeployTask;
import org.eclipse.xpanse.modules.security.IdentityProviderManager;
import org.eclipse.xpanse.modules.workflow.consts.MigrateConstants;
import org.eclipse.xpanse.modules.workflow.utils.WorkflowProcessUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.access.AccessDeniedException;
Expand Down Expand Up @@ -72,9 +67,6 @@ public class ServiceDeployerApi {
@Resource
private IdentityProviderManager identityProviderManager;

@Resource
private WorkflowProcessUtils workflowProcessUtils;

@Resource
private ServiceDetailsViewManager serviceDetailsViewManager;

Expand Down Expand Up @@ -223,32 +215,4 @@ public Response purge(@PathVariable("id") String id) {
return Response.successResponse(Collections.singletonList(successMsg));
}

/**
* Create a job to migrate the deployed service.
*
* @return response
*/
@Tag(name = "Service", description = "APIs to manage the service instances")
@Operation(description = "Create a job to migrate the deployed service.")
@PostMapping(value = "/services/migration", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.ACCEPTED)
public UUID migrate(@Valid @RequestBody MigrateRequest migrateRequest) {

DeployServiceEntity deployServiceEntity =
this.deployServiceEntityHandler.getDeployServiceEntity(migrateRequest.getId());
Optional<String> userIdOptional = identityProviderManager.getCurrentLoginUserId();
String userId = userIdOptional.orElse(null);
if (!StringUtils.equals(userId, deployServiceEntity.getUserId())) {
throw new AccessDeniedException(
"No permissions to migrate services belonging to other users.");
}
UUID newId = UUID.randomUUID();
Map<String, Object> variable = new HashMap<>();
variable.put(MigrateConstants.ID, migrateRequest.getId());
variable.put(MigrateConstants.NEW_ID, newId);
variable.put(MigrateConstants.MIGRATE_REQUEST, migrateRequest);
variable.put(MigrateConstants.USER_ID, userId);
workflowProcessUtils.asyncStartProcess(MigrateConstants.PROCESS_KEY, variable);
return newId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: Huawei Inc.
*
*/

package org.eclipse.xpanse.api.controllers;


import static org.eclipse.xpanse.modules.security.common.RoleConstants.ROLE_ADMIN;
import static org.eclipse.xpanse.modules.security.common.RoleConstants.ROLE_USER;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.runtime.ProcessInstance;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.xpanse.modules.database.service.DeployServiceEntity;
import org.eclipse.xpanse.modules.deployment.DeployServiceEntityHandler;
import org.eclipse.xpanse.modules.deployment.migration.consts.MigrateConstants;
import org.eclipse.xpanse.modules.models.workflow.migrate.MigrateRequest;
import org.eclipse.xpanse.modules.security.IdentityProviderManager;
import org.eclipse.xpanse.modules.workflow.utils.WorkflowUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;


/**
* REST interface methods for Service Migration.
*/
@Slf4j
@RestController
@RequestMapping("/xpanse")
@CrossOrigin
@Secured({ROLE_ADMIN, ROLE_USER})
public class ServiceMigrationApi {

@Resource
private DeployServiceEntityHandler deployServiceEntityHandler;

@Resource
private IdentityProviderManager identityProviderManager;

@Resource
private WorkflowUtils workflowUtils;

/**
* Create a job to migrate the deployed service.
*
* @return response
*/
@Tag(name = "Migration", description = "APIs to manage the service migration.")
@Operation(description = "Create a job to migrate the deployed service.")
@PostMapping(value = "/services/migration", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.ACCEPTED)
public UUID migrate(@Valid @RequestBody MigrateRequest migrateRequest) {
String userId = getUserId(migrateRequest.getId());
Map<String, Object> variable =
getMigrateProcessVariable(migrateRequest, UUID.randomUUID(), userId);
ProcessInstance instance =
workflowUtils.startProcess(MigrateConstants.PROCESS_KEY, variable);
return UUID.fromString(instance.getProcessInstanceId());
}

private String getUserId(UUID migrationId) {
DeployServiceEntity deployServiceEntity =
this.deployServiceEntityHandler.getDeployServiceEntity(migrationId);
Optional<String> userIdOptional = identityProviderManager.getCurrentLoginUserId();
String userId = userIdOptional.orElse(null);
if (!StringUtils.equals(userId, deployServiceEntity.getUserId())) {
throw new AccessDeniedException(
"No permissions to migrate services belonging to other users.");
}
return userId;
}

private Map<String, Object> getMigrateProcessVariable(MigrateRequest migrateRequest,
UUID newServiceId, String userId) {
Map<String, Object> variable = new HashMap<>();
variable.put(MigrateConstants.ID, migrateRequest.getId());
variable.put(MigrateConstants.NEW_ID, newServiceId);
variable.put(MigrateConstants.MIGRATE_REQUEST, migrateRequest);
variable.put(MigrateConstants.USER_ID, userId);
return variable;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public class TerraformBootWebhookApi {
@ResponseStatus(HttpStatus.OK)
public void deployCallback(
@Parameter(name = "task_id", description = "task id")
@PathVariable("task_id") String taskId, @Valid @RequestBody TerraformResult result) {
@PathVariable("task_id") String taskId,
@Valid @RequestBody TerraformResult result) {

terraformDeploymentResultCallbackManager.deployCallback(taskId, result);
}
Expand All @@ -65,8 +66,10 @@ public void deployCallback(
MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public void destroyCallback(
@Parameter(name = "task_id", description = "task id") @PathVariable("task_id")
String taskId, @Valid @RequestBody TerraformResult result) {
@Parameter(name = "task_id", description = "task id")
@PathVariable("task_id") String taskId,
@Valid @RequestBody TerraformResult result) {

terraformDeploymentResultCallbackManager.destroyCallback(taskId, result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
import java.util.Map;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.xpanse.modules.deployment.migration.consts.MigrateConstants;
import org.eclipse.xpanse.modules.models.workflow.WorkFlowTask;
import org.eclipse.xpanse.modules.security.IdentityProviderManager;
import org.eclipse.xpanse.modules.workflow.consts.MigrateConstants;
import org.eclipse.xpanse.modules.workflow.utils.WorkflowProcessUtils;
import org.eclipse.xpanse.modules.workflow.utils.WorkflowUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.access.annotation.Secured;
Expand All @@ -45,7 +45,7 @@
public class WorkFlowApi {

@Resource
private WorkflowProcessUtils workflowProcessUtils;
private WorkflowUtils workflowUtils;

@Resource
private IdentityProviderManager identityProviderManager;
Expand All @@ -59,7 +59,7 @@ public class WorkFlowApi {
@ResponseStatus(HttpStatus.OK)
public List<WorkFlowTask> queryTodoTasks() {
Optional<String> userIdOptional = identityProviderManager.getCurrentLoginUserId();
return workflowProcessUtils.todoTasks(userIdOptional.orElse(null));
return workflowUtils.todoTasks(userIdOptional.orElse(null));
}

/**
Expand All @@ -71,7 +71,7 @@ public List<WorkFlowTask> queryTodoTasks() {
@ResponseStatus(HttpStatus.OK)
public List<WorkFlowTask> queryDoneTasks() {
Optional<String> userIdOptional = identityProviderManager.getCurrentLoginUserId();
return workflowProcessUtils.doneTasks(userIdOptional.orElse(null));
return workflowUtils.doneTasks(userIdOptional.orElse(null));
}

/**
Expand All @@ -86,7 +86,7 @@ public void completeTask(
description = "ID of the workflow task that needs to be handled")
@PathVariable("id") String taskId,
@RequestBody Map<String, Object> variables) {
workflowProcessUtils.completeTask(taskId, variables);
workflowUtils.completeTask(taskId, variables);
}

/**
Expand All @@ -106,7 +106,7 @@ public void manageFailedOrder(
@PathVariable boolean retryOrder) {
Map<String, Object> variables = new HashMap<>();
variables.put(MigrateConstants.IS_RETRY_TASK, retryOrder);
workflowProcessUtils.completeTask(taskId, variables);
workflowUtils.completeTask(taskId, variables);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: Huawei Inc.
*/


package org.eclipse.xpanse.api.exceptions.handler;


import java.util.Collections;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.xpanse.modules.models.response.Response;
import org.eclipse.xpanse.modules.models.response.ResultType;
import org.eclipse.xpanse.modules.models.workflow.migrate.exceptions.ServiceMigrationFailedException;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
* Exception handler related to service migration.
*/
@Slf4j
@Order(Ordered.HIGHEST_PRECEDENCE)
@RestControllerAdvice
public class ServiceMigrationExceptionHandler {

/**
* Exception handler for ServiceMigrationFailedException.
*/
@ExceptionHandler({ServiceMigrationFailedException.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public Response handleServiceNotMigrationException(
ServiceMigrationFailedException ex) {
return Response.errorResponse(ResultType.SERVICE_MIGRATION_FAILED_EXCEPTION,
Collections.singletonList(ex.getMessage()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.eclipse.xpanse.modules.models.service.deploy.exceptions.TerraformProviderNotFoundException;
import org.eclipse.xpanse.modules.models.service.deploy.exceptions.VariableInvalidException;
import org.eclipse.xpanse.modules.security.IdentityProviderManager;
import org.eclipse.xpanse.modules.workflow.utils.WorkflowProcessUtils;
import org.eclipse.xpanse.modules.workflow.utils.WorkflowUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -43,7 +43,7 @@

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {ServiceDeployerApi.class, DeployService.class,
WorkflowProcessUtils.class, DeploymentExceptionHandler.class, IdentityProviderManager.class})
WorkflowUtils.class, DeploymentExceptionHandler.class, IdentityProviderManager.class})
@WebMvcTest
class DeploymentExceptionHandlerTest {

Expand All @@ -54,7 +54,7 @@ class DeploymentExceptionHandlerTest {
private DeployService deployService;

@MockBean
private WorkflowProcessUtils workflowProcessUtils;
private WorkflowUtils workflowUtils;

@MockBean
private DeployerKindManager deployerKindManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
* Implementation of the ServiceMigrationStorage.
*/
@Component
@Transactional
public class DatabaseServiceMigrationStorage implements ServiceMigrationStorage {

private final ServiceMigrationRepository serviceMigrationRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import java.util.UUID;
Expand Down Expand Up @@ -35,6 +37,7 @@ public class ServiceMigrationEntity extends CreateModifiedTime {
@Column(name = "NEW_SERVICE_ID", nullable = false)
private UUID newServiceId;

@Enumerated(EnumType.STRING)
@Column(name = "MIGRATION_STATUS")
private MigrationStatus migrationStatus;

Expand Down
5 changes: 5 additions & 0 deletions modules/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@
<artifactId>async</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.xpanse.modules</groupId>
<artifactId>workflow</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

<build>
Expand Down

0 comments on commit 2029878

Please sign in to comment.