1 // Copyright (C) 2007-2019 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 // ==> always use absolute path and don't 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 * \return a CORBA reference of the component instance, or _nil if not found
137 //=============================================================================
138 Engines::EngineComponent_ptr
139 SALOME_LifeCycleCORBA::FindComponent(const Engines::ContainerParameters& params,
140 const char *componentName)
142 if (! isKnownComponentClass(componentName))
143 return Engines::EngineComponent::_nil();
145 Engines::ContainerParameters new_params(params);
146 new_params.resource_params.componentList.length(1);
147 new_params.resource_params.componentList[0] = componentName;
148 new_params.resource_params.can_run_containers = true;
149 Engines::ResourceList_var listOfResources;
152 listOfResources = _ResManager->GetFittingResources(new_params.resource_params);
154 catch( const SALOME::SALOME_Exception& ex )
156 return Engines::EngineComponent::_nil();
159 Engines::EngineComponent_var compo = _FindComponent(new_params,
163 return compo._retn();
166 //=============================================================================
167 /*! \brief Load a component instance on a container defined by its parameters
169 * \param params container parameters like type or name...
170 * \param componentName the name of component class
171 * \return a CORBA reference of the component instance, or _nil if problem
173 //=============================================================================
175 Engines::EngineComponent_ptr
176 SALOME_LifeCycleCORBA::LoadComponent(const Engines::ContainerParameters& params,
177 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,
203 return compo._retn();
206 //=============================================================================
207 /*! \brief Find an already existing and registered component instance or load a new
208 * component instance on a container defined by its parameters.
210 * \param params container parameters like type or name...
211 * \param componentName the name of component class
212 * \return a CORBA reference of the component instance, or _nil if problem
214 //=============================================================================
216 Engines::EngineComponent_ptr
217 SALOME_LifeCycleCORBA::
218 FindOrLoad_Component(const Engines::ContainerParameters& params,
219 const char *componentName)
221 // --- Check if Component Name is known in ModuleCatalog
223 if (! isKnownComponentClass(componentName))
224 return Engines::EngineComponent::_nil();
226 Engines::ContainerParameters new_params(params);
227 new_params.resource_params.componentList.length(1);
228 new_params.resource_params.componentList[0] = componentName;
229 new_params.resource_params.can_run_containers = true;
231 Engines::ResourceList_var listOfResources;
234 listOfResources = _ResManager->GetFittingResources(new_params.resource_params);
236 catch( const SALOME::SALOME_Exception& ex )
238 return Engines::EngineComponent::_nil();
241 Engines::EngineComponent_var compo = _FindComponent(new_params,
245 if(CORBA::is_nil(compo))
247 new_params.resource_params.resList = listOfResources;
248 compo = _LoadComponent(new_params,
252 return compo._retn();
255 //=============================================================================
256 /*! \brief Find an already existing and registered component instance or load a new
257 * component instance on a container defined by name
259 * \param containerName the name of container, under one of the forms
260 * - 1 aContainer (local container)
261 * - 2 machine/aContainer (container on hostname = machine)
262 * \param componentName the name of component class
263 * \return a CORBA reference of the component instance, or _nil if problem
265 //=============================================================================
267 Engines::EngineComponent_ptr
268 SALOME_LifeCycleCORBA::FindOrLoad_Component(const char *containerName,
269 const char *componentName)
271 MESSAGE("SALOME_LifeCycleCORBA::FindOrLoad_Component INTERACTIF " << containerName << " " << componentName ) ;
273 // --- Check if Component Name is known in ModuleCatalog
274 if (! isKnownComponentClass(componentName))
275 return Engines::EngineComponent::_nil();
277 // --- Check if containerName contains machine name (if yes: rg>0)
278 char *stContainer=strdup(containerName);
279 std::string st2Container(stContainer);
280 int rg=st2Container.find("/");
282 Engines::ContainerParameters params;
286 // containerName doesn't contain "/" => Local container
287 params.container_name = CORBA::string_dup(stContainer);
291 stContainer[rg]='\0';
292 params.container_name = CORBA::string_dup(stContainer+rg+1);
293 params.resource_params.hostname = CORBA::string_dup(stContainer);
295 params.isMPI = false;
296 SCRUTE(params.container_name);
298 return FindOrLoad_Component(params, componentName);
301 //=============================================================================
302 /*! \brief Check if the component class is known in module catalog
304 * \param componentName the name of component class
305 * \return true if found, false otherwise
307 //=============================================================================
309 bool SALOME_LifeCycleCORBA::isKnownComponentClass(const char *componentName)
313 CORBA::Object_var obj = _NS->Resolve("/Kernel/ModulCatalog");
314 SALOME_ModuleCatalog::ModuleCatalog_var Catalog =
315 SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
316 ASSERT(! CORBA::is_nil(Catalog));
317 SALOME_ModuleCatalog::Acomponent_var compoInfo =
318 Catalog->GetComponent(componentName);
319 if (CORBA::is_nil (compoInfo))
321 MESSAGE("Catalog Error: Component not found in the catalog " << componentName);
326 catch (ServiceUnreachable&)
328 INFOS("Caught exception: Naming Service Unreachable");
332 INFOS("Caught unknown exception.");
337 //=============================================================================
338 /*! \brief Initialisation of a given Engines::ResourceParameters with default values.
340 //=============================================================================
343 SALOME_LifeCycleCORBA::preSet(Engines::ResourceParameters& params)
346 params.hostname = "";
350 params.cpu_clock = 0;
352 params.nb_proc_per_node = 0;
354 params.can_launch_batch_jobs = false;
355 params.can_run_containers = false;
358 //=============================================================================
359 /*! \brief Initialisation of a given Engines::ContainerParameters with default values.
361 //=============================================================================
363 void SALOME_LifeCycleCORBA::preSet( Engines::ContainerParameters& params)
365 params.container_name = "";
367 params.workingdir = "";
369 params.isMPI = false;
370 params.parallelLib = "";
371 SALOME_LifeCycleCORBA::preSet(params.resource_params);
374 //=============================================================================
376 * \return a number of processors not 0, only for MPI containers
378 //=============================================================================
380 int SALOME_LifeCycleCORBA::NbProc(const Engines::ContainerParameters& params)
384 else if( params.nb_proc <= 0 )
387 return params.nb_proc;
390 //=============================================================================
391 /*! \brief Get the container manager
393 * \return the container Manager
395 //=============================================================================
397 Engines::ContainerManager_ptr SALOME_LifeCycleCORBA::getContainerManager()
399 Engines::ContainerManager_var contManager =
400 Engines::ContainerManager::_duplicate(_ContManager);
401 return contManager._retn();
404 //=============================================================================
405 /*! \brief Get the resources manager
407 * \return the container Manager
409 //=============================================================================
411 Engines::ResourcesManager_ptr SALOME_LifeCycleCORBA::getResourcesManager()
413 Engines::ResourcesManager_var resManager =
414 Engines::ResourcesManager::_duplicate(_ResManager);
415 return resManager._retn();
418 //=============================================================================
419 /*! \brief shutdown all the SALOME servers except SALOME_Session_Server and omniNames
421 //=============================================================================
423 void SALOME_LifeCycleCORBA::shutdownServers(bool shutdownLauncher)
425 // get each Container from NamingService => shutdown it
426 // (the order is inverse to the order of servers initialization)
428 SALOME::Session_var session = SALOME::Session::_nil();
430 CORBA::Object_var objS = _NS->Resolve("/Kernel/Session");
431 if (!CORBA::is_nil(objS))
433 session = SALOME::Session::_narrow(objS);
434 if (!CORBA::is_nil(session))
436 pid = session->getPID();
441 std::string hostname = Kernel_Utils::GetHostname();
443 // 1) ConnectionManager
446 CORBA::Object_var objCnM=_NS->Resolve("/ConnectionManager");
447 Engines::ConnectionManager_var connMan=Engines::ConnectionManager::_narrow(objCnM);
448 if ( !CORBA::is_nil(connMan) && ( pid != connMan->getPID() ) )
449 connMan->ShutdownWithExit();
451 catch(const CORBA::Exception& e)
453 // ignore and continue
457 ts_req.tv_nsec=100000000;
460 //Wait some time so that ConnectionManager be completely shutdown
462 nanosleep(&ts_req,0);
468 CORBA::Object_var objSDS = _NS->Resolve("/Study");
469 SALOMEDS::Study_var study = SALOMEDS::Study::_narrow(objSDS) ;
470 if ( !CORBA::is_nil(study) && ( pid != study->getPID() ) )
473 catch(const CORBA::Exception& e)
475 // ignore and continue
478 //Wait some time so that study be completely shutdown
480 nanosleep(&ts_req,0);
486 CORBA::Object_var objMC=_NS->Resolve("/Kernel/ModulCatalog");
487 SALOME_ModuleCatalog::ModuleCatalog_var catalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(objMC);
488 if ( !CORBA::is_nil(catalog) && ( pid != catalog->getPID() ) )
491 catch(const CORBA::Exception& e)
493 // ignore and continue
496 //Wait some time so that ModulCatalog be completely shutdown
498 nanosleep(&ts_req,0);
500 // 4 ) Remote ScopeServer (the DataServer is hosted by SalomeLauncher shutdown right after)
503 CORBA::Object_var objDSM(_NS->Resolve(SALOMESDS::DataServerManager::NAME_IN_NS));
504 SALOME::DataServerManager_var dsm(SALOME::DataServerManager::_narrow(objDSM));
505 if ( !CORBA::is_nil(dsm) )
506 dsm->shutdownScopes();
508 catch(const CORBA::Exception& e)
510 // ignore and continue
516 if(shutdownLauncher){
517 CORBA::Object_var objSL = _NS->Resolve("/SalomeLauncher");
518 Engines::SalomeLauncher_var launcher = Engines::SalomeLauncher::_narrow(objSL);
519 if (!CORBA::is_nil(launcher) && (pid != launcher->getPID()))
520 launcher->Shutdown();
523 catch(const CORBA::Exception& e)
525 // ignore and continue
528 //Wait some time so that launcher be completely shutdown
530 nanosleep(&ts_req,0);
536 CORBA::Object_var objR = _NS->Resolve("/Registry");
537 Registry::Components_var registry = Registry::Components::_narrow(objR);
538 if ( !CORBA::is_nil(registry) && ( pid != registry->getPID() ) )
539 registry->Shutdown();
540 _NS->Destroy_Name("/Registry");
542 catch(const CORBA::Exception& e)
544 // ignore and continue
549 char *xargv = (char*)"";
550 char **argv = &xargv;
551 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
553 CORBA::Object_var objLog = CORBA::Object::_nil();
554 CosNaming::NamingContext_var inc;
555 CORBA::Object_var theObj = CORBA::Object::_nil();
556 std::string stdname = "Logger";
557 CosNaming::Name name;
559 name[0].id = CORBA::string_dup(stdname.c_str());
562 if(!CORBA::is_nil(orb))
563 theObj = orb->resolve_initial_references("NameService");
564 if (!CORBA::is_nil(theObj))
565 inc = CosNaming::NamingContext::_narrow(theObj);
570 if(!CORBA::is_nil(inc))
574 objLog = inc->resolve(name);
575 SALOME_Logger::Logger_var logger = SALOME_Logger::Logger::_narrow(objLog);
576 if ( !CORBA::is_nil(logger) )
585 //=============================================================================
586 /*! \brief shutdown omniNames
588 //=============================================================================
590 void SALOME_LifeCycleCORBA::killOmniNames()
592 std::string portNumber (::getenv ("NSPORT") );
593 std::string python_exe;
595 python_exe = std::string("python3");
597 if ( !portNumber.empty() )
601 cmd = std::string("from salome_utils import killOmniNames; ");
602 cmd += std::string("killOmniNames(") + portNumber + "); ";
603 cmd = python_exe + std::string(" -c \"") + cmd +"\"";
605 system( cmd.c_str() );
607 cmd = std::string("from killSalomeWithPort import cleanApplication; ");
608 cmd += std::string("cleanApplication(") + portNumber + "); ";
609 cmd = python_exe + std::string(" -c \"") + cmd +"\"";
611 system( cmd.c_str() );
614 // shutdown portmanager
615 if ( !portNumber.empty() )
619 cmd = std::string("from PortManager import releasePort; ");
620 cmd += std::string("releasePort(") + portNumber + "); ";
621 cmd = python_exe + std::string(" -c \"") + cmd +"\"";
623 system( cmd.c_str() );
627 //=============================================================================
628 /*! \brief Find an already existing and registered component instance.
630 * - build a list of machines on which an instance of the component is running,
631 * - find the best machine among the list
633 * \param params machine parameters like type or name...
634 * \param componentName the name of component class
635 * \param listOfMachines list of machine address
636 * \return a CORBA reference of the component instance, or _nil if not found
638 //=============================================================================
640 Engines::EngineComponent_ptr
641 SALOME_LifeCycleCORBA::
642 _FindComponent(const Engines::ContainerParameters& params,
643 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 * \return a CORBA reference of the component instance, or _nil if problem
696 //=============================================================================
698 Engines::EngineComponent_ptr
699 SALOME_LifeCycleCORBA::
700 _LoadComponent(const Engines::ContainerParameters& params,
701 const char *componentName)
703 MESSAGE("_LoadComponent, required " << params.container_name <<
704 " " << componentName << " " << NbProc(params));
706 Engines::ContainerParameters local_params(params);
707 local_params.mode = CORBA::string_dup("findorstart");
708 Engines::Container_var cont = _ContManager->GiveContainer(local_params);
709 if (CORBA::is_nil(cont)) return Engines::EngineComponent::_nil();
712 bool isLoadable = cont->load_component_Library(componentName,reason);
715 //std::cerr << reason << std::endl;
716 CORBA::string_free(reason);
717 return Engines::EngineComponent::_nil();
719 CORBA::string_free(reason);
721 Engines::EngineComponent_var myInstance =
722 cont->create_component_instance(componentName);
723 return myInstance._retn();
726 //=============================================================================
727 /*! \brief Load a parallel component instance.
729 * \param params machine parameters like type or name...
730 * \param componentName the name of component class
731 * \return a CORBA reference of the parallel component instance, or _nil if problem
733 //=============================================================================
734 Engines::EngineComponent_ptr
735 SALOME_LifeCycleCORBA::Load_ParallelComponent(const Engines::ContainerParameters& params,
736 const char *componentName)
738 MESSAGE("Entering LoadParallelComponent");
740 /*MESSAGE("Parameters : ");
741 MESSAGE("Container name : " << params.container_name);
742 MESSAGE("Number of component nodes : " << params.nb_component_nodes);
743 MESSAGE("Component Name : " << componentName);*/
745 Engines::ContainerParameters parms(params);
746 parms.resource_params.componentList.length(1);
747 parms.resource_params.componentList[0] = componentName;
748 parms.mode = CORBA::string_dup("findorstart");
750 MESSAGE("Starting Parallel Container");
751 Engines::Container_var cont = _ContManager->GiveContainer(parms);
752 if (CORBA::is_nil(cont)) {
753 INFOS("FindOrStartParallelContainer() returns a NULL container !");
754 return Engines::EngineComponent::_nil();
757 MESSAGE("Loading component library");
759 bool isLoadable = cont->load_component_Library(componentName,reason);
761 INFOS(componentName <<" library is not loadable !");
762 //std::cerr << reason << std::endl;
763 CORBA::string_free(reason);
764 return Engines::EngineComponent::_nil();
766 CORBA::string_free(reason);
768 MESSAGE("Creating component instance");
769 // @PARALLEL@ permits to identify that the component requested
770 // is a parallel component.
771 std::string name = std::string(componentName);
772 Engines::EngineComponent_var myInstance = cont->create_component_instance(name.c_str());
773 if (CORBA::is_nil(myInstance))
774 INFOS("create_component_instance returns a NULL component !");
775 return myInstance._retn();
778 /*! \brief copy a file from a source host to a destination host
779 * \param hostSrc the source host
780 * \param fileSrc the file to copy from the source host to the destination host
781 * \param hostDest the destination host
782 * \param fileDest the destination file
784 void SALOME_LifeCycleCORBA::copyFile(const char* hostSrc, const char* fileSrc, const char* hostDest, const char* fileDest)
786 if(strcmp(hostDest,"localhost") == 0)
788 //if localhost use a shortcut
789 SALOME_FileTransferCORBA transfer(hostSrc,fileSrc);
790 transfer.getLocalFile(fileDest);
794 Engines::ContainerManager_var contManager = getContainerManager();
796 Engines::ContainerParameters params;
799 params.resource_params.hostname = hostDest;
800 params.mode = CORBA::string_dup("findorstart");
801 Engines::Container_var containerDest = contManager->GiveContainer(params);
803 params.resource_params.hostname = hostSrc;
804 Engines::Container_var containerSrc = contManager->GiveContainer(params);
806 containerDest->copyFile(containerSrc,fileSrc,fileDest);
809 /*! \brief get the naming service used by the life cycle
811 * \return the naming service
813 SALOME_NamingService * SALOME_LifeCycleCORBA::namingService()
818 /*! \brief get the orb used by the life cycle
822 CORBA::ORB_ptr SALOME_LifeCycleCORBA::orb()