一、介绍
通过阅读MapReduce论文,在本地实现一个分布式MapReduce。课程提供的代码中需要修改的有三处(mr/coordinator.go, mr/worker.go, mr/rpc.go),其中coordinator.go就是论文中的master角色,rpc.go中定义rpc通信需要的一些结构。
二、实现流程
- coordinator根据用户传进来的inputs(pg-*.txt)生成Nmap个Map任务。(我是一个文件生成一个Map任务,也可自行定义,作业未对Map个数作硬性规定)
- worker启动后,向coordinator请求任务(可能是Map任务或Reduce任务,coordinator根据状态进行调度)
- coordinator分配给worker任务后,进行计时(作业要求10秒),时间到后进行查看任务状态,未完成则重新分配给其他worker。
- worker在完成Map的过程中,会存储中间文件,文件名为"mr-X-Y",其中X代表当前Map任务的ID,Y代表后面处理此文件的Reduce任务(根据ihash(key) % Nreduce 生成),并将文件路径返回给coordinator,同时coordinator更新Map任务状态。
- coordinator在所有Map任务完成后,进行状态切换(从Map阶段->Reduce阶段),生成相应个数Nreduce(用户定义)个Reduce任务。
- 此后,进入Reduce任务阶段,worker向coordinator请求reduce任务,同Map一样也需要计时。
- worker完成reduce任务后,存储最终文件,文件名为"mr-out-Y",其中Y代表当前Reduce任务的ID,并将文件路径返回给coordinator,同时coordinator更新Reduce任务状态。
- 所有任务完成后,coordinator返回到用户代码空间,worker进程使用rpc通信时,失败,自行退出。
注意:因为是采用ihash(key) % Nreduce进行选择的Reduce worker,所以实际生成的Reduce任务个数不一定等于Nreduce参数。
三、相关结构定义
-
-1:TaskWait
-
0:JobOver
-
1:MapPhase
-
2:ReducePhase
const TaskWait, JobOver, MapPhase, ReducePhase = -1, 0, 1, 2
Coordinator
type Coordinator struct {
// Your definitions here.
Phase int // 系统状态
Nreduce int // reduce任务并行度
Nmap int // map任务并行度
MapState map[int]bool // map 任务状态
ReduceState map[int]bool // reduce 任务状态
MapTaskQueue chan MapTask // map 任务队列
ReduceTaskQueue chan ReduceTask // reduce 任务队列
MapOutPaths [][]Response // map完成后存储的

这篇博客详细介绍了如何实现一个本地分布式MapReduce系统,包括coordinator的初始化、任务调度、worker的工作流程以及相关结构定义。作者在Go语言中实现了MapReduce论文中的功能,并在实验过程中遇到的问题进行了探讨。
最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



