Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
kcenon::thread::autoscaling_pool_policy Class Reference

Pool policy that implements automatic scaling for dynamic worker management. More...

#include <autoscaling_pool_policy.h>

Inheritance diagram for kcenon::thread::autoscaling_pool_policy:
Inheritance graph
Collaboration diagram for kcenon::thread::autoscaling_pool_policy:
Collaboration graph

Public Member Functions

 autoscaling_pool_policy (thread_pool &pool, const autoscaling_policy &config={})
 Constructs an autoscaling pool policy with the given configuration.
 
 autoscaling_pool_policy (std::shared_ptr< autoscaler > scaler)
 Constructs an autoscaling pool policy with an existing autoscaler.
 
 ~autoscaling_pool_policy () override
 Destructor. Stops the autoscaler if running.
 
 autoscaling_pool_policy (const autoscaling_pool_policy &)=delete
 
autoscaling_pool_policyoperator= (const autoscaling_pool_policy &)=delete
 
 autoscaling_pool_policy (autoscaling_pool_policy &&)=delete
 
autoscaling_pool_policyoperator= (autoscaling_pool_policy &&)=delete
 
auto on_enqueue (job &j) -> common::VoidResult override
 Called before a job is enqueued.
 
void on_job_start (job &j) override
 Called when job starts executing.
 
void on_job_complete (job &j, bool success, const std::exception *error=nullptr) override
 Called when a job completes.
 
auto get_name () const -> std::string override
 Gets the policy name.
 
auto is_enabled () const -> bool override
 Checks if the policy is enabled.
 
void set_enabled (bool enabled) override
 Enables or disables the policy.
 
void start ()
 Starts the autoscaler monitor thread.
 
void stop ()
 Stops the autoscaler monitor thread.
 
auto is_active () const -> bool
 Checks if the autoscaler is currently active.
 
auto get_autoscaler () const -> std::shared_ptr< autoscaler >
 Gets the underlying autoscaler.
 
auto get_stats () const -> autoscaling_stats
 Gets current autoscaling statistics.
 
void set_policy (const autoscaling_policy &config)
 Updates the autoscaling policy configuration.
 
auto get_policy () const -> const autoscaling_policy &
 Gets the current autoscaling policy configuration.
 
auto evaluate_now () -> scaling_decision
 Manually triggers a scaling evaluation.
 
auto scale_to (std::size_t target_workers) -> common::VoidResult
 Manually scales to a specific worker count.
 
- Public Member Functions inherited from kcenon::thread::pool_policy
virtual ~pool_policy ()=default
 Virtual destructor for proper cleanup.
 

Private Attributes

std::shared_ptr< autoscalerautoscaler_
 
std::atomic< bool > enabled_ {true}
 

Detailed Description

Pool policy that implements automatic scaling for dynamic worker management.

This policy wraps the autoscaling functionality as a composable pool policy, enabling automatic scaling without modifying the thread_pool class.

Autoscaling Behavior

The autoscaler monitors thread pool metrics and adjusts worker count:

  • Scale-up triggered when ANY threshold is exceeded (high utilization, queue depth, latency)
  • Scale-down triggered when ALL conditions are met (low utilization, low queue depth, idle workers)

Thread Safety

All methods are thread-safe and can be called from any thread.

Usage Example

config.min_workers = 2;
config.max_workers = 16;
auto as_policy = std::make_unique<autoscaling_pool_policy>(pool, config);
pool->add_policy(std::move(as_policy));
// Now workers are automatically scaled based on load
Configuration for autoscaling behavior.
@ automatic
Fully automatic scaling.
std::size_t min_workers
Minimum number of workers (never scale below this)
std::size_t max_workers
Maximum number of workers (never scale above this)
See also
pool_policy
autoscaler
autoscaling_policy

Definition at line 59 of file autoscaling_pool_policy.h.

Constructor & Destructor Documentation

◆ autoscaling_pool_policy() [1/4]

kcenon::thread::autoscaling_pool_policy::autoscaling_pool_policy ( thread_pool & pool,
const autoscaling_policy & config = {} )
explicit

Constructs an autoscaling pool policy with the given configuration.

Parameters
poolReference to the thread pool to manage.
configAutoscaling policy configuration.

Definition at line 11 of file autoscaling_pool_policy.cpp.

12 : autoscaler_(std::make_shared<autoscaler>(pool, config))
13{
14}

◆ autoscaling_pool_policy() [2/4]

kcenon::thread::autoscaling_pool_policy::autoscaling_pool_policy ( std::shared_ptr< autoscaler > scaler)
explicit

Constructs an autoscaling pool policy with an existing autoscaler.

Parameters
scalerShared pointer to an existing autoscaler.

This allows sharing an autoscaler across multiple pools or components.

Definition at line 16 of file autoscaling_pool_policy.cpp.

17 : autoscaler_(std::move(scaler))
18{
19}

◆ ~autoscaling_pool_policy()

kcenon::thread::autoscaling_pool_policy::~autoscaling_pool_policy ( )
override

Destructor. Stops the autoscaler if running.

Definition at line 21 of file autoscaling_pool_policy.cpp.

22{
23 if (autoscaler_ && autoscaler_->is_active()) {
24 autoscaler_->stop();
25 }
26}

References autoscaler_.

◆ autoscaling_pool_policy() [3/4]

kcenon::thread::autoscaling_pool_policy::autoscaling_pool_policy ( const autoscaling_pool_policy & )
delete

◆ autoscaling_pool_policy() [4/4]

kcenon::thread::autoscaling_pool_policy::autoscaling_pool_policy ( autoscaling_pool_policy && )
delete

Member Function Documentation

◆ evaluate_now()

auto kcenon::thread::autoscaling_pool_policy::evaluate_now ( ) -> scaling_decision
nodiscard

Manually triggers a scaling evaluation.

Returns
The scaling decision that would be made.

Definition at line 124 of file autoscaling_pool_policy.cpp.

125{
126 if (autoscaler_) {
127 return autoscaler_->evaluate_now();
128 }
129 return scaling_decision{};
130}

◆ get_autoscaler()

auto kcenon::thread::autoscaling_pool_policy::get_autoscaler ( ) const -> std::shared_ptr<autoscaler>
nodiscard

Gets the underlying autoscaler.

Returns
Shared pointer to the autoscaler.

Useful for accessing detailed scaling controls and statistics.

Definition at line 95 of file autoscaling_pool_policy.cpp.

96{
97 return autoscaler_;
98}

References autoscaler_.

◆ get_name()

auto kcenon::thread::autoscaling_pool_policy::get_name ( ) const -> std::string
nodiscardoverridevirtual

Gets the policy name.

Returns
"autoscaling_pool_policy"

Implements kcenon::thread::pool_policy.

Definition at line 47 of file autoscaling_pool_policy.cpp.

48{
49 return "autoscaling_pool_policy";
50}

◆ get_policy()

auto kcenon::thread::autoscaling_pool_policy::get_policy ( ) const -> const autoscaling_policy&
nodiscard

Gets the current autoscaling policy configuration.

Returns
Const reference to the policy.

Definition at line 115 of file autoscaling_pool_policy.cpp.

116{
117 static autoscaling_policy default_policy;
118 if (autoscaler_) {
119 return autoscaler_->get_policy();
120 }
121 return default_policy;
122}

References autoscaler_.

◆ get_stats()

auto kcenon::thread::autoscaling_pool_policy::get_stats ( ) const -> autoscaling_stats
nodiscard

Gets current autoscaling statistics.

Returns
Statistics about scaling operations.

Definition at line 100 of file autoscaling_pool_policy.cpp.

101{
102 if (autoscaler_) {
103 return autoscaler_->get_stats();
104 }
105 return autoscaling_stats{};
106}

References autoscaler_.

◆ is_active()

auto kcenon::thread::autoscaling_pool_policy::is_active ( ) const -> bool
nodiscard

Checks if the autoscaler is currently active.

Returns
True if the monitor thread is running.

Definition at line 90 of file autoscaling_pool_policy.cpp.

91{
92 return autoscaler_ && autoscaler_->is_active();
93}

References autoscaler_.

◆ is_enabled()

auto kcenon::thread::autoscaling_pool_policy::is_enabled ( ) const -> bool
nodiscardoverridevirtual

Checks if the policy is enabled.

Returns
True if enabled.

Reimplemented from kcenon::thread::pool_policy.

Definition at line 52 of file autoscaling_pool_policy.cpp.

53{
54 return enabled_.load(std::memory_order_acquire);
55}

References enabled_.

◆ on_enqueue()

auto kcenon::thread::autoscaling_pool_policy::on_enqueue ( job & j) -> common::VoidResult
overridevirtual

Called before a job is enqueued.

Parameters
jReference to the job being enqueued.
Returns
common::ok() - autoscaling always allows jobs.

Autoscaling does not reject jobs; it adjusts worker count to handle load.

Implements kcenon::thread::pool_policy.

Definition at line 28 of file autoscaling_pool_policy.cpp.

29{
30 (void)j; // Autoscaling does not reject jobs
31 return common::ok();
32}

◆ on_job_complete()

void kcenon::thread::autoscaling_pool_policy::on_job_complete ( job & j,
bool success,
const std::exception * error = nullptr )
overridevirtual

Called when a job completes.

Parameters
jReference to the completed job.
successTrue if job succeeded.
errorException pointer if job failed.

Records completion for metrics used in scaling decisions.

Implements kcenon::thread::pool_policy.

Definition at line 39 of file autoscaling_pool_policy.cpp.

40{
41 (void)j;
42 (void)success;
43 (void)error;
44 // Metrics are collected by the autoscaler's monitor thread
45}
@ error
Error events that might still allow continuation.

References kcenon::thread::success.

◆ on_job_start()

void kcenon::thread::autoscaling_pool_policy::on_job_start ( job & j)
overridevirtual

Called when job starts executing.

Parameters
jReference to the job.

Records job start for metrics collection.

Implements kcenon::thread::pool_policy.

Definition at line 34 of file autoscaling_pool_policy.cpp.

35{
36 (void)j; // Metrics are collected by the autoscaler's monitor thread
37}

◆ operator=() [1/2]

autoscaling_pool_policy & kcenon::thread::autoscaling_pool_policy::operator= ( autoscaling_pool_policy && )
delete

◆ operator=() [2/2]

autoscaling_pool_policy & kcenon::thread::autoscaling_pool_policy::operator= ( const autoscaling_pool_policy & )
delete

◆ scale_to()

auto kcenon::thread::autoscaling_pool_policy::scale_to ( std::size_t target_workers) -> common::VoidResult

Manually scales to a specific worker count.

Parameters
target_workersDesired number of workers.
Returns
Error if scaling fails.

Definition at line 132 of file autoscaling_pool_policy.cpp.

133{
134 if (autoscaler_) {
135 return autoscaler_->scale_to(target_workers);
136 }
137 return common::ok();
138}

◆ set_enabled()

void kcenon::thread::autoscaling_pool_policy::set_enabled ( bool enabled)
overridevirtual

Enables or disables the policy.

Parameters
enabledWhether to enable.

When disabled, the autoscaler is stopped. When enabled, it is started (if the pool is running).

Reimplemented from kcenon::thread::pool_policy.

Definition at line 57 of file autoscaling_pool_policy.cpp.

58{
59 bool was_enabled = enabled_.exchange(enabled, std::memory_order_acq_rel);
60
61 if (autoscaler_) {
62 if (enabled && !was_enabled) {
63 // Transitioning to enabled - start the autoscaler
64 if (!autoscaler_->is_active()) {
65 autoscaler_->start();
66 }
67 } else if (!enabled && was_enabled) {
68 // Transitioning to disabled - stop the autoscaler
69 if (autoscaler_->is_active()) {
70 autoscaler_->stop();
71 }
72 }
73 }
74}

References autoscaler_, and enabled_.

◆ set_policy()

void kcenon::thread::autoscaling_pool_policy::set_policy ( const autoscaling_policy & config)

Updates the autoscaling policy configuration.

Parameters
configNew policy configuration.

Definition at line 108 of file autoscaling_pool_policy.cpp.

109{
110 if (autoscaler_) {
111 autoscaler_->set_policy(config);
112 }
113}

References autoscaler_.

◆ start()

void kcenon::thread::autoscaling_pool_policy::start ( )

Starts the autoscaler monitor thread.

Should be called after the pool starts. This is automatically managed if the policy is added before pool.start().

Definition at line 76 of file autoscaling_pool_policy.cpp.

77{
78 if (autoscaler_ && enabled_.load(std::memory_order_acquire)) {
79 autoscaler_->start();
80 }
81}

References autoscaler_, and enabled_.

◆ stop()

void kcenon::thread::autoscaling_pool_policy::stop ( )

Stops the autoscaler monitor thread.

Definition at line 83 of file autoscaling_pool_policy.cpp.

84{
85 if (autoscaler_) {
86 autoscaler_->stop();
87 }
88}

References autoscaler_.

Member Data Documentation

◆ autoscaler_

std::shared_ptr<autoscaler> kcenon::thread::autoscaling_pool_policy::autoscaler_
private

◆ enabled_

std::atomic<bool> kcenon::thread::autoscaling_pool_policy::enabled_ {true}
private

Definition at line 206 of file autoscaling_pool_policy.h.

206{true};

Referenced by is_enabled(), set_enabled(), and start().


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