Impose limits on the heap usage of ChunkPool
#2585
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description of Changes
Heap profiling performed by @jsdt revealed that we had a memory leak in
InstanceEnv::datastore_index_scan_range_bsatn_chunks. A bit of spelunking pointed toChunkPool, a pool of buffers, which prior to this commit was allowed to grow without limit.ChunkPoolis per-database, and so it is easy to imagine a scenario in which a large number of databases each occasionally allocate, use and never free very large chunks, resulting in the overall system's memory usage increasing over time.This commit adds some very simple limits on the heap usage of a
ChunkPool. Specifically, we introduce two constants,MAX_CHUNKS_IN_POOLandMAX_CHUNK_SIZE_IN_BYTES, and when returning a chunk to the pool, we free it if it violates these limits. The values of these constants, 32 and4xROW_ITER_CHUNK_SIZErespectively, were chosen entirely arbitrarily. Using a global pool would be helpful here.This also makes a change to use the
ChunkPoolwhen creating a newChunkedWriter. Before this, we were allocating a fresh chunk every time, which would eventually go into the chunkpool. That is probably why the chunk pool was growing so quickly.In the future we should at least add some metrics around the size of this pool.
API and ABI breaking changes
None.
Expected complexity level and risk
2 ish? Seems possible that my chosen values for the two constants are somehow pathologically bad, and this PR regresses performance.
Testing