Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
kcenon::monitoring::fault_tolerance_manager< T > Class Template Reference

Fault tolerance manager template class. More...

#include <fault_tolerance_manager.h>

Collaboration diagram for kcenon::monitoring::fault_tolerance_manager< T >:
Collaboration graph

Public Member Functions

 fault_tolerance_manager ()
 
 fault_tolerance_manager (const std::string &name)
 
 fault_tolerance_manager (const std::string &name, const fault_tolerance_config &cfg)
 
template<typename Func >
common::Result< T > execute (Func &&func)
 Execute a function with fault tolerance.
 
template<typename Func >
common::Result< T > execute_with_timeout (Func &&func, std::chrono::milliseconds timeout)
 Execute a function with timeout.
 
common::Result< bool > is_healthy ()
 Check if fault tolerance manager is healthy.
 
fault_tolerance_metrics get_metrics () const
 Get fault tolerance metrics.
 
const std::string & get_name () const
 Get manager name.
 

Private Member Functions

void initialize ()
 
template<typename Func >
common::Result< T > execute_internal (Func &&func)
 
template<typename Func >
common::Result< T > execute_circuit_breaker_first (Func &&func)
 
template<typename Func >
common::Result< T > execute_retry_first (Func &&func)
 

Private Attributes

std::string name_
 
fault_tolerance_config config_
 
std::unique_ptr< circuit_breakercircuit_breaker_
 
std::unique_ptr< retry_executor< T > > retry_executor_
 
fault_tolerance_metrics metrics_
 

Detailed Description

template<typename T>
class kcenon::monitoring::fault_tolerance_manager< T >

Fault tolerance manager template class.

Combines circuit breaker and retry logic for resilient operations.

Template Parameters
TThe return value type of operations

Definition at line 105 of file fault_tolerance_manager.h.

Constructor & Destructor Documentation

◆ fault_tolerance_manager() [1/3]

template<typename T >
kcenon::monitoring::fault_tolerance_manager< T >::fault_tolerance_manager ( )
inline

◆ fault_tolerance_manager() [2/3]

template<typename T >
kcenon::monitoring::fault_tolerance_manager< T >::fault_tolerance_manager ( const std::string & name)
inlineexplicit

Definition at line 111 of file fault_tolerance_manager.h.

112 : name_(name), config_() {
113 initialize();
114 }

References kcenon::monitoring::fault_tolerance_manager< T >::initialize().

Here is the call graph for this function:

◆ fault_tolerance_manager() [3/3]

template<typename T >
kcenon::monitoring::fault_tolerance_manager< T >::fault_tolerance_manager ( const std::string & name,
const fault_tolerance_config & cfg )
inlineexplicit

Definition at line 116 of file fault_tolerance_manager.h.

117 : name_(name), config_(cfg) {
118 initialize();
119 }

References kcenon::monitoring::fault_tolerance_manager< T >::initialize().

Here is the call graph for this function:

Member Function Documentation

◆ execute()

template<typename T >
template<typename Func >
common::Result< T > kcenon::monitoring::fault_tolerance_manager< T >::execute ( Func && func)
inline

Execute a function with fault tolerance.

Template Parameters
FuncThe function type to execute (must return common::Result<T>)
Parameters
funcThe function to execute
Returns
common::Result<T> containing success value or error

Definition at line 129 of file fault_tolerance_manager.h.

129 {
131
132 common::Result<T> op_result = common::Result<T>::err(error_info(monitoring_error_code::operation_failed, "Not executed").to_common_error());
133
135 op_result = execute_circuit_breaker_first(std::forward<Func>(func));
136 } else {
137 op_result = execute_retry_first(std::forward<Func>(func));
138 }
139
140 if (op_result.is_ok()) {
142 } else {
144 }
145
146 return op_result;
147 }
common::Result< T > execute_retry_first(Func &&func)
common::Result< T > execute_circuit_breaker_first(Func &&func)

References kcenon::monitoring::fault_tolerance_config::circuit_breaker_first, kcenon::monitoring::fault_tolerance_manager< T >::config_, kcenon::monitoring::fault_tolerance_manager< T >::execute_circuit_breaker_first(), kcenon::monitoring::fault_tolerance_manager< T >::execute_retry_first(), kcenon::monitoring::fault_tolerance_metrics::failed_operations, kcenon::monitoring::fault_tolerance_manager< T >::metrics_, kcenon::monitoring::operation_failed, kcenon::monitoring::fault_tolerance_metrics::successful_operations, and kcenon::monitoring::fault_tolerance_metrics::total_operations.

Referenced by TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ execute_circuit_breaker_first()

template<typename T >
template<typename Func >
common::Result< T > kcenon::monitoring::fault_tolerance_manager< T >::execute_circuit_breaker_first ( Func && func)
inlineprivate

Definition at line 229 of file fault_tolerance_manager.h.

229 {
232 return execute_with_circuit_breaker<T>(*circuit_breaker_, name_, [this, &func]() {
233 return retry_executor_->execute(func);
234 });
235 }
236 return execute_with_circuit_breaker<T>(*circuit_breaker_, name_, std::forward<Func>(func));
237 }
239 return retry_executor_->execute(std::forward<Func>(func));
240 }
241 return func();
242 }
std::unique_ptr< circuit_breaker > circuit_breaker_
std::unique_ptr< retry_executor< T > > retry_executor_
common::Result< T > execute_with_circuit_breaker(circuit_breaker &cb, const std::string &name, Func &&func)
Execute an operation through a circuit breaker.

References kcenon::monitoring::fault_tolerance_manager< T >::circuit_breaker_, kcenon::monitoring::fault_tolerance_manager< T >::config_, kcenon::monitoring::fault_tolerance_config::enable_circuit_breaker, kcenon::monitoring::fault_tolerance_config::enable_retry, kcenon::monitoring::execute_with_circuit_breaker(), kcenon::monitoring::fault_tolerance_manager< T >::name_, and kcenon::monitoring::fault_tolerance_manager< T >::retry_executor_.

Referenced by kcenon::monitoring::fault_tolerance_manager< T >::execute(), and kcenon::monitoring::fault_tolerance_manager< T >::execute_internal().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ execute_internal()

template<typename T >
template<typename Func >
common::Result< T > kcenon::monitoring::fault_tolerance_manager< T >::execute_internal ( Func && func)
inlineprivate

◆ execute_retry_first()

◆ execute_with_timeout()

template<typename T >
template<typename Func >
common::Result< T > kcenon::monitoring::fault_tolerance_manager< T >::execute_with_timeout ( Func && func,
std::chrono::milliseconds timeout )
inline

Execute a function with timeout.

Template Parameters
FuncThe function type to execute (must return common::Result<T>)
Parameters
funcThe function to execute
timeoutMaximum execution time
Returns
common::Result<T> containing success value or error

Definition at line 158 of file fault_tolerance_manager.h.

158 {
160
161 auto future_result = std::async(std::launch::async, [this, func = std::forward<Func>(func)]() mutable {
162 return this->execute_internal(std::move(func));
163 });
164
165 if (future_result.wait_for(timeout) == std::future_status::timeout) {
168 return common::make_error<T>(static_cast<int>(monitoring_error_code::operation_timeout),
169 "Operation timed out after " + std::to_string(timeout.count()) + "ms");
170 }
171
172 auto op_result = future_result.get();
173 if (op_result.is_ok()) {
175 } else {
177 }
178
179 return op_result;
180 }
common::Result< T > execute_internal(Func &&func)

References kcenon::monitoring::fault_tolerance_manager< T >::execute_internal(), kcenon::monitoring::fault_tolerance_metrics::failed_operations, kcenon::monitoring::fault_tolerance_manager< T >::metrics_, kcenon::monitoring::operation_timeout, kcenon::monitoring::fault_tolerance_metrics::successful_operations, kcenon::monitoring::fault_tolerance_metrics::timeouts, and kcenon::monitoring::fault_tolerance_metrics::total_operations.

Referenced by TEST_F().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_metrics()

template<typename T >
fault_tolerance_metrics kcenon::monitoring::fault_tolerance_manager< T >::get_metrics ( ) const
inline

Get fault tolerance metrics.

Definition at line 199 of file fault_tolerance_manager.h.

199 {
200 return metrics_;
201 }

References kcenon::monitoring::fault_tolerance_manager< T >::metrics_.

Referenced by TEST_F().

Here is the caller graph for this function:

◆ get_name()

template<typename T >
const std::string & kcenon::monitoring::fault_tolerance_manager< T >::get_name ( ) const
inline

Get manager name.

Definition at line 206 of file fault_tolerance_manager.h.

206 {
207 return name_;
208 }

References kcenon::monitoring::fault_tolerance_manager< T >::name_.

◆ initialize()

◆ is_healthy()

template<typename T >
common::Result< bool > kcenon::monitoring::fault_tolerance_manager< T >::is_healthy ( )
inline

Check if fault tolerance manager is healthy.

Returns
common::Result<bool> indicating health status

Definition at line 186 of file fault_tolerance_manager.h.

186 {
188 auto state = circuit_breaker_->get_state();
189 if (state == circuit_state::OPEN) {
190 return common::ok(false);
191 }
192 }
193 return common::ok(true);
194 }

References kcenon::monitoring::fault_tolerance_manager< T >::circuit_breaker_, kcenon::monitoring::fault_tolerance_manager< T >::config_, and kcenon::monitoring::fault_tolerance_config::enable_circuit_breaker.

Referenced by TEST_F().

Here is the caller graph for this function:

Member Data Documentation

◆ circuit_breaker_

◆ config_

◆ metrics_

◆ name_

◆ retry_executor_


The documentation for this class was generated from the following file: