diff --git a/CCDB/src/CcdbApi.cxx b/CCDB/src/CcdbApi.cxx index 910e4c8f9d454..b7517c95cf966 100644 --- a/CCDB/src/CcdbApi.cxx +++ b/CCDB/src/CcdbApi.cxx @@ -1243,7 +1243,7 @@ void* CcdbApi::retrieveFromTFile(std::type_info const& tinfo, std::string const& snapshoting = true; out << "CCDB-access[" << getpid() << "] ... " << mUniqueAgentID << " downloading to snapshot " << snapshotfile << "\n"; // if file not already here and valid --> snapshot it - if (!retrieveBlob(path, mSnapshotCachePath, metadata, timestamp)) { + if (!retrieveBlob(path, mSnapshotCachePath, metadata, timestamp, true, "snapshot.root", createdNotAfter, createdNotBefore)) { out << "CCDB-access[" << getpid() << "] ... " << mUniqueAgentID << " failed to create directory for " << snapshotfile << "\n"; } } else { diff --git a/CCDB/test/testCcdbApi.cxx b/CCDB/test/testCcdbApi.cxx index 2723d14caab75..1dd1b933afc17 100644 --- a/CCDB/test/testCcdbApi.cxx +++ b/CCDB/test/testCcdbApi.cxx @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -461,6 +462,48 @@ BOOST_AUTO_TEST_CASE(TestFetchingHeaders, *utf::precondition(if_reachable())) BOOST_CHECK_EQUAL(updated, false); } +// The created-not-after limit must also apply when the object is fetched into the local snapshot cache +BOOST_AUTO_TEST_CASE(snapshot_cache_respects_created_not_after, *utf::precondition(if_reachable())) +{ + test_fixture f; + const std::string path = basePath + "CreatedNotAfter"; + const long timestamp = 1000; + const long endvalidity = 2000; + + o2::ccdb::IdPath v1; + v1.setPath("Test/Version/one"); + f.api.storeAsTFileAny(&v1, path, f.metadata, timestamp, endvalidity); + auto headers = f.api.retrieveHeaders(path, f.metadata, timestamp); + BOOST_REQUIRE(headers.count("Created") == 1); + const std::string notAfter = std::to_string(std::stol(headers["Created"]) + 1); + + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + o2::ccdb::IdPath v2; + v2.setPath("Test/Version/two"); + f.api.storeAsTFileAny(&v2, path, f.metadata, timestamp, endvalidity); + + // without a local cache + auto direct = f.api.retrieveFromTFileAny(path, f.metadata, timestamp, nullptr, "", notAfter); + BOOST_REQUIRE(direct != nullptr); + BOOST_CHECK_EQUAL(std::string(direct->getPathString().Data()), "Test/Version/one"); + + // with a local cache + auto cachedir = o2::utils::Str::create_unique_path(std::filesystem::temp_directory_path().native()); + std::filesystem::create_directories(cachedir); + setenv("ALICEO2_CCDB_LOCALCACHE", cachedir.c_str(), 1); + CcdbApi cachedApi; + cachedApi.init(ccdbUrl); + unsetenv("ALICEO2_CCDB_LOCALCACHE"); + auto cached = cachedApi.retrieveFromTFileAny(path, f.metadata, timestamp, nullptr, "", notAfter); + BOOST_REQUIRE(cached != nullptr); + BOOST_CHECK_EQUAL(std::string(cached->getPathString().Data()), "Test/Version/one"); + // the snapshot itself holds the version that was created before the limit + auto fromSnapshot = cachedApi.retrieveFromTFileAny(path, f.metadata, timestamp); + BOOST_REQUIRE(fromSnapshot != nullptr); + BOOST_CHECK_EQUAL(std::string(fromSnapshot->getPathString().Data()), "Test/Version/one"); + std::filesystem::remove_all(cachedir); +} + BOOST_AUTO_TEST_CASE(TestRetrieveHeaders, *utf::precondition(if_reachable())) { test_fixture f;