forked from jwang/mapreduce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapper.go
42 lines (39 loc) · 903 Bytes
/
mapper.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
package mapreduce
import (
"log"
"rpc"
)
type e struct{}
func runMapper(reducer string, mFunc Mapper) {
c, err := rpc.DialHTTP("tcp", reducer)
if err != nil {
log.Fatal("Connecting to Reducer: ", err)
}
defer c.Close()
if err := c.Call("Reducer.Hello", &e{}, &e{}); err != nil {
log.Fatal("Reducer.Hello: ", err)
}
var calls []*rpc.Call
for {
var w interface{}
if err := c.Call("Reducer.GetWork", &e{}, &w); err != nil {
if err.String() == ErrDone.String() {
break
}
log.Fatal("Reducer.GetWork: ", err)
}
r, err := mFunc(w)
if err != nil {
log.Fatal("Mapper: ", err)
}
call := c.Go("Reducer.SendResult", &r, &e{}, nil)
calls = append(calls, call)
}
for _, call := range calls {
<-call.Done
}
if err := c.Call("Reducer.Goodbye", &e{}, &e{}); err != nil {
log.Fatal("Reducer.Goodbye: ", err)
}
log.Print("Mapper exited cleanly; shutting down")
}