-
Notifications
You must be signed in to change notification settings - Fork 1
/
jobs.c
422 lines (381 loc) · 9.81 KB
/
jobs.c
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
void init_queue(queue *Q) {
Q->head = NULL;
Q->tail = NULL;
Q->active_jobs = 0;
pthread_mutex_init(&(Q->active_jobs_lock), NULL);
pthread_mutex_init(&(Q->head_lock), NULL);
pthread_mutex_init(&(Q->tail_lock), NULL);
}
job *get_local_job() { //dequeue local job
job_list_node *current_node;
current_node = my_queue->head;
while (current_node && current_node->entry && current_node->entry->status != READY) {
current_node = current_node->next;
}
if(current_node && current_node->entry) {
pthread_mutex_lock(&(current_node->lock));
if(current_node->entry->status == READY) {
current_node->entry->status = RUNNING;
pthread_mutex_unlock(&(current_node->lock));
update_job_count(my_queue, -1);
return current_node->entry;
}
pthread_mutex_unlock(&(current_node->lock));
}
return NULL;
}
void print_job_queue(queue *Q) {
job_list_node *runner;
runner = Q->head;
printf("(%d active):\n", Q->active_jobs);
printf("%-10s %-5s %-5s\n",
"name", "id", "hash");
while(runner) {
printf("%-10s | %-5d | %-5d", runner->entry->name, runner->entry->id,
hash(runner->entry->name, runner->entry->id));
if(runner == Q->head) {
printf(" (Head)");
}
if(runner == Q->tail) {
printf(" (Tail)");
}
printf("\n");
runner = runner->next;
}
}
int redistribute_jobs(queue *Q) {
#ifdef GREEDY
return 0;
#endif
int count = 0;
job_list_node *prev, *runner = Q->head;
host_list_node *dest;
while(runner != NULL) {
#ifdef VERBOSE2
print_job_queue(Q);
#endif
prev = runner;
runner = runner->next;
dest = determine_ownership(prev->entry);
if(dest != my_host) {
remove_job(prev, my_queue);
transfer_job(dest->host, prev->entry);
if(prev->entry->status == READY) {
count++;
update_job_count(Q, -1);
}
free_job_node(prev);
}
}
return count;
}
void update_q_host_failed () {
job_list_node *prev, *found, *runner = backup_queue->head;
host_list_node *dest;
job *temp;
while(runner != NULL) {
prev = runner;
runner = runner->next;
dest = determine_ownership(prev->entry);
if(dest == my_host) {
remove_job(prev, backup_queue);
if(contains(prev->entry->id, my_queue)) {
free_job_node(prev);
} else {
add_node_to_queue(prev, my_queue);
}
}
}
}
void free_queue(queue *Q) {
job_list_node *runner = Q->head;
if(runner) {
while(runner->next) {
if(runner->prev) {
free_job_node(runner->prev);
}
runner = runner->next;
}
free_job_node(runner);
}
pthread_mutex_destroy(&(Q->active_jobs_lock));
pthread_mutex_destroy(&(Q->head_lock));
pthread_mutex_destroy(&(Q->tail_lock));
free(Q);
}
void free_job_node(job_list_node *item) {
free(item->entry);
pthread_mutex_destroy(&(item->lock));
free(item);
}
int remove_job(job_list_node *item, queue *list) {
if(item->prev) {
pthread_mutex_lock(&(item->prev->lock));
} else {
pthread_mutex_lock(&(list->head_lock));
}
pthread_mutex_lock(&(item->lock));
if(item->next) {
pthread_mutex_lock(&(item->next->lock));
} else {
pthread_mutex_lock(&(list->tail_lock));
}
if(item->prev) {
item->prev->next = item->next;
} else {
list->head = item->next;
}
if(item->next) {
item->next->prev = item->prev;
} else {
list->tail = item->prev;
}
if(item->prev) {
pthread_mutex_unlock(&(item->prev->lock));
} else {
pthread_mutex_unlock(&(list->head_lock));
}
pthread_mutex_unlock(&(item->lock));
if(item->next) {
pthread_mutex_unlock(&(item->next->lock));
} else {
pthread_mutex_unlock(&(list->tail_lock));
}
update_job_count(list, -1);
}
int transfer_job(host_port *host, job *to_send) {
int connection, err;
err = make_connection_with(host->ip, host->port, &connection);
if (err < OKAY) {
handle_failure(host, 1);
return FAILURE;
}
err = TRANSFER_JOB;
do_rpc(&err);
err = safe_send(connection, to_send, sizeof(job));
if (err < OKAY) {
problem("Transfer Job failed\n");
return FAILURE;
}
close(connection);
return 0;
}
void replicate_my_jobs() {
job_list_node *runner = my_queue->head;
while(runner) {
replicate_job(runner->entry);
runner = runner->next;
}
}
int replicate_job(job *to_send) {
int connection, err;
err = make_connection_with(my_host->next->host->ip, my_host->next->host->port, &connection);
if (err < OKAY) {
handle_failure(my_host->next->host, 1);
return FAILURE;
}
err = RECEIVE_JOB_COPY;
do_rpc(&err);
err = safe_send(connection, to_send, sizeof(job));
if (err < OKAY) {
problem("Replicate Job failed\n");
return FAILURE;
}
return 0;
}
int send_meta_data(job *ajob) {
ajob->status = READY; //needs to be changed once we add dependencies
host_list_node *dest = determine_ownership(ajob);
#ifdef GREEDY
add_to_active_queue(ajob);
#else
if(dest == my_host) {
add_to_queue(ajob, my_queue);
if(my_host->next != my_host)
replicate_job(ajob);
} else {
transfer_job(dest->host, ajob);
free(ajob);
}
#endif
}
host_list_node *determine_ownership(job *ajob) {
char buffer[BUFFER_SIZE];
int job_hash;
host_list_node *runner;
job_hash = hash(ajob->name, ajob->id);
#ifdef VERBOSE2
printf("%s, %d hashes to %d\n", ajob->name, ajob->id, job_hash);
#endif
runner = server_list->head;
do {
runner = runner->next;
} while(runner->host->location <= job_hash && runner->host->location != 0);
#ifdef VERBOSE2
printf("So it belongs to %s\n", runner->host->ip);
#endif
return runner;
}
int write_files(job *ajob, int num_files, data_size *files) {
char buffer[BUFFER_SIZE], back[BUFFER_SIZE];
FILE *temp;
int i;
sprintf(buffer,"./jobs/%d/", ajob->id);
if(mkdir(buffer, S_IRWXU)) {
if(errno == EEXIST) {
#ifdef VERBOSE2
problem("directory already exists...\n");
#endif
} else {
problem("mkdir failed with %d\n", errno);
}
}
for(i = 0; i < num_files; i++) {
printf("%s\n", files[i].name);
sprintf(buffer,"jobs/%d/%s", ajob->id, files[i].name);
temp = NULL;
temp = fopen(buffer, "w");
if(temp) {
fwrite(files[i].data, files[i].size, 1, temp);
} else {
problem("failed to open file %s, errno: %d\n", files[i].name, errno);
}
fclose(temp);
//
char mode[] = "0777";
int i;
i = strtol(mode, 0, 8);
if (chmod (buffer,i) < 0) {
problem("chmod failed");
}
}
}
job *get_job_for_runner(){
#ifdef NO_DEQUEUE
return NULL;
#endif
job *to_return;
#ifdef RUN_LOCAL
if(to_return = get_local_job()) {
return to_return;
}
#endif
if(to_return = get_remote_job()) {
return to_return;
}
return NULL;
}
job *get_remote_job() {
int err;
int status;
int connection;
host_port *server;
job *location = NULL;
server = find_job_server();
if(!server || server == my_host->host) {
//find_job_server only fails when no one has any jobs
return NULL;
}
err = make_connection_with(server->ip, server->port, &connection);
if (err < OKAY) {
handle_failure(server, 1);
return NULL;
}
err = SERVE_JOB;
do_rpc(&err);
status = FAILURE;
err = safe_recv(connection, &status, sizeof(int)); //find out if they have a job
if(err < OKAY) {
close(connection);
return NULL;
}
if(status < 0) {
//set stuff up so we can try again because they didnt have a job
server->jobs = 0;
close(connection);
return NULL;
}
location = malloc(sizeof(job));
err = safe_recv(connection, location, sizeof(job));
#ifdef VERBOSE2
printf("Received job %s, %d, from %s\n", (location)->name, (location)->id, server->ip);
#endif
close(connection);
return location;
}
host_port *find_job_server() { //Finds the host who we believe to have the most jobs
host_list_node *n;
host_port *p;
n = server_list->head;
p = n->host;
do {
if (p->jobs < n->host->jobs) {
p = n->host;
}
n = n-> next;
} while(n != server_list->head);
if(p->jobs) {
return p;
} else {
return NULL;
}
}
void update_job_count(queue *Q, int update) {
pthread_mutex_lock(&(Q->active_jobs_lock));
Q->active_jobs = Q->active_jobs + update;
#ifdef VERBOSE2
printfl("I have %d jobs", Q->active_jobs);
#endif
pthread_mutex_unlock(&(Q->active_jobs_lock));
}
void add_to_queue(job *addJob, queue *Q) {
job_list_node *item = (job_list_node *)malloc(sizeof(job_list_node));
pthread_mutex_init(&(item->lock), NULL);
item->next = NULL;
item->entry = addJob;
add_node_to_queue(item, Q);
}
job_list_node *contains(unsigned int id, queue *Q) { //returns the node with its lock acquired
//threadsafe
//may not need to be threadsafe
job_list_node *runner;
pthread_mutex_t *last_lock;
last_lock = (&(Q->head_lock));
pthread_mutex_lock(last_lock);
runner = Q->head;
while(runner) {
pthread_mutex_lock(&(runner->lock));
if(runner->entry->id == id) {
pthread_mutex_unlock(last_lock);
return runner;
}
pthread_mutex_unlock(last_lock);
last_lock = &(runner->lock);
runner = runner->next;
}
pthread_mutex_unlock(last_lock);
return NULL;
}
void add_node_to_queue(job_list_node *item, queue *Q) {
pthread_mutex_lock(&(item->lock));
pthread_mutex_lock(&(Q->tail_lock)); //We only need to acquire this lock because we only want to prevent other
//additions and removals, i.e. we need the tail to remain who it is until we are done.
if(Q->tail) {
item->prev = Q->tail;
Q->tail->next = item;
Q->tail = item;
} else {
item->prev = NULL;
Q->tail = item;
//Again, it may seem that we want to acquire the head_lock, but we don't need to since the only place the
//head will be changed when the list is empty is with an addition
Q->head = item;
}
pthread_mutex_unlock(&(Q->tail_lock));
pthread_mutex_unlock(&(item->lock));
update_job_count(Q, 1);
}
//Not currently in use
int inform_of_completion(job *completed) {
return OKAY;
}