1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
30 #include <libbatch/BatchManagerCatalog.hxx>
31 #include <libbatch/FactBatchManager.hxx>
32 #include <libbatch/BatchManager.hxx>
35 #include "Basics_Utils.hxx"
36 #include "Basics_DirUtils.hxx"
37 #include "SALOME_Launcher_Handler.hxx"
38 #include "Launcher.hxx"
39 #include "Launcher_Job_Command.hxx"
40 #include "Launcher_XML_Persistence.hxx"
44 //=============================================================================
48 * Define a CORBA single thread policy for the server, which avoid to deal
49 * with non thread-safe usage like Change_Directory in SALOME naming service
51 //=============================================================================
52 Launcher_cpp::Launcher_cpp()
54 LAUNCHER_MESSAGE("Launcher_cpp constructor");
56 _job_cpt_mutex = new pthread_mutex_t();
57 pthread_mutex_init(_job_cpt_mutex, NULL);
60 //=============================================================================
64 //=============================================================================
65 Launcher_cpp::~Launcher_cpp()
67 LAUNCHER_MESSAGE("Launcher_cpp destructor");
69 std::map<int, Launcher::Job *>::const_iterator it_job;
70 for(it_job = _launcher_job_map.begin(); it_job != _launcher_job_map.end(); it_job++)
71 delete it_job->second;
72 std::map <int, Batch::BatchManager * >::const_iterator it1;
73 for(it1=_batchmap.begin();it1!=_batchmap.end();it1++)
77 pthread_mutex_destroy(_job_cpt_mutex);
78 delete _job_cpt_mutex;
83 //=============================================================================
85 * Add a job into the launcher - check resource and choose one
87 //=============================================================================
89 Launcher_cpp::createJob(Launcher::Job * new_job)
91 LAUNCHER_MESSAGE("Creating a new job");
92 // Add job to the jobs map
93 pthread_mutex_lock(_job_cpt_mutex);
94 new_job->setNumber(_job_cpt);
96 pthread_mutex_unlock(_job_cpt_mutex);
97 std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(new_job->getNumber());
98 if (it_job == _launcher_job_map.end())
99 _launcher_job_map[new_job->getNumber()] = new_job;
102 LAUNCHER_INFOS("A job as already the same id: " << new_job->getNumber());
104 throw LauncherException("A job as already the same id - job is not created !");
106 LAUNCHER_MESSAGE("New Job created");
109 //=============================================================================
113 //=============================================================================
115 Launcher_cpp::launchJob(int job_id)
117 LAUNCHER_MESSAGE("Launch a job");
119 // Check if job exist
120 std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(job_id);
121 if (it_job == _launcher_job_map.end())
123 LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
124 throw LauncherException("Cannot find the job, is it created ?");
127 Launcher::Job * job = it_job->second;
129 // Check job state (cannot launch a job already launched...)
130 if (job->getState() != "CREATED")
132 LAUNCHER_INFOS("Bad state of the job: " << job->getState());
133 throw LauncherException("Bad state of the job: " + job->getState());
136 // Third step search batch manager for the job into the map -> instanciate one if does not exist
138 std::map<int, Batch::BatchManager *>::const_iterator it = _batchmap.find(job_id);
139 if(it == _batchmap.end())
141 createBatchManagerForJob(job);
146 Batch::JobId batch_manager_job_id = _batchmap[job_id]->submitJob(*(job->getBatchJob()));
147 job->setBatchManagerJobId(batch_manager_job_id);
148 job->setState("QUEUED");
149 job->setReference(batch_manager_job_id.getReference());
151 catch(const Batch::GenericException &ex)
153 LAUNCHER_INFOS("Job is not launched, exception in submitJob: " << ex.message);
154 throw LauncherException(ex.message.c_str());
156 LAUNCHER_MESSAGE("Job launched");
159 //=============================================================================
163 //=============================================================================
165 Launcher_cpp::getJobState(int job_id)
167 LAUNCHER_MESSAGE("Get job state");
169 // Check if job exist
170 std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(job_id);
171 if (it_job == _launcher_job_map.end())
173 LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
174 throw LauncherException("Cannot find the job, is it created ?");
177 Launcher::Job * job = it_job->second;
182 state = job->updateJobState();
184 catch(const Batch::GenericException &ex)
186 LAUNCHER_INFOS("getJobState failed, exception: " << ex.message);
187 throw LauncherException(ex.message.c_str());
190 return state.c_str();
193 //=============================================================================
195 * Get job assigned hostnames
197 //=============================================================================
199 Launcher_cpp::getAssignedHostnames(int job_id)
201 LAUNCHER_MESSAGE("Get job assigned hostnames");
203 // Check if job exist
204 std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(job_id);
205 if (it_job == _launcher_job_map.end())
207 LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
208 throw LauncherException("Cannot find the job, is it created ?");
211 Launcher::Job * job = it_job->second;
212 std::string assigned_hostnames = job->getAssignedHostnames();
214 return assigned_hostnames.c_str();
217 //=============================================================================
219 * Get Job result - the result directory could be changed
221 //=============================================================================
223 Launcher_cpp::getJobResults(int job_id, std::string directory)
225 LAUNCHER_MESSAGE("Get Job results");
227 // Check if job exist
228 std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(job_id);
229 if (it_job == _launcher_job_map.end())
231 LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
232 throw LauncherException("Cannot find the job, is it created ?");
235 Launcher::Job * job = it_job->second;
236 std::string resource_name = job->getResourceDefinition().Name;
240 _batchmap[job_id]->importOutputFiles(*(job->getBatchJob()), directory);
242 _batchmap[job_id]->importOutputFiles(*(job->getBatchJob()), job->getResultDirectory());
244 catch(const Batch::GenericException &ex)
246 LAUNCHER_INFOS("getJobResult is maybe incomplete, exception: " << ex.message);
247 throw LauncherException(ex.message.c_str());
249 LAUNCHER_MESSAGE("getJobResult ended");
252 //=============================================================================
254 * Get Job dump state - the result directory could be changed
256 //=============================================================================
258 Launcher_cpp::getJobDumpState(int job_id, std::string directory)
261 LAUNCHER_MESSAGE("Get Job dump state");
263 // Check if job exist
264 std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(job_id);
265 if (it_job == _launcher_job_map.end())
267 LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
268 throw LauncherException("Cannot find the job, is it created ?");
271 Launcher::Job * job = it_job->second;
272 std::string resource_name = job->getResourceDefinition().Name;
276 rtn = _batchmap[job_id]->importDumpStateFile(*(job->getBatchJob()), directory);
278 rtn = _batchmap[job_id]->importDumpStateFile(*(job->getBatchJob()), job->getResultDirectory());
280 catch(const Batch::GenericException &ex)
282 LAUNCHER_INFOS("getJobResult is maybe incomplete, exception: " << ex.message);
283 throw LauncherException(ex.message.c_str());
285 LAUNCHER_MESSAGE("getJobResult ended");
289 //=============================================================================
291 * Get one file from the working directory - the result directory can be changed
293 //=============================================================================
295 Launcher_cpp::getJobWorkFile(int job_id,
296 std::string work_file,
297 std::string directory)
300 LAUNCHER_MESSAGE("Get working file " << work_file);
302 // Check if job exist
303 std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(job_id);
304 if (it_job == _launcher_job_map.end())
306 LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
307 throw LauncherException("Cannot find the job, is it created ?");
310 Launcher::Job * job = it_job->second;
311 std::string resource_name = job->getResourceDefinition().Name;
315 rtn = _batchmap[job_id]->importWorkFile(*(job->getBatchJob()), work_file, directory);
317 rtn = _batchmap[job_id]->importWorkFile(*(job->getBatchJob()), work_file, job->getResultDirectory());
319 catch(const Batch::GenericException &ex)
321 LAUNCHER_INFOS("getJobWorkFile is maybe incomplete, exception: " << ex.message);
322 throw LauncherException(ex.message.c_str());
324 LAUNCHER_MESSAGE("getJobWorkFile ended");
328 //=============================================================================
330 * Remove the job - into the Launcher and its batch manager
332 //=============================================================================
334 Launcher_cpp::removeJob(int job_id)
336 LAUNCHER_MESSAGE("Remove Job");
338 // Check if job exist
339 std::map<int, Launcher::Job *>::iterator it_job = _launcher_job_map.find(job_id);
340 if (it_job == _launcher_job_map.end())
342 LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
343 throw LauncherException("Cannot find the job, is it created ?");
346 it_job->second->removeJob();
347 delete it_job->second;
348 _launcher_job_map.erase(it_job);
351 //=============================================================================
355 //=============================================================================
357 Launcher_cpp::stopJob(int job_id)
359 LAUNCHER_MESSAGE("Stop Job");
361 // Check if job exist
362 std::map<int, Launcher::Job *>::iterator it_job = _launcher_job_map.find(job_id);
363 if (it_job == _launcher_job_map.end())
365 LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
366 throw LauncherException("Cannot find the job, is it created ?");
369 it_job->second->stopJob();
372 //=============================================================================
374 * create a launcher job based on a file
375 * \param xmlExecuteFile : to define the execution on the batch cluster
377 //=============================================================================
379 Launcher_cpp::createJobWithFile(const std::string xmlExecuteFile,
380 const std::string clusterName)
382 LAUNCHER_MESSAGE("Begin of Launcher_cpp::createJobWithFile");
385 ParserLauncherType job_params = ParseXmlFile(xmlExecuteFile);
387 // Creating a new job
388 Launcher::Job_Command * new_job = new Launcher::Job_Command();
390 std::string cmdFile = Kernel_Utils::GetTmpFileName();
397 os.open(cmdFile.c_str(), std::ofstream::out );
398 os << "#! /bin/sh" << std::endl;
399 os << job_params.Command;
402 new_job->setJobFile(cmdFile);
403 new_job->setLocalDirectory(job_params.RefDirectory);
404 new_job->setWorkDirectory(job_params.MachinesList[clusterName].WorkDirectory);
405 new_job->setEnvFile(job_params.MachinesList[clusterName].EnvFile);
407 for(int i=0; i < job_params.InputFile.size(); i++)
408 new_job->add_in_file(job_params.InputFile[i]);
409 for(int i=0; i < job_params.OutputFile.size();i++)
410 new_job->add_out_file(job_params.OutputFile[i]);
413 p.hostname = clusterName;
416 p.nb_proc = job_params.NbOfProcesses;
418 p.nb_proc_per_node = 0;
421 new_job->setResourceRequiredParams(p);
424 return new_job->getNumber();
427 //=============================================================================
429 * Factory to instanciate the good batch manager for choosen cluster.
431 //=============================================================================
432 Batch::BatchManager *
433 Launcher_cpp::FactoryBatchManager(ParserResourcesType& params)
436 Batch::CommunicationProtocolType protocol;
437 Batch::FactBatchManager * fact;
439 std::string hostname = params.HostName;
441 switch(params.Protocol)
444 protocol = Batch::SH;
447 protocol = Batch::RSH;
450 protocol = Batch::SSH;
453 throw LauncherException("Unknown protocol for this resource");
485 switch( params.Batch )
518 LAUNCHER_MESSAGE("Bad batch description of the resource: Batch = " << params.Batch);
519 throw LauncherException("No batchmanager for that cluster - Bad batch description of the resource");
521 Batch::BatchManagerCatalog & cata = Batch::BatchManagerCatalog::getInstance();
522 fact = dynamic_cast<Batch::FactBatchManager*>(cata(bmType));
524 LAUNCHER_MESSAGE("Cannot find batch manager factory for " << bmType << ". Check your version of libBatch.");
525 throw LauncherException("Cannot find batch manager factory");
527 LAUNCHER_MESSAGE("Instanciation of batch manager of type: " << bmType);
528 Batch::BatchManager * batch_client = (*fact)(hostname.c_str(), params.UserName.c_str(),
529 protocol, mpi.c_str());
533 //----------------------------------------------------------
534 // Without LIBBATCH - Launcher_cpp do nothing...
535 //----------------------------------------------------------
539 Launcher_cpp::createJob(Launcher::Job * new_job)
541 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot create a job !!!");
543 throw LauncherException("Method Launcher_cpp::createJob is not available "
544 "(libBatch was not present at compilation time)");
548 Launcher_cpp::launchJob(int job_id)
550 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot launch a job !!!");
551 throw LauncherException("Method Launcher_cpp::launchJob is not available "
552 "(libBatch was not present at compilation time)");
556 Launcher_cpp::getJobState(int job_id)
558 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot get job state!!!");
559 throw LauncherException("Method Launcher_cpp::getJobState is not available "
560 "(libBatch was not present at compilation time)");
564 Launcher_cpp::getAssignedHostnames(int job_id)
566 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot get job assigned hostnames!!!");
567 throw LauncherException("Method Launcher_cpp::getAssignedHostnames is not available "
568 "(libBatch was not present at compilation time)");
572 Launcher_cpp::getJobResults(int job_id, std::string directory)
574 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot get job results!!!");
575 throw LauncherException("Method Launcher_cpp::getJobResults is not available "
576 "(libBatch was not present at compilation time)");
580 Launcher_cpp::getJobDumpState(int job_id, std::string directory)
582 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot get job dump state!!!");
583 throw LauncherException("Method Launcher_cpp::getJobDumpState is not available "
584 "(libBatch was not present at compilation time)");
588 Launcher_cpp::getJobWorkFile(int job_id, std::string work_file, std::string directory)
590 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot get job dump state!!!");
591 throw LauncherException("Method Launcher_cpp::getJobWorkFile is not available "
592 "(libBatch was not present at compilation time)");
596 Launcher_cpp::removeJob(int job_id)
598 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot remove job!!!");
599 throw LauncherException("Method Launcher_cpp::removeJob is not available "
600 "(libBatch was not present at compilation time)");
604 Launcher_cpp::stopJob(int job_id)
606 throw LauncherException("Method Launcher_cpp::stopJob is not available "
607 "(libBatch was not present at compilation time)");
611 Launcher_cpp::createJobWithFile( const std::string xmlExecuteFile, std::string clusterName)
613 throw LauncherException("Method Launcher_cpp::createJobWithFile is not available "
614 "(libBatch was not present at compilation time)");
621 Launcher_cpp::ParseXmlFile(std::string xmlExecuteFile)
623 ParserLauncherType job_params;
624 SALOME_Launcher_Handler * handler = new SALOME_Launcher_Handler(job_params);
626 const char* aFilePath = xmlExecuteFile.c_str();
627 FILE* aFile = fopen(aFilePath, "r");
630 xmlDocPtr aDoc = xmlReadFile(aFilePath, NULL, 0);
632 handler->ProcessXmlDocument(aDoc);
635 std::string message = "ResourcesManager_cpp: could not parse file: " + xmlExecuteFile;
636 LAUNCHER_MESSAGE(message);
638 throw LauncherException(message);
646 std::string message = "ResourcesManager_cpp: file is not readable: " + xmlExecuteFile;
647 LAUNCHER_MESSAGE(message);
649 throw LauncherException(message);
657 std::map<int, Launcher::Job *>
658 Launcher_cpp::getJobs()
660 return _launcher_job_map;
664 Launcher_cpp::createBatchManagerForJob(Launcher::Job * job)
666 int job_id = job->getNumber();
668 // Select a ressource for the job
669 std::vector<std::string> ResourceList;
670 resourceParams params = job->getResourceRequiredParams();
671 // Consider only resources that can launch batch jobs
672 params.can_launch_batch_jobs = true;
675 ResourceList = _ResManager->GetFittingResources(params);
677 catch(const ResourcesException &ex)
679 throw LauncherException(ex.msg.c_str());
681 if (ResourceList.size() == 0)
683 LAUNCHER_INFOS("No adequate resource found for the job, number " << job->getNumber());
684 job->setState("ERROR");
685 throw LauncherException("No resource found the job");
688 // Configure the job with the resource selected - the first of the list
689 ParserResourcesType resource_definition = _ResManager->GetResourcesDescr(ResourceList[0]);
691 // Set resource definition to the job
692 // The job will check if the definitions needed
695 job->setResourceDefinition(resource_definition);
697 catch(const LauncherException &ex)
699 LAUNCHER_INFOS("Error in the definition of the resource, mess: " << ex.msg);
700 job->setState("ERROR");
704 // Step 2: We can now add a Factory if the resource is correctly define
706 std::map<int, Batch::BatchManager *>::const_iterator it = _batchmap.find(job_id);
707 if(it == _batchmap.end())
711 // Warning cannot write on one line like this, because map object is constructed before
712 // the method is called...
713 //_batchmap[job_id] = FactoryBatchManager(resource_definition);
714 Batch::BatchManager * batch_client = FactoryBatchManager(resource_definition);
715 _batchmap[job_id] = batch_client;
717 catch(const LauncherException &ex)
719 LAUNCHER_INFOS("Error during creation of the batch manager of the job, mess: " << ex.msg);
722 catch(const Batch::GenericException &ex)
724 LAUNCHER_INFOS("Error during creation of the batch manager of the job, mess: " << ex.message);
725 throw LauncherException(ex.message);
732 Launcher_cpp::addJobDirectlyToMap(Launcher::Job * new_job)
734 // Step 0: Calculated job_id
735 pthread_mutex_lock(_job_cpt_mutex);
736 int job_id = _job_cpt;
738 new_job->setNumber(job_id);
739 pthread_mutex_unlock(_job_cpt_mutex);
741 // Step 1: check if resource is already in the map
742 createBatchManagerForJob(new_job);
744 // Step 2: add the job to the batch manager
748 Batch::JobId batch_manager_job_id = _batchmap[job_id]->addJob(*(new_job->getBatchJob()),
749 new_job->getReference());
750 new_job->setBatchManagerJobId(batch_manager_job_id);
752 catch(const Batch::GenericException &ex)
754 LAUNCHER_INFOS("Job cannot be added, exception in addJob: " << ex.message);
755 throw LauncherException(ex.message.c_str());
758 // Step 3: add job to launcher map
759 std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(new_job->getNumber());
760 if (it_job == _launcher_job_map.end())
761 _launcher_job_map[new_job->getNumber()] = new_job;
764 LAUNCHER_INFOS("A job as already the same id: " << new_job->getNumber());
766 throw LauncherException("A job as already the same id - job is not created !");
768 LAUNCHER_MESSAGE("New job added");
773 Launcher_cpp::loadJobs(const char* jobs_file)
775 list<int> new_jobs_id_list;
777 // Load the jobs from XML file
778 list<Launcher::Job *> jobs_list = Launcher::XML_Persistence::loadJobs(jobs_file);
780 // Create each job in the launcher
781 list<Launcher::Job *>::const_iterator it_job;
782 for (it_job = jobs_list.begin(); it_job != jobs_list.end(); it_job++)
784 Launcher::Job * new_job = *it_job;
785 string job_state = new_job->getState();
789 if (job_state == "CREATED")
791 // In this case, we ignore run_part informations
793 new_jobs_id_list.push_back(new_job->getNumber());
795 else if (job_state == "QUEUED" ||
796 job_state == "RUNNING" ||
797 job_state == "IN_PROCESS" ||
798 job_state == "PAUSED")
800 addJobDirectlyToMap(new_job);
801 new_jobs_id_list.push_back(new_job->getNumber());
803 // Step 4: We check that the BatchManager could resume
806 if (new_job->getBatchManagerJobId().getReference() != new_job->getReference())
808 LAUNCHER_INFOS("BatchManager type cannot resume a job - job state is set to ERROR");
809 new_job->setState("ERROR");
813 else if (job_state == "FINISHED" ||
814 job_state == "FAILED" ||
815 job_state == "ERROR")
817 // Step 2: We add run_part informations
818 addJobDirectlyToMap(new_job);
819 new_jobs_id_list.push_back(new_job->getNumber());
823 LAUNCHER_INFOS("A bad job is found, state unknown " << job_state);
827 catch(const LauncherException &ex)
829 LAUNCHER_INFOS("Cannot load the job. Exception: " << ex.msg.c_str());
834 return new_jobs_id_list;
838 Launcher_cpp::saveJobs(const char* jobs_file)
840 // Create a sorted list from the internal job map
841 list<const Launcher::Job *> jobs_list;
842 for (int i=0; i<_job_cpt; i++)
844 map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(i);
845 if (it_job != _launcher_job_map.end())
846 jobs_list.push_back(it_job->second);
849 // Save the jobs in XML file
850 Launcher::XML_Persistence::saveJobs(jobs_file, jobs_list);