feat(Messaging): epoch-scoped request ledger with conflict detection (#6)
- RequestLedger<Payload, Result, PayloadEqual> keys idempotent host requests by (epoch, trusted client, request id). - check() admits the first request, replays a recorded result for an exact repeat (including a rejected outcome), reports in_progress while the request is still running, rejects a changed payload and rejects keys from an obsolete/future epoch before side effects. - record() stores the caller-defined result exactly once; results are never overwritten. - Capacity/admission/archival is explicit: entries are never evicted within an epoch (capacity_exceeded instead), and advancing the epoch clears memory. An old request number therefore cannot execute a second time after cleanup. - 8 tests: replay, rejected outcome, payload conflict, delayed retry after newer operations, epoch rollover, capacity pressure, in-flight state/immutable result and non-advancing epoch rejection.
This commit is contained in:
1 parent
7b0fea6095
commit
afd4d99a2d
2 files changed
+336
No files matched your search
@@ -0,0 +1,183 @@
|
||||
#pragma once
|
||||
|
||||
#include <compare>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
namespace Void::Messaging
|
||||
{
|
||||
|
||||
/// Epoch-scoped idempotency ledger for host request dispatch.
|
||||
///
|
||||
/// A key is `(epoch, trusted client, request id)`. The ledger records the
|
||||
/// caller-defined payload of the first admission and, once the operation
|
||||
/// finishes, its caller-defined result (including a rejected outcome). An exact
|
||||
/// repeat returns the recorded result instead of executing again; reusing the
|
||||
/// key with a different payload, or after its epoch became obsolete, fails
|
||||
/// before any side effect.
|
||||
///
|
||||
/// Capacity, admission and archival:
|
||||
/// - Entries are never evicted within an epoch. When `capacity` is reached new
|
||||
/// keys are rejected with `capacity_exceeded`; the caller can retry later or
|
||||
/// raise the capacity. Completed entries keep occupying a slot so a delayed
|
||||
/// retry can still be answered.
|
||||
/// - Advancing the epoch clears every entry. Requests from an older (or
|
||||
/// future) epoch are then rejected as `stale_epoch`, so an old request number
|
||||
/// can never execute a second time after memory cleanup.
|
||||
template <typename Payload,
|
||||
typename Result,
|
||||
typename PayloadEqual = std::equal_to<Payload>>
|
||||
class RequestLedger
|
||||
{
|
||||
public:
|
||||
|
||||
struct Key {
|
||||
std::uint64_t epoch{0};
|
||||
std::string client;
|
||||
std::uint64_t request_id{0};
|
||||
|
||||
[[nodiscard]] auto
|
||||
operator<=>(const Key&) const noexcept = default;
|
||||
[[nodiscard]] bool
|
||||
operator==(const Key&) const noexcept = default;
|
||||
};
|
||||
|
||||
enum class Status : std::uint8_t {
|
||||
/// First admission: the caller may execute and must record a result.
|
||||
admitted = 0,
|
||||
/// Exact repeat of a completed request; `result` is the recorded one.
|
||||
replay,
|
||||
/// Exact repeat while the operation is still in flight.
|
||||
in_progress,
|
||||
/// Same key, different payload: reject before side effects.
|
||||
payload_conflict,
|
||||
/// The key's epoch is not the current epoch.
|
||||
stale_epoch,
|
||||
/// The ledger is full; nothing was admitted.
|
||||
capacity_exceeded,
|
||||
};
|
||||
|
||||
struct Decision {
|
||||
Status status{Status::capacity_exceeded};
|
||||
std::optional<Result> result{};
|
||||
};
|
||||
|
||||
struct Snapshot {
|
||||
std::size_t size{0};
|
||||
std::size_t capacity{0};
|
||||
std::size_t in_flight{0};
|
||||
std::size_t completed{0};
|
||||
std::uint64_t epoch{0};
|
||||
};
|
||||
|
||||
explicit RequestLedger(std::size_t capacity = 1024,
|
||||
std::uint64_t epoch = 0,
|
||||
PayloadEqual equal = {})
|
||||
: capacity_(capacity)
|
||||
, epoch_(epoch)
|
||||
, equal_(std::move(equal))
|
||||
{
|
||||
}
|
||||
|
||||
RequestLedger(const RequestLedger&) = delete;
|
||||
RequestLedger& operator=(const RequestLedger&) = delete;
|
||||
|
||||
/// Admit or classify `key` with `payload`.
|
||||
[[nodiscard]] Decision check(const Key& key, const Payload& payload)
|
||||
{
|
||||
std::lock_guard lock{mutex_};
|
||||
if (key.epoch != epoch_) {
|
||||
return Decision{.status = Status::stale_epoch};
|
||||
}
|
||||
const auto found = entries_.find(key);
|
||||
if (found != entries_.end()) {
|
||||
if (!equal_(found->second.payload, payload)) {
|
||||
return Decision{.status = Status::payload_conflict};
|
||||
}
|
||||
if (found->second.result.has_value()) {
|
||||
return Decision{.status = Status::replay,
|
||||
.result = found->second.result};
|
||||
}
|
||||
return Decision{.status = Status::in_progress};
|
||||
}
|
||||
if (entries_.size() >= capacity_) {
|
||||
return Decision{.status = Status::capacity_exceeded};
|
||||
}
|
||||
entries_.emplace(key, Entry{.payload = payload, .result = {}});
|
||||
return Decision{.status = Status::admitted};
|
||||
}
|
||||
|
||||
/// Record the outcome for an admitted key. Returns false when the key is
|
||||
/// unknown, belongs to another epoch or already has a result (results
|
||||
/// are never overwritten).
|
||||
[[nodiscard]] bool record(const Key& key, Result result)
|
||||
{
|
||||
std::lock_guard lock{mutex_};
|
||||
if (key.epoch != epoch_) {
|
||||
return false;
|
||||
}
|
||||
const auto found = entries_.find(key);
|
||||
if (found == entries_.end() || found->second.result.has_value()) {
|
||||
return false;
|
||||
}
|
||||
found->second.result = std::move(result);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Advance to `next` and clear all entries. Only a strictly newer epoch
|
||||
/// is accepted; returns false otherwise.
|
||||
[[nodiscard]] bool advance_epoch(std::uint64_t next)
|
||||
{
|
||||
std::lock_guard lock{mutex_};
|
||||
if (next <= epoch_) {
|
||||
return false;
|
||||
}
|
||||
epoch_ = next;
|
||||
entries_.clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::uint64_t epoch() const
|
||||
{
|
||||
std::lock_guard lock{mutex_};
|
||||
return epoch_;
|
||||
}
|
||||
|
||||
[[nodiscard]] Snapshot snapshot() const
|
||||
{
|
||||
std::lock_guard lock{mutex_};
|
||||
Snapshot out{.size = entries_.size(),
|
||||
.capacity = capacity_,
|
||||
.epoch = epoch_};
|
||||
for (const auto& [key, entry] : entries_) {
|
||||
(void)key;
|
||||
if (entry.result.has_value()) {
|
||||
++out.completed;
|
||||
} else {
|
||||
++out.in_flight;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
struct Entry {
|
||||
Payload payload;
|
||||
std::optional<Result> result;
|
||||
};
|
||||
|
||||
mutable std::mutex mutex_;
|
||||
std::map<Key, Entry> entries_;
|
||||
std::size_t capacity_{1024};
|
||||
std::uint64_t epoch_{0};
|
||||
PayloadEqual equal_{};
|
||||
};
|
||||
|
||||
} // namespace Void::Messaging
|
||||
Reference in new issue
Block a user