mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(sfn): Map ItemReader with s3:listObjectsV2 (#3460)
* feat(sfn): support the s3:listObjectsV2 Distributed Map ItemReader The Map state listed the resource as accepted at CreateStateMachine but failed every execution with States.ItemReaderFailed. It now calls the S3 service in process, reads every page under Prefix, and gives each child the AWS item shape: Etag, Key, LastModified, Size and StorageClass. Bucket and Prefix come from Parameters or JSONata Arguments. An empty prefix succeeds with zero iterations and ReaderConfig.MaxItems trims the list. LastModified is epoch seconds. Real AWS renders it as a double in JSONPath state machines and as an integer in JSONata ones, and the emulator does the same. Fixes #3409 * fix(sfn): evaluate ItemSelector in JSONata Map states A JSONata Map state passed ItemSelector through the JSONPath resolver, so every {% %} expression reached the child as a literal string. The Map state now resolves ItemSelector as a JSONata template with $states.input bound to the Map input and $states.context.Map.Item carrying Value and Index, as on AWS. Real AWS evaluates ItemSelector before it records MapIterationStarted. A failing expression therefore fails the Map state without any event for that iteration, in both query languages. The evaluation moved ahead of the iteration event to match. Verified against real Step Functions in us-east-1 on 2026-09-12. * chore(sfn): simplify the listObjectsV2 reader and the ItemSelector failure path The reader listed the bucket page by page although the S3 service returns the whole sorted listing in one call. The Map state kept two copies of the iteration failure handling. The tests reuse the shared mapper and one JSONata Map definition. * perf(sfn): cap the listObjectsV2 listing at MaxItems ReaderConfig.MaxItems keeps the first keys in key order on AWS, which is what the S3 service returns for maxKeys. The reader now passes MaxItems to the listing instead of building every item and trimming afterwards. * chore(sfn): declare local types in the listObjectsV2 reader Replace var with explicit types in the production code this branch added. * chore(sfn): declare local types in the listObjectsV2 reader tests Replace var with explicit types in the test code this branch added. * fix(sfn): list every object when MaxItems is absent S3Service now treats max keys 0 as a request for an empty page, as AWS does for max-keys=0. The listObjectsV2 reader passed 0 when ReaderConfig.MaxItems was not set, so the Map got no items. It now asks for Integer.MAX_VALUE in that case, like the DynamoDB import path.
This commit is contained in:
1 parent
c317fa52d2
commit
5db516d9d5
3 files changed
+371
-26
No files matched your search
+293
-9
@@ -2,18 +2,26 @@ package io.github.hectorvent.floci.services.stepfunctions;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.services.s3.S3Service;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.Response;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@QuarkusTest
|
||||
class StepFunctionsJsonataIntegrationTest {
|
||||
|
||||
@Inject
|
||||
S3Service s3Service;
|
||||
|
||||
private static final String SFN_CONTENT_TYPE = "application/x-amz-json-1.0";
|
||||
private static final String ROLE_ARN = "arn:aws:iam::000000000000:role/test-role";
|
||||
|
||||
@@ -2260,6 +2268,91 @@ class StepFunctionsJsonataIntegrationTest {
|
||||
+ "returned nothing (undefined).", failure.jsonPath().getString("cause"));
|
||||
}
|
||||
|
||||
private static final String JSONATA_MAP_ITEM_SELECTOR_DEFINITION = """
|
||||
{
|
||||
"QueryLanguage": "JSONata",
|
||||
"StartAt": "M",
|
||||
"States": {
|
||||
"M": {
|
||||
"Type": "Map",
|
||||
"Items": "{% $states.input.numbers %}",
|
||||
"ItemSelector": {
|
||||
"n": "{% $states.context.Map.Item.Value %}",
|
||||
"i": "{% $states.context.Map.Item.Index %}",
|
||||
"label": "{% $states.input.label %}"
|
||||
},
|
||||
"ItemProcessor": {
|
||||
"StartAt": "P",
|
||||
"States": {"P": {"Type": "Pass", "End": true}}
|
||||
},
|
||||
"End": true
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
/**
|
||||
* Checked against real Step Functions (us-east-1, 2026-09-12): inside ItemSelector,
|
||||
* $states.input is the Map state's input and $states.context.Map.Item carries Value and Index.
|
||||
*/
|
||||
@Test
|
||||
void mapItemSelectorEvaluatesJsonataAgainstMapInputAndItemContext() throws Exception {
|
||||
String smArn = createStateMachine("jsonata-map-item-selector-test", JSONATA_MAP_ITEM_SELECTOR_DEFINITION);
|
||||
String output = waitForExecution(startExecution(smArn, "{\"numbers\":[10,20],\"label\":\"x\"}"));
|
||||
|
||||
assertEquals("[{\"n\":10,\"i\":0,\"label\":\"x\"},{\"n\":20,\"i\":1,\"label\":\"x\"}]", output);
|
||||
}
|
||||
|
||||
@Test
|
||||
void mapItemSelectorReturningNothingFailsTheStateNamingTheField() throws Exception {
|
||||
// Real AWS names 'ItemSelector/<field>'.
|
||||
String smArn = createStateMachine("jsonata-map-item-selector-returned-nothing-test",
|
||||
JSONATA_MAP_ITEM_SELECTOR_DEFINITION);
|
||||
String execArn = startExecution(smArn, "{\"numbers\":[10]}");
|
||||
Response failure = waitForExecutionFailure(execArn);
|
||||
|
||||
assertEquals("States.QueryEvaluationError", failure.jsonPath().getString("error"));
|
||||
assertEquals("An error occurred while executing the state 'M' (entered at the event id #2). "
|
||||
+ "The JSONata expression '$states.input.label' specified for the field 'ItemSelector/label' "
|
||||
+ "returned nothing (undefined).", failure.jsonPath().getString("cause"));
|
||||
// AWS evaluates ItemSelector before it records the iteration, so no MapIteration* event.
|
||||
assertEquals(List.of("ExecutionStarted", "MapStateEntered", "MapStateStarted",
|
||||
"EvaluationFailed", "MapStateFailed", "ExecutionFailed"), historyEventTypes(execArn));
|
||||
}
|
||||
|
||||
@Test
|
||||
void mapItemSelectorMissingJsonPathFailsBeforeTheIterationIsRecorded() throws Exception {
|
||||
// Real AWS: States.Runtime straight after MapStateStarted, no MapIteration* event.
|
||||
String definition = """
|
||||
{
|
||||
"StartAt": "M",
|
||||
"States": {
|
||||
"M": {
|
||||
"Type": "Map",
|
||||
"ItemsPath": "$.items",
|
||||
"ItemSelector": {"v.$": "$$.Map.Item.Value.x"},
|
||||
"ItemProcessor": {
|
||||
"StartAt": "P",
|
||||
"States": {"P": {"Type": "Pass", "End": true}}
|
||||
},
|
||||
"End": true
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
String smArn = createStateMachine("jsonpath-map-item-selector-missing-path-test", definition);
|
||||
String execArn = startExecution(smArn, "{\"items\":[{}]}");
|
||||
Response failure = waitForExecutionFailure(execArn);
|
||||
|
||||
assertEquals("States.Runtime", failure.jsonPath().getString("error"));
|
||||
assertTrue(failure.jsonPath().getString("cause").contains(
|
||||
"specified for the field 'v.$' could not be found in the input"),
|
||||
failure.jsonPath().getString("cause"));
|
||||
assertEquals(List.of("ExecutionStarted", "MapStateEntered", "MapStateStarted", "ExecutionFailed"),
|
||||
historyEventTypes(execArn));
|
||||
}
|
||||
|
||||
@Test
|
||||
void mapMaxConcurrencyReturningNothingFailsTheStateNamingTheField() throws Exception {
|
||||
// Real AWS names 'MaxConcurrency'. Before this guard the undefined value reached the
|
||||
@@ -2809,10 +2902,67 @@ class StepFunctionsJsonataIntegrationTest {
|
||||
.then().statusCode(400);
|
||||
}
|
||||
|
||||
/**
|
||||
* The listObjectsV2 ItemReader expectations below were checked against real Step Functions
|
||||
* (us-east-1, 2026-09-12): the item fields, the quoted Etag, LastModified as epoch seconds
|
||||
* rendered as a double in JSONPath state machines and as an integer in JSONata ones, keys
|
||||
* returned as stored, every page read, and an empty prefix succeeding with zero iterations.
|
||||
*/
|
||||
@Test
|
||||
void distributedMapWithListObjectsV2ItemReader_failsWithNotImplementedItemReaderError() throws Exception {
|
||||
void distributedMapWithListObjectsV2ItemReader_iteratesObjectsUnderPrefix() throws Exception {
|
||||
createBucket("map-inputs-list-objects");
|
||||
putObject("map-inputs-list-objects", "workers/a.json", "[]");
|
||||
putObject("map-inputs-list-objects", "workers/b+c.json", "{}");
|
||||
putObject("map-inputs-list-objects", "workers/c.json", "[1]");
|
||||
putObject("map-inputs-list-objects", "other/z.json", "[]");
|
||||
|
||||
String definition = listObjectsDefinition("""
|
||||
"Parameters": {
|
||||
"Bucket": "map-inputs-list-objects",
|
||||
"Prefix.$": "$.prefix"
|
||||
}
|
||||
""");
|
||||
|
||||
String smArn = createStateMachine("map-itemreader-s3-list-objects-v2-test", definition);
|
||||
String execArn = startExecution(smArn, "{\"prefix\":\"workers/\"}");
|
||||
String output = waitForExecution(execArn);
|
||||
JsonNode items = objectMapper.readTree(output);
|
||||
|
||||
assertEquals(3, items.size(), output);
|
||||
JsonNode first = items.get(0);
|
||||
assertEquals("\"d751713988987e9331980363e24189ce\"", first.path("Etag").asText());
|
||||
assertEquals("workers/a.json", first.path("Key").asText());
|
||||
assertTrue(first.path("LastModified").isDouble(), output);
|
||||
assertEquals(2, first.path("Size").asInt());
|
||||
assertEquals("STANDARD", first.path("StorageClass").asText());
|
||||
assertEquals("workers/b+c.json", items.get(1).path("Key").asText());
|
||||
assertEquals("workers/c.json", items.get(2).path("Key").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void distributedMapWithListObjectsV2ItemReader_emptyPrefixSucceedsWithNoIterations() throws Exception {
|
||||
createBucket("map-inputs-list-objects-empty");
|
||||
putObject("map-inputs-list-objects-empty", "other/z.json", "[]");
|
||||
|
||||
String definition = listObjectsDefinition("""
|
||||
"Parameters": {
|
||||
"Bucket": "map-inputs-list-objects-empty",
|
||||
"Prefix": "workers/"
|
||||
}
|
||||
""");
|
||||
|
||||
String smArn = createStateMachine("map-itemreader-s3-list-objects-v2-empty-test", definition);
|
||||
String execArn = startExecution(smArn, "{}");
|
||||
|
||||
assertEquals("[]", waitForExecution(execArn));
|
||||
}
|
||||
|
||||
@Test
|
||||
void distributedMapWithListObjectsV2ItemReader_honorsMaxItemsAndItemSelector() throws Exception {
|
||||
createBucket("map-inputs-list-objects-max");
|
||||
putObject("map-inputs-list-objects-max", "workers/a.json", "[]");
|
||||
putObject("map-inputs-list-objects-max", "workers/b.json", "[]");
|
||||
putObject("map-inputs-list-objects-max", "workers/c.json", "[]");
|
||||
|
||||
String definition = """
|
||||
{
|
||||
@@ -2823,13 +2973,17 @@ class StepFunctionsJsonataIntegrationTest {
|
||||
"ItemReader": {
|
||||
"Resource": "arn:aws:states:::s3:listObjectsV2",
|
||||
"ReaderConfig": {
|
||||
"InputType": "JSON"
|
||||
"MaxItems": 2
|
||||
},
|
||||
"Parameters": {
|
||||
"Bucket": "map-inputs-list-objects",
|
||||
"Bucket": "map-inputs-list-objects-max",
|
||||
"Prefix": "workers/"
|
||||
}
|
||||
},
|
||||
"ItemSelector": {
|
||||
"key.$": "$$.Map.Item.Value.Key",
|
||||
"size.$": "$$.Map.Item.Value.Size"
|
||||
},
|
||||
"ItemProcessor": {
|
||||
"ProcessorConfig": {
|
||||
"Mode": "DISTRIBUTED",
|
||||
@@ -2849,13 +3003,127 @@ class StepFunctionsJsonataIntegrationTest {
|
||||
}
|
||||
""";
|
||||
|
||||
String smArn = createStateMachine("map-itemreader-s3-list-objects-v2-test", definition);
|
||||
String smArn = createStateMachine("map-itemreader-s3-list-objects-v2-max-test", definition);
|
||||
String execArn = startExecution(smArn, "{}");
|
||||
Response failure = waitForExecutionFailure(execArn);
|
||||
JsonNode items = objectMapper.readTree(waitForExecution(execArn));
|
||||
|
||||
assertEquals("FAILED", failure.jsonPath().getString("status"));
|
||||
assertEquals("States.ItemReaderFailed", failure.jsonPath().getString("error"));
|
||||
assertTrue(failure.jsonPath().getString("cause").contains("not yet implemented by the emulator"));
|
||||
assertEquals(2, items.size());
|
||||
assertEquals("workers/a.json", items.get(0).path("key").asText());
|
||||
assertEquals(2, items.get(0).path("size").asInt());
|
||||
assertEquals("workers/b.json", items.get(1).path("key").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void distributedMapWithListObjectsV2ItemReader_readsMoreThanOneThousandObjects() throws Exception {
|
||||
// In-process puts; 1001 REST puts are slow.
|
||||
s3Service.createBucket("map-inputs-list-objects-pages", "us-east-1");
|
||||
for (int i = 1; i <= 1001; i++) {
|
||||
s3Service.putObject("map-inputs-list-objects-pages", String.format("workers/%04d.json", i),
|
||||
"[]".getBytes(), "application/json", new HashMap<>());
|
||||
}
|
||||
|
||||
String definition = listObjectsDefinition("""
|
||||
"Parameters": {
|
||||
"Bucket": "map-inputs-list-objects-pages",
|
||||
"Prefix": "workers/"
|
||||
}
|
||||
""");
|
||||
|
||||
String smArn = createStateMachine("map-itemreader-s3-list-objects-v2-pages-test", definition);
|
||||
String execArn = startExecution(smArn, "{}");
|
||||
JsonNode items = objectMapper.readTree(waitForExecution(execArn, 300));
|
||||
|
||||
assertEquals(1001, items.size());
|
||||
assertEquals("workers/0001.json", items.get(0).path("Key").asText());
|
||||
assertEquals("workers/1001.json", items.get(1000).path("Key").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void distributedMapWithListObjectsV2ItemReader_jsonataArgumentsAndItemSelectorBuildChildInputs() throws Exception {
|
||||
createBucket("map-inputs-list-objects-jsonata");
|
||||
putObject("map-inputs-list-objects-jsonata", "workers/a.json", "[]");
|
||||
putObject("map-inputs-list-objects-jsonata", "workers/b.json", "[]");
|
||||
|
||||
String definition = """
|
||||
{
|
||||
"QueryLanguage": "JSONata",
|
||||
"StartAt": "ProcessWorkers",
|
||||
"States": {
|
||||
"ProcessWorkers": {
|
||||
"Type": "Map",
|
||||
"ItemReader": {
|
||||
"Resource": "arn:aws:states:::s3:listObjectsV2",
|
||||
"Arguments": {
|
||||
"Bucket": "{% $states.input.bucket %}",
|
||||
"Prefix": "{% $states.input.prefix %}"
|
||||
}
|
||||
},
|
||||
"ItemSelector": {
|
||||
"bucket": "{% $states.input.bucket %}",
|
||||
"key": "{% $states.context.Map.Item.Value.Key %}",
|
||||
"modified": "{% $states.context.Map.Item.Value.LastModified %}"
|
||||
},
|
||||
"ItemProcessor": {
|
||||
"ProcessorConfig": {
|
||||
"Mode": "DISTRIBUTED",
|
||||
"ExecutionType": "STANDARD"
|
||||
},
|
||||
"StartAt": "PassItem",
|
||||
"States": {
|
||||
"PassItem": {
|
||||
"Type": "Pass",
|
||||
"End": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"End": true
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
String smArn = createStateMachine("map-itemreader-s3-list-objects-v2-jsonata-test", definition);
|
||||
String execArn = startExecution(smArn,
|
||||
"{\"bucket\":\"map-inputs-list-objects-jsonata\",\"prefix\":\"workers/\"}");
|
||||
String output = waitForExecution(execArn);
|
||||
JsonNode items = objectMapper.readTree(output);
|
||||
|
||||
assertEquals(2, items.size(), output);
|
||||
assertEquals("map-inputs-list-objects-jsonata", items.get(0).path("bucket").asText());
|
||||
assertEquals("workers/a.json", items.get(0).path("key").asText());
|
||||
assertTrue(items.get(0).path("modified").isIntegralNumber(), output);
|
||||
assertEquals("workers/b.json", items.get(1).path("key").asText());
|
||||
}
|
||||
|
||||
private static String listObjectsDefinition(String itemReaderFields) {
|
||||
return String.format("""
|
||||
{
|
||||
"StartAt": "ProcessWorkers",
|
||||
"States": {
|
||||
"ProcessWorkers": {
|
||||
"Type": "Map",
|
||||
"ItemReader": {
|
||||
"Resource": "arn:aws:states:::s3:listObjectsV2",
|
||||
%s
|
||||
},
|
||||
"ItemProcessor": {
|
||||
"ProcessorConfig": {
|
||||
"Mode": "DISTRIBUTED",
|
||||
"ExecutionType": "STANDARD"
|
||||
},
|
||||
"StartAt": "PassItem",
|
||||
"States": {
|
||||
"PassItem": {
|
||||
"Type": "Pass",
|
||||
"End": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"End": true
|
||||
}
|
||||
}
|
||||
}
|
||||
""", itemReaderFields);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -2990,7 +3258,11 @@ class StepFunctionsJsonataIntegrationTest {
|
||||
}
|
||||
|
||||
private String waitForExecution(String execArn) throws InterruptedException {
|
||||
for (int i = 0; i < 50; i++) {
|
||||
return waitForExecution(execArn, 50);
|
||||
}
|
||||
|
||||
private String waitForExecution(String execArn, int attempts) throws InterruptedException {
|
||||
for (int i = 0; i < attempts; i++) {
|
||||
Response resp = describeExecution(execArn);
|
||||
String status = resp.jsonPath().getString("status");
|
||||
if ("SUCCEEDED".equals(status)) {
|
||||
@@ -3021,6 +3293,18 @@ class StepFunctionsJsonataIntegrationTest {
|
||||
return null;
|
||||
}
|
||||
|
||||
private List<String> historyEventTypes(String execArn) {
|
||||
return given()
|
||||
.header("X-Amz-Target", "AWSStepFunctions.GetExecutionHistory")
|
||||
.contentType(SFN_CONTENT_TYPE)
|
||||
.body(String.format("""
|
||||
{ "executionArn": "%s" }
|
||||
""", execArn))
|
||||
.when()
|
||||
.post("/")
|
||||
.jsonPath().getList("events.type", String.class);
|
||||
}
|
||||
|
||||
private Response describeExecution(String execArn) {
|
||||
return given()
|
||||
.header("X-Amz-Target", "AWSStepFunctions.DescribeExecution")
|
||||
|
||||
Reference in new issue
Block a user