/
request.c
4447 lines (3581 loc) · 112 KB
/
request.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (C) 2001,2002,2003,2004,2005,2006 Jorge Daza Garcia-Blanes
// Copyright (C) 2010 Andreas Schroeder
//
// This file is part of DrQueue
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA
//
#include <stdio.h>
#include <signal.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifndef _WIN32
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/shm.h>
#else
#include <io.h>
#endif
#include "libdrqueue.h"
// ONGOING:
// * check r_r_uclimits
/* For the differences between data in big endian and little endian */
/* I transmit everything in network byte order */
int phantom[2];
void
handle_request_master (int sfd, struct database *wdb, int icomp, struct sockaddr_in *addr) {
struct request request;
if (!recv_request (sfd,&request)) {
log_auto (L_WARNING,"Error receiving request (handle_request_master)");
// Log the problem and finish with this handler process
exit(1);
}
// Update the time here, because it could not reach the end of the
// switch if the handler decides to exit this process. (all
// connection handling runs as different processes for every single
// connection. Master forked before accepting this connection and
// this is the child.)
if ((icomp != -1) && (request.who == SLAVE)) {
semaphore_lock (wdb->semid);
/* set the time of the last connection */
wdb->computer[icomp].lastconn = time(NULL);
semaphore_release (wdb->semid);
}
switch (request.type) {
case R_R_REGISTER:
log_auto (L_DEBUG,"Registration of new slave request");
icomp = handle_r_r_register (sfd,wdb,icomp,addr);
break;
case R_R_UCSTATUS:
log_auto (L_DEBUG,"Update computer status request");
handle_r_r_ucstatus (sfd,wdb,icomp);
break;
case R_R_REGISJOB:
log_auto (L_DEBUG,"Registration of new job request");
handle_r_r_regisjob (sfd,wdb);
request_all_slaves_job_available(wdb);
break;
case R_R_AVAILJOB:
log_auto (L_DEBUG,"Request available job");
handle_r_r_availjob (sfd,wdb,icomp);
break;
case R_R_TASKFINI:
log_auto (L_DEBUG,"Request task finished");
handle_r_r_taskfini (sfd,wdb,icomp);
request_all_slaves_job_available(wdb);
break;
case R_R_LISTJOBS:
log_auto (L_DEBUG,"Request list of jobs");
handle_r_r_listjobs (sfd,wdb,icomp);
break;
case R_R_LISTCOMP:
log_auto (L_DEBUG,"Request list of computers");
handle_r_r_listcomp (sfd,wdb,icomp);
break;
case R_R_DELETJOB:
log_auto (L_DEBUG,"Request job deletion");
handle_r_r_deletjob (sfd,wdb,icomp,&request);
break;
case R_R_STOPJOB:
log_auto (L_DEBUG,"Request job stop");
handle_r_r_stopjob (sfd,wdb,icomp,&request);
break;
case R_R_CONTJOB:
log_auto (L_DEBUG,"Request job continue");
handle_r_r_contjob (sfd,wdb,icomp,&request);
request_all_slaves_job_available(wdb);
break;
case R_R_HSTOPJOB:
log_auto (L_DEBUG,"Request job hard stop");
handle_r_r_hstopjob (sfd,wdb,icomp,&request);
break;
case R_R_RERUNJOB:
log_auto (L_DEBUG,"Request job rerun");
handle_r_r_rerunjob (sfd,wdb,icomp,&request);
break;
case R_R_JOBXFER:
log_auto (L_DEBUG,"Request job transfer");
handle_r_r_jobxfer (sfd,wdb,icomp,&request);
break;
case R_R_JOBXFERFI:
log_auto (L_DEBUG,"Request frame info transfer");
handle_r_r_jobxferfi (sfd,wdb,icomp,&request);
break;
case R_R_COMPXFER:
log_auto (L_DEBUG,"Request computer transfer");
handle_r_r_compxfer (sfd,wdb,icomp,&request);
break;
case R_R_JOBFWAIT:
log_auto (L_DEBUG,"Request job frame set to waiting"); // Requeue (do we need another function ?)
handle_r_r_jobfwait (sfd,wdb,icomp,&request);
break;
case R_R_JOBFKILL:
log_auto (L_DEBUG,"Request job frame kill");
handle_r_r_jobfkill (sfd,wdb,icomp,&request);
break;
case R_R_JOBFFINI:
log_auto (L_DEBUG,"Request job frame finished");
handle_r_r_jobffini (sfd,wdb,icomp,&request);
break;
case R_R_JOBFKFIN:
log_auto (L_DEBUG,"Request job frame kill and finished");
handle_r_r_jobfkfin (sfd,wdb,icomp,&request);
break;
case R_R_UCLIMITS:
log_auto (L_DEBUG,"Update computer limits request");
handle_r_r_uclimits (sfd,wdb,icomp,&request);
request_all_slaves_job_available(wdb);
break;
case R_R_SLAVEXIT:
log_auto (L_DEBUG,"Slave exiting");
handle_r_r_slavexit (sfd,wdb,icomp,&request);
break;
case R_R_JOBSESUP:
log_auto (L_DEBUG,"Update job SES");
handle_r_r_jobsesup (sfd,wdb,icomp,&request);
request_all_slaves_job_available(wdb);
break;
case R_R_JOBLNMCS:
log_auto (L_DEBUG,"Set job limits nmaxcpus");
handle_r_r_joblnmcs (sfd,wdb,icomp,&request);
request_all_slaves_job_available(wdb);
break;
case R_R_JOBLNMCCS:
log_auto (L_DEBUG,"Set job limits nmaxcpuscomputer");
handle_r_r_joblnmccs (sfd,wdb,icomp,&request);
request_all_slaves_job_available(wdb);
break;
case R_R_JOBPRIUP:
log_auto (L_DEBUG,"Update priority");
handle_r_r_jobpriup (sfd,wdb,icomp,&request);
request_all_slaves_job_available(wdb);
break;
case R_R_JOBFINFO:
log_auto (L_DEBUG,"Frame info");
handle_r_r_jobfinfo (sfd,wdb,icomp,&request);
break;
case R_R_JOBFRSTRQD:
log_auto (L_DEBUG,"Frame reset requeued");
handle_r_r_jobfrstrqd (sfd,wdb,icomp,&request);
break;
case R_R_JOBBLKHOST:
log_auto (L_DEBUG,"Block host add");
handle_r_r_jobblkhost (sfd,wdb,icomp,&request);
break;
case R_R_JOBDELBLKHOST:
log_auto (L_DEBUG,"Blocked host delete");
handle_r_r_jobdelblkhost (sfd,wdb,icomp,&request);
break;
case R_R_JOBLSTBLKHOST:
log_auto (L_DEBUG,"Blocked hosts list");
handle_r_r_joblstblkhost (sfd,wdb,icomp,&request);
break;
case R_R_JOBLMS:
log_auto (L_DEBUG,"Request job limit memory set");
handle_r_r_joblms (sfd,wdb,icomp,&request);
break;
case R_R_JOBLPS:
log_auto (L_DEBUG,"Request job limit pool set");
handle_r_r_joblps (sfd,wdb,icomp,&request);
break;
case R_R_JOBENVVARS:
log_auto (L_DEBUG,"Request job environment variables");
handle_r_r_jobenvvars (sfd,wdb,icomp,&request);
break;
case R_R_JOBBLKHOSTNAME:
log_auto (L_DEBUG,"Block host add by name");
handle_r_r_jobblkhostname (sfd,wdb,icomp,&request);
break;
case R_R_JOBUNBLKHOSTNAME:
log_auto (L_DEBUG,"Unblock host by name");
handle_r_r_jobunblkhostname (sfd,wdb,icomp,&request);
break;
case R_R_JOBNAME:
log_auto (L_DEBUG,"Requested job name");
handle_r_r_jobname (sfd,wdb,icomp,&request);
break;
default:
log_auto (L_WARNING,"Unknown request");
}
close (sfd);
// The handler process finishes gracefully
exit (0);
}
void
handle_request_slave (int sfd, struct slave_database *sdb) {
/* this function should only be called from the slave connection handler */
struct request request;
if (!recv_request (sfd,&request)) {
log_auto (L_ERROR,"handle_request_slave(): Error receiving request. (%s)",strerror(drerrno_system));
// Log the problem and finish with this handler process
exit(1);
}
switch (request.type) {
case RS_R_KILLTASK:
log_auto (L_DEBUG,"Request kill task");
handle_rs_r_killtask (sfd,sdb,&request);
break;
case RS_R_SETNMAXCPUS:
log_auto (L_DEBUG,"Request set limits maximum number of usable cpus");
handle_rs_r_setnmaxcpus (sfd,sdb,&request);
break;
case RS_R_SETENABLED:
log_auto (L_DEBUG,"Request set limits enabled");
handle_rs_r_setenabled (sfd,sdb,&request);
break;
case RS_R_SETMAXFREELOADCPU:
log_auto (L_DEBUG,"Request set limits maximum free load for cpu");
handle_rs_r_setmaxfreeloadcpu (sfd,sdb,&request);
break;
case RS_R_SETAUTOENABLE:
log_auto (L_DEBUG,"Request set autoenable info");
handle_rs_r_setautoenable (sfd,sdb,&request);
break;
case RS_R_JOBAVAILABLE:
log_auto (L_DEBUG,"Request master has an available job");
write(phantom[1],"D",1);
break;
case RS_R_LIMITSPOOLADD:
log_auto (L_DEBUG,"Request add pool");
handle_rs_r_limitspooladd (sfd,sdb,&request);
break;
case RS_R_LIMITSPOOLREMOVE:
log_auto (L_DEBUG,"Request remove pool");
handle_rs_r_limitspoolremove (sfd,sdb,&request);
break;
default:
log_auto (L_WARNING,"Unknown request received");
}
close (sfd);
// The handler process finishes gracefully
exit (0);
}
int
handle_r_r_register (int sfd, struct database *wdb, int icomp, struct sockaddr_in *addr) {
/* The master handles this type of requests */
struct request answer;
struct computer_hwinfo hwinfo;
int index = -1; /* CHECK THIS: the index in the computer is an uint32_t. */
char *name;
char *dot;
struct hostent *host;
log_auto (L_DEBUG3,"handle_r_r_register(): >Entering...");
if ((host = gethostbyaddr ((const void *)&addr->sin_addr.s_addr,sizeof (struct in_addr),AF_INET)) == NULL) {
log_auto (L_INFO,"handle_r_r_register(). Using IP address as host name because '%s' could not be resolved",inet_ntoa(addr->sin_addr));
name=inet_ntoa(addr->sin_addr);
} else {
if ((dot = strchr (host->h_name,'.')) != NULL)
*dot = '\0';
name = (char*)host->h_name;
}
semaphore_lock(wdb->semid); /* I put the lock here so no race condition can appear... */
if (icomp != -1) {
/* semaphore_release(wdb->semid); */
log_auto (L_WARNING,"Already registered computer requesting registration (%s)",name);
log_auto (L_WARNING,"Registering again !! (%s)",name);
/* answer.type = R_R_REGISTER; */
/* answer.data = RERR_ALREADY; */
/* if (!send_request (sfd,&answer,MASTER)) { */
/* log_auto (L_ERROR,"Sending request handle_r_r_register.RERR_ALREADY to host : '%s'",name); */
/* } */
/* return -1; */
index = icomp;
} else {
if ((index = computer_index_free(wdb)) == -1) {
semaphore_release(wdb->semid);
/* No space left on database */
log_auto (L_ERROR,"No space left for computer: '%s'",name);
answer.type = R_R_REGISTER;
answer.data = RERR_NOSPACE;
if (!send_request (sfd,&answer,MASTER)) {
log_auto (L_ERROR,"Sending request handle_r_r_register.RERR_NOSPACE to host : '%s'",name);
}
return -1;
}
}
computer_init(&wdb->computer[index]);
wdb->computer[index].used = 1;
wdb->computer[index].lastconn = time(NULL);
/* No errors, we (master) can receive the hwinfo from the remote */
/* computer to be registered */
answer.type = R_R_REGISTER;
answer.data = RERR_NOERROR;
if (!send_request (sfd,&answer,MASTER)) {
computer_free(&wdb->computer[index]);
semaphore_release(wdb->semid);
log_auto (L_ERROR,"Sending request (handle_r_r_register) to host : '%s'",name);
return -1;
}
/* Now send to the computer it's id, it's position on the master */
answer.data = (uint32_t) index;
if (!send_request (sfd,&answer,MASTER)) {
computer_free (&wdb->computer[index]);
semaphore_release(wdb->semid);
log_auto (L_ERROR,"Sending request (handle_r_r_register)");
return -1;
}
if (!recv_computer_hwinfo (sfd, &hwinfo)) {
computer_free (&wdb->computer[index]);
semaphore_release(wdb->semid);
log_auto (L_ERROR,"Receiving computer hardware info (handle_r_r_register)");
return -1;
}
memcpy (&wdb->computer[index].hwinfo, &hwinfo, sizeof(hwinfo));
strncpy(wdb->computer[index].hwinfo.name,name,MAXNAMELEN); /* We substitute the name that the computer sent */
/* with the name that we obtained resolving it's ip */
semaphore_release(wdb->semid);
log_auto (L_INFO,"handle_r_r_register(): Computer %s registered with id %i.",wdb->computer[index].hwinfo.name,index);
log_auto (L_DEBUG3,"handle_r_r_register(): <Exiting...");
return index;
}
void
update_computer_status (struct slave_database *sdb) {
/* The slave calls this function to update the information about */
/* his own status on the master */
struct request req;
struct computer_status status;
int sfd;
if ((sfd = connect_to_master ()) == -1) {
log_auto(L_ERROR,"Could not connect to master: %s",drerrno_str());
kill(0,SIGINT);
}
req.type = R_R_UCSTATUS;
if (!send_request (sfd,&req,SLAVE)) {
log_auto (L_WARNING,"Error sending request (update_computer_status): %s",drerrno_str());
close (sfd);
kill (0,SIGINT);
return;
}
if (!recv_request (sfd,&req)) {
log_auto (L_WARNING,"Error receiving request (update_computer_status): %s",drerrno_str());
close (sfd);
kill (0,SIGINT);
return;
}
if (req.type == R_R_UCSTATUS) {
switch (req.data) {
case RERR_NOERROR:
semaphore_lock(sdb->semid);
memcpy(&status,&sdb->comp->status,sizeof(status));
semaphore_release(sdb->semid);
send_computer_status (sfd,&status);
break;
case RERR_NOREGIS:
log_auto (L_ERROR,"Computer not registered");
close (sfd);
kill (0,SIGINT);
break;
default:
log_auto (L_ERROR,"Error code not listed on answer to R_R_UCSTATUS");
break;
}
} else {
log_auto (L_ERROR,"Not appropiate answer to request R_R_UCSTATUS");
}
close (sfd);
}
void
register_slave (struct computer *computer) {
/* The slave calls this function to register himself on the master */
struct request req;
int sfd;
log_auto (L_DEBUG,"Entering register_slave");
if ((sfd = connect_to_master ()) == -1) {
log_auto(L_ERROR,drerrno_str());
kill(0,SIGINT);
}
req.type = R_R_REGISTER;
if (!send_request (sfd,&req,SLAVE)) {
log_auto (L_ERROR,"Sending request (register_slave)");
kill (0,SIGINT);
}
if (!recv_request (sfd,&req)) {
log_auto (L_ERROR,"Receiving request (register_slave)");
kill (0,SIGINT);
}
if (req.type == R_R_REGISTER) {
switch (req.data) {
case RERR_NOERROR:
if (!recv_request (sfd,&req)) {
log_auto (L_ERROR,"Receiving request (register_slave)");
kill (0,SIGINT);
}
computer->hwinfo.id = req.data;
computer->used = 1;
if (!send_computer_hwinfo (sfd,&computer->hwinfo)) {
log_auto (L_ERROR,"Sending computer hardware info (register_slave)");
kill (0,SIGINT);
}
break;
case RERR_ALREADY:
log_auto (L_ERROR,"Already registered");
kill (0,SIGINT);
break;
case RERR_NOSPACE:
log_auto (L_ERROR,"No space on database");
kill (0,SIGINT);
break;
default:
log_auto (L_ERROR,"Error code not listed on answer to R_R_REGISTER");
kill (0,SIGINT);
break;
}
} else {
log_auto (L_ERROR,"Not appropiate answer to request R_R_REGISTER");
kill (0,SIGINT);
}
close (sfd);
}
void
handle_r_r_ucstatus (int sfd, struct database *wdb, int icomp) {
/* The master handles this type of packages */
struct request answer;
struct computer_status status;
if (icomp == -1) {
log_auto (L_WARNING,"Not registered computer requesting update of computer status");
answer.type = R_R_UCSTATUS;
answer.data = RERR_NOREGIS;
if (!send_request (sfd,&answer,MASTER)) {
log_auto (L_WARNING,"Error receiving request (handle_r_r_ucstatus)");
}
return;
}
/* No errors, we (master) can receive the status from the remote */
/* computer already registered */
answer.type = R_R_UCSTATUS;
answer.data = RERR_NOERROR;
send_request (sfd,&answer,MASTER);
computer_status_init(&status);
if (!recv_computer_status (sfd, &status))
return; /* Do not update in case of failure */
semaphore_lock(wdb->semid);
memcpy (&wdb->computer[icomp].status, &status, sizeof(status));
semaphore_release(wdb->semid);
/* report_computer_status (&wdb->computer[icomp].status); */
}
int
register_job (struct job *job) {
/* returns 0 on failure */
struct request req;
int sfd;
if ((sfd = connect_to_master ()) == -1) {
fprintf(stderr,"ERROR: %s\n",drerrno_str());
return 0;
}
req.type = R_R_REGISJOB;
if (!send_request (sfd,&req,CLIENT)) {
fprintf(stderr,"ERROR: sending request (register_job)\n");
close (sfd);
return 0;
}
if (!recv_request (sfd,&req)) {
fprintf(stderr,"ERROR: receiving request (register_job)\n");
close (sfd);
return 0;
}
if (req.type == R_R_REGISJOB) {
switch (req.data) {
case RERR_NOERROR:
if (!send_job (sfd,job)) {
fprintf (stderr,"ERROR: Job couldn't be sent: %s\n",drerrno_str());
close (sfd);
return 0;
}
break;
case RERR_ALREADY:
fprintf (stderr,"ERROR: Already registered\n");
close (sfd);
return 0;
case RERR_NOSPACE:
fprintf (stderr,"ERROR: No space on database\n");
close (sfd);
return 0;
default:
fprintf (stderr,"ERROR: Error code not listed on answer to R_R_REGISJOB\n");
close (sfd);
return 0;
}
} else {
fprintf (stderr,"ERROR: Not appropiate answer to request R_R_REGISJOB\n");
close (sfd);
return 0;
}
// We need to receive the jobid that has been assigned.
if (!recv_request (sfd,&req)) {
fprintf(stderr,"ERROR: receiving request (register_job)\n");
close (sfd);
return 0;
}
job->id = req.data;
close (sfd);
return 1;
}
void
handle_r_r_regisjob (int sfd, struct database *wdb) {
/* The master handles this type of request */
struct request answer;
struct job job;
int index;
/* TO DO */
/* Check if the job is already registered ! Or not ? */
job_init (&job);
semaphore_lock(wdb->semid);
if ((index = job_index_free(wdb)) == -1) {
/* No space left on database */
semaphore_release(wdb->semid);
log_auto (L_WARNING,"No space left for job");
answer.type = R_R_REGISJOB;
answer.data = RERR_NOSPACE;
send_request (sfd,&answer,MASTER);
return;
}
wdb->job[index].used = 1;
semaphore_release(wdb->semid);
/* Debug */
log_auto(L_DEBUG,"Job index %i free",index);
/* No errors, we (master) can receive the job from the remote */
/* computer to be registered */
answer.type = R_R_REGISJOB;
answer.data = RERR_NOERROR;
send_request (sfd,&answer,MASTER);
if (!recv_job (sfd, &job)) {
log_auto (L_ERROR,"Receiving job (handle_r_r_regisjob)");
semaphore_lock(wdb->semid);
job_init (&wdb->job[index]); /* We unassign the reserved space for that job */
semaphore_release(wdb->semid);
return;
}
//job_fix_received_invalid (&job); // Is this needed at all ?
// We send the job first to avoid race conditions with "submit job stopped"
job_init_registered (wdb,index,&job);
// Send job index
answer.type = R_R_REGISJOB;
answer.data = index;
if (!send_request (sfd,&answer,MASTER)) {
log_auto (L_ERROR,"Sending job index");
return;
}
}
void
handle_r_r_availjob (int sfd, struct database *wdb, int icomp) {
/* The master handles this type of packages */
struct request answer;
uint32_t ijob = 0,i,j;
uint16_t itask;
uint32_t iframe;
struct tpol pol[MAXJOBS];
int equal_pols;
int counter;
log_auto (L_DEBUG,"Entering handle_r_r_availjob");
if (icomp == -1) {
log_auto (L_WARNING,"Not registered computer requesting available job");
answer.type = R_R_AVAILJOB;
answer.data = RERR_NOREGIS;
if (!send_request (sfd,&answer,MASTER)) {
log_auto (L_WARNING,"Error sending request (handle_r_r_availjob)");
}
return;
}
log_auto (L_DEBUG,"Creating priority ordered list of jobs");
for (i=0;i<MAXJOBS;i++) {
pol[i].index = i;
pol[i].pri = wdb->job[i].priority;
pol[i].submit_time = wdb->job[i].submit_time;
}
qsort ((void*)pol,MAXJOBS,sizeof(struct tpol),priority_job_compare);
// Compare with previous pol
equal_pols = 1;
for (i=0;i<MAXJOBS;i++) {
if ((pol[i].index != wdb->lb.pol[i].index)
|| (pol[i].pri != wdb->lb.pol[i].pri)) {
equal_pols = 0;
break;
}
}
if (equal_pols == 0) {
log_auto(L_DEBUG2,"handle_r_r_availjob(): equal_pols == 0");
// If they changed we save the new one
for (i=0;i<MAXJOBS;i++) {
wdb->lb.pol[i].index = pol[i].index;
wdb->lb.pol[i].pri = pol[i].pri;
wdb->lb.pol[i].submit_time = pol[i].submit_time;
}
// Then we search for the first available job
counter = 0;
for (i=0;i<MAXJOBS;i++) {
ijob = pol[i].index;
if (pol[i].pri >= wdb->lb.last_priority) {
for (j=wdb->lb.next_i;j<MAXJOBS;j++) {
ijob = pol[j].index;
if (job_available(wdb,ijob,&iframe,icomp)) {
logger_computer = &wdb->computer[icomp];
logger_job = &wdb->job[ijob];
log_auto(L_INFO,"handle_r_r_availjob(): Frame %i assigned",job_frame_index_to_number(&wdb->job[ijob],iframe));
wdb->lb.next_i = j+1;
break;
}
}
i = j;
break;
}
/* ATENTION job_available sets the available frame as FS_ASSIGNED !! */
/* We need to set it back to FS_WAITING if something fails */
if (job_available(wdb,ijob,&iframe,icomp)) {
logger_computer = &wdb->computer[icomp];
logger_job = &wdb->job[ijob];
log_auto(L_INFO,"Frame %i assigned",job_frame_index_to_number(&wdb->job[ijob],iframe));
wdb->lb.next_i = i+1;
break;
}
}
} else {
// Pols are equal
log_auto(L_DEBUG2,"handle_r_r_availjob() equal_pols == 1");
for (i=wdb->lb.next_i;i<MAXJOBS;i++) {
if ((i+1) >= MAXJOBS) {
for (i=0;i<MAXJOBS;i++) {
ijob = pol[i].index;
/* ATENTION job_available sets the available frame as FS_ASSIGNED !! */
/* We need to set it back to FS_WAITING if something fails */
if (job_available(wdb,ijob,&iframe,icomp)) {
logger_computer = &wdb->computer[icomp];
logger_job = &wdb->job[ijob];
log_auto(L_INFO,"handle_r_r_availjob(): Frame %i assigned",job_frame_index_to_number(&wdb->job[ijob],iframe));
wdb->lb.next_i = i+1;
break;
}
}
break;
}
if (pol[i].pri >= wdb->lb.last_priority) {
ijob = pol[i].index;
/* ATENTION job_available sets the available frame as FS_ASSIGNED !! */
/* We need to set it back to FS_WAITING if something fails */
if (job_available(wdb,ijob,&iframe,icomp)) {
logger_computer = &wdb->computer[icomp];
logger_job = &wdb->job[ijob];
log_auto(L_INFO,"handle_r_r_availjob(): Frame %i assigned",job_frame_index_to_number(&wdb->job[ijob],iframe));
wdb->lb.next_i = i+1;
break;
}
} else {
for (i=0;i<MAXJOBS;i++) {
ijob = pol[i].index;
/* ATENTION job_available sets the available frame as FS_ASSIGNED !! */
/* We need to set it back to FS_WAITING if something fails */
if (job_available(wdb,ijob,&iframe,icomp)) {
logger_computer = &wdb->computer[icomp];
logger_job = &wdb->job[ijob];
log_auto(L_INFO,"Frame %i assigned",job_frame_index_to_number(&wdb->job[ijob],iframe));
wdb->lb.next_i = i+1;
break;
}
}
break;
}
}
}
if (i>=MAXJOBS) {
// No available jobs from the last assigned one to the end
// Lets look before.
for (i=0;i<MAXJOBS;i++) {
ijob = pol[i].index;
/* ATENTION job_available sets the available frame as FS_ASSIGNED !! */
/* We need to set it back to FS_WAITING if something fails */
if (job_available(wdb,ijob,&iframe,icomp)) {
logger_computer = &wdb->computer[icomp];
logger_job = &wdb->job[ijob];
log_auto(L_INFO,"Frame %i assigned",job_frame_index_to_number(&wdb->job[ijob],iframe));
wdb->lb.next_i = i+1;
break;
}
}
}
logger_computer = NULL;
logger_job = NULL;
if (i>=MAXJOBS) {
// No available one neither before.
wdb->lb.next_i = 0;
log_auto(L_DEBUG,"No available job");
answer.type = R_R_AVAILJOB;
answer.data = RERR_NOAVJOB;
if (!send_request (sfd,&answer,MASTER)) {
log_auto(L_WARNING,"Error sending request (handle_r_r_availjob)");
}
return;
} else {
wdb->lb.last_priority = pol[i].pri;
}
log_auto (L_DEBUG,"Available job (%i) on frame %i assigned. Sending RERR_NOERROR",ijob,iframe);
/* ijob is now the index to the first available job */
answer.type = R_R_AVAILJOB;
answer.data = RERR_NOERROR;
if (!send_request (sfd,&answer,MASTER)) {
log_auto(L_WARNING,"Error sending request (handle_r_r_availjob)");
job_frame_waiting (wdb,ijob,iframe); /* The reserved frame must be set back to waiting */
exit (0);
}
log_auto (L_DEBUG,"Receiving task availability");
/* Now we receive if there is a task structure available */
if (!recv_request (sfd,&answer)) {
log_auto(L_WARNING,"Error receiving request (handle_r_r_availjob)");
job_frame_waiting (wdb,ijob,iframe);
exit (0);
}
if (answer.type == R_R_AVAILJOB) {
switch (answer.data) {
case RERR_NOERROR:
/* We continue processing the matter */
log_auto(L_DEBUG,"Task space available");
break;
case RERR_NOSPACE:
log_auto(L_WARNING,"No space for task");
job_frame_waiting (wdb,ijob,iframe);
exit (0);
default:
log_auto(L_ERROR,"Error code not listed expecting task error code");
job_frame_waiting (wdb,ijob,iframe);
exit(0);
}
} else {
log_auto (L_ERROR,"Not appropiate answer, expecting task error code");
job_frame_waiting (wdb,ijob,iframe);
exit(0);
}
/* If there is a task structure available (we are here) then we receive the index of that task */
if (!recv_request (sfd,&answer)) {
log_auto(L_WARNING,"Error receiving request (handle_r_r_availjob)");
job_frame_waiting (wdb,ijob,iframe);
exit (0);
}
if (answer.type == R_R_AVAILJOB) {
itask = (uint16_t) answer.data;
log_auto(L_DEBUG,"Task index %i on computer %i",itask,icomp);
} else {
log_auto (L_ERROR,"Not appropiate answer, expecting task index");
job_frame_waiting (wdb,ijob,iframe);
exit (0);
}
log_auto (L_DEBUG,"Updating structures to be sent");
semaphore_lock(wdb->semid);
job_update_assigned (wdb,ijob,iframe,icomp,itask);
computer_update_assigned (wdb,ijob,iframe,icomp,itask);
semaphore_release(wdb->semid);
log_auto (L_DEBUG,"Sending updated task");
if (!send_task (sfd,&wdb->computer[icomp].status.task[itask])) {
log_auto (L_ERROR,"Could not send updated task information to the slave: %s",drerrno_str());
job_frame_waiting (wdb,ijob,iframe);
}
}
int
request_job_available (struct slave_database *sdb, uint16_t *itask) {
/* The slave requests the master an available job, and in case */
/* of finding it the master stores the info into *job and fills the task record */
/* except those fields that cannot be filled until the task is running (pid, etc.) */
/* This function SETS *itask local to this process */
/* This function returns 0 if there is no job available */
struct request req;
int sfd;
struct task ttask; /* Temporary task structure */
if ((sfd = connect_to_master ()) == -1) {
log_auto(L_ERROR,"request_job_available(): could not connect to master. (%s)",strerror(drerrno_system));
return 0;
}
req.type = R_R_AVAILJOB;
if (!send_request (sfd,&req,SLAVE)) {
log_auto (L_ERROR,"Sending request for available job: %s",drerrno_str());
return 0;
}
if (!recv_request (sfd,&req)) {
log_auto (L_ERROR,"Receiving request for available job: %s",drerrno_str());
return 0;
}
if (req.type == R_R_AVAILJOB) {
switch (req.data) {
case RERR_NOERROR:
/* We continue processing the matter */
log_auto(L_DEBUG,"Available job");
break;
case RERR_NOAVJOB:
log_auto(L_DEBUG,"No available job");
close (sfd);
return 0;
break;
case RERR_NOREGIS:
log_auto(L_ERROR,"Computer not registered. Requesting available job.");
close (sfd);
return 0;
break;
default:
log_auto(L_ERROR,"Error code not listed on answer to R_R_AVAILJOB");
close (sfd);
return 0;
}
} else {
log_auto (L_ERROR,"Not appropiate answer to request R_R_REGISJOB");
close (sfd);
return 0;
}
if (((*itask) = task_available (sdb)) == (uint16_t)-1) {
/* No task structure available */
log_auto(L_WARNING,"No task available for job");
req.type = R_R_AVAILJOB;
req.data = RERR_NOSPACE;
if (!send_request(sfd,&req,SLAVE)) {
log_auto (L_ERROR,"Sending request (request_job_available) : %s",drerrno_str());
}
close (sfd); /* Finish */
return 0;
}
log_auto (L_DEBUG,"There is an available task. Sending RERR_NOERROR");
/* We've got an available task */
req.type = R_R_AVAILJOB;
req.data = RERR_NOERROR;
if (!send_request(sfd,&req,SLAVE)) {
log_auto (L_ERROR,"Sending request (request_job_available) : %s",drerrno_str());
semaphore_lock(sdb->semid);
task_init(&sdb->comp->status.task[*itask]);
semaphore_release(sdb->semid);
//kill (0,SIGINT);
close (sfd);
return 0;
}
log_auto (L_DEBUG,"Sending index to task.");
/* So then we send the index */
req.data = *itask;
if (!send_request(sfd,&req,SLAVE)) {
log_auto (L_ERROR,"Sending request (request_job_available) : %s",drerrno_str());
semaphore_lock(sdb->semid);
task_init(&sdb->comp->status.task[*itask]);
semaphore_release(sdb->semid);
//kill (0,SIGINT);
close (sfd);
return 0;
}
log_auto (L_DEBUG,"Receiving the task");
/* Then we receive the task */
if (!recv_task(sfd,&ttask)) {
log_auto (L_ERROR,"Receiving the task (request_job_available) : %s",drerrno_str());
semaphore_lock(sdb->semid);
task_init(&sdb->comp->status.task[*itask]);
semaphore_release(sdb->semid);
//kill (0,SIGINT);
close (sfd);
return 0;
}
/* The we update the computer structure to reflect the new assigned task */
/* that is not yet runnning so pid == 0 */
semaphore_lock(sdb->semid);
memcpy(&sdb->comp->status.task[*itask],&ttask,sizeof(ttask));
sdb->comp->status.task[*itask].status = TASKSTATUS_LOADING;
sdb->comp->status.ntasks = computer_ntasks (sdb->comp);
sdb->comp->status.nrunning = computer_nrunning (sdb->comp);
semaphore_release(sdb->semid);
close (sfd);
return 1;
}
void
request_task_finished (struct slave_database *sdb, uint16_t itask) {
/* This function is called non-blocked */
/* This function is called from inside a slave launcher process */
/* It sends the information to the master about a finished task */
/* FIXME: It doesn't check if the computer is not registered, should we ? */
/* We are just sending a finished task, so it does not really matter if the computer */
/* is not registered anymore */
struct request req;
int sfd;
log_auto(L_DEBUG,"Entering request_task_finished");
if ((sfd = connect_to_master ()) == -1) {
log_auto(L_ERROR,"Connecting to master (request_task_finished) : %s",drerrno_str());
kill(0,SIGINT);
}