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
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() );
624 // shutdown portmanager
625 if ( !portNumber.empty() )
627 std::string cmd = ("from PortManager import releasePort; ");
628 cmd += std::string("releasePort(") + portNumber + "); ";
629 cmd = std::string("python -c \"") + cmd +"\" > /dev/null 2> /dev/null";
631 system( cmd.c_str() );
635 //=============================================================================
636 /*! \brief Find an already existing and registered component instance.
638 * - build a list of machines on which an instance of the component is running,
639 * - find the best machine among the list
641 * \param params machine parameters like type or name...
642 * \param componentName the name of component class
643 * \param studyId default = 0 : multistudy instance
644 * \param listOfMachines list of machine address
645 * \return a CORBA reference of the component instance, or _nil if not found
647 //=============================================================================
649 Engines::EngineComponent_ptr
650 SALOME_LifeCycleCORBA::
651 _FindComponent(const Engines::ContainerParameters& params,
652 const char *componentName,
654 const Engines::ResourceList& listOfResources)
656 // --- build the list of machines on which the component is already running
657 const char *containerName = params.container_name;
658 int nbproc = NbProc(params);
660 Engines::ResourceList_var resourcesOK = new Engines::ResourceList;
662 unsigned int lghtOfresourcesOK = 0;
663 resourcesOK->length(listOfResources.length());
665 for(unsigned int i=0; i < listOfResources.length(); i++)
667 const char * currentResource = listOfResources[i];
668 Engines::ResourceDefinition_var resource_definition =
669 _ResManager->GetResourceDefinition(currentResource);
670 CORBA::Object_var obj = _NS->ResolveComponent(resource_definition->hostname.in(),
674 if (!CORBA::is_nil(obj))
675 resourcesOK[lghtOfresourcesOK++] = CORBA::string_dup(currentResource);
678 // --- find the best machine among the list
679 if(lghtOfresourcesOK != 0)
681 resourcesOK->length(lghtOfresourcesOK);
682 CORBA::String_var bestResource = _ResManager->FindFirst(resourcesOK);
683 Engines::ResourceDefinition_var resource_definition =
684 _ResManager->GetResourceDefinition(bestResource);
685 CORBA::Object_var obj = _NS->ResolveComponent(resource_definition->hostname.in(),
689 return Engines::EngineComponent::_narrow(obj);
692 return Engines::EngineComponent::_nil();
695 //=============================================================================
696 /*! \brief Load a component instance.
698 * - Finds a container in the list of machine or start one.
699 * - Try to load the component library in the container,
700 * - then create an instance of the component.
702 * \param params machine parameters like type or name...
703 * \param componentName the name of component class
704 * \param studyId default = 0 : multistudy instance
705 * \return a CORBA reference of the component instance, or _nil if problem
707 //=============================================================================
709 Engines::EngineComponent_ptr
710 SALOME_LifeCycleCORBA::
711 _LoadComponent(const Engines::ContainerParameters& params,
712 const char *componentName,
715 MESSAGE("_LoadComponent, required " << params.container_name <<
716 " " << componentName << " " << NbProc(params));
718 Engines::ContainerParameters local_params(params);
719 local_params.mode = CORBA::string_dup("findorstart");
720 Engines::Container_var cont = _ContManager->GiveContainer(local_params);
721 if (CORBA::is_nil(cont)) return Engines::EngineComponent::_nil();
724 bool isLoadable = cont->load_component_Library(componentName,reason);
727 //std::cerr << reason << std::endl;
728 CORBA::string_free(reason);
729 return Engines::EngineComponent::_nil();
731 CORBA::string_free(reason);
733 Engines::EngineComponent_var myInstance =
734 cont->create_component_instance(componentName, studyId);
735 return myInstance._retn();
738 //=============================================================================
739 /*! \brief Load a parallel component instance.
741 * \param params machine parameters like type or name...
742 * \param componentName the name of component class
743 * \param studyId default = 0 : multistudy instance
744 * \return a CORBA reference of the parallel component instance, or _nil if problem
746 //=============================================================================
747 Engines::EngineComponent_ptr
748 SALOME_LifeCycleCORBA::Load_ParallelComponent(const Engines::ContainerParameters& params,
749 const char *componentName,
752 MESSAGE("Entering LoadParallelComponent");
754 /*MESSAGE("Parameters : ");
755 MESSAGE("Container name : " << params.container_name);
756 MESSAGE("Number of component nodes : " << params.nb_component_nodes);
757 MESSAGE("Component Name : " << componentName);*/
759 Engines::ContainerParameters parms(params);
760 parms.resource_params.componentList.length(1);
761 parms.resource_params.componentList[0] = componentName;
762 parms.mode = CORBA::string_dup("findorstart");
764 MESSAGE("Starting Parallel Container");
765 Engines::Container_var cont = _ContManager->GiveContainer(parms);
766 if (CORBA::is_nil(cont)) {
767 INFOS("FindOrStartParallelContainer() returns a NULL container !");
768 return Engines::EngineComponent::_nil();
771 MESSAGE("Loading component library");
773 bool isLoadable = cont->load_component_Library(componentName,reason);
775 INFOS(componentName <<" library is not loadable !");
776 //std::cerr << reason << std::endl;
777 CORBA::string_free(reason);
778 return Engines::EngineComponent::_nil();
780 CORBA::string_free(reason);
782 MESSAGE("Creating component instance");
783 // @PARALLEL@ permits to identify that the component requested
784 // is a parallel component.
785 std::string name = std::string(componentName);
786 Engines::EngineComponent_var myInstance = cont->create_component_instance(name.c_str(), studyId);
787 if (CORBA::is_nil(myInstance))
788 INFOS("create_component_instance returns a NULL component !");
789 return myInstance._retn();
792 /*! \brief copy a file from a source host to a destination host
793 * \param hostSrc the source host
794 * \param fileSrc the file to copy from the source host to the destination host
795 * \param hostDest the destination host
796 * \param fileDest the destination file
798 void SALOME_LifeCycleCORBA::copyFile(const char* hostSrc, const char* fileSrc, const char* hostDest, const char* fileDest)
800 if(strcmp(hostDest,"localhost") == 0)
802 //if localhost use a shortcut
803 SALOME_FileTransferCORBA transfer(hostSrc,fileSrc);
804 transfer.getLocalFile(fileDest);
808 Engines::ContainerManager_var contManager = getContainerManager();
810 Engines::ContainerParameters params;
813 params.resource_params.hostname = hostDest;
814 params.mode = CORBA::string_dup("findorstart");
815 Engines::Container_var containerDest = contManager->GiveContainer(params);
817 params.resource_params.hostname = hostSrc;
818 Engines::Container_var containerSrc = contManager->GiveContainer(params);
820 containerDest->copyFile(containerSrc,fileSrc,fileDest);
823 /*! \brief get the naming service used by the life cycle
825 * \return the naming service
827 SALOME_NamingService * SALOME_LifeCycleCORBA::namingService()
832 /*! \brief get the orb used by the life cycle
836 CORBA::ORB_ptr SALOME_LifeCycleCORBA::orb()