Class: Lhm::Throttler::ThreadsRunning

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

Instance Method Summary collapse

Methods included from BackoffReduction

#backoff_stride

Methods included from Command

#run

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(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]

  super
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



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

def connection
  @connection
end

#healthy_rangeObject

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_secondsObject (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_secondsObject (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

#strideObject

Returns the value of attribute stride.



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

def stride
  @stride
end

#timeout_secondsObject

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

#executeObject



53
54
55
# File 'lib/lhm/throttler/threads_running.rb', line 53

def execute
  sleep throttle_seconds
end

#threads_runningObject



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_secondsObject



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