Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Make @Configured, @GradualRetry and @RateLimited Inherited (#2091) #2092

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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