criccomini
08/12/2025, 5:53 PMrust
async fn freeze_current_wal(&self) -> Result<(), SlateDBError> {
let is_empty = self.inner.read().current_wal.is_empty();
if is_empty {
return Ok(());
}
let next_wal_id = self.wal_id_incrementor.next_wal_id();
let mut inner = self.inner.write();
let current_wal = std::mem::replace(&mut inner.current_wal, Arc::new(KVTable::new()));
inner.immutable_wals.push_back((next_wal_id, current_wal));
Ok(())
}
In the above code, I think we should call next_wal_id() after we grab the write lock. As it is now, I think it's possible for WALs to be pushed into immutable_wals out of order:
thread_1: self.wal_id_incrementor.next_wal_id() = 14
thread_2: self.wal_id_incrementor.next_wal_id() = 15
thread_2: self.inner.write()
thread_1: self.inner_write()
thread_2: swaps current memtable and writes it to immutable_wals as ID 15
thread_1: swaps current memtable and writes it to immutable_wals as ID 14
I think we're safe from this right now since this function is only called from the single threaded event loop in the WAL buffer, but it makes me nervous. WDYT about swapping it so we increment after grabbing the lock?flaneur233
08/13/2025, 2:26 PMflaneur233
08/13/2025, 3:18 PMdb_state lock (inside next_wal_id()) with the wal_buffer lock. it might not be very low propbility when we want to lock the db_state first, then accessing a wal_buffer with lock. 😯
but stepping back, i’m thinking about whether it's a must to store next_wal_sst_id in the manifest and CoreDbState? 😲
the next_wal_sst_id value in the manifest doesn’t reflect the real latest "next WAL ID" imo: during startup, we read a base WAL ID from replay_after_wal_id and replay from there, after replay finishes, we derive the actual next_wal_sst_id and store it in CoreDbState as an in-memory state.
in other words:
1. currently we don’t actually depend on the manifest’s next_wal_sst_id value for persistence, instead, we rely on replay_after_wal_id & wal replay to reconstruct the next WAL ID.
2. the next_wal_id is essentially pure in-memory state, like last_committed_seq, which is managed as an atomic variable in Oracle without any lock.
is it possible to move this next_wal_id to Oracle, where we centrally manage ID generation and sequences (in-memory) with atomic variables (with no lock)?
this approach may help with:
- eliminating the nested locking concern
- removing the WalIdStore trait
- truly centralizing sequence management / ID generation in one place
i’m not entirely confident whether this approach might be missing something.. but if this approach sounds ok, i’d love to make a poc about this to verify whether it's feasible or not. 🫡criccomini
08/13/2025, 5:10 PMcriccomini
08/13/2025, 5:12 PMcriccomini
08/13/2025, 5:16 PMcriccomini
08/13/2025, 5:16 PMflaneur233
08/14/2025, 2:20 AMflaneur233
08/17/2025, 5:04 AMRohan
08/19/2025, 9:32 PMwal_id_last_seen from the manifestcriccomini
08/19/2025, 9:33 PMcriccomini
08/19/2025, 9:33 PMcriccomini
08/19/2025, 9:34 PMRohan
08/19/2025, 9:35 PMcriccomini
08/19/2025, 9:36 PMcriccomini
08/19/2025, 9:37 PMflaneur233
08/22/2025, 1:31 PMOracle