RUBY-367 deprecate async refresh
This commit is contained in:
parent
fa10508f07
commit
cf69bf4c2e
@ -44,19 +44,15 @@ be used for reads.
|
|||||||
Refresh mode is disabled by default.
|
Refresh mode is disabled by default.
|
||||||
|
|
||||||
However, if you expect to make live changes to your secondaries, and you want this to be reflected without
|
However, if you expect to make live changes to your secondaries, and you want this to be reflected without
|
||||||
having to manually restart your app server, then you should enable it. You can enable synchronously, which will
|
having to manually restart your app server, then you should enable it. You can enable this mode
|
||||||
refresh the replica set data in a synchronous fashion (which may ocassionally slow down your queries):
|
synchronously, which will refresh the replica set data in a synchronous fashion (which may
|
||||||
|
ocassionally slow down your queries):
|
||||||
|
|
||||||
@connection = ReplSetConnection.new(['n1.mydb.net', 27017], :refresh_mode => :sync)
|
@connection = ReplSetConnection.new(['n1.mydb.net', 27017], :refresh_mode => :sync)
|
||||||
|
|
||||||
If you want to refresh via a background thread, use the `:async` mode. NOTE: the background
|
|
||||||
version may be more effective on platforms that use native threads, such as JRuby:
|
|
||||||
|
|
||||||
@connection = ReplSetConnection.new(['n1.mydb.net', 27017], :refresh_mode => :async)
|
|
||||||
|
|
||||||
If you want to change the default refresh interval of 90 seconds, you can do so like this:
|
If you want to change the default refresh interval of 90 seconds, you can do so like this:
|
||||||
|
|
||||||
@connection = ReplSetConnection.new(['n1.mydb.net', 27017], :refresh_mode => :async,
|
@connection = ReplSetConnection.new(['n1.mydb.net', 27017], :refresh_mode => :sync,
|
||||||
:refresh_interval => 60)
|
:refresh_interval => 60)
|
||||||
|
|
||||||
Do not set this value to anything lower than 30, or you may start to experience performance issues.
|
Do not set this value to anything lower than 30, or you may start to experience performance issues.
|
||||||
|
@ -56,11 +56,10 @@ module Mongo
|
|||||||
# @option opts [Float] :connect_timeout (nil) The number of seconds to wait before timing out a
|
# @option opts [Float] :connect_timeout (nil) The number of seconds to wait before timing out a
|
||||||
# connection attempt.
|
# connection attempt.
|
||||||
# @option opts [Boolean] :ssl (false) If true, create the connection to the server using SSL.
|
# @option opts [Boolean] :ssl (false) If true, create the connection to the server using SSL.
|
||||||
# @option opts [Boolean] :refresh_mode (false) Set this to :async to enable a background thread that
|
# @option opts [Boolean] :refresh_mode (false) Set this to :sync to periodically update the
|
||||||
# periodically updates the state of the connection. If, for example, you initially connect while a secondary
|
# state of the connection every :refresh_interval seconds. Replica set connection failures
|
||||||
# is down, this will reconnect to that secondary behind the scenes to
|
# will always trigger a complete refresh. This option is useful when you want to add new nodes
|
||||||
# prevent you from having to reconnect manually. If set to :sync, refresh will happen
|
# or remove replica set nodes not currently in use by the driver.
|
||||||
# synchronously. If +false+, no automatic refresh will occur unless there's a connection failure.
|
|
||||||
# @option opts [Integer] :refresh_interval (90) If :refresh_mode is enabled, this is the number of seconds
|
# @option opts [Integer] :refresh_interval (90) If :refresh_mode is enabled, this is the number of seconds
|
||||||
# between calls to check the replica set's state.
|
# between calls to check the replica set's state.
|
||||||
# @option opts [Boolean] :require_primary (true) If true, require a primary node for the connection
|
# @option opts [Boolean] :require_primary (true) If true, require a primary node for the connection
|
||||||
@ -107,9 +106,12 @@ module Mongo
|
|||||||
@manager = nil
|
@manager = nil
|
||||||
@pool_mutex = Mutex.new
|
@pool_mutex = Mutex.new
|
||||||
|
|
||||||
if ![:sync, :async, false].include?(@refresh_mode)
|
if @refresh_mode == :async
|
||||||
|
warn ":async refresh mode has been deprecated. Refresh
|
||||||
|
mode will be disabled."
|
||||||
|
elsif ![:sync, false].include?(@refresh_mode)
|
||||||
raise MongoArgumentError,
|
raise MongoArgumentError,
|
||||||
"Refresh mode must be one of :sync, :async, or false."
|
"Refresh mode must be either :sync or false."
|
||||||
end
|
end
|
||||||
|
|
||||||
# Are we allowing reads from secondaries?
|
# Are we allowing reads from secondaries?
|
||||||
@ -124,15 +126,8 @@ module Mongo
|
|||||||
end
|
end
|
||||||
|
|
||||||
@connected = false
|
@connected = false
|
||||||
|
|
||||||
# Store the refresher thread
|
|
||||||
@refresh_thread = nil
|
|
||||||
@refresh_version = 0
|
@refresh_version = 0
|
||||||
|
|
||||||
# Maps
|
|
||||||
@threads_to_sockets = Hash.new { |h, k| h[k] = Hash.new }
|
|
||||||
@tag_map = nil
|
|
||||||
|
|
||||||
# Replica set name
|
# Replica set name
|
||||||
if opts[:rs_name]
|
if opts[:rs_name]
|
||||||
warn ":rs_name option has been deprecated and will be removed in v2.0. " +
|
warn ":rs_name option has been deprecated and will be removed in v2.0. " +
|
||||||
@ -161,7 +156,6 @@ module Mongo
|
|||||||
manager.connect
|
manager.connect
|
||||||
|
|
||||||
update_config(manager)
|
update_config(manager)
|
||||||
initiate_refresh_mode
|
|
||||||
|
|
||||||
if @require_primary && self.primary.nil? #TODO: in v2.0, we'll let this be optional and do a lazy connect.
|
if @require_primary && self.primary.nil? #TODO: in v2.0, we'll let this be optional and do a lazy connect.
|
||||||
close
|
close
|
||||||
@ -213,7 +207,6 @@ module Mongo
|
|||||||
old_manager = @manager
|
old_manager = @manager
|
||||||
update_config(background_manager)
|
update_config(background_manager)
|
||||||
old_manager.close(:soft => true)
|
old_manager.close(:soft => true)
|
||||||
initiate_refresh_mode
|
|
||||||
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
@ -265,7 +258,6 @@ module Mongo
|
|||||||
def close
|
def close
|
||||||
@connected = false
|
@connected = false
|
||||||
@manager.close(:soft => true) if @manager
|
@manager.close(:soft => true) if @manager
|
||||||
@threads_to_sockets.clear
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# If a ConnectionFailure is raised, this method will be called
|
# If a ConnectionFailure is raised, this method will be called
|
||||||
@ -470,6 +462,8 @@ module Mongo
|
|||||||
write_logging_startup_message
|
write_logging_startup_message
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@last_refresh = Time.now
|
||||||
|
|
||||||
should_connect = opts.fetch(:connect, true)
|
should_connect = opts.fetch(:connect, true)
|
||||||
connect if should_connect
|
connect if should_connect
|
||||||
end
|
end
|
||||||
@ -482,24 +476,6 @@ module Mongo
|
|||||||
@refresh_version += 1
|
@refresh_version += 1
|
||||||
end
|
end
|
||||||
|
|
||||||
# If we're using async refresh, start
|
|
||||||
# a background thread to run the refresh method
|
|
||||||
# every @refresh_interval seconds.
|
|
||||||
def initiate_refresh_mode
|
|
||||||
if @refresh_mode == :async
|
|
||||||
return if @refresh_thread && @refresh_thread.alive?
|
|
||||||
@refresh_thread = Thread.new do
|
|
||||||
while true do
|
|
||||||
sleep(@refresh_interval)
|
|
||||||
refresh
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@refresh_thread.priority = 1000
|
|
||||||
end
|
|
||||||
|
|
||||||
@last_refresh = Time.now
|
|
||||||
end
|
|
||||||
|
|
||||||
# Checkout a socket connected to a node with one of
|
# Checkout a socket connected to a node with one of
|
||||||
# the provided tags. If no such node exists, raise
|
# the provided tags. If no such node exists, raise
|
||||||
# an exception.
|
# an exception.
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
require File.join(File.dirname(__FILE__), '..', '..', '..', 'lib', 'mongo')
|
require File.join(File.dirname(__FILE__), '..', '..', '..', 'lib', 'mongo')
|
||||||
require 'logger'
|
require 'logger'
|
||||||
|
|
||||||
$con = Mongo::ReplSetConnection.new(['localhost', 30000], ['localhost', 30001], :read => :secondary, :refresh_mode => :async, :refresh_interval => 30)
|
$con = Mongo::ReplSetConnection.new(['localhost', 30000], ['localhost', 30001], :read => :secondary, :refresh_mode => :sync, :refresh_interval => 30)
|
||||||
$db = $con['foo']
|
$db = $con['foo']
|
||||||
|
|
||||||
class Load < Sinatra::Base
|
class Load < Sinatra::Base
|
||||||
|
Loading…
Reference in New Issue
Block a user