21 #include <boost/lexical_cast.hpp>
22 #include <boost/filesystem/fstream.hpp>
29 struct flock make_flock(short
type, short whence, off_t
start, off_t len, pid_t
pid)
41 testModeNoBuilderUnit_(
42 pset.getUntrackedParameter<bool> (
"testModeNoBuilderUnit",
46 pset.getUntrackedParameter<std::
string> (
"baseDir",
"/data")
49 pset.getUntrackedParameter<std::
string> (
"buBaseDir",
"/data")
52 pset.getUntrackedParameter<bool> (
"directorIsBu",
false)
54 run_(pset.getUntrackedParameter<unsigned int> (
"runNumber",0)),
55 outputAdler32Recheck_(pset.getUntrackedParameter<bool>(
"outputAdler32Recheck",
false)),
56 requireTSPSet_(pset.getUntrackedParameter<bool>(
"requireTransfersPSet",
false)),
57 selectedTransferMode_(pset.getUntrackedParameter<std::
string>(
"selectedTransferMode",
"")),
58 hltSourceDirectory_(pset.getUntrackedParameter<std::
string>(
"hltSourceDirectory",
"")),
62 fu_readwritelock_fd_(-1),
63 data_readwrite_fd_(-1),
64 fulocal_rwlock_fd_(-1),
65 fulocal_rwlock_fd2_(-1),
74 dirManager_(base_dir_),
80 bu_w_flk( make_flock( F_WRLCK, SEEK_SET, 0, 0, 0 )),
81 bu_r_flk( make_flock( F_RDLCK, SEEK_SET, 0, 0, 0 )),
82 bu_w_fulk( make_flock( F_UNLCK, SEEK_SET, 0, 0, 0 )),
83 bu_r_fulk( make_flock( F_UNLCK, SEEK_SET, 0, 0, 0 )),
84 fu_rw_flk( make_flock ( F_WRLCK, SEEK_SET, 0, 0, getpid() )),
85 fu_rw_fulk( make_flock( F_UNLCK, SEEK_SET, 0, 0, getpid() )),
86 data_rw_flk( make_flock ( F_WRLCK, SEEK_SET, 0, 0, getpid() )),
87 data_rw_fulk( make_flock( F_UNLCK, SEEK_SET, 0, 0, getpid() ))
100 std::stringstream
ss;
101 ss <<
"run" << std::setfill(
'0') << std::setw(6) <<
run_;
107 gethostname(hostname,32);
110 int retval = mkdir(
base_dir_.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
111 if (retval != 0 && errno != EEXIST) {
112 throw cms::Exception(
"DaqDirector") <<
" Error checking for base dir -: "
113 <<
base_dir_ <<
" mkdir error:" << strerror(errno);
119 S_IRWXU | S_IRWXG | S_IROTH | S_IRWXO | S_IXOTH);
120 if (retval != 0 && errno != EEXIST) {
121 throw cms::Exception(
"DaqDirector") <<
" Error creating run dir -: "
122 <<
run_dir_ <<
" mkdir error:" << strerror(errno);
130 fulocal_rwlock_fd_ = open(fulocal_lock_.c_str(), O_RDWR | O_CREAT, S_IRWXU | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
132 throw cms::Exception(
"DaqDirector") <<
" Error creating/opening a local lock file -: " << fulocal_lock_.c_str() <<
" : " << strerror(errno);
133 chmod(fulocal_lock_.c_str(),0777);
136 fulocal_rwlock_fd2_ = open(fulocal_lock_.c_str(), O_RDWR, S_IRWXU | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
138 throw cms::Exception(
"DaqDirector") <<
" Error opening a local lock file -: " << fulocal_lock_.c_str() <<
" : " << strerror(errno);
151 S_IRWXU | S_IRWXG | S_IRWXO);
152 if (retval != 0 && errno != EEXIST) {
155 <<
" mkdir error:" << strerror(errno) <<
"\n";
159 S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
160 if (retval != 0 && errno != EEXIST) {
161 throw cms::Exception(
"DaqDirector") <<
" Error creating bu run open dir -: "
168 O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU);
170 edm::LogWarning(
"EvFDaqDirector") <<
"problem with creating filedesc for buwritelock -: "
173 edm::LogInfo(
"EvFDaqDirector") <<
"creating filedesc for buwritelock -: "
177 edm::LogWarning(
"EvFDaqDirector")<<
"Error creating write lock stream -: " << strerror(errno);
192 retval = mkdir(tmphltdir.c_str(),S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
202 boost::filesystem::rename(tmphltdir,hltdir);
213 retval = mkdir(
bu_base_dir_.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
214 if (retval != 0 && errno != EEXIST) {
215 throw cms::Exception(
"DaqDirector") <<
" Error checking for bu base dir -: "
216 <<
bu_base_dir_ <<
" mkdir error:" << strerror(errno) <<
"\n";
270 <<
run_dir_ <<
". This is not the highest run "
280 edm::LogWarning(
"EvFDaqDirector") <<
" Handles to check for files to delete were not set by the input source...";
287 if (it->second->lumi_ == ls) {
289 LogDebug(
"EvFDaqDirector") <<
"Deleting input file -:" << it->second->fileName_;
294 catch (
const boost::filesystem::filesystem_error& ex)
296 edm::LogError(
"EvFDaqDirector") <<
" - deleteFile BOOST FILESYSTEM ERROR CAUGHT -: " << ex.what() <<
". Trying again.";
305 edm::LogError(
"EvFDaqDirector") <<
" - deleteFile std::exception CAUGHT -: " << ex.what() <<
". Trying again.";
415 int retval =
remove(filename.c_str());
417 edm::LogError(
"EvFDaqDirector") <<
"Could not remove used file -: " << filename <<
". error = "
429 int lock_attempts = 0;
435 edm::LogWarning(
"EvFDaqDirector") <<
"Detected stop request from hltd. Ending run for this process after LS -: " << stopFileLS;
441 if (retval==-1) usleep(50000);
445 if (lock_attempts>100 || errno==116) {
447 edm::LogWarning(
"EvFDaqDirector") <<
"Stale lock file handle. Checking if run directory and fu.lock file are present" << std::endl;
449 edm::LogWarning(
"EvFDaqDirector") <<
"Unable to obtain a lock for 5 seconds. Checking if run directory and fu.lock file are present -: errno "
450 << errno <<
":"<< strerror(errno) << std::endl;
454 edm::LogWarning(
"EvFDaqDirector") <<
"Detected local EoLS for lumisection "<<
ls ;
464 if(retval!=0)
return fileStatus;
468 gettimeofday(&ts_lockend,0);
473 unsigned int readLs,
readIndex, jumpLs, jumpIndex;
482 &jumpLs, &jumpIndex);
489 bool bumpedOk =
bumpFile(readLs, readIndex, nextFile, fsize, stopFileLS);
499 edm::LogError(
"EvFDaqDirector") <<
"seek on fu read/write lock for updating failed with error "
507 readIndex + 1, readLs + 2, readIndex + 1);
520 edm::LogInfo(
"EvFDaqDirector") <<
"Written to file -: " << readLs <<
":"
521 << readIndex + 1 <<
" --> " << readLs + 2
522 <<
":" << readIndex + 1;
524 LogDebug(
"EvFDaqDirector") <<
"Written to file -: " << readLs <<
":"
528 edm::LogError(
"EvFDaqDirector") <<
"seek on fu read/write lock for updating failed with error "
532 edm::LogError(
"EvFDaqDirector") <<
"seek on fu read/write lock for reading failed with error "
535 edm::LogError(
"EvFDaqDirector") <<
"fu read/write lock stream is invalid " << strerror(errno);
538 timeval ts_preunlock;
539 gettimeofday(&ts_preunlock,0);
540 int locked_period_int = ts_preunlock.tv_sec - ts_lockend.tv_sec;
541 double locked_period=locked_period_int+double(ts_preunlock.tv_usec - ts_lockend.tv_usec)/1000000;
550 if (retvalu==-1)
edm::LogError(
"EvFDaqDirector") <<
"Error unlocking the fu.lock " << strerror(errno);
553 edm::LogDebug(
"EvFDaqDirector") <<
"Waited during lock -: " << locked_period <<
" seconds";
556 if ( fileStatus ==
noFile ) {
561 if (stopFileLS>=0 && (
int)ls > stopFileLS) {
562 edm::LogInfo(
"EvFDaqDirector") <<
"Reached maximum lumisection set by hltd";
571 boost::filesystem::ifstream ij(BUEoLSFile);
575 if (!reader.
parse(ij, deserializeRoot)) {
576 edm::LogError(
"EvFDaqDirector") <<
"Cannot deserialize input JSON file -:" << BUEoLSFile;
591 if (def.find(
'/')==0)
596 if (stat(fullpath.c_str(), &buf) == 0) {
599 DataPointDefinition::getDataPointDefinitionFor(fullpath, &eolsDpd,&defLabel);
604 DataPointDefinition::getDataPointDefinitionFor(fullpath, &eolsDpd,&defLabel);
606 for (
unsigned int i=0;
i<eolsDpd.
getNames().size();
i++)
613 if (def.size()<=1 || def.find(
'/')==std::string::npos) {
617 def = def.substr(def.find(
'/')+1);
624 edm::LogError(
"EvFDaqDirector") <<
" error reading number of files from BU JSON -: " << BUEoLSFile;
627 return boost::lexical_cast<
int>(
data);
637 edm::LogError(
"EvFDaqDirector") <<
" FastMonitoringService not found";
645 if (maxLS>=0 && ls > (
unsigned int)maxLS)
return false;
648 std::stringstream
ss;
649 unsigned int nextIndex =
index;
654 if (stat(nextFile.c_str(), &buf) == 0) {
663 bool eolFound = (stat(BUEoLSFile.c_str(), &buf) == 0);
664 unsigned int startingLumi =
ls;
668 if (stat(nextFile.c_str(), &buf) == 0) {
675 if (indexFilesInLS < 0)
680 if ((
int)index<indexFilesInLS) {
682 edm::LogError(
"EvFDaqDirector") <<
"Potential miss of index file in LS -: " << ls <<
". Missing "
683 << nextFile <<
" because " << indexFilesInLS-1 <<
" is the highest index expected. Will not update fu.lock file";
692 if (maxLS>=0 && ls > (
unsigned int)maxLS)
return false;
695 if (stat(nextFile.c_str(), &buf) == 0) {
706 std::string cpCmd =
"cp " + sourceEol +
" " + destEol;
707 edm::LogInfo(
"EvFDaqDirector") <<
" testmode: Running copy cmd -: " << cpCmd;
708 int rc = system(cpCmd.c_str());
710 edm::LogError(
"EvFDaqDirector") <<
" testmode: COPY EOL FAILED!!!!! -: " << cpCmd;
717 eolFound = (stat(BUEoLSFile.c_str(), &buf) == 0);
726 edm::LogError(
"EvFDaqDirector") <<
"Error creating fu read/write lock stream "
729 edm::LogInfo(
"EvFDaqDirector") <<
"Initializing FU LOCK FILE";
730 unsigned int readLs = 1,
readIndex = 0, jumpLs = 3, jumpIndex = 0;
742 S_IRWXU | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
743 chmod(fulockfile.c_str(),0766);
748 edm::LogError(
"EvFDaqDirector") <<
"problem with creating filedesc for fuwritelock -: " << fulockfile.c_str()
749 <<
" create:" << create <<
" error:" << strerror(errno);
751 LogDebug(
"EvFDaqDirector") <<
"creating filedesc for fureadwritelock -: "
762 edm::LogError(
"EvFDaqDirector") <<
"problem with creating filedesc for datamerge "
765 LogDebug(
"EvFDaqDirector") <<
"creating filedesc for datamerge -: "
812 if (!boost::filesystem::is_directory(openPath)) {
813 LogDebug(
"EvFDaqDirector") <<
"<open> FU dir not found. Creating... -:" << openPath.string();
814 boost::filesystem::create_directories(openPath);
821 boost::filesystem::ifstream ij(file);
825 if (!reader.
parse(ij, deserializeRoot)) {
826 edm::LogError(
"EvFDaqDirector") <<
"Cannot deserialize input JSON file -:" <<
file;
830 int ret = deserializeRoot.
get(
"lastLS",
"").
asInt();
846 (*transferSystemJson_)[
"destinations"]=destinationsVal;
849 std::vector<std::string> modes = tsPset.
getParameter< std::vector<std::string> >(
"transferModes");
851 (*transferSystemJson_)[
"transferModes"]=modesVal;
853 for (
auto psKeyItr =tsPset.
psetTable().begin();psKeyItr!=tsPset.
psetTable().end(); ++ psKeyItr) {
854 if (psKeyItr->first!=
"destinations" && psKeyItr->first!=
"transferModes") {
857 for (
auto &
mode : modes) {
859 if (!streamDef.
existsAs<std::vector<std::string>>(
mode,
true))
860 throw cms::Exception(
"EvFDaqDirector") <<
" Missing transfer system specification for -:" << psKeyItr->first <<
" (transferMode " <<
mode <<
")";
861 std::vector<std::string> streamDestinations = streamDef.
getParameter<std::vector<std::string>>(
mode);
865 if (!streamDestinations.size())
866 throw cms::Exception(
"EvFDaqDirector") <<
" Missing transter system destination(s) for -: "<< psKeyItr->first <<
", mode:" <<
mode;
868 for (
auto & sdest:streamDestinations) {
869 bool sDestValid=
false;
870 sDestsValue.append(sdest);
871 for (
auto &
dest: destinations) {
872 if (
dest==sdest) sDestValid=
true;
875 throw cms::Exception(
"EvFDaqDirector") <<
" Invalid transter system destination specified for -: "<< psKeyItr->first <<
", mode:" <<
mode <<
", dest:"<<sdest;
877 streamVal[
mode]=sDestsValue;
879 (*transferSystemJson_)[psKeyItr->first] = streamVal;
885 throw cms::Exception(
"EvFDaqDirector") <<
"transferSystem PSet not found";
893 streamRequestName =
stream;
895 std::stringstream
msg;
896 msg <<
"Transfer system mode definitions missing for -: " <<
stream;
900 edm::LogWarning(
"EvFDaqDirector") << msg.str() <<
" (permissive mode)";
906 edm::LogWarning(
"EvFDaqDirector") <<
"Selected mode string is not provided as DaqDirector parameter."
907 <<
"Switch on requireTSPSet parameter to enforce this requirement. Setting mode to empty string.";
911 throw cms::Exception(
"EvFDaqDirector") <<
"Selected mode string is not provided as DaqDirector parameter.";
916 std::stringstream
msg;
917 msg <<
"Selected transfer mode " <<
selectedTransferMode_ <<
" is not specified for stream " << streamRequestName;
921 edm::LogWarning(
"EvFDaqDirector") << msg.str() <<
" (permissive mode)";
930 if (ret!=
"") ret +=
",";
931 ret+=(*it).asString();
unsigned int maxNumberOfThreads() const
T getParameter(std::string const &) const
std::string getStreamDestinations(std::string const &stream) const
Value get(UInt index, const Value &defaultValue) const
std::vector< std::string > & getData()
std::string protocolBufferHistogramFileNameWithInstance(const unsigned int run, const unsigned int ls, std::string const &stream, std::string const &instance)
tuple start
Check for commandline option errors.
std::string bolsFileName(const unsigned int run, const unsigned int ls)
std::string streamerDataChecksumFileNameWithInstance(const unsigned int run, const unsigned int ls, std::string const &stream, std::string const &instance)
std::string getMergedProtocolBufferHistogramFilePath(const unsigned int ls, std::string const &stream) const
void watchPreallocate(Preallocate::slot_type const &iSlot)
std::vector< int > streamFileTracker_
const_iterator begin() const
bool existsAs(std::string const ¶meterName, bool trackiness=true) const
checks if a parameter exists as a given type
std::list< std::pair< int, InputFile * > > * filesToDeletePtr_
std::string getMergedDatChecksumFilePath(const unsigned int ls, std::string const &stream) const
std::shared_ptr< Json::Value > transferSystemJson_
void openFULockfileStream(std::string &fuLockFilePath, bool create)
void accumulateFileSize(unsigned int lumi, unsigned long fileSize)
void watchPreGlobalEndLumi(PreGlobalEndLumi::slot_type const &iSlot)
std::string getInitFilePath(std::string const &stream) const
std::string getMergedRootHistogramFilePath(const unsigned int ls, std::string const &stream) const
std::string inputRawFileName(const unsigned int run, const unsigned int ls, const unsigned int index)
ParameterSet const & getParameterSet(ParameterSetID const &id)
pthread_mutex_t init_lock_
LuminosityBlockID const & luminosityBlockID() const
std::string getProtocolBufferHistogramFilePath(const unsigned int ls, std::string const &stream) const
std::string getRawFilePath(const unsigned int ls, const unsigned int index) const
std::string getOpenInitFilePath(std::string const &stream) const
std::string getOpenRawFilePath(const unsigned int ls, const unsigned int index) const
Value & append(const Value &value)
Append value to array at the end.
bool parse(const std::string &document, Value &root, bool collectComments=true)
Read a Value from a JSON document.
std::string getEoLSFilePathOnBU(const unsigned int ls) const
std::string getEoRFilePath() const
unsigned long previousFileSize_
void createRunOpendirMaybe()
FileStatus updateFuLock(unsigned int &ls, std::string &nextFile, uint32_t &fsize)
unsigned int maxNumberOfStreams() const
std::string hltSourceDirectory_
std::string getOpenProtocolBufferHistogramFilePath(const unsigned int ls, std::string const &stream) const
std::mutex * fileDeleteLockPtr_
std::string streamerDataFileNameWithInstance(const unsigned int run, const unsigned int ls, std::string const &stream, std::string const &instance)
tuple path
else: Piece not in the list, fine.
EvFDaqDirector(const edm::ParameterSet &pset, edm::ActivityRegistry ®)
std::string getMergedDatFilePath(const unsigned int ls, std::string const &stream) const
bool check(const std::string &)
std::string getOpenOutputJsonFilePath(const unsigned int ls, std::string const &stream) const
std::string getOpenRootHistogramFilePath(const unsigned int ls, std::string const &stream) const
void checkTransferSystemPSet()
std::string getRootHistogramFilePath(const unsigned int ls, std::string const &stream) const
std::string getOpenDatFilePath(const unsigned int ls, std::string const &stream) const
std::string inputJsonFileName(const unsigned int run, const unsigned int ls, const unsigned int index)
std::string stopFilePath_
FILE * maybeCreateAndLockFileHeadForStream(unsigned int ls, std::string &stream)
void unlockAndCloseMergeStream()
void tryInitializeFuLockFile()
void removeFile(unsigned int ls, unsigned int index)
std::string selectedTransferMode_
std::string streamerJsonFileNameWithPid(const unsigned int run, const unsigned int ls, std::string const &stream)
std::string getOpenInputJsonFilePath(const unsigned int ls, const unsigned int index) const
std::string eorFileName(const unsigned int run)
int readLastLSEntry(std::string const &file)
virtual void deserialize(Json::Value &root)
unsigned int eolsNFilesIndex_
void watchPreGlobalBeginRun(PreGlobalBeginRun::slot_type const &iSlot)
std::string getOutputJsonFilePath(const unsigned int ls, std::string const &stream) const
int getNFilesFromEoLS(std::string BUEoLSFile)
void preBeginRun(edm::GlobalContext const &globalContext)
std::string rootHistogramFileNameWithPid(const unsigned int run, const unsigned int ls, std::string const &stream)
psettable const & psetTable() const
void preSourceEvent(edm::StreamID const &streamID)
std::string initFileNameWithPid(const unsigned int run, const unsigned int ls, std::string const &stream)
ParameterSet const & getProcessParameterSet()
ParameterSet const & getParameterSet(std::string const &) const
void preGlobalEndLumi(edm::GlobalContext const &globalContext)
bool testModeNoBuilderUnit_
void watchPostGlobalEndRun(PostGlobalEndRun::slot_type const &iSlot)
LuminosityBlockNumber_t luminosityBlock() const
std::string & getDefinition()
evf::FastMonitoringService * fms_
std::string rootHistogramFileNameWithInstance(const unsigned int run, const unsigned int ls, std::string const &stream, std::string const &instance)
std::string findHighestRunDir()
std::string getBoLSFilePathOnFU(const unsigned int ls) const
char data[epos_bytes_allocation]
void postEndRun(edm::GlobalContext const &globalContext)
Unserialize a JSON document into a Value.
const_iterator end() const
std::vector< std::string > const & getNames()
std::string getEoLSFilePathOnFU(const unsigned int ls) const
void watchPreSourceEvent(PreSourceEvent::slot_type const &iSlot)
bool bumpFile(unsigned int &ls, unsigned int &index, std::string &nextFile, uint32_t &fsize, int maxLS)
volatile std::atomic< bool > shutdown_flag false
void preallocate(edm::service::SystemBounds const &bounds)
struct flock data_rw_fulk
Iterator for object and array value.
std::string getInputJsonFilePath(const unsigned int ls, const unsigned int index) const
std::string getRunOpenDirPath() const
std::string protocolBufferHistogramFileNameWithPid(const unsigned int run, const unsigned int ls, std::string const &stream)
std::string eolsFileName(const unsigned int run, const unsigned int ls)
std::string streamerDataFileNameWithPid(const unsigned int run, const unsigned int ls, std::string const &stream)
std::string getEoRFilePathOnFU() const
std::string bu_run_open_dir_
array value (ordered list)