From c1cf4444b2ca9126e8450ae3224cd406f0e43624 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 14 Dec 2011 17:37:23 -0800 Subject: add configurable :fail_timeout for backends This controls the retry timeout of a failed backend --- lib/mogilefs/backend.rb | 7 +++---- lib/mogilefs/client.rb | 5 ++++- test/test_backend.rb | 8 ++++++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/mogilefs/backend.rb b/lib/mogilefs/backend.rb index f33c42d..a45cbed 100644 --- a/lib/mogilefs/backend.rb +++ b/lib/mogilefs/backend.rb @@ -70,6 +70,7 @@ class MogileFS::Backend def initialize(args) @hosts = args[:hosts] + @fail_timeout = args[:fail_timeout] || 5 raise ArgumentError, "must specify at least one host" unless @hosts raise ArgumentError, "must specify at least one host" if @hosts.empty? unless @hosts == @hosts.select { |h| h =~ /:\d+$/ } then @@ -361,17 +362,15 @@ class MogileFS::Backend def socket return @socket if @socket and not @socket.closed? - now = Time.now - @hosts.shuffle.each do |host| - next if @dead.include?(host) and @dead[host][0] > now - 5 + next if dead = @dead[host] and dead[0] > (Time.now - @fail_timeout) begin addr, port = host.split(/:/) @socket = MogileFS::Socket.tcp(addr, port, @timeout) @active_host = host rescue SystemCallError, MogileFS::Timeout => err - @dead[host] = [ now, err ] + @dead[host] = [ Time.now, err ] next end diff --git a/lib/mogilefs/client.rb b/lib/mogilefs/client.rb index 696f31f..7a808f1 100644 --- a/lib/mogilefs/client.rb +++ b/lib/mogilefs/client.rb @@ -26,6 +26,7 @@ class MogileFS::Client @hosts = args[:hosts] @readonly = args[:readonly] ? true : false @timeout = args[:timeout] + @fail_timeout = args[:fail_timeout] reload end @@ -34,7 +35,9 @@ class MogileFS::Client # Creates a new MogileFS::Backend. def reload - @backend = MogileFS::Backend.new :hosts => @hosts, :timeout => @timeout + @backend = MogileFS::Backend.new(:hosts => @hosts, + :timeout => @timeout, + :fail_timeout => @fail_timeout) end ## diff --git a/test/test_backend.rb b/test/test_backend.rb index cc8d3a7..aced57c 100644 --- a/test/test_backend.rb +++ b/test/test_backend.rb @@ -229,5 +229,13 @@ class TestBackend < Test::Unit::TestCase assert_equal expected, actual end + def test_fail_timeout + o = { :domain => "none", :hosts => %w(0:666 0:6 0:66) } + c = MogileFS::MogileFS.new(o) + assert_equal 5, c.backend.instance_variable_get(:@fail_timeout) + o[:fail_timeout] = 0.666 + c = MogileFS::MogileFS.new(o) + assert_equal 0.666, c.backend.instance_variable_get(:@fail_timeout) + end end -- cgit v1.2.3-24-ge0c7