9 #include <framework/database/TestingPayloadStorage.h>
10 #include <framework/dataobjects/EventMetaData.h>
11 #include <framework/logging/Logger.h>
12 #include <framework/utilities/FileSystem.h>
13 #include <framework/utilities/ScopeGuard.h>
15 #include <TDirectory.h>
19 #include <boost/filesystem.hpp>
20 #include <boost/algorithm/string.hpp>
22 namespace fs = boost::filesystem;
24 namespace Belle2::Conditions {
26 m_filename{filename}, m_absoluteFilename{fs::absolute(m_filename).string()},
27 m_payloadDir{fs::path(m_absoluteFilename).parent_path().string()}
40 for (
const auto& [revision, iov] : it->second) {
41 if (iov.contains(event)) {
49 B2FATAL(
"Could not find payload file specified in testing payload storage" <<
LogVar(
"storage filen",
m_filename)
50 <<
LogVar(
"name", info.name) <<
LogVar(
"local revision", info.revision)
51 <<
LogVar(
"payload filename", info.filename));
59 B2WARNING(
"Conditions: Temporary testing payload used for processing" <<
LogVar(
"storage file",
m_filename)
60 <<
LogVar(
"name", info.name) <<
LogVar(
"revision", info.revision) <<
LogVar(
"iov", info.iov));
70 B2WARNING(
"Given testing payload storage file doesn't exist or is not a regular file" <<
LogVar(
"storage file",
m_filename));
75 if (!file.is_open()) {
76 B2FATAL(
"Opening of testing payload storage file failed" <<
LogVar(
"storage file",
m_filename) <<
LogVar(
"error", strerror(errno)));
83 std::getline(file, line);
86 size_t commentChar = line.find(
'#');
87 if (commentChar != std::string::npos) {
88 line = line.substr(0, commentChar);
91 boost::algorithm::trim(line);
93 if (line.empty())
continue;
99 std::stringstream(line) >> name >> revision >> iov;
100 }
catch (std::runtime_error& e) {
101 throw std::runtime_error(
"line must be of the form 'dbstore/<payloadname> <revision> <firstExp>,<firstRun>,<finalExp>,<finalRun>'");
104 size_t pos = name.find(
'/');
105 if (pos == std::string::npos) {
106 throw std::runtime_error(
"payload name must be of the form dbstore/<payloadname>");
108 std::string module = name.substr(pos + 1, name.length());
111 <<
LogVar(
"revision/md5", revision) <<
LogVar(
"iov", iov));
112 m_payloads[module].emplace_back(revision, iov);
114 }
catch (std::exception& e) {
115 B2FATAL(
"Problem reading testing payloads storage" <<
LogVar(
"storage file",
m_filename)
116 <<
LogVar(
"line", lineno) <<
LogVar(
"error", e.what()));
120 std::reverse(payloads.begin(), payloads.end());
125 const std::string& revision)
127 std::stringstream result;
128 if (!path.empty()) result << path <<
'/';
129 result <<
"dbstore_" << name <<
"_rev_" << revision <<
".root";
135 return store(name, iov,
"", [
this, &
object, &name](
const std::string & filename) {
143 fs::path resolved = fs::canonical(fileName);
144 if (not fs::is_regular_file(resolved)) {
145 B2ERROR(
"Problem creating testing payload: Given payload storage file doesn't exist" <<
LogVar(
"storage file", fileName));
148 return store(name, iov, resolved.string(), [&resolved](
const std::string & destination) {
150 fs::copy_file(resolved, destination, fs::copy_option::overwrite_if_exists);
156 const std::string& source,
const std::function<
bool(
const std::string&)>& writer)
159 B2ERROR(
"IoV is empty, refusing to store object in testing payload storage"
160 "Please provide a valid experiment/run range for the data, for example "
161 "using IntervalOfValidity::always() to store data which is always valid"
171 fs::path sourcefile{source};
172 if (source.empty()) {
174 sourcefile = fs::path(
m_payloadDir) / fs::unique_path();
175 auto fd = open(sourcefile.c_str(), O_CREAT | O_EXCL);
180 if (errno != EEXIST && errno != EINTR) {
181 B2ERROR(
"Cannot create payload file:" << strerror(errno));
184 B2DEBUG(35,
"first try to create tempfile failed, trying again");
186 if (!writer(sourcefile.string()))
return false;
192 ScopeGuard delete_srcfile([&sourcefile] {fs::remove(sourcefile);});
193 if (!source.empty()) delete_srcfile.
release();
201 B2ERROR(
"Locking of testing payload storage file failed, cannot create payload"
206 if (!file.is_open()) {
207 B2ERROR(
"Could not open testing payload storage file for writing" <<
LogVar(
"storage file",
m_filename));
212 std::string revision;
214 for (
int i = 6; i <= 32; ++i) {
215 revision = md5.substr(0, i);
220 if (source.empty()) {
221 fs::rename(sourcefile, filename);
222 delete_srcfile.release();
224 fs::copy_file(source, filename, fs::copy_option::overwrite_if_exists);
231 B2ERROR(
"Cannot create payload file: checksum mistmatch for existing files");
235 file <<
"dbstore/" << name <<
" " << revision <<
" " << iov << std::endl;
237 <<
LogVar(
"local revision", revision) <<
LogVar(
"iov", iov));
246 TDirectory::TContext saveDir;
248 std::unique_ptr<TFile> file{TFile::Open((fileName +
"?reproducible=PayloadFile").c_str(),
"RECREATE")};
249 if (!file || !file->IsOpen()) {
250 B2ERROR(
"Could not open payload file for writing." <<
LogVar(
"filename",
m_filename));
254 object->Write(name.c_str(), TObject::kSingleKey);
bool storePayload(const std::string &name, const std::string &fileName, const IntervalOfValidity &iov)
Store an existing file as payload with given name and interval of validity.
bool m_initialized
Remember whether we read the file already.
std::string m_payloadDir
Directory containing the storage file as absolute file name.
bool writePayload(const std::string &fileName, const std::string &name, const TObject *object)
Write a payload file from the given object and name.
static std::string payloadFilename(const std::string &path, const std::string &name, const std::string &revision)
Build the filename for a new payload with a given name and revision in a directory.
std::unordered_map< std::string, std::vector< std::tuple< std::string, IntervalOfValidity > > > m_payloads
Map of known payloads to a list of known revisions and their interval of validity.
void read()
Read the given storage file, done lazily on first access to get() after construction or call to reset...
bool storeData(const std::string &name, TObject *object, const IntervalOfValidity &iov)
Store a TObject instance as a payload with given name and interval of validity.
bool store(const std::string &name, const IntervalOfValidity &iov, const std::string &source, const std::function< bool(const std::string &)> &writer)
Try to store a new payload with the given name and interval of validity.
bool get(const EventMetaData &event, PayloadMetadata &info)
Try to fill the PayloadMetaData for the given EventMetaData, return true on success,...
std::string m_filename
Storage file where to look for payloads.
std::string m_absoluteFilename
Storage file where to look for payloads converted to an absolute path to be robust against directory ...
TestingPayloadStorage(const std::string &filename)
Create a new instance to work on a given filename.
Helper class for locking a file.
bool lock(int timeout=300, bool ignoreErrors=false)
Try to lock the file.
static std::string calculateMD5(const std::string &filename)
Calculate the MD5 checksum of a given file.
static bool fileExists(const std::string &filename)
Check if the file with given filename exists.
A class that describes the interval of experiments/runs for which an object in the database is valid.
Simple ScopeGuard to execute a function at the end of the object lifetime.
void release()
Release the guard without calling the cleanup function.
Class to store variables with their name which were sent to the logging service.