Skip to content

Commit d6354ee

Browse files
committed
FINERACT-2396: Fix retry issue during persisting CommandSource
1 parent a561264 commit d6354ee

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

fineract-core/src/main/java/org/apache/fineract/commands/service/SynchronousCommandProcessingService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.ArrayList;
3131
import java.util.HashMap;
3232
import java.util.Map;
33+
import java.util.concurrent.atomic.AtomicInteger;
3334
import java.util.function.Supplier;
3435
import lombok.RequiredArgsConstructor;
3536
import lombok.extern.slf4j.Slf4j;
@@ -166,13 +167,12 @@ private CommandProcessingResult executeCommand(final CommandWrapper wrapper, fin
166167

167168
try {
168169
CommandSource finalCommandSource = commandSource;
170+
AtomicInteger attemptNumber = new AtomicInteger(0);
169171
CommandSource savedCommandSource = persistenceRetry.executeSupplier(() -> {
170-
// Get metrics for logging
171-
long attemptNumber = persistenceRetry.getMetrics().getNumberOfFailedCallsWithRetryAttempt() + 1;
172-
173172
// Critical: Refetch on retry attempts (not on first attempt)
174173
CommandSource currentSource = finalCommandSource;
175-
if (attemptNumber > 1) {
174+
attemptNumber.getAndIncrement();
175+
if (attemptNumber.get() > 1 && commandSource.getId() != null) {
176176
log.info("Retrying command result save - attempt {} for command ID {}", attemptNumber, finalCommandSource.getId());
177177
currentSource = commandSourceService.getCommandSource(finalCommandSource.getId());
178178
}

fineract-provider/src/test/java/org/apache/fineract/commands/service/SynchronousCommandProcessingServiceTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,5 +591,11 @@ public void testExecuteCommandWithMaxRetryFailure() {
591591
assertEquals(persistentException, exception.getCause());
592592

593593
assertTrue(saveAttempts.get() >= 3, "Expected at least 3 save attempts, but got: " + saveAttempts.get());
594+
// First call was before saving CommandSource, then 2 calls during retry (1st retry does not try to fetch it)
595+
verify(commandSourceService, times(3)).getCommandSource(commandId);
596+
// Let test whether consecutive calls does not try to refetch immediately
597+
when(commandSourceService.saveResultSameTransaction(any(CommandSource.class))).thenReturn(commandSource);
598+
underTest.executeCommand(commandWrapper, jsonCommand, false);
599+
verify(commandSourceService, times(4)).getCommandSource(commandId);
594600
}
595601
}

0 commit comments

Comments
 (0)