Skip to content

Commit

Permalink
add caching for getAvailabilityZones (#1658)
Browse files Browse the repository at this point in the history
  • Loading branch information
WangLiNaruto authored May 31, 2024
1 parent 469d6be commit 0f53ec9
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -308,16 +315,19 @@ public Response purge(@PathVariable("id") String id) {
@ResponseStatus(HttpStatus.OK)
@Secured({ROLE_ADMIN, ROLE_CSP, ROLE_ISV, ROLE_USER})
@AuditApiRequest(methodName = "getCspFromRequestUri")
public List<String> getAvailabilityZones(
public ResponseEntity<List<String>> getAvailabilityZones(
@Parameter(name = "cspName", description = "name of the cloud service provider")
@RequestParam(name = "cspName") Csp csp,
@Parameter(name = "regionName", description = "name of the region")
@RequestParam(name = "regionName") String regionName) {

String currentUserId = this.userServiceHelper.getCurrentUserId();
OrchestratorPlugin orchestratorPlugin = pluginManager.getOrchestratorPlugin(csp);
return orchestratorPlugin.getAvailabilityZonesOfRegion(
List<String> availabilityZones = orchestratorPlugin.getAvailabilityZonesOfRegion(
currentUserId, regionName);
CacheControl cacheControl =
CacheControl.maxAge(duration, TimeUnit.MINUTES).mustRevalidate();
return ResponseEntity.ok().cacheControl(cacheControl).body(availabilityZones);
}

}
37 changes: 37 additions & 0 deletions modules/cache/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ SPDX-License-Identifier: Apache-2.0
~ SPDX-FileCopyrightText: Huawei Inc.
~
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.eclipse.xpanse</groupId>
<artifactId>modules</artifactId>
<version>1.0.15-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<groupId>org.eclipse.xpanse.modules</groupId>
<artifactId>cache</artifactId>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
</dependencies>
</project>
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.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;
}

}
Original file line number Diff line number Diff line change
@@ -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";

}
1 change: 1 addition & 0 deletions modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<module>workflow</module>
<module>observability</module>
<module>async</module>
<module>cache</module>
</modules>

</project>
5 changes: 5 additions & 0 deletions plugins/flexibleengine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@
<artifactId>wiremock-standalone</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.xpanse.modules</groupId>
<artifactId>cache</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -72,6 +74,7 @@ public List<String> getExistingResourceNamesWithKind(String userId, String regio
}

@Override
@Cacheable(CacheNames.REGION_AZ_CACHE_NAME)
public List<String> getAvailabilityZonesOfRegion(String userId, String region) {
return flexibleEngineResourceManager.getAvailabilityZonesOfRegion(userId, region);
}
Expand Down
5 changes: 5 additions & 0 deletions plugins/huaweicloud/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@
<artifactId>wiremock-standalone</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.xpanse.modules</groupId>
<artifactId>cache</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -74,6 +76,7 @@ public List<String> getExistingResourceNamesWithKind(String userId, String regio
}

@Override
@Cacheable(CacheNames.REGION_AZ_CACHE_NAME)
public List<String> getAvailabilityZonesOfRegion(String userId, String region) {
return huaweiCloudResourceManager.getAvailabilityZonesOfRegion(userId, region);
}
Expand Down
5 changes: 5 additions & 0 deletions plugins/openstack/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,10 @@
<artifactId>database</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.xpanse.modules</groupId>
<artifactId>cache</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -76,6 +78,7 @@ public List<String> getExistingResourceNamesWithKind(String userId, String regio
}

@Override
@Cacheable(CacheNames.REGION_AZ_CACHE_NAME)
public List<String> getAvailabilityZonesOfRegion(String userId, String region) {
return openStackResourceManager.getAvailabilityZonesOfRegion(userId, region);
}
Expand Down
5 changes: 5 additions & 0 deletions plugins/scs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,10 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.eclipse.xpanse.modules</groupId>
<artifactId>cache</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -73,6 +75,7 @@ public List<String> getExistingResourceNamesWithKind(String userId, String regio
}

@Override
@Cacheable(CacheNames.REGION_AZ_CACHE_NAME)
public List<String> getAvailabilityZonesOfRegion(String userId, String region) {
return scsResourceManager.getAvailabilityZonesOfRegion(userId, region);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion runtime/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
scs.auto.approve.service.template.enabled=false
region.azs.cache.expire.time.in.minutes=60

0 comments on commit 0f53ec9

Please sign in to comment.