google / leveldb

LevelDB is a fast key-value storage library written at Google that provides an ordered mapping from string keys to string values.
BSD 3-Clause "New" or "Revised" License
36.29k stars 7.8k forks source link

Why DBImpl::Write not call signal when it release the lock ? #1186

Open sunbohong opened 5 months ago

sunbohong commented 5 months ago

image

这里没有释放signal,会导致 write 持续处于 Wait 状态

@ghemawat

https://github.com/google/leveldb/commit/d79762e27369365a7ffe1f2e3a5c64b0632079e1#diff-6fdb755f590d9b01ecb89bd8ceb28577e85536d4472f8e4fc3addeb9a65f3645R1115-R1135

TalpsG commented 3 months ago

并发写会先将把writebatch封装成writer放入写队列,如果此时该writer不是队列的头,就证明前面有写操作正在写,自己先wait下,释放锁。如果是队列的头,则合并部分队列前面的writer到新的writebatch中,并且记录合并过的最后一个writer,last_writer,这个就是BuildBatchGroup的作用。然后写日志和memtable.都写完后将队列前面参与合并过writer 状态都设置为done,表示这个writer完成了,之后才进行signal。 其实就是writebatch也是批量去做的,一次写将队列前几个写请求合并,一起操作,合并时要锁队列防止新增写请求。合并完就能继续添加写请求了,当写日志和memtable完成就标记合并过得writer为done,并唤醒他们,他们done了就直接返回了