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

DEV: Tidy up method ordering and public/private status #17

Merged
merged 1 commit into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
42 changes: 22 additions & 20 deletions lib/rails_failover/active_record/handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,29 @@ def verify_primary(handler_key)

logger.warn "Failover for ActiveRecord has been initiated"

@thread = Thread.new do
loop do
initiate_fallback_to_primary
@thread = Thread.new { loop_until_all_up }
end
end

if all_primaries_up
logger.warn "Fallback to primary for ActiveRecord has been completed."
break
end
end
def primary_down?(handler_key)
primaries_down[handler_key]
end

def primaries_down_count
mon_synchronize do
primaries_down.count
end
end

private

def loop_until_all_up
loop do
initiate_fallback_to_primary

if all_primaries_up
logger.warn "Fallback to primary for ActiveRecord has been completed."
break
end
end
end
Expand Down Expand Up @@ -71,18 +85,6 @@ def initiate_fallback_to_primary
end
end

def primary_down?(handler_key)
primaries_down[handler_key]
end

def primaries_down_count
mon_synchronize do
primaries_down.count
end
end

private

def all_primaries_up
mon_synchronize do
primaries_down.empty?
Expand Down
126 changes: 61 additions & 65 deletions lib/rails_failover/redis/handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,58 @@ def initialize
def verify_primary(options)
mon_synchronize do
primary_down(options)
ensure_failover_thread_running
return if @thread&.alive?
logger&.warn "Failover for Redis has been initiated"
@thread = Thread.new { loop_until_all_up }
end
end

def ensure_failover_thread_running
return if @thread&.alive?
def register_client(client)
key = client.options[:id]

mon_synchronize do
clients[key] ||= []
clients[key] << client
end
end

logger&.warn "Failover for Redis has been initiated"
def deregister_client(client)
key = client.options[:id]

@thread = Thread.new do
loop do
ensure_primary_clients_disconnected
try_fallback_to_primary
mon_synchronize do
if clients[key]
clients[key].delete(client)

if all_primaries_up
logger&.warn "Fallback to primary for Redis has been completed."
break
if clients[key].empty?
clients.delete(key)
end
end
end
end

def ensure_primary_clients_disconnected
mon_synchronize { primaries_down.dup }.each do |key, options|
disconnect_clients(options, RailsFailover::Redis::PRIMARY)
def primary_down?(options)
mon_synchronize do
primaries_down[options[:id]]
end
end

def primaries_down_count
mon_synchronize do
primaries_down.count
end
end

private

def loop_until_all_up
loop do
ensure_primary_clients_disconnected
try_fallback_to_primary

if all_primaries_up
logger&.warn "Fallback to primary for Redis has been completed."
break
end
end
end

Expand Down Expand Up @@ -87,43 +114,6 @@ def try_fallback_to_primary
end
end

def register_client(client)
key = client.options[:id]

mon_synchronize do
clients[key] ||= []
clients[key] << client
end
end

def deregister_client(client)
key = client.options[:id]

mon_synchronize do
if clients[key]
clients[key].delete(client)

if clients[key].empty?
clients.delete(key)
end
end
end
end

def primary_down?(options)
mon_synchronize do
primaries_down[options[:id]]
end
end

def primaries_down_count
mon_synchronize do
primaries_down.count
end
end

private

def all_primaries_up
mon_synchronize { primaries_down.empty? }
end
Expand All @@ -144,39 +134,45 @@ def primary_down(options)
RailsFailover::Redis.on_failover_callback!(options[:id]) if !already_down
end

def clients
def primaries_down
process_pid = Process.pid
return @clients[process_pid] if @clients[process_pid]
return @primaries_down[process_pid] if @primaries_down[process_pid]

mon_synchronize do
if !@clients[process_pid]
@clients[process_pid] = {}
if !@primaries_down[process_pid]
@primaries_down[process_pid] = @primaries_down[@ancestor_pid] || {}

if process_pid != @ancestor_pid
@clients.delete(@ancestor_pid)
@primaries_down.delete(@ancestor_pid)&.each do |id, options|
verify_primary(options)
end
end
end

@clients[process_pid]
@primaries_down[process_pid]
end
end

def primaries_down
def clients
process_pid = Process.pid
return @primaries_down[process_pid] if @primaries_down[process_pid]
return @clients[process_pid] if @clients[process_pid]

mon_synchronize do
if !@primaries_down[process_pid]
@primaries_down[process_pid] = @primaries_down[@ancestor_pid] || {}
if !@clients[process_pid]
@clients[process_pid] = {}

if process_pid != @ancestor_pid
@primaries_down.delete(@ancestor_pid)&.each do |id, options|
verify_primary(options)
end
@clients.delete(@ancestor_pid)
end
end

@primaries_down[process_pid]
@clients[process_pid]
end
end

def ensure_primary_clients_disconnected
mon_synchronize { primaries_down.dup }.each do |key, options|
disconnect_clients(options, RailsFailover::Redis::PRIMARY)
end
end

Expand Down