spark: a framework for iterative and interactive...

49
Spark: A framework for iterative and interactive cluster computing UC BERKELEY Matei Zaharia (presented by Anthony D. Joseph) LASER Summer School September 2013

Upload: hoangnhu

Post on 19-Mar-2018

220 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Spark: A framework for iterative and interactive ���

cluster computing���

UC  BERKELEY  

Matei Zaharia (presented by Anthony D. Joseph)

LASER Summer School

September 2013

Page 2: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

My Talks at LASER 2013

1.  AMP Lab introduction

2.  The Datacenter Needs an Operating System

3.  Mesos, part one

4.  Dominant Resource Fairness

5.  Mesos, part two

6.  Spark 2

Page 3: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Researchers •  Matei Zaharia

•  Mosharaf Chowdhury

•  Michael Franklin

•  Scott Shenker

•  Ion Stoica

http://spark.incubator.apache.org/

Spark: Cluster Computing with Working Sets. Matei Zaharia, Mosharaf Chowdhury, Michael J. Franklin, Scott Shenker, Ion Stoica. HotCloud 2010. June 2010.

3

Page 4: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Berkeley Data Analytics Stack

Apache Spark

Shark BlinkDB

SQL

HDFS / Hadoop Storage / Tachyon

Apache Mesos / YARN Resource Manager

Spark Streaming

GraphX MLBase

4

Page 5: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Motivation

Complex jobs, Machine Learning algorithms, interactive queries and online processing all need one thing that Hadoop MR lacks:

Efficient primitives for data sharing

Stag

e 1

Stag

e 2

Stag

e 3

Iterative job

Query 1

Query 2

Query 3

Interactive mining

Job

1

Job

2

Stream processing 5

Page 6: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Transfer and Sharing in Hadoop

iter. 1 iter. 2 . . .

Input

HDFS ���read

HDFS ���write

HDFS ���read

HDFS ���write

Input

query 1

query 2

query 3

result 1

result 2

result 3

. . .

HDFS ���read

6

Page 7: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

iter. 1 iter. 2 . . .

Input

In-Memory Data Sharing

Distributed���memory

Input

query 1

query 2

query 3

. . .

one-time���processing

7

Page 8: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Spark Goals

Support iterative and stream jobs

Experiment with programmability » Leverage Scala to integrate cleanly into programs » Support interactive use from Scala interpreter

Retain MapReduce’s fine-grained fault-tolerance

8

Page 9: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Programming Model Driver program »  Implements high-level control flow of an application » Launches various operations in parallel

Distributed datasets » HDFS files, “parallelized” Scala collections » Can be transformed with map and filter » Can be cached across parallel operations

Parallel operations » Foreach, reduce, collect

Shared variables » Accumulators (add-only), Broadcast variables (read-only)

9

Page 10: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Distributed Datasets

Represented by a Scala object and constructed: » From a file in a shared filesystem (e.g., HDFS)

» By “parallelizing” a Scala collection (e.g., an array) in the driver program – dividing it into a number of slices

» By “transforming” an existing Distributed Dataset (e.g., using a user-provided function A è List[B], or flatMap)

10

Page 11: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Parallel Operations

reduce – Combines dataset elements using an associative function to produce a result at the driver program

collect – Sends all elements of the dataset to the driver program (e.g., update an array in parallel with parallelize, map, and collect)

foreach – Passes each element through a user provided function

No grouped reduce operation 11

Page 12: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Shared Variables

Broadcast variables » Used for large read-only data (e.g., lookup table) in

multiple parallel operations – distributed once instead of packaging with every closure

Accumulators » Variables that works can only “add” to using an

associative operation, and only the driver program can read

12

Page 13: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Spark Version of Word Count file = spark.textFile("hdfs://...")

 

file.flatMap(line => line.split(" "))

    .map(word => (word, 1))

    .reduceByKey(_ + _)

13

Page 14: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Example 1:���Logistic Regression

14

Page 15: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Logistic Regression

Goal: find best line separating two sets of points

+

+ +

+

+

+

+ + +

– – –

– –

+

target

random initial line

15

Page 16: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Logistic Regression Implementations

val data = readData(...) var w = Vector.random(D) for (i <- 1 to ITERATIONS) { var gradient = Vector.zeros(D) for (p <- data) { val s = (1/(1+exp(-p.y* (w dot p.x)))-1) * p.y gradient += s * p.x } w -= gradient } println("Final w: " + w)

Serial Version Spark Version

16

val data = spark.hdfsTextFile(...) .map(readPoint _).cache() var w = Vector.random(D) for (i <- 1 to ITERATIONS) { var gradient = spark.accumulator( Vector.zeros(D)) for (p <- data) { val s = (1/(1+exp(-p.y* (w dot p.x)))-1) * p.y gradient += s * p.x } w -= gradient.value } println("Final w: " + w)

Page 17: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Serial Version val data = readData(...) var w = Vector.random(D) for (i <- 1 to ITERATIONS) { var gradient = Vector.zeros(D) for (p <- data) { val scale = (1/(1+exp(-p.y*(w dot p.x))) - 1) * p.y gradient += scale * p.x } w -= gradient } println("Final w: " + w)

17

Page 18: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Spark Version val data = spark.hdfsTextFile(...).map(readPoint).cache() var w = Vector.random(D) for (i <- 1 to ITERATIONS) { var gradient = spark.accumulator(Vector.zeros(D)) for (p <- data) { val scale = (1/(1+exp(-p.y*(w dot p.x))) - 1) * p.y gradient += scale * p.x } w -= gradient.value } println("Final w: " + w)

18

Page 19: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Logistic Regression Implementations

val data = readData(...) var w = Vector.random(D) for (i <- 1 to ITERATIONS) { var gradient = Vector.zeros(D) for (p <- data) { val s = (1/(1+exp(-p.y* (w dot p.x)))-1) * p.y gradient += s * p.x } w -= gradient } println("Final w: " + w)

Serial Version Spark Version

19

val data = spark.hdfsTextFile(...) .map(readPoint _).cache() var w = Vector.random(D) for (i <- 1 to ITERATIONS) { var gradient = spark.accumulator( Vector.zeros(D)) for (p <- data) { val s = (1/(1+exp(-p.y* (w dot p.x)))-1) * p.y gradient += s * p.x } w -= gradient.value } println("Final w: " + w)

Page 20: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Spark Version val data = spark.hdfsTextFile(...).map(readPoint).cache() var w = Vector.random(D) for (i <- 1 to ITERATIONS) { var gradient = spark.accumulator(Vector.zeros(D)) for (p <- data) { val scale = (1/(1+exp(-p.y*(w dot p.x))) - 1) * p.y gradient += scale * p.x } w -= gradient.value } println("Final w: " + w)

20

Page 21: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Logistic Regression Implementations

val data = readData(...) var w = Vector.random(D) for (i <- 1 to ITERATIONS) { var gradient = Vector.zeros(D) for (p <- data) { val s = (1/(1+exp(-p.y* (w dot p.x)))-1) * p.y gradient += s * p.x } w -= gradient } println("Final w: " + w)

Serial Version Spark Version

21

val data = spark.hdfsTextFile(...) .map(readPoint _).cache() var w = Vector.random(D) for (i <- 1 to ITERATIONS) { var gradient = spark.accumulator( Vector.zeros(D)) data.foreach(p => { val s = (1/(1+exp(-p.y* (w dot p.x)))-1) * p.y gradient += s * p.x }) w -= gradient.value } println("Final w: " + w)

Page 22: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Spark Version val data = spark.hdfsTextFile(...).map(readPoint).cache() var w = Vector.random(D) for (i <- 1 to ITERATIONS) { var gradient = spark.accumulator(Vector.zeros(D)) data.foreach(p => { val scale = (1/(1+exp(-p.y*(w dot p.x))) - 1) * p.y gradient += scale * p.x }) w -= gradient.value } println("Final w: " + w)

22

Page 23: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Functional Programming Version

val data = spark.hdfsTextFile(...).map(readPoint).cache() var w = Vector.random(D) for (i <- 1 to ITERATIONS) { w -= data.map(p => { val scale = (1/(1+exp(-p.y*(w dot p.x))) - 1) * p.y scale * p.x }).reduce(_+_) } println("Final w: " + w)

23

Page 24: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Job Execution

Big Dataset

Slave 4 Slave 3 Slave 2 Slave 1

Master

R1 R2 R3 R4

aggregate

update param

param

Spark 24

Page 25: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Job Execution

Slave 4 Slave 3 Slave 2 Slave 1

Master

R1 R2 R3 R4

aggregate

update param

param

Master

aggregate

param

Map 4 Map 3 Map 2 Map 1

Reduce

aggregate

Map 8 Map 7 Map 6 Map 5

Reduce

param

� � � Spark Hadoop / Dryad 25

Page 26: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Performance

0 500

1000 1500 2000 2500 3000 3500 4000 4500

1 5 10 20 30

Run

ning

Tim

e (s

)

Number of Iterations

Hadoop

Spark

127 s / iteration

first iteration 174 s further iterations 6 s

26

Page 27: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Example 2:���Alternating Least Squares

27

Page 28: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Collaborative Filtering

Predict movie ratings for a set of users based on their past ratings

R =

1 ? ? 4 5 ? 3 ? ? 3 5 ? ? 3 5 ? 5 ? ? ? 1 4 ? ? ? ? 2 ?

Movies

Users

28

Page 29: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Matrix Factorization

Model R as product of user and movie matrices A and B of dimensions U×K and M×K

R A =

Problem: given subset of R, optimize A and B

BT

29

Page 30: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Alternating Least Squares Algorithm

Start with random A and B

Repeat:

1.  Fixing B, optimize A to minimize error on scores in R

2.  Fixing A, optimize B to minimize error on scores in R

30

Page 31: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Serial ALS val R = readRatingsMatrix(...) var A = (0 until U).map(i => Vector.random(K)) var B = (0 until M).map(i => Vector.random(K)) for (i <- 1 to ITERATIONS) { A = (0 until U).map(i => updateUser(i, B, R)) B = (0 until M).map(i => updateMovie(i, A, R)) }

31

Page 32: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Naïve Spark ALS val R = readRatingsMatrix(...) var A = (0 until U).map(i => Vector.random(K)) var B = (0 until M).map(i => Vector.random(K)) for (i <- 1 to ITERATIONS) { A = spark.parallelize(0 until U, numSlices) .map(i => updateUser(i, B, R)) .collect() B = spark.parallelize(0 until M, numSlices) .map(i => updateMovie(i, A, R)) .collect() }

Problem:���R re-sent to all nodes in

each parallel

operation

32

Page 33: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Efficient Spark ALS val R = spark.broadcast(readRatingsMatrix(...)) var A = (0 until U).map(i => Vector.random(K)) var B = (0 until M).map(i => Vector.random(K)) for (i <- 1 to ITERATIONS) { A = spark.parallelize(0 until U, numSlices) .map(i => updateUser(i, B, R.value)) .collect() B = spark.parallelize(0 until M, numSlices) .map(i => updateMovie(i, A, R.value)) .collect() }

Solution: mark R as broadcast variable

33

Page 34: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

ALS Performance

0

50

100

150

200

250

300

4 cores (1 node)

12 cores (2 nodes)

20 cores (3 nodes)

28 cores (4 nodes)

36 cores (5 nodes)

60 cores (8 nodes)

Itera

tion

Dur

atio

n (s

)

Cluster Configuration

First Iteration

Subsequent Iterations

34

Page 35: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Subsequent Iteration Breakdown

0

50

100

150

200

250

300

4 cores (1 node)

12 cores (2 nodes)

20 cores (3 nodes)

28 cores (4 nodes)

36 cores (5 nodes)

60 cores (8 nodes)

Tim

e w

ithin

Iter

atio

n (s

)

Computation

Broadcast

36% of iteration spent on broadcast

35

Page 36: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Architecture

Driver program connects to Mesos and schedules tasks

Workers run tasks, report results and variable updates

Data shared with HDFS/NFS

No communication between workers for now

Driver

Workers

HDFS user code,

broadcast vars

tasks, results

Mesos

local cache

36

Page 37: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Challenge

How to design a distributed memory abstraction that is both fault-tolerant and efficient?

Traditional in-memory storage systems replicate data or update logs across nodes è slow! » Network write is 10-100× slower than memory

37

Page 38: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Resilient Distributed Datasets

Each distributed dataset object maintains a lineage that is used to rebuild slices that are lost / fall out of cache

Ex:

errors = textFile(“log”).filter(_.contains(“error”)) .map(_.split(‘\t’)(1)) .cache()

HdfsFile path: hdfs://…

FilteredFile func: contains(...)

MappedFile func: split(…)

CachedFile

HDFS Local cache

getIterator(slice)

38

Page 39: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Resilient Distributed Datasets RDDs provide an interface for coarse-grained transformations (map, group-by, join, …)

Efficient fault recovery using lineage » Log one operation to apply to many elements » Recompute lost partitions of RDD on failure » No cost if nothing fails

Rich enough to capture many models: » Data flow models: MapReduce, Dryad, SQL, … » Specialized models for iterative apps: Pregel, Hama, …

M. Zaharia, et al, Resilient Distributed Datasets: A fault-tolerant abstraction for in-memory cluster computing, NSDI 2012.

39

Page 40: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Interactive Spark

Modified Scala interpreter to allow Spark to be used interactively from the command line

Required two changes: » Modified wrapper code generation so that each “line”

typed has references to objects for its dependencies » Place generated classes in distributed filesystem

Enables in-memory exploration of big data

40

Page 41: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Milestones

2010: Spark open sourced

Feb 2013: Spark Streaming alpha open sourced

Jun 2013: Spark entered Apache Incubator

Aug 2013: Machine Learning library for Spark

41

Page 42: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Frameworks Built on Spark MapReduce

HaLoop » Iterative MapReduce from UC Irvine / U Washington

Pregel on Spark (Bagel) » Graph processing framework from ���

Google based on BSP message-passing ���model

Hive on Spark (Shark) » In progress

42

Page 43: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

43

Page 44: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Related Work Daytona » Supports iteration and ML workloads » Azure-specific integration

DryadLINQ » SQL-like queries integrated in C# programs » Build queries through operations on lazy datasets » Cannot have a dataset persist across queries » No concept of shared variables for broadcast etc

Pig & Hive » Query languages that can call into Java/Python/etc UDFs » No support for caching a dataset across queries

OpenMP » Compiler extension for parallel loops in C++ » Annotate variables as read-only or accumulator above loop » Cluster version exists, but not fault-tolerant

44

Page 45: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Conclusions Spark provides two abstractions that enable iterative jobs and interactive use:

1.  Distributed datasets with controllable persistence, supporting fault-tolerant parallel operations

2.  Shared variables for efficient broadcast and imperative style programming

Language integration achieved using Scala features + some amount of hacking

All this is surprisingly little code (~1600 lines)

http://spark.incubator.apache.org/

45

Page 46: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Apache Mesos and Spark

Conceived of by graduate students

Implemented locally first

Tested and scaled up using Amazon EC2

Released and open sourced to community

You can do the same! 46

Page 47: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Berkeley Data Analytics Stack

Apache Spark

Shark BlinkDB

SQL

HDFS / Hadoop Storage / Tachyon

Apache Mesos / YARN Resource Manager

Spark Streaming

GraphX MLBase

47

http://amplab.cs.berkeley.edu/

Page 48: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

48

Page 49: Spark: A framework for iterative and interactive …laser.inf.ethz.ch/2013/material/joseph/LASER-Joseph-6.pdfSpark: A framework for iterative and interactive ! cluster computing! "

Language Integration

Scala closures are Serializable objects » Serialize on driver, load & run on workers

Not quite enough » Nested closures may reference entire outer scope » May pull in non-Serializable variables not used inside » Solution: bytecode analysis + reflection

Shared variables » Accumulators: serialized form contains ID » Broadcast vars: serialized form is path to HDFS file

49