Move retry properties to record (#1104)

This commit is contained in:
erabii
2022-10-20 22:55:40 +03:00
committed by GitHub
parent af61f5d3f9
commit 77298eaf2b
16 changed files with 71 additions and 117 deletions

View File

@@ -16,6 +16,8 @@
package org.springframework.cloud.kubernetes.commons.config;
import org.springframework.boot.context.properties.bind.DefaultValue;
/**
* Abstraction over configuration properties.
*
@@ -38,7 +40,7 @@ public abstract class AbstractConfigProperties {
protected boolean failFast = false;
protected RetryProperties retry = new RetryProperties();
protected RetryProperties retry = RetryProperties.DEFAULT;
public boolean isEnabled() {
return this.enabled;
@@ -98,70 +100,20 @@ public abstract class AbstractConfigProperties {
/**
* Kubernetes config retry properties.
* @param initialInterval Initial retry interval in milliseconds.
* @param multiplier Maximum interval for backoff.
* @param maxInterval Maximum interval
* @param maxAttempts Maximum number of attempts.
* @param enabled Retry enabled or not
*/
public static class RetryProperties {
public record RetryProperties(@DefaultValue("1000") long initialInterval, @DefaultValue("1.1") double multiplier,
@DefaultValue("2000") long maxInterval, @DefaultValue("6") int maxAttempts,
@DefaultValue("true") boolean enabled) {
/**
* Initial retry interval in milliseconds.
* Default instance.
*/
private long initialInterval = 1000;
/**
* Multiplier for next interval.
*/
private double multiplier = 1.1;
/**
* Maximum interval for backoff.
*/
private long maxInterval = 2000;
/**
* Maximum number of attempts.
*/
private int maxAttempts = 6;
private boolean enabled = true;
public long getInitialInterval() {
return this.initialInterval;
}
public void setInitialInterval(long initialInterval) {
this.initialInterval = initialInterval;
}
public double getMultiplier() {
return this.multiplier;
}
public void setMultiplier(double multiplier) {
this.multiplier = multiplier;
}
public long getMaxInterval() {
return this.maxInterval;
}
public void setMaxInterval(long maxInterval) {
this.maxInterval = maxInterval;
}
public int getMaxAttempts() {
return this.maxAttempts;
}
public void setMaxAttempts(int maxAttempts) {
this.maxAttempts = maxAttempts;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public static final RetryProperties DEFAULT = new RetryProperties(1000, 1.1, 2000, 6, true);
}

View File

@@ -39,9 +39,9 @@ public class ConfigDataRetryableConfigMapPropertySourceLocator extends ConfigMap
ConfigMapPropertySourceLocator configMapPropertySourceLocator, ConfigMapConfigProperties properties) {
super(properties);
this.configMapPropertySourceLocator = configMapPropertySourceLocator;
this.retryTemplate = RetryTemplate.builder().maxAttempts(properties.getRetry().getMaxAttempts())
.exponentialBackoff(properties.getRetry().getInitialInterval(), properties.getRetry().getMultiplier(),
properties.getRetry().getMaxInterval())
this.retryTemplate = RetryTemplate.builder().maxAttempts(properties.getRetry().maxAttempts())
.exponentialBackoff(properties.getRetry().initialInterval(), properties.getRetry().multiplier(),
properties.getRetry().maxInterval())
.build();
}

View File

@@ -31,7 +31,7 @@ import org.springframework.retry.support.RetryTemplate;
*/
public class ConfigDataRetryableSecretsPropertySourceLocator extends SecretsPropertySourceLocator {
private RetryTemplate retryTemplate;
private final RetryTemplate retryTemplate;
private SecretsPropertySourceLocator secretsPropertySourceLocator;
@@ -39,9 +39,9 @@ public class ConfigDataRetryableSecretsPropertySourceLocator extends SecretsProp
SecretsConfigProperties secretsConfigProperties) {
super(secretsConfigProperties);
this.secretsPropertySourceLocator = propertySourceLocator;
this.retryTemplate = RetryTemplate.builder().maxAttempts(properties.getRetry().getMaxAttempts())
.exponentialBackoff(properties.getRetry().getInitialInterval(), properties.getRetry().getMultiplier(),
properties.getRetry().getMaxInterval())
this.retryTemplate = RetryTemplate.builder().maxAttempts(properties.getRetry().maxAttempts())
.exponentialBackoff(properties.getRetry().initialInterval(), properties.getRetry().multiplier(),
properties.getRetry().maxInterval())
.build();
}

View File

@@ -50,9 +50,9 @@ public class KubernetesBootstrapConfiguration {
public static RetryOperationsInterceptor retryOperationsInterceptor(
AbstractConfigProperties.RetryProperties retryProperties) {
return RetryInterceptorBuilder.stateless().backOffOptions(retryProperties.getInitialInterval(),
retryProperties.getMultiplier(), retryProperties.getMaxInterval())
.maxAttempts(retryProperties.getMaxAttempts()).build();
return RetryInterceptorBuilder.stateless().backOffOptions(retryProperties.initialInterval(),
retryProperties.multiplier(), retryProperties.maxInterval())
.maxAttempts(retryProperties.maxAttempts()).build();
}
@Bean

View File

@@ -154,12 +154,12 @@ public abstract class KubernetesConfigDataLocationResolver
}
protected boolean isRetryEnabledForConfigMap(ConfigMapConfigProperties configMapProperties) {
return RETRY_IS_PRESENT && configMapProperties != null && configMapProperties.getRetry().isEnabled()
return RETRY_IS_PRESENT && configMapProperties != null && configMapProperties.getRetry().enabled()
&& configMapProperties.isFailFast();
}
protected boolean isRetryEnabledForSecrets(SecretsConfigProperties secretsProperties) {
return RETRY_IS_PRESENT && secretsProperties != null && secretsProperties.getRetry().isEnabled()
return RETRY_IS_PRESENT && secretsProperties != null && secretsProperties.getRetry().enabled()
&& secretsProperties.isFailFast();
}
@@ -190,8 +190,8 @@ public abstract class KubernetesConfigDataLocationResolver
kubernetesClientProperties);
}
else {
kubernetesClientProperties = binder
.bindOrCreate(KubernetesClientProperties.PREFIX, Bindable.of(KubernetesClientProperties.class), bindHandler);
kubernetesClientProperties = binder.bindOrCreate(KubernetesClientProperties.PREFIX,
Bindable.of(KubernetesClientProperties.class), bindHandler);
}
kubernetesClientProperties.setNamespace(namespace);
@@ -200,7 +200,8 @@ public abstract class KubernetesConfigDataLocationResolver
ConfigMapConfigProperties configMapConfigProperties = null;
if (configEnabled) {
configMapConfigProperties = binder.bindOrCreate(ConfigMapConfigProperties.PREFIX, ConfigMapConfigProperties.class);
configMapConfigProperties = binder.bindOrCreate(ConfigMapConfigProperties.PREFIX,
ConfigMapConfigProperties.class);
}
SecretsConfigProperties secretsProperties = null;

View File

@@ -58,22 +58,21 @@ public class ConfigAndSecretsFailFastEnabledWithDefaultRetryConfiguration {
@Test
void retryConfigurationShouldBeDefault() {
AbstractConfigProperties.RetryProperties defaultRetryProperties = new AbstractConfigProperties.RetryProperties();
AbstractConfigProperties.RetryProperties defaultRetryProperties = AbstractConfigProperties.RetryProperties.DEFAULT;
AbstractConfigProperties.RetryProperties configMapRetryProperties = configMapConfigProperties.getRetry();
assertThat(configMapRetryProperties.getMaxAttempts()).isEqualTo(defaultRetryProperties.getMaxAttempts());
assertThat(configMapRetryProperties.getInitialInterval())
.isEqualTo(defaultRetryProperties.getInitialInterval());
assertThat(configMapRetryProperties.getMaxInterval()).isEqualTo(defaultRetryProperties.getMaxInterval());
assertThat(configMapRetryProperties.getMultiplier()).isEqualTo(defaultRetryProperties.getMultiplier());
assertThat(configMapRetryProperties.maxAttempts()).isEqualTo(defaultRetryProperties.maxAttempts());
assertThat(configMapRetryProperties.initialInterval()).isEqualTo(defaultRetryProperties.initialInterval());
assertThat(configMapRetryProperties.maxInterval()).isEqualTo(defaultRetryProperties.maxInterval());
assertThat(configMapRetryProperties.multiplier()).isEqualTo(defaultRetryProperties.multiplier());
AbstractConfigProperties.RetryProperties secretsRetryProperties = secretsConfigProperties.getRetry();
assertThat(secretsRetryProperties.getMaxAttempts()).isEqualTo(defaultRetryProperties.getMaxAttempts());
assertThat(secretsRetryProperties.getInitialInterval()).isEqualTo(defaultRetryProperties.getInitialInterval());
assertThat(secretsRetryProperties.getMaxInterval()).isEqualTo(defaultRetryProperties.getMaxInterval());
assertThat(secretsRetryProperties.getMultiplier()).isEqualTo(defaultRetryProperties.getMultiplier());
assertThat(secretsRetryProperties.maxAttempts()).isEqualTo(defaultRetryProperties.maxAttempts());
assertThat(secretsRetryProperties.initialInterval()).isEqualTo(defaultRetryProperties.initialInterval());
assertThat(secretsRetryProperties.maxInterval()).isEqualTo(defaultRetryProperties.maxInterval());
assertThat(secretsRetryProperties.multiplier()).isEqualTo(defaultRetryProperties.multiplier());
}
}

View File

@@ -54,12 +54,12 @@ class ConfigFailFastEnabled {
@Test
void retryConfigurationShouldBeDefault() {
AbstractConfigProperties.RetryProperties retryProperties = configMapConfigProperties.getRetry();
AbstractConfigProperties.RetryProperties defaultRetryProperties = new AbstractConfigProperties.RetryProperties();
AbstractConfigProperties.RetryProperties defaultRetryProperties = AbstractConfigProperties.RetryProperties.DEFAULT;
assertThat(retryProperties.getMaxAttempts()).isEqualTo(defaultRetryProperties.getMaxAttempts());
assertThat(retryProperties.getInitialInterval()).isEqualTo(defaultRetryProperties.getInitialInterval());
assertThat(retryProperties.getMaxInterval()).isEqualTo(defaultRetryProperties.getMaxInterval());
assertThat(retryProperties.getMultiplier()).isEqualTo(defaultRetryProperties.getMultiplier());
assertThat(retryProperties.maxAttempts()).isEqualTo(defaultRetryProperties.maxAttempts());
assertThat(retryProperties.initialInterval()).isEqualTo(defaultRetryProperties.initialInterval());
assertThat(retryProperties.maxInterval()).isEqualTo(defaultRetryProperties.maxInterval());
assertThat(retryProperties.multiplier()).isEqualTo(defaultRetryProperties.multiplier());
}
}

View File

@@ -44,10 +44,10 @@ class ConfigFailFastEnabledWithCustomRetryConfiguration {
void retryConfigurationShouldBeCustomized() {
AbstractConfigProperties.RetryProperties retryProperties = configMapConfigProperties.getRetry();
assertThat(retryProperties.getMaxAttempts()).isEqualTo(3);
assertThat(retryProperties.getInitialInterval()).isEqualTo(1500L);
assertThat(retryProperties.getMaxInterval()).isEqualTo(3000L);
assertThat(retryProperties.getMultiplier()).isEqualTo(1.5D);
assertThat(retryProperties.maxAttempts()).isEqualTo(3);
assertThat(retryProperties.initialInterval()).isEqualTo(1500L);
assertThat(retryProperties.maxInterval()).isEqualTo(3000L);
assertThat(retryProperties.multiplier()).isEqualTo(1.5D);
}
}

View File

@@ -54,12 +54,13 @@ class SecretsFailFastEnabled {
@Test
void retryConfigurationShouldBeDefault() {
AbstractConfigProperties.RetryProperties retryProperties = secretsConfigProperties.getRetry();
AbstractConfigProperties.RetryProperties defaultRetryProperties = new AbstractConfigProperties.RetryProperties();
AbstractConfigProperties.RetryProperties defaultRetryProperties = new AbstractConfigProperties.RetryProperties(
1000, 1.1, 2000, 6, true);
assertThat(retryProperties.getMaxAttempts()).isEqualTo(defaultRetryProperties.getMaxAttempts());
assertThat(retryProperties.getInitialInterval()).isEqualTo(defaultRetryProperties.getInitialInterval());
assertThat(retryProperties.getMaxInterval()).isEqualTo(defaultRetryProperties.getMaxInterval());
assertThat(retryProperties.getMultiplier()).isEqualTo(defaultRetryProperties.getMultiplier());
assertThat(retryProperties.maxAttempts()).isEqualTo(defaultRetryProperties.maxAttempts());
assertThat(retryProperties.initialInterval()).isEqualTo(defaultRetryProperties.initialInterval());
assertThat(retryProperties.maxInterval()).isEqualTo(defaultRetryProperties.maxInterval());
assertThat(retryProperties.multiplier()).isEqualTo(defaultRetryProperties.multiplier());
}
}

View File

@@ -44,10 +44,10 @@ class SecretsFailFastEnabledWithCustomRetryConfiguration {
void retryConfigurationShouldBeCustomized() {
AbstractConfigProperties.RetryProperties retryProperties = secretsConfigProperties.getRetry();
assertThat(retryProperties.getMaxAttempts()).isEqualTo(3);
assertThat(retryProperties.getInitialInterval()).isEqualTo(1500L);
assertThat(retryProperties.getMaxInterval()).isEqualTo(3000L);
assertThat(retryProperties.getMultiplier()).isEqualTo(1.5D);
assertThat(retryProperties.maxAttempts()).isEqualTo(3);
assertThat(retryProperties.initialInterval()).isEqualTo(1500L);
assertThat(retryProperties.maxInterval()).isEqualTo(3000L);
assertThat(retryProperties.multiplier()).isEqualTo(1.5D);
}
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.cloud.kubernetes.fabric8.config.locator_retry;
package org.springframework.cloud.kubernetes.fabric8.config.locator_retry.config_retry_enabled;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.cloud.kubernetes.fabric8.config.locator_retry;
package org.springframework.cloud.kubernetes.fabric8.config.locator_retry.config_retry_enabled;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
@@ -35,9 +35,10 @@ import static org.mockito.Mockito.spy;
* @author Isik Erhan
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE,
properties = { "spring.cloud.kubernetes.client.namespace=default",
"spring.cloud.kubernetes.config.fail-fast=true", "spring.cloud.kubernetes.config.retry.max-attempts=5",
"spring.main.cloud-platform=KUBERNETES", "spring.config.import=kubernetes:" },
properties = { "spring.cloud.kubernetes.secrets.enabled=false",
"spring.cloud.kubernetes.client.namespace=default", "spring.cloud.kubernetes.config.fail-fast=true",
"spring.cloud.kubernetes.config.retry.max-attempts=5", "spring.main.cloud-platform=KUBERNETES",
"spring.config.import=kubernetes:" },
classes = Application.class)
@EnableKubernetesMockClient
class ConfigDataConfigRetryEnabled extends ConfigRetryEnabled {

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.cloud.kubernetes.fabric8.config.locator_retry;
package org.springframework.cloud.kubernetes.fabric8.config.locator_retry.config_retry_enabled;
import java.util.HashMap;
import java.util.Map;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.cloud.kubernetes.fabric8.config.retry;
package org.springframework.cloud.kubernetes.fabric8.config.retry.secrets_enabled;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
@@ -31,8 +31,8 @@ import org.springframework.cloud.kubernetes.fabric8.config.Fabric8SecretsPropert
* @author Isik Erhan
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE,
properties = { "spring.cloud.kubernetes.client.namespace=default",
"spring.cloud.kubernetes.secrets.fail-fast=true",
properties = { "spring.cloud.kubernetes.config.enabled=false", "spring.cloud.kubernetes.secrets.enabled=true",
"spring.cloud.kubernetes.client.namespace=default", "spring.cloud.kubernetes.secrets.fail-fast=true",
"spring.cloud.kubernetes.secrets.retry.max-attempts=5",
"spring.cloud.kubernetes.secrets.name=my-secret", "spring.cloud.kubernetes.secrets.enable-api=true",
"spring.main.cloud-platform=KUBERNETES", "spring.cloud.bootstrap.enabled=true" },

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.cloud.kubernetes.fabric8.config.retry;
package org.springframework.cloud.kubernetes.fabric8.config.retry.secrets_enabled;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
@@ -33,8 +33,8 @@ import static org.mockito.Mockito.spy;
* @author Isik Erhan
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE,
properties = { "spring.cloud.kubernetes.client.namespace=default",
"spring.cloud.kubernetes.secrets.fail-fast=true",
properties = { "spring.cloud.kubernetes.config.enabled=false", "spring.cloud.kubernetes.secrets.enabled=true",
"spring.cloud.kubernetes.client.namespace=default", "spring.cloud.kubernetes.secrets.fail-fast=true",
"spring.cloud.kubernetes.secrets.retry.max-attempts=5",
"spring.cloud.kubernetes.secrets.name=my-secret", "spring.cloud.kubernetes.secrets.enable-api=true",
"spring.main.cloud-platform=KUBERNETES", "spring.config.import=kubernetes:" },

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.cloud.kubernetes.fabric8.config.retry;
package org.springframework.cloud.kubernetes.fabric8.config.retry.secrets_enabled;
import java.util.Base64;
import java.util.HashMap;