From c6c2d3f58c0739a70c7950b29f55e177802e6915 Mon Sep 17 00:00:00 2001
From: Peter Van Gemmeren <peter.van.gemmeren@cern.ch>
Date: Tue, 15 Sep 2020 09:29:08 +0000
Subject: [PATCH] Ensure all clients can connect to SharedWriter even in short
 jobs.

---
 Control/AthenaIPCTools/src/AthenaSharedWriter.cxx           | 1 +
 Control/AthenaKernel/AthenaKernel/IAthenaSharedWriterSvc.h  | 3 +++
 Control/AthenaMPTools/src/SharedWriterTool.cxx              | 3 +++
 Database/APR/RootStorageSvc/src/RootTreeIndexContainer.cpp  | 5 ++---
 .../AthenaPOOL/AthenaPoolCnvSvc/src/AthenaPoolCnvSvc.cxx    | 3 ++-
 .../AthenaPoolCnvSvc/src/AthenaRootSharedWriterSvc.cxx      | 6 +++---
 .../AthenaPoolCnvSvc/src/AthenaRootSharedWriterSvc.h        | 3 ++-
 7 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/Control/AthenaIPCTools/src/AthenaSharedWriter.cxx b/Control/AthenaIPCTools/src/AthenaSharedWriter.cxx
index 74b6027d7e1..40c3d48b5e5 100644
--- a/Control/AthenaIPCTools/src/AthenaSharedWriter.cxx
+++ b/Control/AthenaIPCTools/src/AthenaSharedWriter.cxx
@@ -31,6 +31,7 @@ StatusCode AthenaSharedWriter::initialize() {
    }
    // Initialize IAthenaSharedWriterSvc
    ATH_CHECK(m_sharedWriterSvc.retrieve());
+   ATH_CHECK(m_sharedWriterSvc->share(m_numberOfClients.value()));
    return StatusCode::SUCCESS;
 }
 //___________________________________________________________________________
diff --git a/Control/AthenaKernel/AthenaKernel/IAthenaSharedWriterSvc.h b/Control/AthenaKernel/AthenaKernel/IAthenaSharedWriterSvc.h
index 43200e7b0c3..e23bff389c0 100644
--- a/Control/AthenaKernel/AthenaKernel/IAthenaSharedWriterSvc.h
+++ b/Control/AthenaKernel/AthenaKernel/IAthenaSharedWriterSvc.h
@@ -11,6 +11,9 @@ static const InterfaceID IID_IAthenaSharedWriterSvc( "IAthenaSharedWriterSvc", 1
 
 class IAthenaSharedWriterSvc : virtual public ::IService {
 public:
+   virtual StatusCode share(int numClients = 0) = 0;
+
+   /// Gaudi boilerplate
    static const InterfaceID& interfaceID() { return IID_IAthenaSharedWriterSvc; }
 };
 
diff --git a/Control/AthenaMPTools/src/SharedWriterTool.cxx b/Control/AthenaMPTools/src/SharedWriterTool.cxx
index 4b743a02e58..5075d75ffd4 100644
--- a/Control/AthenaMPTools/src/SharedWriterTool.cxx
+++ b/Control/AthenaMPTools/src/SharedWriterTool.cxx
@@ -243,6 +243,9 @@ std::unique_ptr<AthenaInterprocess::ScheduledWork> SharedWriterTool::exec_func()
   if(sc.isFailure() || sharedWriterSvc==0) {
     ATH_MSG_ERROR("Error retrieving AthenaRootSharedWriterSvc");
     all_ok=false;
+  } else if(!sharedWriterSvc->share(m_nprocs).isSuccess()) {
+    ATH_MSG_ERROR("Exec function could not share data");
+    all_ok=false;
   }
   AthCnvSvc* cnvSvc = dynamic_cast<AthCnvSvc*>(m_cnvSvc);
   if (cnvSvc == 0 || !cnvSvc->disconnectOutput("").isSuccess()) {
diff --git a/Database/APR/RootStorageSvc/src/RootTreeIndexContainer.cpp b/Database/APR/RootStorageSvc/src/RootTreeIndexContainer.cpp
index 5364d26c5f2..eea0dc2bdd2 100644
--- a/Database/APR/RootStorageSvc/src/RootTreeIndexContainer.cpp
+++ b/Database/APR/RootStorageSvc/src/RootTreeIndexContainer.cpp
@@ -12,10 +12,9 @@
 
 // Root include files
 #include "TTree.h"
+#include "TMemFile.h"
 #include "TBranch.h"
 
-#include <iostream>
-
 using namespace pool;
 using namespace std;
 
@@ -76,7 +75,7 @@ DbStatus RootTreeIndexContainer::transAct(Transaction::Action action) {
    DbStatus status = RootTreeContainer::transAct(action);
    if (action == Transaction::TRANSACT_FLUSH) {
       if (m_tree == nullptr) return Error;
-      if (m_index_ref != nullptr && m_tree->GetEntries() > 0 && m_tree->GetEntryNumberWithIndex(nextRecordId()) == -1) {
+      if (m_index_ref != nullptr && m_tree->GetEntries() > 0 && dynamic_cast<TMemFile*>(m_tree->GetCurrentFile()) == nullptr && m_tree->GetEntryNumberWithIndex(nextRecordId()) == -1) {
          m_tree->BuildIndex("index_ref");
       }
    }
diff --git a/Database/AthenaPOOL/AthenaPoolCnvSvc/src/AthenaPoolCnvSvc.cxx b/Database/AthenaPOOL/AthenaPoolCnvSvc/src/AthenaPoolCnvSvc.cxx
index 4de3b302c08..bca157b86f9 100644
--- a/Database/AthenaPOOL/AthenaPoolCnvSvc/src/AthenaPoolCnvSvc.cxx
+++ b/Database/AthenaPOOL/AthenaPoolCnvSvc/src/AthenaPoolCnvSvc.cxx
@@ -564,6 +564,7 @@ StatusCode AthenaPoolCnvSvc::commitOutput(const std::string& outputConnectionSpe
       ATH_MSG_ERROR("connectOutput FAILED extract file name and technology.");
       return(StatusCode::FAILURE);
    }
+   const std::string oldOutputConnection = outputConnection;
    if (!m_outputStreamingTool.empty() && m_outputStreamingTool[0]->isClient() && m_streamMetaDataOnly) {
       m_fileCommitCounter[outputConnection] = m_fileCommitCounter[outputConnection] + 1;
       if (m_fileFlushSetting[outputConnection] > 0 && m_fileCommitCounter[outputConnection] % m_fileFlushSetting[outputConnection] == 0) {
@@ -599,7 +600,7 @@ StatusCode AthenaPoolCnvSvc::commitOutput(const std::string& outputConnectionSpe
       ATH_MSG_ERROR("commitOutput - caught exception: " << e.what());
       return(StatusCode::FAILURE);
    }
-   if (!this->cleanUp(outputConnection).isSuccess()) {
+   if (!this->cleanUp(oldOutputConnection).isSuccess()) {
       ATH_MSG_ERROR("commitOutput FAILED to cleanup converters.");
       return(StatusCode::FAILURE);
    }
diff --git a/Database/AthenaPOOL/AthenaPoolCnvSvc/src/AthenaRootSharedWriterSvc.cxx b/Database/AthenaPOOL/AthenaPoolCnvSvc/src/AthenaRootSharedWriterSvc.cxx
index 3b25a5de026..410e4e4742b 100644
--- a/Database/AthenaPOOL/AthenaPoolCnvSvc/src/AthenaRootSharedWriterSvc.cxx
+++ b/Database/AthenaPOOL/AthenaPoolCnvSvc/src/AthenaRootSharedWriterSvc.cxx
@@ -69,7 +69,7 @@ StatusCode AthenaRootSharedWriterSvc::initialize() {
    } else {
       std::string propertyName = "StreamMetaDataOnly";
       bool streamMetaDataOnly(false);
-      BooleanProperty streamMetaDataOnlyProp(propertyName,streamMetaDataOnly);
+      BooleanProperty streamMetaDataOnlyProp(propertyName, streamMetaDataOnly);
       if (propertyServer->getProperty(&streamMetaDataOnlyProp).isFailure()) {
          ATH_MSG_INFO("Conversion service does not have StreamMetaDataOnly property");
       } else if(streamMetaDataOnlyProp.value()) {
@@ -87,10 +87,10 @@ StatusCode AthenaRootSharedWriterSvc::initialize() {
    return StatusCode::SUCCESS;
 }
 //___________________________________________________________________________
-StatusCode AthenaRootSharedWriterSvc::start() {
+StatusCode AthenaRootSharedWriterSvc::share(int numClients) {
    ATH_MSG_VERBOSE("Start commitOutput loop");
    StatusCode sc = m_cnvSvc->commitOutput("", false);
-   while (m_rootClientCount > 0 || (m_rootClientIndex == 0 && (sc.isSuccess() || sc.isRecoverable()))) {
+   while (m_rootClientCount > 0 || (m_rootClientIndex < numClients && (sc.isSuccess() || sc.isRecoverable()))) {
       if (sc.isSuccess()) {
          ATH_MSG_VERBOSE("Success in commitOutput loop");
       } else if (m_rootMonitor != nullptr) {
diff --git a/Database/AthenaPOOL/AthenaPoolCnvSvc/src/AthenaRootSharedWriterSvc.h b/Database/AthenaPOOL/AthenaPoolCnvSvc/src/AthenaRootSharedWriterSvc.h
index dc9c989df34..40fbab2425e 100644
--- a/Database/AthenaPOOL/AthenaPoolCnvSvc/src/AthenaRootSharedWriterSvc.h
+++ b/Database/AthenaPOOL/AthenaPoolCnvSvc/src/AthenaRootSharedWriterSvc.h
@@ -36,11 +36,12 @@ public: // Constructor and Destructor
 public:
 /// Gaudi Service Interface method implementations:
    virtual StatusCode initialize() override;
-   virtual StatusCode start() override;
    virtual StatusCode stop() override;
    virtual StatusCode finalize() override;
    virtual StatusCode queryInterface(const InterfaceID& riid, void** ppvInterface) override;
 
+   virtual StatusCode share(int numClients = 0) override;
+
 private:
    ServiceHandle<IConversionSvc> m_cnvSvc{this,"AthenaPoolCnvSvc","AthenaPoolCnvSvc"};
 
-- 
GitLab