Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
add configurable timeout for management mysql connection
Browse files Browse the repository at this point in the history
Change-Id: Ie5134224b219e06b5780b949505b8c23036856f7
  • Loading branch information
Andrew Liu committed May 8, 2012
1 parent 6f4ce71 commit 42e1558
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions mysql/bin/mysql_node
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class VCAP::Services::Mysql::NodeBin < VCAP::Services::Base::NodeBin
options[:mysql] = parse_property(config, "mysql", Hash)
options[:socket] = parse_property(config, "socket", String, :optional => true)
options[:connection_pool_size] = parse_property(config, "connection_pool_size", Integer, :optional => true)
options[:connection_wait_timeout] = parse_property(config, "connection_wait_timeout", Integer, :optional => true)
options
end

Expand Down
2 changes: 2 additions & 0 deletions mysql/config/mysql_node.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ mysql:
user: root
pass: root
connection_pool_size: 5
# connection timeout for all management mysql connection
connection_wait_timeout: 10

# z_interval: 30
# max_nats_payload: 1048576
Expand Down
18 changes: 18 additions & 0 deletions mysql/lib/mysql_service/mysql2_timeout.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright (c) 2009-2012 VMware, Inc.
require "mysql2"

module Mysql2
class Client
class << self
attr_accessor :default_timeout
end

alias :origin_initialize :initialize

def initialize(opts={})
client = origin_initialize(opts)
wait_timeout = self.class.default_timeout
client.query("set @@wait_timeout=#{wait_timeout}") if wait_timeout
end
end
end
8 changes: 7 additions & 1 deletion mysql/lib/mysql_service/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Node < VCAP::Services::Base::Node
end

require "mysql_service/common"
require "mysql_service/mysql2_timeout"
require "mysql_service/util"
require "mysql_service/storage_quota"
require "mysql_service/mysql_error"
Expand Down Expand Up @@ -64,11 +65,16 @@ def initialize(options)
@statistics_lock = Mutex.new
@provision_served = 0
@binding_served = 0

@connection_wait_timeout = options[:connection_wait_timeout]
Mysql2::Client.default_timeout = @connection_wait_timeout
end

def pre_send_announcement
@pool = mysql_connect
EM.add_periodic_timer(KEEP_ALIVE_INTERVAL) {mysql_keep_alive}
keep_alive_interval = KEEP_ALIVE_INTERVAL
keep_alive_interval = [keep_alive_interval, @connection_wait_timeout.to_f/2].min if @connection_wait_timeout
EM.add_periodic_timer(keep_alive_interval) {mysql_keep_alive}
EM.add_periodic_timer(@max_long_query.to_f/2) {kill_long_queries} if @max_long_query > 0
if (@max_long_tx > 0) and (check_innodb_plugin)
EM.add_periodic_timer(@max_long_tx.to_f/2) {kill_long_transaction}
Expand Down
26 changes: 25 additions & 1 deletion mysql/spec/mysql_node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,31 @@ class MysqlError
end
end

after:each do
it "should add timeout option to all management mysql connection" do
EM.run do
opts = @opts.dup
origin_timeout = Mysql2::Client.default_timeout
timeout = 1
opts[:connection_wait_timeout] = timeout
node = VCAP::Services::Mysql::Node.new(opts)

EM.add_timer(2) do
begin
node.pool.with_connection do |conn|
# simulate connection idle
sleep (timeout * 5)
expect{ conn.query("select 1") }.should raise_error(Mysql2::Error, /MySQL server has gone away/)
end
ensure
# restore original timeout
Mysql2::Client.default_timeout = origin_timeout
EM.stop
end
end
end
end

after :each do
@test_dbs.keys.each do |db|
begin
name = db["name"]
Expand Down
1 change: 1 addition & 0 deletions mysql/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def getNodeTestConfig()
:ip_route => parse_property(config, "ip_route", String, :optional => true),
:max_long_tx => parse_property(config, "max_long_tx", Integer),
:max_user_conns => parse_property(config, "max_user_conns", Integer, :optional => true),
:connection_wait_timeout => 10,
}
options
end
Expand Down

0 comments on commit 42e1558

Please sign in to comment.