1. 简介
在这个 Lab 中,我们将建立一个 MapReduce 库,作为使用 GO 编程和构建一个分布式容错系统的初步。在第一个部分,我们将会写一个简单的 MapReduce 程序。在第二个部分,我们将会写一个 Master,去向 workers 分发任务已经 handle workers 的错误。
2. Software
2.1. Go 环境
官方下载 go1.9 源码golang.org
sodu tar -C /usr/local -xzf go1.9.linux-386.tar.gz
Add /usr/local/go/bin
to the PATH environment variable. You can do this by adding this line to your /etc/profile (for a system-wide installation) or $HOME/.profile:
sudo vim ~/.profile
// 此条语句加入到文件中
export PATH=$PATH:/usr/local/go/bin
// 保存退出后执行,使修改生效
source $HOME/.profile
2.2. 6.824 Code
$ git clone git://g.csail.mit.edu/6.824-golabs-2018 6.824
$ cd 6.824
$ ls
Makefile src
当前的Map/Reduce
实现支持两种操作模式,顺序和分布式。 在前者中,map和reduce每次执行一个任务:首先,第一个map任务执行完成,然后是第二个,然后是第三个,等等。当所有map任务完成后,第一个reduce任务运行,然后是第二个,等等。这种模式虽然不是很快,但对于调试很有用。 分布式模式运行许多 worker 线程,这些线程首先并行执行映射任务,然后执行 reduce 任务。 这要快得多,但也难以实现和调试。
3. 序言:熟悉源代码
Over the course of the following exercises, you will have to write/modify doMap
, doReduce
, and schedule
yourself. These are located in common_map.go
, common_reduce.go
, and schedule.go
respectively. You will also have to write the map
andreduce
functions in ../main/wc.go
.
4. Part I: Map/Reduce input and output
现有的代码缺少了两个关键部分:划分 map 任务输出的函数以及为 reduce 收集所有输入的函数。这两个任务分别在doMap()
function in common_map.go
, and the doReduce()
function in common_reduce.go
中执行。
-
Go 文件读写,引入
os
包,其包含常用的文件操作。 -
hash.fnv
FNV算法属于非密码学哈希函数,它最初由Glenn Fowler和Kiem-Phong Vo于1991年在IEEE POSIX P1003.2上首先提出,最后由Landon Curt Noll 完善,故该算法以三人姓的首字母命名。
FNV算法目前有三种,分别是FNV-1,FNV-1a和FNV-0,但是FNV-0算法已经被丢弃了。FNV算法的哈希结果有32、64、128、256、512和1024位等长度。
4.1. 实现
common_map.go
文件中的doMap()
函数工作流程
- 打开输入到 map task 文件;
- 以文件大小
make
一个byte slice,再读取文件内容; - 将文件 data 传入需要用户编写的
mapF(filename string, contents string)
函数中,其函数返回 slice KeyValu