llm3-mapreduce

download llm3-mapreduce

of 30

Transcript of llm3-mapreduce

  • 7/30/2019 llm3-mapreduce

    1/30

    Distributed Computing Seminar

    MapReduce Theory andImplementation

    Christophe Bisciglia, Aaron Kimball, & Sierra Michels-Slettvet

    Summer 2007

    Except as otherwise noted, the content of this presentation is (c) 2007Google Inc. and licensed under the Creative Commons Attribution 3.0

    License.

  • 7/30/2019 llm3-mapreduce

    2/30

    Outline

    Lisp/ML reviewfunctional programming

    map, fold MapReduce overview

  • 7/30/2019 llm3-mapreduce

    3/30

    Functional Programming Review

    Functional operations do not modify datastructures: They always create new ones

    Original data still exists in unmodified form Data flows are implicit in program design

    Order of operations does not matter

  • 7/30/2019 llm3-mapreduce

    4/30

    Functional Programming Review

    fun foo(l: int list) =

    sum(l) + mul(l) + length(l)

    Order of sum() and mul(), etc does notmatter they do not modify l

  • 7/30/2019 llm3-mapreduce

    5/30

    Functional Updates Do Not Modify

    Structuresfun append(x, lst) =

    let lst' = reverse lst in

    reverse ( x :: lst' )

    The append() function above reverses a list, adds a newelement to the front, and returns all of that, reversed,which appends an item.

    But it never modifies lst!

  • 7/30/2019 llm3-mapreduce

    6/30

    Functions Can Be Used As

    Argumentsfun DoDouble(f, x) = f (f x)

    It does not matter what f does to its

    argument; DoDouble() will do it twice.

    What is the type of this function?

  • 7/30/2019 llm3-mapreduce

    7/30

    Map

    map f lst: (a->b) -> (a list) -> (b list)

    Creates a new list by applying f to each element

    of the input list; returns output in order.

    f f f f f f

  • 7/30/2019 llm3-mapreduce

    8/30

    Fold

    fold f x0lst: ('a*'b->'b)->'b->('a list)->'b

    Moves across a list, applying fto each element

    plus an accumulator. f returns the nextaccumulator value, which is combined with thenext element of the list

    f f f f f returned

    initial

  • 7/30/2019 llm3-mapreduce

    9/30

    fold left vs. fold right

    Order of list elements can be significant

    Fold left moves left-to-right across the list

    Fold right moves from right-to-leftSML Implementation:

    fun foldl f a [] = a

    | foldl f a (x::xs) = foldl f (f(x, a)) xs

    fun foldr f a [] = a| foldr f a (x::xs) = f(x, (foldr f a xs))

  • 7/30/2019 llm3-mapreduce

    10/30

    Example

    fun foo(l: int list) =

    sum(l) + mul(l) + length(l)

    How can we implement this?

  • 7/30/2019 llm3-mapreduce

    11/30

    Example (Solved)

    fun foo(l: int list) =

    sum(l) + mul(l) + length(l)

    fun sum(lst) = foldl (fn (x,a)=>x+a) 0 lst

    fun mul(lst) = foldl (fn (x,a)=>x*a) 1 lst

    fun length(lst) = foldl (fn (x,a)=>1+a) 0 lst

  • 7/30/2019 llm3-mapreduce

    12/30

    A More Complicated Fold Problem

    Given a list of numbers, how can wegenerate a list of partial sums?

    e.g.: [1, 4, 8, 3, 7, 9]

    [0, 1, 5, 13, 16, 23, 32]

  • 7/30/2019 llm3-mapreduce

    13/30

    A More Complicated Map Problem

    Given a list of words, can we: reverse theletters in each word, and reverse the

    whole list, so it all comes out backwards?

    [my, happy, cat] -> [tac, yppah, ym]

  • 7/30/2019 llm3-mapreduce

    14/30

    map Implementation

    This implementation moves left-to-rightacross the list, mapping elements one at atime

    But does it need to?

    fun map f [] = []| map f (x::xs) = (f x) :: (map f xs)

  • 7/30/2019 llm3-mapreduce

    15/30

    Implicit Parallelism In map

    In a purely functional setting, elements of a listbeing computed by map cannot see the effectsof the computations on other elements

    If order of application offto elements in list iscommutative, we can reorder or parallelizeexecution

    This is the secret that MapReduce exploits

  • 7/30/2019 llm3-mapreduce

    16/30

    MapReduce

  • 7/30/2019 llm3-mapreduce

    17/30

    Motivation: Large Scale Data

    Processing Want to process lots of data ( > 1 TB)

    Want to parallelize across

    hundreds/thousands of CPUs Want to make this easy

  • 7/30/2019 llm3-mapreduce

    18/30

    MapReduce

    Automatic parallelization & distribution

    Fault-tolerant

    Provides status and monitoring tools Clean abstraction for programmers

  • 7/30/2019 llm3-mapreduce

    19/30

    Programming Model

    Borrows from functional programming

    Users implement interface of two

    functions:

    map (in_key, in_value) ->

    (out_key, intermediate_value) list

    reduce (out_key, intermediate_value list) ->

    out_value list

  • 7/30/2019 llm3-mapreduce

    20/30

    map

    Records from the data source (lines out offiles, rows of a database, etc) are fed into

    the map function as key*value pairs: e.g.,(filename, line).

    map() produces one or more intermediate

    values along with an output key from theinput.

  • 7/30/2019 llm3-mapreduce

    21/30

    reduce

    After the map phase is over, all theintermediate values for a given output key

    are combined together into a list reduce() combines those intermediate

    values into one or more final values forthat same output key

    (in practice, usually only one final valueper key)

  • 7/30/2019 llm3-mapreduce

    22/30

    Data store 1 Data store nmap

    (key 1,values...)

    (key 2,

    values...)(key 3,

    values...)

    map

    (key 1,values...)

    (key 2,

    values...)(key 3,

    values...)

    Input key*value

    pairs

    Input key*value

    pairs

    == Barrier == : Aggregates intermediate values by output key

    reduce reduce reduce

    key 1,intermediate

    values

    key 2,intermediate

    values

    key 3,intermediate

    values

    final key 1

    values

    final key 2

    values

    final key 3

    values

    ...

  • 7/30/2019 llm3-mapreduce

    23/30

    Parallelism

    map() functions run in parallel, creatingdifferent intermediate values from different

    input data sets reduce() functions also run in parallel,each working on a different output key

    All values are processed independently Bottleneck: reduce phase cant start until

    map phase is completely finished.

  • 7/30/2019 llm3-mapreduce

    24/30

    Example: Count word occurrencesmap(String input_key, String input_value):

    // input_key: document name

    // input_value: document contents

    for each word w in input_value:

    EmitIntermediate(w, "1");

    reduce(String output_key, Iteratorintermediate_values):

    // output_key: a word

    // output_values: a list of counts

    int result = 0;

    for each v in intermediate_values:

    result += ParseInt(v);

    Emit(AsString(result));

  • 7/30/2019 llm3-mapreduce

    25/30

    Example vs. Actual Source Code

    Example is written in pseudo-code

    Actual implementation is in C++, using a

    MapReduce library Bindings for Python and Java exist via

    interfaces

    True code is somewhat more involved(defines how the input key/values aredivided up and accessed, etc.)

  • 7/30/2019 llm3-mapreduce

    26/30

    Locality

    Master program divvies up tasks based onlocation of data: tries to have map() tasks

    on same machine as physical file data, orat least same rack

    map() task inputs are divided into 64 MBblocks: same size as Google File Systemchunks

  • 7/30/2019 llm3-mapreduce

    27/30

    Fault Tolerance

    Master detects worker failuresRe-executes completed & in-progress map()

    tasksRe-executes in-progress reduce() tasks

    Master notices particular input key/valuescause crashes in map(), and skips those

    values on re-execution.Effect: Can work around bugs in third-party

    libraries!

  • 7/30/2019 llm3-mapreduce

    28/30

    Optimizations

    No reduce can start until map is complete:A single slow disk controller can rate-limit the

    whole process Master redundantly executes slow-

    moving map tasks; uses results of firstcopy to finish

    Why is it safe to redundantly execute map tasks? Wouldnt this mess upthe total computation?

  • 7/30/2019 llm3-mapreduce

    29/30

    Optimizations

    Combiner functions can run on samemachine as a mapper

    Causes a mini-reduce phase to occurbefore the real reduce phase, to savebandwidth

    Under what conditions is it sound to use a combiner?

  • 7/30/2019 llm3-mapreduce

    30/30

    MapReduce Conclusions

    MapReduce has proven to be a usefulabstraction

    Greatly simplifies large-scale computations atGoogle

    Functional programming paradigm can beapplied to large-scale applications

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