Class: Lhm::Throttler::ThreadsRunning

Inherits:
Object
  • Object
show all
Includes:
Command
Defined in:
lib/lhm/throttler/threads_running.rb

Constant Summary collapse

DEFAULT_INITIAL_TIMEOUT =
0.1
DEFAULT_HEALTHY_RANGE =
(0..50)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Command

#run

Constructor Details

#initialize(options = {}) ⇒ ThreadsRunning

Returns a new instance of ThreadsRunning.



12
13
14
15
16
17
18
# File 'lib/lhm/throttler/threads_running.rb', line 12

def initialize(options = {})
  @initial_timeout_seconds = options[:initial_timeout] || DEFAULT_INITIAL_TIMEOUT
  @max_timeout_seconds = options[:max_timeout] || (@initial_timeout_seconds * 1024)
  @timeout_seconds = @initial_timeout_seconds
  @healthy_range = options[:healthy_range] || DEFAULT_HEALTHY_RANGE
  @connection = options[:connection]
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



9
10
11
# File 'lib/lhm/throttler/threads_running.rb', line 9

def connection
  @connection
end

#healthy_rangeObject

Returns the value of attribute healthy_range.



9
10
11
# File 'lib/lhm/throttler/threads_running.rb', line 9

def healthy_range
  @healthy_range
end

#initial_timeout_secondsObject (readonly)

Returns the value of attribute initial_timeout_seconds.



10
11
12
# File 'lib/lhm/throttler/threads_running.rb', line 10

def initial_timeout_seconds
  @initial_timeout_seconds
end

#max_timeout_secondsObject (readonly)

Returns the value of attribute max_timeout_seconds.



10
11
12
# File 'lib/lhm/throttler/threads_running.rb', line 10

def max_timeout_seconds
  @max_timeout_seconds
end

#timeout_secondsObject

Returns the value of attribute timeout_seconds.



9
10
11
# File 'lib/lhm/throttler/threads_running.rb', line 9

def timeout_seconds
  @timeout_seconds
end

Instance Method Details

#executeObject



48
49
50
# File 'lib/lhm/throttler/threads_running.rb', line 48

def execute
  sleep throttle_seconds
end

#threads_runningObject



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lhm/throttler/threads_running.rb', line 20

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_secondsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/lhm/throttler/threads_running.rb', line 34

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