feat(emr-containers): run a managed endpoint's overridden kernels in containers of their own (#9293)

An endpoint's jupyter-kernel-overrides gives a kernel its own
container-image ("Custom kernel image with interactive endpoint"), and
on EMR on EKS each kernel is a pod of its image. A Floci endpoint ran
every kernel in its gateway's container, whatever image the overrides
named.

- The gateway job tells the gateway program which kernels the
  overrides give an image (python3 and spark-python-kubernetes, the
  kernels the guide's example names, as "You can't add new kernel image
  types"), where it asks Floci for their containers, and a secret Floci
  gives that gateway alone.
- EmrContainersKernelContainersRoute takes the gateway's POST and
  DELETE on /_aws/emr-containers/kernel-containers/<endpoint>/<kernel>.
  The service checks the secret, that the endpoint is ACTIVE and its
  overrides give the kernel an image, and the kernel id, response
  address and public key, then EmrContainersKernelContainers runs the
  kernel's container: the override image, with the gateway's settings
  and environment and the kernel's KERNEL_ variables, running the
  image's Jupyter Enterprise Gateway launcher (launch_ipykernel.py),
  under spark-submit with the %%configure magic for a PySpark kernel.
  A launch is admitted under the lock the gateway's end takes, so an
  endpoint's kernel containers all stop with its gateway, and a reset
  or Floci's shutdown stops every one.
- The gateway program gives such a kernel a jupyter_client provisioner
  that speaks the launcher's protocol: it asks Floci for the container
  with an RSA public key and a response address, decrypts the
  connection the launcher sends back (an AES key, under RSA, over
  AES-GCM), and polls, signals and stops the kernel through the
  launcher's comm port and Floci. It waits KERNEL_LAUNCH_TIMEOUT
  seconds, at most the guide's 400, or 400, and a launch that fails
  once the container was asked for stops it.
- Such a kernel's %%configure magic posts to its ENDPOINT_IP, the
  gateway, on port 9547 over TLS, with a certificate the image's openssl
  makes; the program checks it with the launcher's AES key, which each
  launch refreshes. Every restart, the kernels API's, the
  auto-restarter's or %%configure's, gives the kernel a new container
  with new ports and a new key, and the program moves the kernel's
  clients and its activity watch onto them.
- The nightly check gains a method: an endpoint that overrides both
  kernels with the release's own image runs each in its own container,
  the PySpark one counting an S3 file's lines and taking %%configure.
  The docker-runner group's budget grows by its eighteen minutes.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-23 15:32:02 -04:00
1 parent 8724146f3f
commit 0dd02aed44
10 files changed
+1143 -32

No files matched your search

@@ -1,5 +1,6 @@
package io.floci.aws.services.emrcontainers;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.floci.aws.core.common.AwsArnUtils;
import io.floci.aws.services.emrserverless.EmrServerlessJobRunService;
@@ -11,8 +12,10 @@ import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Base64;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -51,6 +54,13 @@ import java.util.Map;
* the restarted kernel answers, ends the cell with the replies the magic prepared, as the restart takes the kernel
* down before it can; a kernel that dies first, or does not answer in jupyter_server's time for a restart, ends it
* with an error. A kernel's requests take their turn.
*
* <p>A kernel the endpoint's {@code jupyter-kernel-overrides} gives an image of its own runs in a container of that
* image, as on EMR on EKS each kernel is a pod of its image: the program's provisioner asks Floci for it
* ({@link EmrContainersKernelContainersRoute}), and the image's Jupyter Enterprise Gateway launcher sends back where
* the kernel listens, encrypted for the provisioner's key, and takes its signals on its comm port. Its
* {@code %%configure} magic posts to the program's port 9547 over TLS, as its {@code ENDPOINT_IP} says; the restart
* gives the kernel new ports and a new key, which the program moves the kernel's clients onto.
*/
final class EmrContainersGateways {
@@ -79,6 +89,25 @@ final class EmrContainersGateways {
static final String JEG_CONFIG = "jeg-config";
static final List<String> JEG_PROPERTIES = List.of("RemoteMappingKernelManager.cull_idle_timeout",
"RemoteMappingKernelManager.cull_interval");
// The classification that gives a kernel an image of its own, a configuration per kernel naming it, its
// container-image property the image ("Custom kernel image with interactive endpoint"); the kernels its example
// names, as "You can't add new kernel image types".
static final String KERNEL_OVERRIDES = "jupyter-kernel-overrides";
static final String CONTAINER_IMAGE = "container-image";
static final String PYTHON_KERNEL = "python3";
static final String PYSPARK_KERNEL = "spark-python-kubernetes";
static final List<String> OVERRIDABLE_KERNELS = List.of(PYTHON_KERNEL, PYSPARK_KERNEL);
// Where the program learns which kernels run in containers of their own, and the secret it asks Floci for them
// with, in SECRET_HEADER.
static final String OVERRIDES_VARIABLE = "FLOCI_KERNEL_OVERRIDES";
static final String SECRET_VARIABLE = "FLOCI_KERNEL_CONTAINERS_SECRET";
static final String KERNEL_CONTAINERS_VARIABLE = "FLOCI_KERNEL_CONTAINERS_PATH";
static final String SECRET_HEADER = "X-Floci-Kernel-Containers-Secret";
// Jupyter Enterprise Gateway's Python kernel launcher, in the image, which a kernel's own container runs.
static final String LAUNCHER = "/usr/local/bin/kernel-launchers/python/scripts/launch_ipykernel.py";
// What the launcher's additional-info says to load the %%configure magic.
private static final String CONFIGURE_MAGIC_ENABLED = Base64.getEncoder()
.encodeToString("{\"configure_magic_enabled\": \"true\"}".getBytes(StandardCharsets.UTF_8));
/** The servers' kind, in the answers to the calls they cannot take. */
static final SessionServers.Kind KIND = new SessionServers.Kind("Jupyter Enterprise Gateway", "Endpoint",
@@ -94,13 +123,21 @@ final class EmrContainersGateways {
import json
import os
import secrets
import signal
import socket
import ssl
import subprocess
import sys
import uuid
from importlib.metadata import EntryPoint
from Cryptodome.Cipher import AES
from Cryptodome.Cipher import AES, PKCS1_v1_5
from Cryptodome.Hash import SHA256
from Cryptodome.PublicKey import RSA
from Cryptodome.Util.Padding import unpad
from jupyter_client.jsonutil import json_default
from jupyter_client.kernelspec import KernelSpecManager
from jupyter_client.provisioning import KernelProvisionerBase, KernelProvisionerFactory
from jupyter_core.utils import ensure_async
from jupyter_server.auth.decorator import authorized
from jupyter_server.serverapp import ServerApp
@@ -111,9 +148,13 @@ final class EmrContainersGateways {
from jupyter_server.services.sessions import handlers as session_handlers
from jupyter_server.utils import url_escape, url_path_join
from tornado import httpserver, web
from tornado.httpclient import AsyncHTTPClient, HTTPRequest
from tornado.websocket import WebSocketClosedError
from traitlets import Unicode
HOME = "@HOME@"
# The program's kernel specs come first, for the server and for the overrides' lookups alike.
os.environ["JUPYTER_PATH"] = HOME
SESSION_HEADER = "@SESSION_HEADER@"
# The image's %%configure magic, beside Jupyter Enterprise Gateway's kernel launchers.
MAGICS = "/usr/local/bin/kernel-launchers/python/scripts"
@@ -130,6 +171,19 @@ final class EmrContainersGateways {
STARTED_WITH = {}
# Each running kernel's launch.
LAUNCHED = {}
# The kernels the endpoint's jupyter-kernel-overrides gives images of their own, each in a container Floci
# runs, where and with what secret the program asks Floci for those, and how long it waits for one: what
# KERNEL_LAUNCH_TIMEOUT says, at most its maximum, 400 seconds.
OVERRIDDEN = [kernel for kernel in os.environ.get("@OVERRIDES_VARIABLE@", "").split(",") if kernel]
KERNEL_CONTAINERS = (os.environ.get("AWS_ENDPOINT_URL", "").rstrip("/")
+ os.environ.get("@KERNEL_CONTAINERS_VARIABLE@", ""))
KERNEL_CONTAINERS_SECRET = os.environ.pop("@SECRET_VARIABLE@", None)
SECRET_HEADER = "@SECRET_HEADER@"
LAUNCH_TIMEOUT = 400
# Where the magic of a kernel in a container of its own posts its cell's settings: its ENDPOINT_IP, the
# program's address, on this port, over TLS.
CONFIGURE_TLS_PORT = 9547
CONTAINER_PROVISIONER = "floci-kernel-container"
PYSPARK_KERNEL = '''
import sys
@@ -168,6 +222,12 @@ final class EmrContainersGateways {
+ TOREE + ' --profile "$0"',
"{connection_file}"],
}))
# A kernel the overrides give an image of its own runs in a container Floci starts, through the provisioner.
for kernel in OVERRIDDEN:
spec = KernelSpecManager().get_kernel_spec(kernel).to_dict()
spec["metadata"] = dict(spec.get("metadata") or {}, kernel_provisioner={
"provisioner_name": CONTAINER_PROVISIONER, "config": {"kernel": kernel}})
write(HOME + "/kernels/" + kernel + "/kernel.json", json.dumps(spec))
# Whether the request being served carries the session token the kernel was started with.
@@ -212,6 +272,130 @@ final class EmrContainersGateways {
return [message for message in (error, reply, idle) if message]
# Asks Floci for a kernel's container, or for its end.
async def kernel_container(method, kernel_id, body=None):
await AsyncHTTPClient().fetch(HTTPRequest(
KERNEL_CONTAINERS + kernel_id, method=method, body=None if body is None else json.dumps(body),
headers={SECRET_HEADER: KERNEL_CONTAINERS_SECRET, "Content-Type": "application/json"},
allow_nonstandard_methods=True, request_timeout=60))
# A kernel in a container of its own, of the image the overrides give it, which Floci starts at the
# program's request, running Jupyter Enterprise Gateway's launcher: the launcher sends where the kernel
# listens, encrypted for the program's key, and takes the program's signals on its comm port.
class ContainerProvisioner(KernelProvisionerBase):
kernel = Unicode(config=True)
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.comm = None
self.aes_key = None
@property
def has_process(self):
return self.comm is not None
async def pre_launch(self, **kwargs):
kwargs = await super().pre_launch(**kwargs)
kwargs["cmd"] = []
return kwargs
async def launch_kernel(self, cmd, **kwargs):
env = kwargs.get("env") or {}
key = RSA.generate(2048)
response = asyncio.get_running_loop().create_future()
async def respond(reader, writer):
data = await reader.read()
writer.close()
if not response.done():
response.set_result((data, writer.get_extra_info("peername")[0]))
server = await asyncio.start_server(respond, "0.0.0.0", 0)
try:
await kernel_container("POST", self.kernel_id, {
"kernel": self.kernel,
"responseAddress": socket.gethostbyname(socket.gethostname()) + ":"
+ str(server.sockets[0].getsockname()[1]),
"publicKey": base64.b64encode(key.publickey().export_key("DER")).decode(),
"env": {name: value for name, value in env.items() if name.startswith("KERNEL_")}})
timeout = min(int(env.get("KERNEL_LAUNCH_TIMEOUT") or LAUNCH_TIMEOUT), LAUNCH_TIMEOUT)
data, host = await asyncio.wait_for(response, timeout)
payload = json.loads(base64.b64decode(data))
aes_key = PKCS1_v1_5.new(key).decrypt(base64.b64decode(payload["key"]), None)
cipher = AES.new(aes_key, AES.MODE_GCM, nonce=base64.b64decode(payload["nonce"]))
info = json.loads(unpad(cipher.decrypt_and_verify(base64.b64decode(payload["conn_info"]),
base64.b64decode(payload["tag"])), 16))
connection_info = {
"ip": host, "transport": info.get("transport", "tcp"), "key": info["key"].encode(),
"signature_scheme": info["signature_scheme"], "kernel_name": self.kernel,
**{name: int(info[name]) for name in ("shell_port", "iopub_port", "stdin_port",
"hb_port", "control_port")}}
comm = (host, int(info["comm_port"]))
except BaseException:
# A launch that fails after Floci was asked for the container stops it.
await self.cleanup()
raise
finally:
server.close()
self.aes_key = aes_key.hex()
self.comm = comm
self.connection_info = connection_info
# Each launch's launcher, a restart's too, gives its magic a key of its own.
launch = LAUNCHED.get(self.kernel_id)
if launch is not None:
launch.key = self.aes_key
return self.connection_info
# Sends the launcher a request on its comm port; false where it takes none.
async def tell(self, request):
if self.comm is None:
return False
try:
reader, writer = await asyncio.wait_for(asyncio.open_connection(*self.comm), 5)
writer.write(json.dumps(request).encode())
await writer.drain()
writer.close()
await writer.wait_closed()
return True
except (OSError, asyncio.TimeoutError):
return False
async def poll(self):
return None if await self.tell({"signum": 0}) else 1
async def wait(self):
for _ in range(50):
if await self.poll() is not None:
return 1
await asyncio.sleep(0.2)
return None
async def send_signal(self, signum):
await self.tell({"signum": signum})
async def kill(self, restart=False):
await self.tell({"signum": signal.SIGKILL})
await self.cleanup(restart)
async def terminate(self, restart=False):
await self.tell({"signum": signal.SIGTERM})
# Best effort, as jupyter_client's shutdown and restart go on whatever a provisioner's cleanup
# meets: a container Floci fails to stop here still stops with the endpoint's gateway.
async def cleanup(self, restart=False):
self.comm = None
try:
await kernel_container("DELETE", self.kernel_id)
except Exception:
self.log.exception("Kernel %s's container could not be stopped", self.kernel_id)
KernelProvisionerFactory.instance().provisioners[CONTAINER_PROVISIONER] = EntryPoint(
CONTAINER_PROVISIONER, "__main__:ContainerProvisioner", KernelProvisionerFactory.GROUP_NAME)
# A kernel's AES key, which only it and the program hold, its launch environment, and the lock its
# %%configure requests take in turn.
class Launch:
@@ -240,9 +424,38 @@ final class EmrContainersGateways {
env.update({"KERNEL_ID": kernel_id, "ADDITIONAL_INFO": json.dumps({"AES_KEY": key})})
kernel_id = await super().start_kernel(kernel_id=kernel_id, env=env, **kwargs)
STARTED_WITH[kernel_id] = SESSION.get()
LAUNCHED[kernel_id] = Launch(key, env)
# A launcher in a container of its own gives its magic a key of its own, and its restarts, the
# auto-restarter's too, give the kernel new ports and a new key.
provisioner = self.get_kernel(kernel_id).provisioner
LAUNCHED[kernel_id] = Launch(getattr(provisioner, "aes_key", None) or key, env)
if isinstance(provisioner, ContainerProvisioner):
self.add_restart_callback(kernel_id, lambda: self.rebind(kernel_id), "restart")
return kernel_id
async def restart_kernel(self, kernel_id, now=False):
restarted = await super().restart_kernel(kernel_id, now=now)
self.rebind(kernel_id)
return restarted
# A kernel back on new ports, with a new key, as one in a container of its own after a restart: its
# clients move onto them, as jupyter_server moves a client that connects again, and so does its
# activity watch.
def rebind(self, kernel_id):
if kernel_id not in self or not self.ports_changed(kernel_id):
return
self.stop_watching_activity(kernel_id)
self.start_watching_activity(kernel_id)
kernel = self.get_kernel(kernel_id)
for connection in list(ZMQChannelsWebsocketConnection._open_sockets):
if connection.kernel_id == kernel_id:
for stream in connection.channels.values():
if not stream.closed():
stream.close()
connection.session.key = kernel.session.key
connection.create_stream()
for stream in connection.channels.values():
stream.on_recv_stream(connection.handle_outgoing_message)
def remove_kernel(self, kernel_id):
STARTED_WITH.pop(kernel_id, None)
LAUNCHED.pop(kernel_id, None)
@@ -261,6 +474,7 @@ final class EmrContainersGateways {
await client.wait_for_ready(timeout=self.kernel_info_timeout)
finally:
client.stop_channels()
self.rebind(kernel_id)
class WithSession:
@@ -343,9 +557,17 @@ final class EmrContainersGateways {
def init_webapp(self):
super().init_webapp()
httpserver.HTTPServer(web.Application([(r"/api/kernels/configure/([^/]+)", ConfigureHandler,
{"kernel_manager": self.kernel_manager})])
).listen(CONFIGURE_PORT, "127.0.0.1")
configure = web.Application([(r"/api/kernels/configure/([^/]+)", ConfigureHandler,
{"kernel_manager": self.kernel_manager})])
httpserver.HTTPServer(configure).listen(CONFIGURE_PORT, "127.0.0.1")
if OVERRIDDEN:
# The magic does not check the certificate; the image's openssl makes one.
subprocess.run(["openssl", "req", "-x509", "-newkey", "rsa:2048", "-nodes", "-days", "3650",
"-subj", "/CN=floci-gateway", "-keyout", HOME + "/configure.key",
"-out", HOME + "/configure.crt"], check=True, capture_output=True)
tls = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
tls.load_cert_chain(HOME + "/configure.crt", HOME + "/configure.key")
httpserver.HTTPServer(configure, ssl_options=tls).listen(CONFIGURE_TLS_PORT, "0.0.0.0")
REPLACED = {kernel_handlers.MainKernelHandler: MainKernelHandler,
@@ -354,7 +576,6 @@ final class EmrContainersGateways {
for module in (kernel_handlers, session_handlers):
module.default_handlers = [(path, REPLACED.get(handler, handler))
for path, handler in module.default_handlers]
os.environ["JUPYTER_PATH"] = HOME
sys.exit(GatewayApp.launch_instance(argv=[
"--ServerApp.ip=0.0.0.0", "--ServerApp.port=@PORT@", "--ServerApp.port_retries=0",
"--ServerApp.open_browser=False", "--ServerApp.allow_origin=*", "--ServerApp.root_dir=/home/hadoop",
@@ -365,7 +586,10 @@ final class EmrContainersGateways {
"--KernelSpecManager.allowed_kernelspecs=spark-scala-kubernetes",
] + sys.argv[1:], kernel_manager_class=RemoteMappingKernelManager))
""".replace("@HOME@", HOME).replace("@PORT@", String.valueOf(PORT))
.replace("@PROGRAM_VARIABLE@", PROGRAM_VARIABLE).replace("@SESSION_HEADER@", SESSION_HEADER);
.replace("@PROGRAM_VARIABLE@", PROGRAM_VARIABLE).replace("@SESSION_HEADER@", SESSION_HEADER)
.replace("@OVERRIDES_VARIABLE@", OVERRIDES_VARIABLE)
.replace("@KERNEL_CONTAINERS_VARIABLE@", KERNEL_CONTAINERS_VARIABLE)
.replace("@SECRET_VARIABLE@", SECRET_VARIABLE).replace("@SECRET_HEADER@", SECRET_HEADER);
private EmrContainersGateways() {
}
@@ -384,21 +608,24 @@ final class EmrContainersGateways {
* driver does, on the image's Python: the virtualenv the image gives a PySpark driver is made on YARN and
* Kubernetes alone. Their class path gains the AWS SDK that S3A runs on, which the image's own leaves out. The
* allow-listed properties of the endpoint's {@code jeg-config} classification go to the program as its arguments,
* other ones nowhere. As a Spark Connect server does, it checks no token itself: Floci reaches it on the Docker
* other ones nowhere. Where its {@code jupyter-kernel-overrides} gives kernels images of their own, the program
* learns their names, and where and with {@code secret} it asks Floci for their containers
* ({@link #kernelJob}). As a Spark Connect server does, it checks no token itself: Floci reaches it on the Docker
* host's loopback interface alone, and checks the session token first.
*/
static SparkJobRunner.Job job(ObjectNode endpoint, String imageTemplate, String dockerNetwork) {
static SparkJobRunner.Job job(ObjectNode endpoint, String imageTemplate, String dockerNetwork, String secret) {
AwsArnUtils.Arn arn = AwsArnUtils.parse(endpoint.get("arn").textValue());
String releaseLabel = endpoint.get("releaseLabel").textValue();
Map<String, String> sparkDefaults =
new LinkedHashMap<>(EmrContainersSparkJobs.sparkDefaults(endpoint.get("configurationOverrides")));
sparkDefaults.remove(MASTER);
sparkDefaults.put(EmrContainersSparkJobs.DEPLOY_MODE, EmrContainersSparkJobs.CLIENT);
sparkDefaults.put(PYSPARK_VIRTUALENV, "false");
Map<String, String> environment = new LinkedHashMap<>(EmrServerlessJobRunService.environment(sparkDefaults,
"spark.kubernetes.driverEnv."));
environment.merge(CLASS_PATH_VARIABLE, AWS_SDK, (endpointClassPath, sdk) -> endpointClassPath + ":" + sdk);
Map<String, String> sparkDefaults = sparkDefaults(endpoint);
Map<String, String> environment = environment(sparkDefaults);
environment.put(PROGRAM_VARIABLE, PROGRAM);
Map<String, String> kernelImages = kernelImages(endpoint);
if (!kernelImages.isEmpty()) {
environment.put(OVERRIDES_VARIABLE, String.join(",", kernelImages.keySet()));
environment.put(KERNEL_CONTAINERS_VARIABLE,
EmrContainersKernelContainersRoute.PATH + endpoint.get("id").textValue() + "/");
environment.put(SECRET_VARIABLE, secret);
}
List<String> command = new ArrayList<>(List.of("/bin/bash", "-c", "mkdir -p " + HOME + " && printf '%s' \"$"
+ PROGRAM_VARIABLE + "\" > " + HOME + "/gateway.py && exec python3 " + HOME + "/gateway.py \"$@\"",
"gateway"));
@@ -416,6 +643,84 @@ final class EmrContainersGateways {
Map.of(), null).listeningOn(PORT);
}
/**
* What runs kernel {@code kernelId} of an endpoint's gateway in a container of its own, run {@code runId}: the
* image its {@code jupyter-kernel-overrides} gives the kernel, running Jupyter Enterprise Gateway's launcher there,
* which sends where the kernel listens to {@code responseAddress}, encrypted for {@code publicKey}, as it does for
* Jupyter Enterprise Gateway. It has the gateway's settings and environment, with the {@code KERNEL_} variables of
* {@code kernelEnvironment}; a PySpark kernel's launcher runs under {@code spark-submit}, after
* {@code KERNEL_EXTRA_SPARK_OPTS}, and loads the {@code %%configure} magic.
*/
static SparkJobRunner.Job kernelJob(ObjectNode endpoint, String runId, String kernelName, String kernelId,
String responseAddress, String publicKey,
Map<String, String> kernelEnvironment, String imageTemplate,
String dockerNetwork) {
AwsArnUtils.Arn arn = AwsArnUtils.parse(endpoint.get("arn").textValue());
String releaseLabel = endpoint.get("releaseLabel").textValue();
Map<String, String> sparkDefaults = sparkDefaults(endpoint);
Map<String, String> environment = environment(sparkDefaults);
kernelEnvironment.forEach((name, value) -> {
if (name.startsWith("KERNEL_")) {
environment.put(name, value);
}
});
String launch = " " + LAUNCHER + " --kernel-id \"$1\" --response-address \"$2\" --public-key \"$3\""
+ " --port-range 0..0";
List<String> command = PYSPARK_KERNEL.equals(kernelName)
? List.of("/bin/bash", "-c", "exec \"$SPARK_HOME/bin/spark-submit\" $KERNEL_EXTRA_SPARK_OPTS" + launch
+ " --spark-context-initialization-mode lazy --cluster-type spark"
+ " --RemoteProcessProxy.additional-info \"$4\"",
"kernel", kernelId, responseAddress, publicKey, CONFIGURE_MAGIC_ENABLED)
: List.of("/bin/bash", "-c", "exec python3" + launch
+ " --spark-context-initialization-mode none --cluster-type none",
"kernel", kernelId, responseAddress, publicKey);
return new SparkJobRunner.Job(EmrContainersService.SERVICE, runId, arn.accountId(), arn.region(),
releaseLabel, SparkJobRunner.Engine.SPARK, image(imageTemplate, releaseLabel),
kernelImages(endpoint).get(kernelName), dockerNetwork, endpoint.get("executionRoleArn").textValue(),
sparkDefaults, List.of(), command, Duration.ZERO, null, null, environment, List.of(), null, Map.of(),
Map.of(), Map.of(), Map.of(), Map.of(), null);
}
/**
* The images an endpoint's {@code jupyter-kernel-overrides} gives its kernels, by kernel name, a later one's
* winning; a kernel other than those it may override goes unnamed.
*/
static Map<String, String> kernelImages(ObjectNode endpoint) {
Map<String, String> images = new LinkedHashMap<>();
for (JsonNode configuration : endpoint.path("configurationOverrides").path("applicationConfiguration")) {
if (!KERNEL_OVERRIDES.equals(configuration.path("classification").textValue())) {
continue;
}
for (JsonNode kernel : configuration.path("configurations")) {
String name = kernel.path("classification").textValue();
JsonNode image = kernel.path("properties").path(CONTAINER_IMAGE);
if (OVERRIDABLE_KERNELS.contains(name) && image.isTextual()) {
images.put(name, image.textValue());
}
}
}
return images;
}
// The endpoint's spark-defaults as its kernels' spark-submit gets them: in Spark's local mode and client deploy
// mode, with no virtualenv.
private static Map<String, String> sparkDefaults(ObjectNode endpoint) {
Map<String, String> sparkDefaults =
new LinkedHashMap<>(EmrContainersSparkJobs.sparkDefaults(endpoint.get("configurationOverrides")));
sparkDefaults.remove(MASTER);
sparkDefaults.put(EmrContainersSparkJobs.DEPLOY_MODE, EmrContainersSparkJobs.CLIENT);
sparkDefaults.put(PYSPARK_VIRTUALENV, "false");
return sparkDefaults;
}
// The environment of the endpoint's driver, with the AWS SDK that S3A runs on on its class path.
private static Map<String, String> environment(Map<String, String> sparkDefaults) {
Map<String, String> environment = new LinkedHashMap<>(EmrServerlessJobRunService.environment(sparkDefaults,
"spark.kubernetes.driverEnv."));
environment.merge(CLASS_PATH_VARIABLE, AWS_SDK, (endpointClassPath, sdk) -> endpointClassPath + ":" + sdk);
return environment;
}
/**
* Why no gateway answers at {@code host} and {@code port}, or null once one does: any HTTP answer to its API's
* root says jupyter_server runs. A port Docker publishes takes a connection before the server in the container
@@ -0,0 +1,187 @@
package io.floci.aws.services.emrcontainers;
import io.floci.aws.services.emrserverless.SparkJobRunner;
import org.jboss.logging.Logger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.HexFormat;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicLong;
import java.util.regex.Pattern;
/**
* The containers of the kernels an endpoint's {@code jupyter-kernel-overrides} gives an image of their own ("Custom
* kernel image with interactive endpoint"), each kernel in a container of its own, as on EMR on EKS each is a pod of
* its image. The endpoint's gateway asks for a kernel's container as it starts the kernel, and for its end as it stops
* it, with a secret Floci gives that gateway alone; an endpoint's containers end with its gateway.
*/
final class EmrContainersKernelContainers {
private static final Logger LOG = Logger.getLogger(EmrContainersKernelContainers.class);
/** A kernel's id, as jupyter_client makes one. */
static final Pattern KERNEL_ID = Pattern.compile("[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}");
/** Where a kernel's launcher sends where it listens: a host on the gateway's network, and a port. */
static final Pattern RESPONSE_ADDRESS = Pattern.compile("[0-9A-Za-z.-]{1,253}:[0-9]{1,5}");
/** The public key the launcher encrypts that with, in base64. */
static final Pattern PUBLIC_KEY = Pattern.compile("[A-Za-z0-9+/]{1,4096}={0,2}");
private final SparkJobRunner runner;
private final Executor executor;
// Each gateway's secret, account and Region, by its endpoint.
private final Map<String, Gateway> gateways = new ConcurrentHashMap<>();
// The run of each kernel's container, by endpoint, then kernel.
private final Map<String, Map<String, Kernel>> kernels = new HashMap<>();
private final AtomicLong launches = new AtomicLong();
EmrContainersKernelContainers(SparkJobRunner runner, Executor executor) {
this.runner = runner;
this.executor = executor;
}
/** What a gateway was given: its secret, and its endpoint's account and Region. */
record Gateway(String secret, String accountId, String region) {
}
// A kernel's container's run, and whether it was asked to stop.
private static final class Kernel {
final String runId;
volatile boolean stopped;
Kernel(String runId) {
this.runId = runId;
}
}
/** A new secret for endpoint {@code endpointId}'s gateway, in place of any an earlier gateway had. */
String secretFor(String endpointId, String accountId, String region) {
byte[] bytes = new byte[32];
new SecureRandom().nextBytes(bytes);
String secret = HexFormat.of().formatHex(bytes);
gateways.put(endpointId, new Gateway(secret, accountId, region));
return secret;
}
/** The gateway of endpoint {@code endpointId} that {@code secret} is the secret of, or null for none. */
Gateway gateway(String endpointId, String secret) {
Gateway gateway = gateways.get(endpointId);
return gateway != null && secret != null && MessageDigest.isEqual(
gateway.secret().getBytes(StandardCharsets.UTF_8), secret.getBytes(StandardCharsets.UTF_8))
? gateway : null;
}
/** The id of a new run of a kernel's container: its endpoint's and its own, with a count. */
String runId(String endpointId, String kernelId) {
return endpointId + "-" + kernelId + "-" + launches.incrementAndGet();
}
/**
* Runs {@code job}, kernel {@code kernelId}'s container, on a thread of its own, its earlier one stopped; false,
* running nothing, once the endpoint's gateway has ended ({@link #stopAll}), which takes its kernels under the
* same lock.
*/
boolean launch(String endpointId, String kernelId, SparkJobRunner.Job job) {
Kernel kernel = new Kernel(job.runId());
Kernel earlier;
synchronized (this) {
if (!gateways.containsKey(endpointId)) {
return false;
}
earlier = kernels.computeIfAbsent(endpointId, id -> new HashMap<>()).put(kernelId, kernel);
}
if (earlier != null) {
stop(earlier);
}
executor.execute(() -> {
try {
SparkJobRunner.Outcome outcome = runner.run(job, new Progress(kernel));
if (outcome.failure() != null) {
LOG.warnv("The container of kernel {0} of endpoint {1} could not start: {2}", kernelId,
endpointId, outcome.failure());
}
} catch (RuntimeException e) {
LOG.warnv(e, "The container of kernel {0} of endpoint {1} failed", kernelId, endpointId);
} finally {
synchronized (this) {
Map<String, Kernel> endpointKernels = kernels.get(endpointId);
if (endpointKernels != null && endpointKernels.remove(kernelId, kernel)
&& endpointKernels.isEmpty()) {
kernels.remove(endpointId);
}
}
}
});
return true;
}
/** Stops kernel {@code kernelId}'s container, where it runs. */
void stop(String endpointId, String kernelId) {
Kernel kernel;
synchronized (this) {
Map<String, Kernel> endpointKernels = kernels.get(endpointId);
kernel = endpointKernels != null ? endpointKernels.get(kernelId) : null;
}
if (kernel != null) {
stop(kernel);
}
}
/** Stops the containers of the endpoint's kernels, its gateway's secret going with them. */
void stopAll(String endpointId) {
List<Kernel> running;
synchronized (this) {
gateways.remove(endpointId);
Map<String, Kernel> endpointKernels = kernels.get(endpointId);
running = endpointKernels != null ? List.copyOf(endpointKernels.values()) : List.of();
}
running.forEach(this::stop);
}
/** Stops every kernel's container, the gateways' secrets going with them, as a reset ends every endpoint. */
void reset() {
List<Kernel> running;
synchronized (this) {
gateways.clear();
running = kernels.values().stream().flatMap(endpointKernels -> endpointKernels.values().stream())
.toList();
}
running.forEach(this::stop);
}
/** {@link #reset}, and the threads that run the containers stop, as Floci does. */
void shutdown() {
reset();
if (executor instanceof ExecutorService service) {
service.shutdownNow();
}
}
private void stop(Kernel kernel) {
kernel.stopped = true;
runner.stop(kernel.runId);
}
// A kernel's container's run: cancelled once asked to stop.
private record Progress(Kernel kernel) implements SparkJobRunner.Progress {
@Override
public void started() {
}
@Override
public void timedOut() {
}
@Override
public boolean cancelled() {
return kernel.stopped;
}
}
}
@@ -0,0 +1,122 @@
package io.floci.aws.services.emrcontainers;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.core.common.ServiceConfigAccess;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.io.IOException;
/**
* Where an endpoint's gateway asks Floci for the container of a kernel that the endpoint's
* {@code jupyter-kernel-overrides} gives an image of its own, and for its end ({@link EmrContainersKernelContainers}):
* {@code POST} and {@code DELETE} on {@link #PATH}{@code <endpointId>/<kernelId>}, with the secret Floci gave that
* gateway in {@link EmrContainersGateways#SECRET_HEADER}. Floci's own: on EMR on EKS the service runs a kernel's pod
* itself.
*/
@ApplicationScoped
public class EmrContainersKernelContainersRoute {
static final String PATH = "/_aws/emr-containers/kernel-containers/";
private static final Logger LOG = Logger.getLogger(EmrContainersKernelContainersRoute.class);
private static final ObjectMapper JSON = new ObjectMapper();
// A start's body: a kernel's name, a response address, a public key and the kernel's KERNEL_ variables.
private static final long MAX_BODY = 64 * 1024;
private final EmrContainersService service;
private final ServiceConfigAccess serviceConfigAccess;
private final Vertx vertx;
@Inject
public EmrContainersKernelContainersRoute(EmrContainersService service, ServiceConfigAccess serviceConfigAccess,
Vertx vertx) {
this.service = service;
this.serviceConfigAccess = serviceConfigAccess;
this.vertx = vertx;
}
void register(@Observes Router router) {
router.route(PATH + ":endpointId/:kernelId").handler(this::handle);
}
void handle(RoutingContext context) {
// This route runs ahead of the filter that refuses a disabled service, so it checks for itself.
if (!serviceConfigAccess.isEnabled(EmrContainersService.SERVICE)) {
context.next();
return;
}
HttpServerRequest request = context.request();
String endpointId = context.pathParam("endpointId");
String kernelId = context.pathParam("kernelId");
String secret = request.getHeader(EmrContainersGateways.SECRET_HEADER);
if (request.method() == HttpMethod.DELETE) {
vertx.<EmrContainersService.KernelContainerRefusal>executeBlocking(
() -> service.stopKernelContainer(endpointId, kernelId, secret), false)
.onComplete(stopped -> {
if (stopped.failed()) {
LOG.warnv(stopped.cause(), "Kernel {0}'s container could not be stopped", kernelId);
answer(request, 500, "The kernel's container could not be stopped.");
} else if (stopped.result() == null) {
request.response().setStatusCode(204).end();
} else {
answer(request, stopped.result().status(), stopped.result().message());
}
});
return;
}
if (request.method() != HttpMethod.POST) {
answer(request, 405, "A kernel's container is started with POST and stopped with DELETE.");
return;
}
String length = request.getHeader("content-length");
if (length == null || !length.matches("[0-9]{1,6}") || Long.parseLong(length) > MAX_BODY) {
answer(request, 400, "The request's body has no length, or too long a one.");
return;
}
// Quarkus hands the route the request paused, and resumes it for a handler set on it but not for body().
Future<Buffer> received = request.body();
request.resume();
received.onComplete(read -> {
JsonNode body;
try {
body = read.succeeded() ? JSON.readTree(read.result().getBytes()) : null;
} catch (IOException e) {
body = null;
}
if (body == null || !body.isObject()) {
answer(request, 400, "The request's body is no JSON object.");
return;
}
JsonNode start = body;
vertx.<EmrContainersService.KernelContainerRefusal>executeBlocking(
() -> service.startKernelContainer(endpointId, kernelId, secret, start), false)
.onComplete(started -> {
if (started.failed()) {
LOG.warnv(started.cause(), "Kernel {0}'s container could not be started", kernelId);
answer(request, 500, "The kernel's container could not be started.");
} else if (started.result() == null) {
answer(request, 201, "The kernel's container is starting.");
} else {
answer(request, started.result().status(), started.result().message());
}
});
});
}
private static void answer(HttpServerRequest request, int status, String message) {
request.response().setStatusCode(status).putHeader("content-type", "application/json")
.end(new JsonObject().put("message", message).encode());
}
}
@@ -276,15 +276,17 @@ public class EmrContainersService implements Resettable, StoredResourceArns {
}
if (gateways != null) {
gateways.servers().shutdown();
gateways.kernels().shutdown();
}
}
/**
* The managed endpoints' gateways of the docker runner mode: the servers that run them, the image they run in
* ({@code gateway-image-template}, null when not set), and Floci's base URL, which their {@code serverUrl} is
* under.
* The managed endpoints' gateways of the docker runner mode: the servers that run them, the containers of their
* kernels with images of their own, the image they run in ({@code gateway-image-template}, null when not set), and
* Floci's base URL, which their {@code serverUrl} is under.
*/
record EndpointGateways(SessionServers servers, String imageTemplate, String baseUrl) {
record EndpointGateways(SessionServers servers, EmrContainersKernelContainers kernels, String imageTemplate,
String baseUrl) {
static EndpointGateways of(SparkJobRunner runner, Clock clock, String imageTemplate, String baseUrl) {
HttpClient probes = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(5)).build();
@@ -292,6 +294,8 @@ public class EmrContainersService implements Resettable, StoredResourceArns {
Executors.newCachedThreadPool(Thread.ofPlatform().daemon().name("emr-containers-gateway-", 0)
.factory()),
null, (host, port) -> EmrContainersGateways.unanswered(probes, host, port), clock),
new EmrContainersKernelContainers(runner, Executors.newCachedThreadPool(
Thread.ofPlatform().daemon().name("emr-containers-kernel-", 0).factory())),
imageTemplate, baseUrl);
}
}
@@ -674,7 +678,8 @@ public class EmrContainersService implements Resettable, StoredResourceArns {
endpoint.put(CLIENT_TOKEN, clientToken);
endpoints.put(arn, endpoint);
if (gateways != null) {
gateways.servers().launch(EmrContainersGateways.job(endpoint, gateways.imageTemplate(), dockerNetwork),
gateways.servers().launch(EmrContainersGateways.job(endpoint, gateways.imageTemplate(), dockerNetwork,
gateways.kernels().secretFor(id, regionResolver.getAccountId(), regionResolver.getRegion())),
gatewayOwner);
}
return pick(endpoint, "id", "name", "arn", "virtualClusterId");
@@ -778,6 +783,72 @@ public class EmrContainersService implements Resettable, StoredResourceArns {
return gateways == null ? Optional.empty() : gateways.servers().routeWithoutToken(endpointId, gatewayOwner);
}
/**
* Starts the container of kernel {@code kernelId} of endpoint {@code endpointId}, as its gateway asks with
* {@code secret}, the one Floci gave it: the kernel's {@code kernel} names one the endpoint's
* {@code jupyter-kernel-overrides} gives an image, its {@code responseAddress} and {@code publicKey} where its
* launcher sends where the kernel listens, and its {@code env} the kernel's {@code KERNEL_} variables. Null once
* started, else why not, as the status and message of the answer. Called off the event loop.
*/
public KernelContainerRefusal startKernelContainer(String endpointId, String kernelId, String secret,
JsonNode request) {
EmrContainersKernelContainers.Gateway gateway =
gateways == null ? null : gateways.kernels().gateway(endpointId, secret);
if (gateway == null) {
return notSigned(endpointId);
}
String kernel = request.path("kernel").textValue();
String responseAddress = request.path("responseAddress").textValue();
String publicKey = request.path("publicKey").textValue();
if (!EmrContainersKernelContainers.KERNEL_ID.matcher(kernelId).matches() || responseAddress == null
|| !EmrContainersKernelContainers.RESPONSE_ADDRESS.matcher(responseAddress).matches()
|| publicKey == null || !EmrContainersKernelContainers.PUBLIC_KEY.matcher(publicKey).matches()
|| !request.path("env").isObject()) {
return new KernelContainerRefusal(400, "The request names no kernel id, response address, public key "
+ "and env.");
}
Map<String, String> environment = new LinkedHashMap<>();
request.get("env").properties().forEach(variable -> {
if (variable.getValue().isTextual()) {
environment.put(variable.getKey(), variable.getValue().textValue());
}
});
return RequestScopes.callAs(gateway.accountId(), gateway.region(), () -> {
synchronized (this) {
ObjectNode endpoint = endpointById(endpointId).orElse(null);
if (endpoint == null || !ACTIVE.equals(endpoint.path("state").textValue())) {
return new KernelContainerRefusal(409, "Endpoint " + endpointId + " is not ACTIVE.");
}
if (!EmrContainersGateways.kernelImages(endpoint).containsKey(kernel)) {
return new KernelContainerRefusal(400, "Endpoint " + endpointId
+ "'s jupyter-kernel-overrides gives kernel " + kernel + " no image.");
}
// Refused where the gateway's end has taken its secret and kernels since its secret was checked.
return gateways.kernels().launch(endpointId, kernelId, EmrContainersGateways.kernelJob(endpoint,
gateways.kernels().runId(endpointId, kernelId), kernel, kernelId, responseAddress, publicKey,
environment, gateways.imageTemplate(), dockerNetwork)) ? null
: new KernelContainerRefusal(409, "Endpoint " + endpointId + "'s gateway has stopped.");
}
});
}
/** Stops the container of kernel {@code kernelId}, as its gateway asks with {@code secret}: null, else why not. */
public KernelContainerRefusal stopKernelContainer(String endpointId, String kernelId, String secret) {
if (gateways == null || gateways.kernels().gateway(endpointId, secret) == null) {
return notSigned(endpointId);
}
gateways.kernels().stop(endpointId, kernelId);
return null;
}
/** Why a gateway's call for a kernel's container is refused: the answer's status and message. */
public record KernelContainerRefusal(int status, String message) {
}
private static KernelContainerRefusal notSigned(String endpointId) {
return new KernelContainerRefusal(403, "The request is not signed by endpoint " + endpointId + "'s gateway.");
}
/**
* The endpoint as it stands, stored: in the docker runner mode, one whose gateway no thread of this process runs,
* as after a restart, has ended, TERMINATED_WITH_ERRORS, or TERMINATED where it was being deleted.
@@ -855,6 +926,8 @@ public class EmrContainersService implements Resettable, StoredResourceArns {
@Override
public void stopped(String endpointId, SparkJobRunner.Outcome outcome) {
// Its kernels' own containers go with the gateway.
gateways.kernels().stopAll(endpointId);
synchronized (EmrContainersService.this) {
endpointById(endpointId)
.filter(stored -> !FINISHED_ENDPOINT_STATES.contains(stored.path("state").textValue()))
@@ -1068,6 +1141,7 @@ public class EmrContainersService implements Resettable, StoredResourceArns {
executing.clear();
if (gateways != null) {
gateways.servers().reset();
gateways.kernels().reset();
}
}
@@ -1077,6 +1151,7 @@ public class EmrContainersService implements Resettable, StoredResourceArns {
executing.clear();
if (gateways != null) {
gateways.servers().reset();
gateways.kernels().reset();
}
virtualClusters.clear();
jobRuns.clear();