-
Notifications
You must be signed in to change notification settings - Fork 716
fix: add file list id cache #4643
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
Conversation
WalkthroughThis pull request introduces various changes across multiple files, primarily focusing on configuration updates, database access improvements, and modifications to file list management. Key updates include adding a new field for Changes
Possibly related PRs
Suggested labels
Suggested reviewers
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 11
🧹 Outside diff range and nitpick comments (16)
src/infra/src/lib.rs (1)
30-31: LGTM! Consider enhancing error handling.The addition of local cache initialization and garbage collection is well-placed in the initialization sequence and aligns with the PR objectives.
Consider adding more specific error handling or logging for these new operations to aid in troubleshooting if initialization fails. For example:
file_list::LOCAL_CACHE.create_table().await.map_err(|e| { log::error!("Failed to create local cache table: {}", e); e })?; file_list::local_cache_gc().await.map_err(|e| { log::error!("Failed to initialize local cache garbage collection: {}", e); e })?;This would provide more context in case of failures during the initialization process.
src/infra/src/dist_lock.rs (1)
58-60: Approve the early return for None locker, with a minor suggestion.The addition of an early return when
lockerisNoneis a good optimization. It prevents unnecessary processing and aligns well with Rust's idiomatic use ofOption.Consider adding a debug log before the early return to maintain observability:
if locker.is_none() { log::debug!("[trace_id {trace_id}] No locker to unlock"); return Ok(()); }This will help in tracking the flow of the function, especially in cases where
Nonelockers are expected.src/job/mod.rs (1)
182-185: LGTM with a minor suggestion for error handling.The addition of
infra_file_list::LOCAL_CACHE.create_table_index()aligns well with the PR objective of implementing a local SQLite cache. The placement and error handling are consistent with the existing code.Consider using a more descriptive error message in the
expect()call to differentiate it from the remote cache index creation:infra_file_list::LOCAL_CACHE .create_table_index() .await .expect("local file list cache: create table index failed");src/infra/src/db/mod.rs (2)
44-46: LGTM: Addition of get_local_cache functionThe
get_local_cachefunction is well-implemented and consistent with the existing code structure. It correctly usesget_or_initfor lazy initialization of the local cache.Consider adding a brief documentation comment explaining the purpose of this function and when it should be used, similar to other public functions in the module.
81-83: LGTM: Addition of init_local_cache functionThe
init_local_cachefunction is correctly implemented to create a default SQLite database instance for the local cache, aligning with the PR objectives.Consider adding error handling to this function. If the SQLite database initialization fails, it might be helpful to log the error or return a
Resulttype instead of panicking. This would allow for more graceful error handling in theget_local_cachefunction.Example:
async fn init_local_cache() -> Result<Box<dyn Db>> { Box::<sqlite::SqliteDb>::default().map_err(|e| { log::error!("Failed to initialize local cache: {}", e); Error::DbError(DbError::InitializationError(e.to_string())) }) }Then update
get_local_cacheto handle potential errors:pub async fn get_local_cache() -> Result<&'static Box<dyn Db>> { LOCAL_CACHE.get_or_try_init(init_local_cache).await }src/service/db/file_list/remote.rs (1)
106-106: LGTM! Consider adding a log message for the local cache index creation.The addition of
infra_file_list::LOCAL_CACHE.create_table_index().await?is a good implementation of the local caching mechanism described in the PR objectives. It's correctly placed after the main index creation.Consider adding a log message after this line to indicate the successful creation of the local cache index, similar to the log message for the main index. This would improve observability:
log::info!("Load file_list create local cache table index done");src/infra/src/file_list/postgres.rs (3)
81-83: Implement thebatch_add_with_idmethod.The
batch_add_with_idmethod is currently unimplemented. This could lead to runtime panics if called.Would you like assistance in implementing this method? I can provide a basic implementation or open a GitHub issue to track this task.
510-512: Implement theclean_by_min_pk_valuemethod.The
clean_by_min_pk_valuemethod is currently unimplemented. This could lead to runtime panics if called.Would you like assistance in implementing this method? I can provide a basic implementation or open a GitHub issue to track this task.
Line range hint
1-1293: Overall review: Good progress, but some methods need implementation.The changes made to this file, particularly the
query_by_idsandget_min_pk_valuemethods, are well-implemented and consistent with the codebase. However, thebatch_add_with_idandclean_by_min_pk_valuemethods are still unimplemented, which could lead to runtime issues if called.Consider adding appropriate error handling or temporary implementations for these methods to prevent potential runtime panics. Additionally, it might be helpful to add TODO comments explaining the intended functionality of these methods for future implementation.
src/config/src/config.rs (2)
1429-1430: Approved: Extended external meta store detectionThe
check_common_configfunction has been updated to include MySQL and PostgreSQL as potential external meta stores. This change aligns with the PR objectives of enhancing data retrieval processes.Consider adding error handling for cases where the necessary connection details for MySQL or PostgreSQL are not provided, similar to the existing checks for these databases later in the function.
Line range hint
1-1730: Consider improving code organization and documentationWhile the changes made are appropriate and the existing code is well-structured, consider the following suggestions to further improve the file:
- Group related configuration structs and their implementation blocks together to improve readability.
- Add more inline documentation for complex configuration options, explaining their purpose and potential impacts.
- Consider breaking down large functions like
init()into smaller, more focused functions to improve maintainability.- Add unit tests for new configuration options and edge cases introduced by these changes.
These suggestions are not directly related to the current changes but could improve the overall quality and maintainability of the configuration code.
src/infra/src/file_list/mod.rs (2)
429-430: Clarify comment regardinginterval.tick().awaitThe call to
interval.tick().awaitdelays the first tick to after the interval duration, preventing immediate execution. Update the comment to reflect this behavior.Apply this diff to correct the comment:
- interval.tick().await; // the first tick is immediate + interval.tick().await; // delay the first tick to after the interval duration
449-461: Review the use of#[sqlx(default)]attributesEnsure that the
#[sqlx(default)]attributes on theid,deleted, andflattenedfields are necessary and align with the database schema.src/infra/src/file_list/sqlite.rs (1)
965-966: Refactor to reduce code duplicationLines 965-966 show that
inner_addnow callsinner_add_with_idwithNoneas the ID:self.inner_add_with_id(table, None, file, meta).awaitWhile this is a valid approach, consider whether both methods are necessary or if
inner_add_with_idcan be used exclusively, simplifying the codebase.src/infra/src/file_list/mysql.rs (2)
81-83: Unimplemented methodbatch_add_with_idThe method
batch_add_with_idis currently unimplemented and contains atodo!()macro. If this method is required for the cache implementation or future functionality, please implement it or provide a timeline for its completion.Would you like assistance in implementing this method or opening a GitHub issue to track its progress?
496-498: Unimplemented methodclean_by_min_pk_valueThe method
clean_by_min_pk_valueis currently unimplemented and contains atodo!()macro. Please implement this method to handle cache cleanup based on the minimum primary key value or provide an estimated time for its completion.Would you like assistance in implementing this method or opening a GitHub issue to track this task?
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (14)
- src/config/src/config.rs (2 hunks)
- src/infra/src/db/mod.rs (2 hunks)
- src/infra/src/dist_lock.rs (1 hunks)
- src/infra/src/file_list/mod.rs (8 hunks)
- src/infra/src/file_list/mysql.rs (11 hunks)
- src/infra/src/file_list/postgres.rs (5 hunks)
- src/infra/src/file_list/sqlite.rs (9 hunks)
- src/infra/src/lib.rs (1 hunks)
- src/job/mod.rs (1 hunks)
- src/service/db/file_list/remote.rs (1 hunks)
- src/service/file_list.rs (8 hunks)
- src/service/search/cluster/flight.rs (3 hunks)
- src/service/search/grpc/flight.rs (5 hunks)
- src/service/search/grpc/storage.rs (1 hunks)
🧰 Additional context used
📓 Path-based instructions (14)
src/config/src/config.rs (1)
Pattern
**/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestionsMake sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.
src/infra/src/db/mod.rs (1)
Pattern
**/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestionsMake sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.
src/infra/src/dist_lock.rs (1)
Pattern
**/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestionsMake sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.
src/infra/src/file_list/mod.rs (1)
Pattern
**/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestionsMake sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.
src/infra/src/file_list/mysql.rs (1)
Pattern
**/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestionsMake sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.
src/infra/src/file_list/postgres.rs (1)
Pattern
**/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestionsMake sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.
src/infra/src/file_list/sqlite.rs (1)
Pattern
**/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestionsMake sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.
src/infra/src/lib.rs (1)
Pattern
**/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestionsMake sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.
src/job/mod.rs (1)
Pattern
**/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestionsMake sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.
src/service/db/file_list/remote.rs (1)
Pattern
**/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestionsMake sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.
src/service/file_list.rs (1)
Pattern
**/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestionsMake sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.
src/service/search/cluster/flight.rs (1)
Pattern
**/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestionsMake sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.
src/service/search/grpc/flight.rs (1)
Pattern
**/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestionsMake sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.
src/service/search/grpc/storage.rs (1)
Pattern
**/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestionsMake sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.
🔇 Additional comments not posted (27)
src/infra/src/db/mod.rs (2)
36-36: LGTM: Addition of LOCAL_CACHE static variableThe addition of the
LOCAL_CACHEstatic variable usingOnceCellis consistent with the existing code structure and aligns with the PR objective of implementing a local cache. This approach ensures thread-safe lazy initialization.
Line range hint
1-359: Overall assessment: Changes look good with minor suggestionsThe implemented changes for adding a local SQLite cache are well-structured and align with the PR objectives. The code is consistent with the existing patterns in the file. Consider implementing the suggested improvements for documentation and error handling to further enhance the robustness of the implementation.
src/service/search/grpc/storage.rs (1)
555-559: Improved logging for bitmap resultsThe change replaces logging the entire bitmap with logging its length, which is a good improvement for large bitmaps. This change reduces log verbosity and potential performance impact for large result sets.
However, consider the following suggestions:
- Add a debug-level log that includes the full bitmap for troubleshooting purposes.
- Consider using a structured logging approach for better parsing and analysis of logs.
Here's an example of how you could implement these suggestions:
log::info!( - "[trace_id {}] search->storage: Final bitmap for fts_terms {:?} and index_terms: {:?} length {}", - query.trace_id, - *full_text_terms, - index_terms, - res.len(), + "Final bitmap results for search terms"; + "trace_id" => query.trace_id, + "fts_terms" => ?*full_text_terms, + "index_terms" => ?index_terms, + "bitmap_length" => res.len() ); +log::debug!( + "Full bitmap result"; + "trace_id" => query.trace_id, + "bitmap" => ?res +);This change maintains the performance improvement while providing more detailed logging when needed.
src/infra/src/file_list/postgres.rs (2)
Line range hint
362-394: LGTM:query_by_idsmethod update looks good.The changes to the
query_by_idsmethod, including the updated return type and the inclusion of theidin the query and result mapping, are well-implemented and consistent.
501-508: LGTM: Newget_min_pk_valuemethod is well-implemented.The
get_min_pk_valuemethod is correctly implemented to retrieve the minimum primary key value from thefile_listtable. This could be useful for various operations requiring knowledge of the oldest record.src/config/src/config.rs (1)
991-992: Approved: Increased maximum file size for compactionThe default value for
max_file_sizehas been increased from 256 MB to 512 MB. This change aligns with the PR objectives of enhancing efficiency in file retrieval processes. However, be aware that this may impact memory usage and processing time for larger files during compaction.src/service/file_list.rs (2)
68-128: New caching logic inquery_by_idsenhances performanceThe updated
query_by_idsfunction introduces local caching, effectively reducing remote database calls. The implementation correctly handles cached and uncached IDs and updates the local cache as needed.
228-236: Improved error handling indelete_parquet_file_s3functionMapping errors to
Error::Messageprovides clearer error reporting, aligning with the overall error handling strategy.src/service/search/grpc/flight.rs (4)
Line range hint
155-165: Correctly updated function call with error handlingThe
get_file_list_by_idsfunction is now called with the additionaltrace_idparameter, and the returnedResultis appropriately handled using.await?.
Line range hint
273-282: Improved function signature for better traceability and error handlingThe
get_file_list_by_idsfunction now includestrace_idin its parameters, enhancing logging and traceability. The return type has been updated toResult<(Vec<FileKey>, usize), Error>, allowing for proper error propagation.
284-284: Proper error handling inquery_by_idscallThe call to
query_by_idsnow includestrace_idand correctly handles errors using the?operator, ensuring that any errors are propagated upwards.
326-326: Returning Result with accurate timing informationThe function now returns a
Resultcontaining thefilesand the elapsed time in milliseconds, ensuring accurate performance metrics.src/infra/src/file_list/mod.rs (6)
34-34: Initialization ofLOCAL_CACHEis appropriateThe use of
Lazyto initializeLOCAL_CACHEensures thread-safe lazy initialization.
46-48: New functionconnect_local_cacheadded correctlyThe function
connect_local_cachecorrectly returns a defaultSqliteFileList, facilitating local caching.
58-58: New methodbatch_add_with_idadded toFileListtraitThe method
batch_add_with_idis correctly defined in theFileListtrait.
105-106: New methodsget_min_pk_valueandclean_by_min_pk_valueaddedThe methods are correctly added to the
FileListtrait for cache garbage collection purposes.
306-308: Functionget_min_pk_valuecorrectly implementedThe function correctly delegates to
CLIENT.get_min_pk_value().await.
421-445: Implementation oflocal_cache_gcis appropriateThe background task for garbage collection is correctly implemented using
tokio::task::spawnand runs at the specified hourly interval.src/service/search/cluster/flight.rs (3)
121-124: Verify the Correctness ofuse_fst_inverted_indexAssignmentThe variable
use_fst_inverted_indexis assigned from theget_inverted_index_file_listsfunction and then used to setreq.use_inverted_index. Ensure that the logic determininguse_fst_inverted_indexis correct and aligns with the intended behavior, especially considering different configurations ofinverted_index_type.
799-800: Ensure Early Return Handles All Cases AppropriatelyThe early return when
!use_parquet_inverted_indexis true results in:return Ok((use_fst_inverted_index, vec![], 0, 0));Verify that returning empty
idx_file_list, and zero values foridx_scan_sizeandidx_tookis appropriate in this context. Ensure that downstream code properly handles these values without causing unintended side effects.
822-827: Check Consistency of Return ValuesAt the end of the
get_inverted_index_file_listsfunction, the return statement is:Ok(( use_fst_inverted_index, idx_file_list, idx_scan_size, idx_took, ))Confirm that
idx_file_list,idx_scan_size, andidx_tookare correctly initialized in all execution paths, especially whenuse_parquet_inverted_indexis false and the function returns early. This will prevent potential runtime errors due to uninitialized or default values.src/infra/src/file_list/sqlite.rs (5)
Line range hint
366-396: Validate handling of empty result sets inquery_by_idsIn the
query_by_idsmethod, ensure that the function correctly handles cases where the providedidsdo not exist in the database, returning an empty vector as expected.You can verify this behavior by adding unit tests that pass non-existent IDs and checking that the method returns an empty result without errors.
505-511: Confirm correct default behavior inget_min_pk_valueThe method
get_min_pk_valuereturns the minimum primary key value or defaults to zero if none is found. Ensure that this default behavior aligns with the application's expectations when the table is empty.Consider adding a unit test to validate that the method returns zero when the
file_listtable is empty.
513-521: Ensure proper error handling inclean_by_min_pk_valueIn
clean_by_min_pk_value, lines 513-521, ensure that the method handles scenarios where the deletion might fail due to database constraints or locks.Add error handling to catch exceptions from the delete operation and log appropriate error messages. This will aid in troubleshooting issues in production environments.
968-974: Handle optional IDs appropriately ininner_add_with_idIn
inner_add_with_id, ensure that whenidisNone, the database handles the auto-increment of the primary key as expected.Review the database schema and constraints to confirm that inserting a
NULLvalue foridresults in the primary key being auto-generated.
72-78: Ensure consistency with existing methods inbatch_add_with_idThe newly added
batch_add_with_idmethod at lines 72-78 introduces functionality to batch add files with IDs. Ensure that this method aligns with the behavior ofbatch_addand properly handles cases where IDs might already exist in the database.It's important to confirm that the method handles duplicate IDs gracefully. You can use the following script to check for any inconsistencies:
src/infra/src/file_list/mysql.rs (1)
297-299: Potential performance impact due to removal ofFORCE INDEXThe removal of
FORCE INDEX (file_list_stream_ts_idx)from multiple SQL queries may lead to suboptimal query performance if the database does not automatically choose the optimal index. It's important to ensure that the queries still utilize the appropriate indexes to maintain efficiency.Please verify that the database query planner continues to use the desired indexes. If not, consider adding optimizer hints or analyzing the need for additional indexing.
Also applies to: 314-316, 328-330
YashodhanJoshi1
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm 👍
There was a problem hiding this 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
🧹 Outside diff range and nitpick comments (2)
src/config/src/config.rs (2)
949-954: LGTM! Consider makingmeta_id_batch_sizeconfigurable.The addition of
meta_id_batch_sizeis a good improvement for batch processing of IDs. The default value of 5000 seems reasonable. However, to provide more flexibility, consider making this value configurable through an environment variable or configuration file.You could modify the field definition to allow for configuration:
#[env_config( name = "ZO_META_ID_BATCH_SIZE", default = 5000, help = "Batch size for ID processing" )] pub meta_id_batch_size: usize,
1439-1440: LGTM! Consider using an enum for database types.The addition of MySQL and PostgreSQL as potential external metadata stores is a good improvement, expanding the system's flexibility. This change correctly sets
meta_store_externalto true for these new database types.To make the code more maintainable and less error-prone, consider using an enum for the database types. This could look like:
enum MetaStoreType { Sqlite, Etcd, MySQL, PostgreSQL, } impl MetaStoreType { fn is_external(&self) -> bool { matches!(self, MetaStoreType::MySQL | MetaStoreType::PostgreSQL) } } // Then you could use it like this: let meta_store_type = MetaStoreType::from_str(&cfg.common.meta_store).unwrap(); cfg.common.meta_store_external = meta_store_type.is_external();This approach would make it easier to add new database types in the future and reduce the risk of errors in string comparisons.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (5)
- src/config/src/config.rs (4 hunks)
- src/infra/src/file_list/mod.rs (8 hunks)
- src/infra/src/file_list/mysql.rs (11 hunks)
- src/infra/src/file_list/postgres.rs (5 hunks)
- src/infra/src/file_list/sqlite.rs (9 hunks)
🚧 Files skipped from review as they are similar to previous changes (4)
- src/infra/src/file_list/mod.rs
- src/infra/src/file_list/mysql.rs
- src/infra/src/file_list/postgres.rs
- src/infra/src/file_list/sqlite.rs
🧰 Additional context used
📓 Path-based instructions (1)
src/config/src/config.rs (1)
Pattern
**/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestionsMake sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.
🔇 Additional comments (1)
src/config/src/config.rs (1)
1337-1340: LGTM! Good default value handling.The addition of a check for
meta_id_batch_sizein theinitfunction is a good practice. It ensures that the value is always set to a reasonable default (5000) if not specified or set to 0. This prevents potential issues that could arise from an invalid batch size.
We only retrieve the
idfrom thefile_liston the leader. Then, we use thisidto get thefile namefrom the same list. By adding a local SQLite cache for each node, we can significantly reduce calls to the remote database.Summary by CodeRabbit
Release Notes
New Features
get_local_cache,get_min_pk_value, andclean_by_min_pk_value.Bug Fixes
None.Documentation
Refactor