56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
package main
|
|
|
|
//
|
|
// a MapReduce pseudo-application that sometimes crashes,
|
|
// and sometimes takes a long time,
|
|
// to test MapReduce's ability to recover.
|
|
//
|
|
// go build -buildmode=plugin crash.go
|
|
//
|
|
|
|
import "6.5840/mr"
|
|
import crand "crypto/rand"
|
|
import "math/big"
|
|
import "strings"
|
|
import "os"
|
|
import "sort"
|
|
import "strconv"
|
|
import "time"
|
|
|
|
func maybeCrash() {
|
|
max := big.NewInt(1000)
|
|
rr, _ := crand.Int(crand.Reader, max)
|
|
if rr.Int64() < 330 {
|
|
// crash!
|
|
os.Exit(1)
|
|
} else if rr.Int64() < 660 {
|
|
// delay for a while.
|
|
maxms := big.NewInt(10 * 1000)
|
|
ms, _ := crand.Int(crand.Reader, maxms)
|
|
time.Sleep(time.Duration(ms.Int64()) * time.Millisecond)
|
|
}
|
|
}
|
|
|
|
func Map(filename string, contents string) []mr.KeyValue {
|
|
maybeCrash()
|
|
|
|
kva := []mr.KeyValue{}
|
|
kva = append(kva, mr.KeyValue{"a", filename})
|
|
kva = append(kva, mr.KeyValue{"b", strconv.Itoa(len(filename))})
|
|
kva = append(kva, mr.KeyValue{"c", strconv.Itoa(len(contents))})
|
|
kva = append(kva, mr.KeyValue{"d", "xyzzy"})
|
|
return kva
|
|
}
|
|
|
|
func Reduce(key string, values []string) string {
|
|
maybeCrash()
|
|
|
|
// sort values to ensure deterministic output.
|
|
vv := make([]string, len(values))
|
|
copy(vv, values)
|
|
sort.Strings(vv)
|
|
|
|
val := strings.Join(vv, " ")
|
|
return val
|
|
}
|