Skip to content

Commit

Permalink
feat: Make @configured, @GradualRetry and @ratelimited Inherited (#2091
Browse files Browse the repository at this point in the history
…) (#2092)

Signed-off-by: Bram Meerten <bram.mee@gmail.com>

Co-authored-by: Attila Mészáros <csviri@gmail.com>
  • Loading branch information
BramMeerten and csviri authored Nov 13, 2023
1 parent 0112410 commit b9e0b82
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package io.javaoperatorsdk.operator.processing.event.rate;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.concurrent.TimeUnit;

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface RateLimited {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.javaoperatorsdk.operator.processing.retry;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface GradualRetry {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,24 @@ void configuringRateAndRetryViaAnnotationsShouldWork() {
assertEquals(Duration.ofSeconds(3), rateLimiter.getRefreshPeriod());
}

@Test
void configuringRateLimitAndGradualRetryViaSuperClassShouldWork() {
var config = configFor(new GradualRetryAndRateLimitedOnSuperClass());
final var retry = config.getRetry();
final var testRetry = assertInstanceOf(GenericRetry.class, retry);
assertEquals(
BaseClassWithGradualRetryAndRateLimited.RETRY_MAX_ATTEMPTS,
testRetry.getMaxAttempts());

final var rateLimiter = assertInstanceOf(LinearRateLimiter.class, config.getRateLimiter());
assertEquals(
BaseClassWithGradualRetryAndRateLimited.RATE_LIMITED_MAX_RECONCILIATIONS,
rateLimiter.getLimitForPeriod());
assertEquals(
Duration.ofSeconds(BaseClassWithGradualRetryAndRateLimited.RATE_LIMITED_WITHIN_SECONDS),
rateLimiter.getRefreshPeriod());
}

@Test
void checkingRetryingGraduallyWorks() {
var config = configFor(new CheckRetryingGraduallyConfiguration());
Expand Down Expand Up @@ -250,6 +268,7 @@ public UpdateControl<ConfigMap> reconcile(ConfigMap resource, Context<ConfigMap>
@ControllerConfiguration(
dependents = @Dependent(type = ReadOnlyDependent.class, name = NamedDepReconciler.NAME))
private static class NamedDepReconciler implements Reconciler<ConfigMap> {

private static final String NAME = "foo";

@Override
Expand Down Expand Up @@ -325,6 +344,7 @@ public UpdateControl<ConfigMap> reconcile(ConfigMap resource, Context<ConfigMap>
}

public static class TestRetry implements Retry, AnnotationConfigurable<TestRetryConfiguration> {

private int value;

public TestRetry() {}
Expand All @@ -347,6 +367,7 @@ public void initFrom(TestRetryConfiguration configuration) {
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
private @interface TestRetryConfiguration {

int value() default 42;
}

Expand Down Expand Up @@ -382,7 +403,30 @@ public UpdateControl<ConfigMap> reconcile(ConfigMap resource, Context<ConfigMap>
}
}

@ControllerConfiguration
private static class GradualRetryAndRateLimitedOnSuperClass
extends BaseClassWithGradualRetryAndRateLimited
implements Reconciler<ConfigMap> {

@Override
public UpdateControl<ConfigMap> reconcile(ConfigMap resource, Context<ConfigMap> context) {
return null;
}
}

@RateLimited(
maxReconciliations = BaseClassWithGradualRetryAndRateLimited.RATE_LIMITED_MAX_RECONCILIATIONS,
within = BaseClassWithGradualRetryAndRateLimited.RATE_LIMITED_WITHIN_SECONDS)
@GradualRetry(maxAttempts = BaseClassWithGradualRetryAndRateLimited.RETRY_MAX_ATTEMPTS)
private static class BaseClassWithGradualRetryAndRateLimited {

public static final int RATE_LIMITED_MAX_RECONCILIATIONS = 7;
public static final int RATE_LIMITED_WITHIN_SECONDS = 3;
public static final int RETRY_MAX_ATTEMPTS = 3;
}

private static class ControllerConfigurationOnSuperClass extends BaseClass {

}

@ControllerConfiguration
Expand Down Expand Up @@ -443,10 +487,12 @@ private static class ChildCustomAnnotatedDep extends CustomAnnotatedDep {

@Retention(RetentionPolicy.RUNTIME)
private @interface CustomAnnotation {

int value();
}

private static class CustomConfig {

private final int value;

private CustomConfig(int value) {
Expand All @@ -460,6 +506,7 @@ public int getValue() {

private static class CustomConfigConverter
implements ConfigurationConverter<CustomAnnotation, CustomConfig, CustomAnnotatedDep> {

static final int CONVERTER_PROVIDED_DEFAULT = 7;

@Override
Expand Down

0 comments on commit b9e0b82

Please sign in to comment.