-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmr_worker.cc
252 lines (219 loc) · 6.35 KB
/
mr_worker.cc
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <mutex>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include "rpc.h"
#include "mr_protocol.h"
using namespace std;
struct KeyVal {
KeyVal(string key_, string val_) : key(key_), val(val_){}
string key;
string val;
};
int stringToIntHash(const string &keyStr){
int size = keyStr.size();
int hash = 1;
int p = 1999, mod = 101;
for (int i = 0; i < size - 1; ++i) {
hash = (hash * p + i + 1) % mod;
}
return hash % REDUCER_COUNT;
}
//
// The map function is called once for each file of input. The first
// argument is the name of the input file, and the second is the
// file's complete contents. You should ignore the input file name,
// and look only at the contents argument. The return value is a slice
// of key/value pairs.
//
vector<KeyVal> Map(const string &filename, const string &content)
{
// Copy your code from mr_sequential.cc here.
// Your code goes here
// Hints: split contents into an array of words.
vector<KeyVal> word_to_count;
int begin = 0, end = 0;
string str = "";
while (content[end] != '\0'){
if(isalpha(content[end])){
end++;
} else if(end > begin){
str = content.substr(begin, end - begin);
word_to_count.emplace_back(KeyVal(str, "1"));
end ++;
begin = end;
} else{ // illegal character or space, skip it
while (!isalpha(content[end]) && content[end] != '\0'){
end++;
}
begin = end;
}
}
// last string
if(end > begin){
str = content.substr(begin, end - begin);
word_to_count.emplace_back(KeyVal(str, "1"));
}
return word_to_count;
}
//
// The reduce function is called once for each key generated by the
// map tasks, with a list of all the values created for that key by
// any map task.
//
string Reduce(const string &key, const vector < string > &values)
{
// Copy your code from mr_sequential.cc here.
size_t count = 0;
for(auto value : values){
count += atoll(value.c_str());
}
return to_string(count);
}
typedef vector<KeyVal> (*MAPF)(const string &key, const string &value);
typedef string (*REDUCEF)(const string &key, const vector<string> &values);
class Worker {
public:
Worker(const string &dst, const string &dir, MAPF mf, REDUCEF rf);
void doWork();
private:
void doMap(int index, const vector<string> &filenames);
void doReduce(int index, int nfiles);
void doSubmit(mr_tasktype taskType, int index);
mutex mtx;
int id;
bool isworking;
rpcc *cl;
std::string basedir;
MAPF mapf;
REDUCEF reducef;
};
Worker::Worker(const string &dst, const string &dir, MAPF mf, REDUCEF rf)
{
this->basedir = dir;
this->mapf = mf;
this->reducef = rf;
this->isworking = false;
sockaddr_in dstsock;
make_sockaddr(dst.c_str(), &dstsock);
this->cl = new rpcc(dstsock);
if (this->cl->bind() < 0) {
printf("mr worker: call bind error\n");
}
}
void Worker::doMap(int index, const vector<string> &filenames)
{
// Lab4: Your code goes here.
isworking = true;
string intermediatePrefix;
//this->basedir
intermediatePrefix = basedir + "mr-" + to_string(index) + "-";
string content;
string filename = filenames.front();
ifstream file(filename);
ostringstream tmp;
tmp << file.rdbuf();
content = tmp.str();
vector <KeyVal> keyVals = Map(filename, content);
vector <string> contents(REDUCER_COUNT);
for (const KeyVal &keyVal : keyVals) {
int reducerId = stringToIntHash(keyVal.key);
contents[reducerId] += keyVal.key + ' ' + keyVal.val + '\n';
}
for (int i = 0; i < REDUCER_COUNT; ++i) {
const string &content = contents[i];
if (!content.empty()) {
string intermediateFilepath = intermediatePrefix + to_string(i);
ofstream file(intermediateFilepath, ios::out);
file << content;
file.close();
}
}
file.close();
}
void Worker::doReduce(int index, int nfiles)
{
// Lab4: Your code goes here.
string filepath;
unordered_map<string, unsigned long long> wordFreqs;
for (int i = 0; i < nfiles; ++i) {
filepath = basedir + "mr-" + to_string(i) + '-' + to_string(index);
ifstream file(filepath, ios::in);
if (!file.is_open()) {
continue;
}
string key, value;
while (file >> key >> value)
wordFreqs[key] += atoll(value.c_str());
file.close();
}
string content;
for (const pair<string, unsigned long long> &keyVal : wordFreqs)
content += keyVal.first + ' ' + to_string(keyVal.second) + '\n';
ofstream mrOut(basedir + "mr-out", ios::out | ios::app);
mrOut << content << endl;
mrOut.close();
isworking = true;
}
void Worker::doSubmit(mr_tasktype taskType, int index)
{
bool b;
mr_protocol::status ret = this->cl->call(mr_protocol::submittask, taskType, index, b);
if (ret != mr_protocol::OK) {
fprintf(stderr, "submit task failed\n");
exit(-1);
}
isworking = false;
}
void Worker::doWork()
{
for (;;) {
//
// Lab4: Your code goes here.
// Hints: send asktask RPC call to coordinator
// if mr_tasktype::MAP, then doMap and doSubmit
// if mr_tasktype::REDUCE, then doReduce and doSubmit
// if mr_tasktype::NONE, meaning currently no work is needed, then sleep
//
mr_protocol::AskTaskResponse res;
if(!this->isworking){
cl->call(mr_protocol::asktask, id, res);
}
switch (res.tasktype) {
case mr_tasktype::MAP:{
doMap(res.index, vector<string>({res.filename}));
doSubmit(mr_tasktype::MAP, res.index);
break;
}
case mr_tasktype::REDUCE:{
doReduce(res.index, res.nfiles);
doSubmit(mr_tasktype::REDUCE, res.index);
break;
}
case mr_tasktype::NONE:{
sleep(1);
}
}
}
}
int main(int argc, char **argv)
{
if (argc != 3) {
fprintf(stderr, "Usage: %s <coordinator_listen_port> <intermediate_file_dir> \n", argv[0]);
exit(1);
}
MAPF mf = Map;
REDUCEF rf = Reduce;
Worker w(argv[1], argv[2], mf, rf);
w.doWork();
return 0;
}