Class: Lhm::Throttler::ThreadsRunning
- Inherits:
-
Object
- Object
- Lhm::Throttler::ThreadsRunning
- Includes:
- Command, BackoffReduction
- Defined in:
- lib/lhm/throttler/threads_running.rb
Constant Summary collapse
- DEFAULT_STRIDE =
2_000
- DEFAULT_INITIAL_TIMEOUT =
0.1
- DEFAULT_HEALTHY_RANGE =
(0..50)
Constants included from BackoffReduction
BackoffReduction::DEFAULT_BACKOFF_REDUCTION_FACTOR, BackoffReduction::MIN_STRIDE_SIZE
Instance Attribute Summary collapse
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#healthy_range ⇒ Object
Returns the value of attribute healthy_range.
-
#initial_timeout_seconds ⇒ Object
readonly
Returns the value of attribute initial_timeout_seconds.
-
#max_timeout_seconds ⇒ Object
readonly
Returns the value of attribute max_timeout_seconds.
-
#stride ⇒ Object
Returns the value of attribute stride.
-
#timeout_seconds ⇒ Object
Returns the value of attribute timeout_seconds.
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize(options = {}) ⇒ ThreadsRunning
constructor
A new instance of ThreadsRunning.
- #threads_running ⇒ Object
- #throttle_seconds ⇒ Object
Methods included from BackoffReduction
Methods included from Command
Constructor Details
#initialize(options = {}) ⇒ ThreadsRunning
Returns a new instance of ThreadsRunning.
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/lhm/throttler/threads_running.rb', line 14 def initialize( = {}) @initial_timeout_seconds = [:initial_timeout] || DEFAULT_INITIAL_TIMEOUT @stride = [:stride] || DEFAULT_STRIDE @max_timeout_seconds = [:max_timeout] || (@initial_timeout_seconds * 1024) @timeout_seconds = @initial_timeout_seconds @healthy_range = [:healthy_range] || DEFAULT_HEALTHY_RANGE @connection = [:connection] super end |
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
11 12 13 |
# File 'lib/lhm/throttler/threads_running.rb', line 11 def connection @connection end |
#healthy_range ⇒ Object
Returns the value of attribute healthy_range.
11 12 13 |
# File 'lib/lhm/throttler/threads_running.rb', line 11 def healthy_range @healthy_range end |
#initial_timeout_seconds ⇒ Object (readonly)
Returns the value of attribute initial_timeout_seconds.
12 13 14 |
# File 'lib/lhm/throttler/threads_running.rb', line 12 def initial_timeout_seconds @initial_timeout_seconds end |
#max_timeout_seconds ⇒ Object (readonly)
Returns the value of attribute max_timeout_seconds.
12 13 14 |
# File 'lib/lhm/throttler/threads_running.rb', line 12 def max_timeout_seconds @max_timeout_seconds end |
#stride ⇒ Object
Returns the value of attribute stride.
11 12 13 |
# File 'lib/lhm/throttler/threads_running.rb', line 11 def stride @stride end |
#timeout_seconds ⇒ Object
Returns the value of attribute timeout_seconds.
11 12 13 |
# File 'lib/lhm/throttler/threads_running.rb', line 11 def timeout_seconds @timeout_seconds end |
Instance Method Details
#execute ⇒ Object
53 54 55 |
# File 'lib/lhm/throttler/threads_running.rb', line 53 def execute sleep throttle_seconds end |
#threads_running ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/lhm/throttler/threads_running.rb', line 25 def threads_running query = <<~SQL.squish SELECT COUNT(*) as Threads_running FROM ( SELECT 1 FROM performance_schema.threads WHERE NAME='thread/sql/one_connection' AND PROCESSLIST_STATE IS NOT NULL LIMIT #{@healthy_range.max + 1} ) AS LIM SQL @connection.select_value(query) end |
#throttle_seconds ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/lhm/throttler/threads_running.rb', line 39 def throttle_seconds current_threads_running = threads_running if !healthy_range.cover?(current_threads_running) && @timeout_seconds < @max_timeout_seconds Lhm.logger.info("Increasing timeout between strides from #{@timeout_seconds} to #{@timeout_seconds * 2} because threads running is greater than the maximum of #{@healthy_range.max} allowed.") @timeout_seconds = @timeout_seconds * 2 elsif healthy_range.cover?(current_threads_running) && @timeout_seconds > @initial_timeout_seconds Lhm.logger.info("Decreasing timeout between strides from #{@timeout_seconds} to #{@timeout_seconds / 2} because threads running is less than the maximum of #{@healthy_range.max} allowed.") @timeout_seconds = @timeout_seconds / 2 else @timeout_seconds end end |