Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ZeroDivisionError when one node and its weight is 0 #2989

Merged
merged 1 commit into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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