view Framework/Plugins/BaseIndexConnectionsPool.cpp @ 706:2bf70b3ef0d5 sql-opti

use SKIP LOCKED in UpdateInvlidChildCounts
author Alain Mazy <am@orthanc.team>
date Mon, 30 Jun 2025 09:49:36 +0200
parents bcea50e40d6e
children 845c3dcb723e
line wrap: on
line source

/**
 * Orthanc - A Lightweight, RESTful DICOM Store
 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
 * Department, University Hospital of Liege, Belgium
 * Copyright (C) 2017-2023 Osimis S.A., Belgium
 * Copyright (C) 2024-2025 Orthanc Team SRL, Belgium
 * Copyright (C) 2021-2025 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 **/


#include "BaseIndexConnectionsPool.h"

#include <Logging.h>


namespace OrthancDatabases
{
//   class BaseIndexConnectionsPool::ManagerReference : public Orthanc::IDynamicObject
//   {
//   private:
//     DatabaseManager*  manager_;

//   public:
//     explicit ManagerReference(DatabaseManager& manager) :
//       manager_(&manager)
//     {
//     }

//     DatabaseManager& GetManager()
//     {
//       assert(manager_ != NULL);
//       return *manager_;
//     }
//   };


  void BaseIndexConnectionsPool::HousekeepingThread(BaseIndexConnectionsPool* that)
  {
#if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 12, 2)
    OrthancPluginSetCurrentThreadName(OrthancPlugins::GetGlobalContext(), "DB HOUSEKEEPING");    
#endif

    boost::posix_time::ptime lastInvocation = boost::posix_time::second_clock::local_time();

    while (that->housekeepingContinue_)
    {
      if (boost::posix_time::second_clock::local_time() - lastInvocation >= that->housekeepingDelay_)
      {
        try
        {
          {
            Accessor accessor(*that);
            accessor.GetBackend().PerformDbHousekeeping(accessor.GetManager());
          }

          that->PerformPoolHousekeeping();
        }
        catch (Orthanc::OrthancException& e)
        {
          LOG(ERROR) << "Exception during the database housekeeping: " << e.What();
        }
        catch (...)
        {
          LOG(ERROR) << "Native exception during the database houskeeping";
        }

        lastInvocation = boost::posix_time::second_clock::local_time();
      }

      boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
    }
  }


  BaseIndexConnectionsPool::BaseIndexConnectionsPool(IndexBackend* backend,
                                                     unsigned int houseKeepingDelaySeconds) :
    backend_(backend),
    housekeepingContinue_(true),
    housekeepingDelay_(boost::posix_time::seconds(houseKeepingDelaySeconds))
  {
    if (backend == NULL)
    {
      throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
    }
    else if (backend->HasPerformDbHousekeeping() &&
             houseKeepingDelaySeconds == 0)
    {
      throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange,
                                      "The delay between two executions of housekeeping cannot be zero second");
    }
    else
    {
      context_ = backend_->GetContext();
    }
  }

  
  BaseIndexConnectionsPool::~BaseIndexConnectionsPool()
  {
  }


  void BaseIndexConnectionsPool::StartHousekeepingThread()
  {
    housekeepingContinue_ = true;

    if (backend_->HasPerformDbHousekeeping())
    {
      housekeepingThread_ = boost::thread(HousekeepingThread, this);
    }
  }

  void BaseIndexConnectionsPool::StopHousekeepingThread()
  {
    housekeepingContinue_ = false;
    if (housekeepingThread_.joinable())
    {
      housekeepingThread_.join();
    }
  }


  BaseIndexConnectionsPool::Accessor::Accessor(BaseIndexConnectionsPool& pool) :
    // lock_(pool.connectionsMutex_),
    pool_(pool),
    manager_(NULL)
  {
    for (;;)
    {
      std::unique_ptr<DatabaseManager> manager(pool.GetConnection());
      if (manager.get() != NULL)
      {
        manager_ = manager.release();
        return;
      }
      boost::this_thread::sleep(boost::posix_time::millisec(100));
    }
  }

  
  BaseIndexConnectionsPool::Accessor::~Accessor()
  {
    assert(manager_ != NULL);
    pool_.ReleaseConnection(manager_);
    // boost::unique_lock<boost::shared_mutex>  lock(pool_.connectionsMutex_);
    // pool_.availableConnections_.push_front(manager_);
    // pool_.availableConnectionsSemaphore_.Release(1);


  }

  
  IndexBackend& BaseIndexConnectionsPool::Accessor::GetBackend() const
  {
    return *pool_.backend_;
  }

  
  DatabaseManager& BaseIndexConnectionsPool::Accessor::GetManager() const
  {
    assert(manager_ != NULL);
    return *manager_;
  }
}