MapReduce: Simplied Data Processing on Large Clusters

论文 Jeffrey Dean, Sanjay Ghemawat; active1001 [译]   2013-10-09 21:17  

MapReduce is a programming model and an associated implementation for processing and generating large data sets. Users specify a map function that processes a key/value pair to generate a set of intermediate key/value pairs, and a reduce function that merges all intermediate values associated with the same intermediate key. Many real world tasks are expressible in this model, as shown in the paper.

MapReduce是一个编程模型和处理、生成大数据集的相关实现。用户指定一个 map 函数处理一个key/value对来产生中间的key/value对集,然后再指定一个 reduce 函数合并所有的具有相同中间key的中间value。下面将列举许多可以用这个模型来表示的现实世界的任务。

Programs written in this functional style are automatically parallelized and executed on a large cluster of commodity machines. The run-time system takes care of the details of partitioning the input data, scheduling the program's execution across a set of machines, handling machine failures, and managing the required inter-machine communication. This allows programmers without any experience with parallel and distributed systems to easily utilize the resources of a large distributed system.

以这种函数式风格写的程序能自动的在大规模的普通机器上实现并行化。这个运行时系统关心这些细节:分割输入数据,程序执行在机群上的调度,机器的失败处理,管理机器之间必要的通信。这样就可以让那些没有并行分布式处理系统经验的程序员利用大量分布式系统的资源。

Our implementation of MapReduce runs on a large cluster of commodity machines and is highly scalable: a typical MapReduce computation processes many terabytes of data on thousands of machines. Programmers find the system easy to use: hundreds of MapReduce programs have been implemented and upwards of one thousand MapReduce jobs are executed on Google's clusters every day.

我们的MapReduce实现运行在一个高可扩展的由普通机器组成的大规模集群上,一个典型的MapReduce计算处理几千台机器上的以TB计算的数据。程序员发现这个系统非常好用:已经实现了数以百计的MapReduce程序,每天在Google的集群上都有1000多个MapReduce程序在执行。

1 Introduction

Over the past five years, the authors and many others at Google have implemented hundreds of special-purpose computations that process large amounts of raw data, such as crawled documents, web request logs, etc., to compute various kinds of derived data, such as inverted indices, various representations of the graph structure of web documents, summaries of the number of pages crawled per host, the set of most frequent queries in a given day, etc. Most such computations are conceptually straightforward. However, the input data is usually large and the computations have to be distributed across hundreds or thousands of machines in order to nish in a reasonable amount of time. The issues of how to parallelize the computation, distribute the data, and handle failures conspire to obscure the original simple computation with large amounts of complex code to deal with these issues.

在过去的5年里,作者和Google的许多人已经实现了数以百计的专用的计算来处理大量的原始数据,比如爬取的文档、Web请求日志等等。为了计算各种类型的派生数据,比如倒排索引、Web文档的图结构的各种表示、每个主机上爬行的页面数量的总览、每天次数最多的请求的集合等等。很多这样的计算在概念上很容易理解。然而,输入的数据量通常很大,只有计算被分布在成百上千的机器上才能在可以接受的时间内完成。怎样并行计算,分发数据,处理失败,所有这些问题综合在一起,使得原本很简单的计算,被大量处理这些问题的复杂代码搞得难以理解。

As a reaction to this complexity, we designed a new abstraction that allows us to express the simple computations we were trying to perform but hides the messy details of parallelization, fault-tolerance, data distribution and load balancing in a library. Our abstraction is inspired by the map and reduce primitives present in Lisp and many other functional languages. We realized that most of our computations involved applying a map operation to each logical "record" in our input in order to compute a set of intermediate key/value pairs, and then applying a reduce operation to all the values that shared the same key, in order to combine the derived data appropriately. Our use of a functional model with user-specied map and reduce operations allows us to parallelize large computations easily and to use re-execution as the primary mechanism for fault tolerance.

作为对这个复杂性的回应,我们设计了一个新的抽象模型,能让我们表达要执行的简单计算,而把并行化、容错、数据分布式处理、负载均衡那些杂乱的细节隐藏在一个库里。我们的抽象模型的灵感来自Lisp和许多其他函数式语言的 mapreduce 原语。我们认识到我们的大部分计算都涉及,在输入数据的逻辑“记录”上应用 map 操作,计算出一个中间key/value对集,然后在所有具有相同key的value上应用 reduce 操作,来适当地合并派生的数据。使用具有用户指定的 mapreduce 操作的函数式模型,让我们可以很容易地对大规模计算做并行化和把重执行作为容错的基本机制。

The major contributions of this work are a simple and powerful interface that enables automatic parallelization and distribution of large-scale computations, combined with an implementation of this interface that achieves high performance on large clusters of commodity PCs.

这个工作的主要贡献是为自动并行化和大规模分布式计算设计了一个简单强大的接口,以及这个接口的一个实现,可以在普通PC机的大规模集群上实现高性能计算。

Section 2 describes the basic programming model and gives several examples. Section 3 describes an implementation of the MapReduce interface tailored towards our cluster-based computing environment. Section 4 describes several renements of the programming model that we have found useful. Section 5 has performance measurements of our implementation for a variety of tasks. Section 6 explores the use of MapReduce within Google including our experiences in using it as the basis for a rewrite of our production indexing system. Section 7 discusses related and future work.

第2节描述基本的编程模型,并给出了一些例子。第3节描述满足基于集群的计算环境的MapReduce接口的实现。第4节描述我们觉得编程模型中的一些有用的技巧。第5节是对于各种不同的任务测量的我们的实现的性能。第6节探究在Google内部使用MapReduce作为基础来重写我们的索引系统产品。第7节讨论相关的和未来的工作。

2 Programming Model

The computation takes a set of input key/value pairs, and produces a set of output key/value pairs. The user of the MapReduce library expresses the computation as two functions: Map and Reduce.

计算用一个输入key/value对集,产生一个输出key/value对集。MapReduce库的用户用两个函数表达这个计算:Map和Reduce。

Map, written by the user, takes an input pair and produces a set of intermediate key/value pairs. The MapReduce library groups together all intermediate values associated with the same intermediate key I and passes them to the Reduce function.

用户自定义的Map函数,接受一个输入对,然后产生一个中间key/value对集。MapReduce库把所有具有相同中间key I的中间value聚合在一起,然后把它们传递给Reduce函数。

The Reduce function, also written by the user, accepts an intermediate key I and a set of values for that key. It merges together these values to form a possibly smaller set of values. Typically just zero or one output value is produced per Reduce invocation. The intermediate values are supplied to the user's reduce function via an iterator. This allows us to handle lists of values that are too large to fit in memory.

用户自定义的Reduce函数,接受一个中间key I和相关的一个value集。它合并这些value,形成一个比较小的value集。一般的,每次Reduce调用只产生0或1个输出value。通过一个迭代器把中间value提供给用户自定义的reduce函数。这样可以使我们根据内存来控制value列表的大小。

2.1 Example

Consider the problem of counting the number of occurrences of each word in a large collection of documents. The user would write code similar to the following pseudo-code:

考虑这个问题:计算在一个大的文档集合中每个词出现的次数。用户会写和下面类似的伪代码:

map(String key, String value):
    // key: document name
    // value: document contents
    for each word w in value:
        EmitIntermediate(w, "1");

reduce(String key, Iterator values):
    // key: a word
    // values: a list of counts
    int result = 0;
    for each v in values:
        result += ParseInt(v);
    Emit(AsString(result));

The map function emits each word plus an associated count of occurrences (just '1' in this simple example). The reduce function sums together all counts emitted for a particular word.

map 函数产生每个词和这个词的出现次数(在这个简单的例子里就是1)。 reduce 函数把产生的每一个特定的词的计数加在一起。

In addition, the user writes code to fill in a mapreduce specication object with the names of the input and output files, and optional tuning parameters. The user then invokes the MapReduce function, passing it the specication object. The user's code is linked together with the MapReduce library (implemented in C++). Appendix A contains the full program text for this example.

另外,用户用输入输出文件的名字和可选的调节参数来填充一个mapreduce规范的对象。用户然后调用MapReduce函数,并把规范对象传递给它。用户的代码和MapReduce库链接在一起(用C++实现)。附录A包含这个实例的全部代码。

2.2 Types

Even though the previous pseudo-code is written in terms of string inputs and outputs, conceptually the map and reduce functions supplied by the user have associated types:

即使前面的伪代码写成了字符串输入和输出的形式,但是概念上用户写的map和reduce函数有相关联的类型:

map     (k1,v1)       -> list(k2,v2)
reduce  (k2,list(v2)) -> list(v2)

I.e., the input keys and values are drawn from a different domain than the output keys and values. Furthermore, the intermediate keys and values are from the same domain as the output keys and values.

即输入的key和value和输出的key和value的域不同。此外,中间key和value和输出的key和value的域相同。

Our C++ implementation passes strings to and from the user-defined functions and leaves it to the user code to convert between strings and appropriate types.

我们的C++实现传递字符串来和用户自定义的函数交互,而把字符串和适当类型之间的转换留给用户代码。

2.3 More Examples

Here are a few simple examples of interesting programs that can be easily expressed as MapReduce computations.

这里有一些简单的有趣的程序,可以很容易地用MapReduce计算来表达。

Distributed Grep: The map function emits a line if it matches a supplied pattern. The reduce function is an identity function that just copies the supplied intermediate data to the output.

分布式的Grep: 如果输入行匹配给定的样式,map函数就输出这一行。reduce函数只是把中间数据复制到输出。

Count of URL Access Frequency: The map function processes logs of web page requests and outputs <URL,1>. The reduce function adds together all values for the same URL and emits a <URL,total count> pair.

计算URL访问频率: map函数处理web页面请求的日志,输出 <URL,1> 。reduce函数把相同URL的value都加起来,产生一个 <URL,total count> 对。

Reverse Web-Link Graph: The map function outputs <target,source> pairs for each link to a target URL found in a page named source. The reduce function concatenates the list of all source URLs associated with a given target URL and emits the pair: <target,list(source)>

反向网络链接图: map函数为名为 source 的页面中的每个链接(指向 target )输出 <target,source> 对。reduce函数把关联到给定目标URL的所有源URL的列表连接在一起,产生 <target,list(source)> 对。

Term-Vector per Host: A term vector summarizes the most important words that occur in a document or a set of documents as a list of <word,frequency> pairs. The map function emits a <hostname,term vector> pair for each input document (where the hostname is extracted from the URL of the document). The reduce function is passed all per-document term vectors for a given host. It adds these term vectors together, throwing away infrequent terms, and then emits a final <hostname,term vector> pair.

每个主机的检索词向量: 一个检索词向量用一个 <word,frequency> 的列表表示出现在一个文档或一个文档集中的最重要的词。map函数为每一个输入文档产生一个 <hostname,term vector> 对(主机名来自文档的URL)。reduce函数接收给定主机的所有文档的检索词向量。它把这些检索词向量加在一起,丢弃低频的检索词,然后产生一个最终的 <hostname,term vector> 对。

Inverted Index: The map function parses each document, and emits a sequence of <word,document ID> pairs. The reduce function accepts all pairs for a given word, sorts the corresponding document IDs and emits a <word,list(document ID)> pair. The set of all output pairs forms a simple inverted index. It is easy to augment this computation to keep track of word positions.

倒排索引: map函数分析每个文档,然后产生一个 <word,document ID> 对的序列。reduce函数接受一个给定词的所有对,排序相应的文档ID,并产生一个 <word,list(document ID)> 对。所有输出对的集合形成一个简单的倒排索引。很容易增加跟踪词位置的计算。

Distributed Sort: The map function extracts the key from each record, and emits a <key,record> pair. The reduce function emits all pairs unchanged. This computation depends on the partitioning facilities described in Section 4.1 and the ordering properties described in Section 4.2.

分布式排序: map函数从每个记录提取key,并且产生一个 <key,record> 对。reduce函数不改变任何的对。这个计算依赖分割工具(在4.1节描述)和排序属性(在4.2节描述)。

3 Implementation

Many different implementations of the MapReduce interface are possible. The right choice depends on the environment. For example, one implementation may be suitable for a small shared-memory machine, another for a large NUMA multi-processor, and yet another for an even larger collection of networked machines.

MapReduce接口可能有许多不同的实现。这取决于环境。例如,可能一个实现适合共享内存较小的机器,另一个适合大NUMA的多处理器机器,另一个适合更大的网络机器集群。

This section describes an implementation targeted to the computing environment in wide use at Google: large clusters of commodity PCs connected together with switched Ethernet [4]. In our environment:

本节描述一个在Google广泛使用的计算环境的实现:用交换机连接的普通PC机的大集群。在我们的环境中:

  1. Machines are typically dual-processor x86 processors running Linux, with 2-4 GB of memory per machine.

    使用双核x86处理器,2-4GB内存的机器,运行Linux系统。

  2. Commodity networking hardware is used -- typically either 100 megabits/second or 1 gigabit/second at the machine level, but averaging considerably less in overall bisection bandwidth.

    使用普通的网络硬件,带宽为100Mb或1Gb,但是平均小于全部带宽的一半。

  3. A cluster consists of hundreds or thousands of machines, and therefore machine failures are common.

    一个集群包含成百上千的机器,因此机器故障是常见的。

  4. Storage is provided by inexpensive IDE disks attached directly to individual machines. A distributed file system [8] developed in-house is used to manage the data stored on these disks. The file system uses replication to provide availability and reliability on top of unreliable hardware.

    存储用直接连到每个机器上的廉价IDE硬盘。一个内部发展起来的分布式文件系统被用来管理存储在这些磁盘上的数据。文件系统用复制的方式来在不可靠的硬件上保证可靠性和有效性。

  5. Users submit jobs to a scheduling system. Each job consists of a set of tasks, and is mapped by the scheduler to a set of available machines within a cluster.

    用户提交工作给一个调度系统。每个工作包含一个任务集,并被调度器映射到集群中的一组可用的机器上。

3.1 Execution Overview

The Map invocations are distributed across multiple machines by automatically partitioning the input data into a set of M splits. The input splits can be processed in parallel by different machines. Reduce invocations are distributed by partitioning the intermediate key space into R pieces using a partitioning function (e.g., hash(key) mod R). The number of partitions (R) and the partitioning function are specied by the user.

Map调用通过自动分割输入数据为一个有M个分块的集,被分布到多台机器上。输入的分块能够在不同的机器上被并行处理。Reduce调用通过用分割函数分割中间key,形成R个片(例如,hash(key) mod R),被分布到多台机器上。分块数量(R)和分割函数由用户来指定。

/media/note/2013/10/09/mapreduce/fig1.png

Figure 1: Execution overview

Figure 1 shows the overall ow of a MapReduce operation in our implementation. When the user program calls the MapReduce function, the following sequence of actions occurs (the numbered labels in Figure 1 correspond to the numbers in the list below):

图1显示了我们实现的MapReduce操作的全部流程。当用户的程序调用MapReduce的函数的时候,将发生下面的一系列动作(下面的数字和图1中的数字标签相对应):

  1. The MapReduce library in the user program rst splits the input les into M pieces of typically 16 megabytes to 64 megabytes (MB) per piece (controllable by the user via an optional parameter). It then starts up many copies of the program on a cluster of machines.

    在用户程序里的MapReduce库首先分割输入文件成M个片,每个片的大小一般从16MB到64MB(用户可以通过可选的参数来控制)。然后在机群中开始大量的拷贝程序。

  2. One of the copies of the program is special -- the master. The rest are workers that are assigned work by the master. There are M map tasks and R reduce tasks to assign. The master picks idle workers and assigns each one a map task or a reduce task.

    这些程序拷贝中的一个是master,其他的都是由master分配任务的worker。有M个map任务和R个reduce任务将被分配。master分配一个map任务或reduce任务给一个空闲的worker。

  3. A worker who is assigned a map task reads the contents of the corresponding input split. It parses key/value pairs out of the input data and passes each pair to the user-dened Map function. The intermediate key/value pairs produced by the Map function are buffered in memory.

    一个被分配了map任务的worker读取相关输入分块的内容。它从输入数据中分析出key/value对,然后把key/value对传递给用户自定义的Map函数。由Map函数产生的中间key/value对被缓存在内存中。

  4. Periodically, the buffered pairs are written to local disk, partitioned into R regions by the partitioning function. The locations of these buffered pairs on the local disk are passed back to the master, who is responsible for forwarding these locations to the reduce workers.

    缓存在内存中的key/value对被周期性的写入到本地磁盘上,通过分割函数把它们写入R个区域。在本地磁盘上的缓存对的位置被传送给master,master负责把这些位置传送给reduce worker。

  5. When a reduce worker is notified by the master about these locations, it uses remote procedure calls to read the buffered data from the local disks of the map workers. When a reduce worker has read all intermediate data, it sorts it by the intermediate keys so that all occurrences of the same key are grouped together. The sorting is needed because typically many different keys map to the same reduce task. If the amount of intermediate data is too large to fit in memory, an external sort is used.

    当一个reduce worker得到master的位置通知的时候,它使用远程过程调用来从map worker的磁盘上读取缓存的数据。当reduce worker读取了所有的中间数据后,它通过排序使具有相同key的内容聚合在一起。因为许多不同的key映射到相同的reduce任务,所以排序是必须的。如果中间数据比内存还大,那么还需要一个外部排序。

  6. The reduce worker iterates over the sorted intermediate data and for each unique intermediate key encountered, it passes the key and the corresponding set of intermediate values to the user's Reduce function. The output of the Reduce function is appended to a final output file for this reduce partition.

    reduce worker迭代排过序的中间数据,对于遇到的每一个唯一的中间key,它把key和相关的中间value集传递给用户自定义的reduce函数。reduce函数的输出被添加到这个reduce分割的最终的输出文件中。

  7. When all map tasks and reduce tasks have been completed, the master wakes up the user program. At this point, the MapReduce call in the user program returns back to the user code.

    当所有的map和reduce任务都完成了,master唤醒用户程序。在这个时候,在用户程序里的 MapReduce 调用返回到用户代码。

After successful completion, the output of the mapreduce execution is available in the R output files (one per reduce task, with file names as specified by the user). Typically, users do not need to combine these R output files into one file -- they often pass these files as input to another MapReduce call, or use them from another distributed application that is able to deal with input that is partitioned into multiple files.

在成功完成之后,mapreduce执行的输出存放在R个输出文件中(每一个reduce任务产生一个由用户指定名字的文件)。一般,用户不需要合并这R个输出文件成一个文件--他们经常把这些文件当作一个输入传递给其他的MapReduce调用,或者在可以处理多个分割文件的分布式应用中使用它们。

3.2 Master Data Structures

The master keeps several data structures. For each map task and reduce task, it stores the state (idle, in-progress, or completed), and the identity of the worker machine (for non-idle tasks).

master保持一些数据结构。它为每一个map和reduce任务存储它们的状态(空闲、工作中、完成),和worker机器(非空闲任务的)的标识。

The master is the conduit through which the location of intermediate file regions is propagated from map tasks to reduce tasks. Therefore, for each completed map task, the master stores the locations and sizes of the R intermediate file regions produced by the map task. Updates to this location and size information are received as map tasks are completed. The information is pushed incrementally to workers that have in-progress reduce tasks.

master就像一个管道,通过它,中间文件区域的位置从map任务传递到reduce任务。因此,对于每个完成的map任务,master存储由map任务产生的R个中间文件区域的大小和位置。当map任务完成的时候,位置和大小的更新信息被接受。这些信息被逐步增加的传递给那些正在工作的reduce任务。

3.3 Fault Tolerance

Since the MapReduce library is designed to help process very large amounts of data using hundreds or thousands of machines, the library must tolerate machine failures gracefully.

因为MapReduce库被设计用来使用成百上千的机器来帮助处理非常大规模的数据,所以这个库必须要能很好的处理机器故障。

Worker Failure

The master pings every worker periodically. If no response is received from a worker in a certain amount of time, the master marks the worker as failed. Any map tasks completed by the worker are reset back to their initial idle state, and therefore become eligible for scheduling on other workers. Similarly, any map task or reduce task in progress on a failed worker is also reset to idle and becomes eligible for rescheduling.

master周期性地ping每个worker。如果master在一个确定的时间段内没有收到worker返回的信息,那么它将把这个worker标记成失效。因为每一个由这个失效的worker完成的map任务被重新设置成它初始的空闲状态,所以它可以被安排给其他的worker。同样的,每一个在失败的worker上正在运行的map或reduce任务,也被重新设置成空闲状态,并且将被重新调度。

Completed map tasks are re-executed on a failure because their output is stored on the local disk(s) of the failed machine and is therefore inaccessible. Completed reduce tasks do not need to be re-executed since their output is stored in a global file system.

在一个失败机器上已经完成的map任务将被再次执行,因为它的输出存储在它的磁盘上,所以是不可访问的。已经完成的reduce任务将不会再次执行,因为它的输出存储在全局文件系统中。

When a map task is executed first by worker A and then later executed by worker B (because A failed), all workers executing reduce tasks are notified of the re-execution. Any reduce task that has not already read the data from worker A will read the data from worker B.

当一个map任务首先被worker A执行之后,又被B执行了(因为A失败了),重新执行这个情况被通知给所有执行reduce任务的worker。任何还没有从A读数据的reduce任务将从worker B读取数据。

MapReduce is resilient to large-scale worker failures. For example, during one MapReduce operation, network maintenance on a running cluster was causing groups of 80 machines at a time to become unreachable for several minutes. The MapReduce master simply re-executed the work done by the unreachable worker machines, and continued to make forward progress, eventually completing the MapReduce operation.

MapReduce可以处理大规模worker失败的情况。例如,在一个MapReduce操作期间,在正在运行的机群上进行网络维护引起80台机器在几分钟内不可访问了,MapReduce master只是简单的再次执行已经被不可访问的worker完成的工作,继续执行,最终完成这个MapReduce操作。

Master Failure

It is easy to make the master write periodic checkpoints of the master data structures described above. If the master task dies, a new copy can be started from the last checkpointed state. However, given that there is only a single master, its failure is unlikely; therefore our current implementation aborts the MapReduce computation if the master fails. Clients can check for this condition and retry the MapReduce operation if they desire.

可以很容易的让管理者周期地写入上面描述的数据结构的检查点。如果这个master任务失效了,可以从上次最后一个检查点开始启动另一个master进程。然而,因为只有一个master,所以它的失败是比较麻烦的,因此我们现在的实现是,如果master失败,就中止MapReduce计算。客户可以检查这个状态,并且可以根据需要重新执行MapReduce操作。

Semantics in the Presence of Failures

When the user-supplied map and reduce operators are deterministic functions of their input values, our distributed implementation produces the same output as would have been produced by a non-faulting sequential execution of the entire program.

当用户提供的map和reduce操作对它的输出值是确定的函数时,我们的分布式实现产生和全部程序没有错误的顺序执行一样的相同的输出。

We rely on atomic commits of map and reduce task outputs to achieve this property. Each in-progress task writes its output to private temporary files. A reduce task produces one such file, and a map task produces R such files (one per reduce task). When a map task completes, the worker sends a message to the master and includes the names of the R temporary files in the message. If the master receives a completion message for an already completed map task, it ignores the message. Otherwise, it records the names of R files in a master data structure.

我们依赖对map和reduce任务的输出进行原子提交来满足这个性质。每个工作中的任务把它的输出写到私有临时文件中。一个reduce任务产生一个这样的文件,而一个map任务产生R个这样的文件(一个reduce任务对应一个文件)。当一个map任务完成的时候,worker发送一个消息给master,在这个消息中包含这R个临时文件的名字。如果master从一个已经完成的map任务再次收到一个完成的消息,它将忽略这个消息。否则,它在master的数据结构里记录这R个文件的名字。

When a reduce task completes, the reduce worker atomically renames its temporary output file to the final output file. If the same reduce task is executed on multiple machines, multiple rename calls will be executed for the same final output file. We rely on the atomic rename operation provided by the underlying file system to guarantee that the final file system state contains just the data produced by one execution of the reduce task.

当一个reduce任务完成的时候,这个reduce worker原子的把临时文件重命名成最终的输出文件。如果相同的reduce任务在多个机器上执行,多个重命名调用将被执行,并产生相同的输出文件。我们依赖由底层文件系统提供的原子重命名操作来保证,最终的文件系统状态仅仅包含一个reduce任务产生的数据。

The vast majority of our map and reduce operators are deterministic, and the fact that our semantics are equivalent to a sequential execution in this case makes it very easy for programmers to reason about their program's behavior. When the map and/or reduce operators are non-deterministic, we provide weaker but still reasonable semantics. In the presence of non-deterministic operators, the output of a particular reduce task \(R_1\) is equivalent to the output for \(R_1\) produced by a sequential execution of the non-deterministic program. However, the output for a different reduce task \(R_2\) may correspond to the output for \(R_2\) produced by a different sequential execution of the non-deterministic program.

我们的map和reduce操作大部分都是确定的,并且我们的处理机制等价于一个顺序的执行的这个事实,使得程序员可以很容易的理解程序的行为。当map和/或reduce操作是不确定的时候,我们提供虽然比较弱但是合理的处理机制。当在一个非确定操作的前面,一个reduce任务 \(R_1\) 的输出等价于一个非确定顺序程序执行产生的输出。然而,一个不同的reduce任务 \(R_2\) 的输出也许符合一个不同的非确定顺序程序执行产生的输出。

Consider map task \(M\) and reduce tasks \(R_1\) and \(R_2\) . Let \(e(R_i)\) be the execution of \(R_i\) that committed (there is exactly one such execution). The weaker semantics arise because \(e(R_1)\) may have read the output produced by one execution of \(M\) and \(e(R_2)\) may have read the output produced by a different execution of \(M\) .

考虑map任务 \(M\) 和reduce任务 \(R_1\)\(R_2\) 的情况。我们设定 \(e(R_i)\) 为已经提交的 \(R_i\) 的执行(有且仅有一个这样的执行)。这个比较弱的语义出现,因为 \(e(R_1)\) 也许已经读取了由 \(M\) 的执行产生的输出,而 \(e(R_2)\) 也许已经读取了由 \(M\) 的不同执行产生的输出。

3.4 Locality

Network bandwidth is a relatively scarce resource in our computing environment. We conserve network bandwidth by taking advantage of the fact that the input data (managed by GFS [8]) is stored on the local disks of the machines that make up our cluster. GFS divides each file into 64 MB blocks, and stores several copies of each block (typically 3 copies) on different machines. The MapReduce master takes the location information of the input files into account and attempts to schedule a map task on a machine that contains a replica of the corresponding input data. Failing that, it attempts to schedule a map task near a replica of that task's input data (e.g., on a worker machine that is on the same network switch as the machine containing the data). When running large MapReduce operations on a significant fraction of the workers in a cluster, most input data is read locally and consumes no network bandwidth.

在我们的计算机环境里,网络带宽是一个相当缺乏的资源。我们通过把输入数据(由GFS管理)存储在机器的本地磁盘上来节省网络带宽。GFS把每个文件分成64MB的一些块,然后每个块的几个拷贝存储在不同的机器上(一般是3个拷贝)。MapReduce的master考虑输入文件的位置信息,并且努力在一个包含相关输入数据的机器上安排一个map任务。如果这样做失败了,它尝试在那个任务的输入数据的附近安排一个map任务(例如,分配到一个和包含输入数据块在一个交换机里的worker机器上执行)。当在一个集群中的一部分机器上运行大规模MapReduce操作的时候,大部分输入数据在本地被读取,从而不消耗网络带宽。

3.5 Task Granularity

We subdivide the map phase into \(M\) pieces and the reduce phase into \(R\) pieces, as described above. Ideally, \(M\) and \(R\) should be much larger than the number of worker machines. Having each worker perform many different tasks improves dynamic load balancing, and also speeds up recovery when a worker fails: the many map tasks it has completed can be spread out across all the other worker machines.

像上面描述的那样,我们细分map阶段成 \(M\) 个片,reduce阶段成 \(R\) 个片。 \(M\)\(R\) 应当比worker机器的数量大许多。每个worker执行许多不同的工作来提高动态负载均衡,也可以加速从一个worker失效中的恢复,这个机器上的许多已经完成的map任务可以被分配到所有其他的worker机器上。

There are practical bounds on how large \(M\) and \(R\) can be in our implementation, since the master must make \(O(M + R)\) scheduling decisions and keeps \(O(M * R)\) state in memory as described above. (The constant factors for memory usage are small however: the \(O(M * R)\) piece of the state consists of approximately one byte of data per map task/reduce task pair.)

在我们的实现里, \(M\)\(R\) 的范围是有大小限制的,因为master必须做 \(O(M+R)\) 次调度,并且保存 \(O(M*R)\) 个状态在内存中。(这个因素使用的内存是很少的,在 \(O(M*R)\) 个状态片里,大约每个map任务/reduce任务对使用一个字节的数据)。

Furthermore, \(R\) is often constrained by users because the output of each reduce task ends up in a separate output file. In practice, we tend to choose \(M\) so that each individual task is roughly 16 MB to 64 MB of input data (so that the locality optimization described above is most effective), and we make \(R\) a small multiple of the number of worker machines we expect to use. We often perform MapReduce computations with \(M = 200000\) and \(R = 5000\) , using 2000 worker machines.

此外, \(R\) 经常被用户限制,因为每一个reduce任务最终都是一个独立的输出文件。实际上,我们倾向于选择 \(M\) ,以便每一个单独的任务大概都是16到64MB的输入数据(以便上面描述的位置优化是最有效的),我们把 \(R\) 设置成我们希望使用的worker机器数量的小倍数。我们经常执行MapReduce计算,在 \(M=200000\)\(R=5000\) ,使用2000台worker机器的情况下。

3.6 Backup Tasks

One of the common causes that lengthens the total time taken for a MapReduce operation is a "straggler": a machine that takes an unusually long time to complete one of the last few map or reduce tasks in the computation. Stragglers can arise for a whole host of reasons. For example, a machine with a bad disk may experience frequent correctable errors that slow its read performance from 30 MB/s to 1 MB/s. The cluster scheduling system may have scheduled other tasks on the machine, causing it to execute the MapReduce code more slowly due to competition for CPU, memory, local disk, or network bandwidth. A recent problem we experienced was a bug in machine initialization code that caused processor caches to be disabled: computations on affected machines slowed down by over a factor of one hundred.

一个落后者是延长MapReduce操作时间的原因之一:一个机器花费异乎寻常的长时间来完成最后的一些map或reduce任务中的一个。有很多原因可能产生落后者。例如,一个有坏磁盘的机器经常发生可以纠正的错误,这样就使读性能从30MB/s降低到3MB/s。机群调度系统也许已经安排其他的任务在这个机器上,由于计算要使用CPU、内存、本地磁盘、网络带宽的原因,引起它执行MapReduce代码很慢。我们最近遇到的一个问题是,一个在机器初始化时的Bug引起处理器缓存的失效:在一个被影响的机器上的计算性能有上百倍的影响。

We have a general mechanism to alleviate the problem of stragglers. When a MapReduce operation is close to completion, the master schedules backup executions of the remaining in-progress tasks. The task is marked as completed whenever either the primary or the backup execution completes. We have tuned this mechanism so that it typically increases the computational resources used by the operation by no more than a few percent. We have found that this signicantly reduces the time to complete large MapReduce operations. As an example, the sort program described in Section 5.3 takes 44% longer to complete when the backup task mechanism is disabled.

我们有一个通用的机制来减轻这个落后者的问题。当一个MapReduce操作将要完成的时候,master调度备用进程来执行那些剩下的还在执行的任务。无论是原来的还是备用的执行完成了,工作都被标记成完成。我们已经调整了这个机制,通常只会占用多几个百分点的机器资源。我们发现这可以显著的减少完成大规模MapReduce操作的时间。作为一个例子,将要在5.3节描述的排序程序,在关闭掉备用任务机制的情况下,要比有备用任务机制的情况下多花44%的时间。

4 Renements

Although the basic functionality provided by simply writing Map and Reduce functions is sufficient for most needs, we have found a few extensions useful. These are described in this section.

尽管简单的map和reduce函数的功能对于大多数需求是足够的了,但是我们还开发了一些有用的扩充。这些将在这个部分描述。

4.1 Partitioning Function

The users of MapReduce specify the number of reduce tasks/output files that they desire (R). Data gets partitioned across these tasks using a partitioning function on the intermediate key. A default partitioning function is provided that uses hashing (e.g. "hash(key) mod R"). This tends to result in fairly well-balanced partitions. In some cases, however, it is useful to partition data by some other function of the key. For example, sometimes the output keys are URLs, and we want all entries for a single host to end up in the same output file. To support situations like this, the user of the MapReduce library can provide a special partitioning function. For example, using "hash(Hostname(urlkey)) mod R" as the partitioning function causes all URLs from the same host to end up in the same output file.

MapReduce用户指定reduce任务和reduce任务需要的输出文件的数量。在中间key上使用分割函数,使数据分割后通过这些任务。一个缺省的分割函数使用hash方法(例如,hash(key) mod R)。这个导致非常平衡的分割。有的时候,使用其他的key分割函数来分割数据非常有用。例如,有时候,输出的key是URLs,并且我们希望每个主机的所有条目保持在同一个输出文件中。为了支持像这样的情况,MapReduce库的用户可以提供专门的分割函数。例如,使用 "hash(Hostname(urlkey)) mod R" 作为分割函数,使所有来自同一个主机的URLs保存在同一个输出文件中。

4.2 Ordering Guarantees

We guarantee that within a given partition, the intermediate key/value pairs are processed in increasing key order. This ordering guarantee makes it easy to generate a sorted output file per partition, which is useful when the output file format needs to support efficient random access lookups by key, or users of the output find it convenient to have the data sorted.

我们保证在一个给定的分割里面,中间key/value对以key递增的顺序处理。这个顺序保证可以使每个分割产出一个有序的输出文件,当输出文件的格式需要支持有效率的随机访问key的时候,或者对输出数据集再作排序的时候,就很容易。

4.3 Combiner Function

In some cases, there is significant repetition in the intermediate keys produced by each map task, and the user-specied Reduce function is commutative and associative. A good example of this is the word counting example in Section 2.1. Since word frequencies tend to follow a Zipf distribution, each map task will produce hundreds or thousands of records of the form <the, 1>. All of these counts will be sent over the network to a single reduce task and then added together by the Reduce function to produce one number. We allow the user to specify an optional Combiner function that does partial merging of this data before it is sent over the network.

在某些情况下,允许中间结果key重复会占据相当的比重,并且用户定义的reduce函数满足结合律和交换律。一个很好的例子就是在2.1节的词统计程序。因为词频率倾向于一个Zipf分布,每个map任务将产生成百上千个这样的记录 <the, 1> 。所有的这些计数将通过网络被传输到一个单独的reduce任务,然后由reduce函数加在一起产生一个数字。我们允许用户指定一个可选的combiner函数,先在本地进行合并一下,然后再通过网络发送。

The Combiner function is executed on each machine that performs a map task. Typically the same code is used to implement both the combiner and the reduce functions. The only difference between a reduce function and a combiner function is how the MapReduce library handles the output of the function. The output of a reduce function is written to the final output file. The output of a combiner function is written to an intermediate file that will be sent to a reduce task.

在每一个执行map任务的机器上Combiner函数被执行。一般的,相同的代码被同时用在combiner和reduce函数。在combiner和reduce函数之间唯一的区别是MapReduce库怎样控制函数的输出。reduce函数的输出被保存最终输出文件里。combiner函数的输出被写到中间文件里,然后被发送给reduce任务。

Partial combining significantly speeds up certain classes of MapReduce operations. Appendix A contains an example that uses a combiner.

部分使用combiner可以显著的提高一些MapReduce操作的速度。附录A包含一个使用combiner函数的例子。

4.4 Input and Output Types

The MapReduce library provides support for reading input data in several different formats. For example, "text" mode input treats each line as a key/value pair: the key is the offset in the file and the value is the contents of the line. Another common supported format stores a sequence of key/value pairs sorted by key. Each input type implementation knows how to split itself into meaningful ranges for processing as separate map tasks (e.g. text mode's range splitting ensures that range splits occur only at line boundaries). Users can add support for a new input type by providing an implementation of a simple reader interface, though most users just use one of a small number of predefined input types.

MapReduce库支持以几种不同的格式读取输入数据。例如,文本模式输入把每一行看作是一个key/value对。key是文件的偏移量,value是那一行的内容。其他普通的支持格式以key的顺序存储key/value对序列。每一个输入类型的实现知道怎样把输入分割成对每个单独的map任务来说是有意义的(例如,文本模式的范围分割确保仅仅在每行的边界进行范围分割)。虽然许多用户仅仅使用很少的预定义输入类型的一个,但是用户可以通过提供一个简单的reader接口来支持一个新的输入类型。

A reader does not necessarily need to provide data read from a file. For example, it is easy to define a reader that reads records from a database, or from data structures mapped in memory.

一个reader不必要从文件里读数据。例如,我们可以很容易的定义它从数据库里读记录,或从内存中的数据结构读取。

In a similar fashion, we support a set of output types for producing data in different formats and it is easy for user code to add support for new output types.

4.5 Side-effects

In some cases, users of MapReduce have found it convenient to produce auxiliary files as additional outputs from their map and/or reduce operators. We rely on the application writer to make such side-effects atomic and idempotent. Typically the application writes to a temporary file and atomically renames this file once it has been fully generated.

有的时候,MapReduce的用户发现在map操作和/或reduce操作时产生辅助文件作为一个附加的输出是很方便的。我们依靠应用程序写来使这个副作用成为原子的。一般的,应用程序写一个临时文件,然后一旦这个文件全部生成完,就自动的被重命名。

We do not provide support for atomic two-phase commits of multiple output files produced by a single task. Therefore, tasks that produce multiple output files with cross-file consistency requirements should be deterministic. This restriction has never been an issue in practice.

对于单个任务产生的多个输出文件来说,我们没有提供其上的两阶段提交的原子操作支持。因此,一个产生需要交叉文件连接的多个输出文件的任务,应该使确定性的任务。不过这个限制在实际的工作中并不是一个问题。

4.6 Skipping Bad Records

Sometimes there are bugs in user code that cause the Map or Reduce functions to crash deterministically on certain records. Such bugs prevent a MapReduce operation from completing. The usual course of action is to fix the bug, but sometimes this is not feasible; perhaps the bug is in a third-party library for which source code is unavailable. Also, sometimes it is acceptable to ignore a few records, for example when doing statistical analysis on a large data set. We provide an optional mode of execution where the MapReduce library detects which records cause deterministic crashes and skips these records in order to make forward progress.

有的时候因为用户的代码里有bug,导致在某一个记录上Map或Reduce函数突然崩溃。这样的bug使得MapReduce操作不能完成。虽然一般是修复这个bug,但是有时候这是不现实的;也许这个bug是在源代码不可得到的第三方库里。有的时候也可以忽略一些记录,例如,当在一个大的数据集上进行统计分析。我们提供一个可选的执行模式,在这个模式下,MapReduce库检测那些记录引起的崩溃,然后跳过那些记录,来继续执行程序。

Each worker process installs a signal handler that catches segmentation violations and bus errors. Before invoking a user Map or Reduce operation, the MapReduce library stores the sequence number of the argument in a global variable. If the user code generates a signal, the signal handler sends a "last gasp" UDP packet that contains the sequence number to the MapReduce master. When the master has seen more than one failure on a particular record, it indicates that the record should be skipped when it issues the next re-execution of the corresponding Map or Reduce task.

每个worker程序安装一个信号处理器来获取内存段异常和总线错误。在调用一个用户自定义的map或reduce操作之前,MapReduce库把记录的序列号存储在一个全局变量里。如果用户代码产生一个信号,那个信号处理器就会发送一个包含序号的 "last gasp" UDP包给MapReduce的master。当master不止一次看到同一个记录的时候,它就会指出,当相关的map或reduce任务再次执行的时候,这个记录应当被跳过。

4.7 Local Execution

Debugging problems in Map or Reduce functions can be tricky, since the actual computation happens in a distributed system, often on several thousand machines, with work assignment decisions made dynamically by the master. To help facilitate debugging, profiling, and small-scale testing, we have developed an alternative implementation of the MapReduce library that sequentially executes all of the work for a MapReduce operation on the local machine. Controls are provided to the user so that the computation can be limited to particular map tasks. Users invoke their program with a special flag and can then easily use any debugging or testing tools they find useful (e.g. gdb).

调试在map或reduce函数中的问题是很困难的,因为实际的计算发生在一个分布式的系统中,经常是有一个master动态的分配工作给几千台机器。为了简化调试和测试,我们开发了一个可替换的实现,这个实现在本地执行所有的MapReduce操作。用户可以控制执行,这样计算可以限制到特定的map任务上。用户以一个标志调用他们的程序,然后可以容易的使用他们认为好用的任何调试和测试工具(例如, gdb )。

4.8 Status Information

The master runs an internal HTTP server and exports a set of status pages for human consumption. The status pages show the progress of the computation, such as how many tasks have been completed, how many are in progress, bytes of input, bytes of intermediate data, bytes of output, processing rates, etc. The pages also contain links to the standard error and standard output files generated by each task. The user can use this data to predict how long the computation will take, and whether or not more resources should be added to the computation. These pages can also be used to figure out when the computation is much slower than expected.

master运行一个HTTP服务器,并且可以输出一组状况页来供人们使用。状态页显示计算进度,象多少个任务已经完成,多少个还在运行,输入的字节数,中间数据字节数,输出字节数,处理百分比,等等。这个页也包含到标准错误的链接,和由每个任务产生的标准输出的链接。用户可以根据这些数据预测计算需要花费的时间,和是否需要更多的资源。当计算比预期的要慢很多的时候,这些页面也可以被用来判断是不是这样。

In addition, the top-level status page shows which workers have failed, and which map and reduce tasks they were processing when they failed. This information is useful when attempting to diagnose bugs in the user code.

此外,最上面的状态页显示已经有多少个工作者失败了,和当它们失败的时候,哪个map和reduce任务正在运行。当试图诊断在用户代码里的bug时,这个信息也是有用的。

4.9 Counters

The MapReduce library provides a counter facility to count occurrences of various events. For example, user code may want to count total number of words processed or the number of German documents indexed, etc.

MapReduce库提供一个计数器工具,来计算各种事件的发生次数。例如,用户代码想要计算所有处理的词的个数,或者被索引的德文文档的数量。

To use this facility, user code creates a named counter object and then increments the counter appropriately in the Map and/or Reduce function. For example:

为了使用这个工具,用户代码创建一个命名的计数器对象,然后在map和/或reduce函数里适当的增加计数器。例如:

Counter* uppercase;
uppercase = GetCounter("uppercase");

map(String name, String contents):
    for each word w in contents:
        if (IsCapitalized(w)):
            uppercase->Increment();
        EmitIntermediate(w, "1");

The counter values from individual worker machines are periodically propagated to the master (piggybacked on the ping response). The master aggregates the counter values from successful map and reduce tasks and returns them to the user code when the MapReduce operation is completed. The current counter values are also displayed on the master status page so that a human can watch the progress of the live computation. When aggregating counter values, the master eliminates the effects of duplicate executions of the same map or reduce task to avoid double counting. (Duplicate executions can arise from our use of backup tasks and from re-execution of tasks due to failures.)

来自不同worker机器上的计数器值被周期性的传送给master(在ping回应里)。master把来自成功的map和reduce任务的计数器值加起来,在MapReduce操作完成的时候,把它返回给用户代码。当前计数器的值也被显示在master状态页里,以便人们可以查看实际的计算进度。当计算计数器值的时候消除重复执行的影响,避免数据的累加。(备用任务的使用和由于出错的重新执行,可以产生重复执行)

Some counter values are automatically maintained by the MapReduce library, such as the number of input key/value pairs processed and the number of output key/value pairs produced.

有些计数器值被MapReduce库自动的维护,比如,被处理的输入key/value对的数量,和被产生的输出key/value对的数量。

Users have found the counter facility useful for sanity checking the behavior of MapReduce operations. For example, in some MapReduce operations, the user code may want to ensure that the number of output pairs produced exactly equals the number of input pairs processed, or that the fraction of German documents processed is within some tolerable fraction of the total number of documents processed.

用户发现计数器工具对于检查MapReduce操作的完整性很有用。例如,在一些MapReduce操作中,用户代码也许想要确保输出对的数量完全等于输入对的数量,或者处理过的德文文档的数量是在全部被处理的文档数量中属于合理的范围。

5 Performance

In this section we measure the performance of MapReduce on two computations running on a large cluster of machines. One computation searches through approximately one terabyte of data looking for a particular pattern. The other computation sorts approximately one terabyte of data.

在本节,我们用在一个大型集群上运行的两个计算来衡量MapReduce的性能。一个计算用来在一个大概1TB的数据中查找特定的匹配串。另一个计算排序大概1TB的数据。

These two programs are representative of a large subset of the real programs written by users of MapReduce -- one class of programs shuffles data from one representation to another, and another class extracts a small amount of interesting data from a large data set.

这两个程序代表了MapReduce的用户实现的真实的程序的一个大子集。一类是,把数据从一种表示转化到另一种表示。另一类是,从一个大的数据集中提取少量的关心的数据。

5.1 Cluster Conguration

All of the programs were executed on a cluster that consisted of approximately 1800 machines. Each machine had two 2GHz Intel Xeon processors with Hyper-Threading enabled, 4GB of memory, two 160GB IDE disks, and a gigabit Ethernet link. The machines were arranged in a two-level tree-shaped switched network with approximately 100-200 Gbps of aggregate bandwidth available at the root. All of the machines were in the same hosting facility and therefore the round-trip time between any pair of machines was less than a millisecond.

所有的程序在包含大概1800台机器的机群上执行。机器的配置是:2个2G的Intel Xeon超线程处理器,4GB内存,两个160GB IDE磁盘,一个千兆网卡。这些机器部署在一个两层的树形交换网络中,在根节点上大概有100到200G的带宽。所有这些机器都有相同的部署(对等部署),因此任意两点之间的来回时间小于1毫秒。

Out of the 4GB of memory, approximately 1-1.5GB was reserved by other tasks running on the cluster. The programs were executed on a weekend afternoon, when the CPUs, disks, and network were mostly idle.

在4GB的内存里,大概有1-1.5GB被用来运行在机群中其他的任务。这个程序是在周末的下午开始执行的,这个时候CPU、磁盘、网络基本上是空闲的。

5.2 Grep

The grep program scans through \(10^{10}\) 100-byte records, searching for a relatively rare three-character pattern (the pattern occurs in 92,337 records). The input is split into approximately 64MB pieces (\(M = 15000\) ), and the entire output is placed in one file (\(R = 1\) ).

这个Grep程序扫描大概 \(10^{10}\) 个,每个100字节的记录,查找比较少的3字符的查找串(这个查找串出现在92337个记录中)。输入数据被分割成大概64MB的片( \(M=15000\) ),全部的输出存放在一个文件中( \(R=1\) )。

/media/note/2013/10/09/mapreduce/fig2.png

Figure 2: Data transfer rate over time

Figure 2 shows the progress of the computation over time. The Y-axis shows the rate at which the input data is scanned. The rate gradually picks up as more machines are assigned to this MapReduce computation, and peaks at over 30 GB/s when 1764 workers have been assigned. As the map tasks finish, the rate starts dropping and hits zero about 80 seconds into the computation. The entire computation takes approximately 150 seconds from start to finish. This includes about a minute of startup overhead. The overhead is due to the propagation of the program to all worker machines, and delays interacting with GFS to open the set of 1000 input files and to get the information needed for the locality optimization.

图2显示计算过程随时间变化的情况。Y轴表示输入数据被扫描的速度。随着更多的机群被分配给这个MapReduce计算,速度在逐步的提高,当有1764个worker的时候这个速度达到最高的30GB/s。当map任务完成的时候,速度开始下降,在计算开始后80秒,输入的速度降到0。这个计算持续的时间大概是150秒。这包括了前面大概一分钟的启动时间。启动时间用来把程序传播到所有的机器上,等待GFS打开1000个输入文件,得到必要的位置优化信息。

5.3 Sort

The sort program sorts \(10^{10}\) 100-byte records (approximately 1 terabyte of data). This program is modeled after the TeraSort benchmark [10].

这个sort程序排序 \(10^{10}\) 个记录,每个记录100个字节(大概1TB的数据)。这个程序是模仿TeraSort的。

The sorting program consists of less than 50 lines of user code. A three-line Map function extracts a 10-byte sorting key from a text line and emits the key and the original text line as the intermediate key/value pair. We used a built-in Identity function as the Reduce operator. This functions passes the intermediate key/value pair unchanged as the output key/value pair. The final sorted output is written to a set of 2-way replicated GFS files (i.e., 2 terabytes are written as the output of the program).

这个排序程序只包含不到50行的用户代码。其中有3行map函数用来从文本行提取10字节的排序key,并且产生一个由这个key和原始文本行组成的中间key/value对。我们使用一个内置的Identity函数作为reduce操作。这个函数直接把中间key/value对作为输出的key/value对。最终的排序输出写到一个2路复制的GFS文件中(也就是,程序的输出会写2TB的数据)。

As before, the input data is split into 64MB pieces (\(M = 15000\) ). We partition the sorted output into 4000 files (\(R = 4000\) ). The partitioning function uses the initial bytes of the key to segregate it into one of R pieces.

象以前一样,输入数据被分割成64MB的片( \(M=15000\) )。我们把排序后的输出写到4000个文件中( \(R=4000\) )。分区函数使用key的原始字节来把数据分区到R个小片中。

Our partitioning function for this benchmark has builtin knowledge of the distribution of keys. In a general sorting program, we would add a pre-pass MapReduce operation that would collect a sample of the keys and use the distribution of the sampled keys to compute split points for the final sorting pass.

我们以这个基准的分割函数,知道key的分布情况。在一般的排序程序中,我们会增加一个预处理的MapReduce操作,这个操作用于采样key的情况,并且用这个采样的key的分布情况来计算对最终排序处理的分割点。

/media/note/2013/10/09/mapreduce/fig3.png

Figure 3: Data transfer rates over time for different executions of the sort program

Figure 3 (a) shows the progress of a normal execution of the sort program. The top-left graph shows the rate at which input is read. The rate peaks at about 13 GB/s and dies off fairly quickly since all map tasks finish before 200 seconds have elapsed. Note that the input rate is less than for grep. This is because the sort map tasks spend about half their time and I/O bandwidth writing intermediate output to their local disks. The corresponding intermediate output for grep had negligible size.

图3(a)显示这个排序程序的正常执行情况。左上图显示输入数据的读取速度。这个速度最高到达13GB/s,并且在不到200秒所有map任务完成之后迅速滑落到0。注意到这个输入速度小于Grep。这是因为这个排序map任务花费大概一半的时间和带宽,来把中间数据写到本地硬盘中。而Grep相关的中间数据可以忽略不计。

The middle-left graph shows the rate at which data is sent over the network from the map tasks to the reduce tasks. This shuffling starts as soon as the first map task completes. The first hump in the graph is for the first batch of approximately 1700 reduce tasks (the entire MapReduce was assigned about 1700 machines, and each machine executes at most one reduce task at a time). Roughly 300 seconds into the computation, some of these first batch of reduce tasks finish and we start shuffling data for the remaining reduce tasks. All of the shuffling is done about 600 seconds into the computation.

左中图显示数据通过网络从map任务传输给reduce任务的速度。当第一个map任务完成后,这个排序过程就开始了。图示上的第一个高峰是启动了第一批大概1700个reduce任务(整个MapReduce任务被分配到1700台机器上,每个机器一次只执行一个reduce任务)。大概开始计算后的300秒,第一批reduce任务中的一些完成了,我们开始执行剩下的reduce任务。全部的排序过程持续了大概600秒的时间。

The bottom-left graph shows the rate at which sorted data is written to the final output files by the reduce tasks. There is a delay between the end of the first shuffling period and the start of the writing period because the machines are busy sorting the intermediate data. The writes continue at a rate of about 2-4 GB/s for a while. All of the writes finish about 850 seconds into the computation. Including startup overhead, the entire computation takes 891 seconds. This is similar to the current best reported result of 1057 seconds for the TeraSort benchmark [18].

左下图显示排序后的数据被reduce任务写入最终文件的速度。因为机器忙于排序中间数据,所以在第一个排序阶段的结束和写阶段的开始有一个延迟。写的速度大概是2-4GB/s。大概开始计算后的850秒写过程结束。包括前面的启动过程,全部的计算任务持续的891秒。这个和TeraSort benchmark的最高纪录1057秒差不多。

A few things to note: the input rate is higher than the shuffle rate and the output rate because of our locality optimization -- most data is read from a local disk and bypasses our relatively bandwidth constrained network. The shuffle rate is higher than the output rate because the output phase writes two copies of the sorted data (we make two replicas of the output for reliability and availability reasons). We write two replicas because that is the mechanism for reliability and availability provided by our underlying file system. Network bandwidth requirements for writing data would be reduced if the underlying file system used erasure coding [14] rather than replication.

需要注意的事情是:因为位置优化的原因,很多数据都是从本地磁盘读取的而没有通过我们有限带宽的网络,所以输入速度比排序速度和输出速度都要快。排序速度比输出速度快的原因是输出阶段写两个排序后数据的拷贝(我们写两个副本的原因是为了可靠性和可用性)。我们写两份的原因是因为底层文件系统的可靠性和可用性的要求。如果底层文件系统用类似容错编码的方式,而不采用复制写的方式,在写盘阶段可以降低网络带宽的要求。

5.4 Effect of Backup Tasks

In Figure 3 (b), we show an execution of the sort program with backup tasks disabled. The execution flow is similar to that shown in Figure 3 (a), except that there is a very long tail where hardly any write activity occurs. After 960 seconds, all except 5 of the reduce tasks are completed. However these last few stragglers don't finish until 300 seconds later. The entire computation takes 1283 seconds, an increase of 44% in elapsed time.

在图3(b)中,显示我们不用备用任务的排序程序的执行情况。除了它有一个很长的几乎没有写动作发生的尾巴外,执行流程和图3(a)相似。在960秒后,只有5个reduce任务没有完成。然而,就是这最后几个落后者知道300秒后才完成。全部的计算任务执行了1283秒,多花了44%的时间。

5.5 Machine Failures

In Figure 3 (c), we show an execution of the sort program where we intentionally killed 200 out of 1746 worker processes several minutes into the computation. The underlying cluster scheduler immediately restarted new worker processes on these machines (since only the processes were killed, the machines were still functioning properly).

在图3(c)中,显示我们有意的在排序程序计算过程中停止1746台worker中的200台机器上的程序的情况。底层机群调度者在这些机器上马上重新开始新的worker程序(因为仅仅程序被停止,而机器仍然在正常运行)。

The worker deaths show up as a negative input rate since some previously completed map work disappears (since the corresponding map workers were killed) and needs to be redone. The re-execution of this map work happens relatively quickly. The entire computation finishes in 933 seconds including startup overhead (just an increase of 5% over the normal execution time).

因为已经完成的map工作丢失了(由于相关的map worker被杀掉了),需要重新再计算,所以worker死掉会导致一个负数的输入速率。相关map任务的重新执行很快就重新执行了。整个计算过程在933秒内完成,包括了前边的启动时间(只比正常执行时间多了5%的时间)。

6 Experience

We wrote the first version of the MapReduce library in February of 2003, and made significant enhancements to it in August of 2003, including the locality optimization, dynamic load balancing of task execution across worker machines, etc. Since that time, we have been pleasantly surprised at how broadly applicable the MapReduce library has been for the kinds of problems we work on. It has been used across a wide range of domains within Google, including:

我们在2003年的2月写了MapReduce库的第一个版本,并且在2003年的8月做了显著的增强,包括位置优化,worker机器间任务执行的动态负载均衡,等等。从那个时候起,我们惊奇的发现MapReduce函数库广泛用于我们日常处理的问题。它现在在Google内部各个领域内广泛应用,包括:

  • large-scale machine learning problems,

    大规模机器学习问题,

  • clustering problems for the Google News and Froogle products,

    Google News和Froogle产品的机器问题,

  • extraction of data used to produce reports of popular queries (e.g. Google Zeitgeist),

    提取数据产生一个流行查询的报告(例如,Google Zeitgeist),

  • extraction of properties of web pages for new experiments and products (e.g. extraction of geographical locations from a large corpus of web pages for localized search), and

    为新的试验和产品提取网页的属性(例如,从一个web页的大集合中提取位置信息,用在位置查询),

  • large-scale graph computations.

    大规模的图计算。

/media/note/2013/10/09/mapreduce/fig4.png

Figure 4: MapReduce instances over time

Figure 4 shows the significant growth in the number of separate MapReduce programs checked into our primary source code management system over time, from 0 in early 2003 to almost 900 separate instances as of late September 2004. MapReduce has been so successful because it makes it possible to write a simple program and run it efficiently on a thousand machines in the course of half an hour, greatly speeding up the development and prototyping cycle. Furthermore, it allows programmers who have no experience with distributed and/or parallel systems to exploit large amounts of resources easily.

图4显示了我们主要的源代码管理系统中,随着时间推移,MapReduce程序的显著增加,从2003年早先时候的0个增长到2004年9月份的差不多900个不同的程序。MapReduce之所以这样的成功,是因为他能够在不到半小时时间内写出一个简单的能够应用于上千台机器的大规模并发程序,并且极大的提高了开发和原形设计的周期效率。并且,他可以让一个完全没有分布式和/或并行系统经验的程序员,能够很容易的利用大量的资源。

At the end of each job, the MapReduce library logs statistics about the computational resources used by the job. In Table 1, we show some statistics for a subset of MapReduce jobs run at Google in August 2004.

在每一个任务结束的时候,MapReduce函数库记录使用的计算资源的统计信息。在图1里,我们列出了2004年8月份在Google运行的一些MapReduce的工作的统计信息。

Table 1: MapReduce jobs run in August 2004
Number of jobs 29,423
Average job completion time 634 secs
Machine days used 79,186 days
Input data read 3,288 TB
Intermediate data produced 758 TB
Output data written 193 TB
Average worker machines per job 157
Average worker deaths per job 1.2
Average map tasks per job 3,351
Average reduce tasks per job 55
Unique map implementations 395
Unique reduce implementations 269
Unique map/reduce combinations 426

6.1 Large-Scale Indexing

One of our most significant uses of MapReduce to date has been a complete rewrite of the production indexing system that produces the data structures used for the Google web search service. The indexing system takes as input a large set of documents that have been retrieved by our crawling system, stored as a set of GFS files. The raw contents for these documents are more than 20 terabytes of data. The indexing process runs as a sequence of five to ten MapReduce operations. Using MapReduce (instead of the ad-hoc distributed passes in the prior version of the indexing system) has provided several benefits:

到目前为止,最成功的MapReduce的应用就是重写了Google web搜索服务所使用到的index系统。索引系统处理爬虫系统抓回来的超大量的文档集,这些文档集保存在GFS文件里。这些文档的原始内容的大小,超过了20TB。索引程序是通过一系列的,大概5到10次MapReduce操作来建立索引。通过利用MapReduce(替换掉上一个版本的特别设计的分布处理的索引程序版本)有这样一些好处:

  • The indexing code is simpler, smaller, and easier to understand, because the code that deals with fault tolerance, distribution and parallelization is hidden within the MapReduce library. For example, the size of one phase of the computation dropped from approximately 3800 lines of C++ code to approximately 700 lines when expressed using MapReduce.

    索引的代码简单、量少、容易理解,因为容错、分布式、并行处理都隐藏在MapReduce库中了。例如,当使用MapReduce函数库的时候,计算的代码行数从原来的3800行C++代码一下减少到大概700行代码。

  • The performance of the MapReduce library is good enough that we can keep conceptually unrelated computations separate, instead of mixing them together to avoid extra passes over the data. This makes it easy to change the indexing process. For example, one change that took a few months to make in our old indexing system took only a few days to implement in the new system.

    MapReduce的函数库的性能已经非常好,所以我们可以把概念上不相关的计算步骤分开处理,而不是混在一起以期减少在数据上的处理。这使得改变索引过程很容易。例如,我们对老索引系统的一个小更改可能要好几个月的时间,但是在新系统内,只需要花几天时间就可以了。

  • The indexing process has become much easier to operate, because most of the problems caused by machine failures, slow machines, and networking hiccups are dealt with automatically by the MapReduce library without operator intervention. Furthermore, it is easy to improve the performance of the indexing process by adding new machines to the indexing cluster.

    索引系统的操作更容易了,这是因为机器的失败、速度慢、以及网络失败都已经由MapReduce自己解决了,而不需要操作人员的交互。另外,我们可以简单的通过对索引系统增加机器的方式提高处理性能。

8 Conclusions

The MapReduce programming model has been successfully used at Google for many different purposes. We attribute this success to several reasons. First, the model is easy to use, even for programmers without experience with parallel and distributed systems, since it hides the details of parallelization, fault-tolerance, locality optimization, and load balancing. Second, a large variety of problems are easily expressible as MapReduce computations. For example, MapReduce is used for the generation of data for Google's production web search service, for sorting, for data mining, for machine learning, and many other systems. Third, we have developed an implementation of MapReduce that scales to large clusters of machines comprising thousands of machines. The implementation makes efficient use of these machine resources and therefore is suitable for use on many of the large computational problems encountered at Google.

MapReduce编程模型已经在Google成功的用在不同的目的。我们把这个成功归于以下几个原因:第一,这个模型使用简单,甚至对没有并行和分布式经验的程序员也是如此,因为它隐藏了并行化、容错、位置优化和负载均衡的细节。第二,大量不同的问题可以用MapReduce计算来表达。例如,MapReduce被用来为Google的产品web搜索服务、排序、数据挖掘、机器学习,和其他许多系统产生数据。第三,我们已经在一个好几千台计算机的大型集群上开发实现了这个MapReduce。这个实现使得对于这些机器资源的利用非常简单,因此也适用于解决Google遇到的其他很多需要大量计算的问题。

We have learned several things from this work. First, restricting the programming model makes it easy to parallelize and distribute computations and to make such computations fault-tolerant. Second, network bandwidth is a scarce resource. A number of optimizations in our system are therefore targeted at reducing the amount of data sent across the network: the locality optimization allows us to read data from local disks, and writing a single copy of the intermediate data to local disk saves network bandwidth. Third, redundant execution can be used to reduce the impact of slow machines, and to handle machine failures and data loss.

从这个工作中我们也学习到了一些东西。首先,严格的编程模型使得并行化和分布式计算简单,并且也易于构造这样的容错计算环境。第二,网络带宽是系统的瓶颈。因此在我们的系统中大量的优化目标是减少通过网络发送的数据量,本地优化使用我们从本地磁盘读取数据,并且把中间数据写到本地磁盘,以保留网络带宽。第三,冗余的执行可以用来减少速度慢的机器的影响,和控制机器失效和数据丢失。

Acknowledgements

Josh Levenberg has been instrumental in revising and extending the user-level MapReduce API with a number of new features based on his experience with using MapReduce and other people's suggestions for enhancements. MapReduce reads its input from and writes its output to the Google File System [8]. We would like to thank Mohit Aron, Howard Gobioff, Markus Gutschke, David Kramer, Shun-Tak Leung, and Josh Redstone for their work in developing GFS. We would also like to thank Percy Liang and Olcan Sercinoglu for their work in developing the cluster management system used by MapReduce. Mike Burrows, Wilson Hsieh, Josh Levenberg, Sharon Perl, Rob Pike, and Debby Wallach provided helpful comments on earlier drafts of this paper. The anonymous OSDI reviewers, and our shepherd, Eric Brewer, provided many useful suggestions of areas where the paper could be improved. Finally, we thank all the users of MapReduce within Google's engineering organization for providing helpful feedback, suggestions, and bug reports.

Josh Levenberg校定和扩展了用户级别的MapReduce API,并且结合他的适用经验和其他人的改进建议,增加了很多新的功能。MapReduce从GFS中读取和写入数据。我们要感谢Mohit Aron、Howard Gobioff、Markus Gutschke、David Krame、Shun-Tak Leung和Josh Redstone在开发GFS中的工作。我们还感谢Percy Liang、Olcan Sercinoglu在开发用于MapReduce的集群管理系统的工作。Mike Burrows、Wilson Hsieh、Josh Levenberg、Sharon Perl、RobPike、Debby Wallach为本论文提出了宝贵的意见。OSDI的无名审阅者,以及我们的审核者Eric Brewer,在论文应当如何改进方面给出了有益的意见。最后,我们感谢Google的工程部的所有MapReduce的用户,感谢他们提供了有用的反馈,建议,以及错误报告等等。

References

[1]Andrea C. Arpaci-Dusseau, Remzi H. Arpaci-Dusseau, David E.Culler, Joseph M.Hellerstein, and David A. Patterson. High-performance sorting on networks of workstations. In Proceedings of the 1997 ACM SIGMOD International Conference on Management of Data, Tucson, Arizona, May 1997.
[2]Remzi H. Arpaci-Dusseau, Eric Anderson, Noah Treuhaft, David E. Culler, Joseph M. Hellerstein, David Patterson, and Kathy Yelick. Cluster I/O with River: Making the fast case common. InProceedings ofthe Sixth Workshop on Input/Output in Parallel and Distributed Systems (IOPADS '99), pages 10-22, Atlanta, Georgia, May 1999.
[3]Arash Baratloo, Mehmet Karaul, Zvi Kedem, and Peter Wyckoff. Charlotte: Metacomputing on the web. In Proceedings of the 9th International Conference on Parallel and Distributed Computing Systems, 1996.
[4]Luiz A. Barroso, Jeffrey Dean, and Urs Holzle. Web search for a planet: TheGoogle cluster architecture. IEEE Micro, 23(2):22-28, April 2003.
[5]John Bent, Douglas Thain, Andrea C.Arpaci-Dusseau, Remzi H. Arpaci-Dusseau, and Miron Livny. Explicit control in a batch-aware distributed le system. In Proceedings of the 1st USENIX Symposium on Networked Systems Design and Implementation NSDI, March 2004.
[6]Guy E. Blelloch. Scans as primitive parallel operations. IEEE Transactions on Computers, C-38(11), November 1989.
[7]Armando Fox, Steven D. Gribble, Yatin Chawathe, Eric A. Brewer, and Paul Gauthier. Cluster-based scalable network services. In Proceedings of the 16th ACM Symposium on Operating System Principles, pages 78-91, Saint-Malo, France, 1997.
[8](1, 2, 3) Sanjay Ghemawat, Howard Gobioff, and Shun-Tak Leung. The Google le system. In 19th Symposium on Operating Systems Principles, pages 29-43, Lake George, New York, 2003.
[9]S. Gorlatch. Systematic efcient parallelization of scan and other list homomorphisms. In L. Bouge, P. Fraigniaud, A. Mignotte, and Y. Robert, editors, Euro-Par'96. Parallel Processing, Lecture Notes in Computer Science 1124, pages 401-408. Springer-Verlag, 1996.
[10]Jim Gray. Sort benchmark home page. http://research.microsoft.com/barc/SortBenchmark/.
[11]William Gropp, Ewing Lusk, and Anthony Skjellum. Using MPI: Portable Parallel Programming with the Message-Passing Interface. MIT Press, Cambridge, MA, 1999.
[12]L.Huston,R. Sukthankar, R.Wickremesinghe, M. Satyanarayanan, G.R.Ganger,E.Riedel, and A.Ailamaki. Diamond: A storage architecture for early discard in interactive search. In Proceedings of the 2004 USENIX File and Storage Technologies FASTConference, April 2004.
[13]Richard E. Ladner and Michael J. Fischer. Parallel prex computation. Journal of the ACM, 27(4):831-838, 1980.
[14]Michael O. Rabin. Efcient dispersal of information for security, load balancing and fault tolerance. Journal of the ACM, 36(2):335-348, 1989.
[15]Erik Riedel, Christos Faloutsos, Garth A. Gibson, and David Nagle. Active disks for large-scale data processing. IEEE Computer, pages 68-74, June 2001.
[16]Douglas Thain, Todd Tannenbaum, and Miron Livny. Distributed computing in practice: The Condor experience. Concurrency and Computation: Practice and Experience, 2004.
[17]L. G.Valiant. A bridging modelfor parallel computation. Communications of the ACM, 33(8):103-111, 1997.
[18]Jim Wyllie. Spsort: How to sort a terabyte quickly. http://alme1.almaden.ibm.com/cs/spsort.pdf.

A Word Frequency

This section contains a program that counts the number of occurrences of each unique word in a set of input files specied on the command line.

本节包含了一个完整的程序,用于统计在命令行指定的一组输入文件中,每一个不同的单词出现的频率。

 1 #include "mapreduce/mapreduce.h"
 2 
 3 // User's map function
 4 class WordCounter : public Mapper {
 5 public:
 6     virtual void Map(const MapInput& input) {
 7         const string& text = input.value();
 8         const int n = text.size();
 9         for (int i = 0; i < n; ) {
10             // Skip past leading whitespace
11             while ((i < n) && isspace(text[i]))
12                 i++;
13 
14             // Find word end
15             int start = i;
16             while ((i < n) && !isspace(text[i]))
17                 i++;
18             if (start < i)
19                 Emit(text.substr(start,i-start),"1");
20         }
21     }
22 };
23 REGISTER_MAPPER(WordCounter);
24 
25 // User's reduce function
26 class Adder : public Reducer {
27     virtual void Reduce(ReduceInput* input) {
28         // Iterate over all entries with the
29         // same key and add the values
30         int64 value = 0;
31         while (!input->done()) {
32             value += StringToInt(input->value());
33             input->NextValue();
34         }
35 
36         // Emit sum for input->key()
37         Emit(IntToString(value));
38     }
39 };
40 REGISTER_REDUCER(Adder);
41 
42 int main(int argc, char** argv) {
43     ParseCommandLineFlags(argc, argv);
44 
45     MapReduceSpecification spec;
46 
47     // Store list of input files into "spec"
48     for (int i = 1; i < argc; i++) {
49         MapReduceInput* input = spec.add_input();
50         input->set_format("text");
51         input->set_filepattern(argv[i]);
52         input->set_mapper_class("WordCounter");
53     }
54 
55     // Specify the output files:
56     //      /gfs/test/freq-00000-of-00100
57     //      /gfs/test/freq-00001-of-00100
58     //      ...
59     MapReduceOutput* out = spec.output();
60     out->set_filebase("/gfs/test/freq");
61     out->set_num_tasks(100);
62     out->set_format("text");
63     out->set_reducer_class("Adder");
64 
65     // Optional: do partial sums within map
66     // tasks to save network bandwidth
67     out->set_combiner_class("Adder");
68 
69     // Tuning parameters: use at most 2000
70     // machines and 100 MB of memory per task
71     spec.set_machines(2000);
72     spec.set_map_megabytes(100);
73     spec.set_reduce_megabytes(100);
74 
75     // Now run it
76     MapReduceResult result;
77     if (!MapReduce(spec, &result)) abort();
78 
79     // Done: 'result' structure contains info
80     // about counters, time taken, number of
81     // machines used, etc.
82 
83     return 0;
84 }

http://research.google.com/archive/mapreduce.html

http://www.yeolar.com/note/2013/10/09/mapreduce/

http://www.yeolar.com/note/2013/10/09/mapreduce/