LevelDB provides three interfaces for write operations:
- Put: Inserts or modifies a record.
- Delete: deletes a record.
- Write: To insert/modify/delete multiple records atomically.
Both Put and Delete are implemented by direct calls to Write:
- leveldb::DBImpl::Put => leveldb::DB::Put => leveldb::DBImpl::Write
- leveldb::DBImpl::Delete => leveldb::DB::Delete => leveldb::DBImpl::Write
The Write interface
The leveldb::DBImpl::Write function is declared as follows:
virtual Status Write(const WriteOptions& options, WriteBatch* updates) = 0;
Copy the code
- leveldb::WriteOptionsIs a write control parameter with only one member variable
sync
Indicates whether the log is flushed to external storage after each write. - Leveldb ::WriteBatch Indicates operations involving multiple key-value data.
The realization of the Write
Leveldb ::DBImpl::Write.
- Construct a Writer object using the parameters passed in to represent the write operation. Writer is defined as follows:
struct DBImpl::Writer { explicit Writer(port::Mutex* mu) : batch(nullptr), sync(false), done(false), cv(mu) {} Status status; // WriteBatch* batch; // Updated data (1 to multiple key-values) bool sync; // Whether to flush, writeoptions. sync bool done; Port ::CondVar CV; // Conditional variables for concurrency control};Copy the code
-
Get the mutex, put yourself in the write queue, and wait for notification of the condition variable. There are two ways to jump out of the wait: 1) The write operation has been written by another thread; 2) The write operation becomes the head of the write queue. This involves LevelDB concurrency control and performance optimization for writes: since MemTable and WAL do not support concurrent writes, only writers at the head of the write queue perform actual writes. The writer at the head of the queue merges multiple requests in the queue into one request, then performs batch write, and updates the status of each writer.
-
Check writer.done, and return the result if it has already been written by another thread. Otherwise, it is the team leader writer. Continue to execute.
-
Call MakeRoomForWrite to check: Does the number of level-0 files exceed the limit? Check whether the MemTable exceeds the threshold and needs to be switched. And so on. MakeRoomForWrite provides a force parameter that indicates whether to force a new MemTable switch and trigger a Compaction. In the normal write process, force is false. The detailed logic of MakeRoomForWrite is as follows:
-
The call to BuildBatchGroup merges consecutive qualified writers, starting at the head of the queue, into tmp_batch_. The main considerations in the merger are:
- The size of the data written to merge, max_size is 1MB by default. If the size of the first write request is small (less than 128 KB, 128 << 10), max_size is size + 128 KB. This is done to avoid small requests being slowed down by other requests.
- If the first write request sync == false, do not add the write request sync == true.
-
Sets the sequence of data to be written to.
-
Release the mutex. This code guarantees that only one thread will write at a time.
-
Write logs (WAL).
-
Determine whether to sync logs based on the parameters.
-
Update the MemTable.
-
Get the mutex.
-
If sync fails, set bg_error_ and all subsequent writes will fail.
-
Empty the batch operation for temporary merge (tMP_batch_).
-
Update the LastSequence.
-
Notifies all threads that data has been written to.
-
Notify the thread that is still writing to the queue.
summary
That’s the LevelDB writing process. Write queue + merge write operation, logic and code are very simple. On the downside, the entire write process is single-threaded.