changeset 5661:71c7d260510d

update statistics thread
author Alain Mazy <am@orthanc.team>
date Wed, 03 Jul 2024 17:50:43 +0200
parents 4b7bc21db336
children d77292629430
files NEWS OrthancServer/Sources/ServerIndex.cpp OrthancServer/Sources/ServerIndex.h
diffstat 3 files changed, 55 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Tue Jun 18 10:27:28 2024 +0200
+++ b/NEWS	Wed Jul 03 17:50:43 2024 +0200
@@ -13,6 +13,10 @@
 
 * DICOM TLS: "DicomTlsTrustedCertificates" is not required anymore when issuing
   an outgoing SCU connexion when "DicomTlsRemoteCertificateRequired" is set to false.
+* Introduced a new thread to update the statistics at regular interval for the
+  DB plugins that are implementing the UpdateAndGetStatistics function (currently only
+  PostgreSQL).  This avoids very long update times in case you don't call /statistics
+  for a long period.
 
 
 Version 1.12.4 (2024-06-05)
--- a/OrthancServer/Sources/ServerIndex.cpp	Tue Jun 18 10:27:28 2024 +0200
+++ b/OrthancServer/Sources/ServerIndex.cpp	Wed Jul 03 17:50:43 2024 +0200
@@ -266,6 +266,39 @@
     }
   };
 
+  void ServerIndex::UpdateStatisticsThread(ServerIndex* that,
+                                           unsigned int threadSleepGranularityMilliseconds)
+  {
+    Logging::SetCurrentThreadName("DB-STATS");
+
+    static const unsigned int SLEEP_SECONDS = 60;
+
+    if (threadSleepGranularityMilliseconds > 1000)
+    {
+      throw OrthancException(ErrorCode_ParameterOutOfRange);
+    }
+
+    LOG(INFO) << "Starting the update statistics thread (sleep = " << SLEEP_SECONDS << " seconds)";
+
+    unsigned int count = 0;
+    unsigned int countThreshold = (1000 * SLEEP_SECONDS) / threadSleepGranularityMilliseconds;
+
+    while (!that->done_)
+    {
+      boost::this_thread::sleep(boost::posix_time::milliseconds(threadSleepGranularityMilliseconds));
+      count++;
+      
+      if (count >= countThreshold)
+      {
+        uint64_t diskSize, uncompressedSize, countPatients, countStudies, countSeries, countInstances;
+        that->GetGlobalStatistics(diskSize, uncompressedSize, countPatients, countStudies, countSeries, countInstances);
+        
+        count = 0;
+      }
+    }
+
+    LOG(INFO) << "Stopping the update statistics thread";
+  }
 
   void ServerIndex::FlushThread(ServerIndex* that,
                                 unsigned int threadSleepGranularityMilliseconds)
@@ -326,11 +359,20 @@
     // execution of Orthanc
     StandaloneRecycling(maximumStorageMode_, maximumStorageSize_, maximumPatients_);
 
+    // For some DB engines (like SQLite), make sure we flush the DB to disk at regular interval
     if (GetDatabaseCapabilities().HasFlushToDisk())
     {
       flushThread_ = boost::thread(FlushThread, this, threadSleepGranularityMilliseconds);
     }
 
+    // For some DB plugins that implements the UpdateAndGetStatistics function, updating 
+    // the statistics can take quite some time if you have not done it for a long time 
+    // -> make sure they are updated at regular interval
+    if (GetDatabaseCapabilities().HasUpdateAndGetStatistics())
+    {
+      updateStatisticsThread_ = boost::thread(UpdateStatisticsThread, this, threadSleepGranularityMilliseconds);
+    }
+
     unstableResourcesMonitorThread_ = boost::thread
       (UnstableResourcesMonitorThread, this, threadSleepGranularityMilliseconds);
   }
@@ -357,6 +399,11 @@
         flushThread_.join();
       }
 
+      if (updateStatisticsThread_.joinable())
+      {
+        updateStatisticsThread_.join();
+      }
+
       if (unstableResourcesMonitorThread_.joinable())
       {
         unstableResourcesMonitorThread_.join();
--- a/OrthancServer/Sources/ServerIndex.h	Tue Jun 18 10:27:28 2024 +0200
+++ b/OrthancServer/Sources/ServerIndex.h	Wed Jul 03 17:50:43 2024 +0200
@@ -42,6 +42,7 @@
     bool done_;
     boost::mutex monitoringMutex_;
     boost::thread flushThread_;
+    boost::thread updateStatisticsThread_;
     boost::thread unstableResourcesMonitorThread_;
 
     LeastRecentlyUsedIndex<std::pair<ResourceType, int64_t>, UnstableResourcePayload>  unstableResources_;
@@ -53,6 +54,9 @@
     static void FlushThread(ServerIndex* that,
                             unsigned int threadSleep);
 
+    static void UpdateStatisticsThread(ServerIndex* that,
+                                       unsigned int threadSleep);
+
     static void UnstableResourcesMonitorThread(ServerIndex* that,
                                                unsigned int threadSleep);