1818#include " CCDB/CcdbApi.h"
1919#include " CCDB/CCDBTimeStampUtils.h"
2020#include " CommonUtils/NameConf.h"
21+ #include " Framework/DataTakingContext.h"
22+ #include " Framework/DefaultsHelpers.h"
2123#include < string>
2224#include < chrono>
2325#include < map>
@@ -48,12 +50,19 @@ class CCDBManagerInstance
4850 std::string uuid;
4951 long startvalidity = 0 ;
5052 long endvalidity = -1 ;
53+ long cacheValidFrom = 0 ; // time for which the object was cached
54+ long cacheValidUntil = -1 ; // object is guaranteed to be valid till this time (modulo new updates)
5155 size_t minSize = -1ULL ;
5256 size_t maxSize = 0 ;
5357 int queries = 0 ;
5458 int fetches = 0 ;
5559 int failures = 0 ;
56- bool isValid (long ts) { return ts < endvalidity && ts > startvalidity; }
60+ bool isValid (long ts) { return ts < endvalidity && ts >= startvalidity; }
61+ bool isCacheValid (long ts)
62+ {
63+ LOGP (debug, " isCacheValid : {} : {} : {} --> {}" , cacheValidFrom, ts, cacheValidUntil, ts < cacheValidUntil && ts >= cacheValidFrom);
64+ return ts < cacheValidUntil && ts >= cacheValidFrom;
65+ }
5766 void clear ()
5867 {
5968 noCleanupPtr = nullptr ;
@@ -70,6 +79,7 @@ class CCDBManagerInstance
7079 CCDBManagerInstance (std::string const & path) : mCCDBAccessor {}
7180 {
7281 mCCDBAccessor .init (path);
82+ mDeplMode = o2::framework::DefaultsHelpers::deploymentMode ();
7383 }
7484 // / set a URL to query from
7585 void setURL (const std::string& url);
@@ -92,6 +102,10 @@ class CCDBManagerInstance
92102 template <typename T>
93103 T* getForTimeStamp (std::string const & path, long timestamp);
94104
105+ // / retrieve an object of type T from CCDB as stored under path and using the timestamp in the middle of the run
106+ template <typename T>
107+ T* getForRun (std::string const & path, int runNumber, bool setRunMetadata = false );
108+
95109 // / retrieve an object of type T from CCDB as stored under path, timestamp and metaData
96110 template <typename T>
97111 T* getSpecific (std::string const & path, long timestamp = -1 , MD metaData = MD())
@@ -101,6 +115,13 @@ class CCDBManagerInstance
101115 return getForTimeStamp<T>(path, timestamp);
102116 }
103117
118+ // / retrieve an object of type T from CCDB as stored under path and using the timestamp in the middle of the run + metadata. The run number is provided separately to conform to typical analysis use (in which case metadata does not include runNumber)
119+ template <typename T>
120+ T* getSpecificForRun (std::string const & path, int runNumber, MD metaData = MD());
121+
122+ // / detect online processing modes (i.e. CCDB objects may be updated in the lifetime of the manager)
123+ bool isOnline () const { return mDeplMode == o2::framework::DeploymentMode::OnlineAUX || mDeplMode == o2::framework::DeploymentMode::OnlineDDS || mDeplMode == o2::framework::DeploymentMode::OnlineECS; }
124+
104125 // / retrieve an object of type T from CCDB as stored under path; will use the timestamp member
105126 template <typename T>
106127 T* get (std::string const & path)
@@ -134,7 +155,7 @@ class CCDBManagerInstance
134155 if (!isCachingEnabled ()) {
135156 return false ;
136157 }
137- return mCache [path].isValid (timestamp);
158+ return ( mCheckObjValidityEnabled && mCache [path].isValid (timestamp)) || mCache [path]. isCacheValid (timestamp); // use stricter check
138159 }
139160
140161 // / check if checks of object validity before CCDB query is enabled
@@ -167,15 +188,13 @@ class CCDBManagerInstance
167188 void setFatalWhenNull (bool b) { mFatalWhenNull = b; }
168189
169190 // / A convenience function for MC to fetch
170- // / valid start and end timestamps given an ALICE run number.
171- // / On error it fatals (if fatal == true) or else returns the pair -1, -1.
172- std::pair<int64_t , int64_t > getRunDuration (int runnumber, bool fatal = true ) const ;
173-
174- // / A convenience function for MC to fetch
175- // / valid start and end timestamps given an ALICE run number.
191+ // / valid start and end timestamps for recorded TF data given an ALICE run number.
192+ // / In absence of STF/ETF fields in the RCT with fall back to CTP SOX/EOX then to
193+ // / ECS SOR/EOR.
176194 // / On error it fatals (if fatal == true) or else returns the pair -1, -1.
195+ std::pair<int64_t , int64_t > getRunDuration (int runnumber, bool fatal = true );
177196 static std::pair<int64_t , int64_t > getRunDuration (o2::ccdb::CcdbApi const & api, int runnumber, bool fatal = true );
178-
197+ static std::pair< int64_t , int64_t > getRunDuration ( const MD& headers);
179198 std::string getSummaryString () const ;
180199
181200 size_t getFetchedSize () const { return mFetchedSize ; }
@@ -204,7 +223,7 @@ class CCDBManagerInstance
204223 int mQueries = 0 ; // total number of object queries
205224 int mFetches = 0 ; // total number of succesful fetches from CCDB
206225 int mFailures = 0 ; // total number of failed fetches
207-
226+ o2::framework::DeploymentMode mDeplMode ; // O2 deployment mode
208227 ClassDefNV (CCDBManagerInstance, 1 );
209228};
210229
@@ -234,7 +253,7 @@ T* CCDBManagerInstance::getForTimeStamp(std::string const& path, long timestamp)
234253 } else {
235254 auto & cached = mCache [path];
236255 cached.queries ++;
237- if (mCheckObjValidityEnabled && cached.isValid (timestamp)) {
256+ if ((! isOnline () && cached. isCacheValid (timestamp)) || ( mCheckObjValidityEnabled && cached.isValid (timestamp) )) {
238257 return reinterpret_cast <T*>(cached.noCleanupPtr ? cached.noCleanupPtr : cached.objPtr .get ());
239258 }
240259 ptr = mCCDBAccessor .retrieveFromTFileAny <T>(path, mMetaData , timestamp, &mHeaders , cached.uuid ,
@@ -250,6 +269,7 @@ T* CCDBManagerInstance::getForTimeStamp(std::string const& path, long timestamp)
250269 cached.objPtr .reset (ptr);
251270 }
252271 cached.uuid = mHeaders [" ETag" ];
272+
253273 try {
254274 if (mHeaders .find (" Valid-From" ) != mHeaders .end ()) {
255275 cached.startvalidity = std::stol (mHeaders [" Valid-From" ]);
@@ -263,6 +283,7 @@ T* CCDBManagerInstance::getForTimeStamp(std::string const& path, long timestamp)
263283 } else {
264284 cached.endvalidity = std::numeric_limits<long >::max ();
265285 }
286+ cached.cacheValidFrom = timestamp;
266287 } catch (std::exception const & e) {
267288 reportFatal (" Failed to read validity from CCDB response (Valid-From : " + mHeaders [" Valid-From" ] + std::string (" Valid-Until: " ) + mHeaders [" Valid-Until" ] + std::string (" )" ));
268289 }
@@ -276,8 +297,13 @@ T* CCDBManagerInstance::getForTimeStamp(std::string const& path, long timestamp)
276297 } else if (mHeaders .count (" Error" )) { // in case of errors the pointer is 0 and headers["Error"] should be set
277298 cached.failures ++;
278299 cached.clear (); // in case of any error clear cache for this object
279- } else { // the old object is valid
280- ptr = reinterpret_cast <T*>(cached.noCleanupPtr ? cached.noCleanupPtr : cached.objPtr .get ());
300+ }
301+ // the old object is valid, fetch cache end of validity
302+ ptr = reinterpret_cast <T*>(cached.noCleanupPtr ? cached.noCleanupPtr : cached.objPtr .get ());
303+ if (mHeaders .find (" Cache-Valid-Until" ) != mHeaders .end ()) {
304+ cached.cacheValidUntil = std::stol (mHeaders [" Cache-Valid-Until" ]);
305+ } else {
306+ cached.cacheValidUntil = -1 ;
281307 }
282308 mHeaders .clear ();
283309 mMetaData .clear ();
@@ -293,6 +319,27 @@ T* CCDBManagerInstance::getForTimeStamp(std::string const& path, long timestamp)
293319 return ptr;
294320}
295321
322+ template <typename T>
323+ T* CCDBManagerInstance::getForRun (std::string const & path, int runNumber, bool setRunMetadata)
324+ {
325+ auto metaData = setRunMetadata ? MD{{" runNumber" , std::to_string (runNumber)}} : MD{};
326+ mMetaData = metaData;
327+ return getSpecificForRun<T>(path, runNumber, metaData);
328+ }
329+
330+ template <typename T>
331+ T* CCDBManagerInstance::getSpecificForRun (std::string const & path, int runNumber, MD metaData)
332+ {
333+ auto [start, stop] = getRunDuration (runNumber);
334+ if (start < 0 || stop < 0 ) {
335+ if (mFatalWhenNull ) {
336+ reportFatal (std::string (" Failed to get run duration for run " ) + std::to_string (runNumber));
337+ }
338+ return nullptr ;
339+ }
340+ return getSpecific<T>(path, start / 2 + stop / 2 , metaData);
341+ }
342+
296343class BasicCCDBManager : public CCDBManagerInstance
297344{
298345 public:
0 commit comments