|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the Apache 2.0 License. |
| 3 | +#pragma once |
| 4 | + |
| 5 | +#include "snapshots/filenames.h" |
| 6 | +#include "timer.h" |
| 7 | + |
| 8 | +#include <filesystem> |
| 9 | +#include <string> |
| 10 | + |
| 11 | +namespace asynchost |
| 12 | +{ |
| 13 | + class SnapshotCleanupImpl |
| 14 | + { |
| 15 | + private: |
| 16 | + std::filesystem::path dir; |
| 17 | + size_t max_retained; |
| 18 | + |
| 19 | + struct CleanupWork |
| 20 | + { |
| 21 | + std::filesystem::path dir; |
| 22 | + size_t max_retained; |
| 23 | + }; |
| 24 | + |
| 25 | + static void cleanup_old_snapshots( |
| 26 | + const std::filesystem::path& dir, size_t max_retained) |
| 27 | + { |
| 28 | + std::vector<std::filesystem::path> directories{dir}; |
| 29 | + decltype(snapshots::find_committed_snapshots_in_directories( |
| 30 | + directories)) committed; |
| 31 | + try |
| 32 | + { |
| 33 | + committed = |
| 34 | + snapshots::find_committed_snapshots_in_directories(directories); |
| 35 | + } |
| 36 | + catch (const std::filesystem::filesystem_error& e) |
| 37 | + { |
| 38 | + LOG_FAIL_FMT( |
| 39 | + "Failed to list committed snapshots in {}: {}", dir, e.what()); |
| 40 | + return; |
| 41 | + } |
| 42 | + catch (const std::exception& e) |
| 43 | + { |
| 44 | + LOG_FAIL_FMT( |
| 45 | + "Unexpected error while listing committed snapshots in {}: {}", |
| 46 | + dir, |
| 47 | + e.what()); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + if (committed.size() > max_retained) |
| 52 | + { |
| 53 | + // committed is sorted descending by snapshot index, so the |
| 54 | + // oldest are at the end |
| 55 | + for (auto it = committed.rbegin(); |
| 56 | + it != committed.rend() - max_retained; |
| 57 | + ++it) |
| 58 | + { |
| 59 | + const auto& path = it->second; |
| 60 | + LOG_INFO_FMT( |
| 61 | + "Deleting old snapshot {} (retaining {})", |
| 62 | + path.filename(), |
| 63 | + max_retained); |
| 64 | + std::error_code ec; |
| 65 | + std::filesystem::remove(path, ec); |
| 66 | + if (ec) |
| 67 | + { |
| 68 | + LOG_FAIL_FMT( |
| 69 | + "Failed to delete old snapshot {}: {}", |
| 70 | + path.filename(), |
| 71 | + ec.message()); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + static void on_cleanup_work(uv_work_t* req) |
| 78 | + { |
| 79 | + auto* work = static_cast<CleanupWork*>(req->data); |
| 80 | + cleanup_old_snapshots(work->dir, work->max_retained); |
| 81 | + } |
| 82 | + |
| 83 | + static void on_cleanup_work_done(uv_work_t* req, int /*status*/) |
| 84 | + { |
| 85 | + auto* work = static_cast<CleanupWork*>(req->data); |
| 86 | + LOG_DEBUG_FMT("Snapshot cleanup completed"); |
| 87 | + delete work; // NOLINT(cppcoreguidelines-owning-memory) |
| 88 | + delete req; // NOLINT(cppcoreguidelines-owning-memory) |
| 89 | + } |
| 90 | + |
| 91 | + public: |
| 92 | + SnapshotCleanupImpl(const std::string& dir_, size_t max_retained_) : |
| 93 | + dir(dir_), |
| 94 | + max_retained(max_retained_) |
| 95 | + { |
| 96 | + if (max_retained < 1) |
| 97 | + { |
| 98 | + throw std::logic_error(fmt::format( |
| 99 | + "files_cleanup.max_snapshots must be at least 1, got {}", |
| 100 | + max_retained)); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + void on_timer() |
| 105 | + { |
| 106 | + // NOLINTNEXTLINE(cppcoreguidelines-owning-memory) |
| 107 | + auto* work = new CleanupWork{.dir = dir, .max_retained = max_retained}; |
| 108 | + // NOLINTNEXTLINE(cppcoreguidelines-owning-memory) |
| 109 | + auto* req = new uv_work_t; |
| 110 | + req->data = work; |
| 111 | + int rc = uv_queue_work( |
| 112 | + uv_default_loop(), req, &on_cleanup_work, &on_cleanup_work_done); |
| 113 | + if (rc < 0) |
| 114 | + { |
| 115 | + LOG_FAIL_FMT( |
| 116 | + "Failed to queue snapshot cleanup work: {}", uv_strerror(rc)); |
| 117 | + delete work; // NOLINT(cppcoreguidelines-owning-memory) |
| 118 | + delete req; // NOLINT(cppcoreguidelines-owning-memory) |
| 119 | + } |
| 120 | + } |
| 121 | + }; |
| 122 | + |
| 123 | + using SnapshotCleanupTimer = proxy_ptr<Timer<SnapshotCleanupImpl>>; |
| 124 | +} |
0 commit comments