Commit 8d841c64 authored by Benjamin Woodruff's avatar Benjamin Woodruff

some cleanup

parent 6a63d1d7
use std::{fs::create_dir_all, marker::PhantomData, path::Path, thread::available_parallelism};
mod extended_key;
mod tx_cache;
use std::{fs::create_dir_all, path::Path, thread::available_parallelism};
use anyhow::{Context, Result};
use lmdb::{
......@@ -7,26 +10,22 @@ use lmdb::{
use crate::database::{
key_value_database::{KeySpace, KeyValueDatabase},
lmdb::tx_cache::{ReadTxGuard, TxCache, WriteTxGuard},
write_batch::{BaseWriteBatch, SerialWriteBatch, WriteBatch, WriteBuffer},
};
mod extended_key;
mod read_tx_cache;
pub struct LmdbValueBuffer<'l> {
ptr: *const u8,
len: usize,
_guard: read_tx_cache::ReadTxGuard<'l>,
_marker: PhantomData<&'l LmbdKeyValueDatabase>,
_guard: ReadTxGuard<'l>,
}
impl<'l> LmdbValueBuffer<'l> {
fn from_raw_parts(ptr: *const u8, len: usize, guard: read_tx_cache::ReadTxGuard<'l>) -> Self {
fn from_raw_parts(ptr: *const u8, len: usize, guard: ReadTxGuard<'l>) -> Self {
Self {
ptr,
len,
_guard: guard,
_marker: PhantomData,
}
}
}
......@@ -40,8 +39,9 @@ impl AsRef<[u8]> for LmdbValueBuffer<'_> {
}
pub struct LmbdKeyValueDatabase {
// Safety: this cache must be dropped before `env`.
read_tx_cache: read_tx_cache::ReadTxCache,
// Safety: this cache references data stored inside of `env`'s internals and must be dropped
// before `env`.
tx_cache: TxCache,
env: Environment,
infra_db: Database,
data_db: Database,
......@@ -58,13 +58,15 @@ impl LmbdKeyValueDatabase {
#[cfg(not(target_arch = "x86"))]
const MAP_SIZE: usize = 40 * 1024 * 1024 * 1024;
let max_read_tx_per_generation = available_parallelism().map_or(4, |v| v.get()).max(8);
let env = Environment::new()
.set_flags(
EnvironmentFlags::WRITE_MAP
| EnvironmentFlags::NO_META_SYNC
// NO_TLS is needed for memory safety in `tx_cache`
| EnvironmentFlags::NO_TLS,
)
.set_max_readers((available_parallelism().map_or(16, |v| v.get()) * 8) as u32)
.set_max_readers(max_read_tx_per_generation as u32 * 8)
.set_max_dbs(4)
.set_map_size(MAP_SIZE)
.open(path)?;
......@@ -73,7 +75,7 @@ impl LmbdKeyValueDatabase {
let meta_db = env.create_db(Some("meta"), DatabaseFlags::INTEGER_KEY)?;
let task_cache_db = env.create_db(Some("task_cache"), DatabaseFlags::empty())?;
Ok(LmbdKeyValueDatabase {
read_tx_cache: read_tx_cache::ReadTxCache::new(&env),
tx_cache: TxCache::new(&env, max_read_tx_per_generation),
env,
infra_db,
data_db,
......@@ -91,12 +93,12 @@ impl LmbdKeyValueDatabase {
}
}
fn get_read_tx(&self) -> read_tx_cache::ReadTxGuard<'_> {
self.read_tx_cache.get_read_tx()
fn get_read_tx(&self) -> ReadTxGuard<'_> {
self.tx_cache.get_read_tx()
}
fn begin_write(&self) -> read_tx_cache::WriteTxGuard<'_> {
self.read_tx_cache.begin_write(&self.env)
fn begin_write(&self) -> WriteTxGuard<'_> {
self.tx_cache.begin_write(&self.env)
}
fn get_from_tx<'tx>(
......@@ -163,7 +165,7 @@ impl KeyValueDatabase for LmbdKeyValueDatabase {
pub struct LmbdWriteBatch<'l> {
tx: RwTransaction<'l>,
_write_guard: Option<read_tx_cache::WriteTxGuard<'l>>,
_write_guard: Option<WriteTxGuard<'l>>,
this: &'l LmbdKeyValueDatabase,
}
......
......@@ -6,7 +6,6 @@ use std::{
atomic::{AtomicU64, Ordering},
mpsc::{Receiver, SyncSender, sync_channel},
},
thread::available_parallelism,
};
use lmdb::{Environment, RoTransaction};
......@@ -16,12 +15,6 @@ pub struct SendRoTransaction<'tx> {
generation: u64,
}
impl<'tx> SendRoTransaction<'tx> {
fn generation(&self) -> u64 {
self.generation
}
}
impl<'tx> Deref for SendRoTransaction<'tx> {
type Target = RoTransaction<'tx>;
......@@ -50,7 +43,7 @@ impl QueueState {
let tx = env.begin_ro_txn().unwrap_or_else(|err| {
panic!("failed to begin LMDB read transaction: {err}");
});
// Safety: these cached transactions are owned by `ReadTxCache`, which is dropped
// Safety: these cached transactions are owned by `TxCache`, which is dropped
// before the LMDB environment field in the parent database type.
let tx = unsafe { transmute::<RoTransaction<'_>, RoTransaction<'static>>(tx) };
sender
......@@ -76,7 +69,7 @@ impl Drop for ReadTxGuardInner<'_> {
let Some(lease) = self.lease.take() else {
return;
};
if lease.generation() != self.generation.load(Ordering::Acquire) {
if lease.generation != self.generation.load(Ordering::Acquire) {
return;
}
......@@ -132,15 +125,14 @@ impl Drop for WriteTxGuard<'_> {
}
}
pub struct ReadTxCache {
pub struct TxCache {
generation: AtomicU64,
queue: RwLock<QueueState>,
capacity: usize,
}
impl ReadTxCache {
pub fn new(env: &Environment) -> Self {
let capacity = available_parallelism().map_or(1, |v| v.get());
impl TxCache {
pub fn new(env: &Environment, capacity: usize) -> Self {
Self {
generation: AtomicU64::new(0),
queue: RwLock::new(QueueState::new(env, capacity, 0)),
......@@ -159,6 +151,8 @@ impl ReadTxCache {
}
}
/// NOTE: Callers should avoid acquiring multiple guards at once. If a single thread tries to
/// acquire more than `capacity` read transactions, it could deadlock.
pub fn get_read_tx<'db>(&'db self) -> ReadTxGuard<'db> {
loop {
let (sender, receiver) = {
......@@ -174,7 +168,7 @@ impl ReadTxCache {
}
};
if lease.generation() != self.generation.load(Ordering::Acquire) {
if lease.generation != self.generation.load(Ordering::Acquire) {
continue;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment