DistributedApplications: replicated worker queue with inbox/outbox dedup, at-least-once retry, DLQ and consumer recovery (P-DISTRIBUTED-05)
This commit is contained in:
1 parent
23a899a892
commit
b386140899
6 files changed
+871
-18
No files matched your search
@@ -0,0 +1,119 @@
|
||||
#pragma once
|
||||
|
||||
#include "distributed_host/replicated_kv.hpp"
|
||||
|
||||
#include <Void/Functional/Result.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace DistributedHost
|
||||
{
|
||||
enum class JobState : std::uint8_t {
|
||||
Pending,
|
||||
InFlight,
|
||||
Done,
|
||||
Dead
|
||||
};
|
||||
|
||||
[[nodiscard]] std::string_view job_state_name(JobState state) noexcept;
|
||||
|
||||
struct QueueJob {
|
||||
std::string id;
|
||||
std::string payload;
|
||||
JobState state{JobState::Pending};
|
||||
std::uint32_t attempts{0};
|
||||
std::string consumer;
|
||||
/// Set when the acknowledged effect still needs external
|
||||
/// delivery (the outbox).
|
||||
bool outbox_pending{false};
|
||||
std::string reason;
|
||||
};
|
||||
|
||||
struct QueueConfig {
|
||||
std::uint32_t max_attempts{3};
|
||||
std::size_t max_jobs{1024};
|
||||
std::size_t max_payload_bytes{64ULL * 1024ULL};
|
||||
};
|
||||
|
||||
/// Consumer-side effect delivery for dispatched outbox entries.
|
||||
using OutboxSink = std::function<void(const QueueJob&)>;
|
||||
|
||||
/// Worker-queue reference backed by one replicated KV group.
|
||||
///
|
||||
/// Semantics are at-least-once by contract:
|
||||
/// * `publish` is idempotent by caller key and replies only after the
|
||||
/// record is majority-committed; a lost reply is retried with the
|
||||
/// same key;
|
||||
/// * `claim` leases one job to a consumer (attempts incremented);
|
||||
/// * `ack` records the inbox outcome and the outbox entry in one
|
||||
/// replicated job record, so a redelivered ack cannot duplicate the
|
||||
/// effect;
|
||||
/// * `nack` requeues or dead-letters after `max_attempts`;
|
||||
/// * `recover_consumer` requeues or dead-letters a crashed
|
||||
/// consumer's in-flight jobs;
|
||||
/// * `dispatch_outbox` delivers effects externally. A crash between
|
||||
/// delivery and marking duplicates the effect: consumers must dedup
|
||||
/// on the job id.
|
||||
class ReplicatedQueue
|
||||
{
|
||||
public:
|
||||
|
||||
ReplicatedQueue(ReplicatedKvCluster& cluster, std::string leader,
|
||||
QueueConfig config = {});
|
||||
|
||||
[[nodiscard]] Void::Result::Result<std::string>
|
||||
publish(std::string payload, std::string idempotency_key);
|
||||
|
||||
[[nodiscard]] Void::Result::Result<std::optional<QueueJob>>
|
||||
claim(std::string_view consumer);
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void>
|
||||
ack(std::string_view consumer, std::string_view job_id);
|
||||
|
||||
[[nodiscard]] Void::Result::Result<QueueJob>
|
||||
nack(std::string_view consumer, std::string_view job_id,
|
||||
std::string reason);
|
||||
|
||||
/// Requeue (or dead-letter) every in-flight job of a crashed
|
||||
/// consumer. Returns the affected count.
|
||||
[[nodiscard]] Void::Result::Result<std::size_t>
|
||||
recover_consumer(std::string_view consumer);
|
||||
|
||||
/// Deliver pending outbox effects and mark them delivered.
|
||||
/// Returns the delivered count.
|
||||
[[nodiscard]] Void::Result::Result<std::size_t>
|
||||
dispatch_outbox(const OutboxSink& sink);
|
||||
|
||||
[[nodiscard]] Void::Result::Result<std::vector<QueueJob>>
|
||||
dead_letters();
|
||||
|
||||
[[nodiscard]] Void::Result::Result<QueueJob>
|
||||
status(std::string_view job_id);
|
||||
|
||||
/// The leader endpoint used for every replicated operation.
|
||||
void set_leader(std::string leader)
|
||||
{
|
||||
m_leader = std::move(leader);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
[[nodiscard]] static std::string key_for(std::string_view id);
|
||||
[[nodiscard]] Void::Result::Result<void>
|
||||
store(const QueueJob& job);
|
||||
[[nodiscard]] Void::Result::Result<std::optional<QueueJob>>
|
||||
load(std::string_view job_id);
|
||||
[[nodiscard]] Void::Result::Result<
|
||||
std::vector<std::pair<std::string, QueueJob>>>
|
||||
all_jobs();
|
||||
|
||||
ReplicatedKvCluster& m_cluster;
|
||||
std::string m_leader;
|
||||
QueueConfig m_config;
|
||||
};
|
||||
} // namespace DistributedHost
|
||||
Reference in new issue
Block a user