-
Notifications
You must be signed in to change notification settings - Fork 961
[TENT] Add receiver-credit ledger model and protocol invariants #2860
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
catyans
wants to merge
7
commits into
kvcache-ai:main
Choose a base branch
from
catyans:feat/receiver-credit-model
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.
+520
−0
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
74ba94a
tent: add receiver credit ledger model
28f00f5
tent: initialize credit resource indices
818bac5
tent: fence replayed credit activations
d20fcb2
tent: add epoch-safe credit session cleanup
d93ce5b
Merge upstream main into receiver credit ledger model
5382c31
[TENT] Document partial receiver credit grants
61441d4
Merge remote-tracking branch 'upstream/main' into feat/receiver-credi…
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
98 changes: 98 additions & 0 deletions
98
mooncake-transfer-engine/tent/include/tent/runtime/receiver_credit.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,98 @@ | ||
| // Copyright 2026 KVCache.AI | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #ifndef TENT_RUNTIME_RECEIVER_CREDIT_H | ||
| #define TENT_RUNTIME_RECEIVER_CREDIT_H | ||
|
|
||
| #include <array> | ||
| #include <cstdint> | ||
| #include <mutex> | ||
| #include <unordered_map> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #include "tent/common/status.h" | ||
|
|
||
| namespace mooncake::tent { | ||
|
|
||
| struct ReceiverSessionId { | ||
| uint64_t high{0}, low{0}; | ||
| bool operator==(const ReceiverSessionId& o) const { | ||
| return high == o.high && low == o.low; | ||
| } | ||
| }; | ||
| struct CreditKey { | ||
| ReceiverSessionId receiver_session; | ||
| uint64_t sender_peer{0}; | ||
| uint32_t qos_class{0}; | ||
| bool operator==(const CreditKey& o) const { | ||
| return receiver_session == o.receiver_session && | ||
| sender_peer == o.sender_peer && qos_class == o.qos_class; | ||
| } | ||
| }; | ||
| struct CreditKeyHash { | ||
| size_t operator()(const CreditKey&) const noexcept; | ||
| }; | ||
| enum class CreditResource : uint16_t { | ||
| DataBytes = 1, | ||
| RequestSlots, | ||
| StagingSlots, | ||
| ConsumerSlots | ||
| }; | ||
| constexpr size_t kCreditResourceCount = 4; | ||
| struct CreditAmount { | ||
| CreditResource resource; | ||
| uint64_t grant_total{0}; | ||
| }; | ||
| struct CreditCharge { | ||
| std::vector<std::pair<CreditResource, uint64_t>> resources; | ||
| }; | ||
| struct ReceiverCreditUpdateV1 { | ||
| uint16_t schema_version{1}, flags{0}; | ||
| uint32_t qos_class{0}; | ||
| ReceiverSessionId receiver_session_id; | ||
| uint64_t epoch{0}, sequence{0}; | ||
| uint32_t freshness_ttl_ms{0}; | ||
| std::vector<CreditAmount> grants; | ||
| }; | ||
| enum class CreditUpdateDisposition : uint8_t { | ||
| Applied, | ||
| DuplicateOrOld, | ||
| SequenceGap | ||
| }; | ||
|
|
||
| // Private, sender-side state model. It has no network or Admission integration. | ||
| class SenderCreditLedger { | ||
| public: | ||
| explicit SenderCreditLedger(size_t max_entries = 1024) | ||
| : max_entries_(max_entries) {} | ||
| Status activate(const CreditKey&, uint64_t epoch); | ||
| // Removes an exactly matched epoch after the caller has fenced and drained | ||
| // (or failed) its transport-owned work. An old cleanup cannot erase a | ||
| // reactivated, newer epoch. | ||
| Status deactivate(const CreditKey&, uint64_t epoch); | ||
| Status applyUpdate(const CreditKey&, const ReceiverCreditUpdateV1&, | ||
| CreditUpdateDisposition&); | ||
| Status tryReserve(const CreditKey&, const CreditCharge&); | ||
| // Only for work not yet handed to a transport; completions need a new | ||
| // grant. | ||
| Status rollbackReservation(const CreditKey&, const CreditCharge&); | ||
| Status available(const CreditKey&, CreditResource, uint64_t&) const; | ||
| Status consumed(const CreditKey&, CreditResource, uint64_t&) const; | ||
|
|
||
| private: | ||
| struct Entry { | ||
| uint64_t epoch{0}, last_sequence{0}; | ||
| bool has_update{false}; | ||
| std::array<uint64_t, kCreditResourceCount> grants{}, consumed{}; | ||
| }; | ||
| static Status resourceIndex(CreditResource, size_t&); | ||
| static Status normalize(const CreditCharge&, | ||
| std::array<uint64_t, kCreditResourceCount>&); | ||
| mutable std::mutex mutex_; | ||
| const size_t max_entries_; | ||
| std::unordered_map<CreditKey, Entry, CreditKeyHash> entries_; | ||
| }; | ||
|
|
||
| } // namespace mooncake::tent | ||
| #endif | ||
179 changes: 179 additions & 0 deletions
179
mooncake-transfer-engine/tent/src/runtime/receiver_credit.cpp
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,179 @@ | ||
| // Copyright 2026 KVCache.AI | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #include "tent/runtime/receiver_credit.h" | ||
|
|
||
| namespace mooncake::tent { | ||
|
|
||
| size_t CreditKeyHash::operator()(const CreditKey& k) const noexcept { | ||
| size_t h = std::hash<uint64_t>{}(k.receiver_session.high); | ||
| auto mix = [&h](uint64_t v) { | ||
| h ^= std::hash<uint64_t>{}(v) + 0x9e3779b97f4a7c15ULL + (h << 6) + | ||
| (h >> 2); | ||
| }; | ||
| mix(k.receiver_session.low); | ||
| mix(k.sender_peer); | ||
| mix(k.qos_class); | ||
| return h; | ||
| } | ||
|
|
||
| Status SenderCreditLedger::resourceIndex(CreditResource r, size_t& i) { | ||
| auto raw = static_cast<uint16_t>(r); | ||
| if (raw < 1 || raw > kCreditResourceCount) | ||
| return Status::InvalidArgument("unknown credit resource" LOC_MARK); | ||
| i = raw - 1; | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| Status SenderCreditLedger::normalize( | ||
| const CreditCharge& c, std::array<uint64_t, kCreditResourceCount>& out) { | ||
| out.fill(0); | ||
| if (c.resources.empty()) | ||
| return Status::InvalidArgument("empty credit charge" LOC_MARK); | ||
| for (auto [r, amount] : c.resources) { | ||
| size_t i = 0; | ||
| CHECK_STATUS(resourceIndex(r, i)); | ||
| if (!amount || out[i]) | ||
| return Status::InvalidArgument( | ||
| "zero or duplicate credit charge" LOC_MARK); | ||
| out[i] = amount; | ||
| } | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| Status SenderCreditLedger::activate(const CreditKey& k, uint64_t epoch) { | ||
| if (!epoch) return Status::InvalidArgument("zero credit epoch" LOC_MARK); | ||
| std::lock_guard lock(mutex_); | ||
| auto existing = entries_.find(k); | ||
| if (existing != entries_.end()) { | ||
| if (epoch < existing->second.epoch) | ||
| return Status::InvalidEntry("stale credit activation" LOC_MARK); | ||
| if (epoch == existing->second.epoch) return Status::OK(); | ||
| Entry replacement; | ||
| replacement.epoch = epoch; | ||
| existing->second = replacement; | ||
| return Status::OK(); | ||
| } else if (entries_.size() >= max_entries_) { | ||
| return Status::TooManyRequests("credit ledger entry limit" LOC_MARK); | ||
| } | ||
| Entry e; | ||
| e.epoch = epoch; | ||
| entries_.emplace(k, e); | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| Status SenderCreditLedger::deactivate(const CreditKey& k, uint64_t epoch) { | ||
| if (!epoch) return Status::InvalidArgument("zero credit epoch" LOC_MARK); | ||
| std::lock_guard lock(mutex_); | ||
| auto existing = entries_.find(k); | ||
| if (existing == entries_.end()) return Status::OK(); // idempotent cleanup | ||
| if (existing->second.epoch != epoch) | ||
| return Status::InvalidEntry("credit cleanup epoch mismatch" LOC_MARK); | ||
| entries_.erase(existing); | ||
| return Status::OK(); | ||
|
catyans marked this conversation as resolved.
|
||
| } | ||
|
|
||
| Status SenderCreditLedger::applyUpdate(const CreditKey& k, | ||
| const ReceiverCreditUpdateV1& u, | ||
| CreditUpdateDisposition& disposition) { | ||
| if (u.schema_version != 1 || !u.epoch || !u.sequence) | ||
| return Status::InvalidArgument("invalid credit update header" LOC_MARK); | ||
| if (!(u.receiver_session_id == k.receiver_session) || | ||
| u.qos_class != k.qos_class || u.grants.size() > kCreditResourceCount) | ||
| return Status::InvalidArgument("credit update identity/size" LOC_MARK); | ||
| std::array<uint64_t, kCreditResourceCount> proposed{}; | ||
| std::array<bool, kCreditResourceCount> present{}; | ||
| for (auto a : u.grants) { | ||
| size_t i = 0; | ||
| CHECK_STATUS(resourceIndex(a.resource, i)); | ||
| if (present[i]) | ||
| return Status::InvalidArgument("duplicate grant resource" LOC_MARK); | ||
| present[i] = true; | ||
| proposed[i] = a.grant_total; | ||
| } | ||
| std::lock_guard lock(mutex_); | ||
|
catyans marked this conversation as resolved.
|
||
| auto it = entries_.find(k); | ||
| if (it == entries_.end() || it->second.epoch != u.epoch) | ||
| return Status::InvalidEntry("inactive or stale credit epoch" LOC_MARK); | ||
| auto& e = it->second; | ||
| if (e.has_update && u.sequence <= e.last_sequence) { | ||
| disposition = CreditUpdateDisposition::DuplicateOrOld; | ||
| return Status::OK(); | ||
| } | ||
| // `grants` is a partial cumulative update: each resource present in this | ||
| // message replaces that resource's cumulative grant total, while omitted | ||
| // resources retain their previous totals. This lets the receiver refresh | ||
| // only the resources whose available capacity changed. | ||
| for (size_t i = 0; i < kCreditResourceCount; ++i) | ||
| if (present[i] && | ||
| (proposed[i] < e.grants[i] || proposed[i] < e.consumed[i])) | ||
| return Status::InvalidArgument( | ||
| "decreasing or under-consumed grant" LOC_MARK); | ||
| bool gap = e.has_update && u.sequence > e.last_sequence + 1; | ||
| for (size_t i = 0; i < kCreditResourceCount; ++i) | ||
| if (present[i]) e.grants[i] = proposed[i]; | ||
| e.last_sequence = u.sequence; | ||
| e.has_update = true; | ||
| disposition = gap ? CreditUpdateDisposition::SequenceGap | ||
| : CreditUpdateDisposition::Applied; | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| Status SenderCreditLedger::tryReserve(const CreditKey& k, | ||
| const CreditCharge& c) { | ||
| std::array<uint64_t, kCreditResourceCount> n; | ||
| CHECK_STATUS(normalize(c, n)); | ||
| std::lock_guard lock(mutex_); | ||
| auto it = entries_.find(k); | ||
| if (it == entries_.end() || !it->second.has_update) | ||
| return Status::InvalidEntry("credit unavailable" LOC_MARK); | ||
| auto& e = it->second; | ||
| for (size_t i = 0; i < kCreditResourceCount; ++i) | ||
| if (e.consumed[i] > e.grants[i] || n[i] > e.grants[i] - e.consumed[i]) | ||
| return Status::TooManyRequests("insufficient credit" LOC_MARK); | ||
| for (size_t i = 0; i < kCreditResourceCount; ++i) e.consumed[i] += n[i]; | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| Status SenderCreditLedger::rollbackReservation(const CreditKey& k, | ||
| const CreditCharge& c) { | ||
| std::array<uint64_t, kCreditResourceCount> n; | ||
| CHECK_STATUS(normalize(c, n)); | ||
| std::lock_guard lock(mutex_); | ||
| auto it = entries_.find(k); | ||
| if (it == entries_.end()) | ||
| return Status::InvalidEntry("credit session inactive" LOC_MARK); | ||
| for (size_t i = 0; i < kCreditResourceCount; ++i) | ||
| if (n[i] > it->second.consumed[i]) | ||
| return Status::InvalidArgument( | ||
| "credit rollback underflow" LOC_MARK); | ||
| for (size_t i = 0; i < kCreditResourceCount; ++i) | ||
| it->second.consumed[i] -= n[i]; | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| Status SenderCreditLedger::available(const CreditKey& k, CreditResource r, | ||
| uint64_t& v) const { | ||
| size_t i = 0; | ||
| CHECK_STATUS(resourceIndex(r, i)); | ||
| std::lock_guard lock(mutex_); | ||
| auto it = entries_.find(k); | ||
| if (it == entries_.end() || !it->second.has_update) | ||
| return Status::InvalidEntry("credit unavailable" LOC_MARK); | ||
| v = it->second.grants[i] - it->second.consumed[i]; | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| Status SenderCreditLedger::consumed(const CreditKey& k, CreditResource r, | ||
| uint64_t& v) const { | ||
| size_t i = 0; | ||
| CHECK_STATUS(resourceIndex(r, i)); | ||
| std::lock_guard lock(mutex_); | ||
| auto it = entries_.find(k); | ||
| if (it == entries_.end()) | ||
| return Status::InvalidEntry("credit session inactive" LOC_MARK); | ||
| v = it->second.consumed[i]; | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| } // namespace mooncake::tent | ||
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.
Uh oh!
There was an error while loading. Please reload this page.