Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
thread_system_config.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
16#include <kcenon/common/resilience/circuit_breaker_config.h>
17#include "../dag/dag_config.h"
19
20#include <chrono>
21#include <cstddef>
22#include <thread>
23
24namespace kcenon::thread
25{
26 using common::resilience::circuit_breaker_config;
27
28 // Forward declaration for builder pattern
29 class config_builder;
30
57 {
63 {
69 std::size_t worker_count = std::thread::hardware_concurrency();
70
77 std::size_t queue_capacity = 10000;
78
86
93 std::chrono::milliseconds wake_interval{100};
94
101 std::chrono::seconds shutdown_timeout{5};
102
109 std::chrono::seconds worker_idle_timeout{30};
110
117 bool yield_on_idle = true;
118
126
133 std::size_t max_steal_attempts = 3;
134
141 std::chrono::microseconds steal_backoff{50};
143
149 {
157 circuit_breaker_config circuit_breaker{};
159
167
190
198 [[nodiscard]] auto is_valid() const -> bool
199 {
200 // Pool validation
201 if (pool.worker_count == 0)
202 {
203 return false;
204 }
205
206 // Backpressure validation
208 {
209 return false;
210 }
211
212 // DAG validation
213 if (dag.max_retries > 10)
214 {
215 return false; // Sanity check to prevent infinite retries
216 }
217
218 return true;
219 }
220
226 static auto builder() -> config_builder;
227 };
228
250 {
251 public:
252 config_builder() = default;
253
259 auto with_worker_count(std::size_t count) -> config_builder&
260 {
261 config_.pool.worker_count = count;
262 return *this;
263 }
264
270 auto with_queue_capacity(std::size_t capacity) -> config_builder&
271 {
272 config_.pool.queue_capacity = capacity;
273 return *this;
274 }
275
285
292 {
294 return *this;
295 }
296
303 auto with_watermarks(double low, double high) -> config_builder&
304 {
307 return *this;
308 }
309
315 {
316 config_.resilience.circuit_breaker.failure_threshold = 5;
317 return *this;
318 }
319
325 auto with_failure_threshold(std::size_t threshold) -> config_builder&
326 {
327 config_.resilience.circuit_breaker.failure_threshold = threshold;
328 return *this;
329 }
330
336 auto with_open_duration(std::chrono::seconds duration) -> config_builder&
337 {
338 // Note: common_system uses 'timeout' instead of 'open_duration'
339 config_.resilience.circuit_breaker.timeout = duration;
340 return *this;
341 }
342
348 {
350 return *this;
351 }
352
359 auto with_work_stealing_params(std::size_t max_attempts,
360 std::chrono::microseconds backoff) -> config_builder&
361 {
362 config_.pool.max_steal_attempts = max_attempts;
363 config_.pool.steal_backoff = backoff;
364 return *this;
365 }
366
372 {
374 return *this;
375 }
376
384 auto with_priority_aging_params(std::chrono::milliseconds interval,
385 int boost,
386 int max_boost) -> config_builder&
387 {
391 return *this;
392 }
393
399 {
401 return *this;
402 }
403
410 {
411 config_.dag.failure_policy = policy;
412 return *this;
413 }
414
421 auto with_dag_retry_params(std::size_t max_retries,
422 std::chrono::milliseconds delay) -> config_builder&
423 {
424 config_.dag.max_retries = max_retries;
426 return *this;
427 }
428
438 {
439 if (!config_.is_valid())
440 {
441 throw std::invalid_argument("Invalid thread_system_config");
442 }
443 return config_;
444 }
445
446 private:
448 };
449
451 {
452 return config_builder{};
453 }
454
455} // namespace kcenon::thread
Configuration for backpressure and queue overflow policies.
Builder for thread_system_config.
auto enable_circuit_breaker() -> config_builder &
Enables circuit breaker with default settings.
auto with_watermarks(double low, double high) -> config_builder &
Sets backpressure watermarks.
auto enable_auto_scaling() -> config_builder &
Enables auto-scaling.
auto enable_work_stealing() -> config_builder &
Enables work stealing.
auto with_dag_retry_params(std::size_t max_retries, std::chrono::milliseconds delay) -> config_builder &
Sets DAG retry parameters.
auto enable_backpressure() -> config_builder &
Enables backpressure with default settings.
auto with_worker_count(std::size_t count) -> config_builder &
Sets the number of worker threads.
auto with_priority_aging_params(std::chrono::milliseconds interval, int boost, int max_boost) -> config_builder &
Sets priority aging parameters.
auto with_open_duration(std::chrono::seconds duration) -> config_builder &
Sets circuit breaker open duration.
auto with_queue_capacity(std::size_t capacity) -> config_builder &
Sets the queue capacity.
auto with_backpressure_policy(backpressure_policy policy) -> config_builder &
Sets the backpressure policy.
auto build() -> thread_system_config
Builds the final configuration.
auto enable_priority_aging() -> config_builder &
Enables priority aging with default settings.
auto with_work_stealing_params(std::size_t max_attempts, std::chrono::microseconds backoff) -> config_builder &
Sets work stealing parameters.
auto with_dag_failure_policy(dag_failure_policy policy) -> config_builder &
Sets DAG failure policy.
auto with_failure_threshold(std::size_t threshold) -> config_builder &
Sets circuit breaker failure threshold.
Configuration for DAG scheduler including failure handling policies.
backpressure_policy
Policy for handling queue overflow conditions.
@ delay
Delay processing (attempt later)
@ low
Between low and high watermark.
@ high
Above high_watermark, approaching capacity.
@ block
Block until space is available (with timeout)
Core threading foundation of the thread system library.
Definition thread_impl.h:17
dag_failure_policy
Defines how the DAG scheduler handles job failures.
Definition dag_config.h:42
Configuration for priority aging and starvation prevention.
Configuration for backpressure mechanisms.
backpressure_policy policy
The backpressure policy to use.
auto is_valid() const -> bool
Validates the configuration.
double low_watermark
Low watermark threshold (percentage of max_size).
double high_watermark
High watermark threshold (percentage of max_size).
Configuration options for the DAG scheduler.
Definition dag_config.h:73
std::size_t max_retries
Maximum number of retry attempts for failed jobs.
Definition dag_config.h:90
dag_failure_policy failure_policy
How to handle job failures.
Definition dag_config.h:82
std::chrono::milliseconds retry_delay
Delay between retry attempts.
Definition dag_config.h:97
Configuration for priority aging behavior.
bool enabled
Whether priority aging is enabled.
int priority_boost_per_interval
Amount of priority boost applied per aging interval.
int max_priority_boost
Maximum total priority boost that can be applied.
std::chrono::milliseconds aging_interval
Interval at which aging is applied.
Configuration for thread pool behavior.
std::size_t worker_count
Number of worker threads in the pool.
backpressure_config backpressure
Backpressure configuration.
std::size_t max_steal_attempts
Maximum steal attempts before backing off.
std::chrono::seconds shutdown_timeout
Shutdown timeout.
std::chrono::milliseconds wake_interval
Wake interval for idle workers.
std::chrono::microseconds steal_backoff
Backoff duration between steal attempts.
std::size_t queue_capacity
Maximum number of jobs in the queue.
std::chrono::seconds worker_idle_timeout
Worker idle timeout.
circuit_breaker_config circuit_breaker
Circuit breaker configuration.
Configuration for dynamic scaling features.
priority_aging_config priority_aging
Priority aging configuration.
bool auto_scaling_enabled
Enable automatic scaling based on load.
Unified configuration for thread_system.
dag_config dag
DAG (Directed Acyclic Graph) configuration.
auto is_valid() const -> bool
Validates the entire configuration.
struct kcenon::thread::thread_system_config::scaling_config scaling
static auto builder() -> config_builder
Creates a builder for fluent configuration.
struct kcenon::thread::thread_system_config::resilience_config resilience
struct kcenon::thread::thread_system_config::pool_config pool