当前位置:网站首页>Leveldb source code analysis -- writing data
Leveldb source code analysis -- writing data
2022-06-24 17:22:00 【Xiao Lin Gang】
principle
Think about it LSM The process of data writing :
- Write to disk WAL Log files ;
- Update in memory MemTable data ;
Write data call
// Write data external interface
Status DB::Put(const WriteOptions& opt, const Slice& key, const Slice& value) {
WriteBatch batch;
batch.Put(key, value);
return Write(opt, &batch);
}Batch write
WriteBatch Encapsulates an atomization operation for batch data modification ;
It mainly completes the serial splicing of data , The format after splicing is as follows :
count | record | record... (kTypeValue+Key+Value;kKeyDeletion+Key)
// Encode the string , Save splice to rep_ variable
void WriteBatch::Put(const Slice& key, const Slice& value) {
WriteBatchInternal::SetCount(this, WriteBatchInternal::Count(this) + 1);
rep_.push_back(static_cast<char>(kTypeValue));
PutLengthPrefixedSlice(&rep_, key);
PutLengthPrefixedSlice(&rep_, value);
}Writing data
because WAL Log files and MemTable The memory structure is a global shared resource , When multiple threads write data at the same time , A mutex lock is required to ensure the isolation of the operation . Considering writing WAL Write operations involving disks , Takes a long time , This will affect the concurrency of data writing .
leveldb In response to this problem , A batch write optimization is done :
Split the data writing operation into two stages , To shorten the lock waiting time ; In the preparation stage , After the lock is obtained during writing , Add the changed data to the queue to be written ; Then check whether you are at the head of the queue to be written , If not , Then release the lock , Enter the waiting ; If you check that you are in the head of the write queue , Then get the lock again , And read data from the queue to be written as much as possible , Write to WAL Log file .
Status DBImpl::Write(const WriteOptions& options, WriteBatch* updates) {
Writer w(&mutex_);
w.batch = updates;
w.sync = options.sync;
w.done = false;
MutexLock l(&mutex_);
writers_.push_back(&w);
// If the data is not written , And the current writer Not at the head of the queue to be written , Is waiting for
while (!w.done && &w != writers_.front()) {
w.cv.Wait();
}
// If the data is written , The exit ( Other threads have helped complete data writing )
if (w.done) {
return w.status;
}
// Data flow limiting mechanism
// May temporarily unlock and wait.
Status status = MakeRoomForWrite(updates == nullptr);
uint64_t last_sequence = versions_->LastSequence();
Writer* last_writer = &w;
if (status.ok() && updates != nullptr) { // nullptr batch is for compactions
// Get as much data from the queue to be written as possible , Spliced into writing WriteBatch structure
WriteBatch* write_batch = BuildBatchGroup(&last_writer);
WriteBatchInternal::SetSequence(write_batch, last_sequence + 1);
last_sequence += WriteBatchInternal::Count(write_batch);
{
// Release the lock , Let other threads read data , Or put the data into the queue to be written
mutex_.Unlock();
// Write data in bulk
status = log_->AddRecord(WriteBatchInternal::Contents(write_batch));
bool sync_error = false;
if (status.ok() && options.sync) {
status = logfile_->Sync();
}
// Write data to MemTable in
if (status.ok()) {
status = WriteBatchInternal::InsertInto(write_batch, mem_);
}
mutex_.Lock();
}
if (write_batch == tmp_batch_) tmp_batch_->Clear();
// Update the global sequence
versions_->SetLastSequence(last_sequence);
}
// This completed write is excluded from the write queue , Update it to write complete status , And wake up the waiting thread
while (true) {
Writer* ready = writers_.front();
writers_.pop_front();
if (ready != &w) {
ready->status = status;
ready->done = true;
ready->cv.Signal();
}
if (ready == last_writer) break;
}
// For example, there is still data in the queue , Wake up other threads to continue writing data
if (!writers_.empty()) {
writers_.front()->cv.Signal();
}
return status;
}边栏推荐
- Advanced anti DDoS IP solutions and which applications are suitable for use
- Analysis and introduction of NFT meta universe source code construction
- Introduction to visual studio shortcut keys and advanced gameplay
- Install Clickhouse client code 210 connection referred (localhost:9000)
- zblog判断某个插件是否安装启用的内置函数代码
- As for IOT safety, 20 CSOs from major manufacturers say
- About with admin option and with grant option
- What is the problem that the data is not displayed on the login web page after the configuration of the RTSP video security intelligent monitoring system easynvr is completed
- This time, talk about the dry goods of industrial Internet | TVP technology closed door meeting
- Tencent monthly security report helps rural revitalization, releases cloud security reports, and jointly builds a joint network security laboratory
猜你喜欢
![[leetcode108] convert an ordered array into a binary search tree (medium order traversal)](/img/e1/0fac59a531040d74fd7531e2840eb5.jpg)
[leetcode108] convert an ordered array into a binary search tree (medium order traversal)
Using consistent hash algorithm in Presto to enhance the data cache locality of dynamic clusters

Daily algorithm & interview questions, 28 days of special training in large factories - the 15th day (string)

Why do you develop middleware when you are young? "You can choose your own way"

MySQL learning -- table structure of SQL test questions
随机推荐
A solution for building live video based on open source real-time audio and video webrtc architecture
How to convert XML to HL7
Future banks need to think about today's structure with tomorrow's thinking
Talk about some good ways to participate in the project
Audio knowledge (I)
When the game meets NFT, is it "chicken ribs" or "chicken legs"?
Devops in digital transformation digital risk
5g brings opportunities and challenges. Are you ready to defend against DDoS?
Meituan financial report: making money while burning money
How important is it to document the project? I was chosen by the top 100 up leaders and stood up again
Pagoda activities, team members can enjoy a lightweight server 1 core 2g5m 28 yuan for two years
[play Tencent cloud] experience and development of game multimedia engine (II)
Implement typescript runtime type checking
Yupi made an AI programming nickname generator!
未来银行需要用明天的思维,来思考今天架构
Development of block hash game guessing system (mature code)
Explore cloudera manager management software tuning (1)
Collect tke logs through daemonset CRD
Solution to the problem that qlineedit setting qdoublevalidator setting range is invalid
GB gb28181 video cascading intelligent analysis platform easygbs broadcast video console error 401