# HG changeset patch # User Alain Mazy # Date 1720021843 -7200 # Node ID 71c7d260510de724edf5825e03407d5ed4449f3d # Parent 4b7bc21db336a22d9a19faeb01182176b6ae28f0 update statistics thread diff -r 4b7bc21db336 -r 71c7d260510d NEWS --- 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) diff -r 4b7bc21db336 -r 71c7d260510d OrthancServer/Sources/ServerIndex.cpp --- 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(); diff -r 4b7bc21db336 -r 71c7d260510d OrthancServer/Sources/ServerIndex.h --- 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, 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);