Part IV: Handling worker failures
任务描述
处理worker失败的情况。
也就是rpc调用call
函数可能因为超时会返回false。
解决方案
当worker fail后,需要把任务调度给另外一个任务,可以用数组存所有的task。
每次从数组中取任务,失败后又添加回数组中。
但这样还会有一个问题
那就是有可能正常worker完成任务后退出了,所以要确保所有任务都完成,才能退出。
建立一个字段finishTaskCnt
来记录已经完成的任务数,只有所有任务完成后,worker才能退出,否则一直等待获取新任务。上一章完成的代码,只要任务分配完了,worker做完就能退出。现在必须保证所有任务确实做完了,才能退出。
源代码
func schedule(jobName string, mapFiles []string, nReduce int, phase jobPhase, registerChan chan string) {
var ntasks int
var n_other int // number of inputs (for reduce) or outputs (for map)
switch phase {
case mapPhase:
ntasks = len(mapFiles)
n_other = nReduce
case reducePhase:
ntasks = nReduce
n_other = len(mapFiles)
}
fmt.Printf("Schedule: %v %v tasks (%d I/Os)\n", ntasks, phase, n_other)