Knobs Fixed at Startup
Concepts covered: Configuration Tuning
When getOrCreate() starts a session, Spark asks the cluster for executors of the shape you configured, and the cluster launches each as a JVM whose heap size and core count are fixed on the command line that starts it. A running JVM cannot raise its maximum heap, and the cluster has reserved those exact cores and that memory on particular machines. So Spark reads the executor knobs once, at launch, and the executors keep them until the session stops.
SQL settings live somewhere else. Catalyst reads settings such as spark.sql.shuffle.partitions each time it plans a query, and a query is planned when an action runs, so a value changed between 2 actions shapes the second one.
| Setting | Read when | Changed on a running session |
|---|---|---|
| `spark.executor.instances` | The session starts | Refused with an error |
| `spark.executor.cores` | Executors launch | Refused with an error |
| `spark.executor.memory` | Executors launch | Refused with an error |
| `spark.driver.memory` | The driver process starts | Refused with an error |
| `spark.sql.shuffle.partitions` | Each query is planned | Applies from the next action |
| `spark.sql.autoBroadcastJoinThreshold` | Each query is planned | Applies from the next action |
| `spark.sql.adaptive.enabled` | Each query is planned | Applies from the next action |
Trying to change them in place
spark.conf.set("spark.sql.shuffle.partitions", "400")
spark.conf.set("spark.executor.memory", "32g")The first line takes effect at the next action, whose groupBy plans 400 shuffle partitions instead of 200. The second raises an AnalysisException saying it cannot modify the value of a Spark config, spark.executor.memory, and the executors keep their 16g.
A second attempt fails quietly. Calling the builder again with a new spark.executor.memory and then getOrCreate() hands back the session that already exists. Spark applies the runtime SQL settings from the builder, logs a warning about the rest, and the executors keep running at their old size. In a notebook the cell looks like it succeeded.
Changing executor resources for real
Stop the session and start a new one with the new values:
from pyspark.sql import SparkSession
spark.stop()
spark = (
SparkSession.builder
.appName("nightly_watch_time")
.config("spark.executor.instances", "25")
.config("spark.executor.cores", "4")
.config("spark.executor.memory", "32g")
.getOrCreate()
)spark.stop() releases every executor, and everything they held goes with them, cached DataFrames included. The new session waits for the cluster to launch fresh executors, then recomputes any cache the first time it is used. Driver memory is fixed even earlier: the driver is already running when this code runs, so spark.driver.memory belongs in the launch command or the cluster's configuration, where it is read before the driver starts.
- Fixed when the session starts
- spark.executor.instances, spark.executor.cores, spark.executor.memory
- spark.driver.memory and spark.driver.cores
- Changed only by stopping the session and starting a new one
- A restart drops the executors and their cached data
- Read each time a query is planned
- spark.sql.shuffle.partitions
- spark.sql.autoBroadcastJoinThreshold
- spark.sql.adaptive.enabled
- Changed with spark.conf.set, in effect from the next action
A few spark.sql.* settings are static as well. spark.sql.warehouse.dir is read once when the session starts, and setting it later raises an error saying it can't modify a static config.
A notebook session started with 16g executors and spark.sql.shuffle.partitions at 200. A teammate runs this cell, then reruns a slow groupBy. What does the groupBy run with?
spark = (
SparkSession.builder
.config("spark.executor.memory", "32g")
.config("spark.sql.shuffle.partitions", "800")
.getOrCreate()
)- 32g executors and 800 shuffle partitions, since the builder sets both values.
- 16g executors and 200 shuffle partitions, since the builder changed nothing.
- An error, since getOrCreate() refuses executor settings on a live session.
- 16g executors, as before, and 800 shuffle partitions from the builder.
About this lesson
This section is part of the Cluster Sizing: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.