0
0
mirror of https://github.com/tursodatabase/libsql.git synced 2025-05-25 06:20:44 +00:00

prefer local max_frame_no

this assumes that cache is always up to date and uses that instead
of calling the storage server. later we will add replication and make
sure that cache is upto date.
This commit is contained in:
Avinash Sajjanshetty
2024-07-09 14:52:27 +05:30
parent 3e0fb7b1b2
commit 8dce1e533f
2 changed files with 20 additions and 3 deletions
libsql-storage/src

@ -13,7 +13,7 @@ use libsql_sys::rusqlite;
use libsql_sys::wal::{Result, Vfs, Wal, WalManager};
use rpc::storage_client::StorageClient;
use tonic::transport::Channel;
use tracing::{error, trace};
use tracing::{error, trace, warn};
pub mod rpc {
#![allow(clippy::all)]
@ -196,8 +196,13 @@ impl Wal for DurableWal {
// - save the current max_frame_no for this txn
trace!("DurableWal::begin_read_txn()");
let rt = tokio::runtime::Handle::current();
let frame_no = tokio::task::block_in_place(|| rt.block_on(self.frames_count()));
self.max_frame_no = frame_no;
let remote_frame_no = tokio::task::block_in_place(|| rt.block_on(self.frames_count()));
let local_frame_no = self.local_cache.get_max_frame_num().unwrap();
if local_frame_no < remote_frame_no {
warn!("cache is lagging behind the remote!")
// TODO: replicate data from source
}
self.max_frame_no = local_frame_no;
Ok(true)
}

@ -77,6 +77,18 @@ impl LocalCache {
}
}
pub fn get_max_frame_num(&self) -> Result<u64> {
match self
.conn
.query_row("select MAX(frame_no) from frames", params![], |row| {
row.get(0)
}) {
Ok(frame_no) => Ok(frame_no),
Err(Error::QueryReturnedNoRows) => Ok(0),
Err(e) => Err(e),
}
}
pub fn insert_page(&self, txn_id: &str, page_no: u32, frame_data: &[u8]) -> Result<()> {
self.conn.execute(
"INSERT INTO transactions (txn_id, page_no, data) VALUES (?1, ?2, ?3)