fix(emr-serverless): list same-millisecond runs and sessions newest first, and compare retries as held (#8735)

ListJobRuns and ListSessions ordered runs and sessions by their creation
millisecond, breaking a tie by the random id, so of two started within
the same millisecond the newer could list second. Each stored run and
session now keeps a creation sequence, the microseconds of its creation
or one past the latest its service gave, which the listing breaks a tie
by. One stored before keeps 0, and its id orders it, as before. A
nextToken given before, whose cursor holds no sequence, is refused with
"Invalid nextToken." rather than resuming at the wrong place.

A StartJobRun or StartSession retry was compared with the original as
raw request JSON, so one that left out a member the first gave at its
default, or empty, was refused with ConflictException. The two are now
compared as the run or session each would start holds them, built by
the same code that starts one: a run's execution timeout left out or
720, a session's idle timeout left out or 60, tags left out or empty,
and a session's configurationOverrides without a runtimeConfiguration
count as the same. Anything the run would hold otherwise still
conflicts, a mode given where the original left it out, a tag with an
empty value or another name among them. Classic EMR sessions compare
their retries as held since #8699.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-17 09:00:45 -04:00
1 parent 83c64f5d91
commit 1a26ef4f73
8 files changed
+264 -43

No files matched your search

@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.Pagination;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.storage.AccountAwareStorageBackend;
import io.floci.aws.core.storage.StorageFactory;
@@ -14,6 +15,7 @@ import io.floci.aws.services.emrserverless.model.JobDriver;
import io.floci.aws.services.emrserverless.model.JobRun;
import io.floci.aws.services.emrserverless.model.JobRunAttemptSummary;
import io.floci.aws.services.emrserverless.model.JobRunRecord;
import io.floci.aws.services.emrserverless.model.JobRunSummary;
import io.floci.aws.services.emrserverless.model.MaximumCapacity;
import io.floci.aws.services.emrserverless.model.MonitoringConfiguration;
import io.floci.aws.services.emrserverless.model.RuntimeConfiguration;
@@ -25,8 +27,10 @@ import io.floci.aws.testing.MutableClock;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
@@ -101,6 +105,66 @@ class EmrServerlessJobRunServiceTest {
return request;
}
@Test
void runsStartedWithinTheSameMillisecondListNewestFirst() {
EmrServerlessJobRunService service = new EmrServerlessJobRunService(store, applications, iamService,
new RegionResolver(REGION, ACCOUNT_ID), null, objectMapper, 0,
Clock.fixed(Instant.parse("2026-01-01T00:00:00Z"), ZoneOffset.UTC));
List<String> started = new ArrayList<>();
for (int run = 0; run < 12; run++) {
started.add(service.startJobRun(applicationId, request(null, null)).getJobRunId());
}
List<String> listed = service.listJobRuns(applicationId, null, null, null, null, null, null).items().stream()
.map(JobRunSummary::getId).toList();
assertEquals(started.reversed(), listed);
}
@Test
void aRetryIsTheOriginalRunWhereTheRunWouldHoldTheSameAndConflictsWhereItWouldNot() {
EmrServerlessJobRunService service = service(0);
StartJobRunRequest first = request(null, 720L);
first.setTags(Map.of());
JobRun original = service.startJobRun(applicationId, first);
// The run holds the default timeout and no tags either way.
StartJobRunRequest retry = request(null, null);
retry.setClientToken(first.getClientToken());
// A mode, an empty-valued tag and a name the original's run does not hold.
StartJobRunRequest batch = request("BATCH", null);
batch.setClientToken(first.getClientToken());
StartJobRunRequest emptyTag = request(null, null);
emptyTag.setClientToken(first.getClientToken());
emptyTag.setTags(Map.of("team", ""));
StartJobRunRequest renamed = request(null, null);
renamed.setClientToken(first.getClientToken());
renamed.setName("another");
assertEquals(original.getJobRunId(), service.startJobRun(applicationId, retry).getJobRunId());
for (StartJobRunRequest conflicting : List.of(batch, emptyTag, renamed)) {
assertEquals("ConflictException", assertThrows(AwsException.class,
() -> service.startJobRun(applicationId, conflicting)).getErrorCode());
}
}
@Test
void aNextTokenGivenBeforeRunsKeptASequenceIsRefused() {
EmrServerlessJobRunService service = service(0);
List<String> ids = new ArrayList<>();
for (int run = 0; run < 3; run++) {
ids.add(service.startJobRun(applicationId, request(null, null)).getJobRunId());
}
// A token of the listing's earlier cursors, the creation time and the id, under its earlier namespace.
String earlier = Pagination.paginate(ids, id -> id, 1, null,
String.join("|", "ListJobRuns", applicationId, "[]", "null", "null", "null"),
token -> new AwsException("ValidationException", "Invalid nextToken.", 400)).nextToken();
AwsException refused = assertThrows(AwsException.class,
() -> service.listJobRuns(applicationId, null, null, null, null, 1, earlier));
assertEquals("ValidationException", refused.getErrorCode());
}
@Test
void runSucceedsAsSoonAsItStartsByDefault() {
JobRun run = service(0).startJobRun(applicationId, request(null, null));
@@ -20,7 +20,11 @@ import io.floci.aws.testing.MutableClock;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -45,15 +49,17 @@ class EmrServerlessSessionServiceTest {
private EmrServerlessService applications;
private EmrServerlessSessionService sessions;
private MutableClock clock;
private IamService iamService;
private RegionResolver regionResolver;
@BeforeEach
void setUp() {
EmulatorConfig config = mock(EmulatorConfig.class);
when(config.defaultAccountId()).thenReturn(ACCOUNT_ID);
clock = new MutableClock();
RegionResolver regionResolver = new RegionResolver(REGION, ACCOUNT_ID);
regionResolver = new RegionResolver(REGION, ACCOUNT_ID);
applications = new EmrServerlessService(config, new InMemoryStorageFactory(ACCOUNT_ID), regionResolver, clock);
IamService iamService = mock(IamService.class);
iamService = mock(IamService.class);
when(iamService.resolveCallerArn(any())).thenReturn(Optional.empty());
EmrServerlessJobRunService jobRuns = new EmrServerlessJobRunService(AccountAwareStorageBackend.inMemory(
ACCOUNT_ID), applications, iamService, regionResolver, null, objectMapper, 0, clock);
@@ -264,6 +270,50 @@ class EmrServerlessSessionServiceTest {
assertEquals(EmrServerlessSessionService.TOKEN_EXPIRED, sessions.route(token).orElseThrow().refusal());
}
@Test
void sessionsStartedWithinTheSameMillisecondListNewestFirst() {
EmrServerlessJobRunService jobRuns = new EmrServerlessJobRunService(AccountAwareStorageBackend.inMemory(
ACCOUNT_ID), applications, iamService, regionResolver, null, objectMapper, 0, clock);
EmrServerlessSessionService frozen = new EmrServerlessSessionService(AccountAwareStorageBackend.inMemory(
ACCOUNT_ID), applications, jobRuns, iamService, regionResolver, null, objectMapper,
Clock.fixed(Instant.parse("2026-01-01T00:00:00Z"), ZoneOffset.UTC), "http://localhost:4566/");
String applicationId = application("emr-7.13.0", "SPARK", true);
List<String> started = new ArrayList<>();
for (int session = 0; session < 12; session++) {
started.add(frozen.startSession(applicationId, request()).getSessionId());
}
List<String> listed = frozen.listSessions(applicationId, null, null, null, null, null).items().stream()
.map(SessionSummary::getSessionId).toList();
assertEquals(started.reversed(), listed);
}
@Test
void aRetryIsTheOriginalSessionWhereTheSessionWouldHoldTheSameAndConflictsWhereItWouldNot() {
String applicationId = application("emr-7.13.0", "SPARK", true);
StartSessionRequest first = request();
first.setIdleTimeoutMinutes(60L);
first.setTags(Map.of());
first.setConfigurationOverrides(Map.of());
Session original = sessions.startSession(applicationId, first);
// The session holds the default idle timeout, no tags and no overrides either way.
StartSessionRequest retry = request();
retry.setClientToken(first.getClientToken());
StartSessionRequest emptyTag = request();
emptyTag.setClientToken(first.getClientToken());
emptyTag.setTags(Map.of("team", ""));
StartSessionRequest renamed = request();
renamed.setClientToken(first.getClientToken());
renamed.setName("another");
assertEquals(original.getSessionId(), sessions.startSession(applicationId, retry).getSessionId());
for (StartSessionRequest conflicting : List.of(emptyTag, renamed)) {
assertEquals("ConflictException", assertThrows(AwsException.class,
() -> sessions.startSession(applicationId, conflicting)).getErrorCode());
}
}
@Test
void listSessionsFiltersByStateAndGroupsByTheFiltersOrder() {
String applicationId = application("emr-7.13.0", "SPARK", true);