Class: Sidekiq::Routing::Auto::NoisyNeighborDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/routing/auto/noisy_neighbor_detector.rb

Constant Summary collapse

MIN_WORKLOAD_SECONDS =
10

Instance Method Summary collapse

Constructor Details

#initialize(queue_name) ⇒ NoisyNeighborDetector

Returns a new instance of NoisyNeighborDetector.



8
9
10
11
12
# File 'lib/sidekiq/routing/auto/noisy_neighbor_detector.rb', line 8

def initialize(queue_name)
  @queue_name = queue_name
  @queue = Sidekiq::Queue.new(queue_name)
  @duration_cache = {}
end

Instance Method Details

#calculate_workload_by_classObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sidekiq/routing/auto/noisy_neighbor_detector.rb', line 26

def calculate_workload_by_class
  result = Hash.new(0.0)

  @queue.each do |job|
    duration_ms = get_duration_for_class(job.klass)
    next unless duration_ms

    result[job.klass] += duration_ms / 1000.0
  end

  result
end

#identify_noisy_neighborsObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/sidekiq/routing/auto/noisy_neighbor_detector.rb', line 14

def identify_noisy_neighbors
  workload = calculate_workload_by_class
  total_workload = workload.values.sum
  return [] if total_workload < MIN_WORKLOAD_SECONDS

  threshold = Routing::Auto.configuration.noisy_neighbor_threshold_percent

  workload.select do |_job_class, workload_seconds|
    ((workload_seconds / total_workload.to_f) * 100) > threshold
  end.keys
end

#total_estimated_workloadObject



39
40
41
# File 'lib/sidekiq/routing/auto/noisy_neighbor_detector.rb', line 39

def total_estimated_workload
  calculate_workload_by_class.values.sum
end