|
| 1 | +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. |
| 2 | +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. |
| 3 | +// All rights not expressly granted are reserved. |
| 4 | +// |
| 5 | +// This software is distributed under the terms of the GNU General Public |
| 6 | +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". |
| 7 | +// |
| 8 | +// In applying this license CERN does not waive the privileges and immunities |
| 9 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 10 | +// or submit itself to any jurisdiction. |
| 11 | + |
| 12 | +/// \author ruben.shahoyan@cern.ch |
| 13 | +/// Code mostly based on DataDistribution:SubTimeFrameFileSource by Gvozden Nescovic |
| 14 | + |
| 15 | +#ifndef ALICEO2_FILEFETCHER_H_ |
| 16 | +#define ALICEO2_FILEFETCHER_H_ |
| 17 | + |
| 18 | +#include "CommonUtils/FIFO.h" |
| 19 | +#include <unordered_map> |
| 20 | +#include <string> |
| 21 | +#include <thread> |
| 22 | +#include <Rtypes.h> |
| 23 | +#include <mutex> |
| 24 | +#include <regex> |
| 25 | + |
| 26 | +namespace o2 |
| 27 | +{ |
| 28 | +namespace utils |
| 29 | +{ |
| 30 | + |
| 31 | +class FileFetcher |
| 32 | +{ |
| 33 | + public: |
| 34 | + struct FileRef { |
| 35 | + std::string origName{}; |
| 36 | + std::string localName{}; // local alias for for remote files |
| 37 | + bool remote = false; |
| 38 | + bool copied = false; |
| 39 | + |
| 40 | + const auto& getLocalName() const { return remote ? localName : origName; } |
| 41 | + const auto& getOrigName() const { return origName; } |
| 42 | + }; |
| 43 | + |
| 44 | + /* |
| 45 | + * Create file fetcher with predefined cache and async copy of remote files |
| 46 | + * |
| 47 | + * @param input: comma-separated list of input data files and/or files with list of data files and/or directories |
| 48 | + * @param selRegex: regex expression to select files needed for input |
| 49 | + * @param remRegex: optional regex expression to recognize remote files |
| 50 | + * @param copyCmd: optional command to copy remote files in format "<operation> ?src ?dst" |
| 51 | + * @param copyCmd: base directory for copied remote files |
| 52 | + */ |
| 53 | + FileFetcher(const std::string& input, |
| 54 | + const std::string& selRegex = "", |
| 55 | + const std::string& remRegex = "", |
| 56 | + const std::string& copyCmd = "", |
| 57 | + const std::string& copyDir = "/tmp"); |
| 58 | + ~FileFetcher(); |
| 59 | + |
| 60 | + const auto& getFileRef(size_t i) const { return mInputFiles[i]; } |
| 61 | + |
| 62 | + void setMaxFilesInQueue(size_t s) { mMaxInQueue = s > 0 ? s : 1; } |
| 63 | + void setMaxLoops(size_t v) { mMaxLoops = v; } |
| 64 | + bool isRunning() const { return mRunning; } |
| 65 | + void start(); |
| 66 | + void stop(); |
| 67 | + void cleanup(); |
| 68 | + size_t getNLoops() const { return mNLoops; } |
| 69 | + size_t getNFilesProc() const { return mNFilesProc; } |
| 70 | + size_t getNFilesProcOK() const { return mNFilesProcOK; } |
| 71 | + size_t getMaxFilesInQueue() const { return mMaxInQueue; } |
| 72 | + size_t getNRemoteFiles() const { return mNRemote; } |
| 73 | + size_t getNFiles() const { return mInputFiles.size(); } |
| 74 | + size_t popFromQueue(bool discard = false); |
| 75 | + size_t getQueueSize() const { return mQueue.size(); } |
| 76 | + std::string getNextFileInQueue() const; |
| 77 | + void discardFile(const std::string& fname); |
| 78 | + |
| 79 | + private: |
| 80 | + size_t nextInQueue() const; |
| 81 | + void processInput(const std::string& input); |
| 82 | + void processInput(const std::vector<std::string>& input); |
| 83 | + void processDirectory(const std::string& name); |
| 84 | + bool addInputFile(const std::string& fname); |
| 85 | + std::string createCopyName(const std::string& fname) const; |
| 86 | + bool copyFile(size_t id); |
| 87 | + bool isRemote(const std::string& fname) const; |
| 88 | + void fetcher(); |
| 89 | + |
| 90 | + private: |
| 91 | + FIFO<size_t> mQueue{}; |
| 92 | + std::string mCopyDirName{"/tmp"}; |
| 93 | + std::string mCopyCmdLogFile{}; |
| 94 | + std::string mCopyCmd{}; |
| 95 | + std::unique_ptr<std::regex> mSelRegex; |
| 96 | + std::unique_ptr<std::regex> mRemRegex; |
| 97 | + std::unordered_map<std::string, size_t> mCopied{}; |
| 98 | + std::vector<FileRef> mInputFiles{}; |
| 99 | + size_t mNRemote{0}; |
| 100 | + size_t mMaxInQueue{5}; |
| 101 | + bool mRunning = false; |
| 102 | + size_t mMaxLoops = 0; |
| 103 | + size_t mNLoops = 0; |
| 104 | + size_t mNFilesProc = 0; |
| 105 | + size_t mNFilesProcOK = 0; |
| 106 | + mutable std::mutex mMtx; |
| 107 | + std::thread mFetcherThread{}; |
| 108 | + |
| 109 | + ClassDefNV(FileFetcher, 1); |
| 110 | +}; |
| 111 | + |
| 112 | +} // namespace utils |
| 113 | +} // namespace o2 |
| 114 | + |
| 115 | +#endif |
0 commit comments