Repository navigation
fix: OOM when uploading a large snapshot to S3 - #4994
Conversation
|
This PR is ready for review and feedback. Thank you for your time and consideration. |
| // Initialize CpuBudget to manage concurrency | ||
| let cpu_budget = CpuBudget::default(); | ||
| let max_concurrency = cpu_budget.available_cpu_budget(); | ||
|
|
There was a problem hiding this comment.
I will defer to repo maintainers here but I'm curious whether it is better to use available RAM here as a constraining factor instead of the number of available CPUs.
For example, a hard cap of 2GiB can be put on the uploading process (or alternatively, compute this threshold based on the available RAM) and then infer the concurrency limit based on the chunk size. A care would have to be taken not to exceed the limit when passing a buffer to the write method:
let mut copy = std::mem::take(&mut buffer);
copy.truncate(bytes_read);
write.put(copy.into()); // 1. write.write() is sync but a worker thread is spawned internally.
write.wait_for_capacity(...).await?;
buffer.resize(chunk_size, 0u8);There was a problem hiding this comment.
I am thinking of @generall / @timvisee review on the above suggestion. But still the wait_for_capacity expects for max concurrency param.
But we can calculate 'max_concurrency` based on available memory and pass it accordingly. Something it would be like this:
let memory_cap = 2 * 1024 * 1024 * 1024; // 2 GiB hard cap
let mut total_memory_used = 0;
// Determine max_concurrency based on the memory cap and chunk size
let max_concurrency = memory_cap / chunk_size;
while let Ok(bytes_read) = reader.read(&mut buffer) {
if bytes_read == 0 {
break;
}
// Calculate memory usage and truncate buffer if needed
total_memory_used += bytes_read;
if total_memory_used > memory_cap {
write.wait_for_capacity(max_concurrency).await?;
total_memory_used = 0; // Reset after flushing
}
let mut copy = std::mem::take(&mut buffer);
copy.truncate(bytes_read);
write.put(copy.into()); // 1. write.write() is sync but a worker thread is spawned internally.
buffer.resize(chunk_size, 0u8); // Prepare buffer for next chunk
}
…tion * fix oom issues and add logs to monitor memory usage under high contention --------- Signed-off-by: Vamshi Maskuri <[email protected]>
generall
left a comment
There was a problem hiding this comment.
Hey @varshith257, sorry for the late review. Changes look fine to me. Regarding limiting memory - it might be a second step. Current version already looks better then previous.
…tion (#4994) * fix oom issues and add logs to monitor memory usage under high contention --------- Signed-off-by: Vamshi Maskuri <[email protected]>
All Submissions:
devbranch. Did you create your branch fromdev?New Feature Submissions:
cargo +nightly fmt --allcommand prior to submission?cargo clippy --all --all-featurescommand?Changes to Core Features:
fixes #4991
/claim #4991