1 // Copyright (C) 2007-2012 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
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 CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
46 #include CORBA_CLIENT_HEADER(SALOME_Session)
47 #include CORBA_CLIENT_HEADER(DSC_Engines)
48 #include CORBA_CLIENT_HEADER(SALOME_Registry)
49 #include CORBA_CLIENT_HEADER(SALOMEDS)
50 #include CORBA_CLIENT_HEADER(Logger)
51 #include CORBA_CLIENT_HEADER(SALOME_Launcher)
53 #include "SALOME_ContainerManager.hxx"
54 #include "SALOME_Component_i.hxx"
55 #include "SALOME_NamingService.hxx"
56 #include "SALOME_FileTransferCORBA.hxx"
58 IncompatibleComponent::IncompatibleComponent( void ):
59 SALOME_Exception( "IncompatibleComponent" )
63 IncompatibleComponent::IncompatibleComponent(const IncompatibleComponent &ex):
64 SALOME_Exception( ex )
68 /*! \class SALOME_LifeCycleCORBA
69 \brief A class to manage life cycle of SALOME components.
73 //=============================================================================
77 //=============================================================================
79 SALOME_LifeCycleCORBA::SALOME_LifeCycleCORBA(SALOME_NamingService *ns)
81 // be sure to have an instance of traceCollector, when used via SWIG
84 char *xargv = (char*)"";
86 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
87 // LocalTraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
91 _NS = new SALOME_NamingService(orb);
96 _NS->Change_Directory("/"); // mpv 250105: current directory may be not root
97 // (in SALOMEDS for an example)
98 // not enough: set a current directory in naming service is not thread safe
99 // if naming service instance is shared among several threads...
100 // ==> allways use absolute path and dot rely on current directory!
102 CORBA::Object_var obj =
103 _NS->Resolve(SALOME_ContainerManager::_ContainerManagerNameInNS);
104 if (CORBA::is_nil(obj))
105 throw SALOME_Exception("Error: Cannot resolve ContainerManager in Naming Service");
106 _ContManager=Engines::ContainerManager::_narrow(obj);
108 obj = _NS->Resolve(SALOME_ResourcesManager::_ResourcesManagerNameInNS);
109 if (CORBA::is_nil(obj))
110 throw SALOME_Exception("Error: Cannot resolve ResourceManager in Naming Service");
111 _ResManager=Engines::ResourcesManager::_narrow(obj);
114 //=============================================================================
118 //=============================================================================
120 SALOME_LifeCycleCORBA::~SALOME_LifeCycleCORBA()
122 if(_NSnew)delete _NSnew;
125 //=============================================================================
126 /*! \brief Find an already existing and registered component instance.
128 * \param params container parameters like type or name...
129 * \param componentName the name of component class
130 * \param studyId default = 0 : multistudy instance
131 * \return a CORBA reference of the component instance, or _nil if not found
133 //=============================================================================
134 Engines::EngineComponent_ptr
135 SALOME_LifeCycleCORBA::FindComponent(const Engines::ContainerParameters& params,
136 const char *componentName,
139 if (! isKnownComponentClass(componentName))
140 return Engines::EngineComponent::_nil();
142 Engines::ContainerParameters new_params(params);
143 new_params.resource_params.componentList.length(1);
144 new_params.resource_params.componentList[0] = componentName;
145 new_params.resource_params.can_run_containers = true;
146 Engines::ResourceList_var listOfResources;
149 listOfResources = _ResManager->GetFittingResources(new_params.resource_params);
151 catch( const SALOME::SALOME_Exception& ex )
153 return Engines::EngineComponent::_nil();
156 Engines::EngineComponent_var compo = _FindComponent(new_params,
161 return compo._retn();
164 //=============================================================================
165 /*! \brief Load a component instance on a container defined by its parameters
167 * \param params container parameters like type or name...
168 * \param componentName the name of component class
169 * \param studyId default = 0 : multistudy instance
170 * \return a CORBA reference of the component instance, or _nil if problem
172 //=============================================================================
174 Engines::EngineComponent_ptr
175 SALOME_LifeCycleCORBA::LoadComponent(const Engines::ContainerParameters& params,
176 const char *componentName,
179 // --- Check if Component Name is known in ModuleCatalog
181 if (! isKnownComponentClass(componentName))
182 return Engines::EngineComponent::_nil();
184 Engines::ContainerParameters new_params(params);
185 new_params.resource_params.componentList.length(1);
186 new_params.resource_params.componentList[0] = componentName;
187 new_params.resource_params.can_run_containers = true;
189 Engines::ResourceList_var listOfResources;
192 listOfResources = _ResManager->GetFittingResources(new_params.resource_params);
194 catch( const SALOME::SALOME_Exception& ex )
196 return Engines::EngineComponent::_nil();
198 new_params.resource_params.resList = listOfResources;
200 Engines::EngineComponent_var compo = _LoadComponent(new_params,
204 return compo._retn();
207 //=============================================================================
208 /*! \brief Find an already existing and registered component instance or load a new
209 * component instance on a container defined by its parameters.
211 * \param params container parameters like type or name...
212 * \param componentName the name of component class
213 * \param studyId default = 0 : multistudy instance
214 * \return a CORBA reference of the component instance, or _nil if problem
216 //=============================================================================
218 Engines::EngineComponent_ptr
219 SALOME_LifeCycleCORBA::
220 FindOrLoad_Component(const Engines::ContainerParameters& params,
221 const char *componentName,
224 // --- Check if Component Name is known in ModuleCatalog
226 if (! isKnownComponentClass(componentName))
227 return Engines::EngineComponent::_nil();
229 Engines::ContainerParameters new_params(params);
230 new_params.resource_params.componentList.length(1);
231 new_params.resource_params.componentList[0] = componentName;
232 new_params.resource_params.can_run_containers = true;
234 Engines::ResourceList_var listOfResources;
237 listOfResources = _ResManager->GetFittingResources(new_params.resource_params);
239 catch( const SALOME::SALOME_Exception& ex )
241 return Engines::EngineComponent::_nil();
244 Engines::EngineComponent_var compo = _FindComponent(new_params,
249 if(CORBA::is_nil(compo))
251 new_params.resource_params.resList = listOfResources;
252 compo = _LoadComponent(new_params,
257 return compo._retn();
260 //=============================================================================
261 /*! \brief Find an already existing and registered component instance or load a new
262 * component instance on a container defined by name
264 * \param containerName the name of container, under one of the forms
265 * - 1 aContainer (local container)
266 * - 2 machine/aContainer (container on hostname = machine)
267 * \param componentName the name of component class
268 * \return a CORBA reference of the component instance, or _nil if problem
270 //=============================================================================
272 Engines::EngineComponent_ptr
273 SALOME_LifeCycleCORBA::FindOrLoad_Component(const char *containerName,
274 const char *componentName)
276 MESSAGE("SALOME_LifeCycleCORBA::FindOrLoad_Component INTERACTIF " << containerName << " " << componentName ) ;
278 // --- Check if Component Name is known in ModuleCatalog
279 if (! isKnownComponentClass(componentName))
280 return Engines::EngineComponent::_nil();
282 // --- Check if containerName contains machine name (if yes: rg>0)
283 char *stContainer=strdup(containerName);
284 std::string st2Container(stContainer);
285 int rg=st2Container.find("/");
287 Engines::ContainerParameters params;
291 // containerName doesn't contain "/" => Local container
292 params.container_name = CORBA::string_dup(stContainer);
296 stContainer[rg]='\0';
297 params.container_name = CORBA::string_dup(stContainer+rg+1);
298 params.resource_params.hostname = CORBA::string_dup(stContainer);
300 params.isMPI = false;
301 SCRUTE(params.container_name);
303 return FindOrLoad_Component(params, componentName);
306 //=============================================================================
307 /*! \brief Check if the component class is known in module catalog
309 * \param componentName the name of component class
310 * \return true if found, false otherwise
312 //=============================================================================
314 bool SALOME_LifeCycleCORBA::isKnownComponentClass(const char *componentName)
318 CORBA::Object_var obj = _NS->Resolve("/Kernel/ModulCatalog");
319 SALOME_ModuleCatalog::ModuleCatalog_var Catalog =
320 SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
321 ASSERT(! CORBA::is_nil(Catalog));
322 SALOME_ModuleCatalog::Acomponent_var compoInfo =
323 Catalog->GetComponent(componentName);
324 if (CORBA::is_nil (compoInfo))
326 MESSAGE("Catalog Error: Component not found in the catalog " << componentName);
331 catch (ServiceUnreachable&)
333 INFOS("Caught exception: Naming Service Unreachable");
337 INFOS("Caught unknown exception.");
342 //=============================================================================
343 /*! \brief Initialisation of a given Engines::ResourceParameters with default values.
345 //=============================================================================
348 SALOME_LifeCycleCORBA::preSet(Engines::ResourceParameters& params)
351 params.hostname = "";
355 params.cpu_clock = 0;
357 params.nb_proc_per_node = 0;
359 params.can_launch_batch_jobs = false;
360 params.can_run_containers = false;
363 //=============================================================================
364 /*! \brief Initialisation of a given Engines::ContainerParameters with default values.
366 //=============================================================================
368 void SALOME_LifeCycleCORBA::preSet( Engines::ContainerParameters& params)
370 params.container_name = "";
372 params.workingdir = "";
374 params.isMPI = false;
375 params.parallelLib = "";
376 SALOME_LifeCycleCORBA::preSet(params.resource_params);
379 //=============================================================================
381 * \return a number of processors not 0, only for MPI containers
383 //=============================================================================
385 int SALOME_LifeCycleCORBA::NbProc(const Engines::ContainerParameters& params)
389 else if( params.nb_proc <= 0 )
392 return params.nb_proc;
395 //=============================================================================
396 /*! \brief Get the container manager
398 * \return the container Manager
400 //=============================================================================
402 Engines::ContainerManager_ptr SALOME_LifeCycleCORBA::getContainerManager()
404 Engines::ContainerManager_var contManager =
405 Engines::ContainerManager::_duplicate(_ContManager);
406 return contManager._retn();
409 //=============================================================================
410 /*! \brief Get the resources manager
412 * \return the container Manager
414 //=============================================================================
416 Engines::ResourcesManager_ptr SALOME_LifeCycleCORBA::getResourcesManager()
418 Engines::ResourcesManager_var resManager =
419 Engines::ResourcesManager::_duplicate(_ResManager);
420 return resManager._retn();
423 //=============================================================================
424 /*! \brief shutdown all the SALOME servers except SALOME_Session_Server, omniNames and notifd
426 //=============================================================================
428 void SALOME_LifeCycleCORBA::shutdownServers()
430 // get each Container from NamingService => shutdown it
431 // (the order is inverse to the order of servers initialization)
433 SALOME::Session_var session = SALOME::Session::_nil();
435 CORBA::Object_var objS = _NS->Resolve("/Kernel/Session");
436 if (!CORBA::is_nil(objS))
438 session = SALOME::Session::_narrow(objS);
439 if (!CORBA::is_nil(session))
441 pid = session->getPID();
446 std::string hostname = Kernel_Utils::GetHostname();
448 // 1) ConnectionManager
451 CORBA::Object_var objCnM=_NS->Resolve("/ConnectionManager");
452 Engines::ConnectionManager_var connMan=Engines::ConnectionManager::_narrow(objCnM);
453 if ( !CORBA::is_nil(connMan) && ( pid != connMan->getPID() ) )
454 connMan->ShutdownWithExit();
456 catch(const CORBA::Exception& e)
458 // ignore and continue
462 ts_req.tv_nsec=100000000;
465 //Wait some time so that ConnectionManager be completely shutdown
467 nanosleep(&ts_req,0);
473 CORBA::Object_var objSDS = _NS->Resolve("/myStudyManager");
474 SALOMEDS::StudyManager_var studyManager = SALOMEDS::StudyManager::_narrow(objSDS) ;
475 if ( !CORBA::is_nil(studyManager) && ( pid != studyManager->getPID() ) )
476 studyManager->Shutdown();
478 catch(const CORBA::Exception& e)
480 // ignore and continue
483 //Wait some time so that study be completely shutdown
485 nanosleep(&ts_req,0);
491 CORBA::Object_var objMC=_NS->Resolve("/Kernel/ModulCatalog");
492 SALOME_ModuleCatalog::ModuleCatalog_var catalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(objMC);
493 if ( !CORBA::is_nil(catalog) && ( pid != catalog->getPID() ) )
496 catch(const CORBA::Exception& e)
498 // ignore and continue
501 //Wait some time so that ModulCatalog be completely shutdown
503 nanosleep(&ts_req,0);
509 CORBA::Object_var objSL = _NS->Resolve("/SalomeLauncher");
510 Engines::SalomeLauncher_var launcher = Engines::SalomeLauncher::_narrow(objSL);
511 if (!CORBA::is_nil(launcher) && (pid != launcher->getPID()))
512 launcher->Shutdown();
514 catch(const CORBA::Exception& e)
516 // ignore and continue
519 //Wait some time so that launcher be completely shutdown
521 nanosleep(&ts_req,0);
527 CORBA::Object_var objR = _NS->Resolve("/Registry");
528 Registry::Components_var registry = Registry::Components::_narrow(objR);
529 if ( !CORBA::is_nil(registry) && ( pid != registry->getPID() ) )
530 registry->Shutdown();
532 catch(const CORBA::Exception& e)
534 // ignore and continue
539 if ( !CORBA::is_nil( session ) ) {
544 catch(const CORBA::Exception& e)
546 // ignore and continue
553 char *xargv = (char*)"";
554 char **argv = &xargv;
555 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
557 CORBA::Object_var objLog = CORBA::Object::_nil();
558 CosNaming::NamingContext_var inc;
559 CORBA::Object_var theObj = CORBA::Object::_nil();
560 std::string stdname = "Logger";
561 CosNaming::Name name;
563 name[0].id = CORBA::string_dup(stdname.c_str());
566 if(!CORBA::is_nil(orb))
567 theObj = orb->resolve_initial_references("NameService");
568 if (!CORBA::is_nil(theObj))
569 inc = CosNaming::NamingContext::_narrow(theObj);
574 if(!CORBA::is_nil(inc))
578 objLog = inc->resolve(name);
579 SALOME_Logger::Logger_var logger = SALOME_Logger::Logger::_narrow(objLog);
580 if ( !CORBA::is_nil(logger) )
589 //=============================================================================
590 /*! \brief shutdown omniNames and notifd
592 //=============================================================================
594 void SALOME_LifeCycleCORBA::killOmniNames()
596 std::string portNumber (::getenv ("NSPORT") );
597 if ( !portNumber.empty() )
602 cmd = std::string( "ps -eo pid,command | grep -v grep | grep -E \"omniNames.*")
604 + std::string("\" | awk '{cmd=sprintf(\"kill -9 %s\",$1); system(cmd)}'" );
607 system ( cmd.c_str() );
614 // NPAL 18309 (Kill Notifd)
615 if ( !portNumber.empty() )
617 std::string cmd = ("from killSalomeWithPort import killNotifdAndClean; ");
618 cmd += std::string("killNotifdAndClean(") + portNumber + "); ";
619 cmd = std::string("python -c \"") + cmd +"\" > /dev/null 2> /dev/null";
621 system( cmd.c_str() );
625 //=============================================================================
626 /*! \brief Find an already existing and registered component instance.
628 * - build a list of machines on which an instance of the component is running,
629 * - find the best machine among the list
631 * \param params machine parameters like type or name...
632 * \param componentName the name of component class
633 * \param studyId default = 0 : multistudy instance
634 * \param listOfMachines list of machine address
635 * \return a CORBA reference of the component instance, or _nil if not found
637 //=============================================================================
639 Engines::EngineComponent_ptr
640 SALOME_LifeCycleCORBA::
641 _FindComponent(const Engines::ContainerParameters& params,
642 const char *componentName,
644 const Engines::ResourceList& listOfResources)
646 // --- build the list of machines on which the component is already running
647 const char *containerName = params.container_name;
648 int nbproc = NbProc(params);
650 Engines::ResourceList_var resourcesOK = new Engines::ResourceList;
652 unsigned int lghtOfresourcesOK = 0;
653 resourcesOK->length(listOfResources.length());
655 for(unsigned int i=0; i < listOfResources.length(); i++)
657 const char * currentResource = listOfResources[i];
658 Engines::ResourceDefinition_var resource_definition =
659 _ResManager->GetResourceDefinition(currentResource);
660 CORBA::Object_var obj = _NS->ResolveComponent(resource_definition->hostname.in(),
664 if (!CORBA::is_nil(obj))
665 resourcesOK[lghtOfresourcesOK++] = CORBA::string_dup(currentResource);
668 // --- find the best machine among the list
669 if(lghtOfresourcesOK != 0)
671 resourcesOK->length(lghtOfresourcesOK);
672 CORBA::String_var bestResource = _ResManager->FindFirst(resourcesOK);
673 Engines::ResourceDefinition_var resource_definition =
674 _ResManager->GetResourceDefinition(bestResource);
675 CORBA::Object_var obj = _NS->ResolveComponent(resource_definition->hostname.in(),
679 return Engines::EngineComponent::_narrow(obj);
682 return Engines::EngineComponent::_nil();
685 //=============================================================================
686 /*! \brief Load a component instance.
688 * - Finds a container in the list of machine or start one.
689 * - Try to load the component library in the container,
690 * - then create an instance of the component.
692 * \param params machine parameters like type or name...
693 * \param componentName the name of component class
694 * \param studyId default = 0 : multistudy instance
695 * \return a CORBA reference of the component instance, or _nil if problem
697 //=============================================================================
699 Engines::EngineComponent_ptr
700 SALOME_LifeCycleCORBA::
701 _LoadComponent(const Engines::ContainerParameters& params,
702 const char *componentName,
705 MESSAGE("_LoadComponent, required " << params.container_name <<
706 " " << componentName << " " << NbProc(params));
708 Engines::ContainerParameters local_params(params);
709 local_params.mode = CORBA::string_dup("findorstart");
710 Engines::Container_var cont = _ContManager->GiveContainer(local_params);
711 if (CORBA::is_nil(cont)) return Engines::EngineComponent::_nil();
714 bool isLoadable = cont->load_component_Library(componentName,reason);
717 //std::cerr << reason << std::endl;
718 CORBA::string_free(reason);
719 return Engines::EngineComponent::_nil();
721 CORBA::string_free(reason);
723 Engines::EngineComponent_var myInstance =
724 cont->create_component_instance(componentName, studyId);
725 return myInstance._retn();
728 //=============================================================================
729 /*! \brief Load a parallel component instance.
731 * \param params machine parameters like type or name...
732 * \param componentName the name of component class
733 * \param studyId default = 0 : multistudy instance
734 * \return a CORBA reference of the parallel component instance, or _nil if problem
736 //=============================================================================
737 Engines::EngineComponent_ptr
738 SALOME_LifeCycleCORBA::Load_ParallelComponent(const Engines::ContainerParameters& params,
739 const char *componentName,
742 MESSAGE("Entering LoadParallelComponent");
744 /*MESSAGE("Parameters : ");
745 MESSAGE("Container name : " << params.container_name);
746 MESSAGE("Number of component nodes : " << params.nb_component_nodes);
747 MESSAGE("Component Name : " << componentName);*/
749 Engines::ContainerParameters parms(params);
750 parms.resource_params.componentList.length(1);
751 parms.resource_params.componentList[0] = componentName;
752 parms.mode = CORBA::string_dup("findorstart");
754 MESSAGE("Starting Parallel Container");
755 Engines::Container_var cont = _ContManager->GiveContainer(parms);
756 if (CORBA::is_nil(cont)) {
757 INFOS("FindOrStartParallelContainer() returns a NULL container !");
758 return Engines::EngineComponent::_nil();
761 MESSAGE("Loading component library");
763 bool isLoadable = cont->load_component_Library(componentName,reason);
765 INFOS(componentName <<" library is not loadable !");
766 //std::cerr << reason << std::endl;
767 CORBA::string_free(reason);
768 return Engines::EngineComponent::_nil();
770 CORBA::string_free(reason);
772 MESSAGE("Creating component instance");
773 // @PARALLEL@ permits to identify that the component requested
774 // is a parallel component.
775 std::string name = std::string(componentName);
776 Engines::EngineComponent_var myInstance = cont->create_component_instance(name.c_str(), studyId);
777 if (CORBA::is_nil(myInstance))
778 INFOS("create_component_instance returns a NULL component !");
779 return myInstance._retn();
782 /*! \brief copy a file from a source host to a destination host
783 * \param hostSrc the source host
784 * \param fileSrc the file to copy from the source host to the destination host
785 * \param hostDest the destination host
786 * \param fileDest the destination file
788 void SALOME_LifeCycleCORBA::copyFile(const char* hostSrc, const char* fileSrc, const char* hostDest, const char* fileDest)
790 if(strcmp(hostDest,"localhost") == 0)
792 //if localhost use a shortcut
793 SALOME_FileTransferCORBA transfer(hostSrc,fileSrc);
794 transfer.getLocalFile(fileDest);
798 Engines::ContainerManager_var contManager = getContainerManager();
800 Engines::ContainerParameters params;
803 params.resource_params.hostname = hostDest;
804 params.mode = CORBA::string_dup("findorstart");
805 Engines::Container_var containerDest = contManager->GiveContainer(params);
807 params.resource_params.hostname = hostSrc;
808 Engines::Container_var containerSrc = contManager->GiveContainer(params);
810 containerDest->copyFile(containerSrc,fileSrc,fileDest);
813 /*! \brief get the naming service used by the life cycle
815 * \return the naming service
817 SALOME_NamingService * SALOME_LifeCycleCORBA::namingService()
822 /*! \brief get the orb used by the life cycle
826 CORBA::ORB_ptr SALOME_LifeCycleCORBA::orb()