-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmaster.cpp
More file actions
374 lines (320 loc) · 8.64 KB
/
master.cpp
File metadata and controls
374 lines (320 loc) · 8.64 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include "master.h"
#include <string.h>
#include <pthread.h>
#include "csapp.h"
#include <iostream>
#include <string>
//using namespace std;
extern NodeInfo FILE_SYSTEM;
// strategy classes, no need to define in their own file. In fact it's just a function for Master.
class Print : public Strategy{
public:
std::vector<Job> * jobs;
virtual void invoke(int id , Worker &w ){
printf("worker is %s, %s , its ID is %d\n", w.info.IP, w.info.port,w.workerID);
for(int i=0;i< w.jobs.size();i++){
printf("job: %d start is %d, end is %d \n ", jobs->at(w.jobs[i]).jobID, jobs->at(w.jobs[i]).start, jobs->at(w.jobs[i]).end );
}
}
};
class Done : public Strategy{
public:
virtual void invoke(int id , Worker &w ){
char buf[5] = "DONE";
int fd=Node::connectTo(w.info);
Rio_writep(fd, buf, strlen(buf));
Close(fd);
w.jobs.clear();
}
};
class Increase : public TimerProcess{
private:
Master * master;
public:
Increase(Master *m){
master=m;
}
virtual void invoke(){
pthread_mutex_lock(&master->myMutex);
std::map<int,Worker>::iterator it=master->workers.begin();
while(it!=master->workers.end()){
if(!it->second.alive){
it++;
continue;
}
if(it->second.absentTime>=2){
it->second.alive=false;
printf("%d is offline\n",it->first);
Worker broken(it->second);
std::map<int,Worker>::iterator toErase = it;
it++;
master->workers.erase(toErase);
master->reAssign(broken);
}
else{
it->second.absentTime++;
it++;
}
}
pthread_mutex_unlock(&master->myMutex);
}
};
// functions of Master
int Master :: uniqueID =1;
Master::Master(int num, const char * port, const char * ip):Node(port,ip){
numReduce = num;
isDone=true;
pthread_mutex_init(&myMutex, NULL);
pthread_mutex_init(&checkComplete, NULL);
}
int Master::createWorker(const char * ip, const char * port, int state, int work){
pthread_mutex_lock(&myMutex);
Worker w;
strncpy(w.info.IP, ip,20);
strncpy(w.info.port, port,10);
w.workerID= uniqueID;
w.absentTime=0;
w.alive=true;
workers.insert(std::pair<int ,Worker> (w.workerID, w));
uniqueID++;
pthread_mutex_unlock(&myMutex);
return w.workerID;
}
void Master:: run(){
Node::run();
timerProcess = new Increase( this);
timerStart();
while(true){
std::string s;
std::cin>>s;
if(s=="p"){
Print p;
p.jobs=&jobs;
iterateWorkers(p);
}
else if(s=="q"){
return;
}
else{
if(!isDone){
std::cout<<"In processing, try again later!"<<std::endl;
continue;
}
int total = atoi(s.c_str());
assignWork(total);
}
}
}
void Master:: processRequest(rio_t & client, int clientfd){
char buf[MAXLINE];
int numBytes;
char * saveptr;
char * cmd;
numBytes = Rio_readlineb(&client, buf, MAXLINE);
cmd = strtok_r(buf, " \r\n",&saveptr);
if(cmd==NULL){
Close(clientfd);
return;
}
else if(strcmp(cmd,"JOIN")==0){
NodeInfo n= readNodeInfo(client);
int id=createWorker(n.IP,n.port,IDLE,NONE);
sprintf(buf,"%d",id);
Rio_writep(clientfd, buf, strlen(buf));
while(numBytes=Rio_readlineb(&client, buf, MAXLINE)>0){
printf("%s\n",buf);
cmd = strtok_r(buf, "*",&saveptr);
if(strcmp(cmd,"UPDATE")==0){
cmd = strtok_r(NULL, "*",&saveptr);
int workerID = atoi(cmd);
workers[workerID].absentTime=0;
printf("id %d is updating\n", workerID);
}
}
}
else if(strcmp(cmd, "COMPLETE")==0){
numBytes=Rio_readlineb(&client, buf, MAXLINE);
printf("buf is %s\n", buf);
cmd = strtok_r(buf, "*",&saveptr);
int jobID = atoi(cmd);
printf("id complete %s\n", cmd);
if(jobs[jobID].work==REDUCE){
reduceJobComplete(jobID);
}
}
Close(clientfd);
}
void Master:: assignWork(int total){
isDone=false;
int size = workers.size();
if(size<= numReduce){
std::cout<< "Not enough wokers. Please try again later"<<std::endl;
return;
}
int numMap = size - numReduce;
std::vector< std::vector<int> > temp;
for(int i=0;i<numReduce;i++){
temp.push_back(std::vector<int> ());
}
int interval = total/numMap;
int start =1 ;
int current=0;
int currentReduce=0;
for (std::map<int,Worker>::iterator it=workers.begin(); it!=workers.end(); it++,current++){
if(current< numMap){
assignMap(it->second.workerID,current, start, start+interval-1);
temp[(current)%numReduce].push_back(current);
start+=interval;
}
else if(current == numMap-1){
assignMap(it->second.workerID,current, start, total);
temp[(current)%numReduce].push_back(current);
}
else{
assignReduce(it->second.workerID,current, temp[currentReduce]);
currentReduce++;
}
}
}
bool Master:: assignMap(int workerID, int jobID, int start , int end){
Worker & w = (workers.find(workerID)->second);
Job j(jobID,MAP);
j.start = start;
j.end = end;
j.state = INPROCESS;
j.workerID = workerID;
jobs.push_back(j);
w.jobs.push_back(jobs.size()-1);
assert(jobs.size()-1 == jobID);
assignMap(w.info, start, end , jobID);
}
void Master:: assignMap(NodeInfo i, int start, int end, int jobID){
int fd =connectTo(i);
char buf[MAXLINE];
sprintf(buf,"MAP %d\n",jobID);
Rio_writep(fd, buf, strlen(buf));
sprintf(buf,"%d*%d\n",start,end);
Rio_writep(fd,buf,strlen(buf));
Close(fd);
}
bool Master:: assignReduce(int workerID, int jobID, std::vector<int> v ){
Worker & w = (workers.find(workerID)->second);
Job j(jobID,REDUCE);
j.state = INPROCESS;
j.workerID = workerID;
j.mapJobs = v;
jobs.push_back(j);
w.jobs.push_back(jobs.size()-1);
assignReduce(w.info,v,jobID);
}
void Master:: assignReduce(NodeInfo i , std::vector<int> v, int jobID){
int fd =connectTo(i);
char buf[MAXLINE];
sprintf(buf,"REDUCE %d\n", jobID);
Rio_writep(fd, buf, strlen(buf));
for(int i=0; i< v.size();i++){
Worker & w = workers[ jobs[v[i]].workerID ];
sprintf(buf,"%s*%s*%d\n",w.info.IP, w.info.port,v[i]);
Rio_writep(fd,buf,strlen(buf));
}
Close(fd);
}
void Master:: iterateWorkers(Strategy &s){
for (std::map<int,Worker>::iterator it=workers.begin(); it!=workers.end(); ++it){
s.invoke(it->first, it->second);
}
}
Master:: ~Master(){
pthread_mutex_destroy(&myMutex);
}
void Master:: reduceJobComplete(int id){
pthread_mutex_lock(&checkComplete);
jobs[id].state=COMPLETE;
for(int i=0;i<jobs.size();i++){
if(jobs[i].work==REDUCE&&jobs[i].state!=COMPLETE){
pthread_mutex_unlock(&checkComplete);
return;
}
}
done();
pthread_mutex_unlock(&checkComplete);
}
void Master:: done(){
Done d;
iterateWorkers(d);
jobs.clear();
int fd = connectTo(FILE_SYSTEM);
char buf[5];
sprintf(buf,"ALL\n");
Rio_writep(fd, buf, strlen(buf));
int numBytes;
rio_t client;
Rio_readinitb( &client,fd);
printf("The track of the criminal is: \n");
while(numBytes=Rio_readlineb(&client, buf, MAXLINE)>0){
printf("%s\n",buf);
}
Close(fd);
isDone=true;
//output result send all to global file system and output;
}
void Master:: reAssign(Worker & broken){
int workerID = broken.workerID;
workers.erase(workerID);
int min = 10000;
Worker * nWorker = NULL;
for (std::map<int,Worker>::iterator it=workers.begin(); it!=workers.end(); ++it){
if(it->first == workerID)
continue;
else{
if(it->second.jobs.size()<min){
min=it->second.jobs.size();
nWorker = &it->second;
}
}
}
for(int i=0; i<broken.jobs.size();i++){
if(jobs[broken.jobs[i]].work==REDUCE&&jobs[broken.jobs[i]].state!=COMPLETE){
reAssignReduce(nWorker, jobs[broken.jobs[i]]);
}
}
for(int i=0; i<broken.jobs.size();i++){
if(jobs[broken.jobs[i]].work==MAP){
reAssignMap(nWorker, jobs[broken.jobs[i]]);
}
}
}
void Master:: reAssignReduce(Worker * w, Job & j){
j.workerID = w->workerID;
w->jobs.push_back(j.jobID);
assignReduce(w->info , j.mapJobs, j.jobID);
}
void Master:: reAssignMap(Worker * w, Job & j){
j.workerID = w->workerID;
w->jobs.push_back(j.jobID);
bool found =false;
//find who is responsible for this map,Modify;
for(int i=0;i<jobs.size();i++){
if(found)
break;
if(jobs[i].work==REDUCE){
for(int k=0; k< jobs[i].mapJobs.size();k++){
if(jobs[i].mapJobs[k]==j.jobID){
modify(workers[jobs[i].workerID].info, j.jobID, w->info );
found = true;
}
break;
}
}
}
assignMap(w->info, j.start, j.end, j.jobID);
}
void Master:: modify(NodeInfo reduceNode, int jobID , NodeInfo newNode){
int fd = connectTo(reduceNode);
char buf[MAXLINE];
sprintf(buf,"MODIFY *%d\n",jobID);
Rio_writep(fd, buf, strlen(buf));
sprintf(buf,"%s*%s\n",newNode.IP,newNode.port);
Rio_writep(fd, buf, strlen(buf));
Close(fd);
}