Skip to content
Merged
8 changes: 6 additions & 2 deletions projects/hrr/DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,12 @@ atomically renamed), so a crash never leaves a partial final blob.
On the read side, `load_archive` is append-only-aware: a torn record can only be
the last one, so a partial header/payload at the tail is a recovery point — all
complete records are kept and `Archive::truncated` is set, rather than failing
the whole load. `Archive::complete` reflects whether the trailer was found.
`hrr-playback --info` reports both; `hrr-playback --repair` rewrites a truncated
the whole load. The trailer ends the event stream. Records after it, which a
runtime older than the writer fix could leave from calls still in flight at
shutdown, are counted in `Archive::skipped_after_trailer`, reported once and
not replayed. `Archive::complete` is set when the trailer was found and its
`total_events` equals the number of records before it; a mismatch is reported
and leaves the archive incomplete. `hrr-playback --info` reports both; `hrr-playback --repair` rewrites a truncated
archive (trimmed to the last complete record, trailer + manifest added) into a
clean one.
Comment thread
andraghetti marked this conversation as resolved.
Resolved

Expand Down
34 changes: 30 additions & 4 deletions projects/hrr/playback/hrr_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,12 @@ bool load_archive(const std::string& path, Archive& archive) {
// Read events sequentially. read_raw_record handles the framing and the
// torn-tail recovery; a torn record means the capture was interrupted, and
// everything parsed before it is kept. The clean-shutdown trailer
// (hrr_eof_record) explicitly marks a whole archive.
// (hrr_eof_record) explicitly marks a whole archive, and it ends the event
// stream.
bool truncated = false;
bool complete = false;
bool trailer = false;
uint64_t trailer_count = 0;
const uint16_t hdr_size = static_cast<uint16_t>(sizeof(hrr_event_header));
while (true) {
Event ev;
Expand All @@ -349,8 +352,9 @@ bool load_archive(const std::string& path, Archive& archive) {
total == static_cast<uint32_t>(sizeof(hrr_eof_record))) {
const auto* er = reinterpret_cast<const hrr_eof_record*>(ev.raw_payload.data());
if (er->eof_magic == HRR_EOF_MAGIC) {
complete = true;
continue; // do not append trailer as a replay event
trailer = true;
trailer_count = er->total_events;
break; // the trailer is not a replay event, and nothing after it is
}
}

Expand Down Expand Up @@ -598,11 +602,33 @@ bool load_archive(const std::string& path, Archive& archive) {
archive.events.push_back(std::move(ev));
}

// A runtime older than the writer fix for it could append a few records
// after the trailer, from calls still in flight at shutdown. They are not in
// the trailer's count, so they are counted and left out of the replay.
if (trailer) {
std::vector<uint8_t> rest;
while (read_raw_record(f, rest) == RecordStatus::Ok)
++archive.skipped_after_trailer;
if (archive.skipped_after_trailer)
fprintf(stderr,
"[HRR] Ignored %zu records after the clean-shutdown trailer\n",
archive.skipped_after_trailer);
}

fclose(f);

if (trailer) {
complete = trailer_count == archive.events.size();
Comment thread
andraghetti marked this conversation as resolved.
Resolved
if (!complete)
fprintf(stderr,
"[HRR] Clean-shutdown trailer counts %llu events but %zu precede it; "
"the archive is not treated as complete\n",
static_cast<unsigned long long>(trailer_count), archive.events.size());
}

archive.complete = complete;
archive.truncated = truncated;
if (!complete) {
if (!trailer) {
fprintf(stderr,
"[HRR] Archive has no clean-shutdown trailer (capture likely crashed); "
"recovered %zu events%s\n",
Expand Down
8 changes: 6 additions & 2 deletions projects/hrr/playback/hrr_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,18 @@ struct Archive {
std::vector<Event> events;

// Crash-resilience status, set by load_archive:
// complete — the clean-shutdown trailer (hrr_eof_record) was found, so the
// capturing process exited normally and the archive is whole.
// complete — the clean-shutdown trailer (hrr_eof_record) was found and
// counts the records before it, so the capturing process
// exited normally and the archive is whole.
Comment thread
andraghetti marked this conversation as resolved.
Resolved
// truncated — a torn trailing record was detected and discarded; all
// complete records before it were recovered. Replay still works.
// A capture interrupted by a crash typically has complete=false; it may also
// have truncated=true if the final record was only partially written.
bool complete = false;
bool truncated = false;
// Whole records found after the trailer. They are not replayed, and the
// trailer's event count must match the records before it for complete=true.
size_t skipped_after_trailer = 0;

// Content-addressed blobs: hash_hex -> file path
std::unordered_map<std::string, std::string> blobs;
Expand Down
48 changes: 48 additions & 0 deletions projects/hrr/tests/unit/hrr_recovery_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,54 @@ HRR_TEST_CASE(Unit_HRR_Recovery_NoTrailer) {
CHECK_FALSE(a.truncated);
}

/**
* Test Description
* ----------------
* - N records, a clean trailer that counts them, then two more records, as a
* runtime that kept writing after the trailer at shutdown could leave. The
* reader stops at the trailer: the archive is complete with exactly the N
* records, and the two after it are counted and not replayed.
*/
HRR_TEST_CASE(Unit_HRR_Recovery_RecordAfterTrailer) {
TmpArchive arc("after_trailer");
arc.write_records(5);
arc.write_trailer(5);
for (uint64_t seq : {100u, 101u}) {
hrr_event_header h = make_min_record(seq);
arc.write_bytes(&h, sizeof(h));
}
arc.finish();

hrr::Archive a;
REQUIRE(hrr::load_archive(arc.path(), a));
CHECK(a.events.size() == 5);
CHECK(a.complete);
CHECK_FALSE(a.truncated);
CHECK(a.skipped_after_trailer == 2);
for (const auto& ev : a.events) CHECK(ev.header().sequence_id < 100);
}

/**
* Test Description
* ----------------
* - N records followed by a trailer that claims a different count. The
* trailer still ends the stream, but the archive is not treated as a clean
* complete capture, so --repair rewrites it rather than trusting it.
*/
HRR_TEST_CASE(Unit_HRR_Recovery_TrailerCountMismatch) {
TmpArchive arc("count_mismatch");
arc.write_records(5);
arc.write_trailer(4);
arc.finish();

hrr::Archive a;
REQUIRE(hrr::load_archive(arc.path(), a));
CHECK(a.events.size() == 5);
CHECK_FALSE(a.complete);
CHECK_FALSE(a.truncated);
CHECK(a.skipped_after_trailer == 0);
}

/**
* Test Description
* ----------------
Expand Down
Loading