]> SALOME platform Git repositories - modules/kernel.git/commitdiff
Salome HOME
Gestion du log des containers IOR pour tuer proprement les containers. agy/ssl_0
authorAnthony Geay <anthony.geay@edf.fr>
Wed, 21 Apr 2021 14:56:22 +0000 (16:56 +0200)
committerAnthony Geay <anthony.geay@edf.fr>
Wed, 21 Apr 2021 14:56:22 +0000 (16:56 +0200)
src/Container/Container_i.cxx
src/Container/SALOME_Container_i.hxx
src/NamingService/NamingService.i
src/NamingService/SALOME_Fake_NamingService.cxx
src/NamingService/SALOME_Fake_NamingService.hxx

index bf5f0eab50142da77b06be76387ec7d184952366..315f7cee6c1e5f8ea7fa975b6c0d21c19892f304 100644 (file)
@@ -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...");
index e040d7cc3947c2aef13a227e75e0d6873adb22a8..f0a8e4e76eb699704af031d416ebcc068edbbff6 100644 (file)
@@ -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;
index 9b4eb638d504a0e08acc7bacbccb901aa69851c2..08b58d9967bb02af0f1a00b7612a76ff5b94ee63 100644 (file)
@@ -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"
 
 %template(svec) std::vector<std::string>;
 
+%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)
 %}
index 5ca1bf3c8dc9b3d36c6b6b5fc74e8480a85a42b3..e9e19f30d489934edb247937cef95c1ffc612de8 100644 (file)
@@ -25,8 +25,6 @@
 
 #include <sstream>
 #include <fstream>
-#include <thread>
-#include <chrono>
 
 std::mutex SALOME_Fake_NamingService::_mutex;
 std::map<std::string,CORBA::Object_var> SALOME_Fake_NamingService::_map;
@@ -57,6 +55,7 @@ void SALOME_Fake_NamingService::Register(CORBA::Object_ptr ObjRef, const char* P
   std::lock_guard<std::mutex> 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<Engines::Container_var> SALOME_Fake_NamingService::ListOfContainersInNS()
+std::vector< std::pair< std::string, Engines::Container_var> > SALOME_Fake_NamingService::ListOfContainersInNS_NoThreadSafe()
 {
-  std::lock_guard<std::mutex> g(_mutex);
-  std::vector<Engines::Container_var> 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<Engines::Container_var> 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<std::mutex> 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<std::mutex> 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;
 }
index 4c7f81bc4b9300cfe082930f9c11f0604306b7b5..509c3181f6d70517344e1653d9c5524ca2f6ac69 100644 (file)
@@ -24,6 +24,7 @@
 #include "SALOME_NamingService_Abstract.hxx"
 
 #include <mutex>
+#include <utility>
 #include <string>
 #include <map>
 
@@ -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<std::string> 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<Engines::Container_var> 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<std::string,CORBA::Object_var> _map;