Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

routing: Add automatic failover #65

Merged
merged 5 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
ethan-readyset marked this conversation as resolved.
Show resolved Hide resolved
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')
ethan-readyset marked this conversation as resolved.
Show resolved Hide resolved
@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!
ethan-readyset marked this conversation as resolved.
Show resolved Hide resolved
@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
Loading