Skip to content

How it works

What happens when a Jupyter notebook cell runs on Spark

Sparkmagic sends each cell over the Livy API to ilum-core, which keeps one Spark application on the cluster for the notebook. The first cell waits for it to start, and every later cell finds it running.

For Ilum 6.7.3. To follow along you need a cluster with Ilum installed and kubectl.

Four cells.
One Spark session.

session 17: the first cell 51.0 s, every next one 2.9 s

cell 1, print("Hello"): 51.0 s. cell 2, rdd = sc.parallelize([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]): 2.9 s. cell 3, spark.sql("CREATE DATABASE example_intro"): 2.9 s. cell 4, df = spark.read.csv('file:///opt/spark/examples/src/main/resources/people.csv', header=True, inferSchema=True, sep=';'): 2.9 s.

Run the cells.

In a PySpark notebook every cell goes to Spark, with or without %%spark. The first one you run waits for Spark to start. The rest find it running.

Session up

One statement on a driver that is already running.

PySpark
  1. print("Hello")
    
    51.0 s
    Starting Spark application
    IDYARN Application IDKindStateSpark UIDriver logUserCurrent session?
    17spark-253ce7b5f8e847be8543cabbb7d37506pysparkidleLinkLinkproxyUser✔
    SparkSession available as 'spark'.
    Hello
  2. %%spark
        # This command above indicates that you are using an Ilum remote session
    
        rdd = sc.parallelize([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
        print(rdd.count())
    
    2.9 s
    10
  3. %%spark
    
        spark.sql("CREATE DATABASE example_intro")
    
    2.9 s
  4. %%spark
    
        df = spark.read.csv('file:///opt/spark/examples/src/main/resources/people.csv', header=True, inferSchema=True, sep=';')
        df.printSchema()
    
        df.write.format("delta").saveAsTable("example_intro.people")
    
        df.select("name", "age").show()
    
    2.9 s
    root
     |-- name: string (nullable = true)
     |-- age: integer (nullable = true)
     |-- job: string (nullable = true)
    
    +-----+---+
    | name|age|
    +-----+---+
    |Jorge| 30|
    |  Bob| 32|
    +-----+---+

On the cluster

  • ilum-jupyterkernel of the notebookRunning
  • ilum-coreLivy API on port 9888Running
  • session 17a service of kind CODEidle
  • driverservice-<groupId>-<hash>-driverRunning
  • 2 executorsservice-<groupId>-<hash>-exec-<n>Running

Calls of this cellilum-jupyter to ilum-core

  1. POST /sessions201 starting
  2. GET /sessions/17idle
  3. POST /sessions/17/statements201 waiting
  4. GET /sessions/17/statements/{statementId}available
  5. none yet

On a two-node cluster with 8 vCPU the first call to a new Spark service took 51.0 s. Later calls took a median of 2.9 s.

Step by step

Step 1 of 7. your terminal → ilum-ui · ilum-ui → ilum-jupyter

Open a notebook in Jupyter

Jupyter is one of the workloads of a default install, and the console opens it under an address of its own. The kernel of a notebook is a process in the ilum-jupyter pod. It holds no Spark, and so far nothing has asked for any.

A new notebook asks which kernel to use. Python 3 runs your code in the Jupyter pod itself. PySpark and Spark send it to a Spark session, in Python and in Scala.

shelldocs ↗
kubectl port-forward svc/ilum-ui 9777:9777

Spark pods after this step

none

  • your terminal ilum-ui · HTTP · localhost:9777
  • ilum-ui ilum-jupyter · HTTP · /external/jupyter/
Default install
Jupyter is on unless you install with ilum-jupyter.enabled=false.
In the console
The console shows Jupyter in a frame. The proxy of ilum-ui serves it under /external/jupyter/.
Permission
Opening it takes the NOTEBOOK_READ permission of the console.
Without the console
kubectl port-forward svc/ilum-jupyter 8888:8888, then http://localhost:8888/external/jupyter/.
Image
The pod runs ilum/sparkmagic: Jupyter with Sparkmagic installed and pointed at the cluster.
Kernels
Python runs on the Jupyter server. PySpark runs your Python code on a Spark session and Spark does the same for Scala.

Step 2 of 7. ilum-jupyter → ilum-core · ilum-core → driver

The first cell asks for a session

The PySpark kernel belongs to Sparkmagic, and Sparkmagic speaks the Livy protocol. Its endpoint is ilum-core on port 9888, which serves that API itself. Before the first cell can run, it posts a session. ilum-core answers with an id and the state starting, and creates a service for it.

There is no Livy server in the install. ilum-core implements the Livy API in its own process and serves it at the root of its HTTP port, beside its REST API. The chart writes that address into the configuration of Sparkmagic, so a notebook needs no connection settings.

Spark pods after this step

  • service-<groupId>-<hash>-driverPending
  • ilum-jupyter ilum-core · HTTP · POST /sessions
  • ilum-core driver · create the pod
Endpoint
ilum-jupyter.livyEndpoint, which the chart sets to http://ilum-core:9888.
Served by ilum-core
The Livy API is part of ilum-core, behind ilum-core.livy.enabled, and it is on by default.
Version
It answers as Livy 0.8.0.
ilum-livy-proxy
Older releases ran a proxy under this name. What a default install still has is a Service of that name on port 8998 that points at ilum-core, for clients that use the old address. It is deprecated.
The request
POST /sessions with the kind of the session: pyspark for Python, spark for Scala. SparkR sessions are refused.
The answer
201 with the id of the session and the state starting.
What a session can ask for
Memory and cores of the driver and the executors, the number of executors, Spark configuration, extra packages, a time to live and auto-pause.

Step 3 of 7. driver → ilum-core · ilum-jupyter → ilum-core

Spark starts, once per notebook

To ilum-core the session is a service with one replica: a Spark application that stays up. The driver comes up, its executors follow, and the driver reports READY. Sparkmagic keeps asking for the session until its state is idle. That wait is what the first cell costs.

The service is of kind CODE. Its driver runs an interpreter and takes code, where a service of kind JOB takes calls to a class you uploaded. A CODE service has exactly one replica, so everything a notebook runs shares one SparkSession.

On a two-node cluster with 8 vCPU the first call to a new Spark service took 51.0 s. Later calls took a median of 2.9 s.

Spark pods after this step

  • service-<groupId>-<hash>-driverRunning
  • service-<groupId>-<hash>-exec-1Running
  • service-<groupId>-<hash>-exec-2Running
  • driver ilum-core · gRPC · ready
  • ilum-jupyter ilum-core · HTTP · GET /sessions/{id}
One replica
A service of kind CODE cannot be scaled above one.
Name
The name the client gives the session, or livy-session- and four digits.
In the console
The session is listed among the services on the Workloads page, where it can be watched, paused and deleted like any other.
States
The state of a Livy session is the state of the job behind it. STARTING is starting, READY is idle and BUSY is busy.
Executors
Two, unless the session asks for another number.
How long Sparkmagic waits
300 seconds for the session to start, set by ilum-jupyter.sparkmagic.config.livySessionStartupTimeoutSeconds.

Step 4 of 7. ilum-jupyter → ilum-core · ilum-core → driver

The cell travels as a statement

Only now does the code of the cell leave Jupyter. ilum-core pushes it to the driver, where an interpreter runs it against the SparkSession that already exists. Sparkmagic asks for the statement until it is available and prints its output under the cell.

To ilum-core a statement is a request to the service like any other, so it is kept with its code and its result. The driver of a Python session compiles the code and executes it in one namespace that lives as long as the driver. That namespace is where the variables of the notebook are.

Spark pods after this step

  • service-<groupId>-<hash>-driverRunning
  • service-<groupId>-<hash>-exec-1Running
  • service-<groupId>-<hash>-exec-2Running
  • ilum-jupyter ilum-core · HTTP · POST /sessions/{id}/statements
  • ilum-core driver · gRPC · execute
Send
POST /sessions/{id}/statements with the code. The answer is 201 and a statement in the state waiting.
Ask
GET /sessions/{id}/statements/{n} until the state is available. The output is the text the code printed.
Interpreter
Standard output and standard error are captured while the code runs and become the result of the statement.
spark and sc
Both names exist in the namespace before your first line. sc is spark.sparkContext.
Kinds of code
A Python session takes pyspark and sql statements, a Scala session takes spark and sql.
Cancel
A statement can be cancelled while it runs.

Step 5 of 7. ilum-jupyter → ilum-core · ilum-core → driver · …

The next cell finds Spark running

Every later cell is those two calls again and nothing has to start. The session keeps its variables and temporary views from cell to cell. It reaches the metastore and the object storage of the cluster without any setup in the notebook.

ilum-core configures a session the way it configures every job it launches. A table a cell saves goes to the Hive Metastore of the install and its files to the object storage. With lineage on, what the cells read and write is reported like the work of any job. Training models on those tables, with MLflow beside the notebook, is the subject of model development on Ilum.

Spark pods after this step

  • service-<groupId>-<hash>-driverRunning
  • service-<groupId>-<hash>-exec-1Running
  • service-<groupId>-<hash>-exec-2Running
  • ilum-jupyter ilum-core · HTTP · POST /sessions/{id}/statements
  • ilum-core driver · gRPC · execute
  • driver ilum-hive-metastore · Thrift · table metadata
  • exec-2 ilum-minio · S3 · s3a:// write
Storage and metastore
A session reaches every storage linked to the default cluster, and the Hive Metastore, with nothing configured in the notebook.
Lineage and history
Sessions send their lineage to Marquez and their events to the Spark History Server when those are part of the install.
One session per notebook
Each open notebook has a session of its own. Two notebooks do not share variables.
Auto-pause
A session created with auto-pause is paused when it has been idle. Its pods go away, and the next cell starts them again before it runs.
Resume
A statement sent to a paused session resumes it first.

Step 6 of 7. ilum-jupyter → ilum-gitea

The notebook itself is a file in Git

None of this stores the notebook. The file lives in the work folder of the Jupyter pod, on a volume of its own. That folder is a Git repository, and its remote is the Gitea of the same install on port 3000.

When the pod starts for the first time, an init container copies the notebooks that come with Ilum into the work folder, makes the folder a repository, commits it and pushes it to Gitea. From then on committing is yours to do, from the Git panel of JupyterLab or from a terminal in it.

Spark pods after this step

  • service-<groupId>-<hash>-driverRunning
  • service-<groupId>-<hash>-exec-1Running
  • service-<groupId>-<hash>-exec-2Running
  • ilum-jupyter ilum-gitea · HTTP · git push
Folder
/home/jovyan/work, on the volume claim ilum-jupyter-work-pvc.
Size
4Gi unless ilum-jupyter.pvc.storage says otherwise.
Repository
A repository named jupyter under the Gitea account of the install, on the branch master.
Gitea
Part of the default install. Jupyter reaches it at ilum-gitea-http on port 3000.
What comes with it
IlumIntro.ipynb, a tour of the magics, and folders for the table formats, a medallion pipeline, MLflow, Sedona, Nessie, RAG and the pipeline exporter.
The first notebook
IlumIntro.ipynb creates a session, makes a database, writes a Delta table named example_intro.people and reads it back.
One workspace
This Jupyter is a single instance. Everyone who opens it works in the same folder and the same repository.

Step 7 of 7. ilum-jupyter → ilum-core

The session ends with its kernel

When the kernel of the notebook shuts down, Sparkmagic deletes its session. ilum-core stops the service and removes it, and the pods go away. The next notebook starts an application of its own and waits for it.

A session you created by hand in the Python kernel is yours to delete, in the panel that created it. A session can also be created with a time to live, after which ilum-core removes it.

Spark pods after this step

  • service-<groupId>-<hash>-driverremoved
  • service-<groupId>-<hash>-exec-1removed
  • service-<groupId>-<hash>-exec-2removed
  • ilum-jupyter ilum-core · HTTP · DELETE /sessions/{id}
Delete
DELETE /sessions/{id} stops the service and deletes it.
From a cell
%%delete -f -s with an id deletes another session of the endpoint. %%cleanup -f deletes all of them.
Time to live
ilum-core checks the ttl of a session whenever the session is used, and sweeps for expired ones every five minutes.
Kernels left open
A kernel that stays open keeps its driver and its executors. Stop the ones you do not use.
your Kubernetes cluster · namespace default
Spark pods of this session
your terminalhelm, kubectl, curl
ilum-uiWeb console9777 HTTP
ilum-jupyterNotebooks (Sparkmagic)8888 HTTP
ilum-coreJobs, sessions, SQL, REST API9888 HTTP · 9999 gRPC
ilum-giteaGit for notebooks and jobs3000 HTTP
ilum-hive-metastoreCatalog: table metadata9083 Thrift
ilum-minioObject storage (S3 API)9000 S3
driverSpark driver of the sessionPendingRunningCompleted
exec-1Spark executorPendingRunningCompleted
exec-2Spark executorPendingRunningCompleted
HTTPlocalhost:9777HTTP/external/jupyter/HTTPPOST /sessionsK8s APIcreate the podgRPCreadyHTTPGET /sessions/{id}HTTPPOST /sessions/{id}/statementsgRPCexecuteHTTPPOST /sessions/{id}/statementsgRPCexecuteThrifttable metadataS3s3a:// writeHTTPgit pushHTTPDELETE /sessions/{id}A notebook cell: a diagram of the cluster in 7 steps.

What the magics do

A magic is a line that starts with a percent sign and changes what Sparkmagic does with the cell. In the PySpark and Spark kernels a cell without one is sent to the session as it is.

%%spark
Runs the cell on the session. In the Python kernel this is the line that sends a cell to Spark at all. Without it the cell runs in the Jupyter pod.
%%sql
Runs the cell as a Spark SQL query on the session and shows the rows. In the Python kernel the same is written %%spark -c sql.
-o name
On a query, brings the rows back into the Jupyter pod as a pandas DataFrame of that name, for charts and local code.
Rows that come back
At most 2500 for a SQL magic, set by ilum-jupyter.sparkmagic.config.maxResultsSql. The flag -n changes it for one cell.
%%local
Runs the cell in the Jupyter pod and not on Spark.
%%send_to_spark
Copies a local value into the session.
%%pretty
Prints a Spark DataFrame as a table.
%%info
Lists the sessions of the endpoint with their ids and states.
%%configure -f
Sets the Spark parameters of the session as JSON. The session is started again with them, so what the notebook held in memory is gone.
%%logs
Prints the logs of the session.
%manage_spark
In the Python kernel, opens a panel that creates, lists and deletes sessions. A cell picks one of them with %%spark -s and its name.

The other notebooks

Jupyter with Sparkmagic is what a default install gives you. These are the others, the notebooks of the SQL editor among them.

JupyterHub
Enterprise. One Jupyter for each user, with a login and a private Git repository for each.
Apache Zeppelin
Marked as deprecated in the module list of the console.
VS Code
The Jupyter extension of VS Code can use this Jupyter as its server, through a port-forward of ilum-jupyter.
Pipeline exporter
A JupyterLab extension that comes installed. It turns a notebook into an Airflow DAG, or into a job, a service or a schedule of Ilum.
Notebooks of the SQL editor
The SQL editor of the console has notebooks of its own, with a permission of their own. Their cells are SQL statements sent to a SQL engine, and no Livy session is involved.

Run it on your own cluster

Every step runs on any Kubernetes cluster after two commands, on the free Community license.

shelldocs ↗
helm repo add ilum https://charts.ilum.cloud
helm install ilum ilum/ilum
NextHow an interactive Spark session stays warmThe service under a notebook session, without the notebook: one request creates it, every call after that goes to the running driver, and an idle one is paused.