Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
234 lines (166 loc) · 4.76 KB
/
Copy pathmain.go
File metadata and controls
234 lines (166 loc) · 4.76 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package main
import (
"github.com/labstack/echo"
"net"
"encoding/json"
"strings"
"bytes"
"encoding/gob"
"net/http"
"os"
"math"
"io/ioutil"
"fmt"
"sync"
)
func main() {
t := os.Getenv("TYPE")
switch t {
case "MAP":
mapper()
case "MASTER":
master()
case "REDUCE":
reducer()
}
}
func min(a, b int) int { if a <= b { return a }; return b }
func master() {
e := echo.New()
var client = &http.Client{
}
e.GET("/compute", func(c echo.Context) error {
text := c.QueryParam("text")
words := strings.Split(text, " ")
// MAPPING
mapperHost := os.Getenv("MAPPER_HOST")
var mapperIps []string
ips, _ := net.LookupIP(mapperHost)
for _, ip := range ips {
mapperIps = append(mapperIps, ip.String())
}
mapSplitCount := int(math.Ceil(float64(len(words)) / float64(len(mapperIps))))
var mapSplits = map[string][]string{}
for idx, mapperIp := range mapperIps {
if idx*mapSplitCount >= len(words) { break }
mapSplits[mapperIp] = words[idx*mapSplitCount:min(idx*mapSplitCount+mapSplitCount, len(words))]
}
var mapping = map[string]map[string]int{}
var wgm sync.WaitGroup
wgm.Add(len(mapSplits))
for host, split := range mapSplits {
go func(host string, split []string) {
defer wgm.Done()
req, _ := http.NewRequest("GET", fmt.Sprintf("http://%s:%s/map", host, os.Getenv("MAPPER_PORT")), nil)
q := req.URL.Query()
q.Add("str", strings.Join(split, " "))
req.URL.RawQuery = q.Encode()
res, _ := client.Do(req)
body, _ := ioutil.ReadAll(res.Body)
_ = res.Body.Close()
buf := bytes.NewBuffer(body)
var decodedMap map[string]int
decoder := gob.NewDecoder(buf)
_ = decoder.Decode(&decodedMap)
mapping[host] = decodedMap
}(host, split)
}
wgm.Wait()
//SHUFFLING
var shuffling = map[string][]int{}
for _, host := range mapping {
for word, count := range host {
shuffling[word] = append(shuffling[word], count)
}
}
//REDUCING
reducerHost := os.Getenv("REDUCER_HOST")
var reducerIps []string
ips, _ = net.LookupIP(reducerHost)
for _, ip := range ips {
reducerIps = append(reducerIps, ip.String())
}
var shuffleWords []string
for word := range shuffling {
shuffleWords = append(shuffleWords, word)
}
reduceSplitCount := int(math.Ceil(float64(len(shuffleWords)) / float64(len(reducerIps))))
var reduceSplits = map[string]map[string][]int{}
for idx, reducerIp := range reducerIps {
if idx*reduceSplitCount >= len(shuffleWords) { break }
reduceWords := shuffleWords[idx*reduceSplitCount:min(idx*reduceSplitCount+reduceSplitCount, len(shuffleWords))]
reduceSplits[reducerIp] = map[string][]int{}
for _, reduceKey := range reduceWords {
reduceSplits[reducerIp][reduceKey] = shuffling[reduceKey]
}
}
var wgr sync.WaitGroup
wgr.Add(len(reduceSplits))
var reducing = map[string]map[string]int{}
for host, split := range reduceSplits {
go func (host string, split map[string][]int) {
defer wgr.Done()
req, _ := http.NewRequest("GET", fmt.Sprintf("http://%s:%s/reduce", host, os.Getenv("REDUCER_PORT")), nil)
buf := new(bytes.Buffer)
encoder := gob.NewEncoder(buf)
_ = encoder.Encode(split)
q := req.URL.Query()
q.Add("body", string(buf.Bytes()))
req.URL.RawQuery = q.Encode()
res, _ := client.Do(req)
body, _ := ioutil.ReadAll(res.Body)
_ = res.Body.Close()
buf = bytes.NewBuffer(body)
var decodedReduce = map[string]int{}
decoder := gob.NewDecoder(buf)
_ = decoder.Decode(&decodedReduce)
reducing[host] = decodedReduce
}(host, split)
}
wgr.Wait()
return json.NewEncoder(c.Response()).Encode(&reducing)
})
e.Logger.Fatal(e.Start(":8080"))
}
func mapper() {
e := echo.New()
e.GET("/map", func(c echo.Context) error {
str := c.QueryParam("str")
words := strings.Split(str, " ")
mapping := map[string]int{}
for _, word := range words {
if _, prs := mapping[word]; prs {
mapping[word] += 1
} else {
mapping[word] = 1
}
}
buf := new(bytes.Buffer)
encoder := gob.NewEncoder(buf)
_ = encoder.Encode(mapping)
return c.Blob(http.StatusOK, "application/octet-stream", buf.Bytes())
})
e.Logger.Fatal(e.Start(":8080"))
}
func reducer() {
e := echo.New()
e.GET("/reduce", func(c echo.Context) error {
body := c.QueryParam("body")
buf := bytes.NewBuffer([]byte(body))
var reduceData = map[string][]int{}
decoder := gob.NewDecoder(buf)
_ = decoder.Decode(&reduceData)
var reducing = map[string]int{}
for key, value := range reduceData {
reducing[key] = 0
for _, count := range value {
reducing[key] += count
}
}
buf = new(bytes.Buffer)
encoder := gob.NewEncoder(buf)
_ = encoder.Encode(reducing)
return c.Blob(http.StatusOK, "application/octet-stream", buf.Bytes())
})
e.Logger.Fatal(e.Start(":8080"))
}