-
Notifications
You must be signed in to change notification settings - Fork 195
[Storage] Add Closer
to Batch interface and use it to prevent memory leak (BadgerDB & Pebble)
#7257
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
Conversation
Currently, the error path in WithReaderBatchWriter() has a memory leak when using BadgerDB. This commit calls WriteBatch.Cancel() as a defer statement right after BadgerDB WriteBatch is created. BadgerDB v2 docs for WriteBatch.Cancel() says: "Cancel function must be called if there's a chance that Flush might not get called. If neither Flush or Cancel is called, the transaction oracle would never get a chance to clear out the row commit timestamp map, thus causing an unbounded memory consumption. Typically, you can call Cancel as a defer statement right after NewWriteBatch is called."
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #7257 +/- ##
=======================================
Coverage 41.31% 41.32%
=======================================
Files 2174 2174
Lines 190514 190558 +44
=======================================
+ Hits 78713 78745 +32
- Misses 105223 105236 +13
+ Partials 6578 6577 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This commit adds Closer to Batch interface to release memory of th batch. This can be called as a defer statement immediately after creating Batch to reduce risk of unbounded memory consumption. This commit also updates existing code to call Close after a new Batch is created.
WithReaderBatchWriter()
when using BadgerDBClose()
to Batch interface and use it to prevent memory leak (BadgerDB & Pebble)
Close()
to Batch interface and use it to prevent memory leak (BadgerDB & Pebble)Closer
to Batch interface and use it to prevent memory leak (BadgerDB & Pebble)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the fix Faye.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great change!
Closes #7258
Updates #6527
Currently, the
Batch
interface doesn't provide aClose
function to release memory of the batch. Not calling the database function to close or cancel a batch can cause "unbounded memory consumption" according to BadgerDB v2 docs. And for Pebble, we should useBatch.Close()
for both non-error and error paths.This PR adds a
Closer
to Batch interface so it can be used to release memory of the batch. TheClose()
function can be called as a defer statement immediately after creating Batch to reduce risk of unbounded memory consumption.This PR also updates existing code to call
Close()
after a new Batch is created.I found this while getting familiar with storage code related to BadgerDB and Pebble.
Details
BadgerDB v2 docs for
WriteBatch.Cancel()
says:For example, the error path in
WithReaderBatchWriter()
currently has a memory leak when using BadgerDB.Pebble docs says for
Batch.Close()
:Pebble needs their
Batch.Close()
function to be called for both non-error and error paths.