14 / 35 · Distributed Computation
The Spark execution model
Read a lazy dependency graph, identify a shuffle stage and explain how Spark can rebuild a missing partition.
The shop wants the same totals and then a second report over the same input. A pipeline of separate MapReduce jobs can write each intermediate result before continuing. Spark describes transformations as dependencies and can run several local steps together before it exchanges data.
Describe work before starting it#
An RDD is a partitioned collection that Spark can recompute from its dependencies. A transformation such as map or filter defines another collection. An action such as collect asks for a result and triggers execution of the work needed for it. The driver coordinates the application. Executors run tasks that process partitions. (Zaharia et al., 2012, §§2–3; Spark 3.5.8, RDD Programming Guide)
Assume a Spark context sc. The following RDD example uses the 12 toy sales amounts from distributed aggregation. parallelize makes a small driver-side list into a distributed collection. A production job would usually read partitions from storage.
from operator import add
raw = [
(1, 100), (1, 200), (2, 50),
(1, 150), (2, 70), (2, 30),
(1, 100), (1, 50), (2, 200),
(1, 200), (2, 120), (2, 80),
]
sales = sc.parallelize(raw, 4)
positive = sales.filter(lambda row: row[1] > 0)
totals = positive.reduceByKey(add, numPartitions=2)
print(sorted(totals.collect()))
The expected output is [(1, 800), (2, 550)]. We sort the two collected pairs only to make presentation deterministic. A large result should be written or aggregated in the cluster rather than collected into driver memory. (Spark 3.5.8, RDD Programming Guide)
Find the boundary between stages#
In a narrow dependency, a parent partition is needed by at most one child partition. Here filtering a partition requires only the rows already in that partition. The filter can run in the task that prepares its shuffle output. The key reduction needs partial sums from several input partitions. Spark divides execution at the shuffle, with tasks writing shuffle output on one side and tasks consuming the relevant blocks on the other. In this simple example the four input partitions feed two output partitions. (Zaharia et al., 2012, §§3.1 and 5; Spark 3.5.8, Shuffle operations)
The shuffle divides two stages
The partition dependency graph is called lineage. If a cached partition is lost, Spark can recompute it from available parents. Missing shuffle output may require rerunning an upstream task. This relies on input data remaining available and on replayable transformations. Repeating a task can repeat an external effect. Lineage describes data reconstruction, not a transaction with an external service.
The example uses RDD functions so that each dependency is visible. Spark SQL and DataFrames also build logical and physical plans, with an optimizer that can change operators before tasks are scheduled. The same SQL can therefore create a different execution graph than the RDD example. (Armbrust et al., 2015, §§3–4)
Next#
A stage boundary says where movement may occur. The cost of distributed execution measures what the movement carries, what can spill and when materializing a reusable input helps.
References#
- Matei Zaharia et al. Resilient Distributed Datasets. NSDI, 2012, §§2–3 and 5 on lineage, narrow dependencies and scheduling.
- Apache Software Foundation. RDD Programming Guide. Spark 3.5.8 documentation on transformations, actions, shuffle and persistence.
- Michael Armbrust et al. Spark SQL: Relational Data Processing in Spark. SIGMOD, 2015, §§3–4 on the DataFrame API and the Catalyst optimizer.