-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
terraform-boot - Enable async processing of deploy and destroy API me…
…thods
- Loading branch information
1 parent
b353f65
commit 46b1975
Showing
12 changed files
with
354 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/java/org/eclipse/xpanse/terraform/boot/async/ServiceThreadPoolTaskExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* SPDX-FileCopyrightText: Huawei Inc. | ||
* | ||
*/ | ||
|
||
package org.eclipse.xpanse.terraform.boot.async; | ||
|
||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.Future; | ||
import org.slf4j.MDC; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
||
/** | ||
* Overwrite the thread pool to solve the problem that the traceId will be lost in the process of | ||
* printing the log. | ||
*/ | ||
public class ServiceThreadPoolTaskExecutor extends ThreadPoolTaskExecutor { | ||
|
||
public ServiceThreadPoolTaskExecutor() { | ||
super(); | ||
} | ||
|
||
@Override | ||
public void execute(Runnable task) { | ||
super.execute(ThreadMdcUtil.wrap(task, MDC.getCopyOfContextMap())); | ||
} | ||
|
||
|
||
@Override | ||
public <T> Future<T> submit(Callable<T> task) { | ||
return super.submit(ThreadMdcUtil.wrap(task, MDC.getCopyOfContextMap())); | ||
} | ||
|
||
@Override | ||
public Future<?> submit(Runnable task) { | ||
return super.submit(ThreadMdcUtil.wrap(task, MDC.getCopyOfContextMap())); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/org/eclipse/xpanse/terraform/boot/async/TaskConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* SPDX-FileCopyrightText: Huawei Inc. | ||
* | ||
*/ | ||
|
||
package org.eclipse.xpanse.terraform.boot.async; | ||
|
||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* Customize the thread pool. Define ThreadPoolTaskExecutor named taskExecutor to replace @Async's | ||
* default thread pool. | ||
*/ | ||
@Configuration | ||
public class TaskConfiguration { | ||
|
||
private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors(); | ||
|
||
/** | ||
* Define ThreadPoolTaskExecutor named taskExecutor. | ||
* | ||
* @return executor | ||
*/ | ||
@Bean("taskExecutor") | ||
public Executor taskExecutor() { | ||
ServiceThreadPoolTaskExecutor | ||
executor = new ServiceThreadPoolTaskExecutor(); | ||
executor.setCorePoolSize(CPU_COUNT * 2); | ||
executor.setMaxPoolSize(20); | ||
executor.setQueueCapacity(200); | ||
executor.setKeepAliveSeconds(300); | ||
executor.setThreadNamePrefix("thread-pool-"); | ||
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); | ||
executor.initialize(); | ||
return executor; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/org/eclipse/xpanse/terraform/boot/async/ThreadMdcUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* SPDX-FileCopyrightText: Huawei Inc. | ||
* | ||
*/ | ||
|
||
package org.eclipse.xpanse.terraform.boot.async; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.Callable; | ||
import org.slf4j.MDC; | ||
|
||
/** | ||
* Bean to get mdc logging info. | ||
*/ | ||
public final class ThreadMdcUtil { | ||
|
||
/** | ||
* When the parent thread submits a callable task to the thread pool, it copies the data in its | ||
* own MDC to the child thread. | ||
* | ||
* @param callable callable task | ||
* @param context context | ||
* @param <T> return object type | ||
* @return T | ||
*/ | ||
public static <T> Callable<T> wrap(final Callable<T> callable, | ||
final Map<String, String> context) { | ||
return new Callable<>() { | ||
@Override | ||
public T call() throws Exception { | ||
if (context == null) { | ||
MDC.clear(); | ||
} else { | ||
MDC.setContextMap(context); | ||
} | ||
try { | ||
return callable.call(); | ||
} finally { | ||
MDC.clear(); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* When the parent thread submits a runnable task to the thread pool, it copies the data in its | ||
* own MDC to the child thread. | ||
* | ||
* @param runnable runnable task | ||
* @param context context | ||
* @return thread task | ||
*/ | ||
public static Runnable wrap(final Runnable runnable, final Map<String, String> context) { | ||
return () -> { | ||
if (context == null) { | ||
MDC.clear(); | ||
} else { | ||
MDC.setContextMap(context); | ||
} | ||
try { | ||
runnable.run(); | ||
} finally { | ||
MDC.clear(); | ||
} | ||
}; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/org/eclipse/xpanse/terraform/boot/config/RestTemplateConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* SPDX-FileCopyrightText: Huawei Inc. | ||
*/ | ||
|
||
package org.eclipse.xpanse.terraform.boot.config; | ||
|
||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.client.ClientHttpRequestFactory; | ||
import org.springframework.http.client.SimpleClientHttpRequestFactory; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
/** | ||
* Configuration class for RestTemplate. | ||
*/ | ||
@Configuration | ||
public class RestTemplateConfig { | ||
|
||
/** | ||
* Create RestTemplate to IOC. | ||
*/ | ||
@Bean | ||
public RestTemplate restTemplate(ClientHttpRequestFactory factory) { | ||
return new RestTemplate(factory); | ||
} | ||
|
||
/** | ||
* Create ClientHttpRequestFactory to IOC. | ||
*/ | ||
@Bean | ||
public ClientHttpRequestFactory simpleClientHttpRequestFactory() { | ||
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); | ||
factory.setConnectTimeout(15000); | ||
factory.setReadTimeout(5000); | ||
return factory; | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/org/eclipse/xpanse/terraform/boot/models/enums/AuthType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* SPDX-FileCopyrightText: Huawei Inc. | ||
*/ | ||
|
||
package org.eclipse.xpanse.terraform.boot.models.enums; | ||
|
||
/** | ||
* The permission type class when calling back. | ||
*/ | ||
public enum AuthType { | ||
NONE, | ||
OAUTH2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...a/org/eclipse/xpanse/terraform/boot/models/request/async/TerraformAsyncDeployRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* SPDX-FileCopyrightText: Huawei Inc. | ||
*/ | ||
|
||
package org.eclipse.xpanse.terraform.boot.models.request.async; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Data; | ||
import org.eclipse.xpanse.terraform.boot.models.request.TerraformDeployRequest; | ||
|
||
/** | ||
* Data model for the terraform async deploy requests. | ||
*/ | ||
@Data | ||
public class TerraformAsyncDeployRequest extends TerraformDeployRequest { | ||
|
||
@NotNull | ||
@Schema(description = "Configuration information of webhook.") | ||
private WebhookConfig webhookConfig; | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
.../org/eclipse/xpanse/terraform/boot/models/request/async/TerraformAsyncDestroyRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* SPDX-FileCopyrightText: Huawei Inc. | ||
*/ | ||
|
||
package org.eclipse.xpanse.terraform.boot.models.request.async; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Data; | ||
import org.eclipse.xpanse.terraform.boot.models.request.TerraformDestroyRequest; | ||
|
||
/** | ||
* Data model for the terraform async destroy requests. | ||
*/ | ||
@Data | ||
public class TerraformAsyncDestroyRequest extends TerraformDestroyRequest { | ||
|
||
@NotNull | ||
@Schema(description = "Configuration information of webhook.") | ||
private WebhookConfig webhookConfig; | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/org/eclipse/xpanse/terraform/boot/models/request/async/WebhookConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* SPDX-FileCopyrightText: Huawei Inc. | ||
*/ | ||
|
||
package org.eclipse.xpanse.terraform.boot.models.request.async; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Data; | ||
import org.eclipse.xpanse.terraform.boot.models.enums.AuthType; | ||
|
||
/** | ||
* Configuration information class of webhook. | ||
*/ | ||
@Data | ||
public class WebhookConfig { | ||
|
||
@NotNull | ||
@Schema(description = "Callback address after deployment/destroy is completed.") | ||
private String url; | ||
|
||
@NotNull | ||
@Schema(description = "The permission type when calling back.") | ||
private AuthType authType; | ||
} |
Oops, something went wrong.