Class: Lhm::Throttler::ThreadsRunning

Inherits:
Object
  • Object
show all
Includes:
Command
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)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Command

#run

Constructor Details

#initialize(options = {}) ⇒ ThreadsRunning

Returns a new instance of ThreadsRunning.



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

def initialize(options = {})
  @initial_timeout_seconds = options[:initial_timeout] || DEFAULT_INITIAL_TIMEOUT
  @stride = options[:stride] || DEFAULT_STRIDE
  @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.



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

def connection
  @connection
end

#healthy_rangeObject

Returns the value of attribute healthy_range.



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

def healthy_range
  @healthy_range
end

#initial_timeout_secondsObject (readonly)

Returns the value of attribute initial_timeout_seconds.



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

def initial_timeout_seconds
  @initial_timeout_seconds
end

#max_timeout_secondsObject (readonly)

Returns the value of attribute max_timeout_seconds.



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

def max_timeout_seconds
  @max_timeout_seconds
end

#strideObject

Returns the value of attribute stride.



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

def stride
  @stride
end

#timeout_secondsObject

Returns the value of attribute timeout_seconds.



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

def timeout_seconds
  @timeout_seconds
end

Instance Method Details

#executeObject



50
51
52
# File 'lib/lhm/throttler/threads_running.rb', line 50

def execute
  sleep throttle_seconds
end

#threads_runningObject



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

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



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

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