Class: Semian::Simple::PIDController
- Inherits:
-
Object
- Object
- Semian::Simple::PIDController
- Defined in:
- lib/semian/pid_controller.rb
Overview
PID Controller for adaptive circuit breaking Based on the error function: P = (error_rate - ideal_error_rate) - (1 - (error_rate - ideal_error_rate)) * rejection_rate Note: P increases when error_rate increases
P decreases when rejection_rate increases (providing feedback)
Direct Known Subclasses
Instance Attribute Summary collapse
-
#rejection_rate ⇒ Object
readonly
Returns the value of attribute rejection_rate.
Instance Method Summary collapse
-
#initialize(kp:, ki:, kd:, window_size:, sliding_interval:, implementation:, initial_error_rate:, dead_zone_ratio:, ideal_error_rate_estimator_cap_value:, integral_upper_cap:, integral_lower_cap:) ⇒ PIDController
constructor
A new instance of PIDController.
-
#metrics(full: true) ⇒ Object
Get current metrics for monitoring/debugging.
- #record_request(outcome) ⇒ Object
-
#reset ⇒ Object
Reset the controller state.
-
#should_reject? ⇒ Boolean
Should we reject this request based on current rejection rate?.
- #update ⇒ Object
Constructor Details
#initialize(kp:, ki:, kd:, window_size:, sliding_interval:, implementation:, initial_error_rate:, dead_zone_ratio:, ideal_error_rate_estimator_cap_value:, integral_upper_cap:, integral_lower_cap:) ⇒ PIDController
Returns a new instance of PIDController.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/semian/pid_controller.rb', line 16 def initialize(kp:, ki:, kd:, window_size:, sliding_interval:, implementation:, initial_error_rate:, dead_zone_ratio:, ideal_error_rate_estimator_cap_value:, integral_upper_cap:, integral_lower_cap:) @kp = kp @ki = ki @kd = kd @dead_zone_ratio = dead_zone_ratio @integral_upper_cap = integral_upper_cap @integral_lower_cap = integral_lower_cap @rejection_rate = 0.0 @integral = 0.0 @derivative = 0.0 @previous_p_value = 0.0 @last_ideal_error_rate = initial_error_rate @window_size = window_size @sliding_interval = sliding_interval @smoother = SimpleExponentialSmoother.new( cap_value: ideal_error_rate_estimator_cap_value, initial_value: initial_error_rate, observations_per_minute: 60 / sliding_interval, ) @errors = implementation::SlidingWindow.new(max_size: 200 * window_size) @successes = implementation::SlidingWindow.new(max_size: 200 * window_size) @rejections = implementation::SlidingWindow.new(max_size: 200 * window_size) @last_error_rate = 0.0 @last_p_value = 0.0 end |
Instance Attribute Details
#rejection_rate ⇒ Object (readonly)
Returns the value of attribute rejection_rate.
14 15 16 |
# File 'lib/semian/pid_controller.rb', line 14 def rejection_rate @rejection_rate end |
Instance Method Details
#metrics(full: true) ⇒ Object
Get current metrics for monitoring/debugging
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/semian/pid_controller.rb', line 110 def metrics(full: true) result = { rejection_rate: @rejection_rate, error_rate: @last_error_rate, ideal_error_rate: @last_ideal_error_rate, dead_zone_ratio: @dead_zone_ratio, p_value: @last_p_value, previous_p_value: @previous_p_value, integral: @integral, derivative: @derivative, } if full result[:smoother_state] = @smoother.state result[:current_window_requests] = { success: @successes.size, error: @errors.size, rejected: @rejections.size, } end result end |
#record_request(outcome) ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/semian/pid_controller.rb', line 47 def record_request(outcome) case outcome when :error @errors.push(current_time) when :success @successes.push(current_time) when :rejected @rejections.push(current_time) end end |
#reset ⇒ Object
Reset the controller state
95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/semian/pid_controller.rb', line 95 def reset @rejection_rate = 0.0 @integral = 0.0 @previous_p_value = 0.0 @derivative = 0.0 @last_p_value = 0.0 @errors.clear @successes.clear @rejections.clear @last_error_rate = 0.0 @smoother.reset @last_ideal_error_rate = @smoother.forecast end |
#should_reject? ⇒ Boolean
Should we reject this request based on current rejection rate?
90 91 92 |
# File 'lib/semian/pid_controller.rb', line 90 def should_reject? rand < @rejection_rate end |
#update ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/semian/pid_controller.rb', line 58 def update # Store the last window's P value so that we can serve it up in the metrics snapshots @previous_p_value = @last_p_value @last_error_rate = calculate_error_rate store_error_rate(@last_error_rate) dt = @sliding_interval @last_p_value = calculate_p_value(@last_error_rate) proportional = @kp * @last_p_value @integral += @last_p_value * dt integral = @ki * @integral @derivative = @kd * (@last_p_value - @previous_p_value) / dt # Calculate the control signal (change in rejection rate) control_signal = proportional + integral + @derivative # Calculate what the new rejection rate would be new_rejection_rate = @rejection_rate + control_signal # Update rejection rate (clamped between 0 and 1) @rejection_rate = new_rejection_rate.clamp(0.0, 1.0) @integral = @integral.clamp(@integral_lower_cap, @integral_upper_cap) @rejection_rate end |