changeset 598:8f6e7ae942f3 find-refactoring

DB Housekeeping
author Alain Mazy <am@orthanc.team>
date Wed, 27 Nov 2024 16:05:49 +0100
parents 0963afafaa07
children cda99cc96843
files Framework/Plugins/DatabaseBackendAdapterV4.cpp Framework/Plugins/IDatabaseBackend.h Framework/Plugins/IndexBackend.cpp Framework/Plugins/IndexBackend.h PostgreSQL/CMakeLists.txt PostgreSQL/NEWS PostgreSQL/Plugins/PostgreSQLIndex.cpp PostgreSQL/Plugins/PostgreSQLIndex.h
diffstat 8 files changed, 65 insertions(+), 102 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Plugins/DatabaseBackendAdapterV4.cpp	Wed Nov 27 13:05:10 2024 +0100
+++ b/Framework/Plugins/DatabaseBackendAdapterV4.cpp	Wed Nov 27 16:05:49 2024 +0100
@@ -445,6 +445,9 @@
         response.mutable_get_system_information()->set_has_extended_changes(accessor.GetBackend().HasExtendedChanges());
 #endif
 
+#if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 12, 5)
+        response.mutable_get_system_information()->set_has_db_housekeeping(accessor.GetBackend().HasPerformDbHousekeeping());
+#endif
         break;
       }
 
@@ -1335,6 +1338,14 @@
       }
 #endif
 
+#if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 12, 5)
+      case Orthanc::DatabasePluginMessages::OPERATION_PERFORM_DB_HOUSEKEEPING:
+      {
+        backend.PerformDbHousekeeping(manager);
+        break;
+      }
+#endif
+
       default:
         LOG(ERROR) << "Not implemented transaction operation from protobuf: " << request.operation();
         throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
@@ -1436,9 +1447,6 @@
       
       if (isBackendInUse_)
       {
-        IndexConnectionsPool::Accessor accessor(*pool);
-        accessor.GetBackend().Shutdown();
-        
         isBackendInUse_ = false;
       }
       else
--- a/Framework/Plugins/IDatabaseBackend.h	Wed Nov 27 13:05:10 2024 +0100
+++ b/Framework/Plugins/IDatabaseBackend.h	Wed Nov 27 16:05:49 2024 +0100
@@ -388,9 +388,7 @@
 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 12, 5)
     virtual bool HasFindSupport() const = 0;
     virtual bool HasExtendedChanges() const = 0;
-#endif
 
-#if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 12, 5)
     // New in Orthanc 1.12.5
     virtual void ExecuteFind(Orthanc::DatabasePluginMessages::TransactionResponse& response,
                              DatabaseManager& manager,
@@ -400,6 +398,11 @@
     virtual void ExecuteCount(Orthanc::DatabasePluginMessages::TransactionResponse& response,
                               DatabaseManager& manager,
                               const Orthanc::DatabasePluginMessages::Find_Request& request) = 0;
+
+    virtual bool HasPerformDbHousekeeping() = 0;
+
+    virtual void PerformDbHousekeeping(DatabaseManager& manager) = 0;
+
 #endif
   };
 }
--- a/Framework/Plugins/IndexBackend.cpp	Wed Nov 27 13:05:10 2024 +0100
+++ b/Framework/Plugins/IndexBackend.cpp	Wed Nov 27 16:05:49 2024 +0100
@@ -4186,6 +4186,17 @@
       statement->Next();
     }    
   }
+
+  bool IndexBackend::HasPerformDbHousekeeping()
+  {
+    return false;
+  }
+
+  void IndexBackend::PerformDbHousekeeping(DatabaseManager& manager)
+  {
+    throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);    
+  }
+
 #endif
 
 }
--- a/Framework/Plugins/IndexBackend.h	Wed Nov 27 13:05:10 2024 +0100
+++ b/Framework/Plugins/IndexBackend.h	Wed Nov 27 16:05:49 2024 +0100
@@ -443,13 +443,9 @@
     {
       return true;
     }
-#endif
 
-#if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 12, 5)
     virtual bool HasFindSupport() const ORTHANC_OVERRIDE;
-#endif
 
-#if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 12, 5)
     virtual void ExecuteFind(Orthanc::DatabasePluginMessages::TransactionResponse& response,
                              DatabaseManager& manager,
                              const Orthanc::DatabasePluginMessages::Find_Request& request) ORTHANC_OVERRIDE;
@@ -457,11 +453,12 @@
     virtual void ExecuteCount(Orthanc::DatabasePluginMessages::TransactionResponse& response,
                               DatabaseManager& manager,
                               const Orthanc::DatabasePluginMessages::Find_Request& request) ORTHANC_OVERRIDE;
-#endif
 
+    virtual bool HasPerformDbHousekeeping() ORTHANC_OVERRIDE;
 
-    virtual void Shutdown()
-    {};
+    virtual void PerformDbHousekeeping(DatabaseManager& manager) ORTHANC_OVERRIDE;
+
+#endif
 
 
     /**
--- a/PostgreSQL/CMakeLists.txt	Wed Nov 27 13:05:10 2024 +0100
+++ b/PostgreSQL/CMakeLists.txt	Wed Nov 27 16:05:49 2024 +0100
@@ -25,11 +25,11 @@
 set(ORTHANC_PLUGIN_VERSION "mainline")
 
 # This is the preferred version of the Orthanc SDK for this plugin
-set(ORTHANC_SDK_DEFAULT_VERSION "1.12.3")
+set(ORTHANC_SDK_DEFAULT_VERSION "1.12.3")  # TODO: 1.12.5
 
 # This is the list of the versions of the Orthanc SDK against which
 # this plugin will compile
-set(ORTHANC_SDK_COMPATIBLE_VERSIONS "1.12.3" "1.12.4")
+set(ORTHANC_SDK_COMPATIBLE_VERSIONS "1.12.3" "1.12.4")  # TODO: 1.12.5
 
 # This is the minimal version of the Orthanc runtime that will provide
 # best performance. If the version of the Orthanc runtime is below
@@ -37,13 +37,13 @@
 # plugin will still start).
 set(ORTHANC_OPTIMAL_VERSION_MAJOR    1)
 set(ORTHANC_OPTIMAL_VERSION_MINOR    12)
-set(ORTHANC_OPTIMAL_VERSION_REVISION 3)
+set(ORTHANC_OPTIMAL_VERSION_REVISION 3)  # TODO: 1.12.5
 
 if (ORTHANC_PLUGIN_VERSION STREQUAL "mainline")
   set(ORTHANC_FRAMEWORK_VERSION "mainline")
   set(ORTHANC_FRAMEWORK_DEFAULT_SOURCE "hg")
 else()
-  set(ORTHANC_FRAMEWORK_VERSION "1.12.4")
+  set(ORTHANC_FRAMEWORK_VERSION "1.12.4")  # TODO: 1.12.5
   set(ORTHANC_FRAMEWORK_DEFAULT_SOURCE "web")
 endif()
 
--- a/PostgreSQL/NEWS	Wed Nov 27 13:05:10 2024 +0100
+++ b/PostgreSQL/NEWS	Wed Nov 27 16:05:49 2024 +0100
@@ -2,9 +2,9 @@
 ===============================
 
 DB schema revision: 3
-Minimum plugin SDK (for build): 1.12.3
+Minimum plugin SDK (for build): 1.12.5+
 Optimal plugin SDK (for build): 1.12.5+
-Minimum Orthanc runtime: 1.12.3
+Minimum Orthanc runtime: 1.12.5+
 Optimal Orthanc runtime: 1.12.5+
 
 Minimal Postgresql Server version: 9
--- a/PostgreSQL/Plugins/PostgreSQLIndex.cpp	Wed Nov 27 13:05:10 2024 +0100
+++ b/PostgreSQL/Plugins/PostgreSQLIndex.cpp	Wed Nov 27 16:05:49 2024 +0100
@@ -56,7 +56,7 @@
     IndexBackend(context, readOnly),
     parameters_(parameters),
     clearAll_(false),
-    housekeeperShouldStop_(false)
+    hkHasComputedAllMissingChildCount_(false)
   {
   }
 
@@ -254,11 +254,6 @@
         throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
       }
     }
-
-    if (!IsReadOnly())
-    {
-      StartDbHousekeeper(manager);
-    }
   }
 
 
@@ -748,77 +743,38 @@
     throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
   }
 
-  void PostgreSQLIndex::StartDbHousekeeper(DatabaseManager& manager)
-  {
-    housekeeperShouldStop_ = false;
-    dbHousekeeper_.reset(new boost::thread(WorkerHousekeeper, housekeeperShouldStop_, &manager));
-  }
-
-  void PostgreSQLIndex::Shutdown()
+  bool PostgreSQLIndex::HasPerformDbHousekeeping()
   {
-    // TODO: stop thread
-    housekeeperShouldStop_ = true;
-    if (dbHousekeeper_.get() != NULL && dbHousekeeper_->joinable())
-    {
-      dbHousekeeper_->join();
-      dbHousekeeper_.reset();
-    }
+    return true;
   }
 
-  void PostgreSQLIndex::WorkerHousekeeper(bool& housekeeperShouldStop,
-                                          DatabaseManager* manager)
+  void PostgreSQLIndex::PerformDbHousekeeping(DatabaseManager& manager)
   {
-    OrthancPluginSetCurrentThreadName(OrthancPlugins::GetGlobalContext(), "PG DB-HK");
-    LOG(WARNING) << "Starting the DB Housekeeper thread";
+    // Compute the missing child count (table introduced in rev3)
+    if (!hkHasComputedAllMissingChildCount_)
+    {
+      DatabaseManager::CachedStatement statement(STATEMENT_FROM_HERE, manager,
+        "SELECT ComputeMissingChildCount(50)");
 
-    bool hasComputedAllMissingChildCount = false;
-    while (!housekeeperShouldStop && !hasComputedAllMissingChildCount)
-    {
-      if (!hasComputedAllMissingChildCount)
-      {
-        LOG(INFO) << "Computing missing ChildCount";
+      statement.Execute();
 
-        DatabaseManager::Transaction t(*manager, TransactionType_ReadWrite);        
-        
-        try
-        {
-          DatabaseManager::CachedStatement statement(STATEMENT_FROM_HERE, *manager,
-            "SELECT ComputeMissingChildCount(50)");
-
-          statement.Execute();
+      int64_t updatedCount = statement.ReadInteger64(0);
+      hkHasComputedAllMissingChildCount_ = updatedCount == 0;
 
-          int64_t updatedCount = statement.ReadInteger64(0);
-          hasComputedAllMissingChildCount = updatedCount == 0;
-
-          t.Commit();
-        }
-        catch (...)
-        {
-          LOG(ERROR) << "Unexpected error";
-        }
+      if (updatedCount > 0)
+      {
+        LOG(INFO) << "Computed " << updatedCount << " missing ChildCount entries";
       }
-      Orthanc::SystemToolbox::USleep(10000);
+      else
+      {
+        LOG(INFO) << "No missing ChildCount entries";
+      }
     }
 
-    LOG(INFO) << "Stopping the DB Housekeeper thread";
+    // Consume the statistics delta to minimize computation when calling ComputeStatisticsReadOnly
+    {
+      int64_t patientsCount, studiesCount, seriesCount, instancesCount, compressedSize, uncompressedSize;
+      UpdateAndGetStatistics(manager, patientsCount, studiesCount, seriesCount, instancesCount, compressedSize, uncompressedSize);
+    }
   }
-
-// #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 12, 5)
-//   bool PostgreSQLIndex::HasFindSupport() const
-//   {
-//     // TODO-FIND
-//     return false;
-//   }
-// #endif
-
-
-// #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 12, 5)
-//   void PostgreSQLIndex::ExecuteFind(Orthanc::DatabasePluginMessages::TransactionResponse& response,
-//                                     DatabaseManager& manager,
-//                                     const Orthanc::DatabasePluginMessages::Find_Request& request)
-//   {
-//     // TODO-FIND
-//     throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
-//   }
-// #endif
 }
--- a/PostgreSQL/Plugins/PostgreSQLIndex.h	Wed Nov 27 13:05:10 2024 +0100
+++ b/PostgreSQL/Plugins/PostgreSQLIndex.h	Wed Nov 27 16:05:49 2024 +0100
@@ -35,8 +35,7 @@
   private:
     PostgreSQLParameters   parameters_;
     bool                   clearAll_;
-    std::unique_ptr<boost::thread>  dbHousekeeper_;
-    bool                   housekeeperShouldStop_;
+    bool                   hkHasComputedAllMissingChildCount_;
 
   protected:
     virtual void ClearDeletedFiles(DatabaseManager& manager) ORTHANC_OVERRIDE;
@@ -52,10 +51,6 @@
 
     void ApplyPrepareIndex(DatabaseManager::Transaction& t, DatabaseManager& manager);
 
-    void StartDbHousekeeper(DatabaseManager& manager);
-
-    static void WorkerHousekeeper(bool& housekeeperShouldStop, DatabaseManager* manager);
-
   public:
     PostgreSQLIndex(OrthancPluginContext* context,
                     const PostgreSQLParameters& parameters,
@@ -149,16 +144,9 @@
                                         int64_t& compressedSize,
                                         int64_t& uncompressedSize) ORTHANC_OVERRIDE;
 
-    virtual void Shutdown() ORTHANC_OVERRIDE;
-
-// #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 12, 5)
-//     virtual bool HasFindSupport() const ORTHANC_OVERRIDE;
-// #endif
+    virtual bool HasPerformDbHousekeeping() ORTHANC_OVERRIDE;
 
-// #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 12, 5)
-//     virtual void ExecuteFind(Orthanc::DatabasePluginMessages::TransactionResponse& response,
-//                              DatabaseManager& manager,
-//                              const Orthanc::DatabasePluginMessages::Find_Request& request) ORTHANC_OVERRIDE;
-// #endif
+    virtual void PerformDbHousekeeping(DatabaseManager& manager) ORTHANC_OVERRIDE;
+
   };
 }