Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit 88d3ad0

Browse files
committed
FileFetcher class for async. queuing of local and remote files
1 parent e42a846 commit 88d3ad0

5 files changed

Lines changed: 412 additions & 1 deletion

File tree

Common/Utils/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ o2_add_library(CommonUtils
1818
src/KeyValParam.cxx
1919
src/FileSystemUtils.cxx
2020
src/FIFO.cxx
21+
src/FileFetcher.cxx
2122
PUBLIC_LINK_LIBRARIES ROOT::Hist ROOT::Tree Boost::iostreams O2::CommonDataFormat O2::Headers
2223
FairLogger::FairLogger)
2324

@@ -36,6 +37,7 @@ o2_target_root_dictionary(CommonUtils
3637
include/CommonUtils/ConfigurationMacroHelper.h
3738
include/CommonUtils/RootSerializableKeyValueStore.h
3839
include/CommonUtils/KeyValParam.h
40+
include/CommonUtils/FileFetcher.h
3941
)
4042

4143
o2_add_test(TreeStream
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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

Common/Utils/include/CommonUtils/StringUtils.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,14 @@ struct Str {
9393
{
9494
std::string ss = s;
9595
rtrim(ss);
96-
ltrim(ss);
9796
return ss;
9897
}
9998

99+
static inline bool endsWith(const std::string& s, const std::string& ending)
100+
{
101+
return (ending.size() > s.size()) ? false : std::equal(ending.rbegin(), ending.rend(), s.rbegin());
102+
}
103+
100104
// return vector of tokens from the string with provided delimiter. If requested, trim the spaces from tokens
101105
static std::vector<std::string> tokenize(const std::string& src, char delim, bool trimToken = true);
102106

Common/Utils/src/CommonUtilsLinkDef.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#pragma link C++ class o2::utils::MemFileHelper + ;
2323
#pragma link C++ class o2::utils::RootSerializableKeyValueStore::SerializedInfo + ;
2424
#pragma link C++ class o2::utils::Str + ;
25+
#pragma link C++ class o2::utils::FileFetcher + ;
26+
2527
#pragma link C++ class pair < string, o2::utils::RootSerializableKeyValueStore::SerializedInfo> + ;
2628
#pragma link C++ class map < string, o2::utils::RootSerializableKeyValueStore::SerializedInfo> + ;
2729
#pragma link C++ class o2::utils::RootSerializableKeyValueStore + ;

0 commit comments

Comments
 (0)