From 4a774f428af75af47558e3c14f9fb963a88af2b9 Mon Sep 17 00:00:00 2001 From: James Harrison <00jamesh@gmail.com> Date: Mon, 14 Aug 2023 12:09:04 +0100 Subject: [PATCH] Assume malformed head chunk is partially written data Head chunks and the WAL may not be completely flushed to disk if e.g., Prometheus crashes. Partial records may exist at the end of the file; allow this and assume there is not further valid data. A future change should validate CRCs also, and stop reading on the first incorrect CRC. --- src/pdu/block/chunk_reference.cc | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/pdu/block/chunk_reference.cc b/src/pdu/block/chunk_reference.cc index 543719d..cccf209 100644 --- a/src/pdu/block/chunk_reference.cc +++ b/src/pdu/block/chunk_reference.cc @@ -43,9 +43,15 @@ std::optional> readHeadChunkMeta( size_t dataLen = dec.read_varuint(); - // skip data and 4 byte crc, leaving decoder at the start of the next - // chunk meta header. - dec.seek(dataLen + 4, std::ios_base::cur); + try { + // skip data and 4 byte crc, leaving decoder at the start of the next + // chunk meta header. + dec.seek(dataLen + 4, std::ios_base::cur); + } catch (const std::exception&) { + // if we fail to seek that far, assume that this file was partially + // written. There should be no later valid entries + return {}; + } return {{seriesRef, std::move(ref)}}; } \ No newline at end of file