]> git-server-git.apps.pok.os.sepia.ceph.com Git - rocksdb.git/commit
Fix race in WriteBufferManager (#9009)
authorGiuseppe Ottaviano <ott@fb.com>
Tue, 12 Oct 2021 07:14:41 +0000 (00:14 -0700)
committerAndrew Kryczka <andrewkr@fb.com>
Thu, 14 Oct 2021 17:44:46 +0000 (10:44 -0700)
commit3ebe8658d06dbb3958036887d8601a5290c04ba1
treec2804cc80db34aa31b6b2339712b79bacb14d143
parent430fd40e87fa77b679dc1ec406e5dab93cf6132a
Fix race in WriteBufferManager (#9009)

Summary:
EndWriteStall has a data race: `queue_.empty()` is checked outside of the
mutex, so once we enter the critical section another thread may already have
cleared the list, and accessing the `front()` is undefined behavior (and causes
interesting crashes under high concurrency).

This PR fixes the bug, and also rewrites the logic to make it easier to reason
about it. It also fixes another subtle bug: if some writers are stalled and
`SetBufferSize(0)` is called, which disables the WBM, the writer are not
unblocked because of an early `enabled()` check in `EndWriteStall()`.

It doesn't significantly change the locking behavior, as before writers won't
lock unless entering a stall condition, and `FreeMem` almost always locks if
stalling is allowed, but that is inevitable with the current design. Liveness is
guaranteed by the fact that if some writes are blocked, eventually all writes
will be blocked due to `stall_active_`, and eventually all memory is freed.

While at it, do a couple of optimizations:

- In `WBMStallInterface::Signal()` signal the CV only after releasing the
  lock. Signaling under the lock is a common pitfall, as it causes the woken-up
  thread to immediately go back to sleep because the mutex is still locked by
  the awaker.

- Move all allocations and deallocations outside of the lock.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/9009

Test Plan:
```
USE_CLANG=1 make -j64 all check
```

Reviewed By: akankshamahajan15

Differential Revision: D31550668

Pulled By: ot

fbshipit-source-id: 5125387c3dc7ecaaa2b8bbc736e58c4156698580
db/db_impl/db_impl.h
include/rocksdb/write_buffer_manager.h
memtable/write_buffer_manager.cc