22#include <unordered_map>
132 std::lock_guard<std::mutex> lock(
mutex_);
135 "Service already registered: " + config.
name);
151 std::lock_guard<std::mutex> lock(
mutex_);
155 "Service not found: " + name);
165 const std::string& reason) {
166 std::lock_guard<std::mutex> lock(
mutex_);
171 "Service not found: " + name);
175 it->second.current_level = level;
176 it->second.last_degradation_reason = reason;
177 it->second.last_state_change = std::chrono::steady_clock::now();
187 std::lock_guard<std::mutex> lock(
mutex_);
191 "Service not found: " + name);
196 it->second.last_degradation_reason.clear();
197 it->second.last_state_change = std::chrono::steady_clock::now();
207 std::lock_guard<std::mutex> lock(
mutex_);
211 state.last_degradation_reason.clear();
212 state.last_state_change = std::chrono::steady_clock::now();
222 std::lock_guard<std::mutex> lock(
mutex_);
227 return it->second.current_level;
238 std::lock_guard<std::mutex> lock(
mutex_);
246 common::VoidResult
execute_plan(
const std::string& plan_name,
const std::string& reason) {
247 std::lock_guard<std::mutex> lock(
mutex_);
248 auto it =
plans_.find(plan_name);
251 "Plan not found: " + plan_name);
254 const auto& plan = it->second;
257 for (
const auto& service_name : plan.services_to_maintain) {
258 auto service_it =
services_.find(service_name);
261 service_it->second.current_level = plan.target_level;
262 service_it->second.last_degradation_reason = reason;
263 service_it->second.last_state_change = std::chrono::steady_clock::now();
269 for (
const auto& service_name : plan.services_to_disable) {
270 auto service_it =
services_.find(service_name);
274 service_it->second.last_degradation_reason = reason;
275 service_it->second.last_state_change = std::chrono::steady_clock::now();
287 std::lock_guard<std::mutex> lock(
mutex_);
289 return common::ok(
true);
292 size_t normal_count = 0;
293 for (
const auto& [name, state] :
services_) {
299 double healthy_ratio =
static_cast<double>(normal_count) /
static_cast<double>(
services_.size());
300 return common::ok(healthy_ratio > 0.5);
321 std::lock_guard<std::mutex> lock(
mutex_);
322 std::vector<std::string> names;
324 for (
const auto& [name, state] :
services_) {
325 names.push_back(name);
333 std::unordered_map<std::string, service_state>
services_;
334 std::unordered_map<std::string, degradation_plan>
plans_;
350 std::shared_ptr<graceful_degradation_manager> manager,
387 std::shared_ptr<graceful_degradation_manager>
manager_;
399 std::lock_guard<std::mutex> lock(
mutex_);
404 std::shared_ptr<error_boundary<T>>
get_boundary(
const std::string& name) {
405 std::lock_guard<std::mutex> lock(
mutex_);
409 return std::any_cast<std::shared_ptr<error_boundary<T>>>(it->second);
410 }
catch (
const std::bad_any_cast&) {
418 std::lock_guard<std::mutex> lock(
mutex_);
423 std::lock_guard<std::mutex> lock(
mutex_);
424 std::vector<std::string> names;
426 for (
const auto& [name, boundary] :
registry_) {
427 names.push_back(name);
433 std::lock_guard<std::mutex> lock(
mutex_);
456 return std::make_shared<graceful_degradation_manager>(name);
473 const std::vector<std::string>& maintain,
474 const std::vector<std::string>& disable,
489 const std::string& name,
490 std::shared_ptr<graceful_degradation_manager> manager,
493 return std::make_shared<degradable_service<T>>(name, std::move(manager),
494 std::move(normal_op), std::move(degraded_op));
Degradable service wrapper.
normal_operation normal_op_
std::function< common::Result< T >()> normal_operation
const std::string & get_name() const
Get service name.
degraded_operation degraded_op_
common::Result< T > execute()
Execute the service operation.
std::shared_ptr< graceful_degradation_manager > manager_
std::function< common::Result< T >(degradation_level)> degraded_operation
degradable_service(const std::string &name, std::shared_ptr< graceful_degradation_manager > manager, normal_operation normal_op, degraded_operation degraded_op)
Error boundary registry for managing multiple boundaries.
std::unordered_map< std::string, std::any > registry_
std::vector< std::string > get_all_names() const
std::shared_ptr< error_boundary< T > > get_boundary(const std::string &name)
void remove_boundary(const std::string &name)
void register_boundary(const std::string &name, std::shared_ptr< error_boundary< T > > boundary)
Error boundary implementation for resilient operations.
Graceful degradation manager.
degradation_level get_service_level(const std::string &name) const
Get current degradation level for a service.
const std::string & get_name() const
Get manager name.
common::VoidResult unregister_service(const std::string &name)
Unregister a service.
graceful_degradation_manager()
common::VoidResult register_service(const service_config &config)
Register a service for management.
common::VoidResult add_degradation_plan(const degradation_plan &plan)
Add a degradation plan.
std::unordered_map< std::string, service_state > services_
std::unordered_map< std::string, degradation_plan > plans_
graceful_degradation_metrics metrics_
common::VoidResult recover_service(const std::string &name)
Recover a specific service to normal operation.
graceful_degradation_manager(const std::string &name)
common::VoidResult execute_plan(const std::string &plan_name, const std::string &reason)
Execute a degradation plan.
common::VoidResult recover_all_services()
Recover all services to normal operation.
common::Result< bool > is_healthy() const
Check if the manager is healthy (more than 50% services at normal level)
graceful_degradation_metrics get_metrics() const
Get metrics.
common::VoidResult degrade_service(const std::string &name, degradation_level level, const std::string &reason)
Degrade a specific service.
std::vector< std::string > get_service_names() const
Get all registered service names.
Error boundary with degradation levels for fault isolation.
std::shared_ptr< degradable_service< T > > create_degradable_service(const std::string &name, std::shared_ptr< graceful_degradation_manager > manager, typename degradable_service< T >::normal_operation normal_op, typename degradable_service< T >::degraded_operation degraded_op)
Create a degradable service.
service_config create_service_config(const std::string &name, service_priority priority)
Create a service configuration.
service_priority
Service priority levels.
degradation_plan create_degradation_plan(const std::string &name, const std::vector< std::string > &maintain, const std::vector< std::string > &disable, degradation_level level)
Create a degradation plan.
degradation_level
Degradation levels for error boundary.
std::shared_ptr< graceful_degradation_manager > create_degradation_manager(const std::string &name)
Create a graceful degradation manager.
error_boundary_registry & global_error_boundary_registry()
Get global error boundary registry.
Degradation plan for coordinated service degradation.
std::vector< std::string > services_to_disable
std::vector< std::string > services_to_maintain
degradation_level target_level
Extended error information with context.
Graceful degradation metrics.
std::atomic< size_t > successful_recoveries
std::atomic< size_t > successful_degradations
std::atomic< size_t > recovery_attempts
std::atomic< size_t > failed_degradations
graceful_degradation_metrics()=default
graceful_degradation_metrics(const graceful_degradation_metrics &other)
std::atomic< size_t > total_degradations
graceful_degradation_metrics & operator=(const graceful_degradation_metrics &other)
Service configuration for graceful degradation.
double error_rate_threshold
service_priority priority
std::chrono::milliseconds health_check_interval
std::chrono::steady_clock::time_point last_state_change
degradation_level current_level
std::string last_degradation_reason