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 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