-// Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2021 CEA/DEN, EDF R&D, OPEN CASCADE
//
// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
#include <sstream>
#include <sys/stat.h>
#include <time.h>
+#include <memory>
#ifdef WITH_LIBBATCH
#include <libbatch/BatchManagerCatalog.hxx>
#include "SALOME_Launcher_Handler.hxx"
#include "Launcher.hxx"
#include "Launcher_Job_Command.hxx"
+#include "Launcher_Job_YACSFile.hxx"
+#include "Launcher_Job_PythonSALOME.hxx"
+#include "Launcher_Job_CommandSALOME.hxx"
#include "Launcher_XML_Persistence.hxx"
using namespace std;
-class ReadLock
-{
-public:
- ReadLock(pthread_rwlock_t * lock)
- : _lock(lock)
- {
-// pthread_rwlock_rdlock(_lock);
- pthread_rwlock_wrlock(_lock);
- }
- ~ReadLock()
- {
- pthread_rwlock_unlock(_lock);
- }
-private:
- pthread_rwlock_t * _lock;
-};
-
-class WriteLock
-{
-public:
- WriteLock(pthread_rwlock_t * lock)
- : _lock(lock)
- {
- pthread_rwlock_wrlock(_lock);
- }
- ~WriteLock()
- {
- pthread_rwlock_unlock(_lock);
- }
-private:
- pthread_rwlock_t * _lock;
-};
-
//=============================================================================
-/*!
+/*!
* Constructor
* \param orb
* Define a CORBA single thread policy for the server, which avoid to deal
{
LAUNCHER_MESSAGE("Launcher_cpp constructor");
_job_cpt = 0;
- _lock = new pthread_rwlock_t();
- pthread_rwlock_init(_lock, NULL);
}
//=============================================================================
-/*!
+/*!
* destructor
*/
//=============================================================================
for(it1=_batchmap.begin();it1!=_batchmap.end();it1++)
delete it1->second;
#endif
-
- pthread_rwlock_destroy(_lock);
- delete _lock;
}
#ifdef WITH_LIBBATCH
//=============================================================================
/*!
- * Add a job into the launcher - check resource and choose one
- */
+ * Add a job into the launcher - check resource and choose one
+ */
//=============================================================================
-void
+void
Launcher_cpp::createJob(Launcher::Job * new_job)
{
- WriteLock mutex(_lock);
LAUNCHER_MESSAGE("Creating a new job");
// Add job to the jobs map
new_job->setNumber(_job_cpt);
else
{
LAUNCHER_INFOS("A job has already the same id: " << new_job->getNumber());
- delete new_job;
throw LauncherException("A job has already the same id - job is not created !");
}
LAUNCHER_MESSAGE("New Job created");
//=============================================================================
/*!
- * Launch a job
- */
+ * Add a job into the launcher - check resource and choose one
+ */
+//=============================================================================
+int
+Launcher_cpp::createJob(const JobParameters_cpp& job_parameters)
+{
+ std::string job_type = job_parameters.job_type;
+ Launcher::Job * new_job; // It is Launcher_cpp that is going to destroy it
+
+ if (job_type == Launcher::Job_Command::TYPE_NAME)
+ new_job = new Launcher::Job_Command();
+ else if (job_type == Launcher::Job_CommandSALOME::TYPE_NAME)
+ new_job = new Launcher::Job_CommandSALOME();
+ else if (job_type == Launcher::Job_YACSFile::TYPE_NAME)
+ new_job = new Launcher::Job_YACSFile();
+ else if (job_type == Launcher::Job_PythonSALOME::TYPE_NAME)
+ new_job = new Launcher::Job_PythonSALOME();
+ else
+ {
+ std::string message("Launcher_cpp::createJob: bad job type: ");
+ message += job_type;
+ throw LauncherException(message.c_str());
+ }
+
+ // Name
+ new_job->setJobName(job_parameters.job_name);
+
+ // Directories
+ new_job->setWorkDirectory(job_parameters.work_directory);
+ new_job->setLocalDirectory(job_parameters.local_directory);
+ new_job->setResultDirectory(job_parameters.result_directory);
+
+ // Parameters for COORM
+ new_job->setLauncherFile(job_parameters.launcher_file);
+ new_job->setLauncherArgs(job_parameters.launcher_args);
+
+ // Job File
+ new_job->setJobFile(job_parameters.job_file);
+ new_job->setPreCommand(job_parameters.pre_command);
+
+ // Files
+ new_job->setEnvFile(job_parameters.env_file);
+ for(const std::string param : job_parameters.in_files)
+ new_job->add_in_file(param);
+ for(const std::string param : job_parameters.out_files)
+ new_job->add_out_file(param);
+
+ new_job->setMaximumDuration(job_parameters.maximum_duration);
+ new_job->setQueue(job_parameters.queue);
+ new_job->setPartition(job_parameters.partition);
+ new_job->setExclusive(job_parameters.exclusive);
+ new_job->setMemPerCpu(job_parameters.mem_per_cpu);
+ new_job->setWCKey(job_parameters.wckey);
+ new_job->setExtraParams(job_parameters.extra_params);
+
+ // Resources requirements
+ new_job->setResourceRequiredParams(job_parameters.resource_required);
+
+ // Adding specific parameters to the job
+ for (const auto& it_specific : job_parameters.specific_parameters)
+ new_job->addSpecificParameter(it_specific.first, it_specific.second);
+
+ new_job->checkSpecificParameters();
+
+ createJob(new_job);
+
+ return new_job->getNumber();
+}
+
+//=============================================================================
+/*!
+ * Launch a job
+ */
//=============================================================================
-void
+void
Launcher_cpp::launchJob(int job_id)
{
- ReadLock mutex(_lock);
LAUNCHER_MESSAGE("Launch a job");
- // Check if job exist
+ // Check if job exists
Launcher::Job * job = findJob(job_id);
// Check job state (cannot launch a job already launched...)
throw LauncherException("Bad state of the job: " + job->getState());
}
- // Third step search batch manager for the job into the map -> instantiate one if does not exist
- std::map<int, Batch::BatchManager *>::const_iterator it = _batchmap.find(job_id);
- if(it == _batchmap.end())
- {
- createBatchManagerForJob(job);
- }
+ Batch::BatchManager * bm = getBatchManager(job);
try {
- Batch::JobId batch_manager_job_id = _batchmap[job_id]->submitJob(*(job->getBatchJob()));
+ Batch::JobId batch_manager_job_id = bm->submitJob(*(job->getBatchJob()));
job->setBatchManagerJobId(batch_manager_job_id);
job->setState("QUEUED");
job->setReference(batch_manager_job_id.getReference());
//=============================================================================
/*!
* Get job state
- */
+ */
//=============================================================================
-const char *
+std::string
Launcher_cpp::getJobState(int job_id)
{
- ReadLock mutex(_lock);
LAUNCHER_MESSAGE("Get job state");
// Check if job exist
throw LauncherException(ex.message.c_str());
}
- return state.c_str();
+ return state;
}
//=============================================================================
* Get job assigned hostnames
*/
//=============================================================================
-const char *
+std::string
Launcher_cpp::getAssignedHostnames(int job_id)
{
- ReadLock mutex(_lock);
LAUNCHER_MESSAGE("Get job assigned hostnames");
// Check if job exist
Launcher::Job * job = findJob(job_id);
std::string assigned_hostnames = job->getAssignedHostnames();
- return assigned_hostnames.c_str();
+ return assigned_hostnames;
+}
+
+void
+Launcher_cpp::exportInputFiles(int job_id)
+{
+ LAUNCHER_MESSAGE("Copy in files");
+ // Check if job exists
+ Launcher::Job * job = findJob(job_id);
+
+ // Check job state (cannot launch a job already launched...)
+ if (job->getState() != "CREATED")
+ {
+ LAUNCHER_INFOS("Bad state of the job: " << job->getState());
+ throw LauncherException("Bad state of the job: " + job->getState());
+ }
+
+ Batch::BatchManager * bm = getBatchManager(job);
+
+ try {
+ bm->exportInputFiles(*(job->getBatchJob()));
+ }
+ catch(const Batch::GenericException &ex)
+ {
+ LAUNCHER_INFOS("Failed to copy in files: " << ex.message);
+ throw LauncherException(ex.message.c_str());
+ }
+ LAUNCHER_MESSAGE("Files copied");
}
//=============================================================================
/*!
* Get Job result - the result directory could be changed
- */
+ */
//=============================================================================
void
Launcher_cpp::getJobResults(int job_id, std::string directory)
{
- ReadLock mutex(_lock);
LAUNCHER_MESSAGE("Get Job results");
Launcher::Job * job = findJob(job_id);
std::string resource_name = job->getResourceDefinition().Name;
- try
+ try
{
if (directory != "")
_batchmap[job_id]->importOutputFiles(*(job->getBatchJob()), directory);
void
Launcher_cpp::clearJobWorkingDir(int job_id)
{
- ReadLock mutex(_lock);
LAUNCHER_MESSAGE("Clear the remote working directory");
Launcher::Job * job = findJob(job_id);
//=============================================================================
/*!
* Get Job dump state - the result directory could be changed
- */
+ */
//=============================================================================
bool
Launcher_cpp::getJobDumpState(int job_id, std::string directory)
{
bool rtn;
- ReadLock mutex(_lock);
LAUNCHER_MESSAGE("Get Job dump state");
Launcher::Job * job = findJob(job_id);
std::string resource_name = job->getResourceDefinition().Name;
- try
+ try
{
if (directory != "")
rtn = _batchmap[job_id]->importDumpStateFile(*(job->getBatchJob()), directory);
std::string directory)
{
bool rtn;
- ReadLock mutex(_lock);
LAUNCHER_MESSAGE("Get working file " << work_file);
Launcher::Job * job = findJob(job_id);
//=============================================================================
/*!
* Remove the job - into the Launcher and its batch manager
- */
+ */
//=============================================================================
void
Launcher_cpp::removeJob(int job_id)
{
- WriteLock mutex(_lock);
LAUNCHER_MESSAGE("Remove Job");
// Check if job exist
//=============================================================================
/*!
* stop the job
- */
+ */
//=============================================================================
void
Launcher_cpp::stopJob(int job_id)
{
- ReadLock mutex(_lock);
LAUNCHER_MESSAGE("Stop Job");
Launcher::Job * job = findJob(job_id);
std::string
Launcher_cpp::dumpJob(int job_id)
{
- ReadLock mutex(_lock);
LAUNCHER_MESSAGE("dump Job");
Launcher::Job * job = findJob(job_id);
Launcher_cpp::restoreJob(const std::string& dumpedJob)
{
LAUNCHER_MESSAGE("restore Job");
- Launcher::Job * new_job=NULL;
+ Launcher::Job* new_job(nullptr);
int jobId = -1;
try
{
- new_job = Launcher::XML_Persistence::createJobFromString(dumpedJob);
+ {
+ new_job = Launcher::XML_Persistence::createJobFromString(dumpedJob);
+ }
if(new_job)
{
jobId = addJob(new_job);
- if(jobId < 0)
- delete new_job;
}
+ else
+ LAUNCHER_INFOS("Failed to create a new job.");
}
catch(const LauncherException &ex)
{
LAUNCHER_INFOS("Cannot load the job. Exception: " << ex.msg.c_str());
- if(new_job)
- delete new_job;
}
return jobId;
}
+JobParameters_cpp
+Launcher_cpp::getJobParameters(int job_id)
+{
+ Launcher::Job * job = findJob(job_id);
+ JobParameters_cpp job_parameters;
+ job_parameters.job_name = job->getJobName();
+ job_parameters.job_type = job->getJobType();
+ job_parameters.job_file = job->getJobFile();
+ job_parameters.env_file = job->getEnvFile();
+ job_parameters.work_directory = job->getWorkDirectory();
+ job_parameters.local_directory = job->getLocalDirectory();
+ job_parameters.result_directory = job->getResultDirectory();
+ job_parameters.pre_command = job->getPreCommand();
+
+ // Parameters for COORM
+ job_parameters.launcher_file = job->getLauncherFile();
+ job_parameters.launcher_args = job->getLauncherArgs();
+
+ job_parameters.in_files = job->get_in_files();
+ job_parameters.out_files = job->get_out_files();
+
+ job_parameters.maximum_duration = job->getMaximumDuration();
+ job_parameters.queue = job->getQueue();
+ job_parameters.partition = job->getPartition();
+ job_parameters.exclusive = job->getExclusive();
+ job_parameters.mem_per_cpu = job->getMemPerCpu();
+ job_parameters.wckey = job->getWCKey();
+ job_parameters.extra_params = job->getExtraParams();
+
+ resourceParams resource_params = job->getResourceRequiredParams();
+ job_parameters.resource_required.name = resource_params.name;
+ job_parameters.resource_required.hostname = resource_params.hostname;
+ job_parameters.resource_required.OS = resource_params.OS;
+ job_parameters.resource_required.nb_proc = resource_params.nb_proc;
+ job_parameters.resource_required.nb_node = resource_params.nb_node;
+ job_parameters.resource_required.nb_proc_per_node = resource_params.nb_proc_per_node;
+ job_parameters.resource_required.cpu_clock = resource_params.cpu_clock;
+ job_parameters.resource_required.mem_mb = resource_params.mem_mb;
+
+ job_parameters.specific_parameters = job->getSpecificParameters();
+
+ return job_parameters;
+}
+
//=============================================================================
-/*!
+/*!
* create a launcher job based on a file
* \param xmlExecuteFile : to define the execution on the batch cluster
*/
//=============================================================================
-long
-Launcher_cpp::createJobWithFile(const std::string xmlExecuteFile,
+long
+Launcher_cpp::createJobWithFile(const std::string xmlExecuteFile,
const std::string clusterName)
{
LAUNCHER_MESSAGE("Begin of Launcher_cpp::createJobWithFile");
ParserLauncherType job_params = ParseXmlFile(xmlExecuteFile);
// Creating a new job
- Launcher::Job_Command * new_job = new Launcher::Job_Command();
+ std::unique_ptr<Launcher::Job_Command> new_job(new Launcher::Job_Command);
- std::string cmdFile = Kernel_Utils::GetTmpFileName();
+ std::string cmdFile = Kernel_Utils::GetTmpFileName();
#ifndef WIN32
cmdFile += ".sh";
#else
new_job->setWorkDirectory(job_params.MachinesList[clusterName].WorkDirectory);
new_job->setEnvFile(job_params.MachinesList[clusterName].EnvFile);
- for(int i=0; i < job_params.InputFile.size(); i++)
+ for(size_t i=0; i < job_params.InputFile.size(); i++)
new_job->add_in_file(job_params.InputFile[i]);
- for(int i=0; i < job_params.OutputFile.size();i++)
+ for(size_t i=0; i < job_params.OutputFile.size();i++)
new_job->add_out_file(job_params.OutputFile[i]);
resourceParams p;
p.mem_mb = 0;
new_job->setResourceRequiredParams(p);
- createJob(new_job);
+ createJob(new_job.get());
return new_job->getNumber();
}
//=============================================================================
/*!
* Factory to instantiate the good batch manager for chosen cluster.
- */
+ */
//=============================================================================
Batch::BatchManager *
Launcher_cpp::FactoryBatchManager(ParserResourcesType& params)
//----------------------------------------------------------
#else
-void
+void
Launcher_cpp::createJob(Launcher::Job * new_job)
{
LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot create a job !!!");
- delete new_job;
throw LauncherException("Method Launcher_cpp::createJob is not available "
"(libBatch was not present at compilation time)");
}
-void
+int
+Launcher_cpp::createJob(const JobParameters_cpp& job_parameters)
+{
+ LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot create a job !!!");
+ throw LauncherException("Method Launcher_cpp::createJob is not available "
+ "(libBatch was not present at compilation time)");
+}
+
+void
Launcher_cpp::launchJob(int job_id)
{
LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot launch a job !!!");
"(libBatch was not present at compilation time)");
}
-const char *
+std::string
Launcher_cpp::getJobState(int job_id)
{
LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot get job state!!!");
"(libBatch was not present at compilation time)");
}
-const char *
+std::string
Launcher_cpp::getAssignedHostnames(int job_id)
{
LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot get job assigned hostnames!!!");
"(libBatch was not present at compilation time)");
}
+void
+Launcher_cpp::exportInputFiles(int job_id)
+{
+ LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot copy the files!!!");
+ throw LauncherException("Method Launcher_cpp::exportInputFiles is not available "
+ "(libBatch was not present at compilation time)");
+}
+
void
Launcher_cpp::getJobResults(int job_id, std::string directory)
{
return 0;
}
-long
+long
Launcher_cpp::createJobWithFile( const std::string xmlExecuteFile, std::string clusterName)
{
throw LauncherException("Method Launcher_cpp::createJobWithFile is not available "
return 0;
}
+JobParameters_cpp
+Launcher_cpp::getJobParameters(int job_id)
+{
+ throw LauncherException("Method Launcher_cpp::getJobParamaters is not available "
+ "(libBatch was not present at compilation time)");
+}
+
#endif
-ParserLauncherType
+ParserLauncherType
Launcher_cpp::ParseXmlFile(std::string xmlExecuteFile)
{
ParserLauncherType job_params;
return _launcher_job_map;
}
-void
-Launcher_cpp::createBatchManagerForJob(Launcher::Job * job)
+#ifdef WITH_LIBBATCH
+Batch::BatchManager*
+Launcher_cpp::getBatchManager(Launcher::Job * job)
{
+ if(!_ResManager)
+ throw LauncherException("Resource manager is not set.");
+
+ Batch::BatchManager* result = nullptr;
int job_id = job->getNumber();
// Select a resource for the job
}
// Step 2: We can now add a Factory if the resource is correctly define
-#ifdef WITH_LIBBATCH
std::map<int, Batch::BatchManager *>::const_iterator it = _batchmap.find(job_id);
if(it == _batchmap.end())
{
// Warning cannot write on one line like this, because map object is constructed before
// the method is called...
//_batchmap[job_id] = FactoryBatchManager(resource_definition);
- Batch::BatchManager * batch_client = FactoryBatchManager(resource_definition);
- _batchmap[job_id] = batch_client;
+ result = FactoryBatchManager(resource_definition);
+ _batchmap[job_id] = result;
}
catch(const LauncherException &ex)
{
throw LauncherException(ex.message);
}
}
-#endif
+ else
+ result = it->second;
+ return result;
}
+#endif
-void
+void
Launcher_cpp::addJobDirectlyToMap(Launcher::Job * new_job)
{
- WriteLock mutex(_lock);
// Step 0: Calculated job_id
- int job_id = _job_cpt;
+ new_job->setNumber(_job_cpt);
_job_cpt++;
- new_job->setNumber(job_id);
+#ifdef WITH_LIBBATCH
// Step 1: check if resource is already in the map
- createBatchManagerForJob(new_job);
+ Batch::BatchManager * bm = getBatchManager(new_job);
// Step 2: add the job to the batch manager
-#ifdef WITH_LIBBATCH
try
{
- Batch::JobId batch_manager_job_id = _batchmap[job_id]->addJob(*(new_job->getBatchJob()),
- new_job->getReference());
+ Batch::JobId batch_manager_job_id = bm->addJob(*(new_job->getBatchJob()),
+ new_job->getReference());
new_job->setBatchManagerJobId(batch_manager_job_id);
}
catch(const Batch::GenericException &ex)
else
{
LAUNCHER_INFOS("A job as already the same id: " << new_job->getNumber());
- delete new_job;
throw LauncherException("A job as already the same id - job is not created !");
}
LAUNCHER_MESSAGE("New job added");
list<Launcher::Job *>::const_iterator it_job;
for (it_job = jobs_list.begin(); it_job != jobs_list.end(); it_job++)
{
- Launcher::Job * new_job = *it_job;
+ Launcher::Job* new_job(*it_job);
int jobId = -1;
try
{
jobId = addJob(new_job);
if(jobId >= 0)
new_jobs_id_list.push_back(jobId);
- else
- delete new_job;
}
catch(const LauncherException &ex)
{
LAUNCHER_INFOS("Cannot load the job. Exception: " << ex.msg.c_str());
- delete new_job;
}
}
// Create a sorted list from the internal job map
list<const Launcher::Job *> jobs_list;
{
- ReadLock mutex(_lock);
for (int i=0; i<_job_cpt; i++)
{
map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(i);
}
Launcher::Job * job = it_job->second;
return job;
-}
\ No newline at end of file
+}