From 08d7319b4ff79c6687d9e41a83dea60fc80e072d Mon Sep 17 00:00:00 2001 From: Andre Kurait Date: Fri, 30 Oct 2026 13:19:47 -0500 Subject: [PATCH] fix(swf): sweep executions in their owning accounts (#7033) Promote the original two-file fork #93 change onto current main. Carry the stored execution account through timeout, child policy and parent notification writes without mutating request context. Original source and metadata are retained separately. Signed-off-by: Andre Kurait --- .../floci/services/swf/SwfService.java | 56 +++++--- .../floci/services/swf/SwfServiceTest.java | 132 ++++++++++++++++++ 2 files changed, 169 insertions(+), 19 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/swf/SwfService.java b/src/main/java/io/github/hectorvent/floci/services/swf/SwfService.java index 4b50950636..715a9eb3ab 100644 --- a/src/main/java/io/github/hectorvent/floci/services/swf/SwfService.java +++ b/src/main/java/io/github/hectorvent/floci/services/swf/SwfService.java @@ -5,6 +5,7 @@ import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.common.Resettable; +import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; import io.github.hectorvent.floci.services.swf.model.SwfActivityTask; @@ -72,7 +73,7 @@ public class SwfService implements Resettable { private final StorageBackend domainStore; private final StorageBackend workflowTypeStore; private final StorageBackend activityTypeStore; - private final StorageBackend executionStore; + private final AccountAwareStorageBackend executionStore; private final Map decisionTokens = new ConcurrentHashMap<>(); private final Map activityTokens = new ConcurrentHashMap<>(); @@ -1469,13 +1470,18 @@ public class SwfService implements Resettable { private void deliverCancelRequest(SwfWorkflowExecution execution, Map externalExecution, Long externalInitiatedEventId, String cause) { + deliverCancelRequest(execution, externalExecution, externalInitiatedEventId, cause, executionStore.accountId()); + } + + private void deliverCancelRequest(SwfWorkflowExecution execution, Map externalExecution, + Long externalInitiatedEventId, String cause, String accountId) { appendEvent(execution, "WorkflowExecutionCancelRequested") .attr("externalWorkflowExecution", externalExecution) .attr("externalInitiatedEventId", externalInitiatedEventId) .attr("cause", cause); execution.setCancelRequested(true); scheduleDecisionTaskIfIdle(execution); - executionStore.put(executionKey(execution), execution); + executionStore.putForAccount(accountId, executionKey(execution), execution); } public void terminateWorkflowExecution(String region, String domainName, String workflowId, String runId, @@ -1495,13 +1501,18 @@ public class SwfService implements Resettable { private void terminate(SwfWorkflowExecution execution, String reason, String details, String childPolicy, String cause) { + terminate(execution, reason, details, childPolicy, cause, executionStore.accountId()); + } + + private void terminate(SwfWorkflowExecution execution, String reason, String details, + String childPolicy, String cause, String accountId) { appendEvent(execution, "WorkflowExecutionTerminated") .attr("reason", reason) .attr("details", details) .attr("childPolicy", childPolicy) .attr("cause", cause); - closeExecution(execution, SwfConstants.CLOSE_STATUS_TERMINATED); - executionStore.put(executionKey(execution), execution); + closeExecution(execution, SwfConstants.CLOSE_STATUS_TERMINATED, accountId); + executionStore.putForAccount(accountId, executionKey(execution), execution); } // ──────────────────────────────── Tagging ──────────────────────────────── @@ -1565,22 +1576,25 @@ public class SwfService implements Resettable { */ public void sweep() { double nowSeconds = now(); - for (String key : executionStore.keys()) { + for (AccountAwareStorageBackend.AccountEntry entry + : executionStore.scanAllAccountEntries(key -> true)) { + String accountId = entry.accountId(); + String key = entry.key(); synchronized (domainLockForKey(key)) { - SwfWorkflowExecution execution = executionStore.get(key).orElse(null); + SwfWorkflowExecution execution = executionStore.getForAccount(accountId, key).orElse(null); if (execution == null || !execution.isOpen()) { continue; } boolean changed = fireTimers(execution, nowSeconds) | timeOutActivities(execution, nowSeconds) | timeOutDecisionTask(execution, nowSeconds); - if (timeOutExecution(execution, nowSeconds)) { + if (timeOutExecution(execution, nowSeconds, accountId)) { changed = true; } else if (changed && execution.isOpen()) { scheduleDecisionTaskIfIdle(execution); } if (changed) { - executionStore.put(key, execution); + executionStore.putForAccount(accountId, key, execution); } } } @@ -1676,7 +1690,7 @@ public class SwfService implements Resettable { return true; } - private boolean timeOutExecution(SwfWorkflowExecution execution, double nowSeconds) { + private boolean timeOutExecution(SwfWorkflowExecution execution, double nowSeconds, String accountId) { Double startToClose = timeoutSeconds(execution.getExecutionStartToCloseTimeout()); if (startToClose == null || nowSeconds - execution.getStartTimestamp() < startToClose) { return false; @@ -1684,7 +1698,7 @@ public class SwfService implements Resettable { appendEvent(execution, "WorkflowExecutionTimedOut") .attr("timeoutType", "START_TO_CLOSE") .attr("childPolicy", execution.getChildPolicy()); - closeExecution(execution, SwfConstants.CLOSE_STATUS_TIMED_OUT); + closeExecution(execution, SwfConstants.CLOSE_STATUS_TIMED_OUT, accountId); return true; } @@ -1769,6 +1783,10 @@ public class SwfService implements Resettable { } private void closeExecution(SwfWorkflowExecution execution, String closeStatus) { + closeExecution(execution, closeStatus, executionStore.accountId()); + } + + private void closeExecution(SwfWorkflowExecution execution, String closeStatus, String accountId) { execution.setExecutionStatus(SwfConstants.EXECUTION_STATUS_CLOSED); execution.setCloseStatus(closeStatus); execution.setCloseTimestamp(now()); @@ -1787,8 +1805,8 @@ public class SwfService implements Resettable { } execution.getTimers().clear(); - notifyParent(execution); - applyChildPolicy(execution); + notifyParent(execution, accountId); + applyChildPolicy(execution, accountId); } /** @@ -1796,7 +1814,7 @@ public class SwfService implements Resettable { * matching ChildWorkflowExecution* event. CONTINUED_AS_NEW is deliberately silent: * the continuation run carries the parent link and reports for itself. */ - private void notifyParent(SwfWorkflowExecution execution) { + private void notifyParent(SwfWorkflowExecution execution, String accountId) { if (execution.getParentWorkflowId() == null || execution.getParentRunId() == null) { return; } @@ -1805,7 +1823,7 @@ public class SwfService implements Resettable { } String parentKey = executionKey(execution.getRegion(), execution.getDomain(), execution.getParentWorkflowId(), execution.getParentRunId()); - SwfWorkflowExecution parent = executionStore.get(parentKey).orElse(null); + SwfWorkflowExecution parent = executionStore.getForAccount(accountId, parentKey).orElse(null); if (parent == null || !parent.isOpen()) { return; } @@ -1841,7 +1859,7 @@ public class SwfService implements Resettable { } scheduleDecisionTaskIfIdle(parent); - executionStore.put(parentKey, parent); + executionStore.putForAccount(accountId, parentKey, parent); } private Object lastEventAttribute(SwfWorkflowExecution execution, String eventType, String attribute) { @@ -1859,21 +1877,21 @@ public class SwfService implements Resettable { * TERMINATE ends them, REQUEST_CANCEL asks their deciders to wind down, and * ABANDON leaves them running. */ - private void applyChildPolicy(SwfWorkflowExecution execution) { + private void applyChildPolicy(SwfWorkflowExecution execution, String accountId) { if (execution.getChildExecutions().isEmpty() || SwfConstants.CHILD_POLICY_ABANDON.equals(execution.getChildPolicy())) { return; } for (Map.Entry entry : execution.getChildExecutions().entrySet()) { String childKey = executionKey(execution.getRegion(), execution.getDomain(), entry.getKey(), entry.getValue()); - SwfWorkflowExecution child = executionStore.get(childKey).orElse(null); + SwfWorkflowExecution child = executionStore.getForAccount(accountId, childKey).orElse(null); if (child == null || !child.isOpen()) { continue; } if (SwfConstants.CHILD_POLICY_TERMINATE.equals(execution.getChildPolicy())) { - terminate(child, null, null, child.getChildPolicy(), "CHILD_POLICY_APPLIED"); + terminate(child, null, null, child.getChildPolicy(), "CHILD_POLICY_APPLIED", accountId); } else { - deliverCancelRequest(child, null, null, "CHILD_POLICY_APPLIED"); + deliverCancelRequest(child, null, null, "CHILD_POLICY_APPLIED", accountId); } } } diff --git a/src/test/java/io/github/hectorvent/floci/services/swf/SwfServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/swf/SwfServiceTest.java index be2c51267f..a73fc31df5 100644 --- a/src/test/java/io/github/hectorvent/floci/services/swf/SwfServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/swf/SwfServiceTest.java @@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.swf; import com.fasterxml.jackson.core.type.TypeReference; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.common.RequestContext; import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; import io.github.hectorvent.floci.core.storage.InMemoryStorage; import io.github.hectorvent.floci.core.storage.StorageBackend; @@ -18,11 +19,13 @@ import io.github.hectorvent.floci.services.swf.model.SwfHistoryEvent; import io.github.hectorvent.floci.services.swf.model.SwfWorkflowExecution; import io.github.hectorvent.floci.services.swf.model.SwfWorkflowType; import io.github.hectorvent.floci.testing.MutableClock; +import jakarta.enterprise.inject.Instance; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.ValueSource; +import org.mockito.Mockito; import java.nio.charset.StandardCharsets; import java.time.Duration; @@ -427,6 +430,135 @@ class SwfServiceTest { assertEquals(0, service.openTimerCount(execution)); } + @Test + void backgroundSweep_expiresTimersAcrossAccountsAndRegionsWithoutChangingRequestScope() { + RequestContext context = accountScopedService(); + for (String account : List.of("111111111111", "222222222222")) { + context.setAccountId(account); + for (String region : List.of(REGION, "eu-west-1")) { + service.registerDomain(DOMAIN, "scope", "1", Map.of(), region); + service.registerWorkflowType(region, DOMAIN, workflowType("W", "1")); + service.startWorkflowExecution(new StartWorkflowExecutionRequest(region, DOMAIN, + "same-workflow", "W", "1", null, null, null, null, null, null, null, null)); + SwfDecisionTask task = service.pollForDecisionTask(region, DOMAIN, "tl", "d").orElseThrow(); + service.respondDecisionTaskCompleted(task.getTaskToken(), List.of(new Decision("StartTimer", + Map.of("timerId", "same-timer", "startToFireTimeout", + account.equals("111111111111") ? "1" : "30"))), null); + } + } + context.setAccountId(null); // The background thread has no request account. + clock.advance(Duration.ofSeconds(2)); + service.sweep(); + assertNull(context.getAccountId()); + for (String account : List.of("111111111111", "222222222222")) { + context.setAccountId(account); + for (String region : List.of(REGION, "eu-west-1")) { + SwfWorkflowExecution execution = service.describeWorkflowExecution( + region, DOMAIN, "same-workflow", null); + assertEquals(account.equals("111111111111") ? 1 : 0, + execution.getEvents().stream().filter(e -> e.getEventType().equals("TimerFired")).count()); + assertEquals(account.equals("111111111111"), execution.isDecisionTaskOutstanding()); + } + } + context.setAccountId(null); + clock.advance(Duration.ofSeconds(30)); + service.sweep(); + service.sweep(); + assertThrows(AwsException.class, + () -> service.describeWorkflowExecution(REGION, DOMAIN, "same-workflow", null)); + for (String account : List.of("111111111111", "222222222222")) { + context.setAccountId(account); + for (String region : List.of(REGION, "eu-west-1")) { + assertEquals(1, service.getWorkflowExecutionHistory(region, DOMAIN, + "same-workflow", null, false).stream() + .filter(e -> e.getEventType().equals("TimerFired")).count()); + } + } + } + + @ParameterizedTest + @ValueSource(strings = {"TERMINATE", "REQUEST_CANCEL", "ABANDON"}) + void backgroundWorkflowTimeout_keepsChildPolicyInOwningAccount(String policy) { + RequestContext context = accountScopedService(); + Map parents = new LinkedHashMap<>(); + Map children = new LinkedHashMap<>(); + for (String account : List.of("111111111111", "222222222222")) { + context.setAccountId(account); + service.registerDomain(DOMAIN, "scope", "1", Map.of(), REGION); + SwfWorkflowType type = workflowType("W", "1"); + type.setDefaultExecutionStartToCloseTimeout(account.equals("111111111111") ? "1" : "300"); + type.setDefaultChildPolicy(policy); + service.registerWorkflowType(REGION, DOMAIN, type); + parents.put(account, start("parent")); + SwfDecisionTask task = service.pollForDecisionTask(REGION, DOMAIN, "tl", "d").orElseThrow(); + service.respondDecisionTaskCompleted(task.getTaskToken(), List.of(new Decision( + "StartChildWorkflowExecution", Map.of("workflowId", "child", + "workflowType", Map.of("name", "W", "version", "1"), + "executionStartToCloseTimeout", "300", "taskList", Map.of("name", "child")))), null); + children.put(account, service.describeWorkflowExecution(REGION, DOMAIN, "child", null).getRunId()); + } + context.setAccountId(null); + clock.advance(Duration.ofSeconds(2)); + service.sweep(); + assertNull(context.getAccountId()); + context.setAccountId("111111111111"); + assertEquals("TIMED_OUT", service.describeWorkflowExecution(REGION, DOMAIN, "parent", parents.get("111111111111")).getCloseStatus()); + SwfWorkflowExecution child = service.describeWorkflowExecution(REGION, DOMAIN, "child", children.get("111111111111")); + assertEquals(!policy.equals("TERMINATE"), child.isOpen()); + assertEquals(policy.equals("REQUEST_CANCEL"), child.isCancelRequested()); + context.setAccountId("222222222222"); + assertTrue(service.describeWorkflowExecution(REGION, DOMAIN, "parent", null).isOpen()); + child = service.describeWorkflowExecution(REGION, DOMAIN, "child", null); + assertTrue(child.isOpen()); + assertFalse(child.isCancelRequested()); + context.setAccountId(null); + assertThrows(AwsException.class, + () -> service.describeWorkflowExecution(REGION, DOMAIN, "child", null)); + } + + @Test + void backgroundChildTimeout_notifiesOnlyItsOwningParent() { + RequestContext context = accountScopedService(); + for (String account : List.of("111111111111", "222222222222")) { + context.setAccountId(account); + service.registerDomain(DOMAIN, "scope", "1", Map.of(), REGION); + service.registerWorkflowType(REGION, DOMAIN, workflowType("W", "1")); + start("parent"); + SwfDecisionTask task = service.pollForDecisionTask(REGION, DOMAIN, "tl", "d").orElseThrow(); + service.respondDecisionTaskCompleted(task.getTaskToken(), List.of(new Decision( + "StartChildWorkflowExecution", Map.of("workflowId", "child", + "workflowType", Map.of("name", "W", "version", "1"), + "executionStartToCloseTimeout", account.equals("111111111111") ? "1" : "300", + "taskList", Map.of("name", "child")))), null); + } + context.setAccountId(null); + clock.advance(Duration.ofSeconds(2)); + service.sweep(); + for (String account : List.of("111111111111", "222222222222")) { + context.setAccountId(account); + SwfWorkflowExecution parent = service.describeWorkflowExecution(REGION, DOMAIN, "parent", null); + assertEquals(account.equals("111111111111") ? 1 : 0, parent.getEvents().stream() + .filter(e -> e.getEventType().equals("ChildWorkflowExecutionTimedOut")).count()); + } + } + + @SuppressWarnings("unchecked") + private RequestContext accountScopedService() { + RequestContext context = new RequestContext(); + Instance request = Mockito.mock(Instance.class); + Mockito.when(request.get()).thenReturn(context); + StorageFactory factory = new StorageFactory(null, null) { + @Override + public synchronized AccountAwareStorageBackend create(String serviceName, + String fileName, TypeReference> typeReference) { + return new AccountAwareStorageBackend<>(new InMemoryStorage<>(), request, "000000000000"); + } + }; + service = new SwfService(factory, new RegionResolver(REGION, "000000000000"), + clock, recordingLambdaInvoker()); + return context; + } + @Test void timeoutSweep_neverTouchesClosedExecutions() { String runId = start("wf-closed");