-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmr_coordinator.cc
More file actions
230 lines (197 loc) · 5.58 KB
/
mr_coordinator.cc
File metadata and controls
230 lines (197 loc) · 5.58 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
#include <string>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <string>
#include <vector>
#include <mutex>
#include "mr_protocol.h"
#include "rpc.h"
using namespace std;
#define COO_LOG(fmt, args...) \
do { \
} while (0);
//#define COO_LOG(fmt, args...) \
// do { \
// printf("[MR_COORDINATOR_LOG][%s:%d:%s] " fmt "\n", __FILE__, __LINE__, __FUNCTION__ , ##args); \
// } while (0);
struct Task {
int taskType; // should be either Mapper or Reducer
bool isAssigned; // has been assigned to a worker
bool isCompleted; // has been finised by a worker
int index; // index to the file
};
class Coordinator {
public:
Coordinator(const vector<string> &files, int nReduce);
mr_protocol::status askTask(int, mr_protocol::AskTaskResponse &reply);
mr_protocol::status submitTask(int taskType, int index, bool &success);
bool isFinishedMap();
bool isFinishedReduce();
bool Done();
private:
vector<string> files;
vector<Task> mapTasks;
vector<Task> reduceTasks;
mutex mtx; // A big lock to protect the whole data structure
long completedMapCount;
long completedReduceCount;
bool isFinished;
string getFile(int index);
};
// Your code here -- RPC handlers for the worker to call.
mr_protocol::status Coordinator::askTask(int, mr_protocol::AskTaskResponse &reply) {
// Lab4 : Your code goes here.
if (!isFinishedMap()) {
mtx.lock();
reply.tasktype = mr_tasktype::NONE;
// map not finished
for (auto &task : mapTasks) {
if(!task.isAssigned){
reply.tasktype = mr_tasktype::MAP;
reply.index = task.index;
reply.filename = files[task.index];
task.isAssigned = true;
COO_LOG("assign mapTask index %d", task.index)
mtx.unlock();
return mr_protocol::OK;
}
}
mtx.unlock();
} else if (!isFinishedReduce()) {
mtx.lock();
reply.tasktype = mr_tasktype::NONE;
// map finished, reduce not finished
for (auto &task : reduceTasks) {
if(!task.isAssigned){
reply.tasktype = mr_tasktype::REDUCE;
reply.index = task.index;
task.isAssigned = true;
COO_LOG("assign reduceTask index %d", task.index)
mtx.unlock();
return mr_protocol::OK;
}
}
mtx.unlock();
} else {
// all finished
reply.tasktype = mr_tasktype::NONE;
}
return mr_protocol::OK;
}
mr_protocol::status Coordinator::submitTask(int taskType, int index, bool &success) {
// Lab4 : Your code goes here.
mtx.lock();
switch (taskType) {
case mr_tasktype::MAP: {
++completedMapCount;
mapTasks[index].isCompleted = true;
success = true;
break;
}
case mr_tasktype::REDUCE: {
++completedReduceCount;
reduceTasks[index].isCompleted = true;
success = true;
if (this->completedReduceCount >= long(this->reduceTasks.size())) {
isFinished = true;
}
break;
}
case mr_tasktype::NONE: {
success = true;
break;
}
}
mtx.unlock();
return mr_protocol::OK;
}
string Coordinator::getFile(int index) {
this->mtx.lock();
string file = this->files[index];
this->mtx.unlock();
return file;
}
bool Coordinator::isFinishedMap() {
bool isFinished = false;
this->mtx.lock();
if (this->completedMapCount >= long(this->mapTasks.size())) {
isFinished = true;
}
this->mtx.unlock();
return isFinished;
}
bool Coordinator::isFinishedReduce() {
bool isFinished = false;
this->mtx.lock();
if (this->completedReduceCount >= long(this->reduceTasks.size())) {
isFinished = true;
}
this->mtx.unlock();
return isFinished;
}
//
// mr_coordinator calls Done() periodically to find out
// if the entire job has finished.
//
bool Coordinator::Done() {
bool r = false;
this->mtx.lock();
r = this->isFinished;
this->mtx.unlock();
return r;
}
//
// create a Coordinator.
// nReduce is the number of reduce tasks to use.
//
Coordinator::Coordinator(const vector<string> &files, int nReduce) {
this->files = files;
this->isFinished = false;
this->completedMapCount = 0;
this->completedReduceCount = 0;
int filesize = files.size();
COO_LOG("filesize %d", filesize)
for (int i = 0; i < filesize; i++) {
COO_LOG("mapTask %d init", i)
this->mapTasks.push_back(Task{mr_tasktype::MAP, false, false, i});
}
for (int i = 0; i < nReduce; i++) {
COO_LOG("reduceTask %d init", i)
this->reduceTasks.push_back(Task{mr_tasktype::REDUCE, false, false, i});
}
}
int main(int argc, char *argv[]) {
int count = 0;
if (argc < 3) {
fprintf(stderr, "Usage: %s <port-listen> <inputfiles>...\n", argv[0]);
exit(1);
}
char *port_listen = argv[1];
setvbuf(stdout, NULL, _IONBF, 0);
char *count_env = getenv("RPC_COUNT");
if (count_env != NULL) {
count = atoi(count_env);
}
vector<string> files;
char **p = &argv[2];
while (*p) {
files.push_back(string(*p));
++p;
}
rpcs server(atoi(port_listen), count);
Coordinator c(files, REDUCER_COUNT);
//
// Lab4: Your code here.
// Hints: Register "askTask" and "submitTask" as RPC handlers here
//
printf("mr coordinator started at port %d\n", atoi(port_listen));
server.reg(mr_protocol::rpc_numbers::asktask, &c, &Coordinator::askTask);
server.reg(mr_protocol::rpc_numbers::submittask, &c, &Coordinator::submitTask);
while (!c.Done()) {
sleep(1);
}
return 0;
}