storage: Consider moving raft log out of rocksdb
Writing the raft log to rocksdb (which uses its own write-ahead log) is probably suboptimal; it may be more efficient to manage the raft log separately (although this is not clear: as long as raft log entries are GC'd aggressively then it's going to be hard to beat rocksdb).
For future reference:
- https://www.usenix.org/system/files/conference/inflow14/inflow14-yang.pdf discusses the downsides of layering log systems on one another
- Intel has demonstrated impressive performance increases by putting the raft log in non-volatile memory instead of disk: http://thenewstack.io/intel-gives-the-etcd-key-value-store-a-needed-boost/
- https://ayende.com/blog/174753/fast-transaction-log-linux?key=273aa566963445188a9c1c5ef3463311 and http://ayende.com/blog/174785/fast-transaction-log-windows discuss the fastest way to implement a write-ahead log.
@bdarnell Can you specify the requirements of the raft log? For example, we need the ability to truncate both a prefix and a suffix of the log as well as to scan entries from a particular index. Is there anything else?
Yeah, that's pretty much it.
- Append entries
- Retrieve entries by their raft-assigned sequential index
- Each entry has an associated term number (probably stored in the the body of the log entry, although these are queried somewhat more often than the full entry so it may make sense to store or cache them separately)
- Retrieve the index and term number of the first and last entries in the log.
- Discard a prefix of the log. Some metadata about the last entry discarded in this way must be kept.
- Less commonly, discard a suffix of the log
Discarding a prefix is currently synchronized across all replicas of a range. This is not required by raft, although this is currently enforced by our consistency checker.
This is what we have done in etcd. There are a few advantages for a separate WAL. You can control how to do preallocation, avoid unnecessary merging, log compaction only involves one disk io by making segmented files and more. We have a WAL pkg here if you want to look at: https://github.com/coreos/etcd/tree/master/wal. There are a few etcd related logic, but we would love to make it generic if you guys want to use it.
FWIW, in Kudu we also implemented our own log. The way we implemented suffix truncation is to actually just append a new record, and the fact that the operation index went backwards (or repeated) implies a truncation of the preceding entries.
To support random access we write a super simple side "index file" which is basically an mmapped array -- given a raft index, you can directly compute an offset into the index file, and it will point to the appropriate log entry in the WAL. See https://github.com/apache/kudu/blob/master/src/kudu/consensus/log_index.h
FWIW, in Kudu we also implemented our own log. The way we implemented suffix truncation is to actually just append a new record, and the fact that the operation index went backwards (or repeated) implies a truncation of the preceding entries.
This is exact what we do for etcd WAL.
Rocksdb just added support for SingleDelete to make log compaction easier though
A small experiment to disable the Raft log completely and rely solely on the Raft entry cache only shows a 5% performance improvement. This was on a single node running locally on my laptop, so not completely realistic, but interesting nonetheless.
Cc @spencerkimball
Tagging this for 1.1 because it will help a lot with the performance seen in #15245.
If you move the raft log out of rocksdb, would you use one physical log per replicated range? This is likely to exhibit very poor performance: If you have updates that span all 100k ranges each AppendEntry would do an fsync with 1 entry for each of these 100k physical logs, meaning 100k fsyncs for 100k updates.
On the other hand, do you have a design for using one physical log for all the ranges? It seems hard to do that because you need to be able to have range-keyed access (e.g. efficiently access a range's entries in order, because Raft's order is only maintained within a range). Moving, splitting, and merging ranges is not straight forward if they are all appearing in one log without some organization at the physical level.
Using two RocksDB instances is one straightforward way to solve your problem. Use WAL with sync=true for the Raft Log RocksDB instance, and don't use a WAL for the RocksDB instance that stores your data. Ideally each RocksDB instance would also be attached to a different physical disk, as well.
We don't know yet what exactly we're going to do here. We definitely won't be using a separate log per range, but we haven't decided which strategy we'll use instead. We'll update this issue with an RFC when we have something to discuss.
RFC: #16361
Moving, splitting, [...] ranges is not straight forward if they are all appearing in one log without some organization at the physical level.
Internally we don't send over log entries when adding new replicas, we bring them up to speed via snapshots instead.
Internally we don't send over log entries when adding new replicas, we bring them up to speed via snapshots instead.
We send log entries along with the snapshot. See store.go:sendSnapshot().
I am going to close this in favor of tracking this work in #38322.