Epoch-scoped request ledger with conflicting-retry detection (#6) #22
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
|
||||||
@@ -0,0 +1,153 @@
|
|||||||
|
#include <Void/Messaging/RequestLedger.hpp>
|
||||||
|
#include <Void/Testing/Assert.hpp>
|
||||||
|
#include <Void/Testing/Test.hpp>
|
||||||
|
|
||||||
|
#include <expected>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
|
||||||
|
using Ledger =
|
||||||
|
Void::Messaging::RequestLedger<std::string, std::expected<int, std::string>>;
|
||||||
|
using Key = Ledger::Key;
|
||||||
|
using Status = Ledger::Status;
|
||||||
|
|
||||||
|
[[nodiscard]] Key key(std::uint64_t epoch, std::uint64_t request_id,
|
||||||
|
std::string client = "client")
|
||||||
|
{
|
||||||
|
return Key{.epoch = epoch, .client = std::move(client),
|
||||||
|
.request_id = request_id};
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
Void::Testing::Test request_ledger_replays_exact_repeat()
|
||||||
|
{
|
||||||
|
Ledger ledger{8, 1};
|
||||||
|
const auto k = key(1, 42);
|
||||||
|
auto first = ledger.check(k, "op");
|
||||||
|
Void::Testing::expect(first.status == Status::admitted);
|
||||||
|
Void::Testing::require(ledger.record(k, 7));
|
||||||
|
|
||||||
|
auto repeat = ledger.check(k, "op");
|
||||||
|
Void::Testing::expect(repeat.status == Status::replay);
|
||||||
|
Void::Testing::require(repeat.result.has_value());
|
||||||
|
Void::Testing::expect_eq(*repeat.result, 7);
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Testing::Test request_ledger_replays_rejected_outcome()
|
||||||
|
{
|
||||||
|
Ledger ledger{8, 1};
|
||||||
|
const auto k = key(1, 1);
|
||||||
|
Void::Testing::expect(ledger.check(k, "op").status == Status::admitted);
|
||||||
|
Void::Testing::require(ledger.record(
|
||||||
|
k, std::unexpected<std::string>{"insufficient funds"}));
|
||||||
|
|
||||||
|
auto repeat = ledger.check(k, "op");
|
||||||
|
Void::Testing::expect(repeat.status == Status::replay);
|
||||||
|
Void::Testing::require(repeat.result.has_value());
|
||||||
|
Void::Testing::expect(!repeat.result->has_value());
|
||||||
|
Void::Testing::expect_eq(repeat.result->error(),
|
||||||
|
std::string{"insufficient funds"});
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Testing::Test request_ledger_detects_payload_conflict()
|
||||||
|
{
|
||||||
|
Ledger ledger{8, 1};
|
||||||
|
const auto k = key(1, 5);
|
||||||
|
Void::Testing::expect(ledger.check(k, "payload-a").status ==
|
||||||
|
Status::admitted);
|
||||||
|
Void::Testing::require(ledger.record(k, 10));
|
||||||
|
|
||||||
|
auto conflict = ledger.check(k, "payload-b");
|
||||||
|
Void::Testing::expect(conflict.status == Status::payload_conflict);
|
||||||
|
Void::Testing::expect(!conflict.result.has_value());
|
||||||
|
|
||||||
|
// The conflicting retry changed nothing.
|
||||||
|
auto repeat = ledger.check(k, "payload-a");
|
||||||
|
Void::Testing::expect(repeat.status == Status::replay);
|
||||||
|
Void::Testing::expect_eq(*repeat.result, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Testing::Test request_ledger_delayed_retry_after_newer_operations()
|
||||||
|
{
|
||||||
|
Ledger ledger{8, 1};
|
||||||
|
const auto first = key(1, 1);
|
||||||
|
const auto second = key(1, 2);
|
||||||
|
Void::Testing::expect(ledger.check(first, "one").status == Status::admitted);
|
||||||
|
Void::Testing::require(ledger.record(first, 100));
|
||||||
|
Void::Testing::expect(ledger.check(second, "two").status == Status::admitted);
|
||||||
|
Void::Testing::require(ledger.record(second, 200));
|
||||||
|
|
||||||
|
auto delayed = ledger.check(first, "one");
|
||||||
|
Void::Testing::expect(delayed.status == Status::replay);
|
||||||
|
Void::Testing::expect_eq(*delayed.result, 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Testing::Test request_ledger_epoch_rollover()
|
||||||
|
{
|
||||||
|
Ledger ledger{8, 1};
|
||||||
|
const auto old = key(1, 7);
|
||||||
|
Void::Testing::expect(ledger.check(old, "op").status == Status::admitted);
|
||||||
|
Void::Testing::require(ledger.record(old, 1));
|
||||||
|
|
||||||
|
Void::Testing::require(ledger.advance_epoch(2));
|
||||||
|
Void::Testing::expect_eq(ledger.epoch(), std::uint64_t{2});
|
||||||
|
Void::Testing::expect_eq(ledger.snapshot().size, std::size_t{0});
|
||||||
|
|
||||||
|
// The old request number can never execute again after cleanup.
|
||||||
|
Void::Testing::expect(ledger.check(old, "op").status == Status::stale_epoch);
|
||||||
|
|
||||||
|
const auto fresh = key(2, 7);
|
||||||
|
Void::Testing::expect(ledger.check(fresh, "op").status == Status::admitted);
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Testing::Test request_ledger_capacity_pressure()
|
||||||
|
{
|
||||||
|
Ledger ledger{2, 1};
|
||||||
|
Void::Testing::expect(ledger.check(key(1, 1), "a").status ==
|
||||||
|
Status::admitted);
|
||||||
|
Void::Testing::expect(ledger.check(key(1, 2), "b").status ==
|
||||||
|
Status::admitted);
|
||||||
|
Void::Testing::expect(ledger.check(key(1, 3), "c").status ==
|
||||||
|
Status::capacity_exceeded);
|
||||||
|
|
||||||
|
// Completed entries stay retained: capacity is only freed by rollover.
|
||||||
|
Void::Testing::require(ledger.record(key(1, 1), 1));
|
||||||
|
Void::Testing::require(ledger.record(key(1, 2), 2));
|
||||||
|
Void::Testing::expect(ledger.check(key(1, 3), "c").status ==
|
||||||
|
Status::capacity_exceeded);
|
||||||
|
|
||||||
|
Void::Testing::require(ledger.advance_epoch(2));
|
||||||
|
Void::Testing::expect(ledger.check(key(2, 3), "c").status ==
|
||||||
|
Status::admitted);
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Testing::Test request_ledger_in_progress_and_result_is_immutable()
|
||||||
|
{
|
||||||
|
Ledger ledger{8, 1};
|
||||||
|
const auto k = key(1, 9);
|
||||||
|
Void::Testing::expect(ledger.check(k, "op").status == Status::admitted);
|
||||||
|
Void::Testing::expect(ledger.check(k, "op").status == Status::in_progress);
|
||||||
|
|
||||||
|
Void::Testing::require(ledger.record(k, 5));
|
||||||
|
// A second record never overwrites the first answer.
|
||||||
|
Void::Testing::expect(!ledger.record(k, 6));
|
||||||
|
auto repeat = ledger.check(k, "op");
|
||||||
|
Void::Testing::expect(repeat.status == Status::replay);
|
||||||
|
Void::Testing::expect_eq(*repeat.result, 5);
|
||||||
|
|
||||||
|
const auto snap = ledger.snapshot();
|
||||||
|
Void::Testing::expect_eq(snap.size, std::size_t{1});
|
||||||
|
Void::Testing::expect_eq(snap.completed, std::size_t{1});
|
||||||
|
Void::Testing::expect_eq(snap.in_flight, std::size_t{0});
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Testing::Test request_ledger_rejects_non_advancing_epoch()
|
||||||
|
{
|
||||||
|
Ledger ledger{8, 3};
|
||||||
|
Void::Testing::expect(!ledger.advance_epoch(3));
|
||||||
|
Void::Testing::expect(!ledger.advance_epoch(2));
|
||||||
|
Void::Testing::expect_eq(ledger.epoch(), std::uint64_t{3});
|
||||||
|
}
|
||||||
Reference in new issue
Block a user