I have configured Rails to run with Multi Master Manager for Mysql (http://mysql-mmm.org) We also have a load balancer to distribute the DB request, so here is the setup:
rails
|
load balancer (LB)
/ | \
db1 | db2
\ | /
MMM
If one of the db is down, everything is fine, MMM will tell LB to redirect traffic to the other db. However when we restart the db that was down and MMM reconfigure the LB to start sending traffic to that db, Rails got stuck when checking if the connection is active. Specifically when calling @connection.stat
in MysqlAdapter#active?
(it just hang forever).
class MysqlAdapter
def active?
if @connection.respond_to?(:stat)
@connection.stat
else
@connection.query 'select 1'
end
# mysql-ruby doesn't raise an exception when stat fails.
if @connection.respond_to?(:errno)
@connection.errno.zero?
else
true
end
rescue Mysql::Error
false
end
end
I have tried enclosing that code with Timeout::timeout
and SystemTimer.timeout_after
, neither of them works. And using @connection.query 'select 1'
instead of @connection.stat
doesn't help either.
If I just call reconnect! everytime it works, but that's probably a really bad idea. It seems that this is the problem with mysql gem? (ie. trying to call stat or anything else when the connection has gone, just hang) Any idea how to fix this problem?