diff --git a/docs/services/step-functions.md b/docs/services/step-functions.md index 30d0edda69..2e3d78716f 100644 --- a/docs/services/step-functions.md +++ b/docs/services/step-functions.md @@ -94,9 +94,16 @@ JSONata's own `$string` follows AWS's number notation: a whole number is written `1e21` and in exponent notation from there, on both signs, so `$string(1e20)` is `100000000000000000000` and `$string(1e21)` is `1e+21`. -One deviation. AWS bounds the memory an expression may use and fails a `$range` of roughly half a -million elements with `Expression evaluation memory limit exceeded`; Floci has no such bound and -builds the array. +Evaluation is bounded, as it is on AWS: one expression may run for five seconds and nest 500 +levels deep, and past either the state fails with `States.QueryEvaluationError`. Both bounds sit +well above what AWS itself accepts, so an expression that evaluates there evaluates here. AWS +refuses a non-tail-recursive function past a nesting depth near 100, and the largest sequence it +accepts evaluates here in about a tenth of a second. + +One deviation. AWS also bounds the *memory* an expression may use, refusing +`[1..900000] ~> $count()` with `Expression evaluation memory limit exceeded` while accepting the +same expression at 800,000 elements. Floci bounds time and depth but not memory, and its ranges +are lazy, so that expression answers immediately at any size. ## Mocked service integrations diff --git a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/JsonataEvaluator.java b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/JsonataEvaluator.java index a908639b5a..388fe98a5d 100644 --- a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/JsonataEvaluator.java +++ b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/JsonataEvaluator.java @@ -74,12 +74,49 @@ public class JsonataEvaluator { "a", "arrays", "b", "booleans", "f", "functions", "n", "numbers", "o", "objects", "s", "strings"); + /** + * How long one expression may evaluate before the state fails with + * {@code States.QueryEvaluationError}. AWS bounds evaluation and Floci did not, so an + * expression with no base case pinned a core and left the execution {@code RUNNING} for ever. + * JSONata optimises the tail call, so such an expression loops instead of overflowing the + * stack: no {@code Error} is thrown and only a clock can end it. + * + *

Five seconds is the dashjoin library's own default and roughly fifty times the slowest + * evaluation of a payload AWS itself accepts: AWS refuses a sequence of 900,000 elements on + * its memory limit, and summing one that size here takes about 100 ms. Deliberately generous, + * because a bound that fails a working state machine costs more than one that lets a runaway + * expression burn a core for five seconds. + */ + static final long EVALUATION_TIMEOUT_MILLIS = 5_000; + + /** + * How deep evaluation may nest before the state fails. A non-tail-recursive function nests at + * roughly {@code 3n+5} for {@code $f(n)}; measured against AWS with {@code test-state}, AWS + * accepts {@code n=30} (depth 95) and refuses {@code n=40} (depth 125), so its own ceiling + * sits near 100. Five times that leaves no expression AWS accepts able to reach this bound, + * and it still trips before the JVM stack does: on a 1 MB thread stack the bound raises a + * JSONata error while 1000 or more overflows the stack instead. + */ + static final int MAX_EVALUATION_DEPTH = 500; + private final ObjectMapper objectMapper; private final ObjectReader strictJsonReader; private final Map stepFunctionsExtensions; + private final long evaluationTimeoutMillis; + private final int maxEvaluationDepth; @Inject public JsonataEvaluator(ObjectMapper objectMapper) { + this(objectMapper, EVALUATION_TIMEOUT_MILLIS, MAX_EVALUATION_DEPTH); + } + + /** + * Bounds evaluation at values other than the shipped ones, so a test can trip the time bound + * without waiting {@link #EVALUATION_TIMEOUT_MILLIS} for it. + */ + JsonataEvaluator(ObjectMapper objectMapper, long evaluationTimeoutMillis, int maxEvaluationDepth) { + this.evaluationTimeoutMillis = evaluationTimeoutMillis; + this.maxEvaluationDepth = maxEvaluationDepth; this.objectMapper = objectMapper; // AWS rejects what a lenient parser accepts: a second document after the first, a repeated // key, an empty string. All three come back as D3137 there, so $parse reads through this @@ -126,6 +163,10 @@ public class JsonataEvaluator { try { Jsonata jsonataExpr = jsonata(expr); Jsonata.Frame frame = jsonataExpr.createFrame(); + // Without this the library evaluates unbounded: a recursive expression with no base + // case loops for ever and the execution never leaves RUNNING. The bounds raise a + // JSONata error, which the catch below turns into States.QueryEvaluationError. + frame.setRuntimeBounds(evaluationTimeoutMillis, maxEvaluationDepth); stepFunctionsExtensions.forEach(frame::bind); // Workflow variables (the Assign field) are referenced as top-level $name in AWS's // JSONata dialect, e.g. $CheckpointCount. They are bound alongside $states, never diff --git a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/JsonataEvaluatorTest.java b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/JsonataEvaluatorTest.java index f37573b381..e9d09fe268 100644 --- a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/JsonataEvaluatorTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/JsonataEvaluatorTest.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.NullNode; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; import static org.junit.jupiter.api.Assertions.*; @@ -550,6 +551,74 @@ class JsonataEvaluatorTest { assertEquals("[1,2]", result.toString()); } + /** + * The expression from issue #2667. JSONata optimises the tail call, so it loops instead of + * overflowing the stack: nothing is thrown, and before the time bound the execution stayed + * RUNNING for ever with a core pinned. The evaluator is built with a 250 ms bound rather than + * the shipped {@link JsonataEvaluator#EVALUATION_TIMEOUT_MILLIS} so the suite stays fast, and + * the method timeout makes an unbounded evaluation fail the test instead of blocking the run. + */ + @Test + @Timeout(30) + void recursionWithNoBaseCaseFailsOnTheTimeBound() { + JsonataEvaluator shortlyBounded = + new JsonataEvaluator(objectMapper, 250, JsonataEvaluator.MAX_EVALUATION_DEPTH); + JsonNode statesVar = objectMapper.createObjectNode(); + + AslExecutor.FailStateException ex = assertThrows(AslExecutor.FailStateException.class, + () -> shortlyBounded.evaluate("{% ($f := function($x){ $f($x+1) }; $f(1)) %}", statesVar)); + + assertEquals("States.QueryEvaluationError", ex.error); + assertEquals("Expression evaluation timeout: Check for infinite loop", ex.cause); + } + + /** + * Recursion the library cannot optimise grows the evaluation nesting instead of looping, and + * without a depth bound it ends in a StackOverflowError, which is an Error and so escapes the + * evaluator's catch. The shipped bound is what turns it into a state failure. + * + *

The cause is AWS's own string for this failure, followed by the {@code Depth=N max=M} + * suffix that names the bound that was hit. AWS writes one space after the full stop where + * the library writes two. + */ + @Test + @Timeout(30) + void nonTailRecursionFailsOnTheDepthBound() { + JsonNode statesVar = objectMapper.createObjectNode(); + + AslExecutor.FailStateException ex = assertThrows(AslExecutor.FailStateException.class, + () -> evaluator.evaluate( + "{% ($f := function($x){ $x <= 0 ? 0 : 1 + $f($x-1) }; $f(1000000)) %}", statesVar)); + + assertEquals("States.QueryEvaluationError", ex.error); + assertEquals("Stack overflow error: Check for non-terminating recursive" + + " function. Consider rewriting as tail-recursive. Depth=501 max=500", ex.cause); + } + + /** + * The boundary from below: the risk of a bound is that it fails a working state machine, so + * these must all still evaluate. Measured with {@code aws stepfunctions test-state}, AWS + * returns 30 for the first and 800000 for the second, which makes them the ceiling of what + * AWS itself accepts on each axis: it refuses the recursion at n=40 (nesting depth 125) and + * the sequence at 900,000 elements. + * + *

The last two AWS refuses on its memory limit and Floci evaluates, which is where the + * bounds are deliberately looser than AWS rather than tighter. + */ + @Test + @Timeout(60) + void expensiveButLegitimateExpressionsStayWithinTheShippedBounds() { + JsonNode statesVar = objectMapper.createObjectNode(); + String recursionHead = "{% ($f := function($x){ $x <= 0 ? 0 : 1 + $f($x-1) }; $f("; + + assertEquals(30, evaluator.evaluate(recursionHead + "30)) %}", statesVar).asInt()); + assertEquals(800000, evaluator.evaluate("{% [1..800000] ~> $count() %}", statesVar).asInt()); + + assertEquals(160, evaluator.evaluate(recursionHead + "160)) %}", statesVar).asInt()); + assertEquals(40000200000L, + evaluator.evaluate("{% $sum($map([1..200000], function($v){$v * 2})) %}", statesVar).asLong()); + } + @Test void string_writesAWholeNumberInFullBelowTheExponentBoundary() { // dashjoin stops writing digits at Long.MAX_VALUE, 9.223372036854776E18, and prints