-
Notifications
You must be signed in to change notification settings - Fork 957
[Store] Extract master snapshot codec from MasterService(3/5) #2831
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
Open
xiangui33423
wants to merge
7
commits into
kvcache-ai:main
Choose a base branch
from
xiangui33423:store/extract-snapshot-codec
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7d25fbc
[Store] Extract master snapshot codec from MasterService
xiangui33423 ddf6604
[Store] Address code review: improve const-correctness and type safet…
xiangui33423 4e916dc
[Store] Address review: preserve snapshot_id in manifest and wire up …
xiangui33423 8e16b54
[Store] Fix clang-format violation in master_service.h
xiangui33423 f65d221
[Store] Address review: fix snapshot decode order for memory replicas
xiangui33423 32deab6
Merge branch 'main' of https://github.com/kvcache-ai/Mooncake into st…
xiangui33423 6132c65
[Store] Refactor snapshot restore into three-phase architecture
xiangui33423 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
153 changes: 153 additions & 0 deletions
153
mooncake-store/include/ha/snapshot/master_snapshot_codec.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
| #include <memory> | ||
| #include <string> | ||
| #include <vector> | ||
| #include <ylt/util/tl/expected.hpp> | ||
|
|
||
| #include "types.h" | ||
|
|
||
| namespace mooncake { | ||
|
|
||
| // Forward declarations | ||
| class MasterService; | ||
| class SegmentManager; | ||
| class NoFSegmentManager; | ||
| class ClientTaskManager; | ||
|
|
||
| namespace ha { | ||
|
|
||
| /** | ||
| * @brief A view of the live master state for snapshot serialization. | ||
| * | ||
| * This struct holds references to the live state components that need to | ||
| * be serialized into a master snapshot. Non-const references are used because | ||
| * the underlying serializers require non-const pointers, avoiding const_cast. | ||
| */ | ||
| struct MasterSnapshotStateView { | ||
| MasterService& master_service; | ||
| SegmentManager& segment_manager; | ||
| NoFSegmentManager& nof_segment_manager; | ||
| ClientTaskManager& task_manager; | ||
|
|
||
| MasterSnapshotStateView(MasterService& ms, SegmentManager& sm, | ||
| NoFSegmentManager& nsm, ClientTaskManager& tm) | ||
| : master_service(ms), | ||
| segment_manager(sm), | ||
| nof_segment_manager(nsm), | ||
| task_manager(tm) {} | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Container for serialized master snapshot payloads. | ||
| * | ||
| * This struct provides a type-safe, zero-overhead container for the three | ||
| * serialized payload buffers, eliminating map lookup overhead and potential | ||
| * runtime exceptions from using std::unordered_map. | ||
| */ | ||
| struct MasterSnapshotPayloads { | ||
| std::vector<uint8_t> metadata; | ||
| std::vector<uint8_t> segments; | ||
| std::vector<uint8_t> task_manager; | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Encodes and decodes master snapshot payloads. | ||
| * | ||
| * This codec handles serialization of the complete master state bundle: | ||
| * - Metadata shards (objects, replicas, tenant state) | ||
| * - Segment manager state | ||
| * - Task manager state | ||
| * - Discarded replicas | ||
| * | ||
| * The current implementation preserves the existing snapshot format exactly | ||
| * to maintain backward compatibility with existing snapshots. | ||
| * | ||
| * Format details: | ||
| * - metadata: msgpack-encoded metadata shards (compressed per-shard with zstd) | ||
| * - segments: msgpack-encoded segment manager state | ||
| * - task_manager: msgpack-encoded task manager state | ||
| * - manifest.txt: format descriptor "<type>|<version>|<snapshot_id>" | ||
| * (e.g., "messagepack|1.0.0|snapshot-000123") | ||
| */ | ||
| class MasterSnapshotCodec { | ||
| public: | ||
| MasterSnapshotCodec() = default; | ||
| ~MasterSnapshotCodec() = default; | ||
|
|
||
| // Non-copyable, non-movable (contains no state, but enforce ownership | ||
| // semantics) | ||
| MasterSnapshotCodec(const MasterSnapshotCodec&) = delete; | ||
| MasterSnapshotCodec& operator=(const MasterSnapshotCodec&) = delete; | ||
| MasterSnapshotCodec(MasterSnapshotCodec&&) = delete; | ||
| MasterSnapshotCodec& operator=(MasterSnapshotCodec&&) = delete; | ||
|
|
||
| /** | ||
| * @brief Encode master state into serialized buffers. | ||
| * | ||
| * @param state_view View of the live master state | ||
| * @return Structured payloads containing serialized data, or error | ||
| * | ||
| * The returned struct contains: | ||
| * - metadata: serialized metadata shards | ||
| * - segments: serialized segment manager state | ||
| * - task_manager: serialized task manager state | ||
| */ | ||
| tl::expected<MasterSnapshotPayloads, SerializationError> Encode( | ||
| MasterSnapshotStateView& state_view) const; | ||
|
|
||
| /** | ||
| * @brief Decode snapshot payloads and restore into master service. | ||
| * | ||
| * @param master_service Target MasterService to restore state into | ||
| * @param payloads Structured payloads containing serialized data | ||
| * @return void on success, SerializationError on failure | ||
| */ | ||
| tl::expected<void, SerializationError> Decode( | ||
| MasterService* master_service, | ||
| const MasterSnapshotPayloads& payloads) const; | ||
|
|
||
| // Canonical serializer identifiers embedded in the snapshot manifest. | ||
| static constexpr const char* kSerializerType = "messagepack"; | ||
| static constexpr const char* kSerializerVersion = "1.0.0"; | ||
|
|
||
| /** | ||
| * @brief Encode a snapshot manifest into its on-disk byte representation. | ||
| * | ||
| * The manifest is a "<type>|<version>|<snapshot_id>" descriptor. Keeping | ||
| * the encoding here (rather than hand-crafting the format string at the | ||
| * call site) ensures the manifest layout stays owned by the codec. | ||
| * | ||
| * @param type Serializer/protocol type (e.g., "messagepack") | ||
| * @param version Snapshot format version (e.g., "1.0.0") | ||
| * @param snapshot_id Identifier of the snapshot being written | ||
| * @return Manifest bytes ready to upload | ||
| */ | ||
| static std::vector<uint8_t> EncodeManifest(const std::string& type, | ||
| const std::string& version, | ||
| const std::string& snapshot_id); | ||
|
|
||
| private: | ||
| // Metadata encoding/decoding (delegates to MetadataSerializer for now) | ||
| tl::expected<std::vector<uint8_t>, SerializationError> EncodeMetadata( | ||
| MasterService& master_service) const; | ||
| tl::expected<void, SerializationError> DecodeMetadata( | ||
| MasterService* master_service, const std::vector<uint8_t>& data) const; | ||
|
|
||
| // Segment encoding/decoding | ||
| tl::expected<std::vector<uint8_t>, SerializationError> EncodeSegments( | ||
| SegmentManager& segment_manager, | ||
| NoFSegmentManager& nof_segment_manager) const; | ||
| tl::expected<void, SerializationError> DecodeSegments( | ||
| MasterService* master_service, const std::vector<uint8_t>& data) const; | ||
|
|
||
| // Task manager encoding/decoding | ||
| tl::expected<std::vector<uint8_t>, SerializationError> EncodeTaskManager( | ||
| ClientTaskManager& task_manager) const; | ||
| tl::expected<void, SerializationError> DecodeTaskManager( | ||
| MasterService* master_service, const std::vector<uint8_t>& data) const; | ||
| }; | ||
|
|
||
| } // namespace ha | ||
| } // namespace mooncake | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| #pragma once | ||
|
|
||
| namespace mooncake::ha { | ||
|
|
||
| // Snapshot file names | ||
| inline constexpr const char* kSnapshotMetadataFile = "metadata"; | ||
| inline constexpr const char* kSnapshotSegmentsFile = "segments"; | ||
| inline constexpr const char* kSnapshotTaskManagerFile = "task_manager"; | ||
| inline constexpr const char* kSnapshotManifestFile = "manifest.txt"; | ||
| inline constexpr const char* kSnapshotLatestFile = "latest.txt"; | ||
|
|
||
| // Snapshot format | ||
| inline constexpr const char* kSnapshotSerializerType = "messagepack"; | ||
| inline constexpr const char* kSnapshotSerializerVersion = "1.0.0"; | ||
|
|
||
| // Backup directories | ||
| inline constexpr const char* kSnapshotBackupSaveDir = | ||
| "mooncake_snapshot_save_backup"; | ||
| inline constexpr const char* kSnapshotBackupRestoreDir = | ||
| "mooncake_snapshot_restore_backup"; | ||
|
|
||
| // List limit | ||
| inline constexpr size_t kUnlimitedSnapshotList = 0; | ||
|
|
||
| } // namespace mooncake::ha |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The
MasterSnapshotStateViewstruct currently holdsconstreferences to the live state components, but the underlying serializers (e.g.,MetadataSerializer,SegmentSerializer,TaskManagerSerializer) require non-const pointers. This forces the codec to useconst_castinternally during encoding, which violates const-correctness and is a code smell.Since the live state components are non-const in the manager anyway, we should change
MasterSnapshotStateViewto hold non-const references. This allows us to completely eliminate allconst_casts in the codec implementation.