-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.go
155 lines (144 loc) · 3.8 KB
/
worker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"encoding/json"
"fmt"
"hash/fnv"
"log"
"mapreduce/plugins"
"net/rpc"
"os"
"time"
)
type Worker struct {
masterAddr string
plugin plugins.Plugin
}
func (w *Worker) work() {
for {
w.CallRequestJob()
}
}
func (w *Worker) CallRequestJob() {
args := RequestJobArgs{}
reply := RequestJobReply{}
ok := w.call("Coordinator.RequestJob", &args, &reply)
if ok {
// an empty job is a signal to wait
if reply.Job == nil {
duration, _ := time.ParseDuration("1s")
time.Sleep(duration)
return
}
fmt.Printf("Received a job: %v\n", *reply.Job)
w.doTask(reply.Job)
} else {
fmt.Print("call failed!\n")
log.Fatal("worker shutting down ...")
}
}
func (w *Worker) CallCompleteMapJob(id int, filePaths map[int][]string) {
args := CompleteMapJobArgs{Id: id, FilePaths: filePaths}
reply := CompleteMapJobReply{}
ok := w.call("Coordinator.CompleteMapJob", &args, &reply)
if ok {
log.Printf("Worker successfully sent completion notice of map job #%d", id)
} else {
log.Fatal("Worker failed to send completion notice of map job")
}
}
func (w *Worker) CallCompleteReduceJob(id int) {
args := CompleteReduceJobArgs{Id: id}
reply := CompleteReduceJobReply{}
ok := w.call("Coordinator.CompleteReduceJob", &args, &reply)
if ok {
log.Printf("Worker successfully sent completion notice of reduce job #%d", id)
} else {
log.Fatal("Worker failed to send completion notice of reduce job")
}
}
func (w *Worker) call(rpcName string, args interface{}, reply interface{}) bool {
c, err := rpc.DialHTTP("tcp", w.masterAddr)
if err != nil {
log.Fatal("dialing: ", err)
}
defer c.Close()
err = c.Call(rpcName, args, reply)
if err == nil {
return true
}
fmt.Println(err)
return false
}
func (w *Worker) doTask(job *Job) {
if job.Type == "map" {
data, err := os.ReadFile(job.FilePaths[0])
if err != nil {
log.Fatal(err)
}
pairs := w.plugin.Map(string(data))
partitions := w.partitionIntermediate(pairs, job.NReduce)
filePaths := map[int][]string{}
for p, part := range partitions {
intFilePath := fmt.Sprintf("./mock_fs/m%d-%d.txt", job.Id, p)
_, present := filePaths[p]
if present {
filePaths[p] = append(filePaths[p], intFilePath)
} else {
filePaths[p] = []string{intFilePath}
}
f, err := os.Create(intFilePath)
if err != nil {
log.Fatal("error creating new file: ", err)
}
defer f.Close()
err = json.NewEncoder(f).Encode(part)
if err != nil {
log.Fatal("error encoding intermediate pairs: ", err)
}
}
w.CallCompleteMapJob(job.Id, filePaths)
} else if job.Type == "reduce" {
intermediate_pairs := []*plugins.KeyValue{}
for _, fp := range job.FilePaths {
f, err := os.Open(fp)
if err != nil {
log.Fatal(err)
}
part_pairs := []*plugins.KeyValue{}
err = json.NewDecoder(f).Decode(&part_pairs)
if err != nil {
log.Fatal(err)
}
intermediate_pairs = append(intermediate_pairs, part_pairs...)
}
final_pairs := w.plugin.Reduce(intermediate_pairs)
f, err := os.Create(fmt.Sprintf("./mock_fs/o%d.txt", job.Id))
if err != nil {
log.Fatal(err)
}
defer f.Close()
for _, pair := range final_pairs {
f.Write([]byte(fmt.Sprintf("%v: %v\n", pair.Key, pair.Value)))
}
fmt.Printf("Reduce task #%d completed ...", job.Id)
w.CallCompleteReduceJob(job.Id)
}
}
func (w *Worker) partitionIntermediate(pairs []*plugins.KeyValue, nReduce int) map[int][]*plugins.KeyValue {
res := map[int][]*plugins.KeyValue{}
for _, p := range pairs {
h := fnv.New32a()
h.Write([]byte(p.Key))
hash := int(h.Sum32() & 0x7fffffff)
num := hash % nReduce
res[num] = append(res[num], p)
}
return res
}
// instantiate a new worker
func NewWorker(addr string, plugin string) *Worker {
if plugin == "" {
log.Fatal("a worker requires a plugin ...")
}
return &Worker{masterAddr: addr, plugin: plugins.Plugins[plugin]}
}