MIT6.824食用过程

MIT6.824食用过程

Lab1 MapReduce

一、介绍

本实验使用Go语言构建一个mapreduce库,以及一个容错的分布式系统。第一部分完成一个简单的mapreduce程序,第二部分写一个提交到mapreduce workers 的master 并且要能够处理workers 的错误。 库的接口和容错的方法跟mapreduce paper里面描述的类似。

二、环境搭建 vscode&&go

1. 安装golang

2. 安装git

3.安装cntlm

代理工具,用来给git 和 go 配置代理,下载文件。

git config --global http.proxy http:china\\username:password@proxy:8080

git config --global https.proxy https:china\\username:password@proxy:8080

测试: $ git clone https://github.com/dotcloud/docker.git 如果报错ssl certificate。。。

执行 $ git config --global http.sslVerify false

再执行git clone即可

4. 在vscode 里面下载go插件

三、实验说明

map/reduce实现支持两种操作模式:顺序执行、分布式执行。前者是指一次执行一个task,当所有的map task

执行完才轮到reduce task

程序源码在mapreduce 目录下

通过调用master.go来启动job,可以设置顺序执行和分布式执行

程序执行流程如下:

  • 输入包括:输入文件,map函数,reduce函数,reduce task的数量
  • 启动rpc server(master_rpc.go) 然后等待workers 注册 (master.go -> register)
  • schedule.go -> schedule()决定如何分配任务给workers 以及如何处理失败

3.1 第一部分 编写map/reduce 的输入和输出

$ cd src/mapreduce

$ go test -run Sequential

$ go test -v -run Sequential // debug mode (将common.go里面的debugEnabled改为true)

修改src/mapreduce 下的common_map.go 中的doMap() 函数和 common_reduce.go 中的doReduce() 函数

其中doMap() 函数实现:

  • 读取inputfile,根据reduce task 的个数(nReduce) 生成相应个数的中间文件
  • 命名格式为 mrtmp.[jobName]-[mapTasknum]-[reduceTasknum] 比如 mrtmp.test-0-0

duReduce()函数功能:

  • 读取doMap() 生成的中间文件
  • 并且将nMap个文件进行合并 排序 并且输出
  • 输出文件名为 mrtmp.[jobName]-res-[reduceTasknum] 比如: mrtmp.test-res-0

3.2 第二部分 实现MapFunc 和 ReduceFunc

实现src/main/wc.go 中的MapF() 和 ReduceF()

其中MapF(inputfile string, contents string) 功能:

  • inputfile 是输入的文件名 不用考虑

  • contents 为 inputfile 的内容,调用 strings.FieldsFunc 来完成分词功能

    • 代码如下:

    • words := strings.FieldsFunc(contents, func(c rune) bool {
      		return !unicode.IsLetter(c)
      	})
      	var result = make([]mapreduce.KeyValue, 0)
      	for _, w := range words {
      		kv := mapreduce.KeyValue{w, "1"}
      		result = append(result, kv)
      	}
      
    • 示例

    • func main() {
      	f := func(c rune) bool {
      		return !unicode.IsLetter(c) && !unicode.IsNumber(c)
      	}
      	fmt.Printf("Fields are: %q", strings("  foo1;bar2,baz3...", f))
      }
      

ReduceF(key string, values []string)功能:

  • key 代表每一个键的内容

  • values 是key对应的 values列表

  • 实现:需要统计value的总数

  • count := strconv.Itoa(len(values))
    return count
    

3.3 分布式mapreduce任务

schedule() 函数通过读取registerChan 来获取workers集合

posted @ 2019-11-27 18:39  Howardwang  阅读(650)  评论(0编辑  收藏  举报