1 // Copyright (C) 2007-2010 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 <Batch/Batch_Date.hxx>
25 #include <Batch/Batch_BatchManagerCatalog.hxx>
26 #include <Batch/Batch_FactBatchManager_eClient.hxx>
27 #include <Batch/Batch_BatchManager_eClient.hxx>
30 #include "Basics_Utils.hxx"
31 #include "Basics_DirUtils.hxx"
32 #include "SALOME_Launcher_Handler.hxx"
33 #include "Launcher.hxx"
34 #include "Launcher_Job_Command.hxx"
40 //=============================================================================
44 * Define a CORBA single thread policy for the server, which avoid to deal
45 * with non thread-safe usage like Change_Directory in SALOME naming service
47 //=============================================================================
48 Launcher_cpp::Launcher_cpp()
50 LAUNCHER_MESSAGE("Launcher_cpp constructor");
52 _job_cpt_mutex = new pthread_mutex_t();
53 pthread_mutex_init(_job_cpt_mutex, NULL);
56 //=============================================================================
60 //=============================================================================
61 Launcher_cpp::~Launcher_cpp()
63 LAUNCHER_MESSAGE("Launcher_cpp destructor");
65 std::map<int, Launcher::Job *>::const_iterator it_job;
66 for(it_job = _launcher_job_map.begin(); it_job != _launcher_job_map.end(); it_job++)
67 delete it_job->second;
68 std::map < std::string, Batch::BatchManager_eClient * >::const_iterator it1;
69 for(it1=_batchmap.begin();it1!=_batchmap.end();it1++)
73 pthread_mutex_destroy(_job_cpt_mutex);
74 delete _job_cpt_mutex;
79 //=============================================================================
81 * Add a job into the launcher - check resource and choose one
83 //=============================================================================
85 Launcher_cpp::createJob(Launcher::Job * new_job)
87 LAUNCHER_MESSAGE("Creating a new job");
89 // First step take a resource
90 std::vector<std::string> ResourceList;
91 resourceParams params = new_job->getResourceRequiredParams();
93 ResourceList = _ResManager->GetFittingResources(params);
95 catch(const ResourcesException &ex){
96 throw LauncherException(ex.msg.c_str());
98 if (ResourceList.size() == 0)
100 LAUNCHER_INFOS("No adequate resource found for the job, number " << new_job->getNumber() << " - deleting it");
102 throw LauncherException("No resource found the job");
105 // Second step configure the job with the resource selected - the first of the list
106 ParserResourcesType resource_definition = _ResManager->GetResourcesDescr(ResourceList[0]);
108 // Set resource definition to the job
109 // The job will check if the definitions needed
112 new_job->setResourceDefinition(resource_definition);
114 catch(const LauncherException &ex)
116 LAUNCHER_INFOS("Error in the definition of the resource, mess: " << ex.msg);
121 // Third step search batch manager for the resource into the map -> instanciate one if does not exist
123 std::string resource_name = resource_definition.Name;
124 std::map<std::string, Batch::BatchManager_eClient *>::const_iterator it = _batchmap.find(resource_name);
125 if(it == _batchmap.end())
129 // Warning cannot write on one line like this, because map object is constructed before
130 // the method is called...
131 //_batchmap.[resource_name] = FactoryBatchManager(resource_definition);
132 Batch::BatchManager_eClient * batch_client = FactoryBatchManager(resource_definition);
133 _batchmap[resource_name] = batch_client;
135 catch(const LauncherException &ex)
137 LAUNCHER_INFOS("Error during creation of the batch manager of the resource, mess: " << ex.msg);
141 catch(const Batch::EmulationException &ex)
143 LAUNCHER_INFOS("Error during creation of the batch manager of the resource, mess: " << ex.message);
145 throw LauncherException(ex.message);
147 catch(const Batch::InvalidArgumentException &ex)
149 LAUNCHER_INFOS("Error during creation of the batch manager of the resource, mess: " << ex.message);
150 throw LauncherException(ex.message);
155 // Final step - add job to the jobs map
156 pthread_mutex_lock(_job_cpt_mutex);
157 new_job->setNumber(_job_cpt);
159 pthread_mutex_unlock(_job_cpt_mutex);
160 std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(new_job->getNumber());
161 if (it_job == _launcher_job_map.end())
162 _launcher_job_map[new_job->getNumber()] = new_job;
165 LAUNCHER_INFOS("A job as already the same id: " << new_job->getNumber());
167 throw LauncherException("A job as already the same id - job is not created !");
169 LAUNCHER_MESSAGE("New Job created");
172 //=============================================================================
176 //=============================================================================
178 Launcher_cpp::launchJob(int job_id)
180 LAUNCHER_MESSAGE("Launch a job");
182 // Check if job exist
183 std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(job_id);
184 if (it_job == _launcher_job_map.end())
186 LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
187 throw LauncherException("Cannot find the job, is it created ?");
190 Launcher::Job * job = it_job->second;
192 // Check job state (cannot launch a job already launched...)
193 if (job->getState() != "CREATED")
195 LAUNCHER_INFOS("Bad state of the job: " << job->getState());
196 throw LauncherException("Bad state of the job: " + job->getState());
199 std::string resource_name = job->getResourceDefinition().Name;
201 Batch::JobId batch_manager_job_id = _batchmap[resource_name]->submitJob(*(job->getBatchJob()));
202 job->setBatchManagerJobId(batch_manager_job_id);
203 job->setState("QUEUED");
205 catch(const Batch::EmulationException &ex)
207 LAUNCHER_INFOS("Job is not launched, exception in submitJob: " << ex.message);
208 throw LauncherException(ex.message.c_str());
210 LAUNCHER_MESSAGE("Job launched");
213 //=============================================================================
217 //=============================================================================
219 Launcher_cpp::getJobState(int job_id)
221 LAUNCHER_MESSAGE("Get job state");
223 // Check if job exist
224 std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(job_id);
225 if (it_job == _launcher_job_map.end())
227 LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
228 throw LauncherException("Cannot find the job, is it created ?");
231 Launcher::Job * job = it_job->second;
232 std::string state = job->updateJobState();
234 return state.c_str();
237 //=============================================================================
239 * Get Job result - the result directory could be changed
241 //=============================================================================
243 Launcher_cpp::getJobResults(int job_id, std::string directory)
245 LAUNCHER_MESSAGE("Get Job results");
247 // Check if job exist
248 std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(job_id);
249 if (it_job == _launcher_job_map.end())
251 LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
252 throw LauncherException("Cannot find the job, is it created ?");
255 Launcher::Job * job = it_job->second;
256 std::string resource_name = job->getResourceDefinition().Name;
260 _batchmap[resource_name]->importOutputFiles(*(job->getBatchJob()), directory);
262 _batchmap[resource_name]->importOutputFiles(*(job->getBatchJob()), job->getResultDirectory());
264 catch(const Batch::EmulationException &ex)
266 LAUNCHER_INFOS("getJobResult is maybe incomplete, exception: " << ex.message);
267 throw LauncherException(ex.message.c_str());
269 LAUNCHER_MESSAGE("getJobResult ended");
272 //=============================================================================
274 * Get Job dump state - the result directory could be changed
276 //=============================================================================
278 Launcher_cpp::getJobDumpState(int job_id, std::string directory)
281 LAUNCHER_MESSAGE("Get Job dump state");
283 // Check if job exist
284 std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(job_id);
285 if (it_job == _launcher_job_map.end())
287 LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
288 throw LauncherException("Cannot find the job, is it created ?");
291 Launcher::Job * job = it_job->second;
292 std::string resource_name = job->getResourceDefinition().Name;
296 rtn = _batchmap[resource_name]->importDumpStateFile(*(job->getBatchJob()), directory);
298 rtn = _batchmap[resource_name]->importDumpStateFile(*(job->getBatchJob()), job->getResultDirectory());
300 catch(const Batch::EmulationException &ex)
302 LAUNCHER_INFOS("getJobResult is maybe incomplete, exception: " << ex.message);
303 throw LauncherException(ex.message.c_str());
305 LAUNCHER_MESSAGE("getJobResult ended");
309 //=============================================================================
311 * Remove the job - into the Launcher and its batch manager
313 //=============================================================================
315 Launcher_cpp::removeJob(int job_id)
317 LAUNCHER_MESSAGE("Remove Job");
319 // Check if job exist
320 std::map<int, Launcher::Job *>::iterator it_job = _launcher_job_map.find(job_id);
321 if (it_job == _launcher_job_map.end())
323 LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
324 throw LauncherException("Cannot find the job, is it created ?");
327 delete it_job->second;
328 _launcher_job_map.erase(it_job);
331 //=============================================================================
333 * create a launcher job based on a file
334 * \param xmlExecuteFile : to define the execution on the batch cluster
336 //=============================================================================
338 Launcher_cpp::createJobWithFile(const std::string xmlExecuteFile,
339 const std::string clusterName)
341 LAUNCHER_MESSAGE("Begin of Launcher_cpp::createJobWithFile");
344 ParserLauncherType job_params = ParseXmlFile(xmlExecuteFile);
346 // Creating a new job
347 Launcher::Job_Command * new_job = new Launcher::Job_Command();
349 std::string cmdFile = Kernel_Utils::GetTmpFileName();
356 os.open(cmdFile.c_str(), std::ofstream::out );
357 os << "#! /bin/sh" << std::endl;
358 os << job_params.Command;
361 new_job->setJobFile(cmdFile);
362 new_job->setLocalDirectory(job_params.RefDirectory);
363 new_job->setWorkDirectory(job_params.MachinesList[clusterName].WorkDirectory);
364 new_job->setEnvFile(job_params.MachinesList[clusterName].EnvFile);
366 for(int i=0; i < job_params.InputFile.size(); i++)
367 new_job->add_in_file(job_params.InputFile[i]);
368 for(int i=0; i < job_params.OutputFile.size();i++)
369 new_job->add_out_file(job_params.OutputFile[i]);
372 p.hostname = clusterName;
375 p.nb_proc = job_params.NbOfProcesses;
377 p.nb_proc_per_node = 0;
380 new_job->setResourceRequiredParams(p);
383 return new_job->getNumber();
386 //=============================================================================
388 * Factory to instanciate the good batch manager for choosen cluster.
390 //=============================================================================
391 Batch::BatchManager_eClient *
392 Launcher_cpp::FactoryBatchManager(ParserResourcesType& params)
395 Batch::CommunicationProtocolType protocol;
396 Batch::FactBatchManager_eClient* fact;
398 int nb_proc_per_node = params.DataForSort._nbOfProcPerNode;
399 std::string hostname = params.HostName;
401 switch(params.Protocol)
404 protocol = Batch::RSH;
407 protocol = Batch::SSH;
410 throw LauncherException("Unknown protocol for this resource");
439 switch( params.Batch )
460 LAUNCHER_MESSAGE("Bad batch description of the resource: Batch = " << params.Batch);
461 throw LauncherException("No batchmanager for that cluster - Bad batch description of the resource");
463 Batch::BatchManagerCatalog & cata = Batch::BatchManagerCatalog::getInstance();
464 fact = dynamic_cast<Batch::FactBatchManager_eClient*>(cata(bmType));
466 LAUNCHER_MESSAGE("Cannot find batch manager factory for " << bmType << ". Check your version of libBatch.");
467 throw LauncherException("Cannot find batch manager factory");
469 LAUNCHER_MESSAGE("Instanciation of batch manager of type: " << bmType);
470 Batch::BatchManager_eClient * batch_client = (*fact)(hostname.c_str(), params.UserName.c_str(),
471 protocol, mpi.c_str(), nb_proc_per_node);
475 //----------------------------------------------------------
476 // Without LIBBATCH - Launcher_cpp do nothing...
477 //----------------------------------------------------------
481 Launcher_cpp::createJob(Launcher::Job * new_job)
483 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot create a job !!!");
485 throw LauncherException("Method Launcher_cpp::createJob is not available "
486 "(libBatch was not present at compilation time)");
490 Launcher_cpp::launchJob(int job_id)
492 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot launch a job !!!");
493 throw LauncherException("Method Launcher_cpp::launchJob is not available "
494 "(libBatch was not present at compilation time)");
498 Launcher_cpp::getJobState(int job_id)
500 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot get job state!!!");
501 throw LauncherException("Method Launcher_cpp::getJobState is not available "
502 "(libBatch was not present at compilation time)");
506 Launcher_cpp::getJobResults(int job_id, std::string directory)
508 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot get job results!!!");
509 throw LauncherException("Method Launcher_cpp::getJobResults is not available "
510 "(libBatch was not present at compilation time)");
514 Launcher_cpp::getJobDumpState(int job_id, std::string directory)
516 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot get job dump state!!!");
517 throw LauncherException("Method Launcher_cpp::getJobDumpState is not available "
518 "(libBatch was not present at compilation time)");
522 Launcher_cpp::removeJob(int job_id)
524 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot remove job!!!");
525 throw LauncherException("Method Launcher_cpp::removeJob is not available "
526 "(libBatch was not present at compilation time)");
530 Launcher_cpp::createJobWithFile( const std::string xmlExecuteFile, std::string clusterName)
532 throw LauncherException("Method Launcher_cpp::createJobWithFile is not available "
533 "(libBatch was not present at compilation time)");
540 Launcher_cpp::ParseXmlFile(std::string xmlExecuteFile)
542 ParserLauncherType job_params;
543 SALOME_Launcher_Handler * handler = new SALOME_Launcher_Handler(job_params);
545 const char* aFilePath = xmlExecuteFile.c_str();
546 FILE* aFile = fopen(aFilePath, "r");
549 xmlDocPtr aDoc = xmlReadFile(aFilePath, NULL, 0);
551 handler->ProcessXmlDocument(aDoc);
554 std::string message = "ResourcesManager_cpp: could not parse file: " + xmlExecuteFile;
555 LAUNCHER_MESSAGE(message);
557 throw LauncherException(message);
565 std::string message = "ResourcesManager_cpp: file is not readable: " + xmlExecuteFile;
566 LAUNCHER_MESSAGE(message);
568 throw LauncherException(message);
576 std::map<int, Launcher::Job *>
577 Launcher_cpp::getJobs()
579 return _launcher_job_map;
583 Launcher_cpp::checkFactoryForResource(const std::string & resource_name)
585 // Step 1: Check if resource exist in the resource manager
586 ParserResourcesType resource_definition;
589 resource_definition = _ResManager->GetResourcesDescr(resource_name);
591 catch(const ResourcesException &ex)
593 LAUNCHER_INFOS(ex.msg);
594 throw LauncherException(ex.msg);
597 // Step 2: We can now add a Factory is the resource is correctly define
599 std::map<std::string, Batch::BatchManager_eClient *>::const_iterator it = _batchmap.find(resource_name);
600 if(it == _batchmap.end())
604 // Warning cannot write on one line like this, because map object is constructed before
605 // the method is called...
606 //_batchmap.[resource_name] = FactoryBatchManager(resource_definition);
607 Batch::BatchManager_eClient * batch_client = FactoryBatchManager(resource_definition);
608 _batchmap[resource_name] = batch_client;
610 catch(const LauncherException &ex)
612 LAUNCHER_INFOS("Error during creation of the batch manager of the resource, mess: " << ex.msg);
615 catch(const Batch::EmulationException &ex)
617 LAUNCHER_INFOS("Error during creation of the batch manager of the resource, mess: " << ex.message);
618 throw LauncherException(ex.message);
620 catch(const Batch::InvalidArgumentException &ex)
622 LAUNCHER_INFOS("Error during creation of the batch manager of the resource, mess: " << ex.message);
623 throw LauncherException(ex.message);
630 Launcher_cpp::addJobDirectlyToMap(Launcher::Job * new_job, const std::string job_reference)
632 // Step 1: check if resource is already in the map
633 std::string resource_name = new_job->getResourceDefinition().Name;
634 checkFactoryForResource(resource_name);
635 ParserResourcesType resource_definition = _ResManager->GetResourcesDescr(resource_name);
636 new_job->setResourceDefinition(resource_definition);
638 // Step 2: add the job to the batch manager
642 Batch::JobId batch_manager_job_id = _batchmap[resource_name]->addJob(*(new_job->getBatchJob()),
644 new_job->setBatchManagerJobId(batch_manager_job_id);
646 catch(const Batch::EmulationException &ex)
648 LAUNCHER_INFOS("Job is not launched, exception in submitJob: " << ex.message);
649 throw LauncherException(ex.message.c_str());
652 // Step 3: add job to launcher map
653 pthread_mutex_lock(_job_cpt_mutex);
654 new_job->setNumber(_job_cpt);
656 pthread_mutex_unlock(_job_cpt_mutex);
657 std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(new_job->getNumber());
658 if (it_job == _launcher_job_map.end())
659 _launcher_job_map[new_job->getNumber()] = new_job;
662 LAUNCHER_INFOS("A job as already the same id: " << new_job->getNumber());
664 throw LauncherException("A job as already the same id - job is not created !");
666 LAUNCHER_MESSAGE("New job added");