feat: add builder for GenericRetry and deprecate mutable setters by TQJADE · Pull Request #3648 · operator-framework/java-operator-sdk · GitHub
Skip to content
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
14 changes: 10 additions & 4 deletions docs/content/en/docs/documentation/error-handling-retries.md
2 changes: 1 addition & 1 deletion docs/content/en/docs/documentation/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ LocallyRunOperatorExtension.builder()
// Override controller configuration
.withReconciler(new MyReconciler(), config -> config
.settingNamespace("specific-namespace")
.withRetry(new GenericRetry().withLinearRetry()))
.withRetry(GenericRetry.builder().withLinearRetry().build()))
// Pre-deploy infrastructure resources before operator starts
.withInfrastructure(configMap, secret)
// Register CRDs for resources not managed by a reconciler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,42 @@ public class GenericRetry implements Retry, AnnotationConfigurable<GradualRetry>
@Deprecated(forRemoval = true)
public static final Retry DEFAULT = new GenericRetry();

/**
* Creates an instance with the default values. Prefer {@link #builder()} or {@link
* #defaultLimitedExponentialRetry()}; this constructor is kept public so that the class can be
* instantiated and configured from the {@link GradualRetry} annotation.
*/
public GenericRetry() {}

private GenericRetry(Builder builder) {
this.maxAttempts = builder.maxAttempts;
this.initialInterval = builder.initialInterval;
this.intervalMultiplier = builder.intervalMultiplier;
this.maxInterval = builder.maxInterval;
}

public static Builder builder() {
return new Builder();
}

public static GenericRetry defaultLimitedExponentialRetry() {
return new GenericRetry();
return builder().build();
}

public static GenericRetry noRetry() {
return new GenericRetry().setMaxAttempts(0);
return builder().withMaxAttempts(0).build();
}

public static GenericRetry every10second10TimesRetry() {
return new GenericRetry().withLinearRetry().setMaxAttempts(10).setInitialInterval(10000);
return builder()
.withLinearRetry()
.withMaxAttempts(10)
.withInitialInterval(Duration.ofSeconds(10))
.build();
}

public Builder toBuilder() {
return new Builder(this);
}

@Override
Expand All @@ -53,6 +79,10 @@ public int getMaxAttempts() {
return maxAttempts;
}

/**
* @deprecated use {@link Builder#withMaxAttempts(int)} instead
*/
@Deprecated(forRemoval = true)
public GenericRetry setMaxAttempts(int maxRetryAttempts) {
this.maxAttempts = maxRetryAttempts;
return this;
Expand All @@ -62,11 +92,19 @@ public long getInitialInterval() {
return initialInterval;
}

/**
* @deprecated use {@link Builder#withInitialInterval(Duration)} instead
*/
@Deprecated(forRemoval = true)
public GenericRetry setInitialInterval(Duration initialInterval) {
setInitialInterval(initialInterval.toMillis());
this.initialInterval = initialInterval.toMillis();
return this;
}

/**
* @deprecated use {@link Builder#withInitialInterval(long)} instead
*/
@Deprecated(forRemoval = true)
public GenericRetry setInitialInterval(long initialInterval) {
this.initialInterval = initialInterval;
return this;
Expand All @@ -76,6 +114,10 @@ public double getIntervalMultiplier() {
return intervalMultiplier;
}

/**
* @deprecated use {@link Builder#withIntervalMultiplier(double)} instead
*/
@Deprecated(forRemoval = true)
public GenericRetry setIntervalMultiplier(double intervalMultiplier) {
this.intervalMultiplier = intervalMultiplier;
return this;
Expand All @@ -85,24 +127,46 @@ public long getMaxInterval() {
return maxInterval;
}

/**
* @deprecated use {@link Builder#withMaxInterval(long)} instead
*/
@Deprecated(forRemoval = true)
public GenericRetry setMaxInterval(long maxInterval) {
this.maxInterval = maxInterval;
return this;
}

/**
* @deprecated use {@link Builder#withMaxInterval(Duration)} instead
*/
@Deprecated(forRemoval = true)
public GenericRetry setMaxInterval(Duration maxInterval) {
return setMaxInterval(maxInterval.toMillis());
this.maxInterval = maxInterval.toMillis();
return this;
}

/**
* @deprecated use {@link Builder#withoutMaxInterval()} instead
*/
@Deprecated(forRemoval = true)
public GenericRetry withoutMaxInterval() {
this.maxInterval = -1;
return this;
}

/**
* @deprecated use {@link Builder#withoutMaxAttempts()} instead
*/
@Deprecated(forRemoval = true)
public GenericRetry withoutMaxAttempts() {
return this.setMaxAttempts(-1);
this.maxAttempts = -1;
return this;
}

/**
* @deprecated use {@link Builder#withLinearRetry()} instead
*/
@Deprecated(forRemoval = true)
public GenericRetry withLinearRetry() {
this.intervalMultiplier = 1;
return this;
Expand All @@ -118,4 +182,77 @@ public void initFrom(GradualRetry configuration) {
? GradualRetry.DEFAULT_MAX_INTERVAL
: configuration.maxInterval();
}

@Override
public String toString() {
return "GenericRetry{maxAttempts="
+ maxAttempts
+ ", initialInterval="
+ initialInterval
+ ", intervalMultiplier="
+ intervalMultiplier
+ ", maxInterval="
+ maxInterval
+ '}';
}

public static final class Builder {
private int maxAttempts = GradualRetry.DEFAULT_MAX_ATTEMPTS;
private long initialInterval = GradualRetry.DEFAULT_INITIAL_INTERVAL;
private double intervalMultiplier = GradualRetry.DEFAULT_MULTIPLIER;
private long maxInterval = GradualRetry.DEFAULT_MAX_INTERVAL;

private Builder() {}

private Builder(GenericRetry retry) {
this.maxAttempts = retry.maxAttempts;
this.initialInterval = retry.initialInterval;
this.intervalMultiplier = retry.intervalMultiplier;
this.maxInterval = retry.maxInterval;
}

public Builder withMaxAttempts(int maxAttempts) {
this.maxAttempts = maxAttempts;
return this;
}

public Builder withoutMaxAttempts() {
return withMaxAttempts(-1);
}

public Builder withInitialInterval(long initialIntervalMillis) {
this.initialInterval = initialIntervalMillis;
return this;
}

public Builder withInitialInterval(Duration initialInterval) {
return withInitialInterval(initialInterval.toMillis());
}

public Builder withIntervalMultiplier(double intervalMultiplier) {
this.intervalMultiplier = intervalMultiplier;
return this;
}

public Builder withLinearRetry() {
return withIntervalMultiplier(1);
}

public Builder withMaxInterval(long maxIntervalMillis) {
this.maxInterval = maxIntervalMillis;
return this;
}

public Builder withMaxInterval(Duration maxInterval) {
return withMaxInterval(maxInterval.toMillis());
}

public Builder withoutMaxInterval() {
return withMaxInterval(-1);
}

public GenericRetry build() {
return new GenericRetry(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -765,9 +765,7 @@ void processesAdditionalEventWhileInDeleteModeRetry() {
spy(
new EventProcessor(
controllerConfigTriggerAllEvent(
GenericRetry.defaultLimitedExponentialRetry()
.setInitialInterval(1000)
.setMaxAttempts(-1),
GenericRetry.builder().withInitialInterval(1000).withMaxAttempts(-1).build(),
rateLimiterMock),
reconciliationDispatcherMock,
eventSourceManagerMock,
Expand Down Expand Up @@ -796,10 +794,11 @@ void afterRetryExhaustedAdditionalEventTriggerReconciliationWhenDeleteEventPrese
spy(
new EventProcessor(
controllerConfigTriggerAllEvent(
GenericRetry.defaultLimitedExponentialRetry()
.setInitialInterval(100)
.setIntervalMultiplier(1)
.setMaxAttempts(1),
GenericRetry.builder()
.withInitialInterval(100)
.withIntervalMultiplier(1)
.withMaxAttempts(1)
.build(),
rateLimiterMock),
reconciliationDispatcherMock,
eventSourceManagerMock,
Expand Down Expand Up @@ -831,10 +830,11 @@ void rateLimitsDeleteEventInAllEventMode() {
spy(
new EventProcessor(
controllerConfigTriggerAllEvent(
GenericRetry.defaultLimitedExponentialRetry()
.setInitialInterval(100)
.setIntervalMultiplier(1)
.setMaxAttempts(1),
GenericRetry.builder()
.withInitialInterval(100)
.withIntervalMultiplier(1)
.withMaxAttempts(1)
.build(),
rateLimiterMock),
reconciliationDispatcherMock,
eventSourceManagerMock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ void ifRetryLimitedToZeroMaxAttemptsErrorHandlerGetsCorrectLastAttempt() {
var configuration =
MockControllerConfiguration.forResource(
(Class<TestCustomResource>) testCustomResource.getClass());
when(configuration.getRetry()).thenReturn(new GenericRetry().setMaxAttempts(0));
when(configuration.getRetry()).thenReturn(GenericRetry.builder().withMaxAttempts(0).build());
reconciliationDispatcher =
init(testCustomResource, reconciler, configuration, customResourceFacade, false);

Expand Down
Loading
Loading