1
0
mirror of https://github.com/aziascreations/BakaShrot.git synced 2026-11-23 20:58:46 +01:00
Files
BakaShrot/crates/bakashrot-storage/src/batch.rs
T
aziascreations a6ade535d0 Stopped writing once SQLite rolled a batch back
- SQLite may roll a whole transaction back by itself (a full disk, an
  I/O error, out of memory). The writes after it then ran outside any
  transaction, each committed alone, while the batch failed and every
  write was answered ServerError: a retried push stored twice
- Every Batch operation after is refused now (Error::RolledBack,
  Batch::is_rolled_back), the batch fails, nothing in it stored; the
  server's writer stops running the group there
- batch.rs nothing_runs_once_sqlite_rolled_back (a ROLLBACK standing in
  for SQLite's); docs (storage.md) and revamp.md (step 18)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 23:41:04 +01:00

506 lines
20 KiB
Rust

//! Writing: the writer's connection, and what a batch (one transaction)
//! does.
use crate::{
AttachmentState, Error, MAX_STREAMS, Result, find_stream, message_key, part_key, part_keys,
stream_keys,
};
use bakashrot_proto::limits::{
ATTACHMENT_PART_SIZE, MAX_ATTACHMENT_SIZE, MAX_MESSAGE_ID, attachment_part_len,
};
use bakashrot_proto::packets::ReplicatedMessage;
use rusqlite::{Connection, OptionalExtension, Transaction, TransactionBehavior};
/// The connection that writes (one per database).
#[derive(Debug)]
pub struct Writer {
connection: Connection,
}
impl Writer {
pub(crate) fn new(connection: Connection) -> Writer {
Writer { connection }
}
/// Runs `write` in one transaction, committed if it returns `Ok`,
/// rolled back otherwise. Each operation of a [`Batch`] is all or
/// nothing on its own, so `write` may go on after one fails: many
/// pushes in one batch are a group commit, one disk flush for all.
///
/// Unless SQLite rolled the whole transaction back by itself (it may,
/// on a full disk, an I/O error, out of memory): then every operation
/// after is refused ([`Error::RolledBack`], they'd be written outside
/// the transaction, one at a time), and so is the batch: nothing in it
/// was stored.
pub fn batch<T>(&mut self, write: impl FnOnce(&mut Batch<'_>) -> Result<T>) -> Result<T> {
let mut batch = Batch {
transaction: self
.connection
.transaction_with_behavior(TransactionBehavior::Immediate)?,
};
let value = write(&mut batch)?;
batch.check_open()?;
batch.transaction.commit()?;
Ok(value)
}
}
/// What's written in one transaction ([`Writer::batch`]). Names come
/// checked (`bakashrot_proto`'s decoding). Attachment ids are this
/// database's own, from [`Batch::start_upload`] or
/// [`Reader::attachment`](crate::Reader::attachment).
#[derive(Debug)]
pub struct Batch<'c> {
transaction: Transaction<'c>,
}
impl Batch<'_> {
/// Stores a pushed message: the next id of its topic and node (from 1),
/// which it returns. Without a savepoint, the hot path: a push that
/// fails leaves at most its topic and node's (empty) stream.
pub fn push(&mut self, topic: &str, node: &str, pushed_at: u64, data: &str) -> Result<u64> {
self.check_open()?;
Ok(insert_pushed(&self.transaction, topic, node, pushed_at, data)?.0)
}
/// Whether SQLite rolled the transaction back by itself: nothing more
/// may run in it.
pub fn is_rolled_back(&self) -> bool {
self.transaction.is_autocommit()
}
/// [`Error::RolledBack`] once rolled back.
fn check_open(&self) -> Result<()> {
match self.is_rolled_back() {
false => Ok(()),
true => Err(Error::RolledBack),
}
}
/// Stores a pushed message with its attachment, an upload all of whose
/// parts came ([`Batch::write_part`] said so) and whose SHA-256 the
/// caller checked: the attachment is then `ready`.
pub fn push_with_attachment(
&mut self,
upload: i64,
topic: &str,
node: &str,
pushed_at: u64,
data: &str,
) -> Result<u64> {
self.atomic(|db| {
let (state, size, received) = attachment(db, upload)?;
if state != AttachmentState::Uploading || received != size {
return Err(Error::Invalid(format!(
"attachment {upload} isn't a complete upload"
)));
}
let (id, key) = insert_pushed(db, topic, node, pushed_at, data)?;
set_attachment(db, upload, "message_key = ?2, state = 'ready'", key)?;
Ok(id)
})
}
/// Starts an upload: an attachment pushed with its message, whose parts
/// come next. Returns its id.
pub fn start_upload(&mut self, size: u64, sha256: &[u8; 32]) -> Result<i64> {
if !(1..=MAX_ATTACHMENT_SIZE).contains(&size) {
return Err(Error::TooBig(format!(
"an attachment of {size} bytes: 1 to {MAX_ATTACHMENT_SIZE}"
)));
}
self.atomic(|db| {
db.execute(
"INSERT INTO attachments (size, sha256, state) VALUES (?1, ?2, 'uploading')",
(size, sha256),
)?;
Ok(db.last_insert_rowid())
})
}
/// Stores the next part of an upload or of a replicated attachment: in
/// order, each of its exact size (`attachment_part_len`). Returns
/// whether that was the last one.
pub fn write_part(&mut self, attachment_id: i64, part: u64, data: &[u8]) -> Result<bool> {
self.atomic(|db| {
let (state, size, received) = attachment(db, attachment_id)?;
if state != AttachmentState::Uploading && state != AttachmentState::Replicating {
return Err(Error::Invalid(format!(
"attachment {attachment_id} is {state}, it takes no parts"
)));
}
// Every part but the last is whole: what came is whole parts.
let next = received / ATTACHMENT_PART_SIZE;
if received == size || part != next {
return Err(Error::Invalid(format!(
"part {part} of attachment {attachment_id}: part {next} is next"
)));
}
let expected = attachment_part_len(size, part);
if data.len() as u64 != expected {
return Err(Error::Invalid(format!(
"part {part} of attachment {attachment_id}: {} bytes, {expected} expected",
data.len()
)));
}
db.prepare_cached("INSERT INTO attachment_parts (key, part_data) VALUES (?1, ?2)")?
.execute((part_key(attachment_id, part), data))?;
set_attachment(db, attachment_id, "received = received + ?2", expected)?;
Ok(received + expected == size)
})
}
/// Drops an upload that won't be pushed: cut short, or not matching its
/// SHA-256.
pub fn cancel_upload(&mut self, upload: i64) -> Result<()> {
self.atomic(|db| {
if attachment(db, upload)?.0 != AttachmentState::Uploading {
return Err(Error::Invalid(format!(
"attachment {upload} isn't an upload"
)));
}
delete_attachment(db, upload)
})
}
/// Stores messages a peer sent for `topic` and `node`, with their own
/// ids, in order; ones already here are kept as they are. Their
/// attachments are noted, to be asked for (`replicating`), or never if
/// bigger than `max_attachment_size` (`too_big`). Ones pushed before
/// `skip_before` (ms, past their age here) aren't stored, but count as
/// received. Returns the topic and node's last id.
pub fn replicate(
&mut self,
topic: &str,
node: &str,
messages: &[ReplicatedMessage],
max_attachment_size: u64,
skip_before: u64,
) -> Result<u64> {
self.atomic(|db| {
let (stream, mut last_id) = stream(db, topic, node)?;
let mut insert = db.prepare_cached(
"INSERT OR IGNORE INTO data (key, pushed_at, message_data) VALUES (?1, ?2, ?3)",
)?;
let mut attach = db.prepare_cached(
"INSERT INTO attachments (message_key, size, sha256, state) VALUES (?1, ?2, ?3, ?4)",
)?;
for message in messages {
if !(1..=MAX_MESSAGE_ID).contains(&message.id) {
return Err(Error::Invalid(format!("message id {}", message.id)));
}
// What the trigger keeps: one ignored was here, below it.
last_id = last_id.max(message.id);
if message.pushed_at < skip_before {
continue;
}
let key = message_key(stream, message.id);
let stored = insert.execute((key, message.pushed_at, &message.data))? == 1;
if let (true, Some(attachment)) = (stored, &message.attachment) {
let state = if attachment.size > max_attachment_size {
AttachmentState::TooBig
} else {
AttachmentState::Replicating
};
attach.execute((key, attachment.size, &attachment.sha256, state))?;
}
}
// The skipped ones move it too.
db.prepare_cached("UPDATE streams SET last_id = ?2 WHERE stream_id = ?1")?
.execute((stream, last_id))?;
Ok(last_id)
})
}
/// A replicated attachment all of whose parts came: `matches` its
/// SHA-256 (the caller checked), it's `ready`; otherwise its parts are
/// dropped and it's asked for again from the start.
pub fn finish_replicated(&mut self, attachment_id: i64, matches: bool) -> Result<()> {
self.atomic(|db| {
let (state, size, received) = attachment(db, attachment_id)?;
if state != AttachmentState::Replicating || received != size {
return Err(Error::Invalid(format!(
"attachment {attachment_id} isn't a complete replicated one"
)));
}
if matches {
set_attachment(db, attachment_id, "state = ?2", AttachmentState::Ready)
} else {
delete_parts(db, attachment_id, None)?;
set_attachment(db, attachment_id, "received = ?2", 0)
}
})
}
/// A ready attachment found not to match its SHA-256 (a damaged
/// disk, a bug): its parts go, it's asked for again from its peers
/// (`replicating`), and not served meanwhile.
pub fn damaged(&mut self, attachment_id: i64) -> Result<()> {
self.atomic(|db| {
if attachment(db, attachment_id)?.0 != AttachmentState::Ready {
return Err(Error::Invalid(format!(
"attachment {attachment_id} isn't a ready one"
)));
}
delete_parts(db, attachment_id, None)?;
db.prepare_cached(
"UPDATE attachments SET state = 'replicating', received = 0 \
WHERE attachment_id = ?1",
)?
.execute([attachment_id])?;
Ok(())
})
}
/// Retention: deletes `topic`'s messages pushed before `cutoff` (ms),
/// `limit` at most (call again while it returns `limit`). Each stream
/// is read from its oldest message and stops at the first one it keeps:
/// only what's deleted is read. So messages go in id order: one stamped
/// earlier than the one before it (a clock set back) waits for it.
pub fn delete_before(&mut self, topic: &str, cutoff: u64, limit: usize) -> Result<usize> {
self.atomic(|db| {
let streams: Vec<i64> = db
.prepare_cached("SELECT stream_id FROM streams WHERE topic = ?1")?
.query_map([topic], |row| row.get(0))?
.collect::<rusqlite::Result<_>>()?;
let mut oldest = db.prepare_cached(
"SELECT key, pushed_at FROM data WHERE key BETWEEN ?1 AND ?2 ORDER BY key",
)?;
let mut deleted = 0;
for stream in streams {
let (first, last) = stream_keys(stream);
let mut up_to = None;
let mut rows = oldest.query((first, last))?;
while deleted < limit {
let Some(row) = rows.next()? else { break };
if row.get::<_, u64>(1)? >= cutoff {
break;
}
up_to = Some(row.get::<_, i64>(0)?);
deleted += 1;
}
drop(rows);
if let Some(up_to) = up_to {
db.execute(
"DELETE FROM data WHERE key BETWEEN ?1 AND ?2",
(first, up_to),
)?;
}
if deleted == limit {
break;
}
}
Ok(deleted)
})
}
/// Deletes the parts of attachments whose message was deleted,
/// `max_parts` at most (call again while it returns `max_parts`), and
/// their rows once they have none left.
pub fn purge_attachments(&mut self, max_parts: usize) -> Result<usize> {
self.atomic(|db| {
let deleting: Vec<i64> = db
.prepare_cached(
"SELECT attachment_id FROM attachments WHERE state = 'deleting' \
ORDER BY attachment_id",
)?
.query_map([], |row| row.get(0))?
.collect::<rusqlite::Result<_>>()?;
let mut purged = 0;
for attachment_id in deleting {
purged += delete_parts(db, attachment_id, Some(max_parts - purged))?;
if purged == max_parts {
break;
}
db.execute(
"DELETE FROM attachments WHERE attachment_id = ?1",
[attachment_id],
)?;
}
Ok(purged)
})
}
/// Runs one operation all or nothing (a savepoint), so the batch can go
/// on after it fails.
fn atomic<T>(&mut self, operation: impl FnOnce(&Connection) -> Result<T>) -> Result<T> {
// Outside a transaction, a savepoint would start one of its own,
// committed when released.
self.check_open()?;
let savepoint = self.transaction.savepoint()?;
let value = operation(&savepoint)?;
savepoint.commit()?;
Ok(value)
}
}
/// A topic and node's stream, made if new: its id and last id.
fn stream(db: &Connection, topic: &str, node: &str) -> Result<(i64, u64)> {
if let Some(found) = find_stream(db, topic, node)? {
return Ok(found);
}
let next: i64 = db.query_row(
"SELECT coalesce(max(stream_id), 0) + 1 FROM streams",
[],
|row| row.get(0),
)?;
if next > MAX_STREAMS {
return Err(Error::TooBig(format!(
"{topic}/{node}: this database has {MAX_STREAMS} topic and node pairs already"
)));
}
db.execute(
"INSERT INTO streams (stream_id, topic, node) VALUES (?1, ?2, ?3)",
(next, topic, node),
)?;
Ok((next, 0))
}
/// Inserts a pushed message with the next id of its stream: the id and the
/// message's key.
fn insert_pushed(
db: &Connection,
topic: &str,
node: &str,
pushed_at: u64,
data: &str,
) -> Result<(u64, i64)> {
let (stream, last_id) = stream(db, topic, node)?;
let id = last_id + 1;
if id > MAX_MESSAGE_ID {
return Err(Error::TooBig(format!("{topic}/{node}: no message id left")));
}
let key = message_key(stream, id);
db.prepare_cached("INSERT INTO data (key, pushed_at, message_data) VALUES (?1, ?2, ?3)")?
.execute((key, pushed_at, data))?;
Ok((id, key))
}
/// An attachment's state, size and bytes received.
fn attachment(db: &Connection, attachment_id: i64) -> Result<(AttachmentState, u64, u64)> {
db.prepare_cached("SELECT state, size, received FROM attachments WHERE attachment_id = ?1")?
.query_row([attachment_id], |row| {
Ok((row.get(0)?, row.get(1)?, row.get(2)?))
})
.optional()?
.ok_or_else(|| Error::NotFound(format!("no attachment {attachment_id}")))
}
/// `UPDATE attachments SET <set>` of one, `?2` being `value`.
fn set_attachment(
db: &Connection,
attachment_id: i64,
set: &str,
value: impl rusqlite::ToSql,
) -> Result<()> {
db.prepare_cached(&format!(
"UPDATE attachments SET {set} WHERE attachment_id = ?1"
))?
.execute((attachment_id, value))?;
Ok(())
}
/// Deletes an attachment's parts, `limit` at most: how many.
fn delete_parts(db: &Connection, attachment_id: i64, limit: Option<usize>) -> Result<usize> {
let (first, last) = part_keys(attachment_id);
let limit = limit.map_or(-1, |limit| limit as i64);
Ok(db
.prepare_cached(
"DELETE FROM attachment_parts WHERE key IN \
(SELECT key FROM attachment_parts WHERE key BETWEEN ?1 AND ?2 LIMIT ?3)",
)?
.execute((first, last, limit))?)
}
/// Deletes an attachment and its parts.
fn delete_attachment(db: &Connection, attachment_id: i64) -> Result<()> {
delete_parts(db, attachment_id, None)?;
db.execute(
"DELETE FROM attachments WHERE attachment_id = ?1",
[attachment_id],
)?;
Ok(())
}
/// At start: deletes every upload (none of them can be pushed any more),
/// and starts over the replicated attachments all of whose parts came but
/// weren't checked (stopped in between).
pub(crate) fn clean_up(db: &Connection) -> Result<()> {
let unchecked: Vec<i64> = db
.prepare(
"SELECT attachment_id FROM attachments \
WHERE state = 'replicating' AND received = size",
)?
.query_map([], |row| row.get(0))?
.collect::<rusqlite::Result<_>>()?;
for attachment_id in unchecked {
delete_parts(db, attachment_id, None)?;
set_attachment(db, attachment_id, "received = ?2", 0)?;
}
let uploads: Vec<i64> = db
.prepare("SELECT attachment_id FROM attachments WHERE state = 'uploading'")?
.query_map([], |row| row.get(0))?
.collect::<rusqlite::Result<_>>()?;
for upload in uploads {
delete_attachment(db, upload)?;
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
/// SQLite may roll a whole transaction back by itself (a full disk, an
/// I/O error): what came after ran outside it, each committed alone,
/// while the batch failed and every write was answered as failed.
#[test]
fn nothing_runs_once_sqlite_rolled_back() {
let connection = Connection::open_in_memory().unwrap();
connection
.execute_batch(include_str!("schema_v9.sql"))
.unwrap();
let mut writer = Writer::new(connection);
let outcome = writer.batch(|batch| {
assert_eq!(batch.push("logs", "kitchen", 1, "a")?, 1);
assert!(!batch.is_rolled_back());
// What SQLite does on its own, sometimes, on those errors.
batch.transaction.execute_batch("ROLLBACK")?;
assert!(batch.is_rolled_back());
let refused = |result: Result<_>| matches!(result, Err(Error::RolledBack));
assert!(refused(batch.push("logs", "kitchen", 2, "b")));
assert!(refused(batch.push("other", "garage", 3, "c")));
assert!(refused(batch.start_upload(5, &[0; 32]).map(|_| 0)));
assert!(refused(batch.delete_before("logs", 10, 10).map(|_| 0)));
assert!(refused(batch.purge_attachments(10).map(|_| 0)));
Ok(())
});
assert!(matches!(outcome, Err(Error::RolledBack)), "{outcome:?}");
let count = |writer: &Writer, table: &str| -> i64 {
writer
.connection
.query_row(&format!("SELECT count(*) FROM {table}"), [], |row| {
row.get(0)
})
.unwrap()
};
// Nothing stored: neither before the rollback nor after it.
assert_eq!(
(
count(&writer, "data"),
count(&writer, "streams"),
count(&writer, "attachments")
),
(0, 0, 0)
);
// The next batch goes on as usual.
assert_eq!(
writer
.batch(|batch| batch.push("logs", "kitchen", 4, "d"))
.unwrap(),
1
);
assert_eq!(count(&writer, "data"), 1);
}
}