Skip to content

Comments

[Store]: Adapted for snapshot functionality and added testing.#1337

Merged
stmatengss merged 5 commits intokvcache-ai:dev/master-kv-hafrom
openanolis:yizheng/dev/master-kv-ha
Jan 7, 2026
Merged

[Store]: Adapted for snapshot functionality and added testing.#1337
stmatengss merged 5 commits intokvcache-ai:dev/master-kv-hafrom
openanolis:yizheng/dev/master-kv-ha

Conversation

@yangdao479
Copy link
Collaborator

Description

This PR enhances the snapshot/restore functionality for MasterService and adds tests.

  • Abstract snapshot storage backend and implement local file backend(for future support of other storage backends)
  • Fix bugs in master service snapshot and restore
  • Add missing fields for snapshot/restore functionality
  • Add snapshot/restore tests for MasterService

Type of Change

  • Types
    • Bug fix
    • New feature
      • Transfer Engine
      • Mooncake Store
      • Mooncake EP
      • Integration
      • P2P Store
      • Python Wheel
    • Breaking change
    • CI/CD
    • Documentation update
    • Other

How Has This Been Tested?

Checklist

  • I have performed a self-review of my own code.
  • I have updated the documentation.
  • I have added tests to prove my changes are effective.

- Support DISK and LOCAL_DISK replica types in snapshot/restore
- Add allocator_names to preserve allocator ordering
- Add client_local_disk_segment_ snapshot/restore support
- Add client_by_name_ reconstruction on restore
- Add test base class for snapshot/restore verification
- Port tests from master_service_test.cpp for snapshot/restore verification
- Port tests from master_service_ssd_test.cpp for SSD snapshot/restore verification
- Sort unordered_map keys before serialization for deterministic snapshots
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @yangdao479, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly upgrades the MasterService's snapshot and restore capabilities by introducing an extensible storage backend abstraction. It resolves existing issues, adds crucial configuration options, and establishes a robust testing methodology to guarantee the reliability of state persistence and recovery for the Mooncake Store.

Highlights

  • Abstracted Snapshot Storage: The MasterService's snapshot and restore functionality has been refactored to use an abstract SerializerBackend interface, allowing for future support of various storage solutions. A LocalFileBackend implementation is provided by default, with an S3Backend available when the AWS SDK is enabled.
  • Snapshot/Restore Bug Fixes and Enhancements: Several bugs in the MasterService's snapshot and restore process have been addressed. New configuration fields, such as snapshot_backend_type and enable_snapshot_restore_clean_metadata, have been added to provide more control over the snapshot mechanism.
  • Improved Serialization Logic: The serialization logic for ObjectMetadata, SegmentManager, and Replica objects has been enhanced. This includes ensuring deterministic serialization order for map-like structures and correctly handling different replica types (memory, disk, local disk) during serialization and deserialization.
  • Comprehensive Snapshot Testing Framework: A new base class MasterServiceSnapshotTestBase has been introduced, along with extensive test cases (master_service_test_for_snapshot.cpp and master_service_ssd_test_for_snapshot.cpp). This framework automatically performs snapshot, backup, restore, and state comparison for every test, ensuring the integrity and consistency of the MasterService's state across restarts.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request significantly enhances the snapshot and restore functionality by abstracting the storage backend, which is a great step towards supporting different storage systems like S3. The introduction of a local file backend and the associated tests are well-implemented. I particularly appreciate the critical fixes for deterministic serialization by sorting keys in unordered maps before processing; this is crucial for snapshot consistency and debuggability. The overall changes are of high quality and the testing strategy is comprehensive. I have a couple of suggestions to further improve robustness and performance.

Comment on lines +19 to +32
inline SnapshotBackendType ParseSnapshotBackendType(const std::string& type_str) {
#ifdef HAVE_AWS_SDK
if (type_str == "s3" || type_str == "S3") {
return SnapshotBackendType::S3;
}
#else
if (type_str == "s3" || type_str == "S3") {
throw std::invalid_argument(
"S3 backend requested but AWS SDK is not available. "
"Please rebuild with HAVE_AWS_SDK or use 'local' backend.");
}
#endif
return SnapshotBackendType::LOCAL_FILE; // Default to local file
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The current implementation of ParseSnapshotBackendType is a bit too permissive. Any value other than "s3" or "S3" will default to LOCAL_FILE, which could hide configuration typos (e.g., "s4"). It's better to be stricter and explicitly handle known values, throwing an exception for any unknown type to prevent silent failures. The suggestion below allows an empty string to default to LOCAL_FILE for robustness, but will fail loudly for other invalid inputs.

inline SnapshotBackendType ParseSnapshotBackendType(const std::string& type_str) {
#ifdef HAVE_AWS_SDK
    if (type_str == "s3" || type_str == "S3") {
        return SnapshotBackendType::S3;
    }
#else
    if (type_str == "s3" || type_str == "S3") {
        throw std::invalid_argument(
            "S3 backend requested but AWS SDK is not available. "
            "Please rebuild with HAVE_AWS_SDK or use 'local' backend.");
    }
#endif
    if (!type_str.empty() && type_str != "local" && type_str != "LOCAL") {
        throw std::invalid_argument("Invalid snapshot backend type: '" + type_str + "'. Supported types are 'local' and 's3'.");
    }
    return SnapshotBackendType::LOCAL_FILE;  // Default to local file for "local" or empty string
}

Comment on lines +714 to +725
for (const auto& name : saved_allocator_names) {
for (auto& pair : segment_manager_->mounted_segments_) {
MountedSegment& mounted_segment = pair.second;
if (mounted_segment.status == SegmentStatus::OK &&
if (mounted_segment.segment.name == name &&
mounted_segment.status == SegmentStatus::OK &&
mounted_segment.buf_allocator) {
// 添加到allocator_manager_
segment_manager_->allocator_manager_.addAllocator(
mounted_segment.segment.name, mounted_segment.buf_allocator);
name, mounted_segment.buf_allocator);
break;
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This nested loop for restoring allocators has a time complexity of O(N*M), where N is the number of saved allocator names and M is the number of mounted segments. This could be inefficient if there are many segments. You can optimize this by first creating a map from segment name to MountedSegment for an efficient O(1) lookup inside the loop, which would bring the total complexity down to O(N+M).

    // Build a map from segment name to MountedSegment for efficient lookup.
    std::unordered_map<std::string, MountedSegment*> name_to_segment;
    for (auto& pair : segment_manager_->mounted_segments_) {
        name_to_segment[pair.second.segment.name] = &pair.second;
    }

    // 按保存的原始顺序添加 allocator
    for (const auto& name : saved_allocator_names) {
        auto it = name_to_segment.find(name);
        if (it != name_to_segment.end()) {
            MountedSegment* mounted_segment = it->second;
            if (mounted_segment->status == SegmentStatus::OK && mounted_segment->buf_allocator) {
                segment_manager_->allocator_manager_.addAllocator(
                    name, mounted_segment->buf_allocator);
            }
        }
    }


tl::expected<void, SerializationError> Serializer<Replica>::serialize(
const Replica &replica, const SegmentView &segment_view, MsgpackPacker &packer) {
// 统一使用数组结构打包Replica
Copy link
Collaborator

Choose a reason for hiding this comment

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

We'd better use English

@stmatengss stmatengss merged commit 846afa3 into kvcache-ai:dev/master-kv-ha Jan 7, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants