Skip to content

Commit

Permalink
address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ethowitz committed Jan 23, 2024
1 parent af54ea7 commit 8a07b83
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 27 deletions.
4 changes: 1 addition & 3 deletions lib/readyset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,7 @@ class << self

def self.healthchecker
@healthchecker ||= Readyset::Health::Healthchecker.new(
healthcheck_interval: config.failover_healthcheck_interval,
error_window_period: config.failover_error_window_period,
error_window_size: config.failover_error_window_size,
config.failover,
shard: shard,
)
end
Expand Down
20 changes: 14 additions & 6 deletions lib/readyset/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@

module Readyset
class Configuration
attr_accessor :enable_failover, :failover_error_window_period, :failover_error_window_size,
:failover_healthcheck_interval, :migration_path, :shard
attr_accessor :migration_path, :shard

def initialize
@enable_failover = false
@failover_healthcheck_interval = 5.seconds
@failover_error_window_period = 1.minute
@failover_error_window_size = 10
@migration_path = File.join(Rails.root, 'db/readyset_caches.rb')
@shard = :readyset
end

def failover
if @failover
@failover
else
inner = ActiveSupport::OrderedOptions.new
inner.enabled = false
inner.healthcheck_interval = 5.seconds
inner.error_window_period = 1.minute
inner.error_window_size = 10
@failover = inner
end
end

def hostname
ActiveRecord::Base.configurations.configs_for(name: shard.to_s).configuration_hash[:host]
end
Expand Down
10 changes: 6 additions & 4 deletions lib/readyset/health/healthchecker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ module Health
class Healthchecker
UNHEALTHY_ERRORS = [::PG::UnableToSend, ::PG::ConnectionBad].freeze

def initialize(healthcheck_interval:, error_window_period:, error_window_size:, shard:)
def initialize(config, shard:)
@healthy = Concurrent::AtomicBoolean.new(true)
@healthcheck_interval = healthcheck_interval
@healthcheck_interval = config.healthcheck_interval!
@healthchecks = Health::Healthchecks.new(shard: shard)
@lock = Mutex.new
@shard = shard
@window_counter = Readyset::Utils::WindowCounter.
new(window_size: error_window_size, time_period: error_window_period)
@window_counter = Readyset::Utils::WindowCounter.new(
window_size: config.error_window_size!,
time_period: config.error_window_period!,
)
end

# Returns true only if the connection to ReadySet is healthy. ReadySet's health is gauged by
Expand Down
16 changes: 8 additions & 8 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@
expect(config.migration_path).to eq(expected)
end

it 'initializes enable_failover with false' do
it 'initializes failover.enabled with false' do
config = Readyset::Configuration.new
expect(config.enable_failover).to eq(false)
expect(config.failover.enabled).to eq(false)
end

it 'initializes failover_healthcheck_interval to be 5 seconds' do
it 'initializes failover.healthcheck_interval to be 5 seconds' do
config = Readyset::Configuration.new
expect(config.failover_healthcheck_interval).to eq(5.seconds)
expect(config.failover.healthcheck_interval).to eq(5.seconds)
end

it 'initializes failover_error_window_period to be 1 minute' do
it 'initializes failover.error_window_period to be 1 minute' do
config = Readyset::Configuration.new
expect(config.failover_error_window_period).to eq(1.minute)
expect(config.failover.error_window_period).to eq(1.minute)
end

it 'initializes failover_error_window_size to be 10' do
it 'initializes failover.error_window_size to be 10' do
config = Readyset::Configuration.new
expect(config.failover_error_window_size).to eq(10)
expect(config.failover.error_window_size).to eq(10)
end
end
end
12 changes: 6 additions & 6 deletions spec/health/healthchecker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ def setup
private

def build_healthchecker(healthcheck_interval: 5.seconds)
Readyset::Health::Healthchecker.new(
healthcheck_interval: healthcheck_interval,
error_window_period: error_window_period,
error_window_size: error_window_size,
shard: :readyset
)
config = ActiveSupport::OrderedOptions.new
config.healthcheck_interval = healthcheck_interval
config.error_window_period = error_window_period
config.error_window_size = error_window_size

Readyset::Health::Healthchecker.new(config, shard: :readyset)
end

def error_window_period
Expand Down

0 comments on commit 8a07b83

Please sign in to comment.