From bb417f8ae0d1bb2c5b5a5779ae9d679c1b050a3b Mon Sep 17 00:00:00 2001 From: John Blum Date: Thu, 21 Jan 2021 14:40:17 -0800 Subject: [PATCH] Improve matches(..) method override to configure the clusterAvailable state on runtime environment match and iff cluster availability was not already determined. Remove all logging from the overridden matches(..) method. Override the configureTopology(..) method to do nothing. Override the logConnectedRuntimeEnvironment(..) and logUnconnectedRuntimeEnvironment(..) to log appropriate messages for cloud runtime environments. Refactor the StandaloneClusterAvailableCondition match to only act when not in a cloud runtime envrionment. --- .../ClusterAvailableConfiguration.java | 84 ++++++++++++++----- 1 file changed, 63 insertions(+), 21 deletions(-) diff --git a/spring-geode/src/main/java/org/springframework/geode/config/annotation/ClusterAvailableConfiguration.java b/spring-geode/src/main/java/org/springframework/geode/config/annotation/ClusterAvailableConfiguration.java index dcfcd6e7..3658c574 100644 --- a/spring-geode/src/main/java/org/springframework/geode/config/annotation/ClusterAvailableConfiguration.java +++ b/spring-geode/src/main/java/org/springframework/geode/config/annotation/ClusterAvailableConfiguration.java @@ -15,6 +15,8 @@ */ package org.springframework.geode.config.annotation; +import java.util.Set; + import org.springframework.boot.autoconfigure.condition.AnyNestedCondition; import org.springframework.boot.cloud.CloudPlatform; import org.springframework.context.annotation.ConditionContext; @@ -23,6 +25,8 @@ import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import org.springframework.core.type.AnnotatedTypeMetadata; import org.springframework.data.gemfire.config.annotation.EnableClusterConfiguration; +import org.springframework.data.gemfire.support.ConnectionEndpointList; +import org.springframework.data.gemfire.util.CollectionUtils; import org.springframework.lang.NonNull; import org.slf4j.Logger; @@ -50,6 +54,9 @@ import org.slf4j.Logger; @SuppressWarnings("unused") public class ClusterAvailableConfiguration { + private static final Set SUPPORTED_CLOUD_PLATFORMS = + CollectionUtils.asSet(CloudPlatform.CLOUD_FOUNDRY, CloudPlatform.KUBERNETES); + public static final class AnyClusterAvailableCondition extends AnyNestedCondition { public AnyClusterAvailableCondition() { @@ -89,6 +96,24 @@ public class ClusterAvailableConfiguration { return match && (strictMatch || isInfoLoggingEnabled()); } + @Override + public synchronized final boolean matches(@NonNull ConditionContext conditionContext, + @NonNull AnnotatedTypeMetadata typeMetadata) { + + boolean match = isCloudPlatformActive(conditionContext.getEnvironment()); + boolean strictMatch = isStrictMatch(conditionContext, typeMetadata); + + if (isMatchingStrictOrLoggable(match, strictMatch)) { + match |= super.matches(conditionContext, typeMetadata); + } + + if (match && !wasClusterAvailabilityEvaluated()) { + set(true); + } + + return match; + } + @Override protected void logConnectedRuntimeEnvironment(@NonNull Logger logger) { @@ -98,31 +123,23 @@ public class ClusterAvailableConfiguration { } } - protected void logRuntimeEnvironment(@NonNull Logger logger) { + @Override + protected void logUnconnectedRuntimeEnvironment(@NonNull Logger logger) { if (logger.isInfoEnabled()) { - logger.info("Spring Boot application is running in a [{}] Cloud-managed Environment", - getRuntimeEnvironmentName()); + logger.info("No cluster was found; Spring Boot application is running in a [{}]" + + " Cloud-managed Environment", getRuntimeEnvironmentName()); } } @Override - public synchronized final boolean matches(@NonNull ConditionContext conditionContext, - @NonNull AnnotatedTypeMetadata typeMetadata) { - - boolean match = isCloudPlatformActive(conditionContext.getEnvironment()); - boolean strictMatch = isStrictMatch(typeMetadata, conditionContext); - - if (isMatchingStrictOrLoggable(match, strictMatch)) { - logRuntimeEnvironment(getLogger()); - match |= super.matches(conditionContext, typeMetadata); - } - - return match; + protected void configureTopology(@NonNull Environment environment, + @NonNull ConnectionEndpointList connectionEndpoints, int connectionCount) { + // do nothing! } } - public static final class CloudFoundryClusterAvailableCondition extends AbstractCloudPlatformAvailableCondition { + public static class CloudFoundryClusterAvailableCondition extends AbstractCloudPlatformAvailableCondition { protected static final String CLOUD_FOUNDRY_NAME = "CloudFoundry"; protected static final String RUNTIME_ENVIRONMENT_NAME = "VMware Tanzu GemFire for VMs"; @@ -139,11 +156,11 @@ public class ClusterAvailableConfiguration { @Override protected boolean isCloudPlatformActive(@NonNull Environment environment) { - return CloudPlatform.CLOUD_FOUNDRY.isActive(environment); + return environment != null && CloudPlatform.CLOUD_FOUNDRY.isActive(environment); } } - public static final class KubernetesClusterAvailableCondition extends AbstractCloudPlatformAvailableCondition { + public static class KubernetesClusterAvailableCondition extends AbstractCloudPlatformAvailableCondition { protected static final String KUBERNETES_NAME = "Kubernetes"; protected static final String RUNTIME_ENVIRONMENT_NAME = "VMware Tanzu GemFire for K8S"; @@ -160,11 +177,36 @@ public class ClusterAvailableConfiguration { @Override protected boolean isCloudPlatformActive(@NonNull Environment environment) { - return CloudPlatform.KUBERNETES.isActive(environment); + return environment != null && CloudPlatform.KUBERNETES.isActive(environment); } } - public static final class StandaloneClusterAvailableCondition - extends ClusterAwareConfiguration.ClusterAwareCondition { } + public static class StandaloneClusterAvailableCondition + extends ClusterAwareConfiguration.ClusterAwareCondition { + @Override + public synchronized boolean matches(@NonNull ConditionContext conditionContext, + @NonNull AnnotatedTypeMetadata typeMetadata) { + + return isNotSupportedCloudPlatform(conditionContext) + && super.matches(conditionContext, typeMetadata); + } + + private boolean isNotSupportedCloudPlatform(@NonNull ConditionContext conditionContext) { + return conditionContext != null && isNotSupportedCloudPlatform(conditionContext.getEnvironment()); + } + + private boolean isNotSupportedCloudPlatform(@NonNull Environment environment) { + + CloudPlatform activeCloudPlatform = environment != null + ? CloudPlatform.getActive(environment) + : null; + + return !isSupportedCloudPlatform(activeCloudPlatform); + } + + private boolean isSupportedCloudPlatform(@NonNull CloudPlatform cloudPlatform) { + return cloudPlatform != null && SUPPORTED_CLOUD_PLATFORMS.contains(cloudPlatform); + } + } }