https://slatedb.io logo
@flaneur233 Had another question while
# general
c
@flaneur233 Had another question while reading over your PR:
Copy code
rust
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:
Copy code
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?
f
agree, let me think about this 🤔
revisited the code, recalled that i was initially concerned about whether it's possible to have a deadlock when nesting the
db_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. 🫡
c
> i was initially concerned about whether it's possible to have a deadlock when nesting the db_state lock (inside next_wal_id()) with the wal_buffer lock Oh, I didn't realize next_wal_id grabbed a lock! 🤔 hmm
I really like the idea of moving it to the oracle. My only worry is we're forgetting some reason why we need it in the manifest 😅 .. I can't recall any good reason, though.
Ah! I see we don't even have it in the manifest.fbs at all!
Ok, yea I think moving it to oracle makes a lot of sense
f
let me have a try for this 😁
@criccomini made an refactor for this in https://github.com/slatedb/slatedb/pull/782 , please take a look at your convenience 🫡
r
Going to review the PR now, but just for context the reason for storing the last wal id in the manifest is to allow consistent reads from a db checkpoint. The reader will only replay WALs up to the
wal_id_last_seen
from the manifest
c
The PR shouldn't mess with that
(should still work)
It does change the semantics of it slightly, though. The PR switches from updating on WAL write to updating on L0 flush. Not totally clear on that...
r
Taking a look now. Its fine as long as we do it when writing the manifest, so a process taking a checkpoint can flush the db and then take a checkpoint (which writes the manifest) to get a checkpoint with the latest data
c
Yea, that's what I figured...
I don't think it should delay what the reader/checkpointer sees either.. since they're always going to see the WAL last seen in sync with the last memtables written out to l0
f
yeah, it's synced whenever last memtable written to l0, when the next wal id is incremented, it could be a lock-free atomic variable operation around
Oracle