序言

1. master.go 可以正常调用Distributed() 和 Sequential()。
Sequential 顺序执行主要用于调试。
2. master : 创建一个 RPC server(master_rpc.go). 等待 worker 注册服务(master.go Register() RPC 调用). schedule() (schedule.go) 决定调度一个任务给worker 和处理worker 工作失败的状况。
3. master 任务每个输入文件就是一个 map 任务 和调用 doMap() [common_map.go] .可以通过Sequential()直接调用或者 PRC DoTask 分配给worker 处理(worker.go) 。
每个doMap 会生成 nReduce 个中间文件 。 有 nMap 个map任务。那么在 所有map任务处理完,也就一共又 nMap X nReduce 个中间文件。
mrtmp.xxx-0-0 mrtmp.xxx-0-1 mrtmp.xxx-0-2 mrtmp.xxx-1-0 mrtmp.xxx-1-1 mrtmp.xxx-1-2
4. 接下来,master 会调用doReduce()[common_reduce.go], 像doMap() 一样可以直接调用或者通过一个worker . doReduce() 会为 第r 个 reduce 任务 搜集 每个 map任务生成的第r 个中间文件。和调用 reduce 函数处理每个 key的value值,生成 nReduce 结果文件。
5. master 调用 mr.merge() [master_splitmerge.go] 。合并前一步所生成的 nReduce 结果文件输出一个最终结果文件。
6. master 给 每个worker 发送Shutdown RPC, 和master自己的RPC server.

part I

实现 doMap()[ common_map.go] 和 doReduce() [ common_reduce.go]
通过 test_test.go 测试修改的程序正确性。
$ cd 6.824 $ export "GOPATH=$PWD" # go needs $GOPATH to be set to the project's working directory $ cd "$GOPATH/src/mapreduce" $ go test -run Sequential
可以 查看详细运行的信息
set debugEnabled = true in common.go, and add -v to the test command above
$ env "GOPATH=$PWD/../../" go test -v -run Sequential === RUN TestSequentialSingle master: Starting Map/Reduce task test Merge: read mrtmp.test-res-0 master: Map/Reduce task completed --- PASS: TestSequentialSingle (1.34s) === RUN TestSequentialMany master: Starting Map/Reduce task test Merge: read mrtmp.test-res-0 Merge: read mrtmp.test-res-1 Merge: read mrtmp.test-res-2 master: Map/Reduce task completed --- PASS: TestSequentialMany (1.33s) PASS ok mapreduce 2.672s
master@master:~/study/6.824/src/mapreduce$ go test -run Sequential
# runtime/cgo
exec: "gcc": executable file not found in $PATH
FAIL mapreduce [build failed]
安装 gcc
sudo apt-get update
sudo apt-get install gcc
master@master:~/study/6.824/src/mapreduce$ env "GOPATH=$PWD/../../" go test -v -run Sequential
# mapreduce
./master_rpc.go:48:10: debug call has arguments but no formatting directives

调试 遇到问题
master@master:~/study/6.824/src/mapreduce$ go test -v -run Sequential
=== RUN TestSequentialSingle
master: Starting Map/Reduce task test
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x557207]
goroutine 19 [running]:
encoding/json.(*Encoder).Encode(0x0, 0x7a8f60, 0xc0000f8020, 0xe, 0xc000043dd8)
/usr/local/go/src/encoding/json/stream.go:196 +0x37
mapreduce.doMap(0x846c2c, 0x4, 0x0, 0xc000110080, 0x11, 0x1, 0x864d90)
/home/master/study/6.824/src/mapreduce/common_map.go:94 +0x331
mapreduce.Sequential.func1(0x849c4f, 0x8)
/home/master/study/6.824/src/mapreduce/master.go:69 +0x101
mapreduce.(*Master).run(0xc00011e0a0, 0x846c2c, 0x4, 0xc0000fa140, 0x1, 0x1, 0x1, 0xc0000f8100, 0xc0000f6540)
/home/master/study/6.824/src/mapreduce/master.go:142 +0x17b
created by mapreduce.Sequential
/home/master/study/6.824/src/mapreduce/master.go:65 +0x241
exit status 2

多了 : 号

common_map.go

package mapreduce

import (
"hash/fnv"
"io"
"os"
"io/ioutil"
"log"
"encoding/json"
) func doMap(
jobName string, // the name of the MapReduce job
mapTask int, // which map task this is
inFile string,
nReduce int, // the number of reduce task that will be run ("R" in the paper)
mapF func(filename string, contents string) []KeyValue,
) { bs,err:=ioutil.ReadFile(inFile)
if err != io.EOF && err != nil {
log.Fatal(err)
return
}
filecontent := string(bs)
fmap := make(map[string]*json.Encoder)
fo := make([]*os.File,nReduce)
defer func(){
for _, fff := range fo {
fff.Close()
}
}() kvs := mapF(inFile, filecontent)
for _ , kv := range kvs {
k := kv.Key
kh := ihash(k)
r := kh % nReduce
tfileName := reduceName(jobName, mapTask, r)
var encoder *json.Encoder
encoder = fmap[tfileName]
if encoder == nil {
fe , err := os.Create(tfileName)
if err != nil {
log.Fatal(err)
return
}
fo = append(fo,fe)
encoder = json.NewEncoder(fe)
fmap[tfileName] = encoder
}
encoder.Encode(&kv)
} } func ihash(s string) int {
h := fnv.New32a()
h.Write([]byte(s))
return int(h.Sum32() & 0x7fffffff)
}

common_reduce.go

package mapreduce

import (
"io"
"os"
"log"
"encoding/json"
) func doReduce(
jobName string, // the name of the whole MapReduce job
reduceTask int, // which reduce task this is
outFile string, // write the output here
nMap int, // the number of map tasks that were run ("M" in the paper)
reduceF func(key string, values []string) string,
) { kvm := make(map[string][]string)
fo := make([]*os.File,nMap)
defer func(){
for _, fff := range fo {
fff.Close()
}
}()
for i := ; i< nMap; i++ {
tf := reduceName(jobName, i, reduceTask)
ff, err := os.Open(tf)
if err != nil {
log.Fatal(err)
panic(err)
}
fo=append(fo,ff)
decoder := json.NewDecoder(ff)
var ky KeyValue
for {
if err := decoder.Decode(&ky); err == io.EOF {
break
} else if(err != nil) {
log.Fatal(err)
panic(err)
}
vlist := kvm[ky.Key];
vlist = append(vlist, ky.Value)
kvm[ky.Key] = vlist
}
}
tfileName := mergeName(jobName, reduceTask) fe,err := os.Create(tfileName)
if err != nil {
log.Fatal(err)
panic(err)
}
defer func(){
fe.Close();
}()
encoder := json.NewEncoder(fe)
for k , v := range kvm {
encoder.Encode(KeyValue{k, reduceF(k,v)})
} }
 
 

LAB1 partI的更多相关文章

  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. linux之时间设置

    date 显示与设置系统时间 %Y      year %m moth 月 %d day 日期 %H hour 小时 %M      minute   分钟 %S      sec  秒 +%F    ...

  2. python学习笔记 18-4-11

    一.执行一个简单的代码 1.先创建目录 mkdir /home/dev 2.切换到目录 cd /home/dev 3.在目录下创建文件夹 vim hello.py 4.编辑文件内容 vim hello ...

  3. list quen队列

    队列特性:先进先出 stack 栈先进后出 push() 输入 pop()输出 set接口 collectonjiek list接口:可重复集(可以用下标取值) set接口:不可重复集(没下标) Ha ...

  4. 微信小程序生成二维码工具

    实现的效果 使用(非自适应) 完整代码请参考网址里https://github.com/tomfriwel/weapp-qrcode 页面wxml中放置绘制二维码的canvas: <canvas ...

  5. mac下 部分服务启动,结束, 查看状态的命令

    以sshd服务为例 启动sshd服务:sudo launchctl load -w /System/Library/LaunchDaemons/ssh.plist 停止sshd服务:sudo laun ...

  6. springboot入门1

    1引入springboot父依赖,和 spring-boot-starter-web的启动器 依赖引入后jar包展示依赖的情况 入门工程  配置数据源 package com.boot.web.con ...

  7. Go语言极速入门手册.go

    Github: https://github.com/coderzh/CodeTips /* gotips_test.go: Golang速学速查速用代码手册 Source: github.com/c ...

  8. Python控制台颜色

    Python控制台颜色 格式:\033[显示方式;前景色;背景色m 说明: 前景色 背景色 颜色 30 40 黑色 31 41 红色 32 42 绿色 33 43 黃色 34 44 蓝色 35 45 ...

  9. C++ 状态机接口

    最近的状态极差,甚至代码也写不下去了.给自己手臂上的两刀没有任何的作用,看来早已经是麻痹了. 一直想弄一个勉强能用的状态机,用于在各种涉及到状态转换的时候用到,然而脑子并不是太清醒. 先放在这里一个接 ...

  10. ftruncate

    普通文件或共享内存区的大小都可以通过该函数修改 #include <unistd.h> int ftruncate(int fd,off_t leght); //成功返回0失败返回-1 对 ...