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(inverted_index): inverted index cache #4309

Merged
merged 14 commits into from
Jul 8, 2024

Conversation

v0y4g3r
Copy link
Contributor

@v0y4g3r v0y4g3r commented Jul 7, 2024

I hereby agree to the terms of the GreptimeDB CLA.

Refer to a related PR or issue link (optional)

What's changed and what's your intention?

This PR adds im-memory cache for inverted index that boost most queries by reducing the pruning cost, which reduces total cost by 2~3ms:
image

Future work

  • Cache for Puffin file metadata is still missing and I will not include that in this PR since PuffinReader has been refactored recently and I need to find a proper way to pass InvertedIndexCache to BoundedStager.

Checklist

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

Summary by CodeRabbit

  • New Features

    • Introduced caching functionality for inverted index data to improve performance.
    • Added configuration settings for managing cache sizes of inverted index metadata and content.
  • Improvements

    • Enhanced the SstIndexApplier to conditionally use the cache for index reading.
    • Modified methods to return shared ownership of metadata using Arc.
  • Configuration Updates

    • Added settings for inverted index metadata and content cache sizes in configuration files.
  • Testing Enhancements

    • Updated test cases to include new parameters for index cache.

 Update dependencies and add caching for inverted index reader

 - Updated `atomic` to 0.6.0 and `uuid` to 1.9.1 in `Cargo.lock`.
 - Added `moka` and `uuid` dependencies in `Cargo.toml`.
 - Introduced `seek_read` method in `InvertedIndexBlobReader` for common seek and read operations.
 - Added `cache.rs` module to implement caching for inverted index reader using `moka`.
 - Updated `async-compression` to 0.4.11 in `puffin/Cargo.toml`.
 Refactor InvertedIndexReader and Add Index Cache Support

 - Refactored `InvertedIndexReader` to include `seek_read` method and default implementations for `fst` and `bitmap`.
 - Implemented `seek_read` in `InvertedIndexBlobReader` and `CachedInvertedIndexBlobReader`.
 - Introduced `InvertedIndexCache` in `CacheManager` and `SstIndexApplier`.
 - Updated `SstIndexApplierBuilder` to accept and utilize `InvertedIndexCache`.
 - Added `From<FileId> for Uuid` implementation.
 Update Cargo.toml and refactor SstIndexApplier

 - Moved `uuid.workspace` entry in Cargo.toml for better organization.
Copy link
Contributor

coderabbitai bot commented Jul 7, 2024

Important

Review skipped

Review was skipped due to path filters

Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock

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

Walkthrough

The updates include the addition and enhancement of methods to the InvertedIndexReader trait, restructuring the CacheManager to manage index caching, introducing new modules for index caching, and modifying test cases accordingly. Additionally, it includes changing various signature methods and utilizing Arc to manage shared metadata efficiently.

Changes

File/Path Change Summary
src/index/Cargo.toml Added uuid as a workspace dependency.
src/index/src/inverted_index/.../reader.rs Added read_all and seek_read methods and modified return types for metadata, fst, and bitmap.
src/index/src/inverted_index/.../blob.rs Adjusted method signatures to use Arc for metadata and renamed methods for reading data.
src/index/src/inverted_index/.../predicates_apply.rs Modified mock_metas function to use Arc for shared metadata ownership.
src/mito2/src/cache.rs Added index module with caching declarations and methods in CacheManager and CacheManagerBuilder.
src/mito2/src/cache/index.rs Introduced CachedInvertedIndexBlobReader struct and related caching implementations.
src/mito2/src/config.rs Added cache size fields for inverted index metadata and content to MitoConfig.
src/mito2/src/read/scan_region.rs Introduced index_cache variable and passed it to SstIndexApplierBuilder.
src/mito2/src/sst/file.rs Added conversion implementation from FileId to Uuid.
src/mito2/src/sst/.../applier.rs Added inverted_index_cache field, modified apply method, and updated blob reader creation conditions.
src/mito2/src/sst/.../builder.rs Added index_cache field and parameter to SstIndexApplierBuilder.
src/mito2/src/sst/.../between.rs Updated test cases to include None for the index_cache parameter.
src/mito2/src/sst/.../comparison.rs Updated test cases to include None for the index_cache parameter.
src/mito2/src/sst/.../eq_list.rs Updated test cases to include None for the index_cache parameter.
src/mito2/src/sst/.../in_list.rs Updated test cases to include None for the index_cache parameter.
src/mito2/src/sst/.../regex_match.rs Updated test cases to include None for the index_cache parameter.
src/mito2/src/sst/.../creator.rs Added usage of InvertedIndexCache and passed it to the builder.
src/mito2/src/worker.rs Added index metadata and content size configurations.
tests-integration/tests/http.rs Added configuration settings for cache sizes for inverted index metadata and content.

Poem

In the lines of Rust we weave,
With bytes and cache we achieve,
Indexes cached, with data vast,
Efficiency and speed to outlast.

Oh, code of magic, so refined,
Full of treasures left behind,
We celebrate this feat so grand,
In the realm of data we now stand. 🌟


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.

@github-actions github-actions bot added the docs-not-required This change does not impact docs. label Jul 7, 2024
 Refactor InvertedIndexCache to use type alias for Arc

 - Replaced `Arc<InvertedIndexCache>` with `InvertedIndexCacheRef` type alias.
 Add Prometheus metrics and caching improvements for inverted index

 - Introduced `prometheus` and `puffin` dependencies for metrics.
 Refactor InvertedIndexReader and Cache handling

 - Simplified `InvertedIndexReader` trait by removing seek-related comments.
 Add configurable cache sizes for inverted index metadata and content
 - Introduced `index_metadata_size` and `index_content_size` in `CacheManagerBuilder`.
 Refactor and optimize inverted index caching

 - Removed `metrics.rs` and integrated cache metrics into `index.rs`.
 Remove unused dependencies from Cargo.lock and Cargo.toml

 - Removed `moka`, `prometheus`, and `puffin` dependencies from both Cargo.lock and Cargo.toml.
@v0y4g3r v0y4g3r force-pushed the feat/inverted-index-cache branch from 5abcaef to 611109a Compare July 7, 2024 16:51
@v0y4g3r v0y4g3r marked this pull request as ready for review July 8, 2024 02:48
@v0y4g3r v0y4g3r requested review from evenyag, waynexia, zhongzc and a team as code owners July 8, 2024 02:48
@killme2008
Copy link
Contributor

Do we have any benchmark program and results for this PR? @v0y4g3r

@v0y4g3r
Copy link
Contributor Author

v0y4g3r commented Jul 8, 2024

Do we have any benchmark program and results for this PR? @v0y4g3r

I updated the PR description. This PR improves those queries which are already fast, but not fast enough, namely those 5~6ms queries. An improvement of 2 to 3ms means cutting the total cost time by half.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 58f991b and 611109a.

Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
Files selected for processing (19)
  • src/index/Cargo.toml (1 hunks)
  • src/index/src/inverted_index/format/reader.rs (1 hunks)
  • src/index/src/inverted_index/format/reader/blob.rs (2 hunks)
  • src/index/src/inverted_index/search/index_apply/predicates_apply.rs (4 hunks)
  • src/mito2/src/cache.rs (7 hunks)
  • src/mito2/src/cache/index.rs (1 hunks)
  • src/mito2/src/config.rs (2 hunks)
  • src/mito2/src/read/scan_region.rs (1 hunks)
  • src/mito2/src/sst/file.rs (1 hunks)
  • src/mito2/src/sst/index/inverted_index/applier.rs (7 hunks)
  • src/mito2/src/sst/index/inverted_index/applier/builder.rs (6 hunks)
  • src/mito2/src/sst/index/inverted_index/applier/builder/between.rs (5 hunks)
  • src/mito2/src/sst/index/inverted_index/applier/builder/comparison.rs (4 hunks)
  • src/mito2/src/sst/index/inverted_index/applier/builder/eq_list.rs (7 hunks)
  • src/mito2/src/sst/index/inverted_index/applier/builder/in_list.rs (5 hunks)
  • src/mito2/src/sst/index/inverted_index/applier/builder/regex_match.rs (4 hunks)
  • src/mito2/src/sst/index/inverted_index/creator.rs (2 hunks)
  • src/mito2/src/worker.rs (1 hunks)
  • tests-integration/tests/http.rs (1 hunks)
Files skipped from review due to trivial changes (5)
  • src/index/Cargo.toml
  • src/mito2/src/sst/index/inverted_index/applier/builder/between.rs
  • src/mito2/src/sst/index/inverted_index/applier/builder/comparison.rs
  • src/mito2/src/sst/index/inverted_index/applier/builder/eq_list.rs
  • src/mito2/src/sst/index/inverted_index/applier/builder/in_list.rs
Additional comments not posted (46)
src/index/src/inverted_index/format/reader.rs (5)

34-34: LGTM!

The read_all method is correctly implemented, reading all data into the destination vector and returning the size of the data read.


37-37: LGTM!

The seek_read method is correctly implemented, seeking to the given offset and reading the specified size of data.


40-40: LGTM!

The metadata method correctly retrieves and returns the metadata wrapped in an Arc for shared ownership.


43-46: LGTM!

The fst method correctly retrieves the FST map by using the seek_read method and creating an FstMap from the retrieved data.


49-51: LGTM!

The bitmap method correctly retrieves the bitmap by using the seek_read method and creating a BitVec from the retrieved data.

src/mito2/src/sst/index/inverted_index/applier/builder/regex_match.rs (5)

Line range hint 10-31:
LGTM!

The collect_regex_match method correctly collects a regex match expression, performing necessary checks and conditions to ensure validity.


Line range hint 61-80:
LGTM!

The test_regex_match_basic test correctly verifies the basic functionality of the collect_regex_match method.


Line range hint 95-104:
LGTM!

The test_regex_match_field_column test correctly verifies that the collect_regex_match method does not add a regex match predicate for field columns.


Line range hint 117-126:
LGTM!

The test_regex_match_type_mismatch test correctly verifies that the collect_regex_match method does not add a regex match predicate when there is a type mismatch.


Line range hint 139-143:
LGTM!

The test_regex_match_type_nonexist_column test correctly verifies that the collect_regex_match method returns an error when the column does not exist.

src/mito2/src/cache/index.rs (12)

41-49: LGTM!

The CachedInvertedIndexBlobReader constructor correctly initializes the struct with the provided file ID, inner reader, and cache.


57-83: LGTM!

The get_or_load method correctly retrieves index data from the cache or loads it from the source if not already cached, handling errors and edge cases appropriately.


89-94: LGTM!

The read_all method correctly delegates the read operation to the inner reader and returns the size of the data read.


96-102: LGTM!

The seek_read method correctly delegates the seek and read operations to the inner reader and returns the data read.


104-113: LGTM!

The metadata method correctly retrieves metadata from the cache or loads it from the source if not already cached, handling errors and edge cases appropriately.


116-124: LGTM!

The fst method correctly retrieves the FST map by using the get_or_load method and creating an FstMap from the retrieved data.


149-177: LGTM!

The InvertedIndexCache constructor correctly initializes the cache with the provided metadata and content capacities, setting up eviction listeners to update metrics.


181-183: LGTM!

The get_index_metadata method correctly retrieves metadata from the cache for the specified file ID.


185-191: LGTM!

The put_index_metadata method correctly inserts metadata into the cache for the specified file ID and updates the metrics.


194-196: LGTM!

The get_index method correctly retrieves index data from the cache for the specified key.


198-203: LGTM!

The put_index method correctly inserts index data into the cache for the specified key and updates the metrics.


207-214: LGTM!

The index_metadata_weight and index_content_weight methods correctly calculate the weight of the metadata and content for caching purposes.

src/index/src/inverted_index/format/reader/blob.rs (2)

53-58: LGTM!

The read_all method correctly seeks to the start and reads all data into the destination vector, handling errors appropriately.


61-69: LGTM!

The seek_read method correctly seeks to the given offset and reads the specified size of data into a buffer, handling errors appropriately.

src/mito2/src/sst/file.rs (1)

68-72: LGTM!

The implementation of the From trait for FileId to Uuid conversion is correct.

src/mito2/src/sst/index/inverted_index/applier.rs (4)

59-61: LGTM! Ensure proper utilization of the cache.

The addition of the inverted_index_cache field and its inclusion in the new method is correct.

Ensure that the cache is properly utilized in the rest of the codebase.

Also applies to: 73-86


108-125: LGTM! Verify the correct usage of CachedInvertedIndexBlobReader.

The changes to use CachedInvertedIndexBlobReader if inverted_index_cache is available are correct.

Ensure that CachedInvertedIndexBlobReader is correctly implemented and used.


220-220: LGTM!

The addition of the index_cache parameter in the test_index_applier_apply_basic function is correct.


262-262: LGTM!

The addition of the index_cache parameter in the test_index_applier_apply_invalid_blob_type function is correct.

src/mito2/src/sst/index/inverted_index/applier/builder.rs (2)

66-68: LGTM! Ensure proper utilization of the cache.

The addition of the index_cache field and its inclusion in the new method is correct.

Ensure that the cache is properly utilized in the rest of the codebase.

Also applies to: 77-89


331-331: LGTM!

The addition of the index_cache parameter in the test_collect_and_basic function is correct.

src/index/src/inverted_index/search/index_apply/predicates_apply.rs (2)

Line range hint 166-180:
LGTM!

The update to return Arc<InvertedIndexMetas> in the mock_metas function is correct.


305-309: LGTM!

The update to use Arc<InvertedIndexMetas> in the test_index_applier_with_empty_index function is correct.

src/mito2/src/cache.rs (4)

20-20: LGTM! Addition of index module.

The addition of the index module aligns with the purpose of introducing caching mechanisms for inverted index.


64-65: LGTM! Addition of index_cache field in CacheManager.

The addition of the index_cache field is appropriate for managing the cache for inverted index.


175-177: LGTM! Addition of index_cache method in CacheManager.

The method provides access to the index_cache field, which is essential for managing the cache for inverted index.


216-226: LGTM! Addition of index_metadata_size and index_content_size methods in CacheManagerBuilder.

These methods allow configuring the cache sizes for index metadata and content, which is crucial for managing the cache effectively.

src/mito2/src/config.rs (2)

87-90: LGTM! Addition of inverted_index_metadata_cache_size and inverted_index_cache_size fields in MitoConfig.

These fields are essential for configuring the cache sizes for inverted index metadata and content, and their default values are set appropriately.


140-141: LGTM! Default values for inverted_index_metadata_cache_size and inverted_index_cache_size in MitoConfig::default.

The default values of 32MB for both fields are reasonable and align with typical cache size configurations.

src/mito2/src/sst/index/inverted_index/creator.rs (1)

418-423: LGTM! Usage of InvertedIndexCache in SstIndexApplierBuilder.

The integration of the InvertedIndexCache in the SstIndexApplierBuilder is crucial for leveraging the caching mechanisms introduced in this PR.

src/mito2/src/read/scan_region.rs (2)

333-338: LGTM!

The index_cache variable is correctly initialized by safely accessing and cloning the value from the cache_manager.


343-343: LGTM!

The index_cache variable is correctly passed to the SstIndexApplierBuilder::new method, enabling caching functionality.

src/mito2/src/worker.rs (2)

161-161: Good addition: Index metadata size configuration.

The addition of index_metadata_size configuration enhances the cache management for index metadata.


162-162: Good addition: Index content size configuration.

The addition of index_content_size configuration enhances the cache management for index content.

tests-integration/tests/http.rs (2)

823-823: Good addition: Inverted index metadata cache size configuration.

The addition of inverted_index_metadata_cache_size configuration in the config API test ensures the new feature is tested.


824-824: Good addition: Inverted index cache size configuration.

The addition of inverted_index_cache_size configuration in the config API test ensures the new feature is tested.

Copy link

codecov bot commented Jul 8, 2024

Codecov Report

Attention: Patch coverage is 91.07981% with 19 lines in your changes missing coverage. Please review.

Project coverage is 84.85%. Comparing base (e5730a3) to head (b42a932).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4309      +/-   ##
==========================================
- Coverage   85.11%   84.85%   -0.27%     
==========================================
  Files        1060     1061       +1     
  Lines      187648   187832     +184     
==========================================
- Hits       159720   159387     -333     
- Misses      27928    28445     +517     

src/mito2/src/config.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@zhongzc zhongzc left a comment

Choose a reason for hiding this comment

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

most LGTM

src/mito2/src/cache/index.rs Outdated Show resolved Hide resolved
src/mito2/src/cache/index.rs Outdated Show resolved Hide resolved
 Replace Uuid with FileId in CachedInvertedIndexBlobReader

 - Updated `file_id` type from `Uuid` to `FileId` in `CachedInvertedIndexBlobReader` and related methods.
 Refactor cache configuration for inverted index

 - Moved `inverted_index_metadata_cache_size` and `inverted_index_cache_size` from `MitoConfig` to `InvertedIndexConfig`.
@v0y4g3r v0y4g3r requested a review from evenyag July 8, 2024 09:31
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 611109a and 9b457d9.

Files selected for processing (5)
  • src/mito2/src/cache/index.rs (1 hunks)
  • src/mito2/src/config.rs (2 hunks)
  • src/mito2/src/sst/file.rs (1 hunks)
  • src/mito2/src/worker.rs (1 hunks)
  • tests-integration/tests/http.rs (1 hunks)
Files skipped from review as they are similar to previous changes (5)
  • src/mito2/src/cache/index.rs
  • src/mito2/src/config.rs
  • src/mito2/src/sst/file.rs
  • src/mito2/src/worker.rs
  • tests-integration/tests/http.rs

 Remove unnecessary conversion of `file_id` in `SstIndexApplier`

 - Simplified the initialization of `CachedInvertedIndexBlobReader` by removing the redundant `into()` conversion for `file_id`.
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 9b457d9 and 31945be.

Files selected for processing (1)
  • src/mito2/src/sst/index/inverted_index/applier.rs (7 hunks)
Additional comments not posted (5)
src/mito2/src/sst/index/inverted_index/applier.rs (5)

60-61: Addition of inverted_index_cache field is appropriate.

The inverted_index_cache field is correctly added to the SstIndexApplier struct to hold an optional reference to an inverted index cache.


Line range hint 73-86: Changes to the new method are appropriate.

The new method correctly accepts an additional parameter index_cache and assigns it to the inverted_index_cache field.


109-125: Changes to the apply method are appropriate.

The apply method correctly includes logic to use CachedInvertedIndexBlobReader if inverted_index_cache is available, enhancing the efficiency of the index application process.


220-220: Changes to the test_index_applier_apply_basic test are appropriate.

The test correctly passes None for the index_cache parameter to the new method of SstIndexApplier, aligning with the updated method signature.


262-262: Changes to the test_index_applier_apply_invalid_blob_type test are appropriate.

The test correctly passes None for the index_cache parameter to the new method of SstIndexApplier, aligning with the updated method signature.

@v0y4g3r v0y4g3r added this pull request to the merge queue Jul 8, 2024
Merged via the queue into GreptimeTeam:main with commit aa4d10e Jul 8, 2024
51 of 52 checks passed
@v0y4g3r v0y4g3r deleted the feat/inverted-index-cache branch July 8, 2024 12:57
Comment on lines +842 to +843
metadata_cache_size = "32MiB"
content_cache_size = "32MiB"
Copy link
Contributor

Choose a reason for hiding this comment

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

Try to make their default value auto.

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.

None yet

4 participants