-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.go
110 lines (100 loc) · 2.5 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
package mapreduce
import (
"fmt"
"log"
"net"
"net/rpc"
"os"
"sync"
)
// Worker holds the state for a server waiting for DoTask or Shutdown RPCs
type Worker struct {
sync.Mutex
name string
Map func(string, string) []KeyValue
Reduce func(string, []string) string
//MD nRPC是干嘛的
nRPC int // protected by mutex
nTasks int // protected by mutex
l net.Listener
}
// DoTask is called by the master when a new task is being scheduled on this
// worker.
func (wk *Worker) DoTask(arg *DoTaskArgs, _ *struct{}) error {
fmt.Printf("%s: given %v task #%d on file %s (nios: %d)\n",
wk.name, arg.Phase, arg.TaskNumber, arg.File, arg.NumOtherPhase)
switch arg.Phase {
case mapPhase:
doMap(arg.JobName, arg.TaskNumber, arg.File, arg.NumOtherPhase, wk.Map)
case reducePhase:
doReduce(arg.JobName, arg.TaskNumber, arg.NumOtherPhase, wk.Reduce)
}
fmt.Printf("%s: %v task #%d done\n", wk.name, arg.Phase, arg.TaskNumber)
return nil
}
// Shutdown is called by the master when all work has been completed.
// We should respond with the number of tasks we have processed.
func (wk *Worker) Shutdown(_ *struct{}, res *ShutdownReply) error {
debug("Shutdown %s\n", wk.name)
wk.Lock()
defer wk.Unlock()
res.Ntasks = wk.nTasks
wk.nRPC = 1
wk.nTasks-- // Don't count the shutdown RPC
return nil
}
// Tell the master we exist and ready to work
func (wk *Worker) register(master string) {
args := new(RegisterArgs)
args.Worker = wk.name
ok := call(master, "Master.Register", args, new(struct{}))
if ok == false {
fmt.Printf("Register: RPC %s register error\n", master)
}
}
// RunWorker sets up a connection with the master, registers its address, and
// waits for tasks to be scheduled.
func RunWorker(MasterAddress string, me string,
MapFunc func(string, string) []KeyValue,
ReduceFunc func(string, []string) string,
nRPC int,
) {
debug("RunWorker %s\n", me)
wk := new(Worker)
wk.name = me
wk.Map = MapFunc
wk.Reduce = ReduceFunc
wk.nRPC = nRPC
rpcs := rpc.NewServer()
rpcs.Register(wk)
os.Remove(me) // only needed for "unix"
l, e := net.Listen("unix", me)
if e != nil {
log.Fatal("RunWorker: worker ", me, " error: ", e)
}
wk.l = l
wk.register(MasterAddress)
// DON'T MODIFY CODE BELOW
for {
wk.Lock()
if wk.nRPC == 0 {
wk.Unlock()
break
}
wk.Unlock()
conn, err := wk.l.Accept()
if err == nil {
wk.Lock()
wk.nRPC--
wk.Unlock()
go rpcs.ServeConn(conn)
wk.Lock()
wk.nTasks++
wk.Unlock()
} else {
break
}
}
wk.l.Close()
debug("RunWorker %s exit\n", me)
}