From: Anthony Geay Date: Wed, 21 Apr 2021 14:56:22 +0000 (+0200) Subject: Gestion du log des containers IOR pour tuer proprement les containers. X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=refs%2Fheads%2Fagy%2Fssl_0;p=modules%2Fkernel.git Gestion du log des containers IOR pour tuer proprement les containers. --- diff --git a/src/Container/Container_i.cxx b/src/Container/Container_i.cxx index bf5f0eab5..315f7cee6 100644 --- a/src/Container/Container_i.cxx +++ b/src/Container/Container_i.cxx @@ -379,8 +379,17 @@ void Engines_Container_i::Shutdown() } _listInstances_map.clear(); - _NS->Destroy_FullDirectory(_containerName.c_str()); - _NS->Destroy_Name(_containerName.c_str()); + // NS unregistering may throw in SSL mode if master process hosting SALOME_Embedded_NamingService servant has vanished + // In this case it's skip it and still continue. + try + { + _NS->Destroy_FullDirectory(_containerName.c_str()); + _NS->Destroy_Name(_containerName.c_str()); + } + catch(...) + { + } + // if(_isServantAloneInProcess) { MESSAGE("Effective Shutdown of container Begins..."); diff --git a/src/Container/SALOME_Container_i.hxx b/src/Container/SALOME_Container_i.hxx index e040d7cc3..f0a8e4e76 100644 --- a/src/Container/SALOME_Container_i.hxx +++ b/src/Container/SALOME_Container_i.hxx @@ -152,7 +152,7 @@ protected: bool _isSupervContainer; - SALOME_NamingService_Container_Abstract *_NS ; + SALOME_NamingService_Container_Abstract *_NS ; std::string _library_path; std::string _containerName; std::string _logfilename; diff --git a/src/NamingService/NamingService.i b/src/NamingService/NamingService.i index 9b4eb638d..08b58d996 100644 --- a/src/NamingService/NamingService.i +++ b/src/NamingService/NamingService.i @@ -21,7 +21,9 @@ %{ #include "SALOME_Fake_NamingService.hxx" +#include "SALOME_Embedded_NamingService.hxx" #include "SALOME_KernelORB.hxx" +#include "Utils_SALOME_Exception.hxx" %} %include "std_string.i" @@ -31,14 +33,46 @@ %template(svec) std::vector; +%exceptionclass SALOME_Exception; + +class SALOME_Exception +{ +public: + SALOME_Exception(const std::string& text); + ~SALOME_Exception() noexcept; + const char *what() const noexcept; + %extend + { + std::string __str__() const + { + return std::string(self->what()); + } + } +}; + +%exception { + try { + $action + } + catch (SALOME_Exception& _e) { + // Reraise with SWIG_Python_Raise + SWIG_Python_Raise(SWIG_NewPointerObj((new SALOME_Exception(static_cast< const SALOME_Exception& >(_e))),SWIGTYPE_p_SALOME_Exception,SWIG_POINTER_OWN), "SALOME_Exception", SWIGTYPE_p_SALOME_Exception); + SWIG_fail; + } +} + class SALOME_Fake_NamingService { public: SALOME_Fake_NamingService(); std::vector< std::string > repr(); - static void LaunchLogContainersFile(const std::string& logFileName); static std::string GetLogContainersFile(); + static void FlushLogContainersFile(); %extend { + static void SetLogContainersFileInternal(const std::string& logFileName) + { + SALOME_Fake_NamingService::SetLogContainersFile(logFileName); + } std::string _ResolveInternal(const char *Path) { CORBA::Object_var obj = self->Resolve(Path); @@ -52,6 +86,13 @@ public: CORBA::Object_var obj = orb->string_to_object(ior); self->Register(obj,Path); } + static std::string IOROfNS() + { + CORBA::ORB_ptr orb = KERNEL::getORB(); + Engines::EmbeddedNamingService_var ns = GetEmbeddedNamingService(); + CORBA::String_var ior = orb->object_to_string(ns); + return std::string(ior); + } } }; @@ -67,4 +108,31 @@ def NamingService_Register(self,obj,Path): self._RegisterInternal( orb.object_to_string(obj) , Path) NamingService.Resolve = NamingService_Resolve NamingService.Register = NamingService_Register +def NamingService_SetLogContainersFile(cls,logFileName = None): + if logFileName is None: + import tempfile + with tempfile.NamedTemporaryFile() as f: + logFileName = f.name + cls.SetLogContainersFileInternal(logFileName) +NamingService.SetLogContainersFile = classmethod(NamingService_SetLogContainersFile) +def NamingService_RefOfNS(cls): + ret = cls.IOROfNS() + import Engines + import CORBA + orb=CORBA.ORB_init(['']) + return orb.string_to_object(ret) +NamingService.RefOfNS = classmethod(NamingService_RefOfNS) +def NamingService_KillContainersInFile(cls,logFileName): + import Engines + import CORBA + orb=CORBA.ORB_init(['']) + with open(logFileName) as f: + cont_to_kill = [elt.split(" : ") for elt in f] + for name,ior in cont_to_kill: + try: + ref = orb.string_to_object(ior) + ref.Shutdown() + except Exception as e: + print("Failed to kill container remotely \"{}\"".format(name)) +NamingService.KillContainersInFile = classmethod(NamingService_KillContainersInFile) %} diff --git a/src/NamingService/SALOME_Fake_NamingService.cxx b/src/NamingService/SALOME_Fake_NamingService.cxx index 5ca1bf3c8..e9e19f30d 100644 --- a/src/NamingService/SALOME_Fake_NamingService.cxx +++ b/src/NamingService/SALOME_Fake_NamingService.cxx @@ -25,8 +25,6 @@ #include #include -#include -#include std::mutex SALOME_Fake_NamingService::_mutex; std::map SALOME_Fake_NamingService::_map; @@ -57,6 +55,7 @@ void SALOME_Fake_NamingService::Register(CORBA::Object_ptr ObjRef, const char* P std::lock_guard g(_mutex); CORBA::Object_var ObjRefAuto = CORBA::Object::_duplicate(ObjRef); _map[Path] = ObjRefAuto; + FlushLogContainersFile_NoThreadSafe(); } void SALOME_Fake_NamingService::Destroy_Name(const char* Path) @@ -119,58 +118,70 @@ CORBA::Object_ptr SALOME_Fake_NamingService::ResolveComponent(const char* hostna return Resolve(entryToFind.c_str()); } -std::vector SALOME_Fake_NamingService::ListOfContainersInNS() +std::vector< std::pair< std::string, Engines::Container_var> > SALOME_Fake_NamingService::ListOfContainersInNS_NoThreadSafe() { - std::lock_guard g(_mutex); - std::vector ret; + std::vector< std::pair< std::string, Engines::Container_var> > ret; for(auto it : _map) { Engines::Container_var elt = Engines::Container::_narrow(it.second); if(!CORBA::is_nil(elt)) - ret.push_back(elt); + ret.push_back({it.first,elt}); } return ret; } -std::string SALOME_Fake_NamingService::DumpInFileIORS() +std::string SALOME_Fake_NamingService::ReprOfContainersIORS_NoThreadSafe() { - std::vector conts( ListOfContainersInNS() ); + std::vector< std::pair< std::string, Engines::Container_var> > conts( ListOfContainersInNS_NoThreadSafe() ); std::ostringstream oss; CORBA::ORB_ptr orb = KERNEL::getORB(); - char SEP[2] = { '\0', '\0'}; + char SEP[2] = { '\0', '\0' }; + constexpr char SEP2[] = " : "; for(auto it : conts) { - CORBA::String_var ior(orb->object_to_string(it)); - oss << SEP << ior; + CORBA::String_var ior(orb->object_to_string(it.second)); + oss << SEP << it.first << SEP2 << ior; SEP[0] = '\n'; } return oss.str(); } -void WriteContinuously(const std::string& logFileName) +std::string SALOME_Fake_NamingService::ReprOfContainersIORS() { - while(true) - { - std::chrono::milliseconds delta( 2000 ); - std::this_thread::sleep_for( delta ); - std::string content(SALOME_Fake_NamingService::DumpInFileIORS()); - { - std::ofstream ofs(logFileName); - ofs.write(content.c_str(),content.length()); - } - } + std::lock_guard g(_mutex); + return ReprOfContainersIORS_NoThreadSafe(); } -void SALOME_Fake_NamingService::LaunchLogContainersFile(const std::string& logFileName) +std::string SALOME_Fake_NamingService::GetLogContainersFile() { - if(_log_container_file_thread_launched) - THROW_SALOME_EXCEPTION("SALOME_Fake_NamingService::LaunchLogContainersFile : Thread lready launched !"); - _log_container_file_name = logFileName; - std::thread t(WriteContinuously,logFileName); - t.detach(); + return _log_container_file_name; } -std::string SALOME_Fake_NamingService::GetLogContainersFile() +void SALOME_Fake_NamingService::FlushLogContainersFile() { - return _log_container_file_name; + std::lock_guard g(_mutex); + FlushLogContainersFile_NoThreadSafe(); +} + +void SALOME_Fake_NamingService::FlushLogContainersFile_NoThreadSafe() +{ + if(!_log_container_file_name.empty()) + { + std::string content( ReprOfContainersIORS_NoThreadSafe() ); + std::ofstream ofs(_log_container_file_name); + ofs.write(content.c_str(),content.length()); + } +} + +void SALOME_Fake_NamingService::SetLogContainersFile(const std::string& logFileName) +{ + if(logFileName.empty()) + THROW_SALOME_EXCEPTION("SALOME_Fake_NamingService::SetLogContainersFile : empty log name !"); + constexpr char EXPT_CONTENT[] = "SALOME_Fake_NamingService::SetLogContainersFile : input logFileName write access failed ! no log file set !"; + { + std::ofstream ofs(logFileName); + if(!ofs) + THROW_SALOME_EXCEPTION(EXPT_CONTENT); + } + _log_container_file_name = logFileName; } diff --git a/src/NamingService/SALOME_Fake_NamingService.hxx b/src/NamingService/SALOME_Fake_NamingService.hxx index 4c7f81bc4..509c3181f 100644 --- a/src/NamingService/SALOME_Fake_NamingService.hxx +++ b/src/NamingService/SALOME_Fake_NamingService.hxx @@ -24,6 +24,7 @@ #include "SALOME_NamingService_Abstract.hxx" #include +#include #include #include @@ -32,8 +33,9 @@ class NAMINGSERVICE_EXPORT SALOME_Fake_NamingService : public SALOME_NamingServi public: SALOME_Fake_NamingService(CORBA::ORB_ptr orb); SALOME_Fake_NamingService() = default; - static void LaunchLogContainersFile(const std::string& logFileName); + static void SetLogContainersFile(const std::string& logFileName); static std::string GetLogContainersFile(); + static void FlushLogContainersFile(); std::vector< std::string > repr() override; void init_orb(CORBA::ORB_ptr orb=0) override; void Register(CORBA::Object_ptr ObjRef, const char* Path) override; @@ -48,9 +50,11 @@ public: std::vector list_directory_recurs() override; SALOME_NamingService_Abstract *clone() override; CORBA::Object_ptr ResolveComponent(const char* hostname, const char* containerName, const char* componentName, const int nbproc=0) override; - static std::string DumpInFileIORS(); private: - static std::vector ListOfContainersInNS(); + static std::string ReprOfContainersIORS_NoThreadSafe(); + static std::string ReprOfContainersIORS(); + static std::vector< std::pair< std::string, Engines::Container_var> > ListOfContainersInNS_NoThreadSafe(); + static void FlushLogContainersFile_NoThreadSafe(); private: static std::mutex _mutex; static std::map _map;