Skip to content

Partial implementation of #65 - Custom retry delay provider #92

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

Merged
merged 1 commit into from
Jul 23, 2021
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
Expand Up @@ -3,6 +3,7 @@
/*-
* Copyright (C) 2020-2021 Confluent, Inc.
*/

import io.confluent.parallelconsumer.state.WorkContainer;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -12,6 +13,7 @@

import java.time.Duration;
import java.util.Objects;
import java.util.function.Function;

import static io.confluent.csid.utils.StringUtils.msg;
import static io.confluent.parallelconsumer.ParallelConsumerOptions.CommitMode.PERIODIC_TRANSACTIONAL_PRODUCER;
Expand Down Expand Up @@ -143,6 +145,16 @@ public enum CommitMode {
@Builder.Default
private final Duration defaultMessageRetryDelay = Duration.ofSeconds(1);

/**
* When present, use this to generate the retry delay, instad of {@link #getDefaultMessageRetryDelay()}.
* <p>
* Overrides {@link #defaultMessageRetryDelay}, even if it's set.
*/
@Builder.Default
private final Function<WorkContainer, Duration> retryDelayProvider;

public static Function<WorkContainer, Duration> retryDelayProviderStatic;

public void validate() {
Objects.requireNonNull(consumer, "A consumer must be supplied");

Expand All @@ -153,6 +165,7 @@ public void validate() {

//
WorkContainer.setDefaultRetryDelay(getDefaultMessageRetryDelay());
ParallelConsumerOptions.retryDelayProviderStatic = getRetryDelayProvider();
}

public boolean isUsingTransactionalProducer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import io.confluent.csid.utils.WallClock;
import io.confluent.parallelconsumer.ParallelConsumerOptions;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
Expand Down Expand Up @@ -61,6 +62,9 @@ public class WorkContainer<K, V> implements Comparable<WorkContainer> {
*/
private Duration retryDelay;

/**
* @see ParallelConsumerOptions#getDefaultMessageRetryDelay()
*/
@Setter
static Duration defaultRetryDelay = Duration.ofSeconds(1);

Expand Down Expand Up @@ -123,10 +127,15 @@ private Temporal tryAgainAt(WallClock clock) {
}

public Duration getRetryDelay() {
if (retryDelay == null)
return defaultRetryDelay;
else
return retryDelay;
var retryDelayProvider = ParallelConsumerOptions.retryDelayProviderStatic;
if (retryDelayProvider != null) {
return retryDelayProvider.apply(this);
} else {
if (retryDelay == null)
return defaultRetryDelay;
else
return retryDelay;
}
}

@Override
Expand Down Expand Up @@ -193,4 +202,4 @@ public long offset() {
public boolean hasPreviouslyFailed() {
return getNumberOfFailedAttempts() > 0;
}
}
}