1 #include "artdaq/DAQdata/Globals.hh"
2 #define TRACE_NAME "TransferWrapper"
4 #include "artdaq-core/Data/Fragment.hh"
5 #include "artdaq-core/Utilities/ExceptionHandler.hh"
6 #include "artdaq/ArtModules/detail/TransferWrapper.hh"
7 #include "artdaq/DAQdata/NetMonHeader.hh"
8 #include "artdaq/ExternalComms/MakeCommanderPlugin.hh"
9 #include "artdaq/TransferPlugins/MakeTransferPlugin.hh"
11 #include "cetlib/BasicPluginFactory.h"
12 #include "cetlib_except/exception.h"
13 #include "fhiclcpp/ParameterSet.h"
29 void signal_handler(
int signal)
31 gSignalStatus = signal;
35 : timeoutInUsecs_(pset.get<std::size_t>(
"timeoutInUsecs", 100000))
39 , dispatcherHost_(pset.get<std::string>(
"dispatcherHost",
"localhost"))
40 , dispatcherPort_(pset.get<std::string>(
"dispatcherPort",
"5266"))
41 , serverUrl_(pset.get<std::string>(
"server_url",
"http://" + dispatcherHost_ +
":" + dispatcherPort_ +
"/RPC2"))
42 , maxEventsBeforeInit_(pset.get<std::size_t>(
"maxEventsBeforeInit", 5))
43 , allowedFragmentTypes_(pset.get<std::vector<int>>(
"allowedFragmentTypes", {226, 227, 229}))
44 , runningStateTimeout_(pset.get<
double>(
"dispatcherConnectTimeout", 0))
45 , runningStateInterval_us_(pset.get<
size_t>(
"dispatcherConnectRetryInterval_us", 1000000))
46 , quitOnFragmentIntegrityProblem_(pset.get<
bool>(
"quitOnFragmentIntegrityProblem",
true))
47 , multi_run_mode_(pset.get<
bool>(
"allowMultipleRuns",
false))
48 , monitorRegistered_(
false)
50 std::signal(SIGINT, signal_handler);
56 metricMan->initialize(pset.get<fhicl::ParameterSet>(
"metrics", fhicl::ParameterSet()),
"Online Monitor");
57 metricMan->do_start();
62 ExceptionHandler(ExceptionHandlerRethrow::no,
"TransferWrapper: could not configure metrics");
66 if (runningStateInterval_us_ < 1000)
68 TLOG(TLVL_WARNING) <<
"Invalid value " << runningStateInterval_us_ <<
" us detected for dispatcherConnectRetryInterval_us. Setting to 1000 us";
69 runningStateInterval_us_ = 1000;
71 if (runningStateInterval_us_ > 30000000)
73 TLOG(TLVL_WARNING) <<
"Invalid value " << runningStateInterval_us_ <<
" us detected for dispatcherConnectRetryInterval_us. Setting to 30,000,000 us";
74 runningStateInterval_us_ = 30000000;
77 fhicl::ParameterSet new_pset(pset);
78 if (!new_pset.has_key(
"server_url"))
80 new_pset.put<std::string>(
"server_url", serverUrl_);
89 std::unique_ptr<artdaq::Fragment> fragmentPtr;
90 bool receivedFragment =
false;
91 static bool initialized =
false;
92 static size_t fragments_received = 0;
94 while (
true && !gSignalStatus)
96 receivedFragment =
false;
97 fragmentPtr = std::make_unique<artdaq::Fragment>();
99 while (!receivedFragment)
103 TLOG(TLVL_INFO) <<
"Ctrl-C appears to have been hit";
107 if (!monitorRegistered_)
110 if (!monitorRegistered_)
return nullptr;
115 auto result = transfer_->receiveFragment(*fragmentPtr, timeoutInUsecs_);
119 receivedFragment =
true;
120 fragments_received++;
122 static size_t cntr = 0;
123 auto mod = ++cntr % 10;
125 if (mod == 1) suffix =
"-st";
126 if (mod == 2) suffix =
"-nd";
127 if (mod == 3) suffix =
"-rd";
128 TLOG(TLVL_INFO) <<
"Received " << cntr << suffix <<
" event, "
129 <<
"seqID == " << fragmentPtr->sequenceID()
130 <<
", type == " << fragmentPtr->typeString();
135 TLOG(TLVL_ERROR) <<
"Transfer Plugin disconnected or other unrecoverable error. Shutting down.";
144 TLOG(TLVL_WARNING) <<
"Timeout occurred in call to transfer_->receiveFragmentFrom; will try again"
145 <<
", status = " << result;
150 ExceptionHandler(ExceptionHandlerRethrow::yes,
151 "Problem receiving data in TransferWrapper::receiveMessage");
155 if (fragmentPtr->type() == artdaq::Fragment::EndOfDataFragmentType)
173 checkIntegrity(*fragmentPtr);
175 if (initialized || fragmentPtr->type() == artdaq::Fragment::InitFragmentType)
182 receivedFragment =
false;
184 if (fragments_received > maxEventsBeforeInit_)
186 throw cet::exception(
"TransferWrapper") <<
"First " << maxEventsBeforeInit_ <<
" events received did not include the \"Init\" event containing necessary info for art; exiting...";
194 void artdaq::TransferWrapper::checkIntegrity(
const artdaq::Fragment& fragment)
const
196 const size_t artdaqheader = artdaq::detail::RawFragmentHeader::num_words() *
197 sizeof(artdaq::detail::RawFragmentHeader::RawDataType);
198 const size_t payload =
static_cast<size_t>(fragment.dataEndBytes() - fragment.dataBeginBytes());
200 const size_t totalsize = fragment.sizeBytes();
202 const size_t type =
static_cast<size_t>(fragment.type());
204 if (totalsize != artdaqheader + metadata + payload)
206 std::stringstream errmsg;
207 errmsg <<
"Error: artdaq fragment of type " << fragment.typeString() <<
", sequence ID " << fragment.sequenceID() <<
" has internally inconsistent measures of its size, signalling data corruption: in bytes,"
208 <<
" total size = " << totalsize <<
", artdaq fragment header = " << artdaqheader <<
", metadata = " << metadata <<
", payload = " << payload;
210 TLOG(TLVL_ERROR) << errmsg.str();
212 if (quitOnFragmentIntegrityProblem_)
214 throw cet::exception(
"TransferWrapper") << errmsg.str();
222 auto findloc = std::find(allowedFragmentTypes_.begin(), allowedFragmentTypes_.end(),
static_cast<int>(type));
224 if (findloc == allowedFragmentTypes_.end())
226 std::stringstream errmsg;
227 errmsg <<
"Error: artdaq fragment appears to have type "
228 << type <<
", not found in the allowed fragment types list";
230 TLOG(TLVL_ERROR) << errmsg.str();
231 if (quitOnFragmentIntegrityProblem_)
233 throw cet::exception(
"TransferWrapper") << errmsg.str();
242 void artdaq::TransferWrapper::registerMonitor()
246 transfer_.reset(
nullptr);
251 ExceptionHandler(ExceptionHandlerRethrow::yes,
252 "TransferWrapper: failure in call to MakeTransferPlugin");
255 auto start = std::chrono::steady_clock::now();
256 auto sts = getDispatcherStatus();
257 while (sts !=
"Running" && (runningStateTimeout_ == 0 || TimeUtils::GetElapsedTime(start) < runningStateTimeout_))
259 TLOG(TLVL_DEBUG) <<
"Dispatcher state: " << sts;
262 TLOG(TLVL_INFO) <<
"Ctrl-C appears to have been hit";
265 TLOG(TLVL_INFO) <<
"Waited " << std::fixed << std::setprecision(2) << TimeUtils::GetElapsedTime(start) <<
" s / " << runningStateTimeout_ <<
" s for Dispatcher to enter the Running state";
266 usleep(runningStateInterval_us_);
267 sts = getDispatcherStatus();
269 if (sts !=
"Running")
return;
271 auto dispatcherConfig = pset_.get<fhicl::ParameterSet>(
"dispatcher_config");
277 TLOG(TLVL_INFO) <<
"Attempting to register this monitor (\"" << transfer_->uniqueLabel()
278 <<
"\") with the dispatcher aggregator";
280 auto status = commander_->send_register_monitor(dispatcherConfig.to_string());
282 TLOG(TLVL_INFO) <<
"Response from dispatcher is \"" << status <<
"\"";
284 if (status ==
"Success")
286 monitorRegistered_ =
true;
291 TLOG(TLVL_WARNING) <<
"Error in TransferWrapper: attempt to register with dispatcher did not result in the \"Success\" response";
298 void artdaq::TransferWrapper::unregisterMonitor()
300 if (!monitorRegistered_)
302 TLOG(TLVL_WARNING) <<
"The function to unregister the monitor was called, but the monitor doesn't appear to be registered";
306 std::string sts = getDispatcherStatus();
308 if (sts ==
"")
return;
310 if (sts !=
"Running" && sts !=
"Ready")
312 TLOG(TLVL_WARNING) <<
"The Dispatcher is not in the Running or Ready state, will not attempt to unregister";
319 TLOG(TLVL_INFO) <<
"Requesting that this monitor (" << transfer_->uniqueLabel()
320 <<
") be unregistered from the dispatcher aggregator";
322 auto status = commander_->send_unregister_monitor(transfer_->uniqueLabel());
324 TLOG(TLVL_INFO) <<
"Response from dispatcher is \"" << status <<
"\"";
326 if (status ==
"Success")
330 else if (status ==
"busy")
334 TLOG(TLVL_WARNING) <<
"The Dispatcher returned status " << status <<
" when attempting to unregister this monitor!";
340 monitorRegistered_ =
false;
343 std::string artdaq::TransferWrapper::getDispatcherStatus()
347 return commander_->send_status();
349 catch (std::exception
const& ex)
351 TLOG(TLVL_WARNING) <<
"An exception was thrown trying to collect the Dispatcher's status. Most likely cause is the application is no longer running.";
358 if (monitorRegistered_)
366 ExceptionHandler(ExceptionHandlerRethrow::no,
367 "An exception occurred when trying to unregister monitor during TransferWrapper's destruction");
artdaq::FragmentPtr receiveMessage()
Receive a Fragment from the TransferInterface, and send it to art.
Commandable is the base class for all artdaq components which implement the artdaq state machine...
TransferWrapper(const fhicl::ParameterSet &pset)
TransferWrapper Constructor.
static void CleanUpGlobals()
Clean up statically-allocated Manager class instances.
virtual ~TransferWrapper()
TransferWrapper Destructor.
This TransferInterface is a Receiver.
std::unique_ptr< artdaq::TransferInterface > MakeTransferPlugin(const fhicl::ParameterSet &pset, std::string plugin_label, TransferInterface::Role role)
Load a TransferInterface plugin.
volatile std::sig_atomic_t gSignalStatus
Stores singal from signal handler.
std::unique_ptr< artdaq::CommanderInterface > MakeCommanderPlugin(const fhicl::ParameterSet &commander_pset, artdaq::Commandable &commandable)
Load a CommanderInterface plugin.
Value that is to be returned when a Transfer plugin determines that no more data will be arriving...
For code clarity, things checking for successful receive should check retval >= NO_RANK_INFO.