-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
98 lines (82 loc) · 1.85 KB
/
main.go
File metadata and controls
98 lines (82 loc) · 1.85 KB
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
package main
import (
"flag"
"fmt"
"log"
"os"
"time"
"mapreduce/mr"
)
// Example map function for word count.
func mapF(filename string, contents string) []mr.KeyValue {
var kva []mr.KeyValue
word := ""
for _, r := range contents {
if r == ' ' || r == '\n' || r == '\t' || r == '\r' {
if word != "" {
kva = append(kva, mr.KeyValue{Key: word, Value: "1"})
word = ""
}
continue
}
word += string(r)
}
if word != "" {
kva = append(kva, mr.KeyValue{Key: word, Value: "1"})
}
return kva
}
// Example reduce function for word count.
func reduceF(key string, values []string) string {
return fmt.Sprintf("%d", len(values))
}
func main() {
if len(os.Args) < 2 {
fmt.Println("usage: go run main.go coordinator|worker [options]")
os.Exit(1)
}
mode := os.Args[1]
switch mode {
case "coordinator":
coordFlags := flag.NewFlagSet("coordinator", flag.ExitOnError)
nReduce := coordFlags.Int("nreduce", 3, "number of reduce tasks")
coordFlags.Parse(os.Args[2:])
files := coordFlags.Args()
if len(files) == 0 {
log.Fatal("coordinator: need input files")
}
c := mr.MakeCoordinator(files, *nReduce)
// Run coordinator in a separate goroutine
doneChan := make(chan bool)
go func() {
for {
time.Sleep(500 * time.Millisecond)
if c.Done() {
log.Println("Coordinator: all tasks finished")
doneChan <- true
return
}
}
}()
// Main thread waits here
<-doneChan
// Wait for all the worker processes to die
time.Sleep( 2* time.Second)
log.Println("Coordinator exited cleanly")
case "worker":
go func() {
w, err := mr.MakeWorker()
if err != nil {
log.Fatalf("failed to create worker: %v", err)
}
w.StartWorker(mapF, reduceF)
}()
// Keep worker process alive
for {
time.Sleep(1 * time.Second)
}
default:
fmt.Println("unknown mode:", mode)
os.Exit(1)
}
}