fix(sfn): accept a decimal ToleratedFailurePercentage at execution (#7297)

* fix(sfn): accept a decimal ToleratedFailurePercentage at execution

ToleratedFailurePercentage is a number from 0 to 100, not an integer. Create-time
validation already treats it that way, so a definition declaring 100.0 or 12.5 was
accepted by CreateStateMachine and then failed every execution of its Distributed Map
with States.Runtime "ToleratedFailurePercentage must resolve to an integer of 0 or more".

The execution path now resolves the percentage as a decimal, through its literal, its
Path or a JSONata expression, rejects values outside 0 to 100, and absorbs
floor(itemCount * percentage / 100) failed items. DescribeMapRun reports the declared
value unrounded. ToleratedFailureCount is unchanged and stays an integer.

* fix(sfn): accept a decimal ToleratedFailurePercentage only as a literal

The guide asks for "a value between zero and 100" for the literal, but says the Path
"must resolve to a positive integer between 0 and 100 at runtime" and a JSONata
expression must evaluate to an integer. The Path and JSONata forms keep main's integer
check, and the shared resolveMapField refactor is reverted with them.
This commit is contained in:
Ovidiu (Ovi) authored and GitHub committed 2026-11-02 09:36:11 -04:00
1 parent cd6e516929
commit 7abba35051
4 files changed
+204 -11

No files matched your search

@@ -87,7 +87,9 @@ import jakarta.ws.rs.core.Response;
import org.jboss.logging.Logger;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
@@ -3673,12 +3675,14 @@ public class AslExecutor {
? resolveToleranceField(stateDef, "ToleratedFailureCount", Integer.MAX_VALUE, mapInput,
jsonata, context, variables)
: 0;
int percentage = hasPercentage
? resolveToleranceField(stateDef, "ToleratedFailurePercentage", 100, mapInput, jsonata,
context, variables)
: 0;
BigDecimal percentage = hasPercentage
? resolveTolerancePercentage(stateDef, mapInput, jsonata, context, variables)
: BigDecimal.ZERO;
int fromPercentage = (int) ((long) itemCount * percentage / 100);
// The run fails once the failed share exceeds the percentage, so the absorbed count is the
// floor of itemCount * percentage / 100. BigDecimal keeps a decimal percentage exact.
int fromPercentage = BigDecimal.valueOf(itemCount).multiply(percentage)
.divide(BigDecimal.valueOf(100), 0, RoundingMode.FLOOR).intValue();
int threshold;
if (hasCount && hasPercentage) {
threshold = Math.min(count, fromPercentage);
@@ -3687,7 +3691,28 @@ public class AslExecutor {
} else {
threshold = fromPercentage;
}
return new ToleratedFailures(true, count, percentage, threshold);
return new ToleratedFailures(true, count, percentage.doubleValue(), threshold);
}
/**
* The guide asks for "a value between zero and 100" when ToleratedFailurePercentage is a literal, and
* DescribeMapRun models it as a float, so a literal may be a decimal such as {@code 100.0} or
* {@code 12.5}. Its Path must resolve to an integer and its JSONata expression must evaluate to one, so
* those forms keep the integer check.
*/
private BigDecimal resolveTolerancePercentage(JsonNode stateDef, JsonNode mapInput, boolean jsonata,
JsonNode context, ObjectNode variables) {
String field = "ToleratedFailurePercentage";
JsonNode literal = stateDef.get(field);
if (stateDef.has(field + "Path") || literal == null || !literal.isNumber()) {
return BigDecimal.valueOf(resolveToleranceField(stateDef, field, 100, mapInput, jsonata, context,
variables));
}
BigDecimal value = literal.decimalValue();
if (value.signum() < 0 || value.compareTo(BigDecimal.valueOf(100)) > 0) {
throw new FailStateException("States.Runtime", field + " must be a number between 0 and 100", field);
}
return value;
}
/** A tolerance is a percentage or a count, so it shares the Map integer resolver and adds a ceiling. */
@@ -105,7 +105,43 @@ class StepFunctionsContextObjectPathIntegrationTest {
assertEquals(2, results.size(), "results: " + results);
}
/**
* Only a literal ToleratedFailurePercentage may be a decimal: the guide says its Path "must resolve to a
* positive integer between 0 and 100 at runtime", so 50.5 read from the Context Object is refused.
*/
@Test
void toleratedFailurePercentagePathRefusesADecimal() throws Exception {
String definition = """
{"StartAt":"M","States":{"M":{"Type":"Map",
"InputPath":"$.payload","ItemsPath":"$.items",
"ToleratedFailurePercentagePath":"$$.Execution.Input.percentage",
"ItemProcessor":{"ProcessorConfig":{"Mode":"DISTRIBUTED","ExecutionType":"STANDARD"},
"StartAt":"Check","States":{
"Check":{"Type":"Choice",
"Choices":[{"Variable":"$.n","NumericLessThan":0,"Next":"Boom"}],
"Default":"Keep"},
"Boom":{"Type":"Fail","Error":"ItemFailed"},
"Keep":{"Type":"Pass","End":true}}},
"End":true}}}
""";
Response failure = execute(definition,
"{\"percentage\":50.5,\"payload\":{\"items\":[{\"n\":1},{\"n\":-1}]}}");
assertEquals("FAILED", failure.jsonPath().getString("status"));
assertEquals("States.Runtime", failure.jsonPath().getString("error"));
}
private String run(String definition, String input) throws InterruptedException {
Response describe = execute(definition, input);
if (!"SUCCEEDED".equals(describe.jsonPath().getString("status"))) {
fail("Execution failed: " + describe.body().asString());
}
return describe.jsonPath().getString("output");
}
/** Runs the definition and returns the DescribeExecution response once the execution has ended. */
private Response execute(String definition, String input) throws InterruptedException {
String name = "context-path-" + System.nanoTime();
Response create = given()
.header("X-Amz-Target", "AWSStepFunctions.CreateStateMachine")
@@ -131,11 +167,8 @@ class StepFunctionsContextObjectPathIntegrationTest {
.body("{\"executionArn\":\"" + execArn + "\"}")
.when().post("/");
String status = describe.jsonPath().getString("status");
if ("SUCCEEDED".equals(status)) {
return describe.jsonPath().getString("output");
}
if ("FAILED".equals(status)) {
fail("Execution failed: " + describe.body().asString());
if ("SUCCEEDED".equals(status) || "FAILED".equals(status)) {
return describe;
}
Thread.sleep(100);
}
@@ -371,6 +371,41 @@ class StepFunctionsDescribeMapRunIntegrationTest {
assertEquals(50.0, response.jsonPath().getDouble("toleratedFailurePercentage"));
}
/** A decimal percentage is reported as declared, not truncated to an integer. */
@Test
@Order(14)
void describeMapRunReportsADecimalToleratedFailurePercentage() throws Exception {
String definition = """
{
"QueryLanguage": "JSONata",
"StartAt": "Fan",
"States": {
"Fan": {
"Type": "Map",
"End": true,
"Items": [{"n": 1}, {"n": 2}],
"ToleratedFailurePercentage": 12.5,
"ItemProcessor": {
"ProcessorConfig": {"Mode": "DISTRIBUTED", "ExecutionType": "STANDARD"},
"StartAt": "Keep",
"States": {"Keep": {"Type": "Pass", "End": true}}
}
}
}
}
""";
String smArn = createStateMachine("describe-map-run-decimal-percentage", definition);
String execArn = startExecution(smArn, "{}");
Response describe = waitForTerminalState(execArn);
assertEquals("SUCCEEDED", describe.jsonPath().getString("status"),
"cause: " + describe.jsonPath().getString("cause"));
Response response = describeMapRun(mapRunArnFromHistory(execArn));
response.then().statusCode(200);
assertEquals(12.5, response.jsonPath().getDouble("toleratedFailurePercentage"));
}
private static String distributedMap(String items, String bucket, Integer maxConcurrency) {
return """
{
@@ -2078,6 +2078,106 @@ class StepFunctionsJsonataIntegrationTest {
assertEquals(3, results.size(), "one of four items may fail at 25 percent: " + output);
}
/** AWS models the percentage as a number, so a definition may spell 100 as 100.0. */
@Test
void distributedMapToleratedFailurePercentage_acceptsADecimalPercentage() throws Exception {
String definition = toleratedPercentageMap("[{\"n\": 1}, {\"n\": 2}, {\"n\": 3}, {\"n\": -1}]", "100.0");
String smArn = createStateMachine("map-tolerated-percentage-decimal", definition);
String execArn = startExecution(smArn, "{}");
String output = waitForExecution(execArn);
JsonNode results = new ObjectMapper().readTree(output);
assertEquals(3, results.size(), "every item may fail at 100.0 percent: " + output);
}
/** 50.5 percent of four items absorbs two failures: the threshold is the floor of 2.02. */
@Test
void distributedMapToleratedFailurePercentage_absorbsFailuresUpToAFractionalThreshold() throws Exception {
String definition = toleratedPercentageMap("[{\"n\": 1}, {\"n\": 2}, {\"n\": -1}, {\"n\": -2}]", "50.5");
String smArn = createStateMachine("map-tolerated-percentage-fraction-within", definition);
String execArn = startExecution(smArn, "{}");
String output = waitForExecution(execArn);
JsonNode results = new ObjectMapper().readTree(output);
assertEquals(2, results.size(), "two of four items may fail at 50.5 percent: " + output);
}
/** 49.5 percent of four items absorbs one failure, so a second fails the Map. */
@Test
void distributedMapToleratedFailurePercentage_failsPastAFractionalThreshold() throws Exception {
String definition = toleratedPercentageMap("[{\"n\": 1}, {\"n\": 2}, {\"n\": -1}, {\"n\": -2}]", "49.5");
String smArn = createStateMachine("map-tolerated-percentage-fraction-exceeded", definition);
String execArn = startExecution(smArn, "{}");
Response failure = waitForExecutionFailure(execArn);
assertEquals("FAILED", failure.jsonPath().getString("status"));
assertEquals("States.ExceedToleratedFailureThreshold", failure.jsonPath().getString("error"));
}
/** Only a literal may be a decimal: a JSONata expression must evaluate to an integer, as the guide says. */
@Test
void distributedMapToleratedFailurePercentage_refusesADecimalJsonataExpression() throws Exception {
String definition = toleratedPercentageMap("[{\"n\": 1}, {\"n\": -1}]", "\"{% 50.5 %}\"");
String smArn = createStateMachine("map-tolerated-percentage-jsonata-decimal", definition);
String execArn = startExecution(smArn, "{}");
Response failure = waitForExecutionFailure(execArn);
assertEquals("FAILED", failure.jsonPath().getString("status"));
assertEquals("States.QueryEvaluationError", failure.jsonPath().getString("error"));
}
/** A Distributed Map over {@code items} in which every negative {@code n} fails its item. */
private static String toleratedPercentageMap(String items, String percentage) {
return """
{
"QueryLanguage": "JSONata",
"StartAt": "Fan",
"States": {
"Fan": {
"Type": "Map",
"Items": ITEMS,
"ToleratedFailurePercentage": PERCENTAGE,
"ItemProcessor": {
"ProcessorConfig": {
"Mode": "DISTRIBUTED",
"ExecutionType": "STANDARD"
},
"StartAt": "Check",
"States": {
"Check": {
"Type": "Choice",
"Choices": [
{
"Condition": "{% $states.input.n < 0 %}",
"Next": "Boom"
}
],
"Default": "Keep"
},
"Boom": {
"Type": "Fail",
"Error": "ItemFailed",
"Cause": "the item asked to fail"
},
"Keep": {
"Type": "Pass",
"End": true
}
}
},
"End": true
}
}
}
"""
.replace("ITEMS", items)
.replace("PERCENTAGE", percentage);
}
@Test
void distributedMapWithoutAToleratedFailure_stillFailsWithTheItemsOwnError() throws Exception {
String definition = """