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

Switch to Single Redis Connection with Prefixed Keys #82

Merged
merged 2 commits into from
Jul 19, 2024

Conversation

sebasti810
Copy link
Contributor

@sebasti810 sebasti810 commented Jul 19, 2024

Changes

  • Refactored RedisConnections to use a single Redis connection instead of multiple connections for different logical databases.
  • Implemented key prefixes to separate different types of data within the single Redis database.
  • Updated all database operations to use the new prefixed key system.
  • Modified tests to reflect the new single-connection, prefixed-key structure.

Motivation

This change simplifies our Redis usage and aligns with Redis Cloud's limitations, which don't support multiple databases. Using a single connection with prefixed keys provides better performance, reduces complexity in connection management, and maintains logical separation of different data types.

Key Prefixes

  • main: for hashchain entries
  • derived: for derived dictionary entries
  • app_state: for application state (epoch, epoch_operation)
  • merkle_proofs: for Merkle proofs
  • commitments: for epoch commitments
  • input_order remains unprefixed as it's a global list

closes #73

Copy link

coderabbitai bot commented Jul 19, 2024

Walkthrough

This update refines the Redis connection management within the Rust application. The change involves upgrading the thiserror crate and simplifying the connection model from handling multiple Redis connections to using a single connection instance. This not only improves code maintainability and readability but also enhances the overall efficiency of database interactions.

Changes

Files Change Summary
Cargo.toml Updated thiserror crate version from 1.0.50 to 1.0.62.
src/main.rs, src/storage.rs Replaced RedisConnections with RedisConnection, transitioning from a multi-connection to a single connection model, and updated related method signatures and implementations accordingly.

Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1a089b0 and 99618c3.

Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
Files selected for processing (3)
  • Cargo.toml (1 hunks)
  • src/main.rs (2 hunks)
  • src/storage.rs (23 hunks)
Additional comments not posted (33)
Cargo.toml (1)

54-54: Dependency Update: Ensure Compatibility

The thiserror crate has been updated from version 1.0.50 to 1.0.62. Ensure that this version is compatible with your codebase and does not introduce any breaking changes.

src/main.rs (2)

18-18: Update Import: RedisConnection

The import has been updated to reflect the new RedisConnection struct. Ensure that all usages of RedisConnections have been updated accordingly.


51-51: Refactor: Use Single Redis Connection

The instantiation has been changed from RedisConnections::new(&redis_config) to RedisConnection::new(&redis_config). This change simplifies the connection handling logic. Ensure all dependent code is updated to work with a single connection.

src/storage.rs (30)

62-68: Documentation: Key Prefixes

The comments provide a clear explanation of the new key prefixes. This is helpful for maintainability and understanding the structure of the Redis keys.


69-70: Refactor: RedisConnection Struct

The RedisConnection struct now encapsulates a single Mutex<Connection>. This change simplifies the connection management.


148-149: Helper Method: Lock Connection

The lock_connection method provides a convenient way to lock the connection. Ensure that this method is used consistently to avoid data races.


155-161: Method Refactor: get_keys

The get_keys method now uses the single connection and applies the main: prefix. This change is consistent with the new key structure.


165-172: Method Refactor: get_derived_keys

The get_derived_keys method now uses the single connection and applies the derived: prefix. This change is consistent with the new key structure.


Line range hint 176-181:
Method Refactor: get_hashchain

The get_hashchain method now uses the single connection and applies the main: prefix. This change is consistent with the new key structure.


187-188: Method Refactor: get_derived_value

The get_derived_value method now uses the single connection and applies the derived: prefix. This change is consistent with the new key structure.


197-198: Method Refactor: get_derived_keys_in_order

The get_derived_keys_in_order method now uses the single connection and retrieves the keys from the input_order list. This change is consistent with the new key structure.


203-205: Method Refactor: get_commitment

The get_commitment method now uses the single connection and applies the commitments: prefix. This change is consistent with the new key structure.


216-217: Method Refactor: get_proof

The get_proof method now uses the single connection and applies the merkle_proofs: prefix. This change is consistent with the new key structure.


Line range hint 226-244:
Method Refactor: get_proofs_in_epoch

The get_proofs_in_epoch method now uses the single connection and applies the merkle_proofs: prefix. The sorting logic ensures that proofs are returned in the correct order.


252-253: Method Refactor: get_epoch

The get_epoch method now uses the single connection and applies the app_state: prefix. This change is consistent with the new key structure.


259-260: Method Refactor: get_epoch_operation

The get_epoch_operation method now uses the single connection and applies the app_state: prefix. This change is consistent with the new key structure.


266-270: Method Refactor: set_epoch

The set_epoch method now uses the single connection and applies the app_state: prefix. This change is consistent with the new key structure.


274-275: Method Refactor: reset_epoch_operation_counter

The reset_epoch_operation_counter method now uses the single connection and applies the app_state: prefix. This change is consistent with the new key structure.


286-292: Method Refactor: update_hashchain

The update_hashchain method now uses the single connection and applies the main: prefix. This change is consistent with the new key structure.


Line range hint 307-318:
Method Refactor: set_derived_entry

The set_derived_entry method now uses the single connection and applies the derived: prefix. The logic for updating the input_order list ensures consistency with the new key structure.


330-344: Method Refactor: get_epochs

The get_epochs method now uses the single connection and applies the commitments: prefix. The parsing logic ensures that the epochs are correctly extracted from the keys.


350-351: Method Refactor: increment_epoch_operation

The increment_epoch_operation method now uses the single connection and applies the app_state: prefix. This change is consistent with the new key structure.


364-368: Method Refactor: add_merkle_proof

The add_merkle_proof method now uses the single connection and applies the merkle_proofs: prefix. This change is consistent with the new key structure.


379-389: Method Refactor: add_commitment

The add_commitment method now uses the single connection and applies the commitments: prefix. This change is consistent with the new key structure.


393-403: Method Refactor: initialize_derived_dict

The initialize_derived_dict method now uses the single connection and applies the derived: prefix. The logic for updating the input_order list ensures consistency with the new key structure.


414-417: Method Refactor: flush_database

The flush_database method now uses the single connection and flushes all entries. This change is consistent with the new connection structure.


431-434: Test Setup: Initialize RedisConnection

The setup function initializes the RedisConnection and flushes the database before each test. This ensures a clean state for testing.


438-438: Test Teardown: Flush Database

The teardown function flushes the database after each test. This ensures that tests do not interfere with each other.


470-472: Test: Update Hashchain

The test for get_keys has been updated to use the main: prefix. This change is consistent with the new key structure.


586-588: Test: Set Derived Entry

The test for get_derived_keys_in_order has been updated to use the derived: prefix. This change is consistent with the new key structure.


621-621: Test: Update Hashchain

The test for get_hashchain has been updated to use the main: prefix. This change is consistent with the new key structure.


641-641: Test: Update Hashchain

The test for get_hashchain has been updated to use the main: prefix. This change is consistent with the new key structure.


Line range hint 657-690:
Test: Handle Wrong Formatted Hashchain Value

The test for handling a wrong formatted hashchain value ensures that the get_hashchain method correctly returns an error. This is a good practice for robustness.


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:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • 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 Configuration 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.

Copy link

@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

Outside diff range, codebase verification and nitpick comments (1)
src/storage.rs (1)

Line range hint 116-141:
Refactor: RedisConnection Initialization

The new method has been updated to establish a single connection. The logic includes handling the case where the Redis server is not running, which is a good practice. However, the sleep duration is a hack and should be addressed in the future.

-            // TODO: fix this hack
-            sleep(Duration::from_secs(5));
+            // TODO: Replace sleep with a proper check to ensure the Redis server is ready.
+            for _ in 0..10 {
+                if try_client.get_connection().is_ok() {
+                    break;
+                }
+                sleep(Duration::from_secs(1));
+            }

@sebasti810 sebasti810 merged commit 150077c into main Jul 19, 2024
1 check passed
@sebasti810 sebasti810 deleted the single-redis-con branch July 19, 2024 07:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fix: use a single redis database
2 participants