Skip to content

fix: OOM when uploading a large snapshot to S3 - #4994

Merged
generall merged 1 commit into
qdrant:devfrom
varshith257:fix-oom
Sep 14, 2024
Merged

generall merged 1 commit into
qdrant:devfrom
varshith257:fix-oom

Conversation

@varshith257

@varshith257 varshith257 commented Sep 1, 2024 •

Copy link
Copy Markdown
Contributor

All Submissions:

  • Contributions should target the dev branch. Did you create your branch from dev?
  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?

New Feature Submissions:

  1. Does your submission pass tests?
  2. Have you formatted your code locally using cargo +nightly fmt --all command prior to submission?
  3. Have you checked your code using cargo clippy --all --all-features command?

Changes to Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?

fixes #4991
/claim #4991

@varshith257

Copy link
Copy Markdown
Contributor Author

This PR is ready for review and feedback. Thank you for your time and consideration.

Comment on lines +117 to +120
// Initialize CpuBudget to manage concurrency
let cpu_budget = CpuBudget::default();
let max_concurrency = cpu_budget.available_cpu_budget();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense!

@varshith257 varshith257 Sep 1, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 generall left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@generall
generall merged commit e0ab73f into qdrant:dev Sep 14, 2024
@varshith257
varshith257 deleted the fix-oom branch September 15, 2024 03:43
timvisee pushed a commit that referenced this pull request Sep 17, 2024
…tion (#4994)

* fix oom issues and add logs to monitor memory usage under high contention

---------

Signed-off-by: Vamshi Maskuri <[email protected]>
@timvisee timvisee mentioned this pull request Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants