diff --git a/src/XrdApps/XrdClJCachePlugin/cache/Journal.cc b/src/XrdApps/XrdClJCachePlugin/cache/Journal.cc index 6b92ca6d2e6..d565b16eaa1 100644 --- a/src/XrdApps/XrdClJCachePlugin/cache/Journal.cc +++ b/src/XrdApps/XrdClJCachePlugin/cache/Journal.cc @@ -26,14 +26,13 @@ #include "Journal.hh" /*----------------------------------------------------------------------------*/ #include +#include +#include #include #include #include #include #include -#include -#include -#include /*----------------------------------------------------------------------------*/ //------------------------------------------------------------------------------ @@ -61,7 +60,8 @@ Journal::~Journal() { lock.l_type = F_UNLCK; // Unlock the file if (fcntl(fd, F_SETLK, &lock) == -1) { - std::cerr << "error: failed to unlock journal: " << std::strerror(errno) << std::endl; + std::cerr << "error: failed to unlock journal: " << std::strerror(errno) + << std::endl; } int rc = close(fd); @@ -79,7 +79,7 @@ Journal::~Journal() { //------------------------------------------------------------------------------ void Journal::read_jheader() { jheader_t fheader; - bool exists=false; + bool exists = false; auto hr = ::pread64(fd, &fheader, sizeof(jheader), 0); if ((hr > 0) && ((hr != sizeof(jheader)) || (fheader.magic != JOURNAL_MAGIC))) { @@ -90,7 +90,7 @@ void Journal::read_jheader() { return; } - exists = (hr==sizeof(jheader)); + exists = (hr == sizeof(jheader)); // TODO: understand why the mtime is +-1s if (jheader.mtime) { @@ -100,16 +100,17 @@ void Journal::read_jheader() { (fheader.mtime_nsec != jheader.mtime_nsec) || (jheader.filesize && (fheader.filesize != jheader.filesize))) { std::cerr << "warning: remote file change detected - purging path:" - << path << std::endl; - std::cerr << fheader.mtime << ":" << jheader.mtime << " " - << fheader.mtime_nsec << ":" << jheader.mtime_nsec << " " - << fheader.filesize << ":" << jheader.filesize << std::endl; + << path << std::endl; + std::cerr << fheader.mtime << ":" << jheader.mtime << " " + << fheader.mtime_nsec << ":" << jheader.mtime_nsec << " " + << fheader.filesize << ":" << jheader.filesize << std::endl; reset(); return; } } } else { - // we assume the contents referenced in the header is ok to allow disconnected ops + // we assume the contents referenced in the header is ok to allow + // disconnected ops jheader.mtime = fheader.mtime; jheader.filesize = fheader.filesize; } @@ -217,14 +218,15 @@ int Journal::attach(const std::string &lpath, uint64_t mtime, if (fcntl(fd, F_SETLK, &lock) == -1) { if (errno == EACCES || errno == EAGAIN) { - std::cerr << "error: journal file is already locked by another process." - << std::endl; + std::cerr + << "error: journal file is already locked by another process." + << std::endl; close(fd); fd = -1; return -errno; } else { - std::cerr << "error: failed to lock journal file: " << std::strerror(errno) - << std::endl; + std::cerr << "error: failed to lock journal file: " + << std::strerror(errno) << std::endl; close(fd); fd = -1; return -errno; diff --git a/src/XrdApps/XrdClJCachePlugin/cache/Journal.hh b/src/XrdApps/XrdClJCachePlugin/cache/Journal.hh index 85c79dc8fef..43f6343263f 100644 --- a/src/XrdApps/XrdClJCachePlugin/cache/Journal.hh +++ b/src/XrdApps/XrdClJCachePlugin/cache/Journal.hh @@ -81,7 +81,7 @@ public: // base class interface int attach(const std::string &path, uint64_t mtime, uint64_t mtime_nsec, - uint64_t size, bool ifexists=false); + uint64_t size, bool ifexists = false); int detach(); int unlink(); @@ -105,7 +105,7 @@ public: return jheader.filesize; } - off_t getHeaderMtime() { + off_t getHeaderMtime() { std::lock_guard guard(mtx); return jheader.mtime; } diff --git a/src/XrdApps/XrdClJCachePlugin/cache/RbTree.hh b/src/XrdApps/XrdClJCachePlugin/cache/RbTree.hh index fa0043d0381..86dd255606b 100644 --- a/src/XrdApps/XrdClJCachePlugin/cache/RbTree.hh +++ b/src/XrdApps/XrdClJCachePlugin/cache/RbTree.hh @@ -354,7 +354,7 @@ protected: template // make it a template so it works both for constant // and mutable pointers - static PTR &find_in(const K &key, PTR &node) { + static PTR &find_in(const K &key, PTR &node) { if (!node) { return null_node; } @@ -372,7 +372,7 @@ protected: template // make it a template so it works both for constant // and mutable pointers - static PTR &find_min(PTR &node) { + static PTR &find_min(PTR &node) { if (!node) { return null_node; } @@ -386,7 +386,7 @@ protected: template // make it a template so it works both for constant // and mutable pointers - static PTR &find_successor(PTR &node) { + static PTR &find_successor(PTR &node) { if (!node) { return null_node; } diff --git a/src/XrdApps/XrdClJCachePlugin/cleaner/Cleaner.cc b/src/XrdApps/XrdClJCachePlugin/cleaner/Cleaner.cc index da3e57d0e68..4ca1ef0a659 100644 --- a/src/XrdApps/XrdClJCachePlugin/cleaner/Cleaner.cc +++ b/src/XrdApps/XrdClJCachePlugin/cleaner/Cleaner.cc @@ -67,8 +67,9 @@ long long Cleaner::getDirectorySize(const fs::path &directory, bool scan) { struct statfs stat; if (statfs(directory.c_str(), &stat) != 0) { - mLog->Error(1,"JCache:Cleaner: failed to get directory size using statfs."); - return 0; + mLog->Error(1, + "JCache:Cleaner: failed to get directory size using statfs."); + return 0; } } return totalSize; @@ -108,8 +109,10 @@ void Cleaner::cleanDirectory(const fs::path &directory, long long highWatermark, long long currentSize = getDirectorySize(directory); if (currentSize <= highWatermark) { /*----------------------------------------------------------------------------*/ - mLog->Info(1,"JCache:Cleaner: Directory size is within the limit (%lu/%lu). No action needed.", - currentSize, highWatermark); + mLog->Info(1, + "JCache:Cleaner: Directory size is within the limit (%lu/%lu). " + "No action needed.", + currentSize, highWatermark); return; } @@ -130,12 +133,13 @@ void Cleaner::cleanDirectory(const fs::path &directory, long long highWatermark, std::error_code ec; fs::remove_all(parentDir, ec); if (ec) { - mLog->Error(1, "JCache::Cleaner: error deleting directory '%s'", parentDir.c_str()); + mLog->Error(1, "JCache::Cleaner: error deleting directory '%s'", + parentDir.c_str()); } - mLog->Info(1, "JCache:Cleaner : deleted '%s' (Size: %lu bytes)", filePath.c_str(), - fileSize); + mLog->Info(1, "JCache:Cleaner : deleted '%s' (Size: %lu bytes)", + filePath.c_str(), fileSize); } catch (const std::exception &e) { - mLog->Error(1,"JCache::Cleaner error deleting '%'", filePath.c_str()); + mLog->Error(1, "JCache::Cleaner error deleting '%'", filePath.c_str()); } } } diff --git a/src/XrdApps/XrdClJCachePlugin/cleaner/Cleaner.hh b/src/XrdApps/XrdClJCachePlugin/cleaner/Cleaner.hh index f8677a29067..66a50e9f148 100644 --- a/src/XrdApps/XrdClJCachePlugin/cleaner/Cleaner.hh +++ b/src/XrdApps/XrdClJCachePlugin/cleaner/Cleaner.hh @@ -51,14 +51,14 @@ public: bool scan, size_t interval) : lowWatermark(lowWatermark), highWatermark(highWatermark), subtree(path), scan(scan), interval(interval), stopFlag(false) { - mLog = XrdCl::DefaultEnv::GetLog(); - } + mLog = XrdCl::DefaultEnv::GetLog(); + } - Cleaner() - : lowWatermark(0), highWatermark(0), subtree(""), - scan(true), interval(60), stopFlag(false) { - mLog = XrdCl::DefaultEnv::GetLog(); - } + Cleaner() + : lowWatermark(0), highWatermark(0), subtree(""), scan(true), + interval(60), stopFlag(false) { + mLog = XrdCl::DefaultEnv::GetLog(); + } // Method to start the cleaning process in a separate thread void run() { @@ -79,22 +79,22 @@ public: ~Cleaner() { stop(); } // Method to Define Cleaner size - void SetSize(uint64_t size, const std::string& path) { + void SetSize(uint64_t size, const std::string &path) { stop(); - if (size > 1024ll*1024ll*1024ll) { + if (size > 1024ll * 1024ll * 1024ll) { subtree = path; highWatermark = size; lowWatermark = size * 0.9; run(); } else { - mLog->Error(1, "JCache:Cleaner : the size given to the cleaner is less than 1GB - cleaning is disabled!"); + mLog->Error(1, "JCache:Cleaner : the size given to the cleaner is less " + "than 1GB - cleaning is disabled!"); } } - // Method to set the scan option (true means scan, false means don't scan but use statfs!) - void SetScan(bool sc) { - scan = sc; - } + // Method to set the scan option (true means scan, false means don't scan but + // use statfs!) + void SetScan(bool sc) { scan = sc; } private: // Private methods @@ -132,7 +132,7 @@ private: auto duration = std::chrono::duration_cast(end - start); - if ( (size_t)duration.count() < interval) { + if ((size_t)duration.count() < interval) { auto s = std::chrono::seconds(interval) - duration; std::this_thread::sleep_for(s); } diff --git a/src/XrdApps/XrdClJCachePlugin/file/Art.hh b/src/XrdApps/XrdClJCachePlugin/file/Art.hh index 98d80026dd2..86a24ef83e0 100644 --- a/src/XrdApps/XrdClJCachePlugin/file/Art.hh +++ b/src/XrdApps/XrdClJCachePlugin/file/Art.hh @@ -50,7 +50,7 @@ public: minValue = 0; // we round the axis to clean 100 - maxValue = (int)(maxValue+9) / 10 * 10; + maxValue = (int)(maxValue + 9) / 10 * 10; const int plotHeight = 10; // Number of lines in the plot const int plotWidth = 40; // Width of the plot in characters diff --git a/src/XrdApps/XrdClJCachePlugin/file/CacheStats.hh b/src/XrdApps/XrdClJCachePlugin/file/CacheStats.hh index cffd527ee99..a81c49845b5 100644 --- a/src/XrdApps/XrdClJCachePlugin/file/CacheStats.hh +++ b/src/XrdApps/XrdClJCachePlugin/file/CacheStats.hh @@ -47,13 +47,13 @@ namespace JCache { struct CacheStats { CacheStats(bool doe = false) : bytesRead(0), bytesReadV(0), bytesCached(0), bytesCachedV(0), - readOps(0), readVOps(0), readVreadOps(0), nreadfiles(0), totaldatasize(0), - dumponexit(doe), peakrate(0) { + readOps(0), readVOps(0), readVreadOps(0), nreadfiles(0), + totaldatasize(0), dumponexit(doe), peakrate(0) { // Get the current real time struct timeval now; gettimeofday(&now, nullptr); startTime = now.tv_sec + now.tv_usec / 1000000.0; - dumperInterval=0; + dumperInterval = 0; } ~CacheStats() { @@ -82,7 +82,8 @@ struct CacheStats { XrdCl::JCacheFile::sStats.persistToJson(jsonpath, name); } if (XrdCl::JCacheFile::sEnableSummary) { - std::cerr << CacheStats::GlobalStats(XrdCl::JCacheFile::sStats, XrdCl::JCacheFile::sEnableBypass); + std::cerr << CacheStats::GlobalStats(XrdCl::JCacheFile::sStats, + XrdCl::JCacheFile::sEnableBypass); } std::vector bins = XrdCl::JCacheFile::sStats.bench.GetBins(40); JCache::Art art; @@ -102,7 +103,7 @@ struct CacheStats { } void Reset() { - bytesRead = 0 ; + bytesRead = 0; bytesReadV = 0; bytesCached = 0; bytesCachedV = 0; @@ -287,16 +288,16 @@ struct CacheStats { << std::endl; oss << "# JCache : cache read hit rate : " << std::fixed << std::setprecision(2) << (!sStats.ReadOpBytes() ? "\033[9m" : "") - << sStats.HitRate() << " %" << (!sStats.ReadOpBytes() ? "\033[0m" : "") - << std::endl; + << sStats.HitRate() << " %" + << (!sStats.ReadOpBytes() ? "\033[0m" : "") << std::endl; oss << "# JCache : cache readv hit rate : " << std::fixed << std::setprecision(2) << (!sStats.ReadVOpBytes() ? "\033[9m" : "") - << sStats.HitRateV() << " %" << (!sStats.ReadOpBytes() ? "\033[0m" : "") - << std::endl; + << sStats.HitRateV() << " %" + << (!sStats.ReadOpBytes() ? "\033[0m" : "") << std::endl; } oss << "# " - "-------------------------------------------------------------------" - "---- #" + "-------------------------------------------------------------------" + "---- #" << std::endl; oss << "# JCache : total bytes read : " @@ -321,8 +322,8 @@ struct CacheStats { << std::endl; oss << "# JCache : open unique f. read : " << sStats.UniqueUrls() << std::endl; - oss << "# JCache : time to open files (s) : " << std::setprecision(3) << sStats.opentime.load() - << std::endl; + oss << "# JCache : time to open files (s) : " << std::setprecision(3) + << sStats.opentime.load() << std::endl; oss << "# " "-------------------------------------------------------------------" "---- #" @@ -369,7 +370,7 @@ struct CacheStats { std::atomic readVreadOps; std::atomic nreadfiles; std::atomic totaldatasize; - std::atomic opentime; + std::atomic opentime; std::atomic dumponexit; std::set urls; @@ -386,26 +387,30 @@ struct CacheStats { std::thread dumperThread; std::atomic stopFlag; - // Loop to regulary dump the statistics and to reset global counters - static void dumper(CacheStats* stats) { + static void dumper(CacheStats *stats) { while (!stats->stopFlag.load()) { if (stats->dumperInterval) { - std::this_thread::sleep_for(std::chrono::seconds(stats->dumperInterval)); + std::this_thread::sleep_for( + std::chrono::seconds(stats->dumperInterval)); } else { break; } if (XrdCl::JCacheFile::sEnableSummary) { XrdCl::JCacheFile::sStats.GetTimes(); XrdCl::JCacheFile::sStats.bytes_per_second = - XrdCl::JCacheFile::sStats.bench.GetBins((int)(XrdCl::JCacheFile::sStats.realTime)); - XrdCl::JCacheFile::sStats.peakrate = - *(std::max_element(XrdCl::JCacheFile::sStats.bytes_per_second.begin(), + XrdCl::JCacheFile::sStats.bench.GetBins( + (int)(XrdCl::JCacheFile::sStats.realTime)); + XrdCl::JCacheFile::sStats.peakrate = *( + std::max_element(XrdCl::JCacheFile::sStats.bytes_per_second.begin(), XrdCl::JCacheFile::sStats.bytes_per_second.end())); if (XrdCl::JCacheFile::sStats.realTime < 1) { - XrdCl::JCacheFile::sStats.peakrate = XrdCl::JCacheFile::sStats.ReadBytes() / XrdCl::JCacheFile::sStats.realTime; + XrdCl::JCacheFile::sStats.peakrate = + XrdCl::JCacheFile::sStats.ReadBytes() / + XrdCl::JCacheFile::sStats.realTime; } - std::string st = CacheStats::GlobalStats(XrdCl::JCacheFile::sStats, XrdCl::JCacheFile::sEnableBypass); + std::string st = CacheStats::GlobalStats( + XrdCl::JCacheFile::sStats, XrdCl::JCacheFile::sEnableBypass); std::cerr << st << std::endl; } } diff --git a/src/XrdApps/XrdClJCachePlugin/file/XrdClJCacheFile.cc b/src/XrdApps/XrdClJCachePlugin/file/XrdClJCacheFile.cc index fd86d59d3d5..699081f6434 100644 --- a/src/XrdApps/XrdClJCachePlugin/file/XrdClJCacheFile.cc +++ b/src/XrdApps/XrdClJCachePlugin/file/XrdClJCacheFile.cc @@ -44,9 +44,10 @@ namespace XrdCl { //------------------------------------------------------------------------------ // Constructor //------------------------------------------------------------------------------ -JCacheFile::JCacheFile(const std::string &url) : mIsOpen(false), pFile(0), mOpenAsync(false) { +JCacheFile::JCacheFile(const std::string &url) + : mIsOpen(false), pFile(0), mOpenAsync(false) { mAttachedForRead = false; - mOpenState=JCacheFile::CLOSED; + mOpenState = JCacheFile::CLOSED; mLog = DefaultEnv::GetLog(); pOpenHandler = nullptr; } @@ -55,7 +56,7 @@ JCacheFile::JCacheFile(const std::string &url) : mIsOpen(false), pFile(0), mOpen //------------------------------------------------------------------------------ JCacheFile::JCacheFile() : mIsOpen(false), pFile(0), mOpenAsync(false) { mAttachedForRead = false; - mOpenState=JCacheFile::CLOSED; + mOpenState = JCacheFile::CLOSED; pStats = new JCache::CacheStats(); mLog = DefaultEnv::GetLog(); pOpenHandler = nullptr; @@ -104,7 +105,6 @@ XRootDStatus JCacheFile::Open(const std::string &url, OpenFlags::Flags flags, cleanUrl.SetPath(origUrl.GetPath()); pUrl = cleanUrl.GetURL(); - // allow to enable asynchronous operation globally if (sOpenAsync) { mOpenAsync = true; @@ -112,9 +112,9 @@ XRootDStatus JCacheFile::Open(const std::string &url, OpenFlags::Flags flags, // allow to enable asynchronous operation by CGI per file if (origUrl.GetParams().count("xrd.jcache.async") && - origUrl.GetParams().at("xrd.jcache.async")=="1") { + origUrl.GetParams().at("xrd.jcache.async") == "1") { mLog->Info(1, "JCache : user allowed async/detached mode"); - mOpenAsync =true; + mOpenAsync = true; } if ((flags & OpenFlags::Flags::Read) == OpenFlags::Flags::Read) { @@ -126,25 +126,25 @@ XRootDStatus JCacheFile::Open(const std::string &url, OpenFlags::Flags flags, st = pOpenHandler->Wait(); } if (st.IsOK()) { - mIsOpen = true; - mOpenState = OPENING; - if (sEnableVectorCache || (sEnableJournalCache && !sEnableBypass)) { - if ((flags & OpenFlags::Flags::Read) == OpenFlags::Flags::Read) { - std::string JournalDir = - sCachePath + "/" + VectorCache::computeSHA256(pUrl); - pJournalPath = JournalDir + "/journal"; - // it can be that we cannot write the journal directory - if (!VectorCache::ensureLastSubdirectoryExists(JournalDir)) { - st = XRootDStatus(stError, errOSError); - std::cerr << "error: unable to create cache directory: " << JournalDir - << std::endl; - return st; - } + mIsOpen = true; + mOpenState = OPENING; + if (sEnableVectorCache || (sEnableJournalCache && !sEnableBypass)) { + if ((flags & OpenFlags::Flags::Read) == OpenFlags::Flags::Read) { + std::string JournalDir = + sCachePath + "/" + VectorCache::computeSHA256(pUrl); + pJournalPath = JournalDir + "/journal"; + // it can be that we cannot write the journal directory + if (!VectorCache::ensureLastSubdirectoryExists(JournalDir)) { + st = XRootDStatus(stError, errOSError); + std::cerr << "error: unable to create cache directory: " + << JournalDir << std::endl; + return st; } } + } mOpenState = OPENING; // call the external handler to pretend all is already good! - handler->HandleResponseWithHosts(new XRootDStatus(st),0,0); + handler->HandleResponseWithHosts(new XRootDStatus(st), 0, 0); } else { mOpenState = FAILED; } @@ -198,10 +198,11 @@ XRootDStatus JCacheFile::Stat(bool force, ResponseHandler *handler, // let's create a stat response using the cache AnyObject *obj = new AnyObject(); std::string id = pUrl; - auto statInfo = new StatInfo(id, pJournal->getHeaderFileSize(), 0, pJournal->getHeaderMtime()); + auto statInfo = new StatInfo(id, pJournal->getHeaderFileSize(), 0, + pJournal->getHeaderMtime()); obj->Set(statInfo); XRootDStatus *ret_st = new XRootDStatus(XRootDStatus(stOK, 0)); - handler->HandleResponse(ret_st,obj); + handler->HandleResponse(ret_st, obj); st = XRootDStatus(stOK, 0); return st; } @@ -209,7 +210,9 @@ XRootDStatus JCacheFile::Stat(bool force, ResponseHandler *handler, // we have to be sure the file is opened if (pOpenHandler) { st = pOpenHandler->Wait(); - if (!st.IsOK()) {return st;} + if (!st.IsOK()) { + return st; + } } st = pFile->Stat(force, handler, timeout); } else { @@ -231,7 +234,7 @@ XRootDStatus JCacheFile::Read(uint64_t offset, uint32_t size, void *buffer, if (!sEnableBypass && sEnableJournalCache && AttachForRead()) { mLog->Info(1, "JCache : Read: offset=%llu size=%llu buffer=%x path='%s'", - offset, size, buffer, pUrl.c_str()); + offset, size, buffer, pUrl.c_str()); bool eof = false; auto rb = pJournal->pread(buffer, size, offset, eof); if ((rb == size) || (eof && rb)) { @@ -251,12 +254,14 @@ XRootDStatus JCacheFile::Read(uint64_t offset, uint32_t size, void *buffer, // we have to be sure the file is opened if (pOpenHandler) { st = pOpenHandler->Wait(); - if (!st.IsOK()) {return st;} + if (!st.IsOK()) { + return st; + } } - auto jhandler = - new JCacheReadHandler(handler, &pStats->bytesRead, - sEnableJournalCache &&! sEnableBypass? pJournal.get() : nullptr); + auto jhandler = new JCacheReadHandler( + handler, &pStats->bytesRead, + sEnableJournalCache && !sEnableBypass ? pJournal.get() : nullptr); pStats->readOps++; st = pFile->Read(offset, size, buffer, jhandler, timeout); } else { @@ -291,13 +296,14 @@ XRootDStatus JCacheFile::PgRead(uint64_t offset, uint32_t size, void *buffer, sStats.bench.AddMeasurement(size); if (sEnableJournalCache && AttachForRead() && !sEnableBypass) { - mLog->Info(1, "JCache : PgRead: offset=%llu size=%llu buffer=%x path='%s'", - offset, size, buffer, pUrl.c_str()); + mLog->Info(1, + "JCache : PgRead: offset=%llu size=%llu buffer=%x path='%s'", + offset, size, buffer, pUrl.c_str()); bool eof = false; auto rb = pJournal->pread(buffer, size, offset, eof); - mLog->Info(1, "JCache : PgRead: rb=%llu size=%llu eof=%x path='%s'", - rb, size, eof, pUrl.c_str()); + mLog->Info(1, "JCache : PgRead: rb=%llu size=%llu eof=%x path='%s'", rb, + size, eof, pUrl.c_str()); if ((rb == size) || (eof && rb)) { pStats->bytesCached += rb; pStats->readOps++; @@ -315,11 +321,13 @@ XRootDStatus JCacheFile::PgRead(uint64_t offset, uint32_t size, void *buffer, // we have to be sure the file is opened if (pOpenHandler) { st = pOpenHandler->Wait(); - if (!st.IsOK()) {return st;} + if (!st.IsOK()) { + return st; + } } - auto jhandler = - new JCachePgReadHandler(handler, &pStats->bytesRead, - sEnableJournalCache && !sEnableBypass ? pJournal.get() : nullptr); + auto jhandler = new JCachePgReadHandler( + handler, &pStats->bytesRead, + sEnableJournalCache && !sEnableBypass ? pJournal.get() : nullptr); pStats->readOps++; st = pFile->PgRead(offset, size, buffer, jhandler, timeout); } else { @@ -452,7 +460,9 @@ XRootDStatus JCacheFile::VectorRead(const ChunkList &chunks, void *buffer, // we have to be sure the file is opened if (pOpenHandler) { st = pOpenHandler->Wait(); - if (!st.IsOK()) {return st;} + if (!st.IsOK()) { + return st; + } } auto jhandler = new JCacheReadVHandler( @@ -482,7 +492,9 @@ XRootDStatus JCacheFile::Fcntl(const XrdCl::Buffer &arg, // we have to be sure the file is opened if (pOpenHandler) { st = pOpenHandler->Wait(); - if (!st.IsOK()) {return st;} + if (!st.IsOK()) { + return st; + } } if (pFile) { @@ -502,8 +514,10 @@ XRootDStatus JCacheFile::Visa(ResponseHandler *handler, uint16_t timeout) { // we have to be sure the file is opened if (pOpenHandler) { - st = pOpenHandler->Wait(); - if (!st.IsOK()) {return st;} + st = pOpenHandler->Wait(); + if (!st.IsOK()) { + return st; + } } if (pFile) { st = pFile->Visa(handler, timeout); @@ -537,7 +551,9 @@ bool JCacheFile::SetProperty(const std::string &name, bool JCacheFile::GetProperty(const std::string &name, std::string &value) const { if (pOpenHandler) { - if (!pOpenHandler->Wait().IsOK()) { return false;} + if (!pOpenHandler->Wait().IsOK()) { + return false; + } } if (pFile) { return pFile->GetProperty(name, value); @@ -583,15 +599,16 @@ bool JCacheFile::AttachForRead() { // only add a file if it wasn't yet added if (!sStats.HasUrl(pUrl)) { sStats.totaldatasize += sinfo->GetSize(); - sStats.opentime = sStats.opentime.load() + pOpenHandler->GetTimeToOpen(); + sStats.opentime = + sStats.opentime.load() + pOpenHandler->GetTimeToOpen(); } if (pJournal->attach(pJournalPath, sinfo->GetModTime(), 0, sinfo->GetSize())) { if (!sEnableBypass) { - // when bypass=true this might throw an error because we don't create the - // journal directory - we just don't want to see this + // when bypass=true this might throw an error because we don't + // create the journal directory - we just don't want to see this mLog->Error(1, "JCache : failed to attach to cache file: %s", - pJournalPath.c_str()); + pJournalPath.c_str()); } mAttachedForRead = true; delete sinfo; @@ -627,11 +644,8 @@ void JCacheFile::LogStats() { //---------------------------------------------------------------------------- //! @brief set stats interval in CachStats class //---------------------------------------------------------------------------- -void -JCacheFile::SetStatsInterval(uint64_t interval) -{ +void JCacheFile::SetStatsInterval(uint64_t interval) { sStats.SetInterval(interval); } - } // namespace XrdCl diff --git a/src/XrdApps/XrdClJCachePlugin/file/XrdClJCacheFile.hh b/src/XrdApps/XrdClJCachePlugin/file/XrdClJCacheFile.hh index 625243a01c1..dc183cc677b 100644 --- a/src/XrdApps/XrdClJCachePlugin/file/XrdClJCacheFile.hh +++ b/src/XrdApps/XrdClJCachePlugin/file/XrdClJCacheFile.hh @@ -32,10 +32,10 @@ #include "cleaner/Cleaner.hh" #include "file/Art.hh" #include "file/TimeBench.hh" +#include "handler/XrdClJCacheOpenHandler.hh" #include "handler/XrdClJCachePgReadHandler.hh" #include "handler/XrdClJCacheReadHandler.hh" #include "handler/XrdClJCacheReadVHandler.hh" -#include "handler/XrdClJCacheOpenHandler.hh" #include "vector/XrdClVectorCache.hh" /*----------------------------------------------------------------------------*/ #include @@ -223,7 +223,7 @@ public: static void SetJsonPath(const std::string &path) { sJsonPath = path; } static void SetSummary(const bool &value) { sEnableSummary = value; } static void SetBypass(const bool &value) { sEnableBypass = value; } - static void SetSize(uint64_t size) { sCleaner.SetSize(size,sCachePath); } + static void SetSize(uint64_t size) { sCleaner.SetSize(size, sCachePath); } static void SetAsync(bool async) { sOpenAsync = async; } //---------------------------------------------------------------------------- @@ -252,12 +252,7 @@ public: //! @brief cleaner instance static JCache::Cleaner sCleaner; - enum State { - CLOSED = 0, - OPENING, - OPEN, - FAILED - }; + enum State { CLOSED = 0, OPENING, OPEN, FAILED }; //! @brief openstate std::atomic mOpenState; @@ -274,7 +269,7 @@ private: //! @brief boolean to track if file is open bool mIsOpen; //! @brief async open handler - JCacheOpenHandler* pOpenHandler; + JCacheOpenHandler *pOpenHandler; //! @brief pointer to the remote file XrdCl::File *pFile; //! @brief boolean if file open is async diff --git a/src/XrdApps/XrdClJCachePlugin/handler/XrdClJCacheOpenHandler.cc b/src/XrdApps/XrdClJCachePlugin/handler/XrdClJCacheOpenHandler.cc index c98c830b3c9..650cd89a9e2 100644 --- a/src/XrdApps/XrdClJCachePlugin/handler/XrdClJCacheOpenHandler.cc +++ b/src/XrdApps/XrdClJCachePlugin/handler/XrdClJCacheOpenHandler.cc @@ -21,32 +21,29 @@ // 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" +#include "file/XrdClJCacheFile.hh" /*----------------------------------------------------------------------------*/ namespace XrdCl { // ---------------------------------------------------------------------- // -void -JCacheOpenHandler::HandleResponseWithHosts(XrdCl::XRootDStatus* pStatus, - XrdCl::AnyObject* pResponse, - XrdCl::HostList* pHostList) { +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) { + if (pHostList) { delete pHostList; pHostList = nullptr; } - // Response shoud be nullptr in general + // Response shoud be nullptr in general if (pResponse) { delete pResponse; pResponse = nullptr; @@ -62,8 +59,7 @@ JCacheOpenHandler::HandleResponseWithHosts(XrdCl::XRootDStatus* pStatus, cv.notify_one(); // Notify Wait() } -XRootDStatus -JCacheOpenHandler::Wait() { +XRootDStatus JCacheOpenHandler::Wait() { // quick bypass, we know we have opened if (pFile && pFile->mOpenState == JCacheFile::OPEN) return mStatus; @@ -73,6 +69,6 @@ JCacheOpenHandler::Wait() { // 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 index 1e456b4b709..0f2609b6ee7 100644 --- a/src/XrdApps/XrdClJCachePlugin/handler/XrdClJCacheOpenHandler.hh +++ b/src/XrdApps/XrdClJCachePlugin/handler/XrdClJCacheOpenHandler.hh @@ -26,11 +26,11 @@ #include "XrdCl/XrdClFile.hh" #include "XrdCl/XrdClXRootDResponses.hh" /*----------------------------------------------------------------------------*/ -#include #include +#include +#include /*----------------------------------------------------------------------------*/ - namespace XrdCl { class JCacheFile; @@ -39,25 +39,25 @@ 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(); - } + 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); + void HandleResponseWithHosts(XrdCl::XRootDStatus *pStatus, + XrdCl::AnyObject *pResponse, + XrdCl::HostList *pHostList); - XrdCl::XRootDStatus Wait(); - bool ready; + XrdCl::XRootDStatus Wait(); + bool ready; - double GetTimeToOpen() {return t2open;} + double GetTimeToOpen() { return t2open; } private: - XrdCl::JCacheFile* pFile; + XrdCl::JCacheFile *pFile; XrdCl::XRootDStatus mStatus; std::mutex mtx; std::condition_variable cv; diff --git a/src/XrdApps/XrdClJCachePlugin/handler/XrdClJCacheReadVHandler.hh b/src/XrdApps/XrdClJCachePlugin/handler/XrdClJCacheReadVHandler.hh index f377e7671ef..c625b0f859c 100644 --- a/src/XrdApps/XrdClJCachePlugin/handler/XrdClJCacheReadVHandler.hh +++ b/src/XrdApps/XrdClJCachePlugin/handler/XrdClJCacheReadVHandler.hh @@ -81,6 +81,7 @@ public: } } handler->HandleResponse(pStatus, pResponse); + delete this; } XrdCl::ResponseHandler *handler; diff --git a/src/XrdApps/XrdClJCachePlugin/plugin/XrdClJCachePlugin.hh b/src/XrdApps/XrdClJCachePlugin/plugin/XrdClJCachePlugin.hh index 4fe8f2ab899..517b8252469 100644 --- a/src/XrdApps/XrdClJCachePlugin/plugin/XrdClJCachePlugin.hh +++ b/src/XrdApps/XrdClJCachePlugin/plugin/XrdClJCachePlugin.hh @@ -51,56 +51,75 @@ public: JCacheFile::SetCache(itc != config->end() ? itc->second : ""); auto itsz = config->find("size"); - JCacheFile::SetSize(itsz != config->end() ? std::stoll(std::string(itsz->second),0,10) : 0); + JCacheFile::SetSize(itsz != config->end() + ? std::stoll(std::string(itsz->second), 0, 10) + : 0); auto itv = config->find("vector"); - JCacheFile::SetVector(itv != config->end() ? (itv->second == "true") || (itv->second == "1") + JCacheFile::SetVector(itv != config->end() ? (itv->second == "true") || + (itv->second == "1") : false); auto itj = config->find("journal"); - JCacheFile::SetJournal(itj != config->end() ? (itj->second == "true") || (itj->second == "1") + JCacheFile::SetJournal(itj != config->end() ? (itj->second == "true") || + (itj->second == "1") : true); auto ita = config->find("async"); - JCacheFile::SetAsync(ita != config->end() ? (ita->second == "true") || (ita->second == "1") - : false); + JCacheFile::SetAsync(ita != config->end() + ? (ita->second == "true") || (ita->second == "1") + : false); auto itb = config->find("bypass"); - JCacheFile::SetBypass(itb != config->end() ? (itb->second == "true") || (itb->second == "1") - : false); + JCacheFile::SetBypass(itb != config->end() ? (itb->second == "true") || + (itb->second == "1") + : false); auto itjson = config->find("json"); JCacheFile::SetJsonPath(itjson != config->end() ? itjson->second : ""); auto its = config->find("summary"); - JCacheFile::SetSummary(its != config->end() ? (its->second == "true") || (its->second == "1") + JCacheFile::SetSummary(its != config->end() ? (its->second == "true") || + (its->second == "1") : true); auto itsi = config->find("stats"); - JCacheFile::SetStatsInterval(itsi != config->end() ? std::stoll(std::string(itsi->second),0,10) : 0); - + JCacheFile::SetStatsInterval( + itsi != config->end() ? std::stoll(std::string(itsi->second), 0, 10) + : 0); if (const char *v = getenv("XRD_JCACHE_CACHE")) { JCacheFile::SetCache((std::string(v).length()) ? std::string(v) : ""); } if (const char *v = getenv("XRD_JCACHE_SIZE")) { - JCacheFile::SetSize((std::string(v).length()) ? std::stoll(std::string(v),0,10) : 0); + JCacheFile::SetSize( + (std::string(v).length()) ? std::stoll(std::string(v), 0, 10) : 0); } if (const char *v = getenv("XRD_JCACHE_SUMMARY")) { - JCacheFile::SetSummary(((std::string(v) == "true") || (std::string(v) == "1")) ? true : false); + JCacheFile::SetSummary( + ((std::string(v) == "true") || (std::string(v) == "1")) ? true + : false); } if (const char *v = getenv("XRD_JCACHE_JOURNAL")) { - JCacheFile::SetJournal(((std::string(v) == "true") || (std::string(v) == "1")) ? true : false); + JCacheFile::SetJournal( + ((std::string(v) == "true") || (std::string(v) == "1")) ? true + : false); } if (const char *v = getenv("XRD_JCACHE_VECTOR")) { - JCacheFile::SetVector(((std::string(v) == "true") || (std::string(v) == "1")) ? true : false); + JCacheFile::SetVector( + ((std::string(v) == "true") || (std::string(v) == "1")) ? true + : false); } if (const char *v = getenv("XRD_JCACHE_ASYNC")) { - JCacheFile::SetAsync(((std::string(v) == "true") || (std::string(v) == "1")) ? true : false); + JCacheFile::SetAsync( + ((std::string(v) == "true") || (std::string(v) == "1")) ? true + : false); } if (const char *v = getenv("XRD_JCACHE_BYPASS")) { - JCacheFile::SetBypass(((std::string(v) == "true") || (std::string(v) == "1")) ? true : false); + JCacheFile::SetBypass( + ((std::string(v) == "true") || (std::string(v) == "1")) ? true + : false); } if (const char *v = getenv("XRD_JCACHE_JSON")) { @@ -109,7 +128,8 @@ public: } if (const char *v = getenv("XRD_JCACHE_STATS")) { - JCacheFile::SetStatsInterval((std::string(v).length()) ? std::stoll(std::string(v),0,10) : 0); + JCacheFile::SetStatsInterval( + (std::string(v).length()) ? std::stoll(std::string(v), 0, 10) : 0); } Log *log = DefaultEnv::GetLog();