From f1bf6fc6ade440308006b99a7c5eb348548086f6 Mon Sep 17 00:00:00 2001
From: Peter Van Gemmeren <gemmeren@lxplus720.cern.ch>
Date: Mon, 19 Oct 2020 21:54:21 +0200
Subject: [PATCH] Switch PoolSvc into shared mode for SharedWriter, so Catalog
 is shared

---
 .../AthenaPoolCnvSvc/src/AthenaPoolCnvSvc.cxx | 12 +--
 .../AthenaPOOL/PoolSvc/PoolSvc/IPoolSvc.h     |  4 +
 Database/AthenaPOOL/PoolSvc/src/PoolSvc.cxx   | 80 ++++++++++---------
 Database/AthenaPOOL/PoolSvc/src/PoolSvc.h     |  5 ++
 4 files changed, 60 insertions(+), 41 deletions(-)

diff --git a/Database/AthenaPOOL/AthenaPoolCnvSvc/src/AthenaPoolCnvSvc.cxx b/Database/AthenaPOOL/AthenaPoolCnvSvc/src/AthenaPoolCnvSvc.cxx
index afe868a98f5..e4a19aa45a8 100644
--- a/Database/AthenaPOOL/AthenaPoolCnvSvc/src/AthenaPoolCnvSvc.cxx
+++ b/Database/AthenaPOOL/AthenaPoolCnvSvc/src/AthenaPoolCnvSvc.cxx
@@ -51,15 +51,17 @@ StatusCode AthenaPoolCnvSvc::initialize() {
    if (!m_outputStreamingTool.empty()) {
       m_streamClientFiles = m_streamClientFilesProp.value();
       ATH_CHECK(m_outputStreamingTool.retrieve());
-   }
-   if (!m_inputStreamingTool.empty() || !m_outputStreamingTool.empty()) {
-      // Retrieve AthenaSerializeSvc
-      ATH_CHECK(m_serializeSvc.retrieve());
-      if (!m_outputStreamingTool.empty() && m_makeStreamingToolClient.value() == -1) {
+      if (m_makeStreamingToolClient.value() == -1) {
          // Initialize AthenaRootSharedWriter
          ServiceHandle<IService> arswsvc("AthenaRootSharedWriterSvc", this->name());
          ATH_CHECK(arswsvc.retrieve());
       }
+      // Put PoolSvc into share mode to avoid duplicating catalog.
+      m_poolSvc->setShareMode(true);
+   }
+   if (!m_inputStreamingTool.empty() || !m_outputStreamingTool.empty()) {
+      // Retrieve AthenaSerializeSvc
+      ATH_CHECK(m_serializeSvc.retrieve());
    }
    // Register this service for 'I/O' events
    ServiceHandle<IIoComponentMgr> iomgr("IoComponentMgr", name());
diff --git a/Database/AthenaPOOL/PoolSvc/PoolSvc/IPoolSvc.h b/Database/AthenaPOOL/PoolSvc/PoolSvc/IPoolSvc.h
index 477f74db551..20262d49feb 100644
--- a/Database/AthenaPOOL/PoolSvc/PoolSvc/IPoolSvc.h
+++ b/Database/AthenaPOOL/PoolSvc/PoolSvc/IPoolSvc.h
@@ -73,6 +73,10 @@ public: // Non-static members
    /// @param compName [IN] string name of the component to be loaded.
    virtual void loadComponent(const std::string& compName) const = 0;
 
+   /// @return void
+   /// @param shareCat [IN] bool to share the file catalog.
+   virtual void setShareMode(bool shareCat) = 0;
+
    /// @return the file catalog.
    virtual const pool::IFileCatalog* catalog() const = 0;
 
diff --git a/Database/AthenaPOOL/PoolSvc/src/PoolSvc.cxx b/Database/AthenaPOOL/PoolSvc/src/PoolSvc.cxx
index e525cb142b8..81b177952ae 100644
--- a/Database/AthenaPOOL/PoolSvc/src/PoolSvc.cxx
+++ b/Database/AthenaPOOL/PoolSvc/src/PoolSvc.cxx
@@ -65,31 +65,33 @@ StatusCode PoolSvc::initialize() {
       ATH_MSG_FATAL("Could not register myself with the IoComponentMgr !");
       return(StatusCode::FAILURE);
    }
-   // Register input file's names with the I/O manager
-   bool allGood = true;
-   for (auto& catalog : m_readCatalog.value()) {
-      if (catalog.substr(0, 16) == "xmlcatalog_file:") {
-         const std::string& fileName = catalog.substr(16);
-         if (!iomgr->io_register(this, IIoComponentMgr::IoMode::READ, fileName, fileName).isSuccess()) {
-            ATH_MSG_FATAL("could not register [" << catalog << "] for input !");
+   // Register input file's names with the I/O manager, unless in SharedWrite mode, set by AthenaPoolCnvSvc
+   if (!m_shareCat) {
+      bool allGood = true;
+      for (auto& catalog : m_readCatalog.value()) {
+         if (catalog.substr(0, 16) == "xmlcatalog_file:") {
+            const std::string& fileName = catalog.substr(16);
+            if (!iomgr->io_register(this, IIoComponentMgr::IoMode::READ, fileName, fileName).isSuccess()) {
+               ATH_MSG_FATAL("could not register [" << catalog << "] for input !");
+               allGood = false;
+            } else {
+               ATH_MSG_INFO("io_register[" << this->name() << "](" << catalog << ") [ok]");
+            }
+         }
+      }
+      if (m_writeCatalog.value().substr(0, 16) == "xmlcatalog_file:") {
+         const std::string& fileName = m_writeCatalog.value().substr(16);
+         if (!iomgr->io_register(this, IIoComponentMgr::IoMode::WRITE, fileName, fileName).isSuccess()) {
+            ATH_MSG_FATAL("could not register [" << m_writeCatalog.value() << "] for input !");
             allGood = false;
          } else {
-            ATH_MSG_INFO("io_register[" << this->name() << "](" << catalog << ") [ok]");
+            ATH_MSG_INFO("io_register[" << this->name() << "](" << m_writeCatalog.value() << ") [ok]");
          }
       }
-   }
-   if (m_writeCatalog.value().substr(0, 16) == "xmlcatalog_file:") {
-      const std::string& fileName = m_writeCatalog.value().substr(16);
-      if (!iomgr->io_register(this, IIoComponentMgr::IoMode::WRITE, fileName, fileName).isSuccess()) {
-         ATH_MSG_FATAL("could not register [" << m_writeCatalog.value() << "] for input !");
-         allGood = false;
-      } else {
-         ATH_MSG_INFO("io_register[" << this->name() << "](" << m_writeCatalog.value() << ") [ok]");
+      if (!allGood) {
+         return(StatusCode::FAILURE);
       }
    }
-   if (!allGood) {
-      return(StatusCode::FAILURE);
-   }
    m_context = &coral::Context::instance();
    if (m_context == nullptr) {
       ATH_MSG_FATAL("Failed to access CORAL Context");
@@ -146,31 +148,33 @@ StatusCode PoolSvc::io_reinit() {
       ATH_MSG_FATAL("IoComponentMgr does not know about myself !");
       return(StatusCode::FAILURE);
    }
-   std::vector<std::string> readcat = m_readCatalog.value();
-   for (std::size_t icat = 0, imax = readcat.size(); icat < imax; icat++) {
-      if (readcat[icat].substr(0, 16) == "xmlcatalog_file:") {
-         std::string fileName = readcat[icat].substr(16);
+   if (!m_shareCat) {
+      std::vector<std::string> readcat = m_readCatalog.value();
+      for (std::size_t icat = 0, imax = readcat.size(); icat < imax; icat++) {
+         if (readcat[icat].substr(0, 16) == "xmlcatalog_file:") {
+            std::string fileName = readcat[icat].substr(16);
+            if (iomgr->io_contains(this, fileName)) {
+               if (!iomgr->io_retrieve(this, fileName).isSuccess()) {
+                  ATH_MSG_FATAL("Could not retrieve new value for [" << fileName << "] !");
+                  return(StatusCode::FAILURE);
+               }
+               readcat[icat] = "xmlcatalog_file:" + fileName;
+            }
+         }
+      }
+      // all good... copy over.
+      m_readCatalog = readcat;
+      if (m_writeCatalog.value().substr(0, 16) == "xmlcatalog_file:") {
+         std::string fileName = m_writeCatalog.value().substr(16);
          if (iomgr->io_contains(this, fileName)) {
             if (!iomgr->io_retrieve(this, fileName).isSuccess()) {
                ATH_MSG_FATAL("Could not retrieve new value for [" << fileName << "] !");
                return(StatusCode::FAILURE);
             }
-            readcat[icat] = "xmlcatalog_file:" + fileName;
+            m_writeCatalog.setValue("xmlcatalog_file:" + fileName);
          }
       }
    }
-   // all good... copy over.
-   m_readCatalog = readcat;
-   if (m_writeCatalog.value().substr(0, 16) == "xmlcatalog_file:") {
-      std::string fileName = m_writeCatalog.value().substr(16);
-      if (iomgr->io_contains(this, fileName)) {
-         if (!iomgr->io_retrieve(this, fileName).isSuccess()) {
-            ATH_MSG_FATAL("Could not retrieve new value for [" << fileName << "] !");
-            return(StatusCode::FAILURE);
-         }
-         m_writeCatalog.setValue("xmlcatalog_file:" + fileName);
-      }
-   }
    return(setupPersistencySvc());
 }
 //__________________________________________________________________________
@@ -389,6 +393,10 @@ void PoolSvc::loadComponent(const std::string& compName) const {
    m_context->loadComponent(compName);
 }
 //__________________________________________________________________________
+void PoolSvc::setShareMode(bool shareCat) {
+   m_shareCat = shareCat;
+}
+//__________________________________________________________________________
 const pool::IFileCatalog* PoolSvc::catalog() const {
    return(m_catalog);
 }
diff --git a/Database/AthenaPOOL/PoolSvc/src/PoolSvc.h b/Database/AthenaPOOL/PoolSvc/src/PoolSvc.h
index aad4e1a7e14..ec0e5648643 100644
--- a/Database/AthenaPOOL/PoolSvc/src/PoolSvc.h
+++ b/Database/AthenaPOOL/PoolSvc/src/PoolSvc.h
@@ -78,6 +78,10 @@ public: // Non-static members
    /// @param compName [IN] string name of the component to be loaded.
    void loadComponent(const std::string& compName) const;
 
+   /// @return void
+   /// @param shareCat [IN] bool to share the file catalog.
+   void setShareMode(bool shareCat);
+
    /// @return the file catalog.
    const pool::IFileCatalog* catalog() const;
 
@@ -185,6 +189,7 @@ private: // data
    typedef std::recursive_mutex CallMutex;
    mutable CallMutex                                 m_pool_mut ATLAS_THREAD_SAFE;
    coral::Context*                                   m_context{nullptr};
+   bool                                              m_shareCat{false};
    pool::IFileCatalog*                               m_catalog{nullptr};
    std::vector<pool::IPersistencySvc*>               m_persistencySvcVec;
    mutable std::vector<CallMutex*>                   m_pers_mut ATLAS_THREAD_SAFE;
-- 
GitLab