Data-Intensive Text Processing with MapReduce | Jimmy Lin and Chris Dyer (2013 updated version)
Chapter 2: MapReduce Basics
2.1 Functional Programming Roots
Map and Fold in functional programming:
- Map: can be seen "as a concise way to represent the transformation of a dataset"
- Easily to be made parallel, since a function f is mapped onto each element in isolation; no data locality issues.
- Fold: A sort of aggregation operation
- Limitations on data locality: elements must be "brought together" before function g can be applied in the fold. I.e. it needs two data elements, which might be in different locations.
Dealing with data locality issues in fold:
- Grouping of similar elements, so fold not on each single element
- Given, commutativity and associativity: local aggregation and reordering
Chapter 3: Basic MapReduce Algorithm Design
A main issue is synchronization.
Def. from earlier in the text: "In general, synchronization refers to the mechanisms by which multiple concurrently running processes join up", for example, to share intermediate results or otherwise exchange state information." (32-33).
Intermezzo: "In MapReduce, synchronization is accomplished by a barrier between the map and reduce phases of processing. Intermediate key-value pairs must be grouped by key, which is accomplished by a large distributed sort involving all the nodes that executed map tasks and all the nodes that will execute reduce tasks. This necessarily involves copying intermediate data over the network, and therefore the process is commonly known as shuffle and sort". A MapReduce job with m mappers and r reducers involves up to mr distinct copy operations, since each mapper may have intermediate output going to every reducer."
"Within a single MapReduce job, there is only one opportunity for cluster-wide synchronization—during the shuffle and sort stage where intermediate key-value pairs are copied from the mappers to the reducers and grouped by key. Beyond that, mappers and reducers run in isolation without any mechanisms for direct communication" (39)
Within the Hadoop framework a programmer has little control over many aspects of execution, such as:" where a mapper or reducer runs, or when a mapper or reducer begins or finishes, which input key-value pairs are processed by what mapper, and which intermediate key-value pairs are processed by a specific reducer" (40)
Five ways in which programmes can influence MapReduce execution (40):
1) Complex data structures as keys and values: storing and sharing partial results. 2) User-specified initialization and termination code. 3) State-preservation across multiple input or intermediate keys. 4) Control sort order of intermediate keys to influence input order to the reducer. 5) Partition key space to influence which particular reducer will work on which set of keys.
Many algorithms cannot be run in a single MapReduce job, e.g. because they are iterative. N.B. the WordCount example can count word occurrences in a single pass, because it is not iterative.
Also checking of results is often done by an external program, which can also coordinate MapReduce jobs.
Focus on 1) scalability and 2) efficiency.
3.1 Local Aggregation
Main goal of synchronization is sharing partial results, from producers to consumers. However, this requires transmission of data, but network and disk latencies are relatively very high (FYI Hadoop writes intermediate results to disk).
"Through use of the combiner and by taking advantage of the ability to preserve state across multiple inputs, it is often possible to substantially reduce both the number and size of key-value pairs that need to be shuffled from the mappers to the reducers." (45)
Combiners / In-Mapper Combining
Combiners: a "mini-reducer".
E.g. word counting. Emit a count for each unique term i.e. locally aggregate counts per unique term before transmitting to the reducer. So shuffle amount reduced from total number of terms to unique number of terms.
In-mapper combining and state-preservation: tally counts across documents e.g. by only making a single associative array shared by all the map jobs (normally a new object is created per map job), but object creation is expensive.
Arguments for local aggregation:
1) More control: Local aggregation is the mappers is sometimes a must because the Hadoop framework does not make any guarantees about whether the reducer job will run at all. But some problems necessarily require this. 2) More efficient than combiner. In-mapper combining does not only reduce data into the shuffle phase, but also reduces the number of key-value pairs emitted by the mappers. * Or said differently: in any case less data goes into the shuffle and sort phase. However, by using a combiner after a mapper, you do not in any way reduce the amount of key-value pairs that mapper emits. * This is relevant for efficiency because key-value pair instantiation and destruction/garbage collection has a cost and also need to temporarily be stored in memory.
Drawbacks:
1) State preservation breaks pure functional programming style. Efficiency > purity. * But introduces possible order effects. 2) Memory bottleneck: storing of intermediate results. * Define block size: process n key-value pairs, then emit partial results. * Flush: monitor memory usage and flush when memory is full * Difficulty: size must be determined empirically, and this can be particularly difficult for concurrent processes all competing for memory.
When to aggregate?
- Many values per key
- When having a highly-skewed distribution of values with intermediate keys, e.g. the frequent word 'the' has a huge count, whereas some rare word may have one. Without local aggregation this would give one reducer a huge amount of work relative to another. Reduce straggler problem.
P.S. a reducer with too much work is called a "straggler".
Algorithmic correctness
The correctness of the algorithm may never depend on the combiners, as their application is optional.
Combiners and reducers are not interchangeable by definition.
The reducer may be used unmodified as a combiner iff* the reduce computation is both commutative and associative. Otherwise not.
E.g. imagine: compute mean session length per-user from a website log.
Key: user. Value: session length.