FINERACT-2396: Fix retry issue during persisting CommandSource

This commit is contained in:
Adam Saghy
2025-10-21 18:38:05 +02:00
parent a561264f81
commit d6354ee408
2 changed files with 10 additions and 4 deletions
@@ -30,6 +30,7 @@ import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@@ -166,13 +167,12 @@ public class SynchronousCommandProcessingService implements CommandProcessingSer
try {
CommandSource finalCommandSource = commandSource;
AtomicInteger attemptNumber = new AtomicInteger(0);
CommandSource savedCommandSource = persistenceRetry.executeSupplier(() -> {
// Get metrics for logging
long attemptNumber = persistenceRetry.getMetrics().getNumberOfFailedCallsWithRetryAttempt() + 1;
// Critical: Refetch on retry attempts (not on first attempt)
CommandSource currentSource = finalCommandSource;
if (attemptNumber > 1) {
attemptNumber.getAndIncrement();
if (attemptNumber.get() > 1 && commandSource.getId() != null) {
log.info("Retrying command result save - attempt {} for command ID {}", attemptNumber, finalCommandSource.getId());
currentSource = commandSourceService.getCommandSource(finalCommandSource.getId());
}
@@ -591,5 +591,11 @@ public class SynchronousCommandProcessingServiceTest {
assertEquals(persistentException, exception.getCause());
assertTrue(saveAttempts.get() >= 3, "Expected at least 3 save attempts, but got: " + saveAttempts.get());
// First call was before saving CommandSource, then 2 calls during retry (1st retry does not try to fetch it)
verify(commandSourceService, times(3)).getCommandSource(commandId);
// Let test whether consecutive calls does not try to refetch immediately
when(commandSourceService.saveResultSameTransaction(any(CommandSource.class))).thenReturn(commandSource);
underTest.executeCommand(commandWrapper, jsonCommand, false);
verify(commandSourceService, times(4)).getCommandSource(commandId);
}
}