Skip to content

Commit

Permalink
Fix bug for ZeroDivisionError
Browse files Browse the repository at this point in the history
it can happen when it has one node whose weight is 0.

https://github.com/fluent/fluentd/blob/2502cb8b57ece25e7bf83aa29d4ded48acab63b3/lib/fluent/plugin/out_forward/load_balancer.rb#L93

Signed-off-by: Yuta Iwama <ganmacs@gmail.com>
  • Loading branch information
ganmacs committed May 12, 2020
1 parent 55d11dd commit b8f3ee7
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 b8f3ee7

Please sign in to comment.