MIT6.824 MapReduce Lab1 核心思路解讀

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的分配算法。

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章