partIII

实现 分发MapReduce 任务,实现 schedule() 方法在 mapreduce/schedule.go。
schedule()函数的职责是把任务分给可用的worker.
schedule()通过信道registerChan参数读取 注册的worker.
worker与schedule()之间的顺序是没有确定的。
schedule()告诉worker 执行远程函数调用Worker.DoTask.
远程调用的参数 DoTaskArgs定义在 mapreduce/common_rpc.go
File 参数只有 mapTask 才用到.
rpc 使用call() function 定义在 mapreduce/common_rpc.go
 
测试命令
go test -run TestParallel
会执行两边,TestParallelBasic and TestParallelCheck;
master@master:~/study/6.824/src/mapreduce$ go test -run TestParallel
2019/03/22 14:34:40 rpc.Register: method "CleanupFiles" has 1 input parameters; needs exactly three
2019/03/22 14:34:40 rpc.Register: method "Lock" has 1 input parameters; needs exactly three
2019/03/22 14:34:40 rpc.Register: method "Unlock" has 1 input parameters; needs exactly three
2019/03/22 14:34:40 rpc.Register: method "Wait" has 1 input parameters; needs exactly three
2019/03/22 14:34:40 rpc.Register: method "Lock" has 1 input parameters; needs exactly three
2019/03/22 14:34:40 rpc.Register: method "Unlock" has 1 input parameters; needs exactly three
/var/tmp/824-1000/mr20412-master: Starting Map/Reduce task test
2019/03/22 14:34:40 rpc.Register: method "Lock" has 1 input parameters; needs exactly three
2019/03/22 14:34:40 rpc.Register: method "Unlock" has 1 input parameters; needs exactly three
Schedule: 20 mapPhase tasks (10 I/Os)
/var/tmp/824-1000/mr20412-worker0: given mapPhase task #0 on file 824-mrinput-0.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker1: given mapPhase task #1 on file 824-mrinput-1.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker1: mapPhase task #1 done
/var/tmp/824-1000/mr20412-worker0: mapPhase task #0 done
/var/tmp/824-1000/mr20412-worker1: given mapPhase task #2 on file 824-mrinput-2.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker0: given mapPhase task #3 on file 824-mrinput-3.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker0: mapPhase task #3 done
/var/tmp/824-1000/mr20412-worker1: mapPhase task #2 done
/var/tmp/824-1000/mr20412-worker0: given mapPhase task #4 on file 824-mrinput-4.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker1: given mapPhase task #5 on file 824-mrinput-5.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker0: mapPhase task #4 done
/var/tmp/824-1000/mr20412-worker0: given mapPhase task #6 on file 824-mrinput-6.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker1: mapPhase task #5 done
/var/tmp/824-1000/mr20412-worker1: given mapPhase task #7 on file 824-mrinput-7.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker0: mapPhase task #6 done
/var/tmp/824-1000/mr20412-worker0: given mapPhase task #8 on file 824-mrinput-8.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker1: mapPhase task #7 done
/var/tmp/824-1000/mr20412-worker1: given mapPhase task #9 on file 824-mrinput-9.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker0: mapPhase task #8 done
/var/tmp/824-1000/mr20412-worker0: given mapPhase task #10 on file 824-mrinput-10.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker1: mapPhase task #9 done
/var/tmp/824-1000/mr20412-worker1: given mapPhase task #11 on file 824-mrinput-11.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker0: mapPhase task #10 done
/var/tmp/824-1000/mr20412-worker0: given mapPhase task #12 on file 824-mrinput-12.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker1: mapPhase task #11 done
/var/tmp/824-1000/mr20412-worker1: given mapPhase task #13 on file 824-mrinput-13.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker0: mapPhase task #12 done
/var/tmp/824-1000/mr20412-worker0: given mapPhase task #14 on file 824-mrinput-14.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker1: mapPhase task #13 done
/var/tmp/824-1000/mr20412-worker1: given mapPhase task #15 on file 824-mrinput-15.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker0: mapPhase task #14 done
/var/tmp/824-1000/mr20412-worker0: given mapPhase task #16 on file 824-mrinput-16.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker1: mapPhase task #15 done
/var/tmp/824-1000/mr20412-worker1: given mapPhase task #17 on file 824-mrinput-17.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker0: mapPhase task #16 done
/var/tmp/824-1000/mr20412-worker0: given mapPhase task #18 on file 824-mrinput-18.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker1: mapPhase task #17 done
/var/tmp/824-1000/mr20412-worker1: given mapPhase task #19 on file 824-mrinput-19.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker0: mapPhase task #18 done
/var/tmp/824-1000/mr20412-worker1: mapPhase task #19 done
Schedule: mapPhase done
Schedule: 10 reducePhase tasks (20 I/Os)
/var/tmp/824-1000/mr20412-worker1: given reducePhase task #1 on file (nios: 20)
/var/tmp/824-1000/mr20412-worker0: given reducePhase task #0 on file (nios: 20)
/var/tmp/824-1000/mr20412-worker1: reducePhase task #1 done
/var/tmp/824-1000/mr20412-worker1: given reducePhase task #2 on file (nios: 20)
/var/tmp/824-1000/mr20412-worker0: reducePhase task #0 done
/var/tmp/824-1000/mr20412-worker0: given reducePhase task #3 on file (nios: 20)
/var/tmp/824-1000/mr20412-worker1: reducePhase task #2 done
/var/tmp/824-1000/mr20412-worker1: given reducePhase task #4 on file (nios: 20)
/var/tmp/824-1000/mr20412-worker0: reducePhase task #3 done
/var/tmp/824-1000/mr20412-worker0: given reducePhase task #5 on file (nios: 20)
/var/tmp/824-1000/mr20412-worker1: reducePhase task #4 done
/var/tmp/824-1000/mr20412-worker1: given reducePhase task #6 on file (nios: 20)
/var/tmp/824-1000/mr20412-worker0: reducePhase task #5 done
/var/tmp/824-1000/mr20412-worker0: given reducePhase task #7 on file (nios: 20)
/var/tmp/824-1000/mr20412-worker1: reducePhase task #6 done
/var/tmp/824-1000/mr20412-worker1: given reducePhase task #8 on file (nios: 20)
/var/tmp/824-1000/mr20412-worker0: reducePhase task #7 done
/var/tmp/824-1000/mr20412-worker0: given reducePhase task #9 on file (nios: 20)
/var/tmp/824-1000/mr20412-worker1: reducePhase task #8 done
/var/tmp/824-1000/mr20412-worker0: reducePhase task #9 done
Schedule: reducePhase done
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/mr20412-master: Map/Reduce task completed
2019/03/22 14:34:41 rpc.Register: method "CleanupFiles" has 1 input parameters; needs exactly three
2019/03/22 14:34:41 rpc.Register: method "Lock" has 1 input parameters; needs exactly three
2019/03/22 14:34:41 rpc.Register: method "Unlock" has 1 input parameters; needs exactly three
2019/03/22 14:34:41 rpc.Register: method "Wait" has 1 input parameters; needs exactly three
2019/03/22 14:34:41 rpc.Register: method "Lock" has 1 input parameters; needs exactly three
2019/03/22 14:34:41 rpc.Register: method "Unlock" has 1 input parameters; needs exactly three
/var/tmp/824-1000/mr20412-master: Starting Map/Reduce task test
Schedule: 20 mapPhase tasks (10 I/Os)
2019/03/22 14:34:41 rpc.Register: method "Lock" has 1 input parameters; needs exactly three
2019/03/22 14:34:41 rpc.Register: method "Unlock" has 1 input parameters; needs exactly three
/var/tmp/824-1000/mr20412-worker1: given mapPhase task #0 on file 824-mrinput-0.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker0: given mapPhase task #1 on file 824-mrinput-1.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker0: mapPhase task #1 done
/var/tmp/824-1000/mr20412-worker0: given mapPhase task #2 on file 824-mrinput-2.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker0: mapPhase task #2 done
/var/tmp/824-1000/mr20412-worker0: given mapPhase task #3 on file 824-mrinput-3.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker0: mapPhase task #3 done
/var/tmp/824-1000/mr20412-worker0: given mapPhase task #4 on file 824-mrinput-4.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker0: mapPhase task #4 done
/var/tmp/824-1000/mr20412-worker0: given mapPhase task #5 on file 824-mrinput-5.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker0: mapPhase task #5 done
/var/tmp/824-1000/mr20412-worker0: given mapPhase task #6 on file 824-mrinput-6.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker0: mapPhase task #6 done
/var/tmp/824-1000/mr20412-worker0: given mapPhase task #7 on file 824-mrinput-7.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker0: mapPhase task #7 done
/var/tmp/824-1000/mr20412-worker0: given mapPhase task #8 on file 824-mrinput-8.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker0: mapPhase task #8 done
/var/tmp/824-1000/mr20412-worker0: given mapPhase task #9 on file 824-mrinput-9.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker0: mapPhase task #9 done
/var/tmp/824-1000/mr20412-worker0: given mapPhase task #10 on file 824-mrinput-10.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker0: mapPhase task #10 done
/var/tmp/824-1000/mr20412-worker0: given mapPhase task #11 on file 824-mrinput-11.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker0: mapPhase task #11 done
/var/tmp/824-1000/mr20412-worker0: given mapPhase task #12 on file 824-mrinput-12.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker0: mapPhase task #12 done
/var/tmp/824-1000/mr20412-worker0: given mapPhase task #13 on file 824-mrinput-13.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker0: mapPhase task #13 done
/var/tmp/824-1000/mr20412-worker0: given mapPhase task #14 on file 824-mrinput-14.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker0: mapPhase task #14 done
/var/tmp/824-1000/mr20412-worker0: given mapPhase task #15 on file 824-mrinput-15.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker0: mapPhase task #15 done
/var/tmp/824-1000/mr20412-worker0: given mapPhase task #16 on file 824-mrinput-16.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker0: mapPhase task #16 done
/var/tmp/824-1000/mr20412-worker0: given mapPhase task #17 on file 824-mrinput-17.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker0: mapPhase task #17 done
/var/tmp/824-1000/mr20412-worker0: given mapPhase task #18 on file 824-mrinput-18.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker0: mapPhase task #18 done
/var/tmp/824-1000/mr20412-worker0: given mapPhase task #19 on file 824-mrinput-19.txt (nios: 10)
/var/tmp/824-1000/mr20412-worker0: mapPhase task #19 done
/var/tmp/824-1000/mr20412-worker1: mapPhase task #0 done
Schedule: mapPhase done
Schedule: 10 reducePhase tasks (20 I/Os)
/var/tmp/824-1000/mr20412-worker1: given reducePhase task #1 on file (nios: 20)
/var/tmp/824-1000/mr20412-worker0: given reducePhase task #0 on file (nios: 20)
/var/tmp/824-1000/mr20412-worker0: reducePhase task #0 done
/var/tmp/824-1000/mr20412-worker1: reducePhase task #1 done
/var/tmp/824-1000/mr20412-worker0: given reducePhase task #2 on file (nios: 20)
/var/tmp/824-1000/mr20412-worker1: given reducePhase task #3 on file (nios: 20)
/var/tmp/824-1000/mr20412-worker0: reducePhase task #2 done
/var/tmp/824-1000/mr20412-worker0: given reducePhase task #4 on file (nios: 20)
/var/tmp/824-1000/mr20412-worker1: reducePhase task #3 done
/var/tmp/824-1000/mr20412-worker1: given reducePhase task #5 on file (nios: 20)
/var/tmp/824-1000/mr20412-worker0: reducePhase task #4 done
/var/tmp/824-1000/mr20412-worker0: given reducePhase task #6 on file (nios: 20)
/var/tmp/824-1000/mr20412-worker1: reducePhase task #5 done
/var/tmp/824-1000/mr20412-worker1: given reducePhase task #7 on file (nios: 20)
/var/tmp/824-1000/mr20412-worker0: reducePhase task #6 done
/var/tmp/824-1000/mr20412-worker0: given reducePhase task #8 on file (nios: 20)
/var/tmp/824-1000/mr20412-worker1: reducePhase task #7 done
/var/tmp/824-1000/mr20412-worker1: given reducePhase task #9 on file (nios: 20)
/var/tmp/824-1000/mr20412-worker0: reducePhase task #8 done
/var/tmp/824-1000/mr20412-worker1: reducePhase task #9 done
Schedule: reducePhase done
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/mr20412-master: Map/Reduce task completed
PASS
ok _/home/master/study/6.824/src/mapreduce 2.053s
 
schedule.go
package mapreduce

import (
"fmt"
"sync"
"strings"
"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).
//
usableWorkers := make(chan string)
waitGroup := new(sync.WaitGroup)
i :=
for {
var work string;
select {
case work =<- usableWorkers:
//fmt.Printf("usable worker: %v \n", work)
case work =<- registerChan:
//fmt.Printf("usable worker: %v \n", work)
//default:
//fmt.Printf("not usable worker wait for next loop ...\n")
}
if strings.TrimSpace(work) != "" {
waitGroup.Add()
go func(index int) {
rqArs := new(DoTaskArgs)
rqArs.JobName=jobName
if phase == mapPhase {
rqArs.File = mapFiles[index] // only for map, the input file
}
rqArs.Phase=phase // are we in mapPhase or reducePhase?
rqArs.TaskNumber=index // this task's index in the current phase // NumOtherPhase is the total number of tasks in other phase; mappers
// need this to compute the number of output bins, and reducers needs
// this to know how many input files to collect.
rqArs.NumOtherPhase=n_other
defer func(){
waitGroup.Done()
usableWorkers <- work
}()
call(work, "Worker.DoTask",
rqArs, nil)
}(i)
i++
} else {
time.After(time.Second)
}
if i == ntasks {
break
}
}
waitGroup.Wait()
fmt.Printf("Schedule: %v done\n", phase)
}
 
 

LAB1 partIII的更多相关文章

  1. 6.828 lab1 bootload

    MIT6.828 lab1地址:http://pdos.csail.mit.edu/6.828/2014/labs/lab1/ 第一个练习,主要是让我们熟悉汇编,嗯,没什么好说的. Part 1: P ...

  2. Machine Learning #Lab1# Linear Regression

    Machine Learning Lab1 打算把Andrew Ng教授的#Machine Learning#相关的6个实验一一实现了贴出来- 预计时间长度战线会拉的比較长(毕竟JOS的7级浮屠还没搞 ...

  3. ucore lab1 bootloader学习笔记

    ---恢复内容开始--- 开机流程回忆 以Intel 80386为例,计算机加电后,CPU从物理地址0xFFFFFFF0(由初始化的CS:EIP确定,此时CS和IP的值分别是0xF000和0xFFF0 ...

  4. LAB1 partV

    partV 创建文档反向索引.word -> document 与 前面做的 单词统计类似,这个是单词与文档位置的映射关系. mapF 文档解析相同,返回信息不同而已. reduceF 返回归约 ...

  5. 6.824 LAB1 环境搭建

    MIT 6.824 LAB1 环境搭建 vmware 虚拟机 linux ubuntu server   安装 go 官方安装步骤: 下载此压缩包并提取到 /usr/local 目录,在 /usr/l ...

  6. 软件测试:lab1.Junit and Eclemma

    软件测试:lab1.Junit and Eclemma Task: Install Junit(4.12), Hamcrest(1.3) with Eclipse Install Eclemma wi ...

  7. MIT 6.824 lab1:mapreduce

    这是 MIT 6.824 课程 lab1 的学习总结,记录我在学习过程中的收获和踩的坑. 我的实验环境是 windows 10,所以对lab的code 做了一些环境上的修改,如果你仅仅对code 感兴 ...

  8. 清华大学OS操作系统实验lab1练习知识点汇总

    lab1知识点汇总 还是有很多问题,但是我觉得我需要在查看更多资料后回来再理解,学这个也学了一周了,看了大量的资料...还是它们自己的80386手册和lab的指导手册觉得最准确,现在我就把这部分知识做 ...

  9. JOS lab1 part2 分析

    lab1的Exercise 2就是让我们熟悉gdb的si操作,并知道BIOS的几条指令在做什么就够了,所以我们也会尽可能的去分析每一行代码. 首先进入到6.8282/lab这个目录下,输入指令make ...

随机推荐

  1. JAVA字符串的常见处理和操作

    1.纯数字字符串补0为指定位,格式化输出(例如00482这样) 使用String.format处理: int mNumber = 1; // 0 代表前面补充0 // 4 代表长度为4 // d 代表 ...

  2. PYTHON 实现的微信跳一跳【辅助工具】仅作学习

    备注原地址:https://my.oschina.net/anlve/blog/1604163 我又做了一些优化,防止WX检测作弊 准备环境: Windows 10安卓手机,源码中有适配ios,然后链 ...

  3. 求助,我在安装SQL sever2016时遇到了这种情况

    哪位大佬能告诉我这怎么解决?多谢!

  4. Android Tools&Cmd

    1. 生成Keystore文件 keytool -genkey -v -keystore /Users/shiyantao/Desktop/xxx.keystore -alias aliasName ...

  5. 2018-2019-2 网络对抗技术 20165228 Exp2 后门原理与实践

    2018-2019-2 网络对抗技术 20165228 Exp2 后门原理与实践 (1)例举你能想到的一个后门进入到你系统中的可能方式? 在使用盗版软件,盗版系统,甚至游戏外挂时,往往会让我们关闭杀毒 ...

  6. JDK1.8环境下依然报错 Unsupported major.minor version 52.0

    JDK1.8环境下依然报错 Unsupported major.minor version 52.0 在配置elasticsearch-rtf全文搜索引擎时,按照Github上项目readme.md来 ...

  7. margin外边距属性

    外边距属性: 设置元素与元素之间的距离(外边距),4个方向(上右下左). margin:长度值|百分比|auto margin-top margin-right margin-bottom margi ...

  8. HDU - 6203:ping ping ping (DFS序 贪心)

    pro:给定N+1个点的树,有M对关键点,现在让你破坏最少的点,使得M对关键点不连通. sol:贪心,我们把M对点按照LCA深度排序,每次破坏LCA. 如果一对点(u,v,lca),u-lca-v有点 ...

  9. ROADMAP

  10. PYTHON3-LIST.SORT(),SORTED()方法详解。

    python3对于排序提供两种内置方法,一是针对数组的list.sort(), 一是针对所有可迭代序列的sorted().其中list.sort()是在原数组修改,不产生新对象,所以在使用函数后使用赋 ...