- Timestamp:
- Dec 10, 2012, 12:07:22 PM (14 years ago)
- Location:
- branches/sc_branches/pantasks_condor
- Files:
-
- 1 edited
- 1 copied
-
. (copied) (copied from trunk/Ohana/src/opihi ) (1 prop)
-
pantasks/ControllerOps.c (modified) (27 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/sc_branches/pantasks_condor
- Property svn:mergeinfo set to
-
branches/sc_branches/pantasks_condor/pantasks/ControllerOps.c
r34088 r34783 9 9 static IOBuffer stdout_buffer; 10 10 static IOBuffer stderr_buffer; 11 #ifndef IPP_CONDOR 11 12 static int ControllerPID = 0; 13 #else 14 #include "ipp_condor.h" 15 #include <condor/condor.nsmap> 16 #include <shell.h> 17 static struct soap soap; 18 #endif 12 19 13 20 /* local static variables to track the controller host properties */ … … 18 25 static int NHOSTS = 0; 19 26 27 #ifdef IPP_CONDOR 28 #include "ipp_condor.h" 29 static int hosts_list_modified = TRUE; 30 static char* hosts_requirement = NULL; 31 static int is_host_in_list(char *hostname); 32 #endif 33 20 34 int AddHost (char *hostname, int max_threads) { 21 22 35 int N; 23 36 #ifdef IPP_CONDOR 37 int index; 38 index = is_host_in_list(hostname); 39 if (index != -1) { 40 if (hosts[index].max_threads != max_threads) { 41 fprintf(stderr, "Hostname [%s] already declared. Changing max_threads from %d to %d", 42 hostname, 43 hosts[index].max_threads, 44 max_threads); 45 hosts[index].max_threads = max_threads; 46 } else { 47 fprintf(stderr, "Hostname [%s] already declared. Aborting", hostname); 48 } 49 return (TRUE); 50 } 51 #endif 24 52 N = Nhosts; 25 53 Nhosts ++; 26 27 54 CHECK_REALLOCATE (hosts, Host, NHOSTS, Nhosts, 16); 28 29 55 hosts[N].hostname = strcreate (hostname); 30 56 hosts[N].max_threads = max_threads; 31 57 #ifdef IPP_CONDOR 58 fprintf(stderr, "Adding host %s (%d)\n", hostname, max_threads); 59 hosts_list_modified = TRUE; //Tell get_hosts_requirements to rebuild the hosts based requirement 60 #endif 32 61 return (TRUE); 33 62 } … … 37 66 // but perhaps mark it in the client_thread? 38 67 int DeleteHost (char *hostname) { 39 40 68 int i, j; 41 42 for (i = 0; i < Nhosts; i++) { 69 fprintf(stderr, "Attempting to delete %s\n", hostname); 70 #ifdef IPP_CONDOR 71 int index; 72 index = is_host_in_list(hostname); 73 if (index == -1) { 74 return (FALSE); 75 } 76 #endif 77 for (i = 0; i < Nhosts; i++) { //Optimize this for condor 43 78 if (strcmp (hosts[i].hostname, hostname)) continue; 44 45 79 // delete this one 46 80 free (hosts[i].hostname); … … 50 84 } 51 85 Nhosts --; 86 #ifdef IPP_CONDOR 87 hosts_list_modified = TRUE; //Tell get_hosts_requirements to rebuild the hosts based requirement 88 #endif 52 89 return (TRUE); 53 90 } … … 88 125 // float dtime = DTIME (stop, start); 89 126 /* if (VerboseMode()) gprint (GP_ERR, "delete job %f\n", dtime); */ 90 } 127 } 91 128 return (TRUE); 92 129 } … … 95 132 96 133 int status; 97 char cmd[128]; 134 char cmd[128]; 98 135 IOBuffer buffer; 99 136 … … 104 141 return (status); 105 142 } 106 143 107 144 /* ask controller about job status */ 108 145 int CheckControllerJobStatus (Job *job) { … … 169 206 } 170 207 171 /* we read Nbytes from the host, then watch for the prompt */ 208 /* we read Nbytes from the host, then watch for the prompt */ 172 209 int GetJobOutput (char *cmd, int pid, IOBuffer *buffer, int Nbytes) { 173 210 174 211 int i, status, Nstart; 175 212 char *line; … … 222 259 /* submitting a job to the controller automatically starts controller */ 223 260 int SubmitControllerJob (Job *job) { 224 261 #ifndef IPP_CONDOR 262 fprintf(stderr, "Submitting job to pcontrol"); 263 fprintf(stdout, "Submitting job to pcontrol"); 225 264 int i, Nchar, status; 226 265 char *cmd, *p, string[64]; 227 266 IOBuffer buffer; 228 229 if (job[0].task[0].host == NULL) return (FALSE); 230 267 if (job[0].task[0].host == NULL) return (FALSE); 231 268 if (!StartController ()) { 232 269 gprint (GP_ERR, "failure to start pcontrol\n"); 233 270 return (FALSE); 234 271 } 235 236 272 /** construct the line to be sent to the controller **/ 237 238 273 /* determine the total line length */ 239 274 Nchar = 0; … … 247 282 ALLOCATE (cmd, char, Nchar); 248 283 bzero (cmd, Nchar); 249 250 284 /* construct the controller command portion */ 251 285 if (!strcasecmp (job[0].task[0].host, "ANYHOST")) { … … 258 292 } 259 293 } 260 261 294 if (job[0].priority) { 262 295 char tmp[64]; … … 264 297 strcat (cmd, tmp); 265 298 } 266 267 299 /* add the command arguments */ 268 300 for (i = 0; i < job[0].task[0].argc; i++) { … … 270 302 strcat (cmd, job[0].task[0].argv[i]); 271 303 } 272 273 304 // This function is called by the JobTaskThread via SubmitJob. We need to unlock the 274 305 // JobTaskLock to avoid a dead lock with the JobTaskLock called in CheckController … … 280 311 ControlUnlock(__func__); 281 312 JobTaskLock(); 282 283 284 313 /* extract the job PID from the controller response */ 285 314 p = memstr (buffer.buffer, "JobID", buffer.Nbuffer); … … 292 321 sscanf (p, "%*s %s", string); 293 322 FreeIOBuffer (&buffer); 294 295 323 job[0].pid = atoi (string); 296 324 if (job[0].pid < 0) { 297 325 return (FALSE); 298 326 } 327 #else 328 struct ns1__Transaction *transaction; 329 int clusterId; 330 //Create a Transaction 331 transaction = beginTransaction(&soap, 10); 332 //Get a clusterId 333 clusterId = newCluster(&soap, transaction); 334 fprintf(stderr, "ClusterId = %d\n", clusterId); 335 fprintf(stdout, "Submitting job to HTCondor\n"); 336 char *args = paste_args(job[0].task[0].argc-1, job[0].task[0].argv+1); 337 submitJob(&soap, transaction, clusterId, 0, 338 job[0].task[0].argv[0], 339 args, 340 get_hosts_requirements()); 341 commitTransaction(&soap, transaction); 342 #endif 299 343 return (TRUE); 300 344 } 301 345 302 346 int StartController () { 303 347 #ifndef IPP_CONDOR 304 348 char *p; 305 349 char **argv, cmd[128]; … … 307 351 int stdin_fd[2], stdout_fd[2], stderr_fd[2]; 308 352 IOBuffer buffer; 309 310 353 if (ControllerStatus) return (TRUE); 311 312 354 if (VarConfig ("CONTROLLER", "%s", cmd) == NULL) strcpy (cmd, "pcontrol"); 313 314 355 if (hosts == NULL) { 315 356 NHOSTS = 16; 316 357 ALLOCATE (hosts, Host, NHOSTS); 317 358 } 318 319 359 bzero (stdin_fd, 2*sizeof(int)); 320 360 bzero (stdout_fd, 2*sizeof(int)); 321 361 bzero (stderr_fd, 2*sizeof(int)); 322 323 362 if (pipe (stdin_fd) < 0) goto pipe_error; 324 363 if (pipe (stdout_fd) < 0) goto pipe_error; 325 364 if (pipe (stderr_fd) < 0) goto pipe_error; 326 327 365 ALLOCATE (argv, char *, 2); 328 366 argv[0] = cmd; 329 367 argv[1] = 0; 330 331 368 pid = fork (); 332 369 if (!pid) { /* must be child process */ 333 370 gprint (GP_LOG, "starting controller connection\n"); 334 335 371 /* close the other ends of the pipes */ 336 372 close (stdin_fd[1]); 337 373 close (stdout_fd[0]); 338 374 close (stderr_fd[0]); 339 340 375 /* tie our ends of the pipes to stdin, stdout, stderr */ 341 376 dup2 (stdin_fd[0], STDIN_FILENO); 342 377 dup2 (stdout_fd[1], STDOUT_FILENO); 343 378 dup2 (stderr_fd[1], STDERR_FILENO); 344 345 379 /* set all three unblocking */ 346 380 setvbuf (stdin, (char *) NULL, _IONBF, BUFSIZ); 347 381 setvbuf (stdout, (char *) NULL, _IONBF, BUFSIZ); 348 382 setvbuf (stderr, (char *) NULL, _IONBF, BUFSIZ); 349 350 status = execvp (argv[0], argv); 383 status = execvp (argv[0], argv); 351 384 exit (1); 352 385 } 353 386 free (argv); 354 355 387 /* close the other ends of the pipes */ 356 388 close (stdin_fd[0]); stdin_fd[0] = 0; 357 389 close (stdout_fd[1]); stdout_fd[1] = 0; 358 390 close (stderr_fd[1]); stderr_fd[1] = 0; 359 360 391 /* make the pipes non-blocking */ 361 392 fcntl (stdin_fd[1], F_SETFL, O_NONBLOCK); 362 393 fcntl (stdout_fd[0], F_SETFL, O_NONBLOCK); 363 394 fcntl (stderr_fd[0], F_SETFL, O_NONBLOCK); 364 365 395 /* perform handshake with controller to verify alive & running */ 366 396 /** this handshake is similar to ControllerCommand, but has important differences **/ 367 397 InitIOBuffer (&buffer, 0x100); 368 369 398 /* send handshake command */ 370 399 status = write_fmt (stdin_fd[1], "echo CONNECTED\n"); 371 400 if ((status == -1) && (errno == EPIPE)) goto pipe_error; 372 373 401 /* try to get evidence connection is alive - wait upto a few seconds */ 374 402 /* connection is likely slow; don't bother with nanosleep here */ … … 383 411 if (status == -1) goto io_error; 384 412 FreeIOBuffer (&buffer); 385 386 413 /* set local static vars to pipe connections */ 387 414 stdin_cntl = stdin_fd[1]; 388 415 stdout_cntl = stdout_fd[0]; 389 416 stderr_cntl = stderr_fd[0]; 390 391 417 InitIOBuffer (&stdout_buffer, 0x100); 392 418 InitIOBuffer (&stderr_buffer, 0x100); 393 394 419 ControllerPID = pid; 420 #else 421 if (ControllerStatus == FALSE) { 422 //Initiate the HTcondor connection 423 soap_init(&soap); 424 soap_set_namespaces(&soap, condor_namespaces); 425 } 426 #endif 395 427 ControllerStatus = TRUE; 396 428 gprint (GP_LOG, "Connected\n"); 397 429 return (TRUE); 398 430 #ifndef IPP_CONDOR 399 431 pipe_error: 400 432 perror ("pipe error:"); 401 433 goto close_pipes; 402 403 434 io_error: 404 435 gprint (GP_ERR, "timeout while connecting\n"); 405 436 goto close_pipes; 406 407 437 close_pipes: 408 438 if (stdin_fd[0] != 0) close (stdin_fd[0]); … … 413 443 if (stderr_fd[1] != 0) close (stderr_fd[1]); 414 444 return (FALSE); 445 #endif 415 446 } 416 447 417 448 int ControllerCommand (char *cmd, char *response, IOBuffer *buffer) { 418 449 #ifndef IPP_CONDOR 419 450 int i, j, status; 420 451 char *line; 421 452 struct timespec request, remain; 422 453 //fprintf(stderr, "ControllerCommand: [%s]\n", cmd); 423 454 /* avoid blocking on waitpid, test every 100 usec, up to 50 msec */ 424 455 request.tv_sec = 0; 425 456 request.tv_nsec = 100000; 426 427 457 ReadtoIOBuffer (buffer, stdout_cntl); 428 458 FlushIOBuffer (buffer); 429 430 459 /* send command, is pipe still open? */ 431 460 status = write_fmt (stdin_cntl, "%s\n", cmd); … … 438 467 return (FALSE); 439 468 } 440 441 469 /* for commands which don't return a prompt, don't look for one */ 442 470 if (response == NULL) { 443 471 return (TRUE); 444 472 } 445 446 473 /* watch for response - wait up to 1 second */ 447 474 line = NULL; … … 457 484 fprintf (stderr, "controller is down (EOF), restarting\n"); 458 485 if (!RestartController ()) { 459 return (FALSE);486 return (FALSE); 460 487 } 461 488 return (FALSE); … … 470 497 return (FALSE); 471 498 } 472 473 499 /* need to strip off the prompt */ 474 500 line = memstr (buffer[0].buffer, response, buffer[0].Nbuffer); … … 478 504 } 479 505 if (VerboseMode()) fprintf (stderr, "message received, %d cycles\n", i); 506 #endif 480 507 return (TRUE); 481 508 } … … 498 525 break; 499 526 } 500 527 501 528 /* read stderr buffer */ 502 529 Nread = ReadtoIOBuffer (&stderr_buffer, stderr_cntl); … … 576 603 577 604 int QuitController () { 578 605 #ifndef IPP_CONDOR 579 606 char cmd[128]; 580 607 IOBuffer buffer; 581 582 608 if (!ControllerStatus) return (TRUE); 583 584 609 sprintf (cmd, "quit"); 585 610 InitIOBuffer (&buffer, 0x100); 586 611 ControllerCommand (cmd, NULL, &buffer); 587 612 FreeIOBuffer (&buffer); 588 589 /* the quit command does not return a prompt, 613 /* the quit command does not return a prompt, 590 614 check that the controller exited */ 591 615 StopController (); 616 #else 617 soap_destroy(&soap); 618 soap_end(&soap); 619 soap_done(&soap); 620 #endif 592 621 return (TRUE); 593 622 } 594 623 595 624 int StopController () { 596 625 #ifndef IPP_CONDOR 597 626 int i, waitstatus, result; 598 599 627 if (!ControllerStatus) return (TRUE); 600 601 628 ControllerStatus = FALSE; 602 629 result = waitpid (ControllerPID, &waitstatus, WNOHANG); … … 611 638 FreeIOBuffer (&stdout_buffer); 612 639 FreeIOBuffer (&stderr_buffer); 640 #endif 613 641 return (TRUE); 614 642 } 615 643 616 644 int RestartController () { 617 645 #ifndef IPP_CONDOR 618 646 int i, status; 619 647 char command[256]; 620 648 IOBuffer buffer; 621 622 649 gprint (GP_ERR, "attempting to restart pcontrol\n"); 623 650 if (!StartController ()) { … … 625 652 return (FALSE); 626 653 } 627 628 654 InitIOBuffer (&buffer, 0x100); 629 630 655 status = TRUE; 631 632 656 // XXX lock the host table? no: that would risk a dead lock between client and controller threads: 633 657 gprint (GP_ERR, "pcontrol restarted, reloading hosts\n"); … … 637 661 status = ControllerCommand (command, CONTROLLER_PROMPT, &buffer); 638 662 } 639 640 663 if (status) gwrite (buffer.buffer, 1, buffer.Nbuffer, GP_LOG); 641 642 FreeIOBuffer (&buffer); 643 644 return (TRUE); 645 } 664 FreeIOBuffer (&buffer); 665 #endif 666 return (TRUE); 667 } 668 669 #ifdef IPP_CONDOR 670 char* get_hosts_requirements(void) { 671 int i; 672 int hr_length; 673 char* base_requirement_pattern_b = " (machine == \""; 674 char* base_requirement_pattern_e = "\")"; 675 char* base_requirement_pattern_or = " || "; 676 if ( (hosts_requirement != NULL) && (hosts_list_modified == FALSE) ) { 677 return hosts_requirement; 678 } 679 if (hosts_requirement != NULL) { 680 free(hosts_requirement); 681 } 682 printf("Generating hosts based requirement\n"); 683 hosts_requirement = strcreate(""); 684 hr_length = strlen(hosts_requirement); 685 if (Nhosts > 0) { 686 hosts_requirement = opihi_append(hosts_requirement, &hr_length, "( ", NULL); 687 for (i = 0; i < Nhosts; i++) { 688 hosts_requirement = opihi_append(hosts_requirement, &hr_length, 689 base_requirement_pattern_b, NULL); 690 hosts_requirement = opihi_append(hosts_requirement, &hr_length, 691 hosts[i].hostname, NULL); 692 hosts_requirement = opihi_append(hosts_requirement, &hr_length, 693 base_requirement_pattern_e, NULL); 694 if (i != Nhosts-1) { 695 hosts_requirement = opihi_append(hosts_requirement, &hr_length, 696 base_requirement_pattern_or, NULL); 697 } 698 } 699 } else { // No host defined: leave it empty 700 //pass 701 } 702 hosts_list_modified = FALSE; 703 printf("end of Generating hosts based requirement\n"); 704 return hosts_requirement; 705 } 706 707 static int is_host_in_list(char *hostname) { 708 int i; 709 for (i = 0; i < Nhosts; i++) { 710 if (strcmp(hosts[i].hostname, hostname) == 0) { 711 return i; 712 } 713 } 714 return -1; 715 } 716 717 char *get_host_status(char* hostname) { 718 return getHostStatus(&soap, hostname); 719 } 720 721 #endif
Note:
See TracChangeset
for help on using the changeset viewer.
