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

feat(bloom-filter): add memory control for creator #5185

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

zhongzc
Copy link
Contributor

@zhongzc zhongzc commented Dec 17, 2024

I hereby agree to the terms of the GreptimeDB CLA.

Refer to a related PR or issue link (optional)

#5176

What's changed and what's your intention?

  1. Refactor ExternalTempFileProvider to adapt to new index intermediates
  2. Extract FinalizedBloomFilterStorage to a new file
  3. Complete TODOs in FinalizedBloomFilterStorage:
    1. Implement true global memory control by tracking overall memory usage for creating bloom filters through global memory usage
    2. Implement disk flushing behavior during memory flush using injected ExternalTempFileProvider
    3. Update drain to return FinalizedBloomFilterSegment from both intermediate files and memory as a stream to the upper layer

Checklist

  • I have written the necessary rustdoc comments.
  • I have added the necessary unit tests and integration tests.
  • This PR requires documentation updates.

@zhongzc zhongzc requested review from evenyag, v0y4g3r, waynexia and a team as code owners December 17, 2024 12:10
Copy link
Contributor

coderabbitai bot commented Dec 17, 2024

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@zhongzc zhongzc mentioned this pull request Dec 17, 2024
15 tasks
@github-actions github-actions bot added the docs-not-required This change does not impact docs. label Dec 17, 2024
Copy link

codecov bot commented Dec 17, 2024

Codecov Report

Attention: Patch coverage is 99.35065% with 2 lines in your changes missing coverage. Please review.

Project coverage is 83.84%. Comparing base (c33cf59) to head (ebafc06).
Report is 18 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5185      +/-   ##
==========================================
- Coverage   84.05%   83.84%   -0.22%     
==========================================
  Files        1175     1182       +7     
  Lines      218970   219558     +588     
==========================================
+ Hits       184065   184084      +19     
- Misses      34905    35474     +569     

@waynexia waynexia requested a review from Copilot December 18, 2024 06:37

Choose a reason for hiding this comment

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

Copilot reviewed 6 out of 13 changed files in this pull request and generated no comments.

Files not reviewed (7)
  • src/index/src/inverted_index/create/sort.rs: Evaluated as low risk
  • src/mito2/src/sst/index/inverted_index/creator/temp_provider.rs: Evaluated as low risk
  • src/index/src/lib.rs: Evaluated as low risk
  • src/index/src/bloom_filter/creator.rs: Evaluated as low risk
  • src/index/src/inverted_index/create/sort/external_sort.rs: Evaluated as low risk
  • src/index/src/inverted_index/error.rs: Evaluated as low risk
  • src/index/src/bloom_filter/error.rs: Evaluated as low risk
@discord9 discord9 self-requested a review December 19, 2024 07:04
@discord9
Copy link
Contributor

discord9 commented Dec 19, 2024

a few questions on bloom filter perf:

  • why do we need to set seed explictly to a fixed value for bloom filter? Can we have a random seed for each bloom filter created, and save those seeds to memory/disk too(I see now keeping the same seed is useful for read/write after persisent) But it wouldn't need to be the same across all codebase right? Having a fixed seed also weaken SipHash's protection against DDoS if I understand correctly
  • can we switch the hasher of bloom filter to ahash and set the seed randomly?(Which should yield even faster perf gain per author of fastbloom from ~100 ns using SipHash to ~7 ns using ahash(but ahash is not ddos-safe so seed need to be set randomly, which is it's default behavior)

creator
.push_row_elems(vec![b"c".to_vec(), b"d".to_vec()])
.await
.unwrap();
// Finalize the first segment
assert!(creator.cur_seg_distinct_elems_mem_usage == 0);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
assert!(creator.cur_seg_distinct_elems_mem_usage == 0);
assert_eq!(creator.cur_seg_distinct_elems_mem_usage, 0);

}

self.bloom_filter_buf.clear();
write_u64_slice(&mut self.bloom_filter_buf, bf.as_slice());
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can the write_u64_slice method be moved from creator.rs to here?

self.bloom_filter_buf.clear();
write_u64_slice(&mut self.bloom_filter_buf, bf.as_slice());
let fbf = FinalizedBloomFilterSegment {
bloom_filter_bytes: self.bloom_filter_buf.clone(),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Question: Why do we need the buf(self.bloom_filter_buf) ?Can we move bytes to avoid cloning?

Copy link
Contributor

Choose a reason for hiding this comment

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

maybe even use bytes::Bytes for bloom_filter_buf and Vec<bytes::Bytes> for bloom_filter_bytes to make sure of zero copy?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Question: Why do we need the buf(self.bloom_filter_buf) ?Can we move bytes to avoid cloning?

Good advice, this buf is really not needed

@zhongzc
Copy link
Contributor Author

zhongzc commented Dec 19, 2024

a few questions on bloom filter perf:

  • why do we need to set seed explictly to a fixed value for bloom filter? Can we have a random seed for each bloom filter created, and save those seeds to memory/disk too(I see now keeping the same seed is useful for read/write after persisent) But it wouldn't need to be the same across all codebase right? Having a fixed seed also weaken SipHash's protection against DDoS if I understand correctly
  • can we switch the hasher of bloom filter to ahash and set the seed randomly?(Which should yield even faster perf gain per author of fastbloom from ~100 ns using SipHash to ~7 ns using ahash(but ahash is not ddos-safe so seed need to be set randomly, which is it's default behavior)

Thanks for the suggestion, the calculation of the bloom filter is not a performance bottleneck at the moment, so I'm willing to keep it simple in this regard.

/// # Format
///
/// [ elem count ][ size ][ bloom filter ][ elem count ][ size ][ bloom filter ]...
/// |<- u64 LE ->||<- u64 LE ->||<- bf bytes ->||<- u64 LE ->||<- u64 LE ->||<- bf bytes ->|...
Copy link
Contributor

Choose a reason for hiding this comment

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

would a version number to added to file header too? Just in case future update might change file format?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs-not-required This change does not impact docs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants