Class: Autoscaler::LinearScalingStrategy
- Inherits:
-
Object
- Object
- Autoscaler::LinearScalingStrategy
- 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
-
#call(system, event_idle_time) ⇒ Integer
Target number of workers.
-
#initialize(max_workers = 1, worker_capacity = 25, min_factor = 0) ⇒ LinearScalingStrategy
constructor
A new instance of LinearScalingStrategy.
Constructor Details
#initialize(max_workers = 1, worker_capacity = 25, min_factor = 0) ⇒ LinearScalingStrategy
Returns a new instance of LinearScalingStrategy.
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.
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 |