1 // Copyright (C) 2007-2013 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.
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
24 #include <libbatch/BatchManagerCatalog.hxx>
25 #include <libbatch/FactBatchManager.hxx>
26 #include <libbatch/BatchManager.hxx>
29 #include "Basics_Utils.hxx"
30 #include "Basics_DirUtils.hxx"
31 #include "SALOME_Launcher_Handler.hxx"
32 #include "Launcher.hxx"
33 #include "Launcher_Job_Command.hxx"
39 //=============================================================================
43 * Define a CORBA single thread policy for the server, which avoid to deal
44 * with non thread-safe usage like Change_Directory in SALOME naming service
46 //=============================================================================
47 Launcher_cpp::Launcher_cpp()
49 LAUNCHER_MESSAGE("Launcher_cpp constructor");
51 _job_cpt_mutex = new pthread_mutex_t();
52 pthread_mutex_init(_job_cpt_mutex, NULL);
55 //=============================================================================
59 //=============================================================================
60 Launcher_cpp::~Launcher_cpp()
62 LAUNCHER_MESSAGE("Launcher_cpp destructor");
64 std::map<int, Launcher::Job *>::const_iterator it_job;
65 for(it_job = _launcher_job_map.begin(); it_job != _launcher_job_map.end(); it_job++)
66 delete it_job->second;
67 std::map <int, Batch::BatchManager * >::const_iterator it1;
68 for(it1=_batchmap.begin();it1!=_batchmap.end();it1++)
72 pthread_mutex_destroy(_job_cpt_mutex);
73 delete _job_cpt_mutex;
78 //=============================================================================
80 * Add a job into the launcher - check resource and choose one
82 //=============================================================================
84 Launcher_cpp::createJob(Launcher::Job * new_job)
86 LAUNCHER_MESSAGE("Creating a new job");
87 // Add job to the jobs map
88 pthread_mutex_lock(_job_cpt_mutex);
89 new_job->setNumber(_job_cpt);
91 pthread_mutex_unlock(_job_cpt_mutex);
92 std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(new_job->getNumber());
93 if (it_job == _launcher_job_map.end())
94 _launcher_job_map[new_job->getNumber()] = new_job;
97 LAUNCHER_INFOS("A job as already the same id: " << new_job->getNumber());
99 throw LauncherException("A job as already the same id - job is not created !");
101 LAUNCHER_MESSAGE("New Job created");
104 //=============================================================================
108 //=============================================================================
110 Launcher_cpp::launchJob(int job_id)
112 LAUNCHER_MESSAGE("Launch a job");
114 // Check if job exist
115 std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(job_id);
116 if (it_job == _launcher_job_map.end())
118 LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
119 throw LauncherException("Cannot find the job, is it created ?");
122 Launcher::Job * job = it_job->second;
124 // Check job state (cannot launch a job already launched...)
125 if (job->getState() != "CREATED")
127 LAUNCHER_INFOS("Bad state of the job: " << job->getState());
128 throw LauncherException("Bad state of the job: " + job->getState());
131 // Third step search batch manager for the job into the map -> instanciate one if does not exist
133 std::map<int, Batch::BatchManager *>::const_iterator it = _batchmap.find(job_id);
134 if(it == _batchmap.end())
136 createBatchManagerForJob(job);
141 Batch::JobId batch_manager_job_id = _batchmap[job_id]->submitJob(*(job->getBatchJob()));
142 job->setBatchManagerJobId(batch_manager_job_id);
143 job->setState("QUEUED");
145 catch(const Batch::GenericException &ex)
147 LAUNCHER_INFOS("Job is not launched, exception in submitJob: " << ex.message);
148 throw LauncherException(ex.message.c_str());
150 LAUNCHER_MESSAGE("Job launched");
153 //=============================================================================
157 //=============================================================================
159 Launcher_cpp::getJobState(int job_id)
161 LAUNCHER_MESSAGE("Get job state");
163 // Check if job exist
164 std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(job_id);
165 if (it_job == _launcher_job_map.end())
167 LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
168 throw LauncherException("Cannot find the job, is it created ?");
171 Launcher::Job * job = it_job->second;
176 state = job->updateJobState();
178 catch(const Batch::GenericException &ex)
180 LAUNCHER_INFOS("getJobState failed, exception: " << ex.message);
181 throw LauncherException(ex.message.c_str());
184 return state.c_str();
187 //=============================================================================
189 * Get Job result - the result directory could be changed
191 //=============================================================================
193 Launcher_cpp::getJobResults(int job_id, std::string directory)
195 LAUNCHER_MESSAGE("Get Job results");
197 // Check if job exist
198 std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(job_id);
199 if (it_job == _launcher_job_map.end())
201 LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
202 throw LauncherException("Cannot find the job, is it created ?");
205 Launcher::Job * job = it_job->second;
206 std::string resource_name = job->getResourceDefinition().Name;
210 _batchmap[job_id]->importOutputFiles(*(job->getBatchJob()), directory);
212 _batchmap[job_id]->importOutputFiles(*(job->getBatchJob()), job->getResultDirectory());
214 catch(const Batch::GenericException &ex)
216 LAUNCHER_INFOS("getJobResult is maybe incomplete, exception: " << ex.message);
217 throw LauncherException(ex.message.c_str());
219 LAUNCHER_MESSAGE("getJobResult ended");
222 //=============================================================================
224 * Get Job dump state - the result directory could be changed
226 //=============================================================================
228 Launcher_cpp::getJobDumpState(int job_id, std::string directory)
231 LAUNCHER_MESSAGE("Get Job dump state");
233 // Check if job exist
234 std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(job_id);
235 if (it_job == _launcher_job_map.end())
237 LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
238 throw LauncherException("Cannot find the job, is it created ?");
241 Launcher::Job * job = it_job->second;
242 std::string resource_name = job->getResourceDefinition().Name;
246 rtn = _batchmap[job_id]->importDumpStateFile(*(job->getBatchJob()), directory);
248 rtn = _batchmap[job_id]->importDumpStateFile(*(job->getBatchJob()), job->getResultDirectory());
250 catch(const Batch::GenericException &ex)
252 LAUNCHER_INFOS("getJobResult is maybe incomplete, exception: " << ex.message);
253 throw LauncherException(ex.message.c_str());
255 LAUNCHER_MESSAGE("getJobResult ended");
259 //=============================================================================
261 * Remove the job - into the Launcher and its batch manager
263 //=============================================================================
265 Launcher_cpp::removeJob(int job_id)
267 LAUNCHER_MESSAGE("Remove Job");
269 // Check if job exist
270 std::map<int, Launcher::Job *>::iterator it_job = _launcher_job_map.find(job_id);
271 if (it_job == _launcher_job_map.end())
273 LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
274 throw LauncherException("Cannot find the job, is it created ?");
277 it_job->second->removeJob();
278 delete it_job->second;
279 _launcher_job_map.erase(it_job);
282 //=============================================================================
286 //=============================================================================
288 Launcher_cpp::stopJob(int job_id)
290 LAUNCHER_MESSAGE("Stop Job");
292 // Check if job exist
293 std::map<int, Launcher::Job *>::iterator it_job = _launcher_job_map.find(job_id);
294 if (it_job == _launcher_job_map.end())
296 LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
297 throw LauncherException("Cannot find the job, is it created ?");
300 it_job->second->stopJob();
303 //=============================================================================
305 * create a launcher job based on a file
306 * \param xmlExecuteFile : to define the execution on the batch cluster
308 //=============================================================================
310 Launcher_cpp::createJobWithFile(const std::string xmlExecuteFile,
311 const std::string clusterName)
313 LAUNCHER_MESSAGE("Begin of Launcher_cpp::createJobWithFile");
316 ParserLauncherType job_params = ParseXmlFile(xmlExecuteFile);
318 // Creating a new job
319 Launcher::Job_Command * new_job = new Launcher::Job_Command();
321 std::string cmdFile = Kernel_Utils::GetTmpFileName();
328 os.open(cmdFile.c_str(), std::ofstream::out );
329 os << "#! /bin/sh" << std::endl;
330 os << job_params.Command;
333 new_job->setJobFile(cmdFile);
334 new_job->setLocalDirectory(job_params.RefDirectory);
335 new_job->setWorkDirectory(job_params.MachinesList[clusterName].WorkDirectory);
336 new_job->setEnvFile(job_params.MachinesList[clusterName].EnvFile);
338 for(int i=0; i < job_params.InputFile.size(); i++)
339 new_job->add_in_file(job_params.InputFile[i]);
340 for(int i=0; i < job_params.OutputFile.size();i++)
341 new_job->add_out_file(job_params.OutputFile[i]);
344 p.hostname = clusterName;
347 p.nb_proc = job_params.NbOfProcesses;
349 p.nb_proc_per_node = 0;
352 new_job->setResourceRequiredParams(p);
355 return new_job->getNumber();
358 //=============================================================================
360 * Factory to instanciate the good batch manager for choosen cluster.
362 //=============================================================================
363 Batch::BatchManager *
364 Launcher_cpp::FactoryBatchManager(ParserResourcesType& params)
367 Batch::CommunicationProtocolType protocol;
368 Batch::FactBatchManager * fact;
370 std::string hostname = params.HostName;
372 switch(params.Protocol)
375 protocol = Batch::SH;
378 protocol = Batch::RSH;
381 protocol = Batch::SSH;
384 throw LauncherException("Unknown protocol for this resource");
416 switch( params.Batch )
443 LAUNCHER_MESSAGE("Bad batch description of the resource: Batch = " << params.Batch);
444 throw LauncherException("No batchmanager for that cluster - Bad batch description of the resource");
446 Batch::BatchManagerCatalog & cata = Batch::BatchManagerCatalog::getInstance();
447 fact = dynamic_cast<Batch::FactBatchManager*>(cata(bmType));
449 LAUNCHER_MESSAGE("Cannot find batch manager factory for " << bmType << ". Check your version of libBatch.");
450 throw LauncherException("Cannot find batch manager factory");
452 LAUNCHER_MESSAGE("Instanciation of batch manager of type: " << bmType);
453 Batch::BatchManager * batch_client = (*fact)(hostname.c_str(), params.UserName.c_str(),
454 protocol, mpi.c_str());
458 //----------------------------------------------------------
459 // Without LIBBATCH - Launcher_cpp do nothing...
460 //----------------------------------------------------------
464 Launcher_cpp::createJob(Launcher::Job * new_job)
466 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot create a job !!!");
468 throw LauncherException("Method Launcher_cpp::createJob is not available "
469 "(libBatch was not present at compilation time)");
473 Launcher_cpp::launchJob(int job_id)
475 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot launch a job !!!");
476 throw LauncherException("Method Launcher_cpp::launchJob is not available "
477 "(libBatch was not present at compilation time)");
481 Launcher_cpp::getJobState(int job_id)
483 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot get job state!!!");
484 throw LauncherException("Method Launcher_cpp::getJobState is not available "
485 "(libBatch was not present at compilation time)");
489 Launcher_cpp::getJobResults(int job_id, std::string directory)
491 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot get job results!!!");
492 throw LauncherException("Method Launcher_cpp::getJobResults is not available "
493 "(libBatch was not present at compilation time)");
497 Launcher_cpp::getJobDumpState(int job_id, std::string directory)
499 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot get job dump state!!!");
500 throw LauncherException("Method Launcher_cpp::getJobDumpState is not available "
501 "(libBatch was not present at compilation time)");
505 Launcher_cpp::removeJob(int job_id)
507 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot remove job!!!");
508 throw LauncherException("Method Launcher_cpp::removeJob is not available "
509 "(libBatch was not present at compilation time)");
513 Launcher_cpp::stopJob(int job_id)
515 throw LauncherException("Method Launcher_cpp::stopJob is not available "
516 "(libBatch was not present at compilation time)");
520 Launcher_cpp::createJobWithFile( const std::string xmlExecuteFile, std::string clusterName)
522 throw LauncherException("Method Launcher_cpp::createJobWithFile is not available "
523 "(libBatch was not present at compilation time)");
530 Launcher_cpp::ParseXmlFile(std::string xmlExecuteFile)
532 ParserLauncherType job_params;
533 SALOME_Launcher_Handler * handler = new SALOME_Launcher_Handler(job_params);
535 const char* aFilePath = xmlExecuteFile.c_str();
536 FILE* aFile = fopen(aFilePath, "r");
539 xmlDocPtr aDoc = xmlReadFile(aFilePath, NULL, 0);
541 handler->ProcessXmlDocument(aDoc);
544 std::string message = "ResourcesManager_cpp: could not parse file: " + xmlExecuteFile;
545 LAUNCHER_MESSAGE(message);
547 throw LauncherException(message);
555 std::string message = "ResourcesManager_cpp: file is not readable: " + xmlExecuteFile;
556 LAUNCHER_MESSAGE(message);
558 throw LauncherException(message);
566 std::map<int, Launcher::Job *>
567 Launcher_cpp::getJobs()
569 return _launcher_job_map;
573 Launcher_cpp::createBatchManagerForJob(Launcher::Job * job)
575 int job_id = job->getNumber();
577 // Select a ressource for the job
578 std::vector<std::string> ResourceList;
579 resourceParams params = job->getResourceRequiredParams();
580 // Consider only resources that can launch batch jobs
581 params.can_launch_batch_jobs = true;
584 ResourceList = _ResManager->GetFittingResources(params);
586 catch(const ResourcesException &ex)
588 throw LauncherException(ex.msg.c_str());
590 if (ResourceList.size() == 0)
592 LAUNCHER_INFOS("No adequate resource found for the job, number " << job->getNumber());
593 job->setState("ERROR");
594 throw LauncherException("No resource found the job");
597 // Configure the job with the resource selected - the first of the list
598 ParserResourcesType resource_definition = _ResManager->GetResourcesDescr(ResourceList[0]);
600 // Set resource definition to the job
601 // The job will check if the definitions needed
604 job->setResourceDefinition(resource_definition);
606 catch(const LauncherException &ex)
608 LAUNCHER_INFOS("Error in the definition of the resource, mess: " << ex.msg);
609 job->setState("ERROR");
613 // Step 2: We can now add a Factory if the resource is correctly define
615 std::map<int, Batch::BatchManager *>::const_iterator it = _batchmap.find(job_id);
616 if(it == _batchmap.end())
620 // Warning cannot write on one line like this, because map object is constructed before
621 // the method is called...
622 //_batchmap[job_id] = FactoryBatchManager(resource_definition);
623 Batch::BatchManager * batch_client = FactoryBatchManager(resource_definition);
624 _batchmap[job_id] = batch_client;
626 catch(const LauncherException &ex)
628 LAUNCHER_INFOS("Error during creation of the batch manager of the job, mess: " << ex.msg);
631 catch(const Batch::GenericException &ex)
633 LAUNCHER_INFOS("Error during creation of the batch manager of the job, mess: " << ex.message);
634 throw LauncherException(ex.message);
641 Launcher_cpp::addJobDirectlyToMap(Launcher::Job * new_job, const std::string job_reference)
643 // Step 0: Calculated job_id
644 pthread_mutex_lock(_job_cpt_mutex);
645 int job_id = _job_cpt;
647 new_job->setNumber(job_id);
648 pthread_mutex_unlock(_job_cpt_mutex);
650 // Step 1: check if resource is already in the map
651 createBatchManagerForJob(new_job);
653 // Step 2: add the job to the batch manager
657 Batch::JobId batch_manager_job_id = _batchmap[job_id]->addJob(*(new_job->getBatchJob()),
659 new_job->setBatchManagerJobId(batch_manager_job_id);
661 catch(const Batch::GenericException &ex)
663 LAUNCHER_INFOS("Job cannot be added, exception in addJob: " << ex.message);
664 throw LauncherException(ex.message.c_str());
667 // Step 3: add job to launcher map
668 std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(new_job->getNumber());
669 if (it_job == _launcher_job_map.end())
670 _launcher_job_map[new_job->getNumber()] = new_job;
673 LAUNCHER_INFOS("A job as already the same id: " << new_job->getNumber());
675 throw LauncherException("A job as already the same id - job is not created !");
677 LAUNCHER_MESSAGE("New job added");