18 #include "GaudiKernel/ISvcLocator.h"
19 #include "GaudiKernel/IIncidentSvc.h"
20 #include "GaudiKernel/Incident.h"
21 #include "GaudiKernel/IAlgTool.h"
22 #include "GaudiKernel/IToolSvc.h"
23 #include "GaudiKernel/IClassIDSvc.h"
24 #include "GaudiKernel/Guards.h"
25 #include "GaudiKernel/ConcurrencyFlags.h"
50 const char *cstr =
str.c_str();
52 for (
unsigned int i=0;
i <
str.length(); ++
i) {
53 str2 += toupper(*(cstr+
i));
60 std::atomic<bool> s_firstRun(
true);
75 if (
x->clID() ==
y->clID() ) {
76 return (
x->name() <
y->name() );
78 return (
x->clID() <
y->clID() );
90 if (
a->name()!=
b->name())
return a->name()<
b->name();
91 if (
a->clID()!=
b->clID())
return a->clID()<
b->clID();
104 const IInterface*
parent):
106 m_storeName(
"StoreGateSvc"),
107 p_cndSvc(
"DetectorStore",
name),
108 p_incSvc(
"IncidentSvc",
name), p_PPSvc(
"ProxyProviderSvc",
name),
109 p_CLIDSvc(
"ClassIDSvc",
name), p_toolSvc(
"ToolSvc",
name),
130 delete ( oitr->second );
140 std::set< const TransientAddress*, SortTADptr >::const_iterator titr;
155 static const bool CREATEIF(
true);
158 ATH_CHECK( service(
"IOVSvc", p_iovSvc,CREATEIF) );
160 IProperty* iovSvcProp =
dynamic_cast<IProperty*
>( p_iovSvc );
161 if (iovSvcProp ==
nullptr) {
163 return StatusCode::FAILURE;
166 ATH_CHECK( setProperty( iovSvcProp->getProperty(
"preLoadRanges") ) );
167 ATH_CHECK( setProperty( iovSvcProp->getProperty(
"preLoadData") ) );
168 ATH_CHECK( setProperty( iovSvcProp->getProperty(
"partialPreLoadData") ) );
169 ATH_CHECK( setProperty( iovSvcProp->getProperty(
"preLoadExtensibleFolders") ) );
170 ATH_CHECK( setProperty( iovSvcProp->getProperty(
"updateInterval") ) );
171 ATH_CHECK( setProperty( iovSvcProp->getProperty(
"sortKeys") ) );
172 ATH_CHECK( setProperty( iovSvcProp->getProperty(
"forceResetAtBeginRun") ) );
173 ATH_CHECK( setProperty( iovSvcProp->getProperty(
"OutputLevel") ) );
182 p_incSvc->addListener(
this,
"BeginRun", pri,
true);
185 msg() <<
"IOVRanges will be checked only ";
186 msg().setColor(MSG::CYAN);
189 msg() <<
" at the start of the job" <<
endmsg;
190 }
else if (updi ==
"RUN") {
192 p_incSvc->addListener(
this,
"BeginRun", pri,
true);
195 msg() <<
"IOVRanges will be checked at every ";
196 msg().setColor(MSG::CYAN);
198 }
else if (updi ==
"EVENT") {
200 p_incSvc->addListener(
this,
"BeginEvent", pri,
true);
201 p_incSvc->addListener(
this,
"BeginRun", pri,
true);
204 msg() <<
"IOVRanges will be checked at every ";
205 msg().setColor(MSG::CYAN);
209 <<
"\"event\" \"run\" or \"job\"");
210 return StatusCode::FAILURE;
216 msg() <<
"IOV Data will be preloaded at the same interval" <<
endmsg;
221 return StatusCode::SUCCESS;
238 else if (!initial_first) {
239 if ( inc.type() !=
m_checkTrigger && inc.type() != IncidentType::BeginRun ) {
244 std::lock_guard<std::recursive_mutex> lock(m_handleMutex);
251 if ( inc.type() !=
m_checkTrigger && inc.type() != IncidentType::BeginRun ) {
260 if (
proxy ==
nullptr) {
274 const bool first = initial_first;
277 if(Gaudi::Concurrency::ConcurrencyFlags::numProcs()==0) {
278 if (inc.type() == IncidentType::BeginRun) {
290 set< DataProxy*, SortDPptr > proxiesToReset;
291 if ( inc.type() ==
m_checkTrigger || inc.type() == IncidentType::BeginRun ) {
293 const EventContext& context = inc.context();
297 const EventIDBase& eventID = context.eventID();
298 uint32_t event = eventID.lumi_block();
305 curTime.setTimestamp(1000000000L*(
uint64_t)eventID.time_stamp() + eventID.time_stamp_ns_offset());
309 msg() << inc.type() <<
": [R/LB] = " << curTime <<
endmsg;
312 if (inc.type() == IncidentType::BeginRun) {
315 if (StatusCode::SUCCESS != service(
"IOVDbSvc", iovDB,
false)) {
332 std::set< const TransientAddress*, SortTADptr >::const_iterator titr;
336 if (StatusCode::SUCCESS !=
sc) {
356 throw( std::runtime_error(
"IOVSvcTool::preLoadProxies") );
361 if (service(
"IOVDbSvc", iovDB,
false).isSuccess()) {
363 ATH_MSG_DEBUG(
"Signaled end proxy preload to IOVDbSvc " << curTime);
381 std::map<BFCN*, std::list<std::string> > resetKeys;
388 if (inc.type() == IncidentType::BeginRun &&
m_forceReset && !s_firstRun) {
390 ATH_MSG_DEBUG(
"Resetting all proxies on BeginRun incident for store \""
394 std::set< const SG::DataProxy* >::const_iterator pit;
411 auto itr = proxiesToReset.find(
p);
412 if (itr != proxiesToReset.end()) {
413 proxiesToReset.erase( itr );
418 if (!
first && proxiesToReset.size() > 0 &&
420 Gaudi::Concurrency::ConcurrencyFlags::numConcurrentEvents()) > 0 ) ) {
421 ATH_MSG_FATAL(
"Cannot update Conditions via callback functions in MT after the first event");
422 for (
const auto* prox : proxiesToReset) {
423 ATH_MSG_FATAL(
"CLID=" << prox->clID() <<
", name=" << prox->name());
425 throw GaudiException(
"Cannot update Conditions via callback functions in MT after the first event",
name(),StatusCode::FAILURE);
449 Gaudi::Guards::AuditorGuard auditor(
m_names.at(prx), auditorSvc(),
"preLoadProxy");
450 if (prx->accessData() ==
nullptr) {
455 std::list<std::string>
keys;
456 pair<pmITR,pmITR> fitr =
m_proxyMap.equal_range( prx );
457 for (
pmITR p=fitr.first;
p!=fitr.second; ++
p) {
459 std::string
key = prx->name();
460 resetKeys[
f].push_back(
key);
473 CBTree::nodeSet::const_iterator itt, itt_s, itt_e;
475 for (itt = itt_s; itt != itt_e; ++itt) {
478 if (
node->trigger()) {
480 auditorSvc()->before(
"Callback",
m_fcnMap.at(
ff).name());
481 if ((*
ff)(
i,resetKeys[
ff]).isFailure()) {
482 auditorSvc()->after(
"Callback",
m_fcnMap.at(
ff).name());
484 << std::endl <<
"Skipping all subsequent callbacks.");
489 auditorSvc()->after(
"Callback",
m_fcnMap.at(
ff).name());
493 if (
perr !=
nullptr)
break;
517 if ( pitr !=
m_entries.end() && pitr->second->range()->isInRange(curTime) ) {
521 if (!prx->updateAddress()) {
529 IOpaqueAddress *ioa = prx->address();
533 if (iova !=
nullptr) {
544 if ( inc.type() == IncidentType::BeginRun) {
559 if (
proxy ==
nullptr) {
561 return StatusCode::FAILURE;
568 <<
" already registered: " <<
proxy->name());
569 return StatusCode::SUCCESS;
572 std::string tname, fullname;
575 fullname = tname +
"[" +
key +
"]";
582 return StatusCode::SUCCESS;
596 if (
proxy ==
nullptr) {
598 return StatusCode::FAILURE;
606 <<
" not registered: " <<
proxy->name());
607 return StatusCode::SUCCESS;
614 return StatusCode::SUCCESS;
626 while (
it !=
set.end() && !
set.key_comp()(*
it, ent) && !
set.key_comp()(ent,*
it)) {
645 std::lock_guard<std::recursive_mutex> lock(m_handleMutex);
646 assert(
nullptr != pOld);
647 assert(
nullptr != pNew);
657 return StatusCode::SUCCESS;
676 setRange_impl (pNew, *(
const_cast<IOVRange*
>(ent->second->range())));
683 StatusCode::SUCCESS :
684 StatusCode::FAILURE );
697 if (
proxy ==
nullptr) {
700 return StatusCode::FAILURE;
717 if (
proxy ==
nullptr) {
720 return StatusCode::FAILURE;
738 << tad_in->
clID() <<
"/" << tad_in->
name()
739 <<
") alread in preLoad set. Not inserting");
740 return StatusCode::SUCCESS;
746 << tad_in->
clID() <<
"/" << tad_in->
name()
747 <<
") alread in partPreLoad set. Not inserting");
748 return StatusCode::SUCCESS;
754 return StatusCode::SUCCESS;
768 <<
" alread in preLoad set. Not inserting");
769 return StatusCode::SUCCESS;
775 <<
" alread in partPreLoad set. Not inserting");
776 return StatusCode::SUCCESS;
783 return StatusCode::SUCCESS;
790 if (iovr.start().isTimestamp()) {
798 IOVRange *
range =
new IOVRange(iovr);
804 const IOVRange *irn = ent->
range();
845 <<
" in IOVrange:" << iovr);
847 if (!iovr.start().isValid() || !iovr.stop().isValid()) {
848 ATH_MSG_ERROR(
"IOVRange " << iovr <<
"is not valid. Start OK: "
849 << iovr.start().isValid() <<
" Stop OK: " << iovr.stop().isValid()
850 <<
" run/evt/time min/max "
854 return StatusCode::FAILURE;
859 if (
proxy ==
nullptr) {
861 return StatusCode::FAILURE;
864 std::lock_guard<std::recursive_mutex> lock(m_handleMutex);
866 return StatusCode::SUCCESS;
872 IOVRange&
iov)
const {
876 std::lock_guard<std::recursive_mutex> lock(m_handleMutex);
877 std::map<const DataProxy*,IOVEntry*>::const_iterator itr(
m_entries.find(
dp));
879 return StatusCode::FAILURE;
882 iov = *(itr->second->range());
884 return StatusCode::SUCCESS;
893 std::unique_ptr<IOpaqueAddress>& ioa,
894 const IOVTime& curTime)
const {
896 if (curTime.isValid()) {
900 return StatusCode::FAILURE;
911 std::unique_ptr<IOpaqueAddress>& ioa)
const {
917 if (
idb !=
nullptr) {
932 const IOVRange&
range,
const std::string &
tag) {
935 if (!
range.start().isValid() || !
range.stop().isValid()) {
937 return StatusCode::FAILURE;
944 return StatusCode::FAILURE;
947 std::lock_guard<std::recursive_mutex> lock(m_handleMutex);
948 std::map<const DataProxy*,IOVEntry*>::const_iterator itr(
m_entries.find(
dp));
956 if (
idb !=
nullptr) {
960 return StatusCode::FAILURE;
975 service(
"IOVDbSvc", iovDB,
false).ignore();
977 std::map<BFCN*, std::list<std::string> > resetKeys;
979 Gaudi::Guards::AuditorGuard auditor(
m_names[
dp], auditorSvc(),
"preLoadProxy");
982 msg().setColor(MSG::CYAN);
983 msg() <<
"loading proxy for CLID: " <<
dp->clID()
987 if (
dp->provider() ==
nullptr) {
989 <<
". It is probably not a conditions object" << endl;
990 msg() <<
"Proxy Map: ";
993 scr = StatusCode::FAILURE;
1004 if (!
dp->updateAddress())
1005 sc = StatusCode::FAILURE;
1019 <<
dp->clID() <<
"/"
1020 <<
dp->name() <<
")");
1021 if(
dp->accessData() !=
nullptr ) {
1022 sc = StatusCode::SUCCESS;
1024 sc = StatusCode::FAILURE;
1025 ATH_MSG_ERROR(
"preLoading proxies: accessData() failed for " <<
1026 dp->clID() <<
"/" <<
dp->name() <<
")");
1031 if (
sc.isFailure()) scr=
sc;
1036 for (pitr=
pi.first; pitr!=
pi.second; ++pitr) {
1037 BFCN *
f = pitr->second;
1038 std::string
key =
dp->name();
1039 resetKeys[
f].push_back(
key);
1043 if (cn !=
nullptr) {
1049 if (scr.isFailure()) {
1050 ATH_MSG_ERROR(
"Problems preLoading proxies. No callbacks triggered.");
1056 CBTree::nodeSet::const_iterator itt, itt_s, itt_e;
1058 for (itt = itt_s; itt != itt_e; ++itt) {
1061 if (
node->trigger()) {
1065 if ((*
ff)(
i,resetKeys[
ff]).isFailure()) {
1068 return StatusCode::FAILURE;
1089 std::list<std::string> klist;
1090 klist.push_back(
key);
1091 if ( (*
fcn)(
I,klist).isFailure() ) {
1093 return StatusCode::FAILURE;
1096 return StatusCode::SUCCESS;
1103 const std::string&
key ) {
1107 std::map<const SG::DataProxy*, BFCN*>::const_iterator pitr =
1111 return StatusCode::FAILURE;
1125 std::string objname;
1128 msg() << endl <<
"ClockTime start set: " << endl;
1130 objname =
m_names.at( (*start_itr)->proxy() );
1131 msg() <<
" " << objname <<
" (" << (*start_itr)->proxy() <<
") "
1132 << (*start_itr)->range()->start() << endl;
1138 msg() <<
"Run/Event start set: " << endl;
1140 objname =
m_names.at( (*start_itr)->proxy() );
1141 msg() <<
" " << objname <<
" (" << (*start_itr)->proxy() <<
") "
1142 << (*start_itr)->range()->start() << endl;
1153 std::string objname;
1156 msg() << endl <<
"ClockTime stop set: " << endl;
1158 objname =
m_names.at((*stop_itr)->proxy());
1159 msg() <<
" " << objname <<
" (" << (*stop_itr)->proxy() <<
") "
1160 << (*stop_itr)->range()->stop() << endl;
1166 msg() <<
"Run/Event stop set: " << endl;
1168 objname =
m_names.at((*stop_itr)->proxy());
1169 msg() <<
" " << objname <<
" (" << (*stop_itr)->proxy() <<
") "
1170 << (*stop_itr)->range()->stop() << endl;
1180 msg() <<
"------------------------------ IOVSvc Proxy Map "
1181 <<
"------------------------------" << endl;
1186 msg() <<
"----------------------------------------------------------"
1187 <<
"---------------------" << endl;
1195 msg() <<
" " <<
dp <<
" " <<
dp->clID() <<
" "
1198 if (
pi.first ==
pi.second) {
1199 msg() <<
" -> no callback associated" << endl;
1201 for (
auto pitr=
pi.first; pitr!=
pi.second; ++pitr) {
1203 map<BFCN*,CallBackID>::const_iterator fitr =
m_fcnMap.find(
fcn);
1205 msg() <<
" -> " <<
fcn <<
" " << cbid.
name() << endl;
1219 std::string tname,fullname;
1221 if (
sc.isFailure()) {
1223 return StatusCode::FAILURE;
1225 fullname = tname +
"[" +
dp->name() +
"]";
1230 <<
" with DataHandle " << fullname
1231 <<
" -> Need to bind DataHandle first");
1232 return StatusCode::FAILURE;
1239 std::pair<pmITR,pmITR> fitr =
m_proxyMap.equal_range(
dp );
1240 for (
pmITR p=fitr.first;
p!=fitr.second; ++
p) {
1243 <<
" already registered against " << fullname);
1244 return StatusCode::FAILURE;
1260 m_proxyMap.insert(std::pair<const SG::DataProxy*,BFCN* >(
dp,obs));
1261 m_bfcnMap.insert(std::pair<BFCN*, const SG::DataProxy*> (obs,
dp));
1264 ObjMap::const_iterator oitr =
m_objMap.find(
c.ptr());
1266 oitr->second->insert(
c);
1268 std::set<CallBackID> *cbs =
new std::set<CallBackID>;
1275 if ( cn ==
nullptr) {
1282 ATH_MSG_ERROR(
"Cannot find callback node for parent DataProxy "
1286 ATH_MSG_DEBUG(
"register by " <<
c.name() <<
" bound to " << fullname);
1290 ATH_MSG_INFO(
"Still in initialize phase, not tiggering callback for "
1291 <<
c.name() <<
" bound to " << fullname);
1297 return StatusCode::SUCCESS;
1310 std::set<const SG::DataProxy*> proxyset;
1314 std::pair<fnITR,fnITR> fi1 =
m_bfcnMap.equal_range( obs1 );
1315 for (
fnITR fitr1= fi1.first; fitr1!=fi1.second; ++fitr1) {
1318 std::pair<fnITR,fnITR> fi2 =
m_bfcnMap.equal_range( obs2 );
1319 for (
fnITR fitr2=fi2.first; fitr2!=fi2.second; ++fitr2) {
1324 <<
" cannot be registered since it has already been registered "
1325 <<
"against " <<
m_names[prx1]);
1327 proxyset.insert(prx1);
1332 obs2 =
new BFCN( fcn2 );
1337 std::pair<fnITR,fnITR> fi1 =
m_bfcnMap.equal_range( obs1 );
1338 for(
fnITR fitr1=fi1.first; fitr1!=fi1.second; ++fitr1) {
1340 proxyset.insert(prx1);
1344 if (proxyset.size() == 0) {
1346 <<
" cannot be registered, since it has already been registered"
1347 <<
" against everything it can be.");
1348 return StatusCode::SUCCESS;
1352 ObjMap::const_iterator oitr =
m_objMap.find(
c2.ptr());
1354 oitr->second->insert(
c2);
1356 std::set<CallBackID> *cbs =
new std::set<CallBackID>;
1363 std::list<std::string> klist;
1364 for (pitr=proxyset.begin(); pitr!=proxyset.end(); ++pitr) {
1366 m_proxyMap.insert(std::pair<const SG::DataProxy*,BFCN* >(prx,obs2));
1367 m_bfcnMap.insert(std::pair<BFCN*,const SG::DataProxy*>(obs2,prx));
1370 klist.push_back( prx->name() );
1377 if ( cn ==
nullptr) {
1381 if (
cp ==
nullptr) {
1383 <<
". This should never happen");
1384 return StatusCode::FAILURE;
1392 ATH_MSG_INFO(
"Still in initialize phase, not tiggering callback for "
1393 <<
c2.name() <<
" bound to " << *klist.begin());
1399 return StatusCode::SUCCESS;
1410 ObjMap::const_iterator oitr =
m_objMap.find( ia );
1414 ATH_MSG_ERROR(
"No callback registered with AlgTool " << ia->name());
1415 return StatusCode::FAILURE;
1418 std::set<CallBackID> *
sc = oitr->second;
1420 if (
sc->size() == 1) {
1428 ATH_MSG_ERROR(
"More than one callback registered to AlgTool "
1429 << ia->name() <<
". Found : " <<
sc->size());
1430 return StatusCode::FAILURE;
1440 std::set<std::string>&
tools) {
1444 if (
key == pitr->first->name()) {
1450 return ( (
match) ? StatusCode::SUCCESS : StatusCode::FAILURE );
1460 return (StatusCode::SUCCESS);
1467 std::set<SG::DataProxy*, SortDPptr> &proxiesToReset,
1468 const IOVTime& curTime)
const {
1470 if (pSet.begin()==pSet.end())
return;
1476 startITR start_itr( pSet.begin() );
1477 while ( start_itr != pSet.end() ) {
1481 msg() <<
"\t" <<
m_names.at((*start_itr)->proxy()) <<
": "
1482 << (*start_itr)->range()->start()<<
" <- removed"<<endl;
1484 proxiesToReset.insert( (*start_itr)->proxy() );
1486 (*start_itr)->setRemovedStart(
true );
1487 pSet.erase(start_itr++);
1504 std::set<SG::DataProxy*, SortDPptr> &proxiesToReset,
1505 const IOVTime& curTime)
const {
1507 if (pSet.begin()==pSet.end())
return;
1512 stopITR stop_itr(pSet.begin());
1513 while ( stop_itr != pSet.end() ) {
1517 msg() <<
" " <<
m_names.at((*stop_itr)->proxy()) <<
": "
1518 << (*stop_itr)->range()->stop()<<
" -> removed"<<endl;
1520 proxiesToReset.insert( (*stop_itr)->proxy() );
1522 (*stop_itr)->setRemovedStop(
true );
1523 pSet.erase(stop_itr++);
1551 if (
proxy ==
nullptr) {
1575 ObjMap::const_iterator oitr =
m_objMap.find( ia );
1618 std::string fullname, tname;
1619 if (
p_CLIDSvc->getTypeNameOfID( clid, tname ).isFailure()) {