PartIV
实现 处理worker 失败情况。
worker 处理失败,master 应该重新分配该任务给其他的worker 处理。
rpc 失败情况复杂,可能worker 结果回应丢失了,也有可能 worker还在处理,但是
master rpc 超时了。考虑两个worker都拿到相同的task 且都有结果文件的情况,
要确保输出任务的原子性。当前这个程序框架无法保证任务输出的原子性,只考虑失败的worker没任何输出。
测试命令
$ go test -run Failure
master@master:~/study/6.824/src/mapreduce$
master@master:~/study/6.824/src/mapreduce$ go test -run Failure
2019/03/24 08:59:25 rpc.Register: method "CleanupFiles" has 1 input parameters; needs exactly three
2019/03/24 08:59:25 rpc.Register: method "Lock" has 1 input parameters; needs exactly three
2019/03/24 08:59:25 rpc.Register: method "Unlock" has 1 input parameters; needs exactly three
2019/03/24 08:59:25 rpc.Register: method "Wait" has 1 input parameters; needs exactly three
2019/03/24 08:59:25 rpc.Register: method "Lock" has 1 input parameters; needs exactly three
2019/03/24 08:59:25 rpc.Register: method "Unlock" has 1 input parameters; needs exactly three
/var/tmp/824-1000/mr2499-master: Starting Map/Reduce task test
2019/03/24 08:59:25 rpc.Register: method "Lock" has 1 input parameters; needs exactly three
2019/03/24 08:59:25 rpc.Register: method "Unlock" has 1 input parameters; needs exactly three
Schedule: 20 mapPhase tasks (10 I/Os)
/var/tmp/824-1000/mr2499-worker1: given mapPhase task #0 on file 824-mrinput-0.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker0: given mapPhase task #1 on file 824-mrinput-1.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker0: mapPhase task #1 done
/var/tmp/824-1000/mr2499-worker1: mapPhase task #0 done
/var/tmp/824-1000/mr2499-worker1: given mapPhase task #3 on file 824-mrinput-3.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker0: given mapPhase task #2 on file 824-mrinput-2.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker0: mapPhase task #2 done
/var/tmp/824-1000/mr2499-worker0: given mapPhase task #4 on file 824-mrinput-4.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker1: mapPhase task #3 done
/var/tmp/824-1000/mr2499-worker1: given mapPhase task #5 on file 824-mrinput-5.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker1: mapPhase task #5 done
/var/tmp/824-1000/mr2499-worker1: given mapPhase task #6 on file 824-mrinput-6.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker0: mapPhase task #4 done
/var/tmp/824-1000/mr2499-worker0: given mapPhase task #7 on file 824-mrinput-7.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker1: mapPhase task #6 done
/var/tmp/824-1000/mr2499-worker1: given mapPhase task #8 on file 824-mrinput-8.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker0: mapPhase task #7 done
/var/tmp/824-1000/mr2499-worker0: given mapPhase task #9 on file 824-mrinput-9.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker0: mapPhase task #9 done
/var/tmp/824-1000/mr2499-worker0: given mapPhase task #10 on file 824-mrinput-10.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker1: mapPhase task #8 done
/var/tmp/824-1000/mr2499-worker1: given mapPhase task #11 on file 824-mrinput-11.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker0: mapPhase task #10 done
/var/tmp/824-1000/mr2499-worker0: given mapPhase task #12 on file 824-mrinput-12.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker1: mapPhase task #11 done
/var/tmp/824-1000/mr2499-worker1: given mapPhase task #13 on file 824-mrinput-13.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker0: mapPhase task #12 done
/var/tmp/824-1000/mr2499-worker0: given mapPhase task #14 on file 824-mrinput-14.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker1: mapPhase task #13 done
/var/tmp/824-1000/mr2499-worker1: given mapPhase task #15 on file 824-mrinput-15.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker0: mapPhase task #14 done
/var/tmp/824-1000/mr2499-worker0: given mapPhase task #16 on file 824-mrinput-16.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker1: mapPhase task #15 done
/var/tmp/824-1000/mr2499-worker1: given mapPhase task #17 on file 824-mrinput-17.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker0: mapPhase task #16 done
/var/tmp/824-1000/mr2499-worker0: given mapPhase task #18 on file 824-mrinput-18.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker1: mapPhase task #17 done
/var/tmp/824-1000/mr2499-worker1: given mapPhase task #19 on file 824-mrinput-19.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker0: mapPhase task #18 done
/var/tmp/824-1000/mr2499-worker1: mapPhase task #19 done
Schedule: mapPhase done
Schedule: 10 reducePhase tasks (20 I/Os)
/var/tmp/824-1000/mr2499-worker1: given reducePhase task #1 on file (nios: 20)
/var/tmp/824-1000/mr2499-worker1: reducePhase task #1 done
/var/tmp/824-1000/mr2499-worker1: given reducePhase task #2 on file (nios: 20)
/var/tmp/824-1000/mr2499-worker1: reducePhase task #2 done
/var/tmp/824-1000/mr2499-worker1: given reducePhase task #3 on file (nios: 20)
/var/tmp/824-1000/mr2499-worker1: reducePhase task #3 done
/var/tmp/824-1000/mr2499-worker1: given reducePhase task #4 on file (nios: 20)
/var/tmp/824-1000/mr2499-worker1: reducePhase task #4 done
/var/tmp/824-1000/mr2499-worker1: given reducePhase task #5 on file (nios: 20)
/var/tmp/824-1000/mr2499-worker1: reducePhase task #5 done
/var/tmp/824-1000/mr2499-worker1: given reducePhase task #6 on file (nios: 20)
/var/tmp/824-1000/mr2499-worker1: reducePhase task #6 done
/var/tmp/824-1000/mr2499-worker1: given reducePhase task #7 on file (nios: 20)
/var/tmp/824-1000/mr2499-worker1: reducePhase task #7 done
/var/tmp/824-1000/mr2499-worker1: given reducePhase task #8 on file (nios: 20)
/var/tmp/824-1000/mr2499-worker1: reducePhase task #8 done
/var/tmp/824-1000/mr2499-worker1: given reducePhase task #9 on file (nios: 20)
/var/tmp/824-1000/mr2499-worker1: reducePhase task #9 done
/var/tmp/824-1000/mr2499-worker1: given reducePhase task #0 on file (nios: 20)
/var/tmp/824-1000/mr2499-worker1: reducePhase task #0 done
Schedule: reducePhase done
Master: RPC /var/tmp/824-1000/mr2499-worker0 shutdown error
Merge: read mrtmp.test-res-0
Merge: read mrtmp.test-res-1
Merge: read mrtmp.test-res-2
Merge: read mrtmp.test-res-3
Merge: read mrtmp.test-res-4
Merge: read mrtmp.test-res-5
Merge: read mrtmp.test-res-6
Merge: read mrtmp.test-res-7
Merge: read mrtmp.test-res-8
Merge: read mrtmp.test-res-9
/var/tmp/824-1000/mr2499-master: Map/Reduce task completed
2019/03/24 08:59:25 rpc.Register: method "CleanupFiles" has 1 input parameters; needs exactly three
2019/03/24 08:59:25 rpc.Register: method "Lock" has 1 input parameters; needs exactly three
2019/03/24 08:59:25 rpc.Register: method "Unlock" has 1 input parameters; needs exactly three
2019/03/24 08:59:25 rpc.Register: method "Wait" has 1 input parameters; needs exactly three
/var/tmp/824-1000/mr2499-master: Starting Map/Reduce task test
Schedule: 20 mapPhase tasks (10 I/Os)
2019/03/24 08:59:25 rpc.Register: method "Lock" has 1 input parameters; needs exactly three
2019/03/24 08:59:25 rpc.Register: method "Unlock" has 1 input parameters; needs exactly three
2019/03/24 08:59:25 rpc.Register: method "Lock" has 1 input parameters; needs exactly three
2019/03/24 08:59:25 rpc.Register: method "Unlock" has 1 input parameters; needs exactly three
/var/tmp/824-1000/mr2499-worker1: given mapPhase task #0 on file 824-mrinput-0.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker0: given mapPhase task #1 on file 824-mrinput-1.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker1: mapPhase task #0 done
/var/tmp/824-1000/mr2499-worker0: mapPhase task #1 done
/var/tmp/824-1000/mr2499-worker1: given mapPhase task #2 on file 824-mrinput-2.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker0: given mapPhase task #3 on file 824-mrinput-3.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker1: mapPhase task #2 done
/var/tmp/824-1000/mr2499-worker0: mapPhase task #3 done
/var/tmp/824-1000/mr2499-worker1: given mapPhase task #4 on file 824-mrinput-4.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker0: given mapPhase task #5 on file 824-mrinput-5.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker1: mapPhase task #4 done
/var/tmp/824-1000/mr2499-worker0: mapPhase task #5 done
/var/tmp/824-1000/mr2499-worker0: given mapPhase task #6 on file 824-mrinput-6.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker1: given mapPhase task #7 on file 824-mrinput-7.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker1: mapPhase task #7 done
/var/tmp/824-1000/mr2499-worker0: mapPhase task #6 done
/var/tmp/824-1000/mr2499-worker1: given mapPhase task #8 on file 824-mrinput-8.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker0: given mapPhase task #9 on file 824-mrinput-9.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker1: mapPhase task #8 done
/var/tmp/824-1000/mr2499-worker0: mapPhase task #9 done
/var/tmp/824-1000/mr2499-worker1: given mapPhase task #10 on file 824-mrinput-10.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker0: given mapPhase task #11 on file 824-mrinput-11.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker1: mapPhase task #10 done
/var/tmp/824-1000/mr2499-worker1: given mapPhase task #12 on file 824-mrinput-12.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker0: mapPhase task #11 done
/var/tmp/824-1000/mr2499-worker0: given mapPhase task #13 on file 824-mrinput-13.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker1: mapPhase task #12 done
/var/tmp/824-1000/mr2499-worker1: given mapPhase task #14 on file 824-mrinput-14.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker0: mapPhase task #13 done
/var/tmp/824-1000/mr2499-worker0: given mapPhase task #15 on file 824-mrinput-15.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker1: mapPhase task #14 done
/var/tmp/824-1000/mr2499-worker1: given mapPhase task #16 on file 824-mrinput-16.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker0: mapPhase task #15 done
/var/tmp/824-1000/mr2499-worker0: given mapPhase task #17 on file 824-mrinput-17.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker1: mapPhase task #16 done
/var/tmp/824-1000/mr2499-worker1: given mapPhase task #18 on file 824-mrinput-18.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker0: mapPhase task #17 done
/var/tmp/824-1000/mr2499-worker0: given mapPhase task #19 on file 824-mrinput-19.txt (nios: 10)
/var/tmp/824-1000/mr2499-worker1: mapPhase task #18 done
/var/tmp/824-1000/mr2499-worker0: mapPhase task #19 done
Schedule: mapPhase done
Schedule: 10 reducePhase tasks (20 I/Os)
2019/03/24 08:59:26 rpc.Register: method "Lock" has 1 input parameters; needs exactly three
2019/03/24 08:59:26 rpc.Register: method "Lock" has 1 input parameters; needs exactly three
2019/03/24 08:59:26 rpc.Register: method "Unlock" has 1 input parameters; needs exactly three
2019/03/24 08:59:26 rpc.Register: method "Unlock" has 1 input parameters; needs exactly three
/var/tmp/824-1000/mr2499-worker3: given reducePhase task #2 on file (nios: 20)
/var/tmp/824-1000/mr2499-worker2: given reducePhase task #3 on file (nios: 20)
/var/tmp/824-1000/mr2499-worker3: reducePhase task #2 done
/var/tmp/824-1000/mr2499-worker2: reducePhase task #3 done
/var/tmp/824-1000/mr2499-worker3: given reducePhase task #5 on file (nios: 20)
/var/tmp/824-1000/mr2499-worker2: given reducePhase task #4 on file (nios: 20)
/var/tmp/824-1000/mr2499-worker3: reducePhase task #5 done
/var/tmp/824-1000/mr2499-worker3: given reducePhase task #6 on file (nios: 20)
/var/tmp/824-1000/mr2499-worker2: reducePhase task #4 done
/var/tmp/824-1000/mr2499-worker2: given reducePhase task #7 on file (nios: 20)
/var/tmp/824-1000/mr2499-worker3: reducePhase task #6 done
/var/tmp/824-1000/mr2499-worker3: given reducePhase task #8 on file (nios: 20)
/var/tmp/824-1000/mr2499-worker2: reducePhase task #7 done
/var/tmp/824-1000/mr2499-worker2: given reducePhase task #9 on file (nios: 20)
/var/tmp/824-1000/mr2499-worker3: reducePhase task #8 done
/var/tmp/824-1000/mr2499-worker3: given reducePhase task #0 on file (nios: 20)
/var/tmp/824-1000/mr2499-worker2: reducePhase task #9 done
/var/tmp/824-1000/mr2499-worker2: given reducePhase task #1 on file (nios: 20)
/var/tmp/824-1000/mr2499-worker3: reducePhase task #0 done
/var/tmp/824-1000/mr2499-worker2: reducePhase task #1 done
Schedule: reducePhase done
Master: RPC /var/tmp/824-1000/mr2499-worker1 shutdown error
Master: RPC /var/tmp/824-1000/mr2499-worker0 shutdown error
Merge: read mrtmp.test-res-0
Merge: read mrtmp.test-res-1
Merge: read mrtmp.test-res-2
Merge: read mrtmp.test-res-3
Merge: read mrtmp.test-res-4
Merge: read mrtmp.test-res-5
Merge: read mrtmp.test-res-6
Merge: read mrtmp.test-res-7
Merge: read mrtmp.test-res-8
Merge: read mrtmp.test-res-9
/var/tmp/824-1000/mr2499-master: Map/Reduce task completed
PASS
ok _/home/master/study/6.824/src/mapreduce 2.845s
master@master:~/study/6.824/src/mapreduce$
schedule.go
package mapreduce import (
"fmt"
"strings"
"sync"
"time"
) //
// schedule() starts and waits for all tasks in the given phase (mapPhase
// or reducePhase). the mapFiles argument holds the names of the files that
// are the inputs to the map phase, one per map task. nReduce is the
// number of reduce tasks. the registerChan argument yields a stream
// of registered workers; each item is the worker's RPC address,
// suitable for passing to call(). registerChan will yield all
// existing registered workers (if any) and new ones as they register.
//
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) // All ntasks tasks have to be scheduled on workers. Once all tasks
// have completed successfully, schedule() should return.
//
// Your code here (Part III, Part IV).
// //没有任务直接返回
if ntasks <= 0 {
return
}
//完成标志,应该取值为负数,不能与任务下标重叠
finishFlag := -2
//创建可工作worker队列
usableWorkers := make(chan string)
//创建任务队列
waitTaskChan := make(chan int, ntasks)
//初始化任务队列
for k:=0; k<ntasks; k++ {
waitTaskChan <- k
}
//创建任务完成计数跟踪线程通知主线程处理结果,主线线程堵塞获取待处理任务
handleCount := sync.WaitGroup{}
handleCount.Add(ntasks)
go func() {
handleCount.Wait()
waitTaskChan <- finishFlag
}() //获取待处理任务
for taskIndex := range waitTaskChan {
// 标志为处理所有的任务处理完成 if taskIndex == finishFlag {
break;
}
//两个信道怎么堵塞读取:worker怎么改为不为自旋的方式?
for {
var work string;
//如果有待处理的任务,则获取可用的worker
select {
//应该先读新注册的worker.
case work =<- registerChan:
case work =<- usableWorkers:
}
if strings.TrimSpace(work) != "" {
//创建处理任务线程
go func(index int) {
rqArs := new(DoTaskArgs)
rqArs.JobName=jobName
if phase == mapPhase {
rqArs.File = mapFiles[index]
}
rqArs.Phase=phase
rqArs.TaskNumber=index
rqArs.NumOtherPhase=n_other
handleResult := false
defer func(){
if handleResult {
//worker 处理成功,则完成计数减一,worker返回可用的队列
handleCount.Done()
usableWorkers <- work
} else {
//usableWorkers <- work //处理失败的 worker 不应该放回可用worker队列?
waitTaskChan <- index
}
}()
handleResult = call(work, "Worker.DoTask",
rqArs, nil)
}(taskIndex)
break
} else {
//释放cpu
time.After(time.Second)
}
}
}
fmt.Printf("Schedule: %v done\n", phase)
}