changeset 5493:b3ebe249ed5b pg-transactions

At startup, when using a database plugin, display the latency to access the DB
author Alain Mazy <am@osimis.io>
date Mon, 15 Jan 2024 18:25:08 +0100
parents 26877f4b306f
children 3e02be2ccaee
files NEWS OrthancServer/Plugins/Engine/OrthancPluginDatabase.cpp OrthancServer/Plugins/Engine/OrthancPluginDatabase.h OrthancServer/Plugins/Engine/OrthancPluginDatabaseV3.cpp OrthancServer/Plugins/Engine/OrthancPluginDatabaseV3.h OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.cpp OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.h OrthancServer/Plugins/Include/orthanc/OrthancDatabasePlugin.proto OrthancServer/Sources/Database/IDatabaseWrapper.h OrthancServer/Sources/Database/SQLiteDatabaseWrapper.cpp OrthancServer/Sources/Database/SQLiteDatabaseWrapper.h OrthancServer/Sources/main.cpp
diffstat 12 files changed, 93 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Wed Jan 10 15:26:10 2024 +0100
+++ b/NEWS	Mon Jan 15 18:25:08 2024 +0100
@@ -1,6 +1,17 @@
 Pending changes in the mainline
 ===============================
 
+General
+-------
+
+* Performance:
+  - Databases:
+    - At startup, when using a database plugin, display the latency to access the DB.
+    - Added support for new DB primitives to enable the "READ COMMITTED" transaction mode
+      in the PostgreSQL plugin.
+
+
+
 
 Version 1.12.2 (2023-12-19)
 ===========================
--- a/OrthancServer/Plugins/Engine/OrthancPluginDatabase.cpp	Wed Jan 10 15:26:10 2024 +0100
+++ b/OrthancServer/Plugins/Engine/OrthancPluginDatabase.cpp	Mon Jan 15 18:25:08 2024 +0100
@@ -1478,7 +1478,7 @@
     activeTransaction_(NULL),
     fastGetTotalSize_(false),
     currentDiskSize_(0),
-    dbCapabilities_(false, false, false, false, false)
+    dbCapabilities_(false, false, false, false, false, false)
   {
     static const char* const MISSING = "  Missing extension in database index plugin: ";
     
--- a/OrthancServer/Plugins/Engine/OrthancPluginDatabase.h	Wed Jan 10 15:26:10 2024 +0100
+++ b/OrthancServer/Plugins/Engine/OrthancPluginDatabase.h	Mon Jan 15 18:25:08 2024 +0100
@@ -109,6 +109,10 @@
       return dbCapabilities_;
     }
 
+    virtual uint64_t MeasureLatency() ORTHANC_OVERRIDE
+    {
+      throw OrthancException(ErrorCode_NotImplemented);  // only implemented in V4
+    }
 
     void AnswerReceived(const _OrthancPluginDatabaseAnswer& answer);
   };
--- a/OrthancServer/Plugins/Engine/OrthancPluginDatabaseV3.cpp	Wed Jan 10 15:26:10 2024 +0100
+++ b/OrthancServer/Plugins/Engine/OrthancPluginDatabaseV3.cpp	Mon Jan 15 18:25:08 2024 +0100
@@ -1092,7 +1092,9 @@
                     false,  /* revision support is updated in open() */ 
                     false,  /* hasLabelsSupport */
                     false,  /* hasAtomicIncrementGlobalProperty */
-                    false   /* hasUpdateAndGetStatistics */)
+                    false, /* hasUpdateAndGetStatistics */
+                    false  /* hasMeasureLatency */)
+
   {
     CLOG(INFO, PLUGINS) << "Identifier of this Orthanc server for the global properties "
                         << "of the custom database: \"" << serverIdentifier << "\"";
--- a/OrthancServer/Plugins/Engine/OrthancPluginDatabaseV3.h	Wed Jan 10 15:26:10 2024 +0100
+++ b/OrthancServer/Plugins/Engine/OrthancPluginDatabaseV3.h	Mon Jan 15 18:25:08 2024 +0100
@@ -77,6 +77,11 @@
     virtual void Upgrade(unsigned int targetVersion,
                          IStorageArea& storageArea) ORTHANC_OVERRIDE;    
 
+    virtual uint64_t MeasureLatency() ORTHANC_OVERRIDE
+    {
+      throw OrthancException(ErrorCode_NotImplemented);  // only implemented in V4
+    }
+
     const IDatabaseWrapper::Capabilities& GetDatabaseCapabilities() const ORTHANC_OVERRIDE
     {
       return dbCapabilities_;
--- a/OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.cpp	Wed Jan 10 15:26:10 2024 +0100
+++ b/OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.cpp	Mon Jan 15 18:25:08 2024 +0100
@@ -1293,7 +1293,7 @@
     serverIdentifier_(serverIdentifier),
     open_(false),
     databaseVersion_(0),
-    dbCapabilities_(false, false, false, false, false) // updated in Open()
+    dbCapabilities_(false, false, false, false, false, false) // updated in Open()
   {
     CLOG(INFO, PLUGINS) << "Identifier of this Orthanc server for the global properties "
                         << "of the custom database: \"" << serverIdentifier << "\"";
@@ -1368,6 +1368,7 @@
       dbCapabilities_.hasLabelsSupport_ = systemInfo.supports_labels();
       dbCapabilities_.hasAtomicIncrementGlobalProperty_ = systemInfo.supports_increment_global_property();
       dbCapabilities_.hasUpdateAndGetStatistics_ = systemInfo.has_update_and_get_statistics();
+      dbCapabilities_.hasMeasureLatency_ = systemInfo.has_measure_latency();
     }
 
     open_ = true;
@@ -1466,6 +1467,30 @@
   }
 
 
+  uint64_t OrthancPluginDatabaseV4::MeasureLatency()
+  {
+    if (!open_)
+    {
+      throw OrthancException(ErrorCode_BadSequenceOfCalls);
+    }
+    else
+    {
+      try
+      {
+        DatabasePluginMessages::DatabaseRequest request;
+        DatabasePluginMessages::DatabaseResponse response;
+
+        ExecuteDatabase(response, *this, DatabasePluginMessages::OPERATION_MEASURE_LATENCY, request);
+        return response.measure_latency().latency_us();
+
+      }
+      catch (OrthancException& e)
+      {
+        throw;
+      }
+    }
+  }
+
   const IDatabaseWrapper::Capabilities& OrthancPluginDatabaseV4::GetDatabaseCapabilities() const
   {
     if (!open_)
--- a/OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.h	Wed Jan 10 15:26:10 2024 +0100
+++ b/OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.h	Mon Jan 15 18:25:08 2024 +0100
@@ -89,6 +89,8 @@
     virtual void Upgrade(unsigned int targetVersion,
                          IStorageArea& storageArea) ORTHANC_OVERRIDE;    
 
+    virtual uint64_t MeasureLatency() ORTHANC_OVERRIDE;
+
     virtual const IDatabaseWrapper::Capabilities& GetDatabaseCapabilities() const ORTHANC_OVERRIDE;
   };
 }
--- a/OrthancServer/Plugins/Include/orthanc/OrthancDatabasePlugin.proto	Wed Jan 10 15:26:10 2024 +0100
+++ b/OrthancServer/Plugins/Include/orthanc/OrthancDatabasePlugin.proto	Mon Jan 15 18:25:08 2024 +0100
@@ -121,6 +121,7 @@
   OPERATION_START_TRANSACTION = 4;
   OPERATION_UPGRADE = 5;
   OPERATION_FINALIZE_TRANSACTION = 6;
+  OPERATION_MEASURE_LATENCY = 7;         // New in Orthanc 1.12.X
 }
 
 enum TransactionType {
@@ -138,6 +139,7 @@
     bool supports_labels = 4;
     bool supports_increment_global_property = 5;
     bool has_update_and_get_statistics = 6;
+    bool has_measure_latency = 7;
   }
 }
 
@@ -200,6 +202,15 @@
   }
 }
 
+message MeasureLatency {
+  message Request {
+  }
+  message Response {
+    int64 latency_us = 1;
+  }
+}
+
+
 message DatabaseRequest {
   sfixed64           database = 1;
   DatabaseOperation  operation = 2;
@@ -210,7 +221,8 @@
   FlushToDisk.Request           flush_to_disk = 103;
   StartTransaction.Request      start_transaction = 104;
   Upgrade.Request               upgrade = 105;
-  FinalizeTransaction.Request   finalize_transaction = 106;
+  FinalizeTransaction.Request   finalize_transaction = 106; 
+  MeasureLatency.Request        measure_latency = 107;
 }
 
 message DatabaseResponse {
@@ -221,6 +233,7 @@
   StartTransaction.Response      start_transaction = 104;
   Upgrade.Response               upgrade = 105;
   FinalizeTransaction.Response   finalize_transaction = 106;
+  MeasureLatency.Response        measure_latency = 107;
 }
 
 
--- a/OrthancServer/Sources/Database/IDatabaseWrapper.h	Wed Jan 10 15:26:10 2024 +0100
+++ b/OrthancServer/Sources/Database/IDatabaseWrapper.h	Mon Jan 15 18:25:08 2024 +0100
@@ -57,18 +57,21 @@
       bool hasLabelsSupport_;
       bool hasAtomicIncrementGlobalProperty_;
       bool hasUpdateAndGetStatistics_;
+      bool hasMeasureLatency_;
 
     public:
       Capabilities(bool hasFlushToDisk,
                    bool hasRevisionsSupport,
                    bool hasLabelsSupport,
                    bool hasAtomicIncrementGlobalProperty,
-                   bool hasUpdateAndGetStatistics)
+                   bool hasUpdateAndGetStatistics,
+                   bool hasMeasureLatency)
       : hasFlushToDisk_(hasFlushToDisk),
         hasRevisionsSupport_(hasRevisionsSupport),
         hasLabelsSupport_(hasLabelsSupport),
         hasAtomicIncrementGlobalProperty_(hasAtomicIncrementGlobalProperty),
-        hasUpdateAndGetStatistics_(hasUpdateAndGetStatistics)
+        hasUpdateAndGetStatistics_(hasUpdateAndGetStatistics),
+        hasMeasureLatency_(hasMeasureLatency)
       {
       }
 
@@ -97,6 +100,11 @@
         return hasUpdateAndGetStatistics_;
       }
 
+      bool HasMeasureLatency() const
+      {
+        return hasMeasureLatency_;
+      }
+
     };
 
     struct CreateInstanceResult : public boost::noncopyable
@@ -348,5 +356,7 @@
                          IStorageArea& storageArea) = 0;
 
     virtual const IDatabaseWrapper::Capabilities& GetDatabaseCapabilities() const = 0;
+
+    virtual uint64_t MeasureLatency() = 0;
   };
 }
--- a/OrthancServer/Sources/Database/SQLiteDatabaseWrapper.cpp	Wed Jan 10 15:26:10 2024 +0100
+++ b/OrthancServer/Sources/Database/SQLiteDatabaseWrapper.cpp	Mon Jan 15 18:25:08 2024 +0100
@@ -1336,7 +1336,8 @@
                     false, /* hasRevisionsSupport TODO: implement revisions in SQLite */ 
                     true,  /* hasLabelsSupport */
                     false, /* hasAtomicIncrementGlobalProperty */
-                    false  /* hasUpdateAndGetStatistics */)
+                    false, /* hasUpdateAndGetStatistics */
+                    false  /* hasMeasureLatency */)
   {
     db_.Open(path);
   }
@@ -1350,7 +1351,8 @@
                     false, /* hasRevisionsSupport TODO: implement revisions in SQLite */ 
                     true,  /* hasLabelsSupport */
                     false, /* hasAtomicIncrementGlobalProperty */
-                    false  /* hasUpdateAndGetStatistics */)
+                    false, /* hasUpdateAndGetStatistics */
+                    false  /* hasMeasureLatency */)
   {
     db_.OpenInMemory();
   }
--- a/OrthancServer/Sources/Database/SQLiteDatabaseWrapper.h	Wed Jan 10 15:26:10 2024 +0100
+++ b/OrthancServer/Sources/Database/SQLiteDatabaseWrapper.h	Mon Jan 15 18:25:08 2024 +0100
@@ -93,6 +93,11 @@
       return dbCapabilities_;
     }
 
+    virtual uint64_t MeasureLatency() ORTHANC_OVERRIDE
+    {
+      throw OrthancException(ErrorCode_NotImplemented);
+    }
+
     /**
      * The "StartTransaction()" method is guaranteed to return a class
      * derived from "UnitTestsTransaction". The methods of
--- a/OrthancServer/Sources/main.cpp	Wed Jan 10 15:26:10 2024 +0100
+++ b/OrthancServer/Sources/main.cpp	Mon Jan 15 18:25:08 2024 +0100
@@ -1672,6 +1672,12 @@
     LOG(WARNING) << "The custom database back-end has *no* support for labels";
   }
 
+  if (database.GetDatabaseCapabilities().HasMeasureLatency())
+  {
+    LOG(WARNING) << "The DB latency is " << database.MeasureLatency() << " µs";
+  }
+
+
   bool success = ConfigureServerContext(database, storageArea, plugins, loadJobsFromDatabase);
 
   database.Close();