Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why remove the memtable from imm_memtables in LsmStorgae.sync()? #12

Closed
qgaye opened this issue Feb 19, 2023 · 4 comments
Closed

Why remove the memtable from imm_memtables in LsmStorgae.sync()? #12

qgaye opened this issue Feb 19, 2023 · 4 comments

Comments

@qgaye
Copy link

qgaye commented Feb 19, 2023

// Add the flushed L0 table to the list.
{
let mut guard = self.inner.write();
let mut snapshot = guard.as_ref().clone();
// Remove the memtable from the immutable memtables.
snapshot.imm_memtables.pop();
// Add L0 table
snapshot.l0_sstables.push(sst);
// Update SST ID
snapshot.next_sst_id += 1;
// Update the snapshot.
*guard = Arc::new(snapshot);
}

In sync() we push the memtable to imm_memtables first, but remove it during flush to ssTable. What's the reason for removing memtable from imm_memtables? If we do not remove it, we can get key-value in imm_memtables which is located in memory.

And what is the role of imm_memtables in LsmStorage? It's unclear to me.

@skyzh
Copy link
Owner

skyzh commented Feb 19, 2023

imm_memtables are immutable memtables. Memory is not unlimited, and it will eventually become full. We will need to flush those memtables to disk as SST files. After flushing to disk, you can read them from block cache, and we can also run background compaction so consolidate SSTs.

@qgaye
Copy link
Author

qgaye commented Feb 20, 2023

Soga. I found that I overlooked block cache in SsTable.
There is another point I don't really understand. The variable imm_memtable seems to be only used for temporarily store memtable. Is this all its functions? or are other aspects ignored by me?

@skyzh
Copy link
Owner

skyzh commented Feb 20, 2023

In the future, we will have flush thread in the background, which flushes imm_memtable to disk. sync will be very light-weight. It will only do a fsync on WAL file.

@skyzh skyzh closed this as completed Feb 20, 2023
@qgaye
Copy link
Author

qgaye commented Feb 20, 2023

very much looking forward to the future chapter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants