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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
type KeyValue struct { Key string Value string }
type ByKey []KeyValue func (a ByKey) Len() int { return len(a) } func (a ByKey) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a ByKey) Less(i, j int) bool { return a[i].Key < a[j].Key }
func Worker(mapf func(string, string) []KeyValue, reducef func(string, []string) string) { var lastTaskType string var lastTaskIndex int id := strconv.Itoa(os.Getpid()) for { args := Args{ WorkerId: id, LastTaskType: lastTaskType, LastTaskIndex: lastTaskIndex, } reply := Reply{} call("Coordinator.RpcHandler", &args, &reply) log.Printf("Worker: receive Coordinator's reply %v\n", reply) switch reply.TaskType { case MAP: mapTask(mapf, &reply, id) case REDUCE: reduceTask(reducef, &reply, id) case WAIT: time.Sleep(1 * time.Second) case COMPLETE: log.Printf("MR Task finished! Worker exit.\n") return } lastTaskType = reply.TaskType lastTaskIndex = reply.TaskIndex } }
func mapTask(mapf func(string, string) []KeyValue, reply *Reply, workerId string) { filename := reply.Filename log.Printf("Worker-%s get a map task, read file content from: %v\n", workerId, filename) file, err := os.Open(filename) if err != nil { log.Fatalf("Map Task: cannot open %v", filename) } content, err := ioutil.ReadAll(file) if err != nil { log.Fatalf("Map Task: cannot read %v", filename) } err = file.Close() if err != nil { log.Fatalf("Map Task: cannot close %v", filename) } kva := mapf(filename, string(content)) buckets := make(map[int][]KeyValue) for _, kv := range kva { index := ihash(kv.Key) % reply.NReduce buckets[index] = append(buckets[index], kv) } for i := 0; i < reply.NReduce; i++ { ofile, _ := os.Create(TempMapOutFile(workerId, reply.TaskIndex, i)) for _, kv := range buckets[i] { _, err := fmt.Fprintf(ofile, "%v\t%v\n", kv.Key, kv.Value) if err != nil { log.Printf("Map Task: can't write some data into file " + ofile.Name()) } } err := ofile.Close() if err != nil { log.Printf("Map Task: can't close file " + ofile.Name()) } } }
func reduceTask(reducef func(string, []string) string, reply *Reply, workerId string) { var lines []string for i := 0; i < reply.NMap; i++ { filename := FinalMapOutFile(i, reply.TaskIndex) file, err := os.Open(filename) if err != nil { log.Fatalf("Reduce Task: cannot read %v", filename) } content, err := ioutil.ReadAll(file) if err != nil { log.Fatalf("Reduce Task: cannot read %v", filename) } err = file.Close() if err != nil { log.Fatalf("Reduce Task: cannot close %v", filename) } lines = append(lines, strings.Split(string(content), "\n")...) } var kva []KeyValue for _, line := range lines { if strings.TrimSpace(line) == "" { continue } seg := strings.Split(line, "\t") kva = append(kva, KeyValue{Key: seg[0], Value: seg[1]}) } sort.Sort(ByKey(kva)) ofile, _ := os.Create(TempReduceOutFile(workerId, reply.TaskIndex)) i := 0 for i < len(kva) { j := i + 1 for j < len(kva) && kva[j].Key == kva[i].Key { j++ } var values []string for k := i; k < j; k++ { values = append(values, kva[k].Value) } output := reducef(kva[i].Key, values) _, err := fmt.Fprintf(ofile, "%v %v\n", kva[i].Key, output) if err != nil { log.Printf("Map Task: can't write some data into file " + ofile.Name()) } i = j } err := ofile.Close() if err != nil { log.Printf("Map Task: can't close file " + ofile.Name()) } }
|