[MIT.6.5840 | lab 1 Mapreduce]

113 阅读3分钟

简介MapReduce

  • Mapreduce is a programming model and an associated implementation for processing and generating large data sets. Mapreduce 是一个模型框架来更好的处理和产生大数据
  • map function that processes a key/value pair to generate a set of intermediate key/value pairs Map 是负责产生中间键值对
  • reduce function merges all intermediate values associated with the same intermediate key Reduce 负责合并相同键的中间值
  • programs wrtitten 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.

Mapreduce 模型

基础内容

  • Map函数:产生中间键值对并传递给Reduce函数
  • Reduce函数:合并所有相同中间键的值,通常Reduce函数产生的每个键只有一个值或没有。
  • 中间键值对被迭代发送给reduce函数(可以处理远大于运存的数据)

流程

  1. 将输入文件分成M份,然后在集群的多个机器上运行同一份代码
  2. 有一个是Master,其它的都是Worker, Master给空闲的worker分配map task或者reduce task
  3. 处理map task的worker会读取输入数据的键值对并传递给用户定义好的Map函数。中间键值对被缓存在内存里
  4. 缓存的数据被定期地写入磁盘,并且它们的位置需要传送回给master。然后master再将这些位置传递给reduce worker
  5. reduce worker使用remote procedure calls来读取这些中间键值对,并将其排序来把相同键的键值对分在一起。如果中间键值对数据太大就采取外部排序。
  6. reduce worker循环处理这些中间键值对并把该键及其一系列值传递到user's Reduce function并输出。
  7. 当全部map和reduce的tasks被完成后,master就换其调用该Mapreduce的用户程序并返回结果。

数据结构

master需要对于每一个map和reduce task存储

  • 状态:空闲(idle)、执行中(in-progress),已完成(completed)
  • 对非空闲的worker machine 进行识别,是map还是reduce
  • 对每个已完成的map task存储中间文件的位置及大小,并且更新这些位置以及大小信息被认为这个map task已经完成了
  • 这些位置及大小信息被传输到in-progress的reduce task

Fault Tolerance

Worker Failure

  • 检测worker是否出错:master定期地向每个worker发送消息,如果没有收到回复就认为该worker失败了。
  • 对于已经完成的map task worker或者在in progress状态中执行失败的map和reduce task会被设置为空闲状态
  • 已经完成某些map task的woker在失败后需要重新执行这些已经完成的任务,因为那些已完成任务的输出存储在本地磁盘
  • 完成某些reduce task的worker后来失败后不需要重新执行,因为它们的输出是在全局文件系统中的
  • 当一个map task从由worker A执行到由worker B执行后,所有reduce work的workers会被通知需要重新执行并从worker B中读取数据。

Master failure

  • 主节点可以定期保存那些worker的数据,然后在失败后从最近的状态执行