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

fix:ttl will deafult 0 when keys have ttl #2730

Merged
merged 3 commits into from
Jun 17, 2024

Conversation

chejinge
Copy link
Collaborator

@chejinge chejinge commented Jun 14, 2024

写mgetwithttl这个函数的目的是:
当mget多个key,其中有一些key不是string类型的时候,返回nil而不是返回 错误,这里跟redis保持一致了

  • New Features
    • Introduced a new method to retrieve values along with their Time To Live (TTL) information for multiple keys in the Storage class. This feature allows users to fetch data along with its expiration details in one call.

Summary by CodeRabbit

  • New Features

    • Introduced a method to retrieve multiple key-value pairs with their time-to-live (TTL) information.
  • Enhancements

    • Improved the GetWithTTL method to better handle value retrieval and TTL calculation.

Copy link

coderabbitai bot commented Jun 14, 2024

Walkthrough

A new feature to retrieve multiple key-value pairs with associated TTL (time-to-live) information has been added to both Storage and Redis classes. This update involves introducing the MGetWithTTL function for fetching values along with their expiration times. Additionally, other helper methods for clearing values, calculating TTL, and handling parsed string values have been introduced to support this functionality.

Changes

File Summary
src/storage/include/storage/storage.h Added Status MGetWithTTL(const Slice& key, std::string* value, int64_t* ttl); in Storage class.
src/storage/src/redis.h Added MGetWithTTL method to the Redis class for retrieving multiple values with TTL information.
src/storage/src/storage.cc Implemented the MGetWithTTL method in the Storage class for multi-key retrieval with TTL.
src/storage/src/redis_strings.cc Added the MGetWithTTL method and other helper functions for TTL calculations and value handling in Redis class.

Sequence Diagrams

sequenceDiagram
    participant Client
    participant Redis
    participant Storage

    Client->>Redis: MGetWithTTL(key, value*, ttl*)
    Redis->>Storage: MGetWithTTL(key, value*, ttl*)
    Storage-->>Redis: value, ttl
    Redis-->>Client: value, ttl
Loading

Poem

In the fields where data grows,
A new function gracefully flows.
Retrieving keys with time in tow,
To see when each is set to go.
With MGetWithTTL our code now knows,
The life of keys, like wind that blows.
🐇📦⏰🌿


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 ☢️ Bug Something isn't working label Jun 14, 2024
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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 84cdb18 and e2328bb.

Files selected for processing (1)
  • src/pika_kv.cc (1 hunks)
Additional comments not posted (2)
src/pika_kv.cc (2)

536-536: Review the implementation of MGetWithTTL to ensure it handles all edge cases.

#!/bin/bash
# Description: Verify the implementation of `MGetWithTTL` method in the storage interface.

# Test: Search for the implementation of `MGetWithTTL`. Expect: Proper handling of edge cases.
ast-grep --lang cpp --pattern $'class Storage {
  $$$
  MGetWithTTL($_, $_) {
    $$$
  }
  $$$  
}'

536-536: Ensure all calls to MGetWithTTL correctly handle the additional TTL information in their implementations.

#!/bin/bash
# Description: Verify that all parts of the codebase that call `MGetWithTTL` handle the TTL information correctly.

# Test: Search for usage of `MGetWithTTL`. Expect: Proper handling of TTL.
rg --type cpp $'MGetWithTTL'

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: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e2328bb and c48a916.

Files selected for processing (4)
  • src/storage/include/storage/storage.h (1 hunks)
  • src/storage/src/redis.h (1 hunks)
  • src/storage/src/redis_strings.cc (1 hunks)
  • src/storage/src/storage.cc (2 hunks)
Additional comments not posted (4)
src/storage/src/redis.h (1)

159-159: The method MGetWithTTL correctly adheres to the method signature mentioned in the AI-generated summary. However, it's essential to ensure that this method is correctly implemented in the corresponding .cc file and that it integrates effectively with other parts of the system.

Verification successful

The method MGetWithTTL is correctly implemented in src/storage/src/redis_strings.cc and is actively used across different parts of the system. This confirms that it is well-integrated.

  • src/storage/src/redis_strings.cc: Implementation of MGetWithTTL.
  • src/pika_kv.cc: Usage of MGetWithTTL.
  • src/storage/src/storage.cc: Multiple references to MGetWithTTL.
  • src/storage/include/storage/storage.h: Declaration of MGetWithTTL.
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Ensure the correct implementation and integration of `MGetWithTTL`.

# Test: Search for method implementation in `redis_strings.cc`.
rg --type cpp "Status Redis::MGetWithTTL" src/storage/src/redis_strings.cc

# Test: Search for usage of this method in the codebase to ensure it's being used correctly.
rg --type cpp "MGetWithTTL" --glob '!src/storage/src/redis.h'

Length of output: 1046

src/storage/include/storage/storage.h (1)

1117-1117: The method MGetWithTTL is correctly declared and aligns with the PR's objectives. Ensure the implementation correctly handles edge cases such as non-existing keys and keys without a set TTL.

Verification successful

The MGetWithTTL method in redis_strings.cc correctly handles edge cases such as non-existing keys, stale keys, and keys without TTL. Here are the relevant details:

  • Clears the value and sets ttl to -2 for non-existing or stale keys.
  • Sets ttl to -1 for keys without TTL.
  • Handles current time calculation for TTL.
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify edge case handling in the implementation of `MGetWithTTL`.

# Test: Search for the function implementation and look for edge case handling. Expect: Proper checks and handling for non-existing keys and keys without TTL.
rg --type cpp $'Status Storage::MGetWithTTL'

Length of output: 280



Script:

#!/bin/bash
# Extract the implementation of the `MGetWithTTL` functions for analysis.
rg --type cpp --context 20 'Status Storage::MGetWithTTL' src/storage/src/storage.cc

Length of output: 2507



Script:

#!/bin/bash
# Extract the implementation of `inst->MGetWithTTL` for analysis.
rg --type cpp --context 20 'Status DBInstance::MGetWithTTL' src/storage/src/storage.cc

Length of output: 88



Script:

#!/bin/bash
# Use ast-grep to locate the `MGetWithTTL` method within the `DBInstance` class or any related class.
ast-grep --lang cpp --pattern $'class DBInstance {
  $$$
  Status MGetWithTTL($_, $_, $_) {
    $$$
  }
  $$$
}'

Length of output: 458



Script:

#!/bin/bash
# Use rg to search for all occurrences of `MGetWithTTL` in the codebase.
rg --type cpp 'MGetWithTTL'

Length of output: 953



Script:

#!/bin/bash
# Extract the implementation of the `MGetWithTTL` method from `redis_strings.cc` for analysis.
rg --type cpp --context 20 'Status Redis::MGetWithTTL' src/storage/src/redis_strings.cc

Length of output: 1229

src/storage/src/storage.cc (2)

158-160: The implementation of MGetWithTTL method looks correct and aligns with the intended functionality to fetch multiple keys with their TTL.


216-216: Verify the implementation of MGetWithTTL in the Redis class to ensure it handles the logic as expected.

Verification successful

The MGetWithTTL method is implemented in src/storage/src/redis_strings.cc and is referenced in several files, including src/storage/src/storage.cc and src/storage/src/redis.h. This confirms that the method is properly defined and utilized within the codebase.

  • src/storage/src/redis_strings.cc: Contains the implementation of MGetWithTTL.
  • src/storage/src/storage.cc: Contains calls to MGetWithTTL.
  • src/storage/src/redis.h: Contains the method declaration.
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the `MGetWithTTL` method implementation in the Redis class.

# Test: Search for the method implementation in the Redis class.
ast-grep --lang cpp --pattern $'class Redis { $$$ MGetWithTTL($_, $_, $_) { $$$ } $$$ }'

Length of output: 434



Script:

#!/bin/bash
# Description: Broaden the search for the `MGetWithTTL` method implementation in the Redis class.

# Test: Search for any method named `MGetWithTTL` in C++ files.
rg --glob '*.cpp' --glob '*.h' 'MGetWithTTL'

Length of output: 383



Script:

#!/bin/bash
# Description: Locate and review the implementations of the `MGetWithTTL` method.

# Test: Search for the implementation of the `MGetWithTTL` method in C++ files.
rg --glob '*.cpp' 'MGetWithTTL'

Length of output: 180



Script:

#!/bin/bash
# Description: Debug the search for the implementation of the `MGetWithTTL` method.

# Test: Re-run the `rg` command with debugging enabled to understand why files are being skipped.
rg --glob '*.cpp' 'MGetWithTTL' --debug

Length of output: 388654



Script:

#!/bin/bash
# Description: Locate and review the implementations of the `MGetWithTTL` method without using any glob patterns.

# Test: Search for the implementation of the `MGetWithTTL` method in all files.
rg 'MGetWithTTL'

Length of output: 942

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: 2

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c48a916 and 7795056.

Files selected for processing (1)
  • src/storage/src/redis_strings.cc (1 hunks)
Additional comments not posted (3)
src/storage/src/redis_strings.cc (3)

371-381: Review error handling and data consistency in HandleParsedStringsValue.

The function HandleParsedStringsValue clears the value and sets TTL directly. Verify that this handling is consistent with the business logic, especially for stale or expired keys.

Verification successful

The handling of clearing values and setting TTL directly in HandleParsedStringsValue is consistent with the business logic in the contexts where it is used. Both occurrences of the function call ensure that stale keys are appropriately handled by clearing the value and setting the TTL.

  • Located in src/storage/src/redis_strings.cc:
    • Within an unnamed function.
    • Within the MGetWithTTL method of the Redis class.
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that clearing values and setting TTL directly in HandleParsedStringsValue is consistent across the application.
# Test: Search for usage of HandleParsedStringsValue in other parts of the application.
rg --type cpp 'HandleParsedStringsValue'

Length of output: 391



Script:

#!/bin/bash
# Extract and analyze the context around the calls to `HandleParsedStringsValue` in `src/storage/src/redis_strings.cc`
rg -A 10 -B 10 'HandleParsedStringsValue' src/storage/src/redis_strings.cc

Length of output: 2040


388-405: Check error handling consistency in GetWithTTL.

The function GetWithTTL uses a pattern of error handling that should be consistent across similar functions. Verify this consistency to ensure uniform behavior across the application.

Verification successful

The search for Status::InvalidArgument across the codebase has yielded numerous instances, including in files such as redis_strings.cc, redis_streams.cc, and others. This indicates that the pattern of using Status::InvalidArgument for error handling is indeed consistent across various parts of the codebase.

  • Instances of Status::InvalidArgument with similar error messages were found in multiple files, confirming consistent error handling practices.

Locations in the codebase:

  • src/storage/src/redis_strings.cc
  • src/storage/src/redis_streams.cc
  • src/storage/src/redis_lists.cc
  • src/storage/src/redis_zsets.cc
  • src/storage/src/redis_hashes.cc
  • src/storage/src/redis_sets.cc
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Check for consistent error handling in similar functions.
# Test: Search for similar patterns in other TTL related functions.
rg --type cpp 'Status::InvalidArgument("WRONGTYPE'

Length of output: 161



Script:

#!/bin/bash
# Description: Check for consistent error handling in similar functions.
# Test: Search for similar patterns in other TTL related functions.
rg --type cpp 'Status::InvalidArgument\("WRONGTYPE"'

Length of output: 54



Script:

#!/bin/bash
# Description: Check for consistent error handling in similar functions.
# Test: Search for patterns involving `Status::InvalidArgument`.
rg --type cpp 'Status::InvalidArgument'

Length of output: 41268


407-421: Validate error handling and TTL setting in MGetWithTTL.

MGetWithTTL should handle errors consistently and set TTL appropriately. This function is critical as it directly affects the application's data fetching logic.

Verification successful

The implementations of MGetWithTTL in storage.cc and redis_strings.cc have been successfully extracted. Here's a summary of the findings:

  • In storage.cc, MGetWithTTL delegates the call to GetDBInstance(key) and returns the result of inst->MGetWithTTL(key, value, ttl).
  • In redis_strings.cc, MGetWithTTL handles the database retrieval, checks the meta value, and processes the parsed string value. It sets the TTL to -2 if the key is not found.

The error handling and TTL setting in the MGetWithTTL method in redis_strings.cc appear consistent and appropriate. The method in storage.cc delegates the actual work to another instance, suggesting the logic is likely consistent across implementations.

  • redis_strings.cc correctly sets ttl to -2 when the key is not found.
  • storage.cc delegates the call and does not directly handle TTL setting or error handling, leaving it to the actual implementation in the delegated instance.

This confirms that MGetWithTTL handles errors consistently and sets TTL appropriately.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Ensure that MGetWithTTL handles errors consistently and sets TTL correctly.
# Test: Compare with similar functions for consistency.
rg --type cpp 'MGetWithTTL'

Length of output: 953



Script:

#!/bin/bash
# Extract the implementation of MGetWithTTL from storage.cc
ast-grep --lang cpp --pattern $'Status Storage::MGetWithTTL(const Slice& key, std::string* value, int64_t* ttl) {
  $$$
}' src/storage/src/storage.cc

# Extract the implementation of MGetWithTTL from redis_strings.cc
ast-grep --lang cpp --pattern $'Status Redis::MGetWithTTL(const Slice& key, std::string* value, int64_t* ttl) {
  $$$
}' src/storage/src/redis_strings.cc

# Extract the declaration of MGetWithTTL from storage.h
rg 'MGetWithTTL' src/storage/include/storage/storage.h

Length of output: 525



Script:

#!/bin/bash
# Extract the full implementation of MGetWithTTL from storage.cc
ast-grep --lang cpp --pattern 'Status Storage::MGetWithTTL(const Slice& key, std::string* value, int64_t* ttl) {
  $$$
}' src/storage/src/storage.cc --json

# Extract the full implementation of MGetWithTTL from redis_strings.cc
ast-grep --lang cpp --pattern 'Status Redis::MGetWithTTL(const Slice& key, std::string* value, int64_t* ttl) {
  $$$
}' src/storage/src/redis_strings.cc --json

Length of output: 325



Script:

#!/bin/bash
# Search for the MGetWithTTL function definitions in storage.cc and redis_strings.cc and include surrounding lines for context
rg 'Status Storage::MGetWithTTL' -A 20 src/storage/src/storage.cc
rg 'Status Redis::MGetWithTTL' -A 20 src/storage/src/redis_strings.cc

Length of output: 2118

@chejinge chejinge merged commit d562abd into OpenAtomFoundation:unstable Jun 17, 2024
13 checks passed
cheniujh pushed a commit to cheniujh/pika that referenced this pull request Sep 24, 2024
* fix:ttl will deafult 0 when keys have ttl

---------

Co-authored-by: chejinge <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.5.5 4.0.0 ☢️ Bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants