36
Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

  • View
    216

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Map Reduce

Allan JeffersonArmando Gonçalves

Rocir LeiteFilipe??

Page 2: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Single-node architecture

Memory

Disk

CPU

Machine Learning, Statistics

“Classical” Data Mining

Page 3: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Commodity Clusters

• Web data sets can be very large o Tens to hundreds of terabytes

• Cannot mine on a single server (why?)• Standard architecture emerging:

o Cluster of commodity Linux nodeso Gigabit ethernet interconnect

• How to organize computations on this architecture?o Mask issues such as hardware failure

Page 4: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Cluster Architecture

Mem

Disk

CPU

Mem

Disk

CPU

Switch

Each rack contains 16-64 nodes

Mem

Disk

CPU

Mem

Disk

CPU

Switch

Switch1 Gbps between any pair of nodesin a rack

2-10 Gbps backbone between racks

Page 5: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Stable storage

• First order problem: if nodes can fail, how can we store data persistently?

• Answer: Distributed File Systemo Provides global file namespaceo Google GFS; Hadoop HDFS; Kosmix KFS

• Typical usage patterno Huge files (100s of GB to TB)o Data is rarely updated in placeo Reads and appends are common

Page 6: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Distributed File System

• Chunk Serverso File is split into contiguous chunkso Typically each chunk is 16-64MBo Each chunk replicated (usually 2x or 3x)o Try to keep replicas in different racks

• Master nodeo a.k.a. Name Nodes in HDFSo Stores metadatao Might be replicated

• Client library for file accesso Talks to master to find chunk servers o Connects directly to chunkservers to access data

Page 7: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Motivation for MapReduce (why)

• Large-Scale Data Processingo Want to use 1000s of CPUs

But don’t want hassle of managing things

• MapReduce Architecture provideso Automatic parallelization & distributiono Fault toleranceo I/O schedulingo Monitoring & status updates

Page 8: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

What is Map/Reduce

• Map/Reduce o Programming model from LISPo (and other functional languages)

• Many problems can be phrased this way

• Easy to distribute across nodes• Nice retry/failure semantics

Page 9: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Map in Haskell(Scheme)

• (map f list [list2 list3 …])

• (map square ‘(1 2 3 4))o (1 4 9 16)

Unary operator

Page 10: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Warm up: Word Count

• We have a large file of words, one word to a line

• Count the number of times each distinct word appears in the file

• Sample application: analyze web server logs to find popular URLs

Page 11: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Word Count (2)

• Case 1: Entire file fits in memory• Case 2: File too large for mem, but all

<word, count> pairs fit in mem• Case 3: File on disk, too many distinct

words to fit in memoryo sort datafile | uniq –c

Page 12: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Word Count (3)

• To make it slightly harder, suppose we have a large corpus of documents

• Count the number of times each distinct word occurs in the corpus words(docs/*) | sort | uniq -c where words takes a file and outputs the

words in it, one to a line• The above captures the essence of

MapReduceo Great thing is it is naturally parallelizable

Page 13: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

MapReduce

• Input: a set of key/value pairs• User supplies two functions:

o map(k,v) • list(k1,v1)o reduce(k1, list(v1)) • v2

• (k1,v1) is an intermediate key/value pair

• Output is the set of (k1,v2) pairs

Page 14: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Word Count using MapReduce

map(key, value):// key: document name; value: text of documentfor each word w in value:emit(w, 1)

reduce(key, values):// key: a word; value: an iterator over countsresult = 0for each count v in values:result += vemit(key,result)

Page 15: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Count, Illustrated

map(key=url, val=contents): For each word w in contents, emit (w, “1”) reduce(key=word,

values=uniq_counts): Sum all “1”s in values list Emit result “(word, sum)”

see bob run see spot throw

see 1 bob 1 run 1 see 1 spot 1 throw 1

bob 1 run 1 see 2 spot 1 throw 1

Page 16: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Example uses: distributed grep   distributed sort   web link-graph reversal

term-vector / host web access log stats

inverted index construction

document clustering

machine learning statistical machine translation

... ... ...

Model is Widely ApplicableMapReduce Programs In Google Source Tree

Page 17: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Typical cluster:

100s/1000s of 2-CPU x86 machines, 2-4 GB of memory

Limited bisection bandwidth Storage is on local IDE disks GFS: distributed file system manages data

(SOSP'03) Job scheduling system: jobs made up of tasks,

scheduler assigns tasks to machines

Implementation is a C++ library linked into user programs

Implementation Overview

Page 18: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Distributed Execution Overview

UserProgram

Worker

Worker

Master

Worker

Worker

Worker

fork fork fork

assignmap

assignreduce

readlocalwrite

remoteread,sort

OutputFile 0

OutputFile 1

write

Split 0Split 1Split 2

Input Data

Page 19: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Data flow

• Input, final output are stored on a distributed file systemo Scheduler tries to schedule map tasks

“close” to physical storage location of input data

• Intermediate results are stored on local FS of map and reduce workers

• Output is often input to another map reduce task

Page 20: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Coordination

• Master data structureso Task status: (idle, in-progress, completed)o Idle tasks get scheduled as workers become

availableo When a map task completes, it sends the

master the location and sizes of its R intermediate files, one for each reducer

o Master pushes this info to reducers• Master pings workers periodically to

detect failures

Page 21: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Failures

• Map worker failureo Map tasks completed or in-progress at

worker are reset to idleo Reduce workers are notified when task is

rescheduled on another worker• Reduce worker failure

o Only in-progress tasks are reset to idle• Master failure

o MapReduce task is aborted and client is notified

Page 22: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Execution

Page 23: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Parallel Execution

Page 24: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

How many Map and Reduce jobs?

• M map tasks, R reduce tasks• Rule of thumb:

o Make M and R much larger than the number of nodes in cluster

o One DFS chunk per map is commono Improves dynamic load balancing and

speeds recovery from worker failure• Usually R is smaller than M, because

output is spread across R files

Page 25: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Combiners

• Often a map task will produce many pairs of the form (k,v1), (k,v2), … for the same key ko E.g., popular words in Word Count

• Can save network time by pre-aggregating at mappero combine(k1, list(v1)) • v2o Usually same as reduce function

• Works only if reduce function is commutative and associative

Page 26: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Partition Function

• Inputs to map tasks are created by contiguous splits of input file

• For reduce, we need to ensure that records with the same intermediate key end up at the same worker

• System uses a default partition function e.g., hash(key) mod R

• Sometimes useful to override o E.g., hash(hostname(URL)) mod R ensures

URLs from a host end up in the same output file

Page 27: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Execution Summary

• How is this distributed?1.Partition input key/value pairs into

chunks, run map() tasks in parallel2.After all map()s are complete, consolidate

all emitted values for each unique emitted key

3.Now partition space of output map keys, and run reduce() in parallel

• If map() or reduce() fails, reexecute!

Page 28: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Exercise 1: Host size

• Suppose we have a large web corpus• Let’s look at the metadata file

o Lines of the form (URL, size, date, …)• For each host, find the total number of

byteso i.e., the sum of the page sizes for all URLs

from that host

Page 29: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Exercise 2: Distributed Grep

• Find all occurrences of the given pattern in a very large set of files

Page 30: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Grep

• Input consists of (url+offset, single line)• map(key=url+offset, val=line):

o If contents matches regexp, emit (line, “1”)

• reduce(key=line, values=uniq_counts):o Don’t do anything; just emit line

Page 31: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Exercise 3: Graph reversal

• Given a directed graph as an adjacency list:

src1: dest11, dest12, …src2: dest21, dest22, …

• Construct the graph in which all the links are reversed

Page 32: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Reverse Web-Link Graph

• Mapo For each URL linking to target, …o Output <target, source> pairs

• Reduceo Concatenate list of all source URLso Outputs: <target, list (source)> pairs

Page 33: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Exercise 4: Frequent Pairs

• Given a large set of market baskets, find all frequent pairso Remember definitions from Association

Rules lectures

Page 34: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Hadoop

• An open-source implementation of Map Reduce in Javao Uses HDFS for stable storage

• Download from:http://lucene.apache.org/hadoop/

Page 35: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Reading

• Jeffrey Dean and Sanjay Ghemawat,MapReduce: Simplified Data Processing on Large Clustershttp://labs.google.com/papers/mapreduce.html

• Sanjay Ghemawat, Howard Gobioff, and Shun-Tak Leung, The Google File System

http://labs.google.com/papers/gfs.html

Page 36: Map Reduce Allan Jefferson Armando Gonçalves Rocir Leite Filipe??

Conclusions

• MapReduce proven to be useful abstraction

• Greatly simplifies large-scale computations

• Fun to use: o focus on problem, o let library deal w/ messy details