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,10 +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)
55+ size_t minSize = -1ULL ;
56+ size_t maxSize = 0 ;
5157 int queries = 0 ;
5258 int fetches = 0 ;
5359 int failures = 0 ;
54- 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+ }
5566 void clear ()
5667 {
5768 noCleanupPtr = nullptr ;
@@ -68,6 +79,7 @@ class CCDBManagerInstance
6879 CCDBManagerInstance (std::string const & path) : mCCDBAccessor {}
6980 {
7081 mCCDBAccessor .init (path);
82+ mDeplMode = o2::framework::DefaultsHelpers::deploymentMode ();
7183 }
7284 // / set a URL to query from
7385 void setURL (const std::string& url);
@@ -99,6 +111,9 @@ class CCDBManagerInstance
99111 return getForTimeStamp<T>(path, timestamp);
100112 }
101113
114+ // / detect online processing modes (i.e. CCDB objects may be updated in the lifetime of the manager)
115+ bool isOnline () const { return mDeplMode == o2::framework::DeploymentMode::OnlineAUX || mDeplMode == o2::framework::DeploymentMode::OnlineDDS || mDeplMode == o2::framework::DeploymentMode::OnlineECS; }
116+
102117 // / retrieve an object of type T from CCDB as stored under path; will use the timestamp member
103118 template <typename T>
104119 T* get (std::string const & path)
@@ -132,7 +147,7 @@ class CCDBManagerInstance
132147 if (!isCachingEnabled ()) {
133148 return false ;
134149 }
135- return mCache [path].isValid (timestamp);
150+ return ( mCheckObjValidityEnabled && mCache [path].isValid (timestamp)) || mCache [path]. isCacheValid (timestamp); // use stricter check
136151 }
137152
138153 // / check if checks of object validity before CCDB query is enabled
@@ -165,17 +180,19 @@ class CCDBManagerInstance
165180 void setFatalWhenNull (bool b) { mFatalWhenNull = b; }
166181
167182 // / A convenience function for MC to fetch
168- // / valid start and end timestamps given an ALICE run number.
169- // / On error it fatals (if fatal == true) or else returns the pair -1, -1.
170- std::pair<int64_t , int64_t > getRunDuration (int runnumber, bool fatal = true ) const ;
171-
172- // / A convenience function for MC to fetch
173- // / valid start and end timestamps given an ALICE run number.
183+ // / valid start and end timestamps for recorded TF data given an ALICE run number.
184+ // / In absence of STF/ETF fields in the RCT with fall back to CTP SOX/EOX then to
185+ // / ECS SOR/EOR.
174186 // / On error it fatals (if fatal == true) or else returns the pair -1, -1.
187+ std::pair<int64_t , int64_t > getRunDuration (int runnumber, bool fatal = true );
175188 static std::pair<int64_t , int64_t > getRunDuration (o2::ccdb::CcdbApi const & api, int runnumber, bool fatal = true );
176189
177190 std::string getSummaryString () const ;
178191
192+ size_t getFetchedSize () const { return mFetchedSize ; }
193+
194+ void report (bool longrep = false );
195+
179196 void endOfStream ();
180197
181198 private:
@@ -190,14 +207,15 @@ class CCDBManagerInstance
190207 bool mCanDefault = false ; // whether default is ok --> useful for testing purposes done standalone/isolation
191208 bool mCachingEnabled = true ; // whether caching is enabled
192209 bool mCheckObjValidityEnabled = false ; // wether the validity of cached object is checked before proceeding to a CCDB API query
210+ bool mFatalWhenNull = true ; // if nullptr blob replies should be treated as fatal (can be set by user)
193211 long mCreatedNotAfter = 0 ; // upper limit for object creation timestamp (TimeMachine mode) - If-Not-After HTTP header
194212 long mCreatedNotBefore = 0 ; // lower limit for object creation timestamp (TimeMachine mode) - If-Not-Before HTTP header
195213 long mTimerMS = 0 ; // timer for queries
196- bool mFatalWhenNull = true ; // if nullptr blob replies should be treated as fatal (can be set by user)
214+ size_t mFetchedSize = 0 ; // total fetched size
197215 int mQueries = 0 ; // total number of object queries
198216 int mFetches = 0 ; // total number of succesful fetches from CCDB
199217 int mFailures = 0 ; // total number of failed fetches
200-
218+ o2::framework::DeploymentMode mDeplMode ; // O2 deployment mode
201219 ClassDefNV (CCDBManagerInstance, 1 );
202220};
203221
@@ -218,11 +236,16 @@ T* CCDBManagerInstance::getForTimeStamp(std::string const& path, long timestamp)
218236 mFailures ++;
219237 } else {
220238 mFetches ++;
239+ auto sh = mHeaders .find (" fileSize" );
240+ if (sh != mHeaders .end ()) {
241+ size_t s = atol (sh->second .c_str ());
242+ mFetchedSize += s;
243+ }
221244 }
222245 } else {
223246 auto & cached = mCache [path];
224- if ( mCheckObjValidityEnabled && cached.isValid (timestamp)) {
225- cached.queries ++;
247+ cached.queries ++;
248+ if ((! isOnline () && cached.isCacheValid (timestamp)) || ( mCheckObjValidityEnabled && cached. isValid (timestamp))) {
226249 return reinterpret_cast <T*>(cached.noCleanupPtr ? cached.noCleanupPtr : cached.objPtr .get ());
227250 }
228251 ptr = mCCDBAccessor .retrieveFromTFileAny <T>(path, mMetaData , timestamp, &mHeaders , cached.uuid ,
@@ -238,17 +261,41 @@ T* CCDBManagerInstance::getForTimeStamp(std::string const& path, long timestamp)
238261 cached.objPtr .reset (ptr);
239262 }
240263 cached.uuid = mHeaders [" ETag" ];
241- try { // this conversion can throw, better to catch immediately
242- cached.startvalidity = std::stol (mHeaders [" Valid-From" ]);
243- cached.endvalidity = std::stol (mHeaders [" Valid-Until" ]);
264+
265+ try {
266+ if (mHeaders .find (" Valid-From" ) != mHeaders .end ()) {
267+ cached.startvalidity = std::stol (mHeaders [" Valid-From" ]);
268+ } else {
269+ // if meta-information missing assume infinit validity
270+ // (should happen only for locally created objects)
271+ cached.startvalidity = 0 ;
272+ }
273+ if (mHeaders .find (" Valid-Until" ) != mHeaders .end ()) {
274+ cached.endvalidity = std::stol (mHeaders [" Valid-Until" ]);
275+ } else {
276+ cached.endvalidity = std::numeric_limits<long >::max ();
277+ }
278+ cached.cacheValidFrom = timestamp;
244279 } catch (std::exception const & e) {
245280 reportFatal (" Failed to read validity from CCDB response (Valid-From : " + mHeaders [" Valid-From" ] + std::string (" Valid-Until: " ) + mHeaders [" Valid-Until" ] + std::string (" )" ));
246281 }
282+ auto sh = mHeaders .find (" fileSize" );
283+ if (sh != mHeaders .end ()) {
284+ size_t s = atol (sh->second .c_str ());
285+ mFetchedSize += s;
286+ cached.minSize = std::min (s, cached.minSize );
287+ cached.maxSize = std::max (s, cached.minSize );
288+ }
247289 } else if (mHeaders .count (" Error" )) { // in case of errors the pointer is 0 and headers["Error"] should be set
248290 cached.failures ++;
249291 cached.clear (); // in case of any error clear cache for this object
250- } else { // the old object is valid
251- ptr = reinterpret_cast <T*>(cached.noCleanupPtr ? cached.noCleanupPtr : cached.objPtr .get ());
292+ }
293+ // the old object is valid, fetch cache end of validity
294+ ptr = reinterpret_cast <T*>(cached.noCleanupPtr ? cached.noCleanupPtr : cached.objPtr .get ());
295+ if (mHeaders .find (" Cache-Valid-Until" ) != mHeaders .end ()) {
296+ cached.cacheValidUntil = std::stol (mHeaders [" Cache-Valid-Until" ]);
297+ } else {
298+ cached.cacheValidUntil = -1 ;
252299 }
253300 mHeaders .clear ();
254301 mMetaData .clear ();
0 commit comments