25 #include <boost/lexical_cast.hpp>
26 #include <boost/filesystem/fstream.hpp>
27 #include <boost/algorithm/string.hpp>
38 const std::vector<std::string> EvFDaqDirector::MergeTypeNames_ = {
"",
"DAT",
"PB",
"JSNDATA"};
41 : base_dir_(
pset.getUntrackedParameter<
std::
string>(
"baseDir")),
42 bu_base_dir_(
pset.getUntrackedParameter<
std::
string>(
"buBaseDir")),
43 run_(
pset.getUntrackedParameter<unsigned
int>(
"runNumber")),
44 useFileBroker_(
pset.getUntrackedParameter<
bool>(
"useFileBroker")),
45 fileBrokerHostFromCfg_(
pset.getUntrackedParameter<
bool>(
"fileBrokerHostFromCfg",
true)),
46 fileBrokerHost_(
pset.getUntrackedParameter<
std::
string>(
"fileBrokerHost",
"InValid")),
47 fileBrokerPort_(
pset.getUntrackedParameter<
std::
string>(
"fileBrokerPort",
"8080")),
48 fileBrokerKeepAlive_(
pset.getUntrackedParameter<
bool>(
"fileBrokerKeepAlive",
true)),
49 fileBrokerUseLocalLock_(
pset.getUntrackedParameter<
bool>(
"fileBrokerUseLocalLock",
true)),
50 fuLockPollInterval_(
pset.getUntrackedParameter<unsigned
int>(
"fuLockPollInterval", 2000)),
51 outputAdler32Recheck_(
pset.getUntrackedParameter<
bool>(
"outputAdler32Recheck",
false)),
52 requireTSPSet_(
pset.getUntrackedParameter<
bool>(
"requireTransfersPSet",
false)),
53 selectedTransferMode_(
pset.getUntrackedParameter<
std::
string>(
"selectedTransferMode",
"")),
54 mergeTypePset_(
pset.getUntrackedParameter<
std::
string>(
"mergingPset",
"")),
55 directorBU_(
pset.getUntrackedParameter<
bool>(
"directorIsBU",
false)),
56 hltSourceDirectory_(
pset.getUntrackedParameter<
std::
string>(
"hltSourceDirectory",
"")),
60 fu_readwritelock_fd_(-1),
61 fulocal_rwlock_fd_(-1),
62 fulocal_rwlock_fd2_(-1),
63 bu_w_lock_stream(nullptr),
64 bu_r_lock_stream(nullptr),
65 fu_rw_lock_stream(nullptr),
66 dirManager_(base_dir_),
68 bu_w_flk(make_flock(F_WRLCK, SEEK_SET, 0, 0, 0)),
69 bu_r_flk(make_flock(F_RDLCK, SEEK_SET, 0, 0, 0)),
70 bu_w_fulk(make_flock(F_UNLCK, SEEK_SET, 0, 0, 0)),
71 bu_r_fulk(make_flock(F_UNLCK, SEEK_SET, 0, 0, 0)),
72 fu_rw_flk(make_flock(F_WRLCK, SEEK_SET, 0, 0, getpid())),
73 fu_rw_fulk(make_flock(F_UNLCK, SEEK_SET, 0, 0, getpid())) {
82 gethostname(hostname, 32);
85 char* fuLockPollIntervalPtr = std::getenv(
"FFF_LOCKPOLLINTERVAL");
86 if (fuLockPollIntervalPtr) {
91 }
catch (boost::bad_lexical_cast
const&) {
97 char* fileBrokerParamPtr = std::getenv(
"FFF_USEFILEBROKER");
98 if (fileBrokerParamPtr) {
102 }
catch (boost::bad_lexical_cast
const&) {
111 if (
stat(
"/etc/appliance/bus.config", &
buf) == 0) {
116 throw cms::Exception(
"EvFDaqDirector") <<
"No file service or BU data address information";
119 <<
"fileBrokerHostFromCfg must be set to true if fileBrokerHost parameter is not valid or empty";
127 char* startFromLSPtr = std::getenv(
"FFF_STARTFROMLS");
128 if (startFromLSPtr) {
132 }
catch (boost::bad_lexical_cast
const&) {
138 char* fileBrokerUseLockParamPtr = std::getenv(
"FFF_FILEBROKERUSELOCALLOCK");
139 if (fileBrokerUseLockParamPtr) {
142 edm::LogInfo(
"EvFDaqDirector") <<
"Setting fileBrokerUseLocalLock parameter by environment string: "
144 }
catch (boost::bad_lexical_cast
const&) {
151 std::stringstream
ss;
152 ss <<
"run" << std::setfill(
'0') << std::setw(6) <<
run_;
154 ss = std::stringstream();
158 ss = std::stringstream();
163 int retval =
mkdir(
base_dir_.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
164 if (retval != 0 && errno != EEXIST) {
166 <<
" Error checking for base dir -: " <<
base_dir_ <<
" mkdir error:" << strerror(errno);
171 retval =
mkdir(
run_dir_.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IRWXO | S_IXOTH);
172 if (retval != 0 && errno != EEXIST) {
174 <<
" Error creating run dir -: " <<
run_dir_ <<
" mkdir error:" << strerror(errno);
182 open(fulocal_lock_.c_str(), O_RDWR | O_CREAT, S_IRWXU | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
185 <<
" Error creating/opening a local lock file -: " << fulocal_lock_.c_str() <<
" : " << strerror(errno);
186 chmod(fulocal_lock_.c_str(), 0777);
190 open(fulocal_lock_.c_str(), O_RDWR, S_IRWXU | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
193 <<
" Error opening a local lock file -: " << fulocal_lock_.c_str() <<
" : " << strerror(errno);
205 if (retval != 0 && errno != EEXIST) {
207 <<
" Error creating bu run dir -: " <<
bu_run_dir_ <<
" mkdir error:" << strerror(errno) <<
"\n";
211 if (retval != 0 && errno != EEXIST) {
213 <<
" Error creating bu run open dir -: " <<
bu_run_open_dir_ <<
" mkdir error:" << strerror(errno) <<
"\n";
217 bu_writelock_fd_ = open(bulockfile.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU);
219 edm::LogWarning(
"EvFDaqDirector") <<
"problem with creating filedesc for buwritelock -: " << strerror(errno);
224 edm::LogWarning(
"EvFDaqDirector") <<
"Error creating write lock stream -: " << strerror(errno);
238 retval =
mkdir(tmphltdir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
239 if (retval != 0 && errno != EEXIST)
241 <<
" Error creating bu run dir -: " << hltdir <<
" mkdir error:" << strerror(errno) <<
"\n";
243 boost::filesystem::copy_file(
hltSourceDirectory_ +
"/HltConfig.py", tmphltdir +
"/HltConfig.py");
245 boost::filesystem::copy_file(
hltSourceDirectory_ +
"/fffParameters.jsn", tmphltdir +
"/fffParameters.jsn");
247 boost::filesystem::rename(tmphltdir, hltdir);
256 if (retval != 0 && errno != EEXIST) {
258 <<
" Error checking for bu base dir -: " <<
bu_base_dir_ <<
" mkdir error:" << strerror(errno) <<
"\n";
269 std::stringstream sstp;
276 if (!
stat(defPath.c_str(), &statbuf))
277 edm::LogInfo(
"EvFDaqDirector") <<
"found JSD file in ramdisk -: " << defPath;
280 std::string defPathSuffix =
"src/EventFilter/Utilities/plugins/budef.jsd";
281 defPath =
std::string(std::getenv(
"CMSSW_BASE")) +
"/" + defPathSuffix;
282 if (
stat(defPath.c_str(), &statbuf)) {
283 defPath =
std::string(std::getenv(
"CMSSW_RELEASE_BASE")) +
"/" + defPathSuffix;
284 if (
stat(defPath.c_str(), &statbuf)) {
285 defPath = defPathSuffix;
291 DataPointDefinition::getDataPointDefinitionFor(defPath,
dpd_, &defLabel);
298 boost::system::error_code ec;
299 socket_->shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
324 "Service used for file locking arbitration and for propagating information between other EvF components");
327 desc.
addUntracked<
unsigned int>(
"runNumber", 0)->setComment(
"Run Number in ramdisk to open");
329 ->setComment(
"Use BU file service to grab input data instead of NFS file locking");
331 ->setComment(
"Allow service to discover BU address from hltd configuration");
335 ->setComment(
"Use keep alive to avoid using large number of sockets");
337 ->setComment(
"Use local lock file to synchronize appearance of index and EoLS file markers for hltd");
338 desc.
addUntracked<
unsigned int>(
"fuLockPollInterval", 2000)
339 ->setComment(
"Lock polling interval in microseconds for the input directory file lock");
341 ->setComment(
"Check Adler32 of per-process output files while micro-merging");
343 ->setComment(
"Require complete transferSystem PSet in the process configuration");
345 ->setComment(
"Selected transfer mode (choice in Lvl0 propagated as Python parameter");
346 desc.
addUntracked<
bool>(
"directorIsBU",
false)->setComment(
"BU director mode used for testing");
349 ->setComment(
"Name of merging PSet to look for merging type definitions for streams");
350 descriptions.
add(
"EvFDaqDirector", desc);
381 edm::LogWarning(
"EvFDaqDirector") <<
" Handles to check for files to delete were not set by the input source...";
388 if (it->second->lumi_ ==
ls) {
492 <<
". error = " << strerror(errno);
500 uint16_t& rawHeaderSize,
502 bool& setExceptionState) {
507 int lock_attempts = 0;
508 long total_lock_attempts = 0;
514 if (stopFileCheck == 0 || stopFilePidCheck == 0) {
515 if (stopFileCheck == 0)
521 if (stopFileLS >= 0 && (
int)
ls >= stopFileLS)
525 edm::LogWarning(
"EvFDaqDirector") <<
"Detected stop request from hltd. Ending run for this process after LS -: "
531 timeval ts_lockbegin;
532 gettimeofday(&ts_lockbegin,
nullptr);
534 while (retval == -1) {
543 if (lock_attempts > 5000000 || errno == 116) {
546 <<
"Stale lock file handle. Checking if run directory and fu.lock file are present" << std::endl;
548 edm::LogWarning(
"EvFDaqDirector") <<
"Unable to obtain a lock for 5 seconds. Checking if run directory and "
549 "fu.lock file are present -: errno "
550 << errno <<
":" << strerror(errno) << std::endl;
553 edm::LogWarning(
"EvFDaqDirector") <<
"Detected local EoLS for lumisection " <<
ls;
565 if (total_lock_attempts > 5 * 60000000) {
566 edm::LogError(
"EvFDaqDirector") <<
"Unable to obtain a lock for 5 minutes. Stopping polling activity.";
572 gettimeofday(&ts_lockend,
nullptr);
573 long deltat = (ts_lockend.tv_usec - ts_lockbegin.tv_usec) + (ts_lockend.tv_sec - ts_lockbegin.tv_sec) * 1000000;
575 lockWaitTime = deltat;
582 gettimeofday(&ts_lockend, 0);
586 int fu_readwritelock_fd2 = open(
fulockfile_.c_str(), O_RDWR, S_IRWXU);
587 if (fu_readwritelock_fd2 == -1)
589 <<
" create. error:" << strerror(errno);
591 FILE* fu_rw_lock_stream2 = fdopen(fu_readwritelock_fd2,
"r+");
594 if (fu_rw_lock_stream2 !=
nullptr) {
598 check = fseek(fu_rw_lock_stream2, 0, SEEK_SET);
602 fscanf(fu_rw_lock_stream2,
"%u %u", &readLs, &
readIndex);
605 unsigned int currentLs = readLs;
606 bool bumpedOk =
false;
609 if (
ls &&
ls + 1 < currentLs)
613 bumpedOk =
bumpFile(readLs,
readIndex, nextFile, fsize, rawHeaderSize, stopFileLS, setExceptionState);
615 if (
ls && readLs > currentLs && currentLs >
ls) {
617 readLs = currentLs =
ls;
622 if (
ls == 0 && readLs > currentLs) {
637 check = fseek(fu_rw_lock_stream2, 0, SEEK_SET);
639 ftruncate(fu_readwritelock_fd2, 0);
641 fprintf(fu_rw_lock_stream2,
"%u %u", readLs,
readIndex + 1);
642 fflush(fu_rw_lock_stream2);
643 fsync(fu_readwritelock_fd2);
645 LogDebug(
"EvFDaqDirector") <<
"Written to file -: " << readLs <<
":" <<
readIndex + 1;
648 <<
"seek on fu read/write lock for updating failed with error " << strerror(errno);
649 setExceptionState =
true;
652 }
else if (currentLs < readLs) {
654 check = fseek(fu_rw_lock_stream2, 0, SEEK_SET);
656 ftruncate(fu_readwritelock_fd2, 0);
658 fprintf(fu_rw_lock_stream2,
"%u %u", readLs,
readIndex);
659 fflush(fu_rw_lock_stream2);
660 fsync(fu_readwritelock_fd2);
661 LogDebug(
"EvFDaqDirector") <<
"Written to file -: " << readLs <<
":" <<
readIndex;
664 <<
"seek on fu read/write lock for updating failed with error " << strerror(errno);
665 setExceptionState =
true;
670 edm::LogError(
"EvFDaqDirector") <<
"seek on fu read/write lock for reading failed with error "
674 edm::LogError(
"EvFDaqDirector") <<
"fu read/write lock stream is invalid " << strerror(errno);
676 fclose(fu_rw_lock_stream2);
679 timeval ts_preunlock;
680 gettimeofday(&ts_preunlock, 0);
681 int locked_period_int = ts_preunlock.tv_sec - ts_lockend.tv_sec;
682 double locked_period = locked_period_int + double(ts_preunlock.tv_usec - ts_lockend.tv_usec) / 1000000;
693 edm::LogError(
"EvFDaqDirector") <<
"Error unlocking the fu.lock " << strerror(errno);
696 edm::LogDebug(
"EvFDaqDirector") <<
"Waited during lock -: " << locked_period <<
" seconds";
699 if (fileStatus ==
noFile) {
704 if (stopFileLS >= 0 && (
int)
ls > stopFileLS) {
705 edm::LogInfo(
"EvFDaqDirector") <<
"Reached maximum lumisection set by hltd";
713 boost::filesystem::ifstream ij(BUEoLSFile);
717 if (!
reader.parse(ij, deserializeRoot)) {
718 edm::LogError(
"EvFDaqDirector") <<
"Cannot deserialize input JSON file -:" << BUEoLSFile;
724 dp.deserialize(deserializeRoot);
732 while (!
def.empty()) {
734 if (
def.find(
'/') == 0)
742 DataPointDefinition::getDataPointDefinitionFor(
fullpath, &eolsDpd, &defLabel);
747 DataPointDefinition::getDataPointDefinitionFor(
fullpath, &eolsDpd, &defLabel);
749 for (
unsigned int i = 0;
i < eolsDpd.
getNames().size();
i++)
750 if (eolsDpd.
getNames().at(
i) ==
"NFiles")
756 if (
def.size() <= 1 ||
def.find(
'/') == std::string::npos) {
767 edm::LogError(
"EvFDaqDirector") <<
" error reading number of files from BU JSON -: " << BUEoLSFile;
770 return boost::lexical_cast<int>(
data);
777 uint16_t& rawHeaderSize,
779 bool& setExceptionState) {
791 if (maxLS >= 0 &&
ls > (
unsigned int)maxLS)
795 std::stringstream
ss;
796 unsigned int nextIndex =
index;
801 if (
stat(nextFileJson.c_str(), &
buf) == 0) {
803 nextFile = nextFileJson;
812 nextFile = nextFileRaw;
818 if (
stat(BUEoLSFile.c_str(), &
buf) == 0) {
820 if (
stat(nextFileJson.c_str(), &
buf) == 0) {
822 nextFile = nextFileJson;
827 nextFile = nextFileRaw;
832 if (indexFilesInLS < 0)
837 if ((
int)
index < indexFilesInLS) {
840 <<
"Potential miss of index file in LS -: " <<
ls <<
". Missing " << nextFile <<
" because "
841 << indexFilesInLS - 1 <<
" is the highest index expected. Will not update fu.lock file";
842 setExceptionState =
true;
851 if (maxLS >= 0 &&
ls > (
unsigned int)maxLS)
856 if (
stat(nextFileJson.c_str(), &
buf) == 0) {
859 nextFile = nextFileJson;
864 nextFile = nextFileRaw;
876 edm::LogError(
"EvFDaqDirector") <<
"Error creating fu read/write lock stream " << strerror(errno);
878 edm::LogInfo(
"EvFDaqDirector") <<
"Initializing FU LOCK FILE";
887 open(
fulockfile_.c_str(), O_RDWR | O_CREAT, S_IRWXU | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
894 <<
" create:" <<
create <<
" error:" << strerror(errno);
900 edm::LogError(
"EvFDaqDirector") <<
"problem with opening fuwritelock file stream -: " << strerror(errno);
931 if (checkIfExists ==
false ||
stat(fuBoLS.c_str(), &
buf) != 0) {
932 int bol_fd = open(fuBoLS.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
938 const uint32_t currentLumiSection,
940 if (currentLumiSection > 0) {
945 int eol_fd = open(fuEoLS.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
950 }
else if (doCreateBoLS) {
957 uint16_t& rawHeaderSize,
958 uint32_t& lsFromHeader,
959 int32_t& eventsFromHeader,
960 int64_t& fileSizeFromHeader,
966 if ((
infile = ::open(rawSourcePath.c_str(), O_RDONLY)) < 0) {
969 <<
"parseFRDFileHeader - failed to open input file -: " << rawSourcePath <<
" : " << strerror(errno);
980 if ((
infile = ::open(rawSourcePath.c_str(), O_RDONLY)) < 0) {
982 <<
"parseFRDFileHeader - failed to open input file -: " << rawSourcePath <<
" : " << strerror(errno);
994 ssize_t sz_read =
::read(
infile, (
char*)&fileHead, buf_sz);
1001 edm::LogError(
"EvFDaqDirector") <<
"parseFRDFileHeader - unable to read " << rawSourcePath <<
" : "
1007 if ((
size_t)sz_read < buf_sz) {
1008 edm::LogError(
"EvFDaqDirector") <<
"parseFRDFileHeader - file smaller than header: " << rawSourcePath;
1016 if (frd_version == 0) {
1018 if (requireHeader) {
1019 edm::LogError(
"EvFDaqDirector") <<
"no header or invalid version string found in:" << rawSourcePath;
1025 lseek(
infile, 0, SEEK_SET);
1028 eventsFromHeader = -1;
1029 fileSizeFromHeader = -1;
1033 uint32_t headerSizeRaw = fileHead.headerSize_;
1034 if (headerSizeRaw < buf_sz) {
1035 edm::LogError(
"EvFDaqDirector") <<
"inconsistent header size: " << rawSourcePath <<
" size: " << headerSizeRaw
1036 <<
" v:" << frd_version;
1042 lsFromHeader = fileHead.lumiSection_;
1043 eventsFromHeader = (int32_t)fileHead.eventCount_;
1044 fileSizeFromHeader = (int64_t)fileHead.fileSize_;
1045 rawHeaderSize = fileHead.headerSize_;
1053 if ((
infile = ::open(rawSourcePath.c_str(), O_RDONLY)) < 0) {
1054 edm::LogWarning(
"EvFDaqDirector") <<
"rawFileHasHeader - failed to open input file -: " << rawSourcePath <<
" : "
1061 ssize_t sz_read =
::read(
infile, (
char*)&fileHead, buf_sz);
1064 edm::LogError(
"EvFDaqDirector") <<
"rawFileHasHeader - unable to read " << rawSourcePath <<
" : "
1070 if ((
size_t)sz_read < buf_sz) {
1071 edm::LogError(
"EvFDaqDirector") <<
"rawFileHasHeader - file smaller than header: " << rawSourcePath;
1081 if (frd_version > 0) {
1082 rawHeaderSize = fileHead.headerSize_;
1092 uint16_t& rawHeaderSize,
1093 int64_t& fileSizeFromHeader,
1101 size_t pos = 0, n_tokens = 0;
1102 while (n_tokens++ < 3 && (
pos = jsonStem.find(
"_",
pos + 1)) != std::string::npos) {
1106 std::ostringstream fileNameWithPID;
1108 fileNameWithPID << reducedJsonStem <<
"_pid" << std::setfill(
'0') << std::setw(5) <<
pid_ <<
".jsn";
1112 LogDebug(
"EvFDaqDirector") <<
"RAW parse -: " << rawSourcePath <<
" and JSON create " << jsonDestPath;
1116 int32_t nbEventsWrittenRaw;
1117 int64_t fileSizeFromRaw;
1119 rawSourcePath, rawFd, rawHeaderSize, lsFromRaw, nbEventsWrittenRaw, fileSizeFromRaw,
true,
true, closeFile);
1127 int oflag = O_CREAT | O_WRONLY | O_TRUNC | O_EXCL;
1128 int omode = S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH;
1129 if ((
outfile = ::open(jsonDestPath.c_str(), oflag, omode)) < 0) {
1130 if (errno == EEXIST) {
1131 edm::LogError(
"EvFDaqDirector") <<
"grabNextJsonFromRaw - destination file already exists -: " << jsonDestPath
1135 edm::LogError(
"EvFDaqDirector") <<
"grabNextJsonFromRaw - failed to open output file -: " << jsonDestPath <<
" : "
1137 struct stat out_stat;
1138 if (
stat(jsonDestPath.c_str(), &out_stat) == 0) {
1140 <<
"grabNextJsonFromRaw - output file possibly got created with error, deleting and retry -: "
1142 if (unlink(jsonDestPath.c_str()) == -1) {
1144 <<
"grabNextJsonFromRaw - failed to remove -: " << jsonDestPath <<
" : " << strerror(errno);
1147 if ((
outfile = ::open(jsonDestPath.c_str(), oflag, omode)) < 0) {
1148 edm::LogError(
"EvFDaqDirector") <<
"grabNextJsonFromRaw - failed to open output file (on retry) -: "
1149 << jsonDestPath <<
" : " << strerror(errno);
1154 std::stringstream
ss;
1155 ss <<
"{\"data\":[" << nbEventsWrittenRaw <<
"," << fileSizeFromRaw <<
",\"" << rawSourcePath <<
"\"]}";
1159 edm::LogError(
"EvFDaqDirector") <<
"grabNextJsonFromRaw - failed to write to output file file -: " << jsonDestPath
1160 <<
" : " << strerror(errno);
1164 if (serverLS && serverLS != lsFromRaw)
1165 edm::LogWarning(
"EvFDaqDirector") <<
"grabNextJsonFromRaw - mismatch in expected (server) LS " << serverLS
1166 <<
" and raw file header LS " << lsFromRaw;
1168 fileSizeFromHeader = fileSizeFromRaw;
1169 return nbEventsWrittenRaw;
1174 int64_t& fileSizeFromJson,
1179 std::ostringstream fileNameWithPID;
1181 << std::setw(5) <<
pid_ <<
".jsn";
1186 LogDebug(
"EvFDaqDirector") <<
"JSON rename -: " << jsonSourcePath <<
" to " << jsonDestPath;
1190 if ((
infile = ::open(jsonSourcePath.c_str(), O_RDONLY)) < 0) {
1191 edm::LogWarning(
"EvFDaqDirector") <<
"grabNextJsonFile - failed to open input file -: " << jsonSourcePath <<
" : "
1193 if ((
infile = ::open(jsonSourcePath.c_str(), O_RDONLY)) < 0) {
1194 edm::LogError(
"EvFDaqDirector") <<
"grabNextJsonFile - failed to open input file (on retry) -: "
1195 << jsonSourcePath <<
" : " << strerror(errno);
1196 if (errno == ENOENT)
1202 int oflag = O_CREAT | O_WRONLY | O_TRUNC | O_EXCL;
1203 int omode = S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH;
1204 if ((
outfile = ::open(jsonDestPath.c_str(), oflag, omode)) < 0) {
1205 if (errno == EEXIST) {
1206 edm::LogError(
"EvFDaqDirector") <<
"grabNextJsonFile - destination file already exists -: " << jsonDestPath
1211 edm::LogError(
"EvFDaqDirector") <<
"grabNextJsonFile - failed to open output file -: " << jsonDestPath <<
" : "
1213 struct stat out_stat;
1214 if (
stat(jsonDestPath.c_str(), &out_stat) == 0) {
1216 <<
"grabNextJsonFile - output file possibly got created with error, deleting and retry -: " << jsonDestPath;
1217 if (unlink(jsonDestPath.c_str()) == -1) {
1219 <<
"grabNextJsonFile - failed to remove -: " << jsonDestPath <<
" : " << strerror(errno);
1222 if ((
outfile = ::open(jsonDestPath.c_str(), oflag, omode)) < 0) {
1223 edm::LogError(
"EvFDaqDirector") <<
"grabNextJsonFile - failed to open output file (on retry) -: "
1224 << jsonDestPath <<
" : " << strerror(errno);
1230 const std::size_t buf_sz = 512;
1231 std::size_t tot_written = 0;
1232 std::unique_ptr<char>
buf(
new char[buf_sz]);
1234 ssize_t sz, sz_read = 1, sz_write;
1235 while (sz_read > 0 && (sz_read = ::
read(
infile,
buf.get(), buf_sz)) > 0) {
1238 assert(sz_read - sz_write > 0);
1239 if ((sz = ::
write(
outfile,
buf.get() + sz_write, sz_read - sz_write)) < 0) {
1246 }
while (sz_write < sz_read);
1251 if (tot_written > 0) {
1253 if (unlink(jsonSourcePath.c_str()) == -1) {
1254 edm::LogError(
"EvFDaqDirector") <<
"grabNextJsonFile - failed to remove -: " << jsonSourcePath <<
" : "
1259 edm::LogError(
"EvFDaqDirector") <<
"grabNextJsonFile - failed to copy json file or file was empty -: "
1268 std::stringstream
ss;
1271 if (tot_written <= buf_sz) {
1276 boost::filesystem::ifstream ij(jsonDestPath);
1278 }
catch (boost::filesystem::filesystem_error
const& ex) {
1279 edm::LogError(
"EvFDaqDirector") <<
"grabNextJsonFile - BOOST FILESYSTEM ERROR CAUGHT -: " << ex.what();
1285 if (tot_written <= buf_sz)
1287 edm::LogError(
"EvFDaqDirector") <<
"Failed to deserialize JSON file -: " << jsonDestPath <<
"\nERROR:\n"
1288 <<
reader.getFormatedErrorMessages() <<
"CONTENT:\n"
1295 dp.deserialize(deserializeRoot);
1299 if (
i <
dp.getData().size()) {
1306 if (!
dp.getData().empty())
1310 <<
"grabNextJsonFile - "
1311 <<
" error reading number of events from BU JSON; No input value. data -: " <<
data;
1317 fileSizeFromJson = -1;
1320 if (
i <
dp.getData().size()) {
1323 fileSizeFromJson = boost::lexical_cast<long>(dataSize);
1324 }
catch (boost::bad_lexical_cast
const&) {
1326 edm::LogWarning(
"EvFDaqDirector") <<
"grabNextJsonFile - error parsing number of Bytes from BU JSON. "
1327 <<
"Input value is -: " << dataSize;
1333 return boost::lexical_cast<int>(
data);
1334 }
catch (boost::bad_lexical_cast
const&
e) {
1335 edm::LogError(
"EvFDaqDirector") <<
"grabNextJsonFile - error parsing number of events from BU JSON. "
1336 <<
"Input value is -: " <<
data;
1337 }
catch (std::runtime_error
const&
e) {
1339 edm::LogError(
"EvFDaqDirector") <<
"grabNextJsonFile - std::runtime_error exception -: " <<
e.what();
1343 edm::LogError(
"EvFDaqDirector") <<
"grabNextJsonFile - SOME OTHER EXCEPTION OCCURED! -: " <<
e.what();
1346 edm::LogError(
"EvFDaqDirector") <<
"grabNextJsonFile - SOME OTHER EXCEPTION OCCURED!";
1359 std::ostringstream fileNameWithPID;
1360 fileNameWithPID << jsonSourcePath.stem().string() <<
"_pid" << std::setfill(
'0') << std::setw(5) << getpid()
1362 jsonDestPath /= fileNameWithPID.str();
1364 LogDebug(
"EvFDaqDirector") <<
"JSON rename -: " << jsonSourcePath <<
" to " << jsonDestPath;
1367 }
catch (boost::filesystem::filesystem_error
const& ex) {
1369 edm::LogError(
"EvFDaqDirector") <<
"grabNextFile BOOST FILESYSTEM ERROR CAUGHT -: " << ex.what();
1379 }
catch (boost::filesystem::filesystem_error
const& ex) {
1381 edm::LogError(
"EvFDaqDirector") <<
"grabNextFile BOOST FILESYSTEM ERROR CAUGHT -: " << ex.what();
1384 edm::LogError(
"EvFDaqDirector") <<
"grabNextFile std::exception CAUGHT -: " << ex.what();
1387 boost::filesystem::ifstream ij(jsonDestPath);
1391 std::stringstream
ss;
1393 if (!
reader.parse(
ss.str(), deserializeRoot)) {
1394 edm::LogError(
"EvFDaqDirector") <<
"grabNextFile Failed to deserialize JSON file -: " << jsonDestPath
1396 <<
reader.getFormatedErrorMessages() <<
"CONTENT:\n"
1398 throw std::runtime_error(
"Cannot deserialize input JSON file");
1404 dp.deserialize(deserializeRoot);
1408 if (
i <
dp.getData().size()) {
1414 if (!
dp.getData().empty())
1418 <<
" error reading number of events from BU JSON -: No input value " <<
data;
1420 return boost::lexical_cast<int>(
data);
1421 }
catch (boost::filesystem::filesystem_error
const& ex) {
1424 edm::LogError(
"EvFDaqDirector") <<
"grabNextFile BOOST FILESYSTEM ERROR CAUGHT -: " << ex.what();
1425 }
catch (std::runtime_error
const&
e) {
1428 edm::LogError(
"EvFDaqDirector") <<
"grabNextFile runtime Exception -: " <<
e.what();
1429 }
catch (boost::bad_lexical_cast
const&) {
1430 edm::LogError(
"EvFDaqDirector") <<
"grabNextFile error parsing number of events from BU JSON. "
1431 <<
"Input value is -: " <<
data;
1435 edm::LogError(
"EvFDaqDirector") <<
"grabNextFile SOME OTHER EXCEPTION OCCURED!!!! -: " <<
e.what();
1444 uint32_t& closedServerLS,
1450 serverError =
false;
1452 boost::system::error_code ec;
1460 edm::LogWarning(
"EvFDaqDirector") <<
"boost::asio::connect error -:" << ec;
1466 boost::asio::streambuf request;
1467 std::ostream request_stream(&request);
1470 std::stringstream spath;
1471 spath <<
path <<
"&stopls=" << maxLS;
1475 request_stream <<
"GET " <<
path <<
" HTTP/1.1\r\n";
1477 request_stream <<
"Accept: */*\r\n";
1478 request_stream <<
"Connection: keep-alive\r\n\r\n";
1483 edm::LogInfo(
"EvFDaqDirector") <<
"reconnecting socket on received connection_reset";
1487 edm::LogWarning(
"EvFDaqDirector") <<
"boost::asio::connect error -:" << ec;
1493 edm::LogWarning(
"EvFDaqDirector") <<
"boost::asio::write error -:" << ec;
1498 boost::asio::streambuf response;
1499 boost::asio::read_until(*
socket_, response,
"\r\n", ec);
1501 edm::LogWarning(
"EvFDaqDirector") <<
"boost::asio::read_until error -:" << ec;
1506 std::istream response_stream(&response);
1509 response_stream >> http_version;
1511 response_stream >> serverHttpStatus;
1514 std::getline(response_stream, status_message);
1515 if (!response_stream || http_version.substr(0, 5) !=
"HTTP/") {
1520 if (serverHttpStatus != 200) {
1521 edm::LogWarning(
"EvFDaqDirector") <<
"Response returned with status code " << serverHttpStatus;
1528 while (std::getline(response_stream,
header) &&
header !=
"\r") {
1532 std::map<std::string, std::string> serverMap;
1533 while (std::getline(response_stream, fileInfo) && fileInfo !=
"\r") {
1534 auto pos = fileInfo.find(
"=");
1535 if (
pos == std::string::npos)
1537 auto stitle = fileInfo.substr(0,
pos);
1538 auto svalue = fileInfo.substr(
pos + 1);
1539 serverMap[stitle] = svalue;
1543 auto server_version = serverMap.find(
"version");
1544 assert(server_version != serverMap.end());
1546 auto server_run = serverMap.find(
"runnumber");
1547 assert(server_run != serverMap.end());
1550 auto server_state = serverMap.find(
"state");
1551 assert(server_state != serverMap.end());
1553 auto server_eols = serverMap.find(
"lasteols");
1554 assert(server_eols != serverMap.end());
1556 auto server_ls = serverMap.find(
"lumisection");
1558 int version_maj = 1;
1559 int version_min = 0;
1560 int version_rev = 0;
1562 auto* s_ptr = server_version->second.c_str();
1563 if (!server_version->second.empty() && server_version->second[0] ==
'"')
1565 auto res = sscanf(s_ptr,
"%d.%d.%d", &version_maj, &version_min, &version_rev);
1567 res = sscanf(s_ptr,
"%d.%d", &version_maj, &version_min);
1569 res = sscanf(s_ptr,
"%d", &version_maj);
1572 edm::LogWarning(
"EvFDaqDirector") <<
"Can not parse server version " << server_version->second;
1579 if (server_ls != serverMap.end())
1582 serverLS = closedServerLS + 1;
1585 if (s_state ==
"STARTING")
1587 auto server_file = serverMap.find(
"file");
1588 assert(server_file == serverMap.end());
1590 edm::LogInfo(
"EvFDaqDirector") <<
"Got STARTING notification with last EOLS " << closedServerLS;
1591 }
else if (s_state ==
"READY") {
1592 auto server_file = serverMap.find(
"file");
1593 if (server_file == serverMap.end()) {
1595 if (serverLS <= closedServerLS)
1596 serverLS = closedServerLS + 1;
1599 <<
"Got READY notification with last EOLS " << closedServerLS <<
" and no new file";
1603 auto server_fileprefix = serverMap.find(
"fileprefix");
1605 if (server_fileprefix != serverMap.end()) {
1606 auto pssize = server_fileprefix->second.size();
1607 if (pssize > 1 && server_fileprefix->second[0] ==
'"' && server_fileprefix->second[pssize - 1] ==
'"')
1608 fileprefix = server_fileprefix->second.substr(1, pssize - 2);
1610 fileprefix = server_fileprefix->second;
1614 auto ssize = server_file->second.size();
1615 if (ssize > 1 && server_file->second[0] ==
'"' && server_file->second[ssize - 1] ==
'"')
1616 filestem = server_file->second.substr(1, ssize - 2);
1618 filestem = server_file->second;
1619 assert(!filestem.empty());
1620 if (version_maj > 1) {
1621 nextFileRaw =
bu_run_dir_ +
"/" + fileprefix + filestem +
".raw";
1622 filestem =
bu_run_dir_ +
"/" + fileprefix + filestem;
1626 nextFileRaw =
bu_run_dir_ +
"/" + filestem +
".raw";
1627 filestem =
bu_run_dir_ +
"/" + fileprefix + filestem;
1628 nextFileJson = filestem +
".jsn";
1632 edm::LogInfo(
"EvFDaqDirector") <<
"Got READY notification with last EOLS " << closedServerLS <<
" new LS "
1633 << serverLS <<
" file:" << filestem;
1635 }
else if (s_state ==
"EOLS") {
1636 serverLS = closedServerLS + 1;
1637 edm::LogInfo(
"EvFDaqDirector") <<
"Got EOLS notification with last EOLS " << closedServerLS;
1639 }
else if (s_state ==
"EOR") {
1641 edm::LogInfo(
"EvFDaqDirector") <<
"Got EOR notification with last EOLS " << closedServerLS;
1643 }
else if (s_state ==
"NORUN") {
1644 auto err_msg = serverMap.find(
"errormessage");
1645 if (err_msg != serverMap.end())
1646 edm::LogWarning(
"EvFDaqDirector") <<
"Server NORUN -:" << server_state->second <<
" : " << err_msg->second;
1651 }
else if (s_state ==
"ERROR") {
1652 auto err_msg = serverMap.find(
"errormessage");
1653 if (err_msg != serverMap.end())
1654 edm::LogWarning(
"EvFDaqDirector") <<
"Server error -:" << server_state->second <<
" : " << err_msg->second;
1656 edm::LogWarning(
"EvFDaqDirector") <<
"Server error -:" << server_state->second;
1660 edm::LogWarning(
"EvFDaqDirector") <<
"Unknown Server state -:" << server_state->second;
1669 if (ec != boost::asio::error::eof) {
1670 edm::LogWarning(
"EvFDaqDirector") <<
"boost::asio::read_until error -:" << ec;
1682 socket_->shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
1684 edm::LogWarning(
"EvFDaqDirector") <<
"socket shutdown error -:" << ec;
1708 uint16_t& rawHeaderSize,
1709 int32_t& serverEventsInNewFile,
1710 int64_t& fileSizeFromMetadata,
1719 int stopFileLS = -1;
1722 if (stopFileCheck == 0 || stopFilePidCheck == 0) {
1723 if (stopFileCheck == 0)
1729 if (stopFileLS >= 0 && (
int)
ls >= stopFileLS)
1733 edm::LogWarning(
"EvFDaqDirector") <<
"Detected stop request from hltd. Ending run for this process after LS -: "
1747 timeval ts_lockbegin;
1748 gettimeofday(&ts_lockbegin,
nullptr);
1751 uint32_t serverLS, closedServerLS;
1752 unsigned int serverHttpStatus;
1759 int maxLS = stopFileLS < 0 ? -1 :
std::max(stopFileLS, (
int)currentLumiSection);
1760 bool rawHeader =
false;
1762 serverHttpStatus, serverError, serverLS, closedServerLS, nextFileJson, nextFileRaw, rawHeader, maxLS);
1772 if (currentLumiSection == 0) {
1780 if (closedServerLS >= currentLumiSection) {
1781 for (uint32_t
i =
std::max(currentLumiSection, 1
U);
i <= closedServerLS;
i++)
1786 bool fileFound =
true;
1790 serverEventsInNewFile =
1791 grabNextJsonFromRaw(nextFileRaw, rawFd, rawHeaderSize, fileSizeFromMetadata, fileFound, serverLS,
false);
1793 serverEventsInNewFile =
grabNextJsonFile(nextFileJson, nextFileRaw, fileSizeFromMetadata, fileFound);
1796 if (serverEventsInNewFile < 0 && rawFd != -1) {
1816 else if (fileStatus ==
newFile) {
1819 }
else if (fileStatus ==
noFile) {
1823 edm::LogWarning(
"EvFDaqDirector") <<
"Server reported LS " << serverLS
1824 <<
" which is smaller than currently open LS " <<
ls <<
". Ignoring response";
1836 if (!boost::filesystem::is_directory(openPath)) {
1837 LogDebug(
"EvFDaqDirector") <<
"<open> FU dir not found. Creating... -:" << openPath.string();
1838 boost::filesystem::create_directories(openPath);
1843 boost::filesystem::ifstream ij(
file);
1847 if (!
reader.parse(ij, deserializeRoot)) {
1848 edm::LogError(
"EvFDaqDirector") <<
"Cannot deserialize input JSON file -:" <<
file;
1852 int ret = deserializeRoot.
get(
"lastLS",
"").
asInt();
1862 std::stringstream
ss;
1863 ss << fileprefix << std::setfill(
'0') << std::setw(4) << lscount <<
"_EoLS.jsn";
1881 std::vector<std::string>
destinations = tsPset.getParameter<std::vector<std::string>>(
"destinations");
1883 destinationsVal.append(
dest);
1884 (*transferSystemJson_)[
"destinations"] = destinationsVal;
1887 std::vector<std::string> modes = tsPset.getParameter<std::vector<std::string>>(
"transferModes");
1888 for (
auto&
mode : modes)
1890 (*transferSystemJson_)[
"transferModes"] = modesVal;
1892 for (
auto psKeyItr = tsPset.psetTable().begin(); psKeyItr != tsPset.psetTable().end(); ++psKeyItr) {
1893 if (psKeyItr->first !=
"destinations" && psKeyItr->first !=
"transferModes") {
1896 for (
auto&
mode : modes) {
1898 if (!streamDef.
existsAs<std::vector<std::string>>(
mode,
true))
1900 <<
" Missing transfer system specification for -:" << psKeyItr->first <<
" (transferMode " <<
mode
1902 std::vector<std::string> streamDestinations = streamDef.
getParameter<std::vector<std::string>>(
mode);
1906 if (streamDestinations.empty())
1908 <<
" Missing transter system destination(s) for -: " << psKeyItr->first <<
", mode:" <<
mode;
1910 for (
auto& sdest : streamDestinations) {
1911 bool sDestValid =
false;
1912 sDestsValue.append(sdest);
1919 <<
" Invalid transter system destination specified for -: " << psKeyItr->first <<
", mode:" <<
mode
1920 <<
", dest:" << sdest;
1922 streamVal[
mode] = sDestsValue;
1924 (*transferSystemJson_)[psKeyItr->first] = streamVal;
1929 throw cms::Exception(
"EvFDaqDirector") <<
"transferSystem PSet not found";
1936 streamRequestName =
stream;
1938 std::stringstream
msg;
1939 msg <<
"Transfer system mode definitions missing for -: " <<
stream;
1950 <<
"Selected mode string is not provided as DaqDirector parameter."
1951 <<
"Switch on requireTSPSet parameter to enforce this requirement. Setting mode to empty string.";
1955 throw cms::Exception(
"EvFDaqDirector") <<
"Selected mode string is not provided as DaqDirector parameter.";
1959 std::stringstream
msg;
1974 ret += (*it).asString();
1989 tbb::concurrent_hash_map<std::string, std::string>::accessor ac;
1991 ac->second = streamType;
1998 tbb::concurrent_hash_map<std::string, std::string>::const_accessor search_ac;
2000 return search_ac->second;
2002 edm::LogInfo(
"EvFDaqDirector") <<
" No merging type specified for stream " <<
stream <<
". Using default value";
2004 tbb::concurrent_hash_map<std::string, std::string>::accessor ac;
2006 ac->second = defaultName;
2013 int proc_flag_fd = open(proc_flag.c_str(), O_RDWR | O_CREAT, S_IRWXU | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
2014 close(proc_flag_fd);
2017 struct flock
EvFDaqDirector::make_flock(short type, short whence, off_t start, off_t len, pid_t pid) {