Skip to content

Commit

Permalink
XrdApps::JCache: add new source files for the Open handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
apeters1971 committed Jun 14, 2024
1 parent f8fcd50 commit 0d5c538
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 0 deletions.
78 changes: 78 additions & 0 deletions src/XrdApps/XrdClJCachePlugin/handler/XrdClJCacheOpenHandler.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//------------------------------------------------------------------------------
// Copyright (c) 2024 by European Organization for Nuclear Research (CERN)
// Author: Andreas-Joachim Peters <[email protected]>
//------------------------------------------------------------------------------
// 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 <http://www.gnu.org/licenses/>.
//
// 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<double> 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<std::mutex> 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<std::mutex> lock(mtx);
// Wait until `ready` becomes true
cv.wait(lock, [this] { return this->ready; });
return mStatus;
}

} // namespace XrdCl
69 changes: 69 additions & 0 deletions src/XrdApps/XrdClJCachePlugin/handler/XrdClJCacheOpenHandler.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//------------------------------------------------------------------------------
// Copyright (c) 2024 by European Organization for Nuclear Research (CERN)
// Author: Andreas-Joachim Peters <[email protected]>
//------------------------------------------------------------------------------
// 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 <http://www.gnu.org/licenses/>.
//
// 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 <mutex>
#include <condition_variable>
/*----------------------------------------------------------------------------*/


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<double> t2open;
std::chrono::time_point<std::chrono::steady_clock> creationTime;
std::chrono::time_point<std::chrono::steady_clock> openedTime;
};

} // namespace XrdCl

0 comments on commit 0d5c538

Please sign in to comment.