Skip to content

Commit 3d5bd1e

Browse files
authored
Merge pull request #146 from illuin-tech/resilience/fix/retry-count
fix: use a container for retry count
2 parents 97f6dcc + 787c5b5 commit 3d5bd1e

2 files changed

Lines changed: 38 additions & 20 deletions

File tree

resilience4j/src/main/java/tech/illuin/pipeline/resilience4j/sink/wrapper/retry/RetrySink.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import tech.illuin.pipeline.sink.execution.wrapper.SinkWrapperException;
1616

1717
import java.util.Map;
18+
import java.util.concurrent.ConcurrentHashMap;
1819

1920
import static tech.illuin.pipeline.metering.MeterRegistryKey.fill;
2021

@@ -26,6 +27,7 @@ public class RetrySink implements Sink
2627
private final Sink sink;
2728
private final Retry retry;
2829
private final RetrySinkHandler handler;
30+
private final Map<String, Integer> retryCounterContainer;
2931

3032
public static final String RUN_COUNT_KEY = "pipeline.sink.retry.run_count";
3133
public static final String RETRY_COUNT_KEY = "pipeline.sink.retry.retry_count";
@@ -41,6 +43,7 @@ public RetrySink(Sink sink, Retry retry, RetrySinkHandler handler)
4143
this.sink = sink;
4244
this.retry = retry;
4345
this.handler = handler;
46+
this.retryCounterContainer = new ConcurrentHashMap<>();
4447
}
4548

4649
@Override
@@ -98,12 +101,13 @@ private void onSuccess(Output output, LocalContext context)
98101
context.pipelineTag().pipeline(),
99102
context.pipelineTag().uid(),
100103
context.componentTag().id(),
101-
this.retry.getMetrics().getNumberOfTotalCalls()
104+
this.retryCounterContainer.get(context.pipelineTag().uid())
102105
);
103106
this.handler.onSuccess(output, context);
104107
}
105108
finally {
106109
counter(RUN_SUCCESS_KEY, context).increment();
110+
this.retryCounterContainer.remove(context.pipelineTag().uid());
107111
}
108112
}
109113

@@ -123,21 +127,26 @@ private void onError(Output output, LocalContext context, Exception ex)
123127
}
124128
finally {
125129
counter(RUN_FAILURE_KEY, context, Tag.of("error", ex.getClass().getName())).increment();
130+
this.retryCounterContainer.remove(context.pipelineTag().uid());
126131
}
127132
}
128133

129134
private void onAttempt(Output output, LocalContext context)
130135
{
131136
try {
132-
long totalCalls = this.retry.getMetrics().getNumberOfTotalCalls();
133-
logger.trace(
134-
"{}#{} retry wrapper {} - retry attempt #{} - {} left",
135-
context.pipelineTag().pipeline(),
136-
context.pipelineTag().uid(),
137-
context.componentTag().id(),
138-
totalCalls,
139-
this.retry.getRetryConfig().getMaxAttempts() - (totalCalls + 1)
140-
);
137+
if (!this.retryCounterContainer.containsKey(context.pipelineTag().uid()))
138+
this.retryCounterContainer.put(context.pipelineTag().uid(), 0);
139+
else {
140+
Integer retryCount = this.retryCounterContainer.merge(context.pipelineTag().uid(), 1, Integer::sum);
141+
logger.trace(
142+
"{}#{} retry wrapper {} - retry attempt #{} - {} left",
143+
context.pipelineTag().pipeline(),
144+
context.pipelineTag().uid(),
145+
context.componentTag().id(),
146+
retryCount,
147+
this.retry.getRetryConfig().getMaxAttempts() - retryCount
148+
);
149+
}
141150
this.handler.onRetry(output, context);
142151
}
143152
finally {

resilience4j/src/main/java/tech/illuin/pipeline/resilience4j/step/wrapper/retry/RetryStep.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import tech.illuin.pipeline.step.result.ResultView;
1818

1919
import java.util.Map;
20+
import java.util.concurrent.ConcurrentHashMap;
2021

2122
import static tech.illuin.pipeline.metering.MeterRegistryKey.fill;
2223

@@ -28,6 +29,7 @@ public class RetryStep<T extends Indexable, I> implements Step<T, I>
2829
private final Step<T, I> step;
2930
private final Retry retry;
3031
private final RetryStepHandler handler;
32+
private final Map<String, Integer> retryCounterContainer;
3133

3234
public static final String RUN_COUNT_KEY = "pipeline.step.retry.run_count";
3335
public static final String RETRY_COUNT_KEY = "pipeline.step.retry.retry_count";
@@ -43,6 +45,7 @@ public RetryStep(Step<T, I> step, Retry retry, RetryStepHandler handler)
4345
this.step = step;
4446
this.retry = retry;
4547
this.handler = handler;
48+
this.retryCounterContainer = new ConcurrentHashMap<>();
4649
}
4750

4851
@Override
@@ -101,12 +104,13 @@ private void onSuccess(T object, I input, Object payload, ResultView view, Local
101104
context.pipelineTag().pipeline(),
102105
context.pipelineTag().uid(),
103106
context.componentTag().id(),
104-
this.retry.getMetrics().getNumberOfTotalCalls()
107+
this.retryCounterContainer.get(context.pipelineTag().uid())
105108
);
106109
this.handler.onSuccess(object, input, payload, view, context);
107110
}
108111
finally {
109112
counter(RUN_SUCCESS_KEY, context).increment();
113+
this.retryCounterContainer.remove(context.pipelineTag().uid());
110114
}
111115
}
112116

@@ -126,21 +130,26 @@ private void onError(T object, I input, Object payload, ResultView view, LocalCo
126130
}
127131
finally {
128132
counter(RUN_FAILURE_KEY, context, Tag.of("error", ex.getClass().getName())).increment();
133+
this.retryCounterContainer.remove(context.pipelineTag().uid());
129134
}
130135
}
131136

132137
private void onAttempt(T object, I input, Object payload, ResultView view, LocalContext context)
133138
{
134139
try {
135-
long totalCalls = this.retry.getMetrics().getNumberOfTotalCalls();
136-
logger.trace(
137-
"{}#{} retry wrapper {} - retry attempt #{} - {} left",
138-
context.pipelineTag().pipeline(),
139-
context.pipelineTag().uid(),
140-
context.componentTag().id(),
141-
totalCalls,
142-
this.retry.getRetryConfig().getMaxAttempts() - (totalCalls + 1)
143-
);
140+
if (!this.retryCounterContainer.containsKey(context.pipelineTag().uid()))
141+
this.retryCounterContainer.put(context.pipelineTag().uid(), 0);
142+
else {
143+
Integer retryCount = this.retryCounterContainer.merge(context.pipelineTag().uid(), 1, Integer::sum);
144+
logger.trace(
145+
"{}#{} retry wrapper {} - retry attempt #{} - {} left",
146+
context.pipelineTag().pipeline(),
147+
context.pipelineTag().uid(),
148+
context.componentTag().id(),
149+
retryCount,
150+
this.retry.getRetryConfig().getMaxAttempts() - retryCount
151+
);
152+
}
144153
this.handler.onRetry(object, input, payload, view, context);
145154
}
146155
finally {

0 commit comments

Comments
 (0)