about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2011-12-14 17:37:23 -0800
committerEric Wong <normalperson@yhbt.net>2011-12-14 17:44:23 -0800
commitc1cf4444b2ca9126e8450ae3224cd406f0e43624 (patch)
treedcfda617ac8db129eb3bc1e979d1412f470d61af
parent0b933fc83b8b519c8e587caa6606dd372dda18af (diff)
downloadmogilefs-client-c1cf4444b2ca9126e8450ae3224cd406f0e43624.tar.gz
This controls the retry timeout of a failed backend
-rw-r--r--lib/mogilefs/backend.rb7
-rw-r--r--lib/mogilefs/client.rb5
-rw-r--r--test/test_backend.rb8
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