Class: HawatelTlb::Mode::Weighted
- Inherits:
-
Object
- Object
- HawatelTlb::Mode::Weighted
- Defined in:
- lib/hawatel_tlb/mode/weighted.rb
Constant Summary collapse
- HISTORY_SIZE =
number of elements stored in history array
3
Instance Method Summary collapse
-
#initialize(group) ⇒ Weighted
constructor
A new instance of Weighted.
-
#node ⇒ Hash
Hostname/ip address and port number.
-
#refresh(group) ⇒ Object
Refresh group table after delete or add host.
Constructor Details
#initialize(group) ⇒ Weighted
Returns a new instance of Weighted.
7 8 9 10 11 |
# File 'lib/hawatel_tlb/mode/weighted.rb', line 7 def initialize(group) @group = group @history = Array.new @counter = 0 end |
Instance Method Details
#node ⇒ Hash
Returns hostname/ip address and port number.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/hawatel_tlb/mode/weighted.rb', line 22 def node # push nodes statistics to history array if @counter < HISTORY_SIZE @history[@counter] = @group.dup @counter += 1 else @counter = 0 @history[@counter] = @group.dup end # build array with only current_online = Array.new @group.each do |node| current_online.push(node.id) if node.state == 'enable' && node.status[:state] == 'online' end # there is no any available node return false if current_online.empty? # there is only one available node return find_node(current_online[0]) if current_online.size == 1 # there is more that one nodes - choose the most reliable node nodes_reliability = Array.new @history.each do |round| round.each do |node| if node.state == 'enable' && node.status[:state] == 'online' nodes_reliability[node.id] = Hash.new if nodes_reliability[node.id].nil? nodes_reliability[node.id][:online_count] = 0 if nodes_reliability[node.id][:online_count].nil? nodes_reliability[node.id][:online_count] += 1 end end end # choose only top nodes with equal online state top_nodes_by_state = Array.new top_value = 0 sort_by_state(nodes_reliability).each do |node| top_value = node[:online_count] if top_value == 0 if node[:online_count] == top_value node_param = @group.select {|item| item[:id] == node[:hostid]} top_nodes_by_state << node_param else break end end # there is only one node with highest online count return {:host => top_nodes_by_state[0][:host], port => top_nodes_by_state[0][:port]} if top_nodes_by_state.size == 1 # find node with highest weight nodes = sort_by_weight(top_nodes_by_state) return {:host => nodes[0][0].host, :port => nodes[0][0].port} end |
#refresh(group) ⇒ Object
Refresh group table after delete or add host
16 17 18 |
# File 'lib/hawatel_tlb/mode/weighted.rb', line 16 def refresh(group) @group = group end |