seqeron 0.18.0
Loading...
Searching...
No Matches
OutstandingWork.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5#include <unordered_map>
6#include <unordered_set>
7#include <vector>
8
10
11// Leader-only request/reply work tracked across a failover. Outstanding is replicated (sequenced request to
12// sequenced reply); dispatched is local. Emission is at-least-once, so key on the request's clusterSeqNo. The
13// Java twin is app/OutstandingWork.java, which carries the rationale; keep the two in step.
14template<typename Work, typename Key = std::int64_t>
16{
17 public:
25 void onRequest(const Key& key, Work work)
26 {
27 if (m_outstanding.insert_or_assign(key, std::move(work)).second)
28 {
29 m_order.push_back(key);
30 }
31 }
32
38 void onReply(const Key& key)
39 {
40 m_outstanding.erase(key);
41 m_dispatched.erase(key);
42 }
43
44 // The leader gate closed: forget what this node dispatched, so the next opening re-dispatches it.
46 {
47 m_dispatched.clear();
48 }
49
55 void onReplyNotEmitted(const Key& key)
56 {
57 m_dispatched.erase(key);
58 }
59
67 template<typename DispatchFn>
68 int dispatchUndispatched(DispatchFn&& dispatch)
69 {
70 int dispatchedCount = 0;
71 std::size_t live = 0;
72 bool stopped = false;
73 for (std::size_t i = 0; i < m_order.size(); ++i)
74 {
75 const Key& key = m_order[i];
76 const auto entry = m_outstanding.find(key);
77 if (entry != m_outstanding.end())
78 {
79 m_order[live++] = key;
80 if (!stopped && !m_dispatched.contains(key))
81 {
82 if (dispatch(key, entry->second))
83 {
84 m_dispatched.insert(key);
85 ++dispatchedCount;
86 }
87 else
88 {
89 stopped = true;
90 }
91 }
92 }
93 }
94 m_order.resize(live);
95 return dispatchedCount;
96 }
97
98 [[nodiscard]] std::size_t size() const noexcept
99 {
100 return m_outstanding.size();
101 }
102
103 [[nodiscard]] bool isDispatched(const Key& key) const
104 {
105 return m_dispatched.contains(key);
106 }
107
108 private:
109 std::unordered_map<Key, Work> m_outstanding;
110 std::unordered_set<Key> m_dispatched;
111 std::vector<Key> m_order; // request order; compacted by each sweep
112};
113
114} // namespace org::limitless::seqeron::app
Definition OutstandingWork.hpp:16
void onNotLeader()
Definition OutstandingWork.hpp:45
void onReply(const Key &key)
Records a request's sequenced reply, on every replica: the request is answered.
Definition OutstandingWork.hpp:38
void onReplyNotEmitted(const Key &key)
Records that a reply offer failed, so no sequenced reply will come.
Definition OutstandingWork.hpp:55
std::size_t size() const noexcept
Definition OutstandingWork.hpp:98
bool isDispatched(const Key &key) const
Definition OutstandingWork.hpp:103
void onRequest(const Key &key, Work work)
Records a request seen on the sequenced stream, on every replica.
Definition OutstandingWork.hpp:25
int dispatchUndispatched(DispatchFn &&dispatch)
Offers each undispatched outstanding request, in request order.
Definition OutstandingWork.hpp:68
Definition ClusterError.hpp:5