大规模数据处理 / 云计算 lecture 3 – mapreduce basics 闫宏飞...
TRANSCRIPT
![Page 1: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/1.jpg)
大规模数据处理 /云计算 Lecture 3 – MapReduce Basics
闫宏飞北京大学信息科学技术学院
7/12/2011http://net.pku.edu.cn/~course/cs402/
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United StatesSee http://creativecommons.org/licenses/by-nc-sa/3.0/us/ for details
Jimmy LinUniversity of Maryland 课程建设 SEWMGroup
![Page 2: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/2.jpg)
How do we scale up?
![Page 3: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/3.jpg)
Source: Wikipedia (IBM Roadrunner)
![Page 4: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/4.jpg)
Divide and Conquer
“Work”
w1 w2 w3
r1 r2 r3
“Result”
“worker” “worker” “worker”
Partition
Combine
![Page 5: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/5.jpg)
Parallelization Challenges
• How do we assign work units to workers?• What if we have more work units than workers?• What if workers need to share partial results?• How do we aggregate partial results?• How do we know all the workers have finished?• What if workers die?
What is the common theme of all of these problems?
![Page 6: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/6.jpg)
Common Theme?
• Parallelization problems arise from:– Communication between workers (e.g., to
exchange state)– Access to shared resources (e.g., data)
• Thus, we need a synchronization mechanism
![Page 7: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/7.jpg)
Source: Ricardo Guimarães Herrmann
![Page 8: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/8.jpg)
Managing Multiple Workers• Difficult because
– We don’t know the order in which workers run– We don’t know when workers interrupt each other– We don’t know the order in which workers access shared data
• Thus, we need:– Semaphores (lock, unlock)– Conditional variables (wait, notify, broadcast)– Barriers
• Still, lots of problems:– Deadlock, livelock, race conditions...– Dining philosophers, sleepy barbers, cigarette smokers...
• Moral of the story: be careful!
![Page 9: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/9.jpg)
Current Tools• Programming models
– Shared memory (pthreads)
– Message passing (MPI)
• Design Patterns– Master-slaves
– Producer-consumer flows
– Shared work queues
Message Passing
P1 P2 P3 P4 P5
Shared Memory
P1 P2 P3 P4 P5
Me
mo
ry
master
slaves
producer consumer
producer consumer
work queue
![Page 10: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/10.jpg)
Where the rubber meets the road
• Concurrency is difficult to reason about• Concurrency is even more difficult to reason about
– At the scale of datacenters (even across datacenters)– In the presence of failures– In terms of multiple interacting services
• Not to mention debugging…• The reality:
– Lots of one-off solutions, custom code– Write you own dedicated library, then program with it– Burden on the programmer to explicitly manage everything
![Page 11: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/11.jpg)
Source: Wikipedia (Flat Tire)
![Page 12: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/12.jpg)
Source: MIT Open Courseware
![Page 13: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/13.jpg)
Source: MIT Open Courseware
![Page 14: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/14.jpg)
Source: Harper’s (Feb, 2008)
![Page 15: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/15.jpg)
What’s the point?
• It’s all about the right level of abstraction– The von Neumann architecture has served us well, but is no
longer appropriate for the multi-core/cluster environment
• Hide system-level details from the developers– No more race conditions, lock contention, etc.
• Separating the what from how– Developer specifies the computation that needs to be performed– Execution framework (“runtime”) handles actual execution
The datacenter is the computer!
![Page 16: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/16.jpg)
“Big Ideas”
• Scale “out”, not “up”– Limits of SMP and large shared-memory machines
• Move processing to the data– Cluster have limited bandwidth
• Process data sequentially, avoid random access– Seeks are expensive, disk throughput is reasonable
• Seamless scalability– From the mythical man-month to the tradable
machine-hour
![Page 17: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/17.jpg)
MapReduce
![Page 18: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/18.jpg)
g g g g g
f f f f fMap
Fold
Roots in Functional Programming
![Page 19: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/19.jpg)
Typical Large-Data Problem Iterate over a large number of records
Extract something of interest from each
Shuffle and sort intermediate results
Aggregate intermediate results
Generate final output
Key idea: provide a functional abstraction for these two operations
Map
Reduce
(Dean and Ghemawat, OSDI 2004)
19
![Page 20: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/20.jpg)
MapReduce Programmers specify two functions:
map (k, v) → <k’, v’>*reduce (k’, v’) → <k’ ’, v’ ’>* All values with the same key are sent to the same reducer
The execution framework handles everything else…
20
![Page 21: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/21.jpg)
mapmap map map
Shuffle and Sort: aggregate values by keys
reduce reduce reduce
k1 k2 k3 k4 k5 k6v1 v2 v3 v4 v5 v6
ba 1 2 c c3 6 a c5 2 b c7 8
a 1 5 b 2 7 c 2 3 6 8
r1 s1 r2 s2 r3 s3
21
![Page 22: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/22.jpg)
MapReduce Programmers specify two functions:
map (k, v) → <k’, v’>*reduce (k’, v’) → <k’, v’>* All values with the same key are sent to the same reducer
The execution framework handles everything else…
What’s “everything else”?
22
![Page 23: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/23.jpg)
MapReduce “Runtime” Handles scheduling
Assigns workers to map and reduce tasks
Handles “data distribution” Moves processes to data
Handles synchronization Gathers, sorts, and shuffles intermediate data
Handles errors and faults Detects worker failures and restarts
Everything happens on top of a distributed FS (later)
23
![Page 24: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/24.jpg)
MapReduce Programmers specify two functions:
map (k, v) → <k’, v’>*reduce (k’, v’) → <k’, v’>* All values with the same key are reduced together
The execution framework handles everything else…
Not quite…usually, programmers also specify:partition (k’, number of partitions) → partition for k’ Often a simple hash of the key, e.g., hash(k’) mod n Divides up key space for parallel reduce operationscombine (k’, v’) → <k’, v’>* Mini-reducers that run in memory after the map phase Used as an optimization to reduce network traffic
24
![Page 25: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/25.jpg)
combinecombine combine combine
ba 1 2 c 9 a c5 2 b c7 8
partition partition partition partition
mapmap map map
k1 k2 k3 k4 k5 k6v1 v2 v3 v4 v5 v6
ba 1 2 c c3 6 a c5 2 b c7 8
Shuffle and Sort: aggregate values by keys
reduce reduce reduce
a 1 5 b 2 7 c 2 9 8
r1 s1 r2 s2 r3 s3
c 2 3 6 8
25
![Page 26: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/26.jpg)
Two more details… Barrier between map and reduce phases
But we can begin copying intermediate data earlier
Keys arrive at each reducer in sorted order No enforced ordering across reducers
26
![Page 27: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/27.jpg)
“Hello World”: Word Count
Map(String docid, String text): for each word w in text: Emit(w, 1);
Reduce(String term, Iterator<Int> values): int sum = 0; for each v in values: sum += v; Emit(term, sum);
27
![Page 28: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/28.jpg)
MapReduce can refer to… The programming model
The execution framework (aka “runtime”)
The specific implementation
Usage is usually clear from context!
28
![Page 29: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/29.jpg)
MapReduce Implementations Google has a proprietary implementation in C++
Bindings in Java, Python
Hadoop is an open-source implementation in Java An Apache project Large contribution of development led by Yahoo, used in
production Rapidly expanding software ecosystem
Lots of custom research implementations For GPUs, cell processors, etc.
29
![Page 30: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/30.jpg)
split 0
split 1
split 2
split 3
split 4
worker
worker
worker
worker
worker
Master
UserProgram
outputfile 0
outputfile 1
(1) submit
(2) schedule map (2) schedule reduce
(3) read(4) local write
(5) remote read(6) write
Inputfiles
Mapphase
Intermediate files(on local disk)
Reducephase
Outputfiles
Adapted from (Dean and Ghemawat, OSDI 2004)
30
![Page 31: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/31.jpg)
How do we get data to the workers?
Compute Nodes
NAS
SAN
What’s the problem here?
31
![Page 32: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/32.jpg)
Distributed File System Don’t move data to workers… move workers to the data!
Store data on the local disks of nodes in the cluster Start up the workers on the node that has the data local
Why? Not enough RAM to hold all the data in memory Disk access is slow, but disk throughput is reasonable
A distributed file system is the answer GFS (Google File System) for Google’s MapReduce HDFS (Hadoop Distributed File System) for Hadoop
32
![Page 33: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/33.jpg)
GFS: Assumptions Commodity hardware over “exotic” hardware
Scale “out”, not “up”
High component failure rates Inexpensive commodity components fail all the time
“Modest” number of huge files Multi-gigabyte files are common, if not encouraged
Files are write-once, mostly appended to Perhaps concurrently
Large streaming reads over random access High sustained throughput over low latency
GFS slides adapted from material by (Ghemawat et al., SOSP 2003)
33
![Page 34: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/34.jpg)
GFS: Design Decisions Files stored as chunks
Fixed size (64MB)
Reliability through replication Each chunk replicated across 3+ chunkservers
Single master to coordinate access, keep metadata Simple centralized management
No data caching Little benefit due to large datasets, streaming reads
Simplify the API Push some of the issues onto the client (e.g., data layout)
HDFS = GFS clone (same basic ideas)34
![Page 35: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/35.jpg)
From GFS to HDFS Terminology differences:
GFS master = Hadoop namenode GFS chunkservers = Hadoop datanodes
Functional differences: No file appends in HDFS (planned feature) HDFS performance is (likely) slower
For the most part, we’ll use the Hadoop terminology…35
![Page 36: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/36.jpg)
Adapted from (Ghemawat et al., SOSP 2003)
(file name, block id)
(block id, block location)
instructions to datanode
datanode state(block id, byte range)
block data
HDFS namenode
HDFS datanode
Linux file system
…
HDFS datanode
Linux file system
…
File namespace/foo/bar
block 3df2block 3df2
Application
HDFS Client
HDFS Architecture
36
![Page 37: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/37.jpg)
Namenode Responsibilities Managing the file system namespace:
Holds file/directory structure, metadata, file-to-block mapping, access permissions, etc.
Coordinating file operations: Directs clients to datanodes for reads and writes No data is moved through the namenode
Maintaining overall health: Periodic communication with the datanodes Block re-replication and rebalancing Garbage collection
37
![Page 38: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/38.jpg)
Putting everything together…
datanode daemon
Linux file system
…
tasktracker
slave node
datanode daemon
Linux file system
…
tasktracker
slave node
datanode daemon
Linux file system
…
tasktracker
slave node
namenode
namenode daemon
job submission node
jobtracker
38
![Page 39: 大规模数据处理 / 云计算 Lecture 3 – MapReduce Basics 闫宏飞 北京大学信息科学技术学院 7/12/2011 course/cs402/ This work is licensed under a Creative Commons](https://reader034.vdocuments.mx/reader034/viewer/2022050707/56649ef25503460f94c04aa5/html5/thumbnails/39.jpg)
References [Lin]Ch2:Mapreduce Basic
[Tom]Ch6:How mapreduce works
2003 "The Google file system," in sosp. Bolton Landing, NY, USA: ACM Press, 2003.
2004 "MapReduce: Simplified Data Processing on Large Clusters," in osdi, 2004,
39