Class: HawatelTlb::Mode::DynamicRatio

Inherits:
Ratio
  • Object
show all
Defined in:
lib/hawatel_tlb/mode/dynamicratio.rb

Overview

Dynamic Ratio algorithm

Thease are dynamic load balancing algorithm based on dynamic ratio weights. Algorithm explanation:

  1. Weight for each node is setting up by following formula: 100 - (respond_time / sum_respond_time) * 100 Weights are refreshing once per minute

  2. divide the amount of traffic (requests) sent to each server by weight (configured in the Client)

  3. Sort by ratio (result from 1. point) from smallest to largest

  4. Get node with smallest ratio

If you want to see how it works you can set client.mode.debug to 1 and run dynamicratio_spec.rb.

Constant Summary collapse

RECALC_WEIGHT_INTERVAL =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Ratio

#node, #refresh

Constructor Details

#initialize(group) ⇒ DynamicRatio

Returns a new instance of DynamicRatio.



21
22
23
24
25
26
27
28
# File 'lib/hawatel_tlb/mode/dynamicratio.rb', line 21

def initialize(group)
  super(group)

  @recalc_interval = RECALC_WEIGHT_INTERVAL
  @dynamic_weight = 1
  set_weights
  thread_start
end

Instance Attribute Details

#debugObject

0: enable, 1: disable



16
17
18
19
20
21
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
# File 'lib/hawatel_tlb/mode/dynamicratio.rb', line 16

class DynamicRatio < Ratio
  attr_accessor :recalc_interval

  RECALC_WEIGHT_INTERVAL = 10

  def initialize(group)
    super(group)

    @recalc_interval = RECALC_WEIGHT_INTERVAL
    @dynamic_weight = 1
    set_weights
    thread_start
  end

  # Destroy thread
  def destroy
    @dynamic_weight = 0
  end

  private

  def sum_respond_time
    sum = 0
    @group.each do |node|
      if node.status[:state] == 'online' && node.state == 'enable'
        sum += node.status[:respond_time]
      end
    end
    sum
  end

  def set_weights
    sum = sum_respond_time
    @group.each do |node|
      if node.status[:state] == 'online' && node.state == 'enable'
        node.weight = 100 - ((node.status[:respond_time] / sum) * 100).to_i if !sum.zero?
      end
    end
  end

  def thread_start
    @thr = Thread.new{
      while(@dynamic_weight == 1)
        sleep(RECALC_WEIGHT_INTERVAL)
        thread_heartbeat
        set_weights
      end
    }
  rescue => e
    puts "Exception: #{e.inspect}"
  end

  def thread_heartbeat
    if @debug > 0
      puts "#{Time.now} thread hearbeat #{Thread.current.object_id}"
    end
  end

end

#recalc_intervalObject

Returns the value of attribute recalc_interval.



17
18
19
# File 'lib/hawatel_tlb/mode/dynamicratio.rb', line 17

def recalc_interval
  @recalc_interval
end

Instance Method Details

#destroyObject

Destroy thread



31
32
33
# File 'lib/hawatel_tlb/mode/dynamicratio.rb', line 31

def destroy
  @dynamic_weight = 0
end