Skip to content

Commit b1eac92

Browse files
jjerphanKlaim
andcommitted
Address review comments
Signed-off-by: Julien Jerphanion <git@jjerphan.xyz> Co-authored-by: Klaim <Klaim@users.noreply.github.com>
1 parent ac400e2 commit b1eac92

File tree

4 files changed

+45
-35
lines changed

4 files changed

+45
-35
lines changed

libmamba/include/mamba/core/shard_index_loader.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
#ifndef MAMBA_CORE_SHARD_INDEX_LOADER_HPP
88
#define MAMBA_CORE_SHARD_INDEX_LOADER_HPP
99

10+
#include <cstdint>
1011
#include <optional>
12+
#include <string_view>
1113

1214
#include "mamba/core/error_handling.hpp"
1315
#include "mamba/core/shard_types.hpp"
@@ -21,6 +23,14 @@
2123

2224
namespace mamba
2325
{
26+
/**
27+
* Compute shard index cache age in seconds from the file modification time.
28+
*
29+
* On failure (e.g. file missing or inaccessible), logs at debug level and returns nullopt.
30+
*/
31+
std::optional<std::int64_t>
32+
shard_index_cache_age_seconds(const fs::u8path& cache_path, std::string_view subdir_name);
33+
2434
/**
2535
* Fetch and parse shard index from repodata_shards.msgpack.zst.
2636
*

libmamba/src/core/shard_index_loader.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// The full license is in the file LICENSE, distributed with this software.
66

77
#include <chrono>
8+
#include <cstdint>
89
#include <fstream>
910
#include <iostream>
1011
#include <map>
@@ -286,6 +287,27 @@ namespace
286287

287288
namespace mamba
288289
{
290+
std::optional<std::int64_t>
291+
shard_index_cache_age_seconds(const fs::u8path& cache_path, std::string_view subdir_name)
292+
{
293+
try
294+
{
295+
const fs::file_time_type last_write = fs::last_write_time(cache_path);
296+
const fs::file_time_type now = fs::file_time_type::clock::now();
297+
const std::int64_t age_sec = std::chrono::duration_cast<std::chrono::seconds>(
298+
now - last_write
299+
)
300+
.count();
301+
return age_sec;
302+
}
303+
catch (const std::exception& e)
304+
{
305+
LOG_DEBUG << "Could not check shard index cache age for " << subdir_name << ": "
306+
<< e.what();
307+
return std::nullopt;
308+
}
309+
}
310+
289311
auto ShardIndexLoader::shard_index_cache_path(const SubdirIndexLoader& subdir) -> fs::u8path
290312
{
291313
// Use similar naming as repodata.json cache but with .msgpack.zst extension
@@ -510,18 +532,15 @@ namespace mamba
510532
fs::u8path cache_path = shard_index_cache_path(subdir);
511533
if (fs::exists(cache_path) && shards_ttl > 0)
512534
{
513-
try
535+
if (auto age_sec = shard_index_cache_age_seconds(cache_path, subdir.name()))
514536
{
515-
auto last_write = fs::last_write_time(cache_path);
516-
auto now = fs::file_time_type::clock::now();
517-
auto age = std::chrono::duration_cast<std::chrono::seconds>(now - last_write).count();
518-
if (age >= 0 && static_cast<std::size_t>(age) <= shards_ttl)
537+
if (*age_sec >= 0 && static_cast<std::size_t>(*age_sec) <= shards_ttl)
519538
{
520539
auto cached_index = parse_shard_index(cache_path);
521540
if (cached_index.has_value())
522541
{
523542
LOG_DEBUG << "Using cached shard index for " << subdir.name()
524-
<< " (within TTL, " << age << "s old)";
543+
<< " (within TTL, " << *age_sec << "s old)";
525544
if (Console::can_report_status())
526545
{
527546
std::string label = "Using Cached Shard Index for " + subdir.name();
@@ -535,10 +554,6 @@ namespace mamba
535554
}
536555
}
537556
}
538-
catch (const std::exception& e)
539-
{
540-
LOG_DEBUG << "Could not check cache age for " << subdir.name() << ": " << e.what();
541-
}
542557
}
543558

544559
// Refresh shards availability (HEAD check) if metadata is stale for TTL, then re-check.

libmamba/src/core/subdir_index.cpp

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -527,22 +527,15 @@ namespace mamba
527527
{
528528
return;
529529
}
530-
try
530+
if (auto age_sec = shard_index_cache_age_seconds(cache_path, name()))
531531
{
532-
auto last_write = fs::last_write_time(cache_path);
533-
auto now = fs::file_time_type::clock::now();
534-
auto age = std::chrono::duration_cast<std::chrono::seconds>(now - last_write).count();
535-
if (age >= 0 && static_cast<std::size_t>(age) <= params.repodata_shards_ttl)
532+
if (*age_sec >= 0 && static_cast<std::size_t>(*age_sec) <= params.repodata_shards_ttl)
536533
{
537-
LOG_DEBUG << "Shard index cache valid for " << name() << " (" << age << "s old), "
538-
<< "skipping full repodata download";
534+
LOG_DEBUG << "Shard index cache valid for " << name() << " (" << *age_sec
535+
<< "s old), skipping full repodata download";
539536
set_shards_availability(true);
540537
}
541538
}
542-
catch (const std::exception& e)
543-
{
544-
LOG_DEBUG << "Could not check shard index cache age for " << name() << ": " << e.what();
545-
}
546539
}
547540

548541
void SubdirIndexLoader::clear_valid_cache_files()
@@ -876,16 +869,13 @@ namespace mamba
876869
fs::u8path cache_path = ShardIndexLoader::shard_index_cache_path(*this);
877870
if (fs::exists(cache_path))
878871
{
879-
try
872+
if (auto age_sec = shard_index_cache_age_seconds(cache_path, name()))
880873
{
881-
auto last_write = fs::last_write_time(cache_path);
882-
auto now = fs::file_time_type::clock::now();
883-
auto age = std::chrono::duration_cast<std::chrono::seconds>(now - last_write)
884-
.count();
885-
if (age >= 0 && static_cast<std::size_t>(age) <= params.repodata_shards_ttl)
874+
if (*age_sec >= 0
875+
&& static_cast<std::size_t>(*age_sec) <= params.repodata_shards_ttl)
886876
{
887877
LOG_DEBUG << "Skipping shards HEAD check for " << name()
888-
<< " (cached index within TTL, " << age << "s old)";
878+
<< " (cached index within TTL, " << *age_sec << "s old)";
889879
// Mark shards as available so load_subdir_with_shards is used
890880
set_shards_availability(true);
891881
}
@@ -896,10 +886,8 @@ namespace mamba
896886
);
897887
}
898888
}
899-
catch (const std::exception& e)
889+
else
900890
{
901-
LOG_DEBUG << "Could not check shard index cache age for " << name() << ": "
902-
<< e.what();
903891
request.push_back(
904892
ShardIndexLoader::build_shards_availability_check_request(*this)
905893
);

libmamba/tests/src/core/test_sharded_repodata_integration.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -967,10 +967,7 @@ TEST_CASE(
967967
"[mamba::core][sharded][.integration][!mayfail]"
968968
)
969969
{
970-
auto& ctx = mambatests::context();
971-
const bool original_offline = ctx.offline;
972-
on_scope_exit restore_offline{ [&] { ctx.offline = original_offline; } };
973-
970+
Context ctx;
974971
ctx.channels = { "https://prefix.dev/conda-forge" };
975972
ctx.repodata_use_shards = true;
976973

0 commit comments

Comments
 (0)