-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobcount.go
46 lines (41 loc) · 1006 Bytes
/
jobcount.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
package main
//
// a MapReduce pseudo-application that counts the number of times map/reduce
// tasks are run, to test whether jobs are assigned multiple times even when
// there is no failure.
//
// go build -buildmode=plugin crash.go
//
import "6.824/mr"
import "math/rand"
import "strings"
import "strconv"
import "time"
import "fmt"
import "os"
import "io/ioutil"
var count int
func Map(filename string, contents string) []mr.KeyValue {
me := os.Getpid()
f := fmt.Sprintf("mr-worker-jobcount-%d-%d", me, count)
count++
err := ioutil.WriteFile(f, []byte("x"), 0666)
if err != nil {
panic(err)
}
time.Sleep(time.Duration(2000+rand.Intn(3000)) * time.Millisecond)
return []mr.KeyValue{mr.KeyValue{"a", "x"}}
}
func Reduce(key string, values []string) string {
files, err := ioutil.ReadDir(".")
if err != nil {
panic(err)
}
invocations := 0
for _, f := range files {
if strings.HasPrefix(f.Name(), "mr-worker-jobcount") {
invocations++
}
}
return strconv.Itoa(invocations)
}