Fray Engine
Loading...
Searching...
No Matches
neighbor_monitor.hpp
Go to the documentation of this file.
1#ifndef FRAY_NEIGHBOR_MONITOR_HPP
2#define FRAY_NEIGHBOR_MONITOR_HPP
3
4#include <chrono>
5#include <functional>
6#include <map>
7#include <optional>
8#include <utility>
9#include <vector>
10
11#include "outgoing_router.hpp"
12
13namespace fray::routing
14{
16 enum class MonitorCommand { probe, ping };
17
25 template <class Key>
27 {
28 public:
30 using Clock = std::chrono::steady_clock;
32 using Now = std::function<Clock::time_point()>;
34 using Send = std::function<void(Key, MonitorCommand)>;
36 using State = std::function<NeighborState(Key)>;
38 using Changed = std::function<void(Key, NeighborState)>;
40 struct QueueHealth { Key destination; uint64_t depth; uint64_t oldestAgeMs; };
43 {
47 uint64_t progressAgeMs;
49 uint64_t heartbeat;
51 std::vector<QueueHealth> queues;
52 };
53
62 Send send,
64 Changed changed,
65 Now now = [] { return Clock::now(); })
66 : _send(std::move(send)), _state(std::move(state)), _changed(std::move(changed)), _now(std::move(now)) {}
67
72 void start(
73 Key neighbor)
74 {
75 const auto now = _now();
76 _neighbors[neighbor] = {now, now, std::nullopt, std::nullopt, now, now, now};
78 }
79
86 void observe(
87 Key neighbor,
88 bool ready)
89 {
90 auto found = _neighbors.find(neighbor);
91 if (found == _neighbors.end()) return;
92 auto &entry = found->second;
93 entry.lastHeard = _now();
94 if (ready && !entry.healthExpectedSince) entry.healthExpectedSince = entry.lastHeard;
95 const auto state = _state(neighbor);
98 setState(neighbor, isDegraded(entry, entry.lastHeard) ? NeighborState::degraded : NeighborState::ready);
99 }
100
107 void report(
108 Key neighbor,
109 HealthReport health)
110 {
111 const auto found = _neighbors.find(neighbor);
112 if (found == _neighbors.end()) return;
113 auto &entry = found->second;
114 const auto now = _now();
115 if (!entry.health || entry.health->heartbeat != health.heartbeat) entry.heartbeatChanged = now;
116 const bool queued = hasQueue(health);
117 const bool wasQueued = entry.health && hasQueue(*entry.health);
118 if (!queued || !wasQueued || entry.health->processedCount != health.processedCount)
119 entry.queueProgress = now;
120 entry.health = std::move(health);
121 entry.healthReceived = now;
122 }
123
130 void tick()
131 {
132 using namespace std::chrono_literals;
133 const auto now = _now();
134 for (auto &[neighbor, entry] : _neighbors)
135 {
136 const auto state = _state(neighbor);
138 {
139 if (now - entry.since >= 10s) setState(neighbor, NeighborState::unreachable);
140 else _send(neighbor, MonitorCommand::probe);
141 }
145 {
146 const auto silence = now - entry.lastHeard;
147 if (silence >= 10s) setState(neighbor, NeighborState::down);
148 else if (silence >= 3s) setState(neighbor, NeighborState::suspect);
149 else if (isDegraded(entry, now)) setState(neighbor, NeighborState::degraded);
151 _send(neighbor, MonitorCommand::ping);
152 }
153 }
154 }
155
156 private:
158 struct Neighbor
159 {
161 Clock::time_point since;
163 Clock::time_point lastHeard;
165 std::optional<Clock::time_point> healthExpectedSince;
167 std::optional<HealthReport> health;
169 Clock::time_point healthReceived;
171 Clock::time_point heartbeatChanged;
173 Clock::time_point queueProgress;
174 };
175
176 static bool hasQueue(
177 const HealthReport &health)
178 {
179 for (const auto &queue : health.queues) if (queue.depth != 0) return true;
180 return false;
181 }
182
188 static bool isDegraded(
189 const Neighbor &entry,
190 Clock::time_point now)
191 {
192 using namespace std::chrono_literals;
193 if (!entry.health)
194 return entry.healthExpectedSince && now - *entry.healthExpectedSince >= 3s;
195 return now - entry.healthReceived >= 3s || now - entry.heartbeatChanged >= 3s ||
196 (hasQueue(*entry.health) && now - entry.queueProgress >= 3s);
197 }
198
204 Key neighbor,
206 {
207 if (_state(neighbor) != state) _changed(neighbor, state);
208 }
209
218 std::map<Key, Neighbor> _neighbors;
219 };
220}
221#endif
static bool hasQueue(const HealthReport &health)
True if any queue in the report has a nonzero depth.
std::map< Key, Neighbor > _neighbors
static bool isDegraded(const Neighbor &entry, Clock::time_point now)
Decides whether a neighbor's health is bad at the given time.
NeighborMonitor(Send send, State state, Changed changed, Now now=[] { return Clock::now();})
Creates a monitor with no neighbors.
void start(Key neighbor)
Begins monitoring a neighbor (restarting if already tracked) and requests the probing state.
void tick()
Periodic check (the owner calls it about once a second): applies the timing rules and sends probes/pi...
std::chrono::steady_clock Clock
std::function< Clock::time_point()> Now
std::function< void(Key, MonitorCommand)> Send
void report(Key neighbor, HealthReport health)
Stores a health report received from a neighbor and updates heartbeat and queue-progress timestamps....
void setState(Key neighbor, NeighborState state)
Calls the Changed callback only if the router's current state differs from the requested one.
std::function< NeighborState(Key)> State
std::function< void(Key, NeighborState)> Changed
void observe(Key neighbor, bool ready)
Records that a message was heard from the neighbor and may move it to ready or degraded....
NeighborState
Readiness of a neighbor process as seen by the router. unknown: nothing known yet; probing: readiness...
MonitorCommand
What the monitor asks its Send callback to transmit: probe = ask for readiness, ping = request a heal...
std::optional< Clock::time_point > healthExpectedSince