From 0f53ec967c5e24189748045520a168839a3ff945 Mon Sep 17 00:00:00 2001 From: WangLiNaruto <122504203+WangLiNaruto@users.noreply.github.com> Date: Fri, 31 May 2024 15:51:10 +0800 Subject: [PATCH] add caching for getAvailabilityZones (#1658) --- .../api/controllers/ServiceDeployerApi.java | 14 ++++++- modules/cache/pom.xml | 37 +++++++++++++++++ .../modules/cache/RegionAzCacheConfig.java | 40 +++++++++++++++++++ .../modules/cache/consts/CacheNames.java | 16 ++++++++ modules/pom.xml | 1 + plugins/flexibleengine/pom.xml | 5 +++ .../FlexibleEngineOrchestratorPlugin.java | 3 ++ plugins/huaweicloud/pom.xml | 5 +++ .../HuaweiCloudOrchestratorPlugin.java | 3 ++ plugins/openstack/pom.xml | 5 +++ .../OpenstackOrchestratorPlugin.java | 3 ++ plugins/scs/pom.xml | 5 +++ .../plugins/scs/ScsOrchestratorPlugin.java | 3 ++ .../xpanse/runtime/XpanseApplication.java | 2 + .../src/main/resources/application.properties | 3 +- 15 files changed, 142 insertions(+), 3 deletions(-) create mode 100644 modules/cache/pom.xml create mode 100644 modules/cache/src/main/java/org/eclipse/xpanse/modules/cache/RegionAzCacheConfig.java create mode 100644 modules/cache/src/main/java/org/eclipse/xpanse/modules/cache/consts/CacheNames.java diff --git a/modules/api/src/main/java/org/eclipse/xpanse/api/controllers/ServiceDeployerApi.java b/modules/api/src/main/java/org/eclipse/xpanse/api/controllers/ServiceDeployerApi.java index 5eb0ea991..eda639793 100644 --- a/modules/api/src/main/java/org/eclipse/xpanse/api/controllers/ServiceDeployerApi.java +++ b/modules/api/src/main/java/org/eclipse/xpanse/api/controllers/ServiceDeployerApi.java @@ -21,6 +21,7 @@ import java.util.List; import java.util.Objects; import java.util.UUID; +import java.util.concurrent.TimeUnit; import lombok.extern.slf4j.Slf4j; import org.eclipse.xpanse.api.config.AuditApiRequest; import org.eclipse.xpanse.modules.database.service.DeployServiceEntity; @@ -41,8 +42,11 @@ import org.eclipse.xpanse.modules.orchestrator.PluginManager; import org.eclipse.xpanse.modules.orchestrator.deployment.DeployTask; import org.eclipse.xpanse.modules.security.UserServiceHelper; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.CacheControl; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.security.access.AccessDeniedException; import org.springframework.security.access.annotation.Secured; import org.springframework.web.bind.annotation.CrossOrigin; @@ -68,6 +72,9 @@ @Secured({ROLE_ADMIN, ROLE_USER}) public class ServiceDeployerApi { + @Value("${region.azs.cache.expire.time.in.minutes:60}") + private long duration; + @Resource private DeployService deployService; @@ -308,7 +315,7 @@ public Response purge(@PathVariable("id") String id) { @ResponseStatus(HttpStatus.OK) @Secured({ROLE_ADMIN, ROLE_CSP, ROLE_ISV, ROLE_USER}) @AuditApiRequest(methodName = "getCspFromRequestUri") - public List getAvailabilityZones( + public ResponseEntity> getAvailabilityZones( @Parameter(name = "cspName", description = "name of the cloud service provider") @RequestParam(name = "cspName") Csp csp, @Parameter(name = "regionName", description = "name of the region") @@ -316,8 +323,11 @@ public List getAvailabilityZones( String currentUserId = this.userServiceHelper.getCurrentUserId(); OrchestratorPlugin orchestratorPlugin = pluginManager.getOrchestratorPlugin(csp); - return orchestratorPlugin.getAvailabilityZonesOfRegion( + List availabilityZones = orchestratorPlugin.getAvailabilityZonesOfRegion( currentUserId, regionName); + CacheControl cacheControl = + CacheControl.maxAge(duration, TimeUnit.MINUTES).mustRevalidate(); + return ResponseEntity.ok().cacheControl(cacheControl).body(availabilityZones); } } diff --git a/modules/cache/pom.xml b/modules/cache/pom.xml new file mode 100644 index 000000000..ca8d79ac0 --- /dev/null +++ b/modules/cache/pom.xml @@ -0,0 +1,37 @@ + + + + + 4.0.0 + + org.eclipse.xpanse + modules + 1.0.15-SNAPSHOT + ../pom.xml + + + org.eclipse.xpanse.modules + cache + + + + org.projectlombok + lombok + provided + + + com.github.ben-manes.caffeine + caffeine + + + org.springframework.boot + spring-boot-starter-cache + + + \ No newline at end of file diff --git a/modules/cache/src/main/java/org/eclipse/xpanse/modules/cache/RegionAzCacheConfig.java b/modules/cache/src/main/java/org/eclipse/xpanse/modules/cache/RegionAzCacheConfig.java new file mode 100644 index 000000000..786bece16 --- /dev/null +++ b/modules/cache/src/main/java/org/eclipse/xpanse/modules/cache/RegionAzCacheConfig.java @@ -0,0 +1,40 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * SPDX-FileCopyrightText: Huawei Inc. + * + */ + +package org.eclipse.xpanse.modules.cache; + +import com.github.benmanes.caffeine.cache.Caffeine; +import java.util.concurrent.TimeUnit; +import org.eclipse.xpanse.modules.cache.consts.CacheNames; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.cache.caffeine.CaffeineCacheManager; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Region AZ information cache configuration class. + */ +@Configuration +public class RegionAzCacheConfig { + + @Value("${region.azs.cache.expire.time.in.minutes:60}") + private long duration; + + /** + * Create the configured CaffeineCacheManager. + * + * @return CaffeineCacheManager + */ + @Bean + public CaffeineCacheManager caffeineCacheManager() { + CaffeineCacheManager cacheManager = new CaffeineCacheManager( + CacheNames.REGION_AZ_CACHE_NAME); + cacheManager.setCaffeine(Caffeine.newBuilder() + .expireAfterWrite(duration, TimeUnit.MINUTES)); + return cacheManager; + } + +} diff --git a/modules/cache/src/main/java/org/eclipse/xpanse/modules/cache/consts/CacheNames.java b/modules/cache/src/main/java/org/eclipse/xpanse/modules/cache/consts/CacheNames.java new file mode 100644 index 000000000..3cbd7e480 --- /dev/null +++ b/modules/cache/src/main/java/org/eclipse/xpanse/modules/cache/consts/CacheNames.java @@ -0,0 +1,16 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * SPDX-FileCopyrightText: Huawei Inc. + * + */ + +package org.eclipse.xpanse.modules.cache.consts; + +/** + * Region az information cache constant class. + */ +public class CacheNames { + + public static final String REGION_AZ_CACHE_NAME = "region_azs_cache"; + +} diff --git a/modules/pom.xml b/modules/pom.xml index 61ececf9b..197134065 100644 --- a/modules/pom.xml +++ b/modules/pom.xml @@ -35,6 +35,7 @@ workflow observability async + cache diff --git a/plugins/flexibleengine/pom.xml b/plugins/flexibleengine/pom.xml index f36c2aed7..bb5dc1666 100644 --- a/plugins/flexibleengine/pom.xml +++ b/plugins/flexibleengine/pom.xml @@ -123,6 +123,11 @@ wiremock-standalone test + + org.eclipse.xpanse.modules + cache + ${project.version} + diff --git a/plugins/flexibleengine/src/main/java/org/eclipse/xpanse/plugins/flexibleengine/FlexibleEngineOrchestratorPlugin.java b/plugins/flexibleengine/src/main/java/org/eclipse/xpanse/plugins/flexibleengine/FlexibleEngineOrchestratorPlugin.java index de6e333c4..3137febf0 100644 --- a/plugins/flexibleengine/src/main/java/org/eclipse/xpanse/plugins/flexibleengine/FlexibleEngineOrchestratorPlugin.java +++ b/plugins/flexibleengine/src/main/java/org/eclipse/xpanse/plugins/flexibleengine/FlexibleEngineOrchestratorPlugin.java @@ -13,6 +13,7 @@ import java.util.List; import java.util.Map; import lombok.extern.slf4j.Slf4j; +import org.eclipse.xpanse.modules.cache.consts.CacheNames; import org.eclipse.xpanse.modules.models.billing.FlavorPriceResult; import org.eclipse.xpanse.modules.models.common.enums.Csp; import org.eclipse.xpanse.modules.models.credential.AbstractCredentialInfo; @@ -36,6 +37,7 @@ import org.eclipse.xpanse.plugins.flexibleengine.price.FlexibleEnginePriceCalculator; import org.eclipse.xpanse.plugins.flexibleengine.resourcehandler.FlexibleEngineTerraformResourceHandler; import org.springframework.beans.factory.annotation.Value; +import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Component; /** @@ -72,6 +74,7 @@ public List getExistingResourceNamesWithKind(String userId, String regio } @Override + @Cacheable(CacheNames.REGION_AZ_CACHE_NAME) public List getAvailabilityZonesOfRegion(String userId, String region) { return flexibleEngineResourceManager.getAvailabilityZonesOfRegion(userId, region); } diff --git a/plugins/huaweicloud/pom.xml b/plugins/huaweicloud/pom.xml index c82248335..0cd9c2add 100644 --- a/plugins/huaweicloud/pom.xml +++ b/plugins/huaweicloud/pom.xml @@ -124,6 +124,11 @@ wiremock-standalone test + + org.eclipse.xpanse.modules + cache + ${project.version} + diff --git a/plugins/huaweicloud/src/main/java/org/eclipse/xpanse/plugins/huaweicloud/HuaweiCloudOrchestratorPlugin.java b/plugins/huaweicloud/src/main/java/org/eclipse/xpanse/plugins/huaweicloud/HuaweiCloudOrchestratorPlugin.java index 247174004..49967f844 100644 --- a/plugins/huaweicloud/src/main/java/org/eclipse/xpanse/plugins/huaweicloud/HuaweiCloudOrchestratorPlugin.java +++ b/plugins/huaweicloud/src/main/java/org/eclipse/xpanse/plugins/huaweicloud/HuaweiCloudOrchestratorPlugin.java @@ -13,6 +13,7 @@ import java.util.List; import java.util.Map; import lombok.extern.slf4j.Slf4j; +import org.eclipse.xpanse.modules.cache.consts.CacheNames; import org.eclipse.xpanse.modules.models.billing.FlavorPriceResult; import org.eclipse.xpanse.modules.models.common.enums.Csp; import org.eclipse.xpanse.modules.models.credential.AbstractCredentialInfo; @@ -36,6 +37,7 @@ import org.eclipse.xpanse.plugins.huaweicloud.price.HuaweiCloudPriceCalculator; import org.eclipse.xpanse.plugins.huaweicloud.resourcehandler.HuaweiCloudTerraformResourceHandler; import org.springframework.beans.factory.annotation.Value; +import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Component; /** @@ -74,6 +76,7 @@ public List getExistingResourceNamesWithKind(String userId, String regio } @Override + @Cacheable(CacheNames.REGION_AZ_CACHE_NAME) public List getAvailabilityZonesOfRegion(String userId, String region) { return huaweiCloudResourceManager.getAvailabilityZonesOfRegion(userId, region); } diff --git a/plugins/openstack/pom.xml b/plugins/openstack/pom.xml index 0a0d64d9f..0d9f83489 100644 --- a/plugins/openstack/pom.xml +++ b/plugins/openstack/pom.xml @@ -106,5 +106,10 @@ database ${project.version} + + org.eclipse.xpanse.modules + cache + ${project.version} + diff --git a/plugins/openstack/src/main/java/org/eclipse/xpanse/plugins/openstack/OpenstackOrchestratorPlugin.java b/plugins/openstack/src/main/java/org/eclipse/xpanse/plugins/openstack/OpenstackOrchestratorPlugin.java index dec6e8dce..6e900fd56 100644 --- a/plugins/openstack/src/main/java/org/eclipse/xpanse/plugins/openstack/OpenstackOrchestratorPlugin.java +++ b/plugins/openstack/src/main/java/org/eclipse/xpanse/plugins/openstack/OpenstackOrchestratorPlugin.java @@ -13,6 +13,7 @@ import java.util.List; import java.util.Map; import lombok.extern.slf4j.Slf4j; +import org.eclipse.xpanse.modules.cache.consts.CacheNames; import org.eclipse.xpanse.modules.models.billing.FlavorPriceResult; import org.eclipse.xpanse.modules.models.common.enums.Csp; import org.eclipse.xpanse.modules.models.credential.AbstractCredentialInfo; @@ -37,6 +38,7 @@ import org.eclipse.xpanse.plugins.openstack.price.OpenstackPriceCalculator; import org.eclipse.xpanse.plugins.openstack.resourcehandler.OpenstackTerraformResourceHandler; import org.springframework.beans.factory.annotation.Value; +import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Component; /** @@ -76,6 +78,7 @@ public List getExistingResourceNamesWithKind(String userId, String regio } @Override + @Cacheable(CacheNames.REGION_AZ_CACHE_NAME) public List getAvailabilityZonesOfRegion(String userId, String region) { return openStackResourceManager.getAvailabilityZonesOfRegion(userId, region); } diff --git a/plugins/scs/pom.xml b/plugins/scs/pom.xml index e9a20211d..b2fdc6ea9 100644 --- a/plugins/scs/pom.xml +++ b/plugins/scs/pom.xml @@ -64,5 +64,10 @@ + + org.eclipse.xpanse.modules + cache + ${project.version} + \ No newline at end of file diff --git a/plugins/scs/src/main/java/org/eclipse/xpanse/plugins/scs/ScsOrchestratorPlugin.java b/plugins/scs/src/main/java/org/eclipse/xpanse/plugins/scs/ScsOrchestratorPlugin.java index 3abaaab0d..4181761fd 100644 --- a/plugins/scs/src/main/java/org/eclipse/xpanse/plugins/scs/ScsOrchestratorPlugin.java +++ b/plugins/scs/src/main/java/org/eclipse/xpanse/plugins/scs/ScsOrchestratorPlugin.java @@ -13,6 +13,7 @@ import java.util.List; import java.util.Map; import lombok.extern.slf4j.Slf4j; +import org.eclipse.xpanse.modules.cache.consts.CacheNames; import org.eclipse.xpanse.modules.models.billing.FlavorPriceResult; import org.eclipse.xpanse.modules.models.common.enums.Csp; import org.eclipse.xpanse.modules.models.credential.AbstractCredentialInfo; @@ -35,6 +36,7 @@ import org.eclipse.xpanse.plugins.scs.price.ScsPriceCalculator; import org.eclipse.xpanse.plugins.scs.resourcehandler.ScsTerraformResourceHandler; import org.springframework.beans.factory.annotation.Value; +import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Component; /** @@ -73,6 +75,7 @@ public List getExistingResourceNamesWithKind(String userId, String regio } @Override + @Cacheable(CacheNames.REGION_AZ_CACHE_NAME) public List getAvailabilityZonesOfRegion(String userId, String region) { return scsResourceManager.getAvailabilityZonesOfRegion(userId, region); } diff --git a/runtime/src/main/java/org/eclipse/xpanse/runtime/XpanseApplication.java b/runtime/src/main/java/org/eclipse/xpanse/runtime/XpanseApplication.java index 8a9e06c79..b10ff55ef 100644 --- a/runtime/src/main/java/org/eclipse/xpanse/runtime/XpanseApplication.java +++ b/runtime/src/main/java/org/eclipse/xpanse/runtime/XpanseApplication.java @@ -12,6 +12,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; +import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.ComponentScan; import org.springframework.data.jpa.repository.config.EnableJpaAuditing; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @@ -21,6 +22,7 @@ /** * Main entry class to xpanse runtime. This class can be directly executed to start the server. */ +@EnableCaching @EnableRetry @EnableAsync(proxyTargetClass = true) @EnableJpaRepositories(basePackages = "org.eclipse.xpanse") diff --git a/runtime/src/main/resources/application.properties b/runtime/src/main/resources/application.properties index 62ced778b..e65ad4372 100644 --- a/runtime/src/main/resources/application.properties +++ b/runtime/src/main/resources/application.properties @@ -49,4 +49,5 @@ http.request.retry.delay.milliseconds=1000 huaweicloud.auto.approve.service.template.enabled=false flexibleengine.auto.approve.service.template.enabled=false openstack.auto.approve.service.template.enabled=false -scs.auto.approve.service.template.enabled=false \ No newline at end of file +scs.auto.approve.service.template.enabled=false +region.azs.cache.expire.time.in.minutes=60 \ No newline at end of file