Skip to content

Commit

Permalink
Merge pull request #2989 from ganmacs/fix-for-zero-weight-server
Browse files Browse the repository at this point in the history
Fix ZeroDivisionError when one node and its weight is 0
  • Loading branch information
ganmacs authored May 12, 2020
2 parents 46bb832 + b8f3ee7 commit 46c368a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/fluent/plugin/out_forward/load_balancer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def select_healthy_node
end

def rebuild_weight_array(nodes)
standby_nodes, regular_nodes = nodes.partition {|n|
standby_nodes, regular_nodes = nodes.select { |e| e.weight > 0 }.partition {|n|
n.standby?
}

Expand Down
46 changes: 46 additions & 0 deletions test/plugin/out_forward/test_load_balancer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,58 @@ class LoadBalancerTest < Test::Unit::TestCase
end
end

test 'call like round robin without weight=0 node' do
lb = Fluent::Plugin::ForwardOutput::LoadBalancer.new($log)
n1 = flexmock('node', :'standby?' => false, :'available?' => true, weight: 1)
n2 = flexmock('node', :'standby?' => false, :'available?' => true, weight: 1)
n3 = flexmock('node', :'standby?' => false, :'available?' => true, weight: 0)

lb.rebuild_weight_array([n1, n2, n3])

lb.select_healthy_node do |node|
# to handle random choice
if node == n1
lb.select_healthy_node do |node|
assert_equal(node, n2)
end

lb.select_healthy_node do |node|
assert_equal(node, n1)
end

lb.select_healthy_node do |node|
assert_equal(node, n2)
end
else
lb.select_healthy_node do |node|
assert_equal(node, n1)
end

lb.select_healthy_node do |node|
assert_equal(node, n2)
end

lb.select_healthy_node do |node|
assert_equal(node, n1)
end
end
end
end

test 'raise an error if all node are unavialble' do
lb = Fluent::Plugin::ForwardOutput::LoadBalancer.new($log)
lb.rebuild_weight_array([flexmock('node', :'standby?' => false, :'available?' => false, weight: 1)])
assert_raise(Fluent::Plugin::ForwardOutput::NoNodesAvailable) do
lb.select_healthy_node
end
end

test 'it regards weight=0 node as unavialble' do
lb = Fluent::Plugin::ForwardOutput::LoadBalancer.new($log)
lb.rebuild_weight_array([flexmock('node', :'standby?' => false, :'available?' => true, weight: 0)])
assert_raise(Fluent::Plugin::ForwardOutput::NoNodesAvailable) do
lb.select_healthy_node
end
end
end
end

0 comments on commit 46c368a

Please sign in to comment.