Class: Autoscaler::LinearScalingStrategy

Inherits:
Object
  • Object
show all
Defined in:
lib/autoscaler/linear_scaling_strategy.rb

Overview

Strategies determine the target number of workers This strategy sets the number of workers to be proportional to the number of enqueued jobs.

Instance Method Summary collapse

Constructor Details

#initialize(max_workers = 1, worker_capacity = 25, min_factor = 0) ⇒ LinearScalingStrategy

Returns a new instance of LinearScalingStrategy.

Parameters:

  • max_workers (integer) (defaults to: 1)

    maximum number of workers to spin up.

  • worker_capacity (integer) (defaults to: 25)

    the amount of jobs one worker can handle

  • min_factor (float) (defaults to: 0)

    minimum work required to scale, as percentage of worker_capacity



8
9
10
11
12
13
# File 'lib/autoscaler/linear_scaling_strategy.rb', line 8

def initialize(max_workers = 1, worker_capacity = 25, min_factor = 0)
  @max_workers             = max_workers # max # of workers we can scale to
  @total_capacity          = (@max_workers * worker_capacity).to_f # total capacity of max workers
  min_capacity             = [0, min_factor].max.to_f * worker_capacity # min capacity required to scale first worker
  @min_capacity_percentage = min_capacity / @total_capacity # min percentage of total capacity
end

Instance Method Details

#call(system, event_idle_time) ⇒ Integer

Returns target number of workers.

Parameters:

  • system (QueueSystem)

    interface to the queuing system

  • event_idle_time (Numeric)

    number of seconds since a job related event

Returns:

  • (Integer)

    target number of workers



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/autoscaler/linear_scaling_strategy.rb', line 18

def call(system, event_idle_time)
  requested_capacity_percentage = total_work(system) / @total_capacity

  # Scale requested capacity taking into account the minimum required
  scale_factor = (requested_capacity_percentage - @min_capacity_percentage) / (@total_capacity - @min_capacity_percentage)
  scale_factor = 0 if scale_factor.nan? # Handle DIVZERO

  scaled_capacity_percentage = scale_factor * @total_capacity

  ideal_workers = ([0, scaled_capacity_percentage].max * @max_workers).ceil
  min_workers   = [system.workers, ideal_workers].max  # Don't scale down past number of currently engaged workers
  max_workers   = [min_workers,  @max_workers].min     # Don't scale up past number of max workers

  return [min_workers, max_workers].min
end