Skip to content

libsql: Check integrity during WAL pull #1998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions libsql/src/local/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::local::rows::BatchedRows;
use crate::params::Params;
use crate::{connection::BatchRows, errors};
use crate::value::Value;
use std::time::Duration;

use super::{Database, Error, Result, Rows, RowsFuture, Statement, Transaction};
Expand Down Expand Up @@ -568,6 +569,18 @@ impl Connection {
self.wal_insert_begin()?;
Ok(WalInsertHandle { conn: self, in_session: RefCell::new(true) })
}

pub(crate) fn check_integrity(&self) -> Result<bool> {
let stmt = self.prepare("PRAGMA integrity_check")?;
let rows = stmt.query(&Params::None)?;
let mut result = true;
if let Some(row) = rows.next()? {
if row.get_value(0)? != Value::Text("ok".to_string()) {
result = false;
}
}
Ok(result)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would prefer returning an error instead of bools

}

pub(crate) struct WalInsertHandle<'a> {
Expand Down
3 changes: 3 additions & 0 deletions libsql/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ async fn try_pull(
match sync_ctx.pull_one_frame(generation, frame_no).await {
Ok(PullResult::Frame(frame)) => {
insert_handle.insert(&frame)?;
assert!(conn.check_integrity()?);
sync_ctx.durable_frame_num = frame_no;
}
Ok(PullResult::EndOfGeneration { max_generation }) => {
Expand All @@ -666,8 +667,10 @@ async fn try_pull(
insert_handle.end()?;
sync_ctx.write_metadata().await?;

assert!(conn.check_integrity()?);
// TODO: Make this crash-proof.
conn.wal_checkpoint(true)?;
assert!(conn.check_integrity()?);

sync_ctx.next_generation();
sync_ctx.write_metadata().await?;
Expand Down
Loading