Skip to content

fix:Type Command Expired Key Consistency#3132

Merged
Mixficsol merged 2 commits intoOpenAtomFoundation:unstablefrom
YuCai18:pika4.0
Jul 22, 2025
Merged

fix:Type Command Expired Key Consistency#3132
Mixficsol merged 2 commits intoOpenAtomFoundation:unstablefrom
YuCai18:pika4.0

Conversation

@YuCai18
Copy link
Copy Markdown
Collaborator

@YuCai18 YuCai18 commented Jul 20, 2025

#3130
在PikiwiDB v4中,当哈希类型的键过期时,在使用类似HLEN等命令时会返回0或空结果,表示键不存在。但当使用TYPE命令时,仍然会返回"hash"而不是"none",这与v355版本的行为不一致。

问题原因:

在Redis::GetType方法中,即使键已过期,也没有检查过期状态,而是直接返回键的类型。元数据中记录了键的类型,即使键过期,这个元数据仍然存在,只是被标记为过期(stale)。

解决方案:

1.修改了Redis::GetType方法,添加了对键是否过期的检查。
2.实现了Storage::Type方法以确保TYPE命令能正确处理过期键。

修复后的效果:

1.现在当哈希键过期时,TYPE命令会返回"none"而不是"hash"
2.这与v355版本的行为一致,避免了在应用程序中使用TYPE命令判断键类型时的混淆
3.解决方案保持了PikiwiDB内部代码的一致性,确保所有命令对过期键的处理逻辑相同

Summary by CodeRabbit

  • Bug Fixes
    • Improved key type retrieval to correctly handle expired or non-existent keys, ensuring expired keys are now reported as non-existent.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jul 20, 2025

Walkthrough

The GetType method in redis_strings.cc was updated to check for key expiration before determining and assigning the key's type. If the key is expired or missing, the type is set to kNones. The method now always returns Status::OK() regardless of key status.

Changes

File(s) Change Summary
src/storage/src/redis_strings.cc Modified GetType to check key expiration, assign kNones for expired/missing keys, always return OK

Estimated code review effort

1 (<30 minutes)

Suggested labels

bug

Suggested reviewers

  • cheniujh
  • chejinge

Poem

A key once found, but now it's stale,
The code now checks, so none prevail.
If time has passed, or key is gone,
kNones is set, and life goes on.
A hop, a check, a tidy fix—
The rabbit cheers, "No more tricks!" 🐇


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d336a27 and 0e25a5b.

📒 Files selected for processing (1)
  • src/storage/src/redis_strings.cc (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/storage/src/redis_strings.cc
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: Analyze (go)
  • GitHub Check: build_on_ubuntu
  • GitHub Check: build_on_macos
  • GitHub Check: build_on_centos
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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.

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.

@github-actions github-actions Bot added the ☢️ Bug Something isn't working label Jul 20, 2025
Copy link
Copy Markdown

@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

🧹 Nitpick comments (1)
src/storage/src/redis_strings.cc (1)

1674-1679: Consider improving error handling and code consistency.

While the core logic is correct, there are a few improvements to consider:

  1. Error handling: The method returns Status::OK() even when database read operations fail with errors other than NotFound. Consider handling database errors explicitly.
  2. Code comments: The Chinese comments should be translated to English for consistency with the rest of the codebase.
  3. Return status consistency: Unlike other Redis operations that return Status::NotFound() for missing keys, this method always returns Status::OK(). While this might be intentional for the TYPE command, ensure this behavior is documented and consistent with the expected API contract.
- // 检查键是否已过期
- if (ExpectedStale(meta_value)) {
-   type = DataType::kNones;  // 如果键已过期,返回"none"类型
- } else {
-   type = static_cast<enum DataType>(static_cast<uint8_t>(meta_value[0]));
- }
+ // Check if the key has expired
+ if (ExpectedStale(meta_value)) {
+   type = DataType::kNones;  // Return "none" type if key is expired
+ } else {
+   type = static_cast<enum DataType>(static_cast<uint8_t>(meta_value[0]));
+ }
- type = DataType::kNones;  // 如果键不存在,返回"none"类型
+ type = DataType::kNones;  // Return "none" type if key does not exist
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8b077fc and 0cdad27.

📒 Files selected for processing (2)
  • src/storage/src/redis_strings.cc (1 hunks)
  • src/storage/src/storage.cc (1 hunks)
🔇 Additional comments (3)
src/storage/src/storage.cc (1)

1517-1526: Clean wrapper implementation that provides string-based type interface.

The new Type method effectively wraps the existing GetType functionality and provides a consistent string-based interface. The implementation correctly:

  • Clears the output vector before use
  • Delegates to the existing GetType method (which should now handle expiration checks per the PR objectives)
  • Converts the DataType enum to its string representation
  • Returns the appropriate status

This aligns well with the PR's goal to fix TYPE command expired key consistency by leveraging the updated GetType method.

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

1669-1684: LGTM! The implementation correctly handles expired keys for the TYPE command.

The logic properly checks for key expiration using ExpectedStale(meta_value) and returns DataType::kNones for both expired and non-existent keys, which aligns with the PR objective to fix the TYPE command consistency issue.


1672-1684: No changes needed: TYPE returns “none” for missing/expired keys

The storage layer already sets DataType::kNones and always returns Status::OK(), and Storage::Type only pushes the stringified type on OK. That means:

  • GetType(key, type) → OK + type == kNones for non-existent/expired keys
  • Type(key, types) → types = { "none" }

This matches Redis’s TYPE semantics.

Comment thread src/storage/src/redis_strings.cc Outdated
rocksdb::Status s = db_->Get(default_read_options_, handles_[kMetaCF], base_meta_key.Encode(), &meta_value);
if (s.ok()) {
type = static_cast<enum DataType>(static_cast<uint8_t>(meta_value[0]));
// 检查键是否已过期
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

注释修改成英文

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

done

Comment thread src/storage/src/storage.cc Outdated
return Status::OK();
}

Status Storage::Type(const std::string& key, std::vector<std::string>& types) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

这里为什么要新增加一个Type,不可以直接用之前的GetType接口吗

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

因为 rocksdb::Status Type() 这个接口在头文件里一直存在,并且已经被大量外部模块(如 pika_migrate、pika_cache、rsync_client 等)调用,它返回的是 Redis 协议要求的字符串类型(如 “string” / “hash”),而旧的 GetType()函数 只返回内部枚举 DataType。

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

防御性编程,先删去了。

@YuCai18
Copy link
Copy Markdown
Collaborator Author

YuCai18 commented Jul 21, 2025

done @Mixficsol

@Mixficsol Mixficsol merged commit 9054553 into OpenAtomFoundation:unstable Jul 22, 2025
14 checks passed
byseea11 pushed a commit to byseea11/pikiwidb that referenced this pull request Sep 27, 2025
* Fix Type Command Expired Key Consistency

* Change to English comments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

☢️ Bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants