Skip to content

Commit 888731f

Browse files
committed
Remove validation cache after resource creation, fixes #1894647
1 parent 88c1a6f commit 888731f

File tree

4 files changed

+28
-37
lines changed

4 files changed

+28
-37
lines changed

PluginsAndFeatures/azure-toolkit-for-intellij/src/main/java/com/microsoft/azure/toolkit/intellij/webapp/runner/webappconfig/WebAppRunState.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.microsoft.azuretools.utils.AzureUIRefreshEvent;
2929
import com.microsoft.azuretools.utils.WebAppUtils;
3030
import com.microsoft.intellij.RunProcessHandler;
31+
import com.microsoft.intellij.util.ValidationUtils;
3132
import org.apache.commons.collections4.MapUtils;
3233
import org.apache.commons.compress.utils.FileNameUtils;
3334
import org.apache.commons.lang3.StringUtils;
@@ -164,7 +165,9 @@ private IWebApp getOrCreateWebappFromAppSettingModel(AzureAppService azureAppSer
164165
}
165166
if (webAppSettingModel.isCreatingNew()) {
166167
processHandler.setText(message("webapp.deploy.hint.creatingWebApp"));
167-
return AzureWebAppMvpModel.getInstance().createWebAppFromSettingModel(webAppSettingModel);
168+
final IWebApp webAppFromSettingModel = AzureWebAppMvpModel.getInstance().createWebAppFromSettingModel(webAppSettingModel);
169+
ValidationUtils.evictCacheForAppServiceNameValidation(webAppSettingModel.getSubscriptionId(), webAppSettingModel.getWebAppName());
170+
return webAppFromSettingModel;
168171
} else {
169172
processHandler.setText(message("appService.deploy.hint.failed"));
170173
throw new Exception(message("webapp.deploy.error.noWebApp"));

PluginsAndFeatures/azure-toolkit-for-intellij/src/main/java/com/microsoft/azure/toolkit/lib/function/FunctionAppService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.microsoft.azure.toolkit.lib.common.telemetry.AzureTelemetry;
2525
import com.microsoft.azure.toolkit.lib.resource.AzureGroup;
2626
import com.microsoft.azuretools.sdkmanage.IdentityAzureManager;
27+
import com.microsoft.intellij.util.ValidationUtils;
2728
import org.apache.commons.lang3.StringUtils;
2829
import org.zeroturnaround.zip.ZipUtil;
2930

@@ -101,6 +102,7 @@ public IFunctionApp createFunctionApp(final FunctionAppConfig config) {
101102
.withAppSettings(appSettings)
102103
.withDiagnosticConfig(config.getMonitorConfig().getDiagnosticConfig())
103104
.commit();
105+
ValidationUtils.evictCacheForAppServiceNameValidation(config.getSubscriptionId(), config.getName());
104106
AzureMessager.getMessager().info(String.format(CREATE_FUNCTION_APP_DONE, result.name()));
105107
return result;
106108
}

PluginsAndFeatures/azure-toolkit-for-intellij/src/main/java/com/microsoft/azure/toolkit/lib/webapp/WebAppService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.microsoft.azuretools.telemetrywrapper.EventUtil;
2121
import com.microsoft.azuretools.telemetrywrapper.Operation;
2222
import com.microsoft.azuretools.telemetrywrapper.TelemetryManager;
23+
import com.microsoft.intellij.util.ValidationUtils;
2324
import org.apache.commons.lang3.StringUtils;
2425

2526
import javax.annotation.Nonnull;
@@ -52,6 +53,7 @@ public IWebApp createWebApp(final WebAppConfig config) {
5253
EventUtil.logError(operation, ErrorType.userError, e, properties, null);
5354
throw e;
5455
} finally {
56+
ValidationUtils.evictCacheForAppServiceNameValidation(config.getSubscriptionId(), config.getName());
5557
operation.complete();
5658
}
5759
}

PluginsAndFeatures/azure-toolkit-for-intellij/src/main/java/com/microsoft/intellij/util/ValidationUtils.java

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88
import com.azure.core.management.exception.ManagementException;
99
import com.microsoft.azure.toolkit.lib.Azure;
1010
import com.microsoft.azure.toolkit.lib.appservice.AzureAppService;
11+
import com.microsoft.azure.toolkit.lib.common.cache.CacheManager;
12+
import com.microsoft.azure.toolkit.lib.common.cache.Cacheable;
1113
import com.microsoft.azure.toolkit.lib.common.entity.CheckNameAvailabilityResultEntity;
1214
import com.microsoft.azure.toolkit.lib.common.model.ResourceGroup;
1315
import com.microsoft.azure.toolkit.lib.resource.AzureGroup;
1416
import com.microsoft.azure.toolkit.lib.springcloud.SpringCloudCluster;
1517
import org.apache.commons.lang3.StringUtils;
16-
import org.apache.commons.lang3.tuple.Pair;
1718

18-
import java.util.HashMap;
1919
import java.util.HashSet;
20-
import java.util.Map;
2120
import java.util.Objects;
2221
import java.util.Set;
22+
import java.util.concurrent.ExecutionException;
2323
import java.util.regex.Matcher;
2424
import java.util.regex.Pattern;
2525

@@ -36,9 +36,6 @@ public class ValidationUtils {
3636
private static final String SPRING_CLOUD_APP_NAME_PATTERN = "^[a-z][a-z0-9-]{2,30}[a-z0-9]$";
3737
private static final String APP_INSIGHTS_NAME_INVALID_CHARACTERS = "[*;/?:@&=+$,<>#%\\\"\\{}|^'`\\\\\\[\\]]";
3838

39-
private static Map<Pair<String, String>, String> appServiceNameValidationCache = new HashMap<>();
40-
private static Map<String, String> resourceGroupValidationCache = new HashMap<>();
41-
4239
public static boolean isValidJavaPackageName(String packageName) {
4340
return packageName != null && packageName.matches(PACKAGE_NAME_REGEX);
4441
}
@@ -60,45 +57,43 @@ public static boolean isValidVersion(String version) {
6057
return version != null && version.matches(VERSION_REGEX);
6158
}
6259

60+
@Cacheable(cacheName = "appservice/validation/name", key = "$subscriptionId/$appServiceName")
6361
public static void validateAppServiceName(String subscriptionId, String appServiceName) {
64-
final Pair<String, String> cacheKey = Pair.of(subscriptionId, appServiceName);
65-
if (appServiceNameValidationCache.containsKey(cacheKey)) {
66-
throwCachedValidationResult(appServiceNameValidationCache.get(cacheKey));
67-
return;
68-
}
6962
if (StringUtils.isEmpty(subscriptionId)) {
70-
cacheAndThrow(appServiceNameValidationCache, cacheKey, message("appService.subscription.validate.empty"));
63+
throw new IllegalArgumentException(message("appService.subscription.validate.empty"));
7164
}
7265
if (!isValidAppServiceName(appServiceName)) {
73-
cacheAndThrow(appServiceNameValidationCache, cacheKey, message("appService.subscription.validate.invalidName"));
66+
throw new IllegalArgumentException(message("appService.subscription.validate.invalidName"));
7467
}
7568
final CheckNameAvailabilityResultEntity result = Azure.az(AzureAppService.class).checkNameAvailability(subscriptionId, appServiceName);
7669
if (!result.isAvailable()) {
77-
cacheAndThrow(appServiceNameValidationCache, cacheKey, result.getUnavailabilityMessage());
70+
throw new IllegalArgumentException(result.getUnavailabilityMessage());
7871
}
79-
appServiceNameValidationCache.put(cacheKey, null);
8072
}
8173

82-
public static void validateResourceGroupName(String subscriptionId, String resourceGroup) {
83-
if (resourceGroupValidationCache.containsKey(subscriptionId)) {
84-
throwCachedValidationResult(appServiceNameValidationCache.get(subscriptionId));
85-
return;
74+
// todo: move validation and related cache management to toolkit lib
75+
public static void evictCacheForAppServiceNameValidation(String subscriptionId, String appServiceName) {
76+
try {
77+
CacheManager.evictCache("appservice/validation/name", String.format("%s/%s", subscriptionId, appServiceName));
78+
} catch (ExecutionException e) {
79+
// swallow exception for clear cache
8680
}
81+
}
82+
83+
@Cacheable(cacheName = "resourcegroup/validation/name", key = "$subscriptionId/$resourceGroup")
84+
public static void validateResourceGroupName(String subscriptionId, String resourceGroup) {
8785
if (StringUtils.isEmpty(subscriptionId)) {
88-
cacheAndThrow(resourceGroupValidationCache, subscriptionId, message("appService.subscription.validate.empty"));
86+
throw new IllegalArgumentException(message("appService.subscription.validate.empty"));
8987
}
9088
if (StringUtils.isEmpty(resourceGroup)) {
91-
cacheAndThrow(resourceGroupValidationCache, subscriptionId, message("appService.resourceGroup.validate.empty"));
89+
throw new IllegalArgumentException(message("appService.resourceGroup.validate.empty"));
9290
}
9391
try {
9492
final ResourceGroup rg = Azure.az(AzureGroup.class).get(subscriptionId, resourceGroup);
95-
if (rg != null) {
96-
cacheAndThrow(resourceGroupValidationCache, subscriptionId, message("appService.resourceGroup.validate.exist"));
97-
}
93+
throw new IllegalArgumentException(message("appService.resourceGroup.validate.exist"));
9894
} catch (ManagementException e) {
9995
// swallow exception for get resources
10096
}
101-
resourceGroupValidationCache.put(subscriptionId, null);
10297
}
10398

10499
public static void validateAppServicePlanName(String appServicePlan) {
@@ -142,15 +137,4 @@ public static void validateSpringCloudAppName(final String name, final SpringClo
142137
throw new IllegalArgumentException(message("springcloud.app.name.validate.exist", name));
143138
}
144139
}
145-
146-
private static void cacheAndThrow(Map exceptionCache, Object key, String errorMessage) {
147-
exceptionCache.put(key, errorMessage);
148-
throw new IllegalArgumentException(errorMessage);
149-
}
150-
151-
private static void throwCachedValidationResult(String errorMessage) {
152-
if (StringUtils.isNotEmpty(errorMessage)) {
153-
throw new IllegalArgumentException(errorMessage);
154-
}
155-
}
156140
}

0 commit comments

Comments
 (0)