diff --git a/src/main/java/io/floci/aws/services/emr/EmrService.java b/src/main/java/io/floci/aws/services/emr/EmrService.java index 726c738afb..a1466cb778 100644 --- a/src/main/java/io/floci/aws/services/emr/EmrService.java +++ b/src/main/java/io/floci/aws/services/emr/EmrService.java @@ -41,7 +41,7 @@ import io.floci.aws.services.emr.model.EmrSession; import io.floci.aws.services.emr.model.EmrStep; import io.floci.aws.services.emr.model.SecurityConfiguration; import io.floci.aws.services.emrserverless.EmrServerlessSparkRunner; -import io.floci.aws.services.emrserverless.SparkConnectServers; +import io.floci.aws.services.emrserverless.SessionServers; import io.floci.aws.services.emrserverless.SparkConnectSession; import io.floci.aws.services.emrserverless.SparkConnectSessionOwner; import io.floci.aws.services.emrserverless.SparkJobRunner; @@ -191,7 +191,7 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear private final Set primaryNodeStarts = ConcurrentHashMap.newKeySet(); private final AtomicLong primaryNodeStartCount = new AtomicLong(); // The Spark Connect servers of the sessions, in the docker runner mode, and the tokens given for every session. - private final SparkConnectServers sessionServers; + private final SessionServers sessionServers; private final SessionOwner sessionOwner = new SessionOwner(); private final IamService iamService; // Where a step's driver runs: image-template, with the release label for %s, and docker-network; null when @@ -332,10 +332,10 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear RUNNER_DOCKER.equalsIgnoreCase(config.services().emr().runnerMode()) ? releaseImages : null, Executors.newCachedThreadPool(Thread.ofPlatform().daemon().name("emr-step-", 0).factory()), iamService, - new SparkConnectServers( + new SessionServers(SessionServers.Kind.SPARK_CONNECT, RUNNER_DOCKER.equalsIgnoreCase(config.services().emr().runnerMode()) ? sparkRunner : null, Executors.newCachedThreadPool(Thread.ofPlatform().daemon().name("emr-session-", 0).factory()), - SparkConnectServers.checkThread("emr-session-checks"), SparkConnectServers.probe(proxy), + SessionServers.checkThread("emr-session-checks"), SessionServers.sparkConnectProbe(proxy), Clock.systemUTC())); } @@ -351,13 +351,14 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear Executor stepExecutor, IamService iamService) { this(storageFactory, config, regionResolver, eventPublisher, clock, wakeups, sparkRunner, bootstrapRunner, clusterStorage, releaseImages, stepExecutor, iamService, - new SparkConnectServers(sparkRunner, stepExecutor, null, (host, port) -> "not probed", clock)); + new SessionServers(SessionServers.Kind.SPARK_CONNECT, sparkRunner, stepExecutor, null, + (host, port) -> "not probed", clock)); } EmrService(StorageFactory storageFactory, EmulatorConfig config, RegionResolver regionResolver, EmrEventPublisher eventPublisher, Clock clock, Wakeups wakeups, SparkJobRunner sparkRunner, BootstrapRunner bootstrapRunner, ClusterStorage clusterStorage, ReleaseImages releaseImages, - Executor stepExecutor, IamService iamService, SparkConnectServers sessionServers) { + Executor stepExecutor, IamService iamService, SessionServers sessionServers) { this.sessionServers = sessionServers; this.clusterStore = storageFactory.create("emr", "emr-clusters.json", new TypeReference>() {}); @@ -907,7 +908,7 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear }); } - /** Where a Spark Connect call carrying {@code token} goes: a session's server, {@link SparkConnectServers}. */ + /** Where a Spark Connect call carrying {@code token} goes: a session's server, {@link SessionServers}. */ @Override public Optional route(String token) { return sessionServers.route(token, sessionOwner); @@ -3186,7 +3187,7 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear * the job then runs on. A job a cancel, its cluster's end or a reset has passed makes none of it. Afterwards the * image may go once a newer one of its kind is made, and the metadata address to another. */ - private final class ClusterPreparation implements SparkConnectServers.Preparation { + private final class ClusterPreparation implements SessionServers.Preparation { private final String clusterId; private final String accountId; diff --git a/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessSessionService.java b/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessSessionService.java index d3dc13dcf8..aae2d42b3e 100644 --- a/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessSessionService.java +++ b/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessSessionService.java @@ -116,7 +116,7 @@ public class EmrServerlessSessionService implements SparkConnectTargets, Resetta // The docker runner mode's runner, which runs each session's Spark Connect server; null in the immediate mode. private final SparkJobRunner sparkRunner; // The sessions' Spark Connect servers, and the tokens given for them. - private final SparkConnectServers servers; + private final SessionServers servers; private final SessionOwner owner = new SessionOwner(); // The latest sequence a session was given, EmrServerlessService.nextSequence. private long lastSequence; @@ -132,7 +132,7 @@ public class EmrServerlessSessionService implements SparkConnectTargets, Resetta config.effectiveBaseUrl(), jobRuns.containerRunner(), Executors.newCachedThreadPool(Thread.ofPlatform().daemon().name("emr-serverless-session-", 0) .factory()), - SparkConnectServers.checkThread("emr-serverless-session-checks"), SparkConnectServers.probe(proxy)); + SessionServers.checkThread("emr-serverless-session-checks"), SessionServers.sparkConnectProbe(proxy)); } EmrServerlessSessionService(AccountAwareStorageBackend store, EmrServerlessService applications, @@ -148,9 +148,10 @@ public class EmrServerlessSessionService implements SparkConnectTargets, Resetta RegionResolver regionResolver, RequestContext requestContext, ObjectMapper objectMapper, Clock clock, String baseUrl, SparkJobRunner sparkRunner, Executor serverExecutor, ScheduledExecutorService checks, - SparkConnectServers.Probe probe) { + SessionServers.Probe probe) { this.sparkRunner = sparkRunner; - this.servers = new SparkConnectServers(sparkRunner, serverExecutor, checks, probe, clock); + this.servers = new SessionServers(SessionServers.Kind.SPARK_CONNECT, sparkRunner, serverExecutor, checks, + probe, clock); this.store = store; this.applications = applications; this.jobRuns = jobRuns; diff --git a/src/main/java/io/floci/aws/services/emrserverless/SparkConnectServers.java b/src/main/java/io/floci/aws/services/emrserverless/SessionServers.java similarity index 81% rename from src/main/java/io/floci/aws/services/emrserverless/SparkConnectServers.java rename to src/main/java/io/floci/aws/services/emrserverless/SessionServers.java index 23ebb21255..f08363e1f6 100644 --- a/src/main/java/io/floci/aws/services/emrserverless/SparkConnectServers.java +++ b/src/main/java/io/floci/aws/services/emrserverless/SessionServers.java @@ -21,29 +21,42 @@ import java.util.concurrent.TimeUnit; import java.util.function.BooleanSupplier; /** - * The Spark Connect servers a service runs for its sessions, as EMR Serverless's and EMR's do in their docker runner - * mode: each a container the runner runs as a job's driver ({@link SparkJobRunner}), which a client reaches through - * {@link SparkConnectProxy} with a token GetSessionEndpoint gave. It runs each server, asks it whether it answers once - * its container runs, keeps the tokens given for its session, counts the calls it takes, and keeps one check for its - * session's next deadline. The service, its {@link Owner}, holds each session and its state, and is told of each step - * as the session's account and Region, never while this class holds its own lock. + * The servers a service runs for its sessions in its docker runner mode, which a client reaches through a proxy of + * Floci's with a token the service gave: EMR Serverless's and EMR's Spark Connect servers, reached through + * {@link SparkConnectProxy} with a token GetSessionEndpoint gave. Each is a container the runner runs as a job's driver + * ({@link SparkJobRunner}). It runs each server, asks it whether it answers once its container runs, keeps the tokens + * given for its session, counts the calls it takes, and keeps one check for its session's next deadline. The service, + * its {@link Owner}, holds each session and its state, and is told of each step as the session's account and Region, + * never while this class holds its own lock. Its {@link Kind} names what the servers are in its answers and its log. */ -public final class SparkConnectServers { +public final class SessionServers { - private static final Logger LOG = Logger.getLogger(SparkConnectServers.class); + private static final Logger LOG = Logger.getLogger(SessionServers.class); /** How long a server may take to answer once its container runs. */ public static final Duration START_LIMIT = Duration.ofMinutes(10); private static final long PROBE_INTERVAL_MILLIS = 500; - // No source gives AWS's wording for these; they are Floci's. - public static final String SERVER_EXITED = "The session's Spark Connect server exited with code %d."; - public static final String SERVER_NOT_STARTED = "The session's Spark Connect server could not start: %s"; - public static final String SERVER_SILENT = "The session's Spark Connect server did not answer within " - + START_LIMIT.toMinutes() + " minutes: %s"; - public static final String SERVER_ENDED = "The session's Spark Connect server ended with no exit code."; - public static final String TOKEN_EXPIRED = - "The authentication token has expired. Call GetSessionEndpoint for a new one."; - public static final String NO_RUNNING_SERVER = "Session %s has no Spark Connect server running."; + + /** + * What a service's servers are, in the answers to the calls they cannot take and in Floci's log: what one is + * called, what each serves, and the operation that gives the tokens that reach them. No source gives AWS's + * wording for those answers; it is Floci's. + */ + public record Kind(String server, String holder, String tokenOperation) { + + /** EMR Serverless's and EMR's: a session's Spark Connect server, reached with GetSessionEndpoint's tokens. */ + public static final Kind SPARK_CONNECT = new Kind("Spark Connect server", "Session", "GetSessionEndpoint"); + + /** The answer to a call whose token has expired. */ + public String tokenExpired() { + return "The authentication token has expired. Call " + tokenOperation + " for a new one."; + } + + /** The answer to a call whose session has no server running. */ + public String noRunningServer(String sessionId) { + return holder + " " + sessionId + " has no " + server + " running."; + } + } /** The service whose sessions these are. Each method is called as the session's account and Region. */ public interface Owner { @@ -59,7 +72,7 @@ public final class SparkConnectServers { /** * The session's server has stopped, as {@code outcome} says; its tokens and its check go once this returns. - * {@link #failure} tells whether that failed the session. + * Whether that failed the session is the owner's to say. */ void stopped(String sessionId, SparkJobRunner.Outcome outcome); @@ -97,12 +110,13 @@ public final class SparkConnectServers { } } - /** Why no Spark Connect server answers at a host and port, or null once one does. */ + /** Why no server answers at a host and port, or null once one does. */ @FunctionalInterface public interface Probe { String unanswered(String host, int port); } + private final Kind kind; private final SparkJobRunner runner; private final Executor executor; private final ScheduledExecutorService checks; @@ -110,17 +124,19 @@ public final class SparkConnectServers { private final Clock clock; // The sessions whose server a thread of this process runs; guarded by this. private final Map servers = new HashMap<>(); - // The authentication tokens GetSessionEndpoint gave, until they expire or their session ends. + // The authentication tokens the service gave, until they expire or their session ends. private final Map grants = new ConcurrentHashMap<>(); // Raised by each reset, so a server's thread from before it tells its owner nothing more; guarded by this. private long generation; /** - * Servers run by {@code runner} (null where none runs, as in the immediate runner mode, where tokens are still - * kept) on {@code executor}'s threads, their sessions' deadlines checked on {@code checks} (null for none). + * Servers of {@code kind} run by {@code runner} (null where none runs, as in the immediate runner mode, where + * tokens are still kept) on {@code executor}'s threads, their sessions' deadlines checked on {@code checks} (null + * for none). */ - public SparkConnectServers(SparkJobRunner runner, Executor executor, ScheduledExecutorService checks, Probe probe, - Clock clock) { + public SessionServers(Kind kind, SparkJobRunner runner, Executor executor, ScheduledExecutorService checks, + Probe probe, Clock clock) { + this.kind = kind; this.runner = runner; this.executor = executor; this.checks = checks; @@ -136,8 +152,8 @@ public final class SparkConnectServers { return thread; } - /** {@link SparkConnectProxy#unanswered}, waited for at most ten seconds. */ - public static Probe probe(SparkConnectProxy proxy) { + /** For a Spark Connect server: {@link SparkConnectProxy#unanswered}, waited for at most ten seconds. */ + public static Probe sparkConnectProbe(SparkConnectProxy proxy) { return (host, port) -> { try { return proxy.unanswered(host, port).toCompletionStage().toCompletableFuture() @@ -152,20 +168,6 @@ public final class SparkConnectServers { }; } - /** - * What a session whose server stopped as {@code outcome} says ended with, where that failed it: why it could not - * start, or its exit code; null where it did not fail, as when it was stopped or ran out of time. - */ - public static String failure(SparkJobRunner.Outcome outcome) { - if (outcome.timedOut() || outcome.stopped()) { - return null; - } - if (outcome.failure() != null) { - return String.format(SERVER_NOT_STARTED, outcome.failure()); - } - return outcome.exitCode() != null ? String.format(SERVER_EXITED, outcome.exitCode()) : SERVER_ENDED; - } - /** Whether a thread of this process runs the session's server. */ public synchronized boolean runs(String sessionId) { return servers.containsKey(sessionId); @@ -207,7 +209,7 @@ public final class SparkConnectServers { } } - /** Keeps a token GetSessionEndpoint gave for the session, until {@code expiresAt}. */ + /** Keeps a token the service gave for the session, until {@code expiresAt}. */ public void grant(String token, String sessionId, String accountId, String region, Instant expiresAt) { Instant now = clock.instant(); grants.values().removeIf(grant -> !now.isBefore(grant.expiresAt())); @@ -231,7 +233,7 @@ public final class SparkConnectServers { } if (!clock.instant().isBefore(grant.expiresAt())) { grants.remove(token); - return Optional.of(Route.refused(Route.UNAUTHENTICATED, TOKEN_EXPIRED)); + return Optional.of(Route.refused(Route.UNAUTHENTICATED, kind.tokenExpired())); } return Optional.of(RequestScopes.callAs(grant.accountId(), grant.region(), () -> callStarted(grant, owner))); } @@ -263,7 +265,7 @@ public final class SparkConnectServers { /** A reset has begun: the servers' threads tell their owner nothing more, and every token goes. */ public synchronized void reset() { generation++; - servers.values().forEach(SparkConnectServers::cancelCheck); + servers.values().forEach(SessionServers::cancelCheck); servers.clear(); grants.clear(); } @@ -292,7 +294,7 @@ public final class SparkConnectServers { synchronized (this) { server = servers.get(sessionId); if (server == null) { - return Route.refused(Route.UNAVAILABLE, String.format(NO_RUNNING_SERVER, sessionId)); + return Route.refused(Route.UNAVAILABLE, kind.noRunningServer(sessionId)); } } synchronized (server.notifications) { @@ -302,7 +304,7 @@ public final class SparkConnectServers { long launchedIn; synchronized (this) { if (server.stopping || server.host == null || servers.get(sessionId) != server) { - return Route.refused(Route.UNAVAILABLE, String.format(NO_RUNNING_SERVER, sessionId)); + return Route.refused(Route.UNAVAILABLE, kind.noRunningServer(sessionId)); } first = server.calls++ == 0; host = server.host; @@ -343,7 +345,7 @@ public final class SparkConnectServers { // An end, a reset or Floci's shutdown cut its start short. outcome = SparkJobRunner.Outcome.halted(); } else { - LOG.warnv(e, "The Spark Connect server of session {0} could not run", job.runId()); + LOG.warnv(e, "The {0} of {1} {2} could not run", kind.server(), kind.holder(), job.runId()); outcome = SparkJobRunner.Outcome.couldNotStart(e.getMessage() != null ? e.getMessage() : e.toString()); } @@ -351,8 +353,8 @@ public final class SparkConnectServers { try { preparation.release(); } catch (RuntimeException e) { - LOG.warnv(e, "What the Spark Connect server of session {0} took could not all be given back", - job.runId()); + LOG.warnv(e, "What the {0} of {1} {2} took could not all be given back", kind.server(), + kind.holder(), job.runId()); } } if (!current(launchedIn)) { @@ -365,7 +367,7 @@ public final class SparkConnectServers { // whose server a restart left behind. RequestScopes.runAs(job.accountId(), job.region(), () -> owner.stopped(sessionId, ended)); } catch (RuntimeException e) { - LOG.warnv(e, "Session {0} could not be told its Spark Connect server stopped", sessionId); + LOG.warnv(e, "{0} {1} could not be told its {2} stopped", kind.holder(), sessionId, kind.server()); } finally { synchronized (this) { if (launchedIn == generation) { @@ -380,7 +382,7 @@ public final class SparkConnectServers { try { RequestScopes.runAs(job.accountId(), job.region(), () -> owner.gone(sessionId)); } catch (RuntimeException e) { - LOG.warnv(e, "Session {0} could not be told its Spark Connect server has gone", sessionId); + LOG.warnv(e, "{0} {1} could not be told its {2} has gone", kind.holder(), sessionId, kind.server()); } } @@ -396,7 +398,8 @@ public final class SparkConnectServers { return; } if (System.nanoTime() - giveUp >= 0) { - LOG.warnv("The Spark Connect server of session {0} did not answer: {1}", sessionId, unanswered); + LOG.warnv("The {0} of {1} {2} did not answer: {3}", kind.server(), kind.holder(), sessionId, + unanswered); RequestScopes.runAs(job.accountId(), job.region(), () -> owner.silent(sessionId, unanswered)); stop(sessionId); return; @@ -476,7 +479,7 @@ public final class SparkConnectServers { @Override public void listening(String host, int port) { - if (SparkConnectServers.this.listening(job.runId(), host, port, launchedIn)) { + if (SessionServers.this.listening(job.runId(), host, port, launchedIn)) { executor.execute(() -> await(job, owner, host, port, launchedIn)); } } diff --git a/src/main/java/io/floci/aws/services/emrserverless/SparkConnectSessionOwner.java b/src/main/java/io/floci/aws/services/emrserverless/SparkConnectSessionOwner.java index aced3e18e9..edbfc4734d 100644 --- a/src/main/java/io/floci/aws/services/emrserverless/SparkConnectSessionOwner.java +++ b/src/main/java/io/floci/aws/services/emrserverless/SparkConnectSessionOwner.java @@ -9,14 +9,14 @@ import java.util.function.Consumer; import java.util.function.Supplier; /** - * A service's sessions as their servers' owner ({@link SparkConnectServers.Owner}): the states a session goes through + * A service's sessions as their servers' owner ({@link SessionServers.Owner}): the states a session goes through * as its server starts, answers, takes calls and stops, which EMR Serverless's sessions and EMR's share. It is * SUBMITTED until its server's container runs, STARTING until the server answers, then IDLE, its idle timeout counting * from then; BUSY while a call runs on its server and IDLE again once none does; and once its server stops, TERMINATED, * or FAILED where that failed it. Each step is taken under the service's lock on the session as stored; {@code R} is * what the service stores, {@link #session} the session in it. */ -public abstract class SparkConnectSessionOwner implements SparkConnectServers.Owner { +public abstract class SparkConnectSessionOwner implements SessionServers.Owner { private static final Logger LOG = Logger.getLogger(SparkConnectSessionOwner.class); @@ -28,6 +28,12 @@ public abstract class SparkConnectSessionOwner implements SparkConnectServers public static final String TERMINATED = "TERMINATED"; public static final String FAILED = "FAILED"; private static final Set ENDED = Set.of(TERMINATED, FAILED); + // No source gives AWS's wording for these; they are Floci's. + public static final String SERVER_EXITED = "The session's Spark Connect server exited with code %d."; + public static final String SERVER_NOT_STARTED = "The session's Spark Connect server could not start: %s"; + public static final String SERVER_SILENT = "The session's Spark Connect server did not answer within " + + SessionServers.START_LIMIT.toMinutes() + " minutes: %s"; + public static final String SERVER_ENDED = "The session's Spark Connect server ended with no exit code."; /** Runs {@code body} under the service's lock. */ protected abstract T locked(Supplier body); @@ -83,14 +89,28 @@ public abstract class SparkConnectSessionOwner implements SparkConnectServers @Override public void silent(String sessionId, String why) { inState(sessionId, STARTING, - stored -> end(stored, FAILED, String.format(SparkConnectServers.SERVER_SILENT, why))); + stored -> end(stored, FAILED, String.format(SERVER_SILENT, why))); + } + + /** + * What a session whose server stopped as {@code outcome} says ended with, where that failed it: why it could not + * start, or its exit code; null where it did not fail, as when it was stopped or ran out of time. + */ + public static String failure(SparkJobRunner.Outcome outcome) { + if (outcome.timedOut() || outcome.stopped()) { + return null; + } + if (outcome.failure() != null) { + return String.format(SERVER_NOT_STARTED, outcome.failure()); + } + return outcome.exitCode() != null ? String.format(SERVER_EXITED, outcome.exitCode()) : SERVER_ENDED; } @Override public void stopped(String sessionId, SparkJobRunner.Outcome outcome) { locked(() -> { find(sessionId).filter(stored -> !ENDED.contains(session(stored).getState())).ifPresent(stored -> { - String failure = SparkConnectServers.failure(outcome); + String failure = failure(outcome); if (TERMINATING.equals(session(stored).getState()) || failure == null) { end(stored, TERMINATED, endReason(stored, outcome)); } else { diff --git a/src/test/java/io/floci/aws/services/emr/EmrServiceFixture.java b/src/test/java/io/floci/aws/services/emr/EmrServiceFixture.java index ef910b6cc6..5b6408d70c 100644 --- a/src/test/java/io/floci/aws/services/emr/EmrServiceFixture.java +++ b/src/test/java/io/floci/aws/services/emr/EmrServiceFixture.java @@ -8,7 +8,7 @@ import io.floci.aws.core.common.docker.ContainerLifecycleManager; import io.floci.aws.services.ec2.model.Instance; import io.floci.aws.services.emr.model.EmrCluster; import io.floci.aws.services.emr.model.EmrStep; -import io.floci.aws.services.emrserverless.SparkConnectServers; +import io.floci.aws.services.emrserverless.SessionServers; import io.floci.aws.services.emrserverless.SparkJobRunner; import io.floci.aws.services.eventbridge.EventBridgeService; import io.floci.aws.services.iam.IamService; @@ -299,7 +299,7 @@ final class EmrServiceFixture { private final IamService iam; private final int[] durations; // How the docker runner mode's session servers are asked whether they answer; null for no session servers. - private final SparkConnectServers.Probe sessionProbe; + private final SessionServers.Probe sessionProbe; /** The service as configured by default, every duration 0. */ EmrServiceFixture() { @@ -331,7 +331,7 @@ final class EmrServiceFixture { * As {@link #docker(SparkJobRunner, IamService)}, its sessions' Spark Connect servers run by {@code runner} too, * each on a thread of its own, and asked whether they answer by {@code probe}. */ - static EmrServiceFixture dockerSessions(SparkJobRunner runner, IamService iam, SparkConnectServers.Probe probe) { + static EmrServiceFixture dockerSessions(SparkJobRunner runner, IamService iam, SessionServers.Probe probe) { return new EmrServiceFixture(new InMemoryStorageFactory(ACCOUNT), new MutableClock(), runner, null, new RecordingStorage(), new RecordingReleaseImages(), iam, probe, 0, 0, 0); } @@ -339,7 +339,7 @@ final class EmrServiceFixture { private EmrServiceFixture(InMemoryStorageFactory storage, MutableClock clock, SparkJobRunner runner, BootstrapRunner bootstrap, RecordingStorage clusterStorage, RecordingReleaseImages releaseImages, IamService iam, - SparkConnectServers.Probe sessionProbe, int... durations) { + SessionServers.Probe sessionProbe, int... durations) { this.sessionProbe = sessionProbe; this.storage = storage; this.clock = clock; @@ -379,8 +379,8 @@ final class EmrServiceFixture { clusterStorage, releaseImages, this::handToThread, iam) : new EmrService(storage, config(false), regionResolver, publisher, clock, recorded, runner, bootstrap, clusterStorage, releaseImages, this::handToThread, iam, - new SparkConnectServers(runner, task -> Thread.ofPlatform().daemon().start(task), null, - sessionProbe, clock)); + new SessionServers(SessionServers.Kind.SPARK_CONNECT, runner, + task -> Thread.ofPlatform().daemon().start(task), null, sessionProbe, clock)); } /** A new service on the same storage and clock, as after a restart of Floci with persistent storage. */ diff --git a/src/test/java/io/floci/aws/services/emr/EmrSessionServerTest.java b/src/test/java/io/floci/aws/services/emr/EmrSessionServerTest.java index 16f3fe180a..1d714ce48d 100644 --- a/src/test/java/io/floci/aws/services/emr/EmrSessionServerTest.java +++ b/src/test/java/io/floci/aws/services/emr/EmrSessionServerTest.java @@ -4,7 +4,8 @@ import io.floci.aws.core.common.SparkConnectEndpoint; import io.floci.aws.core.common.SparkConnectTargets; import io.floci.aws.services.emr.model.EmrCluster; import io.floci.aws.services.emr.model.EmrSession; -import io.floci.aws.services.emrserverless.SparkConnectServers; +import io.floci.aws.services.emrserverless.SessionServers; +import io.floci.aws.services.emrserverless.SparkConnectSessionOwner; import io.floci.aws.services.emrserverless.SparkJobRunner; import io.floci.aws.services.iam.IamService; import org.junit.jupiter.api.Test; @@ -43,7 +44,7 @@ class EmrSessionServerTest { private final FakeServerRunner runner = new FakeServerRunner(); private final AtomicBoolean answering = new AtomicBoolean(true); - private final SparkConnectServers.Probe probe = (host, port) -> + private final SessionServers.Probe probe = (host, port) -> answering.get() && SERVER_HOST.equals(host) && port == SERVER_PORT ? null : "Connection refused"; private final EmrServiceFixture fixture = EmrServiceFixture.dockerSessions(runner, mock(IamService.class), probe); private final EmrService service = fixture.service; @@ -136,7 +137,7 @@ class EmrSessionServerTest { runner.end(sessionId).complete(SparkJobRunner.Outcome.exitedWith(1)); EmrSession failed = awaitState(clusterId, sessionId, "FAILED"); - assertEquals(String.format(SparkConnectServers.SERVER_EXITED, 1), failed.getStateChangeReason()); + assertEquals(String.format(SparkConnectSessionOwner.SERVER_EXITED, 1), failed.getStateChangeReason()); } @Test diff --git a/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessSessionServerTest.java b/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessSessionServerTest.java index 7e8e855d13..9e2ca4ddf2 100644 --- a/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessSessionServerTest.java +++ b/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessSessionServerTest.java @@ -171,7 +171,7 @@ class EmrServerlessSessionServerTest { Session failed = awaitState(applicationId, sessionId, "FAILED"); - assertEquals(String.format(SparkConnectServers.SERVER_NOT_STARTED, "java.lang.IllegalStateException"), + assertEquals(String.format(SparkConnectSessionOwner.SERVER_NOT_STARTED, "java.lang.IllegalStateException"), failed.getStateDetails()); } @@ -272,7 +272,7 @@ class EmrServerlessSessionServerTest { runner.end(sessionId).complete(SparkJobRunner.Outcome.exitedWith(1)); Session failed = awaitState(applicationId, sessionId, "FAILED"); - assertEquals(String.format(SparkConnectServers.SERVER_EXITED, 1), failed.getStateDetails()); + assertEquals(String.format(SparkConnectSessionOwner.SERVER_EXITED, 1), failed.getStateDetails()); } @Test diff --git a/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessSessionServiceTest.java b/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessSessionServiceTest.java index 4517da3d94..2fd33e2d9a 100644 --- a/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessSessionServiceTest.java +++ b/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessSessionServiceTest.java @@ -267,7 +267,7 @@ class EmrServerlessSessionServiceTest { assertEquals(String.format(EmrServerlessSessionService.NO_SERVER, sessionId), route.refusal()); assertTrue(sessions.route("not-a-token").isEmpty()); clock.advance(EmrServerlessSessionService.TOKEN_VALIDITY); - assertEquals(SparkConnectServers.TOKEN_EXPIRED, sessions.route(token).orElseThrow().refusal()); + assertEquals(SessionServers.Kind.SPARK_CONNECT.tokenExpired(), sessions.route(token).orElseThrow().refusal()); } @Test diff --git a/src/test/java/io/floci/aws/services/emrserverless/SparkConnectServersTest.java b/src/test/java/io/floci/aws/services/emrserverless/SessionServersTest.java similarity index 88% rename from src/test/java/io/floci/aws/services/emrserverless/SparkConnectServersTest.java rename to src/test/java/io/floci/aws/services/emrserverless/SessionServersTest.java index 85df531db5..b37180b565 100644 --- a/src/test/java/io/floci/aws/services/emrserverless/SparkConnectServersTest.java +++ b/src/test/java/io/floci/aws/services/emrserverless/SessionServersTest.java @@ -24,16 +24,16 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; /** - * The servers {@link SparkConnectServers} runs, read from its owner's side: what reaches the owner, in what order, and + * The servers {@link SessionServers} runs, read from its owner's side: what reaches the owner, in what order, and * what a call is answered, as servers start, take calls and stop. */ -class SparkConnectServersTest { +class SessionServersTest { private static final String SESSION = "session-1"; private static final String TOKEN = "a-token"; private final ServerRunner runner = new ServerRunner(); - private final SparkConnectServers servers = new SparkConnectServers(runner, + private final SessionServers servers = new SessionServers(SessionServers.Kind.SPARK_CONNECT, runner, task -> Thread.ofPlatform().daemon().start(task), null, (host, port) -> null, Clock.systemUTC()); private final RecordingOwner owner = new RecordingOwner(); @@ -49,7 +49,15 @@ class SparkConnectServersTest { Route route = servers.route(TOKEN, owner).orElseThrow(); assertEquals(Route.UNAVAILABLE, route.status()); - assertEquals(String.format(SparkConnectServers.NO_RUNNING_SERVER, SESSION), route.refusal()); + assertEquals(SessionServers.Kind.SPARK_CONNECT.noRunningServer(SESSION), route.refusal()); + } + + @Test + void sparkConnectServersRefuseACallInTheWordsTheyAlwaysHad() { + assertEquals("The authentication token has expired. Call GetSessionEndpoint for a new one.", + SessionServers.Kind.SPARK_CONNECT.tokenExpired()); + assertEquals("Session " + SESSION + " has no Spark Connect server running.", + SessionServers.Kind.SPARK_CONNECT.noRunningServer(SESSION)); } @Test @@ -116,7 +124,7 @@ class SparkConnectServersTest { } /** An owner that records what reaches it, the idle as it ends, and answers as the test sets it to. */ - private static final class RecordingOwner implements SparkConnectServers.Owner { + private static final class RecordingOwner implements SessionServers.Owner { final List events = new CopyOnWriteArrayList<>(); volatile Supplier refusal = () -> null; volatile Runnable idleWait = () -> { };