Skip to content

Commit

Permalink
Update RTT warning to use multiple samples, #4851
Browse files Browse the repository at this point in the history
TIL about Array#fill
  • Loading branch information
mperham committed Mar 30, 2021
1 parent bedbd7f commit 5b94bfe
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/sidekiq/launcher.rb
Expand Up @@ -188,6 +188,10 @@ def ❤
end
end

# We run the heartbeat every five seconds.
# Capture five samples of RTT, log a warning if each sample
# is above our warning threshold.
RTT_READINGS = RingBuffer.new(5)
RTT_WARNING_LEVEL = 50_000

def check_rtt
Expand All @@ -198,15 +202,17 @@ def check_rtt
b = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC, :microsecond)
end
rtt = b - a
RTT_READINGS << rtt
# Ideal RTT for Redis is < 1000µs
# Workable is < 10,000µs
# Log a warning if it's a disaster.
if rtt > RTT_WARNING_LEVEL
Sidekiq.logger.warn <<-EOM
if RTT_READINGS.all? { |x| x > RTT_WARNING_LEVEL }
Sidekiq.logger.warn <<~EOM
Your Redis network connection is performing extremely poorly.
Current RTT is #{rtt} µs, ideally this should be < 1000.
Last RTT readings were #{RTT_READINGS.buffer.inspect}, ideally these should be < 1000.
Ensure Redis is running in the same AZ or datacenter as Sidekiq.
EOM
RTT_READINGS.reset
end
rtt
end
Expand Down
28 changes: 28 additions & 0 deletions lib/sidekiq/util.rb
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require "forwardable"
require "socket"
require "securerandom"
require "sidekiq/exception_handler"
Expand All @@ -8,6 +9,33 @@ module Sidekiq
##
# This module is part of Sidekiq core and not intended for extensions.
#

class RingBuffer
include Enumerable
extend Forwardable
def_delegators :@buf, :[], :each, :size

def initialize(size, default=0)
@size = size
@buf = Array.new(size, default)
@index = 0
end

def <<(element)
@buf[@index % @size] = element
@index += 1
element
end

def buffer
@buf
end

def reset(default=0)
@buf.fill(default)
end
end

module Util
include ExceptionHandler

Expand Down

0 comments on commit 5b94bfe

Please sign in to comment.