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
23 // SALOME LifeCycleCORBA : implementation of containers and engines life cycle both in Python and C++
24 // File : SALOME_LifeCycleCORBA.cxx
25 // Author : Paul RASCLE, EDF
39 #include "Basics_Utils.hxx"
40 #include "utilities.h"
42 #include <ServiceUnreachable.hxx>
44 #include "SALOME_LifeCycleCORBA.hxx"
45 #include "SALOME_ResourcesManager.hxx"
46 #include "SALOMESDS_DataServerManager.hxx"
48 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
49 #include CORBA_CLIENT_HEADER(SALOME_Session)
50 #include CORBA_CLIENT_HEADER(DSC_Engines)
51 #include CORBA_CLIENT_HEADER(SALOME_Registry)
52 #include CORBA_CLIENT_HEADER(SALOMEDS)
53 #include CORBA_CLIENT_HEADER(SALOME_SDS)
54 #include CORBA_CLIENT_HEADER(Logger)
55 #include CORBA_CLIENT_HEADER(SALOME_Launcher)
57 #include "SALOME_ResourcesManager.hxx"
58 #include "SALOME_ContainerManager.hxx"
59 #include "SALOME_Component_i.hxx"
60 #include "SALOME_NamingService.hxx"
61 #include "SALOME_FileTransferCORBA.hxx"
63 IncompatibleComponent::IncompatibleComponent( void ):
64 SALOME_Exception( "IncompatibleComponent" )
68 IncompatibleComponent::IncompatibleComponent(const IncompatibleComponent &ex):
69 SALOME_Exception( ex )
73 /*! \class SALOME_LifeCycleCORBA
74 \brief A class to manage life cycle of SALOME components.
78 //=============================================================================
82 //=============================================================================
84 SALOME_LifeCycleCORBA::SALOME_LifeCycleCORBA(SALOME_NamingService *ns)
86 // be sure to have an instance of traceCollector, when used via SWIG
89 char *xargv = (char*)"";
91 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
92 // LocalTraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
96 _NS = new SALOME_NamingService(orb);
101 _NS->Change_Directory("/"); // mpv 250105: current directory may be not root
102 // (in SALOMEDS for an example)
103 // not enough: set a current directory in naming service is not thread safe
104 // if naming service instance is shared among several threads...
105 // ==> allways use absolute path and dot rely on current directory!
107 CORBA::Object_var obj =
108 _NS->Resolve(SALOME_ContainerManager::_ContainerManagerNameInNS);
109 if (CORBA::is_nil(obj))
110 throw SALOME_Exception("Error: Cannot resolve ContainerManager in Naming Service");
111 _ContManager=Engines::ContainerManager::_narrow(obj);
113 obj = _NS->Resolve(SALOME_ResourcesManager::_ResourcesManagerNameInNS);
114 if (CORBA::is_nil(obj))
115 throw SALOME_Exception("Error: Cannot resolve ResourceManager in Naming Service");
116 _ResManager=Engines::ResourcesManager::_narrow(obj);
119 //=============================================================================
123 //=============================================================================
125 SALOME_LifeCycleCORBA::~SALOME_LifeCycleCORBA()
127 if(_NSnew)delete _NSnew;
130 //=============================================================================
131 /*! \brief Find an already existing and registered component instance.
133 * \param params container parameters like type or name...
134 * \param componentName the name of component class
135 * \param studyId default = 0 : multistudy instance
136 * \return a CORBA reference of the component instance, or _nil if not found
138 //=============================================================================
139 Engines::EngineComponent_ptr
140 SALOME_LifeCycleCORBA::FindComponent(const Engines::ContainerParameters& params,
141 const char *componentName,
144 if (! isKnownComponentClass(componentName))
145 return Engines::EngineComponent::_nil();
147 Engines::ContainerParameters new_params(params);
148 new_params.resource_params.componentList.length(1);
149 new_params.resource_params.componentList[0] = componentName;
150 new_params.resource_params.can_run_containers = true;
151 Engines::ResourceList_var listOfResources;
154 listOfResources = _ResManager->GetFittingResources(new_params.resource_params);
156 catch( const SALOME::SALOME_Exception& ex )
158 return Engines::EngineComponent::_nil();
161 Engines::EngineComponent_var compo = _FindComponent(new_params,
166 return compo._retn();
169 //=============================================================================
170 /*! \brief Load a component instance on a container defined by its parameters
172 * \param params container parameters like type or name...
173 * \param componentName the name of component class
174 * \param studyId default = 0 : multistudy instance
175 * \return a CORBA reference of the component instance, or _nil if problem
177 //=============================================================================
179 Engines::EngineComponent_ptr
180 SALOME_LifeCycleCORBA::LoadComponent(const Engines::ContainerParameters& params,
181 const char *componentName,
184 // --- Check if Component Name is known in ModuleCatalog
186 if (! isKnownComponentClass(componentName))
187 return Engines::EngineComponent::_nil();
189 Engines::ContainerParameters new_params(params);
190 new_params.resource_params.componentList.length(1);
191 new_params.resource_params.componentList[0] = componentName;
192 new_params.resource_params.can_run_containers = true;
194 Engines::ResourceList_var listOfResources;
197 listOfResources = _ResManager->GetFittingResources(new_params.resource_params);
199 catch( const SALOME::SALOME_Exception& ex )
201 return Engines::EngineComponent::_nil();
203 new_params.resource_params.resList = listOfResources;
205 Engines::EngineComponent_var compo = _LoadComponent(new_params,
209 return compo._retn();
212 //=============================================================================
213 /*! \brief Find an already existing and registered component instance or load a new
214 * component instance on a container defined by its parameters.
216 * \param params container parameters like type or name...
217 * \param componentName the name of component class
218 * \param studyId default = 0 : multistudy instance
219 * \return a CORBA reference of the component instance, or _nil if problem
221 //=============================================================================
223 Engines::EngineComponent_ptr
224 SALOME_LifeCycleCORBA::
225 FindOrLoad_Component(const Engines::ContainerParameters& params,
226 const char *componentName,
229 // --- Check if Component Name is known in ModuleCatalog
231 if (! isKnownComponentClass(componentName))
232 return Engines::EngineComponent::_nil();
234 Engines::ContainerParameters new_params(params);
235 new_params.resource_params.componentList.length(1);
236 new_params.resource_params.componentList[0] = componentName;
237 new_params.resource_params.can_run_containers = true;
239 Engines::ResourceList_var listOfResources;
242 listOfResources = _ResManager->GetFittingResources(new_params.resource_params);
244 catch( const SALOME::SALOME_Exception& ex )
246 return Engines::EngineComponent::_nil();
249 Engines::EngineComponent_var compo = _FindComponent(new_params,
254 if(CORBA::is_nil(compo))
256 new_params.resource_params.resList = listOfResources;
257 compo = _LoadComponent(new_params,
262 return compo._retn();
265 //=============================================================================
266 /*! \brief Find an already existing and registered component instance or load a new
267 * component instance on a container defined by name
269 * \param containerName the name of container, under one of the forms
270 * - 1 aContainer (local container)
271 * - 2 machine/aContainer (container on hostname = machine)
272 * \param componentName the name of component class
273 * \return a CORBA reference of the component instance, or _nil if problem
275 //=============================================================================
277 Engines::EngineComponent_ptr
278 SALOME_LifeCycleCORBA::FindOrLoad_Component(const char *containerName,
279 const char *componentName)
281 MESSAGE("SALOME_LifeCycleCORBA::FindOrLoad_Component INTERACTIF " << containerName << " " << componentName ) ;
283 // --- Check if Component Name is known in ModuleCatalog
284 if (! isKnownComponentClass(componentName))
285 return Engines::EngineComponent::_nil();
287 // --- Check if containerName contains machine name (if yes: rg>0)
288 char *stContainer=strdup(containerName);
289 std::string st2Container(stContainer);
290 int rg=st2Container.find("/");
292 Engines::ContainerParameters params;
296 // containerName doesn't contain "/" => Local container
297 params.container_name = CORBA::string_dup(stContainer);
301 stContainer[rg]='\0';
302 params.container_name = CORBA::string_dup(stContainer+rg+1);
303 params.resource_params.hostname = CORBA::string_dup(stContainer);
305 params.isMPI = false;
306 SCRUTE(params.container_name);
308 return FindOrLoad_Component(params, componentName);
311 //=============================================================================
312 /*! \brief Check if the component class is known in module catalog
314 * \param componentName the name of component class
315 * \return true if found, false otherwise
317 //=============================================================================
319 bool SALOME_LifeCycleCORBA::isKnownComponentClass(const char *componentName)
323 CORBA::Object_var obj = _NS->Resolve("/Kernel/ModulCatalog");
324 SALOME_ModuleCatalog::ModuleCatalog_var Catalog =
325 SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
326 ASSERT(! CORBA::is_nil(Catalog));
327 SALOME_ModuleCatalog::Acomponent_var compoInfo =
328 Catalog->GetComponent(componentName);
329 if (CORBA::is_nil (compoInfo))
331 MESSAGE("Catalog Error: Component not found in the catalog " << componentName);
336 catch (ServiceUnreachable&)
338 INFOS("Caught exception: Naming Service Unreachable");
342 INFOS("Caught unknown exception.");
347 //=============================================================================
348 /*! \brief Initialisation of a given Engines::ResourceParameters with default values.
350 //=============================================================================
353 SALOME_LifeCycleCORBA::preSet(Engines::ResourceParameters& params)
356 params.hostname = "";
360 params.cpu_clock = 0;
362 params.nb_proc_per_node = 0;
364 params.can_launch_batch_jobs = false;
365 params.can_run_containers = false;
368 //=============================================================================
369 /*! \brief Initialisation of a given Engines::ContainerParameters with default values.
371 //=============================================================================
373 void SALOME_LifeCycleCORBA::preSet( Engines::ContainerParameters& params)
375 params.container_name = "";
377 params.workingdir = "";
379 params.isMPI = false;
380 params.parallelLib = "";
381 SALOME_LifeCycleCORBA::preSet(params.resource_params);
384 //=============================================================================
386 * \return a number of processors not 0, only for MPI containers
388 //=============================================================================
390 int SALOME_LifeCycleCORBA::NbProc(const Engines::ContainerParameters& params)
394 else if( params.nb_proc <= 0 )
397 return params.nb_proc;
400 //=============================================================================
401 /*! \brief Get the container manager
403 * \return the container Manager
405 //=============================================================================
407 Engines::ContainerManager_ptr SALOME_LifeCycleCORBA::getContainerManager()
409 Engines::ContainerManager_var contManager =
410 Engines::ContainerManager::_duplicate(_ContManager);
411 return contManager._retn();
414 //=============================================================================
415 /*! \brief Get the resources manager
417 * \return the container Manager
419 //=============================================================================
421 Engines::ResourcesManager_ptr SALOME_LifeCycleCORBA::getResourcesManager()
423 Engines::ResourcesManager_var resManager =
424 Engines::ResourcesManager::_duplicate(_ResManager);
425 return resManager._retn();
428 //=============================================================================
429 /*! \brief shutdown all the SALOME servers except SALOME_Session_Server and omniNames
431 //=============================================================================
433 void SALOME_LifeCycleCORBA::shutdownServers()
435 // get each Container from NamingService => shutdown it
436 // (the order is inverse to the order of servers initialization)
438 SALOME::Session_var session = SALOME::Session::_nil();
440 CORBA::Object_var objS = _NS->Resolve("/Kernel/Session");
441 if (!CORBA::is_nil(objS))
443 session = SALOME::Session::_narrow(objS);
444 if (!CORBA::is_nil(session))
446 pid = session->getPID();
451 std::string hostname = Kernel_Utils::GetHostname();
453 // 1) ConnectionManager
456 CORBA::Object_var objCnM=_NS->Resolve("/ConnectionManager");
457 Engines::ConnectionManager_var connMan=Engines::ConnectionManager::_narrow(objCnM);
458 if ( !CORBA::is_nil(connMan) && ( pid != connMan->getPID() ) )
459 connMan->ShutdownWithExit();
461 catch(const CORBA::Exception& e)
463 // ignore and continue
467 ts_req.tv_nsec=100000000;
470 //Wait some time so that ConnectionManager be completely shutdown
472 nanosleep(&ts_req,0);
478 CORBA::Object_var objSDS = _NS->Resolve("/myStudyManager");
479 SALOMEDS::StudyManager_var studyManager = SALOMEDS::StudyManager::_narrow(objSDS) ;
480 if ( !CORBA::is_nil(studyManager) && ( pid != studyManager->getPID() ) )
481 studyManager->Shutdown();
483 catch(const CORBA::Exception& e)
485 // ignore and continue
488 //Wait some time so that study be completely shutdown
490 nanosleep(&ts_req,0);
496 CORBA::Object_var objMC=_NS->Resolve("/Kernel/ModulCatalog");
497 SALOME_ModuleCatalog::ModuleCatalog_var catalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(objMC);
498 if ( !CORBA::is_nil(catalog) && ( pid != catalog->getPID() ) )
501 catch(const CORBA::Exception& e)
503 // ignore and continue
506 //Wait some time so that ModulCatalog be completely shutdown
508 nanosleep(&ts_req,0);
510 // 4 ) Remote ScopeServer (the DataServer is hosted by SalomeLauncher shutdown right after)
513 CORBA::Object_var objDSM(_NS->Resolve(SALOMESDS::DataServerManager::NAME_IN_NS));
514 SALOME::DataServerManager_var dsm(SALOME::DataServerManager::_narrow(objDSM));
515 if ( !CORBA::is_nil(dsm) )
516 dsm->shutdownScopes();
518 catch(const CORBA::Exception& e)
520 // ignore and continue
526 CORBA::Object_var objSL = _NS->Resolve("/SalomeLauncher");
527 Engines::SalomeLauncher_var launcher = Engines::SalomeLauncher::_narrow(objSL);
528 if (!CORBA::is_nil(launcher) && (pid != launcher->getPID()))
529 launcher->Shutdown();
531 catch(const CORBA::Exception& e)
533 // ignore and continue
536 //Wait some time so that launcher be completely shutdown
538 nanosleep(&ts_req,0);
544 CORBA::Object_var objR = _NS->Resolve("/Registry");
545 Registry::Components_var registry = Registry::Components::_narrow(objR);
546 if ( !CORBA::is_nil(registry) && ( pid != registry->getPID() ) )
547 registry->Shutdown();
549 catch(const CORBA::Exception& e)
551 // ignore and continue
556 char *xargv = (char*)"";
557 char **argv = &xargv;
558 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
560 CORBA::Object_var objLog = CORBA::Object::_nil();
561 CosNaming::NamingContext_var inc;
562 CORBA::Object_var theObj = CORBA::Object::_nil();
563 std::string stdname = "Logger";
564 CosNaming::Name name;
566 name[0].id = CORBA::string_dup(stdname.c_str());
569 if(!CORBA::is_nil(orb))
570 theObj = orb->resolve_initial_references("NameService");
571 if (!CORBA::is_nil(theObj))
572 inc = CosNaming::NamingContext::_narrow(theObj);
577 if(!CORBA::is_nil(inc))
581 objLog = inc->resolve(name);
582 SALOME_Logger::Logger_var logger = SALOME_Logger::Logger::_narrow(objLog);
583 if ( !CORBA::is_nil(logger) )
592 //=============================================================================
593 /*! \brief shutdown omniNames
595 //=============================================================================
597 void SALOME_LifeCycleCORBA::killOmniNames()
599 std::string portNumber (::getenv ("NSPORT") );
600 std::string python_exe;
602 python_exe = std::string("python");
604 if ( !portNumber.empty() )
608 cmd = std::string("from salome_utils import killOmniNames; ");
609 cmd += std::string("killOmniNames(") + portNumber + "); ";
610 cmd = python_exe + std::string(" -c \"") + cmd +"\"";
612 system( cmd.c_str() );
614 cmd = std::string("from killSalomeWithPort import cleanApplication; ");
615 cmd += std::string("cleanApplication(") + portNumber + "); ";
616 cmd = python_exe + std::string(" -c \"") + cmd +"\"";
618 system( cmd.c_str() );
621 // shutdown portmanager
622 if ( !portNumber.empty() )
626 cmd = std::string("from PortManager import releasePort; ");
627 cmd += std::string("releasePort(") + portNumber + "); ";
628 cmd = python_exe + std::string(" -c \"") + cmd +"\"";
630 system( cmd.c_str() );
634 //=============================================================================
635 /*! \brief Find an already existing and registered component instance.
637 * - build a list of machines on which an instance of the component is running,
638 * - find the best machine among the list
640 * \param params machine parameters like type or name...
641 * \param componentName the name of component class
642 * \param studyId default = 0 : multistudy instance
643 * \param listOfMachines list of machine address
644 * \return a CORBA reference of the component instance, or _nil if not found
646 //=============================================================================
648 Engines::EngineComponent_ptr
649 SALOME_LifeCycleCORBA::
650 _FindComponent(const Engines::ContainerParameters& params,
651 const char *componentName,
653 const Engines::ResourceList& listOfResources)
655 // --- build the list of machines on which the component is already running
656 const char *containerName = params.container_name;
657 int nbproc = NbProc(params);
659 Engines::ResourceList_var resourcesOK = new Engines::ResourceList;
661 unsigned int lghtOfresourcesOK = 0;
662 resourcesOK->length(listOfResources.length());
664 for(unsigned int i=0; i < listOfResources.length(); i++)
666 const char * currentResource = listOfResources[i];
667 Engines::ResourceDefinition_var resource_definition =
668 _ResManager->GetResourceDefinition(currentResource);
669 CORBA::Object_var obj = _NS->ResolveComponent(resource_definition->hostname.in(),
673 if (!CORBA::is_nil(obj))
674 resourcesOK[lghtOfresourcesOK++] = CORBA::string_dup(currentResource);
677 // --- find the best machine among the list
678 if(lghtOfresourcesOK != 0)
680 resourcesOK->length(lghtOfresourcesOK);
681 CORBA::String_var bestResource = _ResManager->FindFirst(resourcesOK);
682 Engines::ResourceDefinition_var resource_definition =
683 _ResManager->GetResourceDefinition(bestResource);
684 CORBA::Object_var obj = _NS->ResolveComponent(resource_definition->hostname.in(),
688 return Engines::EngineComponent::_narrow(obj);
691 return Engines::EngineComponent::_nil();
694 //=============================================================================
695 /*! \brief Load a component instance.
697 * - Finds a container in the list of machine or start one.
698 * - Try to load the component library in the container,
699 * - then create an instance of the component.
701 * \param params machine parameters like type or name...
702 * \param componentName the name of component class
703 * \param studyId default = 0 : multistudy instance
704 * \return a CORBA reference of the component instance, or _nil if problem
706 //=============================================================================
708 Engines::EngineComponent_ptr
709 SALOME_LifeCycleCORBA::
710 _LoadComponent(const Engines::ContainerParameters& params,
711 const char *componentName,
714 MESSAGE("_LoadComponent, required " << params.container_name <<
715 " " << componentName << " " << NbProc(params));
717 Engines::ContainerParameters local_params(params);
718 local_params.mode = CORBA::string_dup("findorstart");
719 Engines::Container_var cont = _ContManager->GiveContainer(local_params);
720 if (CORBA::is_nil(cont)) return Engines::EngineComponent::_nil();
723 bool isLoadable = cont->load_component_Library(componentName,reason);
726 //std::cerr << reason << std::endl;
727 CORBA::string_free(reason);
728 return Engines::EngineComponent::_nil();
730 CORBA::string_free(reason);
732 Engines::EngineComponent_var myInstance =
733 cont->create_component_instance(componentName, studyId);
734 return myInstance._retn();
737 //=============================================================================
738 /*! \brief Load a parallel component instance.
740 * \param params machine parameters like type or name...
741 * \param componentName the name of component class
742 * \param studyId default = 0 : multistudy instance
743 * \return a CORBA reference of the parallel component instance, or _nil if problem
745 //=============================================================================
746 Engines::EngineComponent_ptr
747 SALOME_LifeCycleCORBA::Load_ParallelComponent(const Engines::ContainerParameters& params,
748 const char *componentName,
751 MESSAGE("Entering LoadParallelComponent");
753 /*MESSAGE("Parameters : ");
754 MESSAGE("Container name : " << params.container_name);
755 MESSAGE("Number of component nodes : " << params.nb_component_nodes);
756 MESSAGE("Component Name : " << componentName);*/
758 Engines::ContainerParameters parms(params);
759 parms.resource_params.componentList.length(1);
760 parms.resource_params.componentList[0] = componentName;
761 parms.mode = CORBA::string_dup("findorstart");
763 MESSAGE("Starting Parallel Container");
764 Engines::Container_var cont = _ContManager->GiveContainer(parms);
765 if (CORBA::is_nil(cont)) {
766 INFOS("FindOrStartParallelContainer() returns a NULL container !");
767 return Engines::EngineComponent::_nil();
770 MESSAGE("Loading component library");
772 bool isLoadable = cont->load_component_Library(componentName,reason);
774 INFOS(componentName <<" library is not loadable !");
775 //std::cerr << reason << std::endl;
776 CORBA::string_free(reason);
777 return Engines::EngineComponent::_nil();
779 CORBA::string_free(reason);
781 MESSAGE("Creating component instance");
782 // @PARALLEL@ permits to identify that the component requested
783 // is a parallel component.
784 std::string name = std::string(componentName);
785 Engines::EngineComponent_var myInstance = cont->create_component_instance(name.c_str(), studyId);
786 if (CORBA::is_nil(myInstance))
787 INFOS("create_component_instance returns a NULL component !");
788 return myInstance._retn();
791 /*! \brief copy a file from a source host to a destination host
792 * \param hostSrc the source host
793 * \param fileSrc the file to copy from the source host to the destination host
794 * \param hostDest the destination host
795 * \param fileDest the destination file
797 void SALOME_LifeCycleCORBA::copyFile(const char* hostSrc, const char* fileSrc, const char* hostDest, const char* fileDest)
799 if(strcmp(hostDest,"localhost") == 0)
801 //if localhost use a shortcut
802 SALOME_FileTransferCORBA transfer(hostSrc,fileSrc);
803 transfer.getLocalFile(fileDest);
807 Engines::ContainerManager_var contManager = getContainerManager();
809 Engines::ContainerParameters params;
812 params.resource_params.hostname = hostDest;
813 params.mode = CORBA::string_dup("findorstart");
814 Engines::Container_var containerDest = contManager->GiveContainer(params);
816 params.resource_params.hostname = hostSrc;
817 Engines::Container_var containerSrc = contManager->GiveContainer(params);
819 containerDest->copyFile(containerSrc,fileSrc,fileDest);
822 /*! \brief get the naming service used by the life cycle
824 * \return the naming service
826 SALOME_NamingService * SALOME_LifeCycleCORBA::namingService()
831 /*! \brief get the orb used by the life cycle
835 CORBA::ORB_ptr SALOME_LifeCycleCORBA::orb()