spacetimedb_commitlog/commit.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
use std::{
io::{self, Read, Write},
ops::Range,
};
use crc32c::{Crc32cReader, Crc32cWriter};
use spacetimedb_sats::buffer::{BufReader, Cursor, DecodeError};
use crate::{error::ChecksumMismatch, payload::Decoder, segment::CHECKSUM_ALGORITHM_CRC32C, Transaction};
pub struct Header {
pub min_tx_offset: u64,
pub n: u16,
pub len: u32,
}
impl Header {
pub const LEN: usize = /* offset */ 8 + /* n */ 2 + /* len */ 4;
/// Read [`Self::LEN`] bytes from `reader` and interpret them as the
/// "header" of a [`Commit`].
///
/// Returns `None` if:
///
/// - The reader cannot provide exactly [`Self::LEN`] bytes
///
/// I.e. it is at EOF
///
/// - Or, the read bytes are all zeroes
///
/// This is to allow preallocation of segments.
///
pub fn decode<R: Read>(mut reader: R) -> io::Result<Option<Self>> {
let mut hdr = [0; Self::LEN];
if let Err(e) = reader.read_exact(&mut hdr) {
if e.kind() == io::ErrorKind::UnexpectedEof {
return Ok(None);
}
return Err(e);
}
match &mut hdr.as_slice() {
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] => Ok(None),
buf => {
let min_tx_offset = buf.get_u64().map_err(decode_error)?;
let n = buf.get_u16().map_err(decode_error)?;
let len = buf.get_u32().map_err(decode_error)?;
Ok(Some(Self { min_tx_offset, n, len }))
}
}
}
}
/// Entry type of a [`crate::Commitlog`].
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Commit {
/// The offset of the first record in this commit.
///
/// The offset starts from zero and is counted from the beginning of the
/// entire log.
pub min_tx_offset: u64,
/// The number of records in the commit.
pub n: u16,
/// A buffer of all records in the commit in serialized form.
///
/// Readers must bring their own [`crate::Decoder`] to interpret this buffer.
/// `n` indicates how many records the buffer contains.
pub records: Vec<u8>,
}
impl Commit {
pub const FRAMING_LEN: usize = Header::LEN + /* crc32 */ 4;
pub const CHECKSUM_ALGORITHM: u8 = CHECKSUM_ALGORITHM_CRC32C;
/// The range of transaction offsets contained in this commit.
pub fn tx_range(&self) -> Range<u64> {
self.min_tx_offset..self.min_tx_offset + self.n as u64
}
/// Length in bytes of this commit when written to the log via [`Self::write`].
pub fn encoded_len(&self) -> usize {
Self::FRAMING_LEN + self.records.len()
}
/// Serialize and write `self` to `out`.
///
/// Returns the crc32 checksum of the commit on success.
pub fn write<W: Write>(&self, out: W) -> io::Result<u32> {
let mut out = Crc32cWriter::new(out);
let min_tx_offset = self.min_tx_offset.to_le_bytes();
let n = self.n.to_le_bytes();
let len = (self.records.len() as u32).to_le_bytes();
out.write_all(&min_tx_offset)?;
out.write_all(&n)?;
out.write_all(&len)?;
out.write_all(&self.records)?;
let crc = out.crc32c();
let mut out = out.into_inner();
out.write_all(&crc.to_le_bytes())?;
Ok(crc)
}
/// Attempt to read one [`Commit`] from the given [`Read`]er.
///
/// Returns `None` if the reader is already at EOF.
///
/// Verifies the checksum of the commit. If it doesn't match, an error of
/// kind [`io::ErrorKind::InvalidData`] with an inner error downcastable to
/// [`ChecksumMismatch`] is returned.
///
/// To retain access to the checksum, use [`StoredCommit::decode`].
pub fn decode<R: Read>(reader: R) -> io::Result<Option<Self>> {
let commit = StoredCommit::decode(reader)?;
Ok(commit.map(Into::into))
}
/// Convert `self` into an iterator yielding [`Transaction`]s.
///
/// The supplied [`Decoder`] is responsible for extracting individual
/// transactions from the `records` buffer.
///
/// `version` is the log format version of the current segment, and gets
/// passed to [`Decoder::decode_record`].
///
/// `from_offset` is the transaction offset within the current commit from
/// which to start decoding. That is:
///
/// * if the tx offset within the commit is smaller than `from_offset`,
/// [`Decoder::skip_record`] is called.
///
/// The iterator does not yield a value, unless `skip_record` returns an
/// error.
///
/// * if the tx offset within the commit is greater of equal to `from_offset`,
/// [`Decoder::decode_record`] is called.
///
/// The iterator yields the result of this call.
///
/// * if `from_offset` doesn't fall into the current commit, the iterator
/// yields nothing.
///
pub fn into_transactions<D: Decoder>(
self,
version: u8,
from_offset: u64,
de: &D,
) -> impl Iterator<Item = Result<Transaction<D::Record>, D::Error>> + '_ {
let records = Cursor::new(self.records);
(self.min_tx_offset..(self.min_tx_offset + self.n as u64))
.scan(records, move |recs, offset| {
let mut cursor = &*recs;
let ret = if offset < from_offset {
de.skip_record(version, offset, &mut cursor).err().map(Err)
} else {
let tx = de
.decode_record(version, offset, &mut cursor)
.map(|txdata| Transaction { offset, txdata });
Some(tx)
};
Some(ret)
})
.flatten()
}
}
impl From<StoredCommit> for Commit {
fn from(
StoredCommit {
min_tx_offset,
n,
records,
checksum: _,
}: StoredCommit,
) -> Self {
Self {
min_tx_offset,
n,
records,
}
}
}
/// A [`Commit`] as stored on disk.
///
/// Differs from [`Commit`] only in the presence of a `checksum` field, which
/// is computed when encoding a commit for storage.
#[derive(Debug, PartialEq)]
pub struct StoredCommit {
/// See [`Commit::min_tx_offset`].
pub min_tx_offset: u64,
/// See [`Commit::n`].
pub n: u16,
/// See [`Commit::records`].
pub records: Vec<u8>,
/// The checksum computed when encoding a [`Commit`] for storage.
pub checksum: u32,
}
impl StoredCommit {
/// The range of transaction offsets contained in this commit.
pub fn tx_range(&self) -> Range<u64> {
self.min_tx_offset..self.min_tx_offset + self.n as u64
}
/// Attempt to read one [`StoredCommit`] from the given [`Read`]er.
///
/// Returns `None` if the reader is already at EOF.
///
/// Verifies the checksum of the commit. If it doesn't match, an error of
/// kind [`io::ErrorKind::InvalidData`] with an inner error downcastable to
/// [`ChecksumMismatch`] is returned.
pub fn decode<R: Read>(reader: R) -> io::Result<Option<Self>> {
let mut reader = Crc32cReader::new(reader);
let Some(hdr) = Header::decode(&mut reader)? else {
return Ok(None);
};
let mut records = vec![0; hdr.len as usize];
reader.read_exact(&mut records)?;
let chk = reader.crc32c();
let crc = decode_u32(reader.into_inner())?;
if chk != crc {
return Err(invalid_data(ChecksumMismatch));
}
Ok(Some(Self {
min_tx_offset: hdr.min_tx_offset,
n: hdr.n,
records,
checksum: crc,
}))
}
/// Convert `self` into an iterator yielding [`Transaction`]s.
///
/// The supplied [`Decoder`] is responsible for extracting individual
/// transactions from the `records` buffer.
pub fn into_transactions<D: Decoder>(
self,
version: u8,
from_offset: u64,
de: &D,
) -> impl Iterator<Item = Result<Transaction<D::Record>, D::Error>> + '_ {
Commit::from(self).into_transactions(version, from_offset, de)
}
}
/// Numbers needed to compute [`crate::segment::Header`].
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Metadata {
pub tx_range: Range<u64>,
pub size_in_bytes: u64,
}
impl Metadata {
/// Extract the [`Metadata`] of a single [`Commit`] from the given reader.
///
/// Note that this decodes the commit due to checksum verification.
/// Like [`Commit::decode`], returns `None` if the reader is at EOF already.
pub fn extract<R: io::Read>(reader: R) -> io::Result<Option<Self>> {
Commit::decode(reader).map(|maybe_commit| maybe_commit.map(Self::from))
}
}
impl From<Commit> for Metadata {
fn from(commit: Commit) -> Self {
Self {
tx_range: commit.tx_range(),
size_in_bytes: commit.encoded_len() as u64,
}
}
}
fn decode_u32<R: Read>(mut read: R) -> io::Result<u32> {
let mut buf = [0; 4];
read.read_exact(&mut buf)?;
Ok(u32::from_le_bytes(buf))
}
fn decode_error(e: DecodeError) -> io::Error {
invalid_data(e)
}
fn invalid_data<E>(e: E) -> io::Error
where
E: Into<Box<dyn std::error::Error + Send + Sync>>,
{
io::Error::new(io::ErrorKind::InvalidData, e)
}
#[cfg(test)]
mod tests {
use std::num::NonZeroU8;
use proptest::prelude::*;
use super::*;
use crate::{payload::ArrayDecoder, tests::helpers::enable_logging, DEFAULT_LOG_FORMAT_VERSION};
#[test]
fn commit_roundtrip() {
let records = vec![0; 128];
let commit = Commit {
min_tx_offset: 0,
n: 3,
records,
};
let mut buf = Vec::with_capacity(commit.encoded_len());
commit.write(&mut buf).unwrap();
let commit2 = Commit::decode(&mut buf.as_slice()).unwrap().unwrap();
assert_eq!(commit, commit2);
}
#[test]
fn into_transactions_can_skip_txs() {
enable_logging();
let commit = Commit {
min_tx_offset: 0,
n: 4,
records: vec![0; 128],
};
let txs = commit
.into_transactions(DEFAULT_LOG_FORMAT_VERSION, 2, &ArrayDecoder::<32>)
.collect::<Result<Vec<_>, _>>()
.unwrap();
assert_eq!(
txs,
vec![
Transaction {
offset: 2,
txdata: [0u8; 32]
},
Transaction {
offset: 3,
txdata: [0; 32]
}
]
)
}
proptest! {
#[test]
fn bitflip(pos in Header::LEN..512, mask in any::<NonZeroU8>()) {
let commit = Commit {
min_tx_offset: 42,
n: 10,
records: vec![1; 512],
};
let mut buf = Vec::with_capacity(commit.encoded_len());
commit.write(&mut buf).unwrap();
// Flip bit in the `records` section,
// so we get `ChecksumMismatch` not any other error.
buf[pos] ^= mask.get();
match Commit::decode(&mut buf.as_slice()) {
Err(e) => {
assert_eq!(e.kind(), io::ErrorKind::InvalidData);
e.into_inner()
.unwrap()
.downcast::<ChecksumMismatch>()
.expect("IO inner should be checksum mismatch");
}
Ok(commit) => panic!("expected checksum mismatch, got valid commit: {commit:?}"),
}
}
}
}