1. 理解分布式处理,可以用worker和master之间的RPC通讯方式入手
2. 用户定义了Map和Reduce函数在wc.go文件中,传递给了worker,worker需要保存下这两个函数,用于后续处理,显然,worker还需要一个id编号,这样完成worker结构体的设计
//
// The map function is called once for each file of input. The first
// argument is the name of the input file, and the second is the
// file's complete contents. You should ignore the input file name,
// and look only at the contents argument. The return value is a slice
// of key/value pairs.
//
func Map(filename string, contents string) []mr.KeyValue
//
// The reduce function is called once for each key generated by the
// map tasks, with a list of all the values created for that key by
// any map task.
//
func Reduce(key string, values []string) string
3. worker首先需要像master注册一个id,这个id由master进行分配管理, worker像master请求任务,master分配给worker任务,
4. worker的核心算法是doMap和doReduce两个函数
master分配给worker一个文件名,worker读取这个文件,然后调用Map得到kv数组,之后需要用一个Hash算法,将key-value分成nReduce组。得到一个二维的kv数组。worker需要将这个kv数组用json的格式写成文件,文件名字为mr-HashMapId-ReduceId, 第一个参数为Map(worker的编号),第二个参数为ReduceId的编号。
nReduce对应N个worker,每个worker读取对应的reduceId文件,然后调用Reduce函数,将相同的key合并,得到最终的答案。
5. 完成这一部分工作后,再去完成Master的分配算法。