Branch data Line data Source code
1 : : // Copyright 2026 HPActor Contributors
2 : : //
3 : : // Licensed under the Apache License, Version 2.0 (the "License");
4 : : // you may not use this file except in compliance with the License.
5 : : // You may obtain a copy of the License at
6 : : //
7 : : // http://www.apache.org/licenses/LICENSE-2.0
8 : : //
9 : : // Unless required by applicable law or agreed to in writing, software
10 : : // distributed under the License is distributed on an "AS IS" BASIS,
11 : : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : : // See the License for the specific language governing permissions and
13 : : // limitations under the License.
14 : :
15 : : #pragma once
16 : : #include <chrono>
17 : : #include <hpactor/actor/event_based_actor.hpp>
18 : : #include <hpactor/actor_context.hpp>
19 : : #include <hpactor/mem/std_allocator.hpp>
20 : : #include <hpactor/metrics/metrics_ring_buffer.hpp>
21 : : #include <hpactor/ref/actor_address.hpp>
22 : : #include <hpactor/ref/actor_ref.hpp>
23 : : #include <hpactor/types/types.hpp>
24 : : #include <unordered_map>
25 : : #include <vector>
26 : :
27 : : namespace hpactor {
28 : :
29 : : // SupervisionDirective - directive from supervisor to child on failure
30 : : enum class SupervisionDirective { Restart, Stop, Escalate };
31 : :
32 : : // ChildFailure - information about a child actor failure
33 : : struct ChildFailure {
34 : : ActorId child_id;
35 : : error reason;
36 : : SupervisionDirective directive;
37 : : };
38 : :
39 : : // SupervisionPolicy - configuration for supervision behavior
40 : : struct SupervisionPolicy {
41 : : enum class Strategy { OneForOne, AllForOne, OneForAll };
42 : : Strategy strategy = Strategy::OneForOne;
43 : : uint32_t max_restarts = 10;
44 : 3 : std::chrono::milliseconds restart_interval{5000};
45 : : };
46 : :
47 : : // Supervisor - interface for supervision strategy
48 : : class Supervisor {
49 : : public:
50 : 6 : virtual ~Supervisor() = default;
51 : : virtual SupervisionDirective on_child_failure(const ChildFailure& failure) = 0;
52 : : virtual void on_child_stopped(ActorId child_id);
53 : : };
54 : :
55 : : // SupervisorActor - EventBasedActor that implements supervision
56 : : class SupervisorActor : public EventBasedActor {
57 : : public:
58 : : SupervisorActor(ActorContext* ctx, ActorSystem& sys, Supervisor& strategy,
59 : : std::vector<Actor> children);
60 : :
61 : : protected:
62 : : Behavior make_behavior() override;
63 : :
64 : : // Override to add actual spawn logic for restarted children.
65 : : // The base implementation manages restart counts and the sliding window.
66 : : virtual void restart_child(ActorId child_id, const error& reason);
67 : : void restart_all_children();
68 : :
69 : : Supervisor& strategy_;
70 : :
71 : : public:
72 : : using ActorVec = std::vector<Actor, mem::MemStdAllocator<Actor>>;
73 : : using RestartCountMap =
74 : : std::unordered_map<ActorId, uint32_t, std::hash<ActorId>, std::equal_to<>,
75 : : mem::MemStdAllocator<std::pair<const ActorId, uint32_t>>>;
76 : :
77 : : protected:
78 : : ActorVec children_;
79 : : RestartCountMap restart_counts_{
80 : : mem::MemStdAllocator<std::pair<const ActorId, uint32_t>>(
81 : : id_ptr(), mem::RegionType::kActor)};
82 : : std::chrono::steady_clock::time_point first_failure_time_;
83 : :
84 : : private:
85 : : void handle_child_down(TypeTag tag, const StreamBuffer& payload);
86 : : };
87 : :
88 : : // SelfSupervisingActor - EventBasedActor that manages its own children
89 : : class SelfSupervisingActor : public EventBasedActor {
90 : : public:
91 : : // Container type aliases with MemStdAllocator
92 : : using ActorVec = std::vector<Actor, mem::MemStdAllocator<Actor>>;
93 : : using ActorRefVec = std::vector<ActorRef, mem::MemStdAllocator<ActorRef>>;
94 : : using ActorAddrVec =
95 : : std::vector<ActorAddress, mem::MemStdAllocator<ActorAddress>>;
96 : : using RestartCountMap =
97 : : std::unordered_map<ActorId, uint32_t, std::hash<ActorId>, std::equal_to<>,
98 : : mem::MemStdAllocator<std::pair<const ActorId, uint32_t>>>;
99 : :
100 : : SelfSupervisingActor(ActorContext* ctx, ActorSystem& sys,
101 : : SupervisionPolicy policy = SupervisionPolicy{});
102 : :
103 : : void add_child(Actor child);
104 : : void remove_child(Actor child);
105 : :
106 : : // Remote child management
107 : : void add_remote_child(ActorRef child);
108 : : bool has_remote_child(const ActorAddress& addr) const;
109 : : ActorRef get_remote_child(const ActorAddress& addr) const;
110 : : void remove_remote_child(const ActorAddress& addr);
111 : : const ActorRefVec& remote_children() const {
112 : : return remote_children_;
113 : : }
114 : :
115 : : protected:
116 : : virtual SupervisionDirective on_failure(ActorId child_id, const error& err);
117 : : void handle_child_down(TypeTag tag, const StreamBuffer& payload);
118 : :
119 : : private:
120 : : SupervisionDirective decide_restart(ActorId child_id, const error& err);
121 : :
122 : : ActorVec children_;
123 : : SupervisionPolicy policy_;
124 : : RestartCountMap restart_counts_{
125 : : mem::MemStdAllocator<std::pair<const ActorId, uint32_t>>(
126 : : id_ptr(), mem::RegionType::kActor)};
127 : : std::chrono::steady_clock::time_point first_failure_time_;
128 : : ActorRefVec remote_children_;
129 : : ActorAddrVec remote_child_addresses_;
130 : : };
131 : :
132 : : } // namespace hpactor
|