mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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:
1 parent
cd6e516929
commit
7abba35051
4 files changed
+204
-11
No files matched your search
+38
-5
@@ -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);
|
||||
}
|
||||
|
||||
+35
@@ -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 """
|
||||
{
|
||||
|
||||
+100
@@ -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 = """
|
||||
|
||||
Reference in new issue
Block a user