-
Notifications
You must be signed in to change notification settings - Fork 0
/
mr_coordinator.cc
212 lines (173 loc) · 5.3 KB
/
mr_coordinator.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
#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;
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();
bool assignTask(Task &newTask);
private:
vector <string> files;
vector <Task> mapTasks;
vector <Task> reduceTasks;
mutex mtx;
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.
Task newTask;
if (assignTask(newTask)) {
reply.index = newTask.index;
reply.tasktype = static_cast<mr_tasktype>(newTask.taskType);
reply.filename = getFile(newTask.index);
reply.nfiles = files.size();
} else {
reply.index = -1;
reply.tasktype = mr_tasktype::NONE;
reply.filename = "";
}
return mr_protocol::OK;
}
mr_protocol::status Coordinator::submitTask(int taskType, int index, bool &success) {
// Lab4 : Your code goes here.
std::unique_lock <std::mutex> lock(mtx);
if (taskType == mr_tasktype::MAP) {
mapTasks[index].isAssigned = false;
mapTasks[index].isCompleted = true;
completedMapCount++;
} else if (taskType == mr_tasktype::REDUCE) {
reduceTasks[index].isAssigned = false;
reduceTasks[index].isCompleted = true;
completedReduceCount++;
}
// judge if all task are completed
if (completedMapCount >= (long) mapTasks.size() && completedReduceCount >= (long) reduceTasks.size()) {
isFinished = true;
}
success = true;
return mr_protocol::OK;
}
string Coordinator::getFile(int index) {
std::unique_lock <std::mutex> lock(mtx);
string file = this->files[index];
return file;
}
bool Coordinator::isFinishedMap() {
bool isFinished = false;
std::unique_lock <std::mutex> lock(mtx);
if (this->completedMapCount >= long(this->mapTasks.size())) {
isFinished = true;
}
return isFinished;
}
bool Coordinator::isFinishedReduce() {
bool isFinished = false;
std::unique_lock <std::mutex> lock(mtx);
if (this->completedReduceCount >= long(this->reduceTasks.size())) {
isFinished = true;
}
return isFinished;
}
bool Coordinator::assignTask(Task &newTask) {
newTask.taskType = mr_tasktype::NONE;
std::unique_lock <std::mutex> lock(mtx);
if (completedMapCount < (long) mapTasks.size()) {
for (auto &mapTask: mapTasks) {
if (!mapTask.isAssigned && !mapTask.isCompleted) {
newTask = mapTask;
mapTask.isAssigned = true;
return true;
}
}
} else if (completedReduceCount < (long) reduceTasks.size()) {
for (auto &reduceTask: reduceTasks) {
if (!reduceTask.isAssigned && !reduceTask.isCompleted) {
newTask = reduceTask;
reduceTask.isAssigned = true;
return true;
}
}
}
return false;
}
//
// mr_coordinator calls Done() periodically to find out
// if the entire job has finished.
//
bool Coordinator::Done() {
bool r = false;
std::unique_lock <std::mutex> lock(mtx);
r = this->isFinished;
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();
for (int i = 0; i < filesize; i++) {
this->mapTasks.push_back(Task{mr_tasktype::MAP, false, false, i});
}
for (int i = 0; i < nReduce; 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
//
server.reg(mr_protocol::asktask, &c, &Coordinator::askTask);
server.reg(mr_protocol::submittask, &c, &Coordinator::submitTask);
while (!c.Done()) {
sleep(1);
}
return 0;
}