From 0d5c538113615a22043a8e477848480cabdd6be9 Mon Sep 17 00:00:00 2001 From: Andreas Joachim Peters Date: Fri, 14 Jun 2024 17:27:15 +0200 Subject: [PATCH] XrdApps::JCache: add new source files for the Open handlers --- .../handler/XrdClJCacheOpenHandler.cc | 78 +++++++++++++++++++ .../handler/XrdClJCacheOpenHandler.hh | 69 ++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 src/XrdApps/XrdClJCachePlugin/handler/XrdClJCacheOpenHandler.cc create mode 100644 src/XrdApps/XrdClJCachePlugin/handler/XrdClJCacheOpenHandler.hh diff --git a/src/XrdApps/XrdClJCachePlugin/handler/XrdClJCacheOpenHandler.cc b/src/XrdApps/XrdClJCachePlugin/handler/XrdClJCacheOpenHandler.cc new file mode 100644 index 00000000000..c98c830b3c9 --- /dev/null +++ b/src/XrdApps/XrdClJCachePlugin/handler/XrdClJCacheOpenHandler.cc @@ -0,0 +1,78 @@ +//------------------------------------------------------------------------------ +// Copyright (c) 2024 by European Organization for Nuclear Research (CERN) +// Author: Andreas-Joachim Peters +//------------------------------------------------------------------------------ +// This file is part of the XRootD software suite. +// +// XRootD is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// XRootD 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 General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with XRootD. If not, see . +// +// In applying this licence, CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + + +/*----------------------------------------------------------------------------*/ + +/*----------------------------------------------------------------------------*/ +#include "file/XrdClJCacheFile.hh" +#include "handler/XrdClJCacheOpenHandler.hh" +/*----------------------------------------------------------------------------*/ + + +namespace XrdCl { +// ---------------------------------------------------------------------- // +void +JCacheOpenHandler::HandleResponseWithHosts(XrdCl::XRootDStatus* pStatus, + XrdCl::AnyObject* pResponse, + XrdCl::HostList* pHostList) { + + + openedTime = std::chrono::steady_clock::now(); + std::chrono::duration topen = openedTime - creationTime; + t2open = topen.count(); + + if (pHostList) { + delete pHostList; + pHostList = nullptr; + } + // Response shoud be nullptr in general + if (pResponse) { + delete pResponse; + pResponse = nullptr; + } + if (pStatus->IsOK()) { + pFile->mOpenState = JCacheFile::OPEN; + } else { + pFile->mOpenState = JCacheFile::FAILED; + } + mStatus = *pStatus; + std::lock_guard lock(mtx); + ready = true; + cv.notify_one(); // Notify Wait() +} + +XRootDStatus +JCacheOpenHandler::Wait() { + // quick bypass, we know we have opened + if (pFile && pFile->mOpenState == JCacheFile::OPEN) + return mStatus; + + // slower condition variable path + std::unique_lock lock(mtx); + // Wait until `ready` becomes true + cv.wait(lock, [this] { return this->ready; }); + return mStatus; +} + +} // namespace XrdCl diff --git a/src/XrdApps/XrdClJCachePlugin/handler/XrdClJCacheOpenHandler.hh b/src/XrdApps/XrdClJCachePlugin/handler/XrdClJCacheOpenHandler.hh new file mode 100644 index 00000000000..1e456b4b709 --- /dev/null +++ b/src/XrdApps/XrdClJCachePlugin/handler/XrdClJCacheOpenHandler.hh @@ -0,0 +1,69 @@ +//------------------------------------------------------------------------------ +// Copyright (c) 2024 by European Organization for Nuclear Research (CERN) +// Author: Andreas-Joachim Peters +//------------------------------------------------------------------------------ +// This file is part of the XRootD software suite. +// +// XRootD is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// XRootD 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 General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with XRootD. If not, see . +// +// In applying this licence, CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + +#pragma once +/*----------------------------------------------------------------------------*/ +#include "XrdCl/XrdClFile.hh" +#include "XrdCl/XrdClXRootDResponses.hh" +/*----------------------------------------------------------------------------*/ +#include +#include +/*----------------------------------------------------------------------------*/ + + +namespace XrdCl { + +class JCacheFile; + +class JCacheOpenHandler : public XrdCl::ResponseHandler +// ---------------------------------------------------------------------- // +{ +public: + JCacheOpenHandler() : ready(false), pFile(nullptr), t2open(0) {} + JCacheOpenHandler(XrdCl::JCacheFile* file) + : ready(false), pFile(file), t2open(0) { + creationTime = std::chrono::steady_clock::now(); + } + + virtual ~JCacheOpenHandler() {} + + void HandleResponseWithHosts(XrdCl::XRootDStatus* pStatus, + XrdCl::AnyObject* pResponse, + XrdCl::HostList* pHostList); + + XrdCl::XRootDStatus Wait(); + bool ready; + + double GetTimeToOpen() {return t2open;} + +private: + XrdCl::JCacheFile* pFile; + XrdCl::XRootDStatus mStatus; + std::mutex mtx; + std::condition_variable cv; + std::atomic t2open; + std::chrono::time_point creationTime; + std::chrono::time_point openedTime; +}; + +} // namespace XrdCl