Check each backup set against the quota before uploading
C++ · C++ · intermediate · modification
The storage service accepts any upload and bills whatever goes past the account's plan, so the agent should stop a backup set that is over the quota before it sends a byte. This PR adds `SetSize` and `backup::measure` to `src/backup/plan.hpp`, implemented in `src/backup/plan.cpp` with a `std::accumulate` over the chunk lengths. `src/backup/agent.hpp` gains `RunStatus::over_quota`, and `Agent::run_once` in `src/backup/agent.cpp` measures the plan first: over the quota it logs a warning with both numbers and returns `over_quota` without uploading, otherwise it logs the set's size and chunk count and uploads as before. `tests/backup_quota_test.cpp` (new) runs `measure` on an empty plan and on a plan built from split files, and `run_once` with a fake uploader and log on a set at the quota and a set one byte over it.
C++20, standard library only, built with CMake (unchanged: it builds `src/backup/*.cpp` into the agent and every `tests/*_test.cpp` together with `tests/check_main.cpp` into the test runner) by Apple clang 17 and GCC 13 in CI, both with `-Wall -Wextra -Wconversion -Werror`. Targets are 64-bit Linux and macOS, where `unsigned int` is 32 bits and `std::size_t` is 64 bits. The backup agent is single-threaded: the scheduler calls `Agent::run_once` once an hour on its only thread. Existing and unchanged: the scanner builds the plan with `split_file`, which cuts each file (at most 16 TiB) into `Chunk`s of `max_chunk_bytes` (64 MiB), the last one shorter, so every chunk's `length` is from 1 to 67,108,864; a plan has at most 1,000,000 chunks, and customers' backup sets commonly run from a few GiB to several TiB. `AgentConfig::quota_bytes` (already loaded from `agent.toml` at startup) is the account's limit for one backup set, from 1 GiB to 16 TiB. `Uploader` and `Log` (`src/backup/uploader.hpp`) are abstract interfaces: `bool upload(const Chunk&)`, and `info`/`warn` taking a `std::string_view`. The scheduler only compares the returned status with `RunStatus::failed` (retry in 10 minutes); any other status waits for the next hourly run. `tests/check.hpp` is the repo's tiny test helper: `TEST(name) { ... }` registers a test, `CHECK(expr)` and `CHECK_EQ(a, b)` record a failure with file and line, and the `main` in `tests/check_main.cpp` runs every registered test and exits non-zero if any check failed.
Requirements
- New: `backup::measure(std::span<const Chunk> chunks)` returns a `SetSize` whose `bytes` is the exact sum of every chunk's `length`, as a `std::uint64_t`, and whose `chunks` is the number of chunks. An empty span gives 0 and 0.
- `Agent::run_once(plan)` (existing) measures the plan after its existing empty-plan check. When the set's `bytes` is greater than `config.quota_bytes`, it uploads nothing, logs one warning `backup set is N bytes, over the quota of Q bytes` and returns the new `RunStatus::over_quota`. A set exactly at the quota is uploaded. Otherwise it logs `uploading N bytes in K chunks` before the first upload and uploads as before. N, Q and K are plain decimal numbers.
- Unchanged: an empty plan returns `RunStatus::nothing_to_do` and logs nothing; chunks are uploaded in plan order; a failed upload logs `upload failed: PATH at OFFSET` and returns `RunStatus::failed` without uploading the rest; a finished upload logs `uploaded K chunks` and returns `RunStatus::uploaded`.
- Ship unit tests for `measure` and for the quota check in `run_once`, using the repo's `tests/check.hpp` helper.
Files touched
- src/backup/plan.hpp
- src/backup/plan.cpp
- src/backup/agent.hpp
- src/backup/agent.cpp
- tests/backup_quota_test.cpp