Class: BigBrother::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/big_brother/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Node

Returns a new instance of Node.



7
8
9
10
11
12
13
# File 'lib/big_brother/node.rb', line 7

def initialize(attributes={})
  @address = attributes[:address]
  @port = attributes[:port]
  @path = attributes[:path]
  @weight = attributes[:weight]
  @start_time = attributes.fetch(:start_time, Time.now.to_i)
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



5
6
7
# File 'lib/big_brother/node.rb', line 5

def address
  @address
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/big_brother/node.rb', line 5

def path
  @path
end

#portObject (readonly)

Returns the value of attribute port.



5
6
7
# File 'lib/big_brother/node.rb', line 5

def port
  @port
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



5
6
7
# File 'lib/big_brother/node.rb', line 5

def start_time
  @start_time
end

#weightObject (readonly)

Returns the value of attribute weight.



5
6
7
# File 'lib/big_brother/node.rb', line 5

def weight
  @weight
end

Instance Method Details

#==(other) ⇒ Object



39
40
41
# File 'lib/big_brother/node.rb', line 39

def ==(other)
  address == other.address && port == other.port
end

#_determine_weight(cluster) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/big_brother/node.rb', line 43

def _determine_weight(cluster)
  if cluster.up_file_exists?
    100
  elsif cluster.down_file_exists?
    0
  else
    _weight_health(BigBrother::HealthFetcher.current_health(@address, @port, @path), cluster.ramp_up_time)
  end
end

#_weight_health(health, ramp_up_time) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/big_brother/node.rb', line 53

def _weight_health(health, ramp_up_time)
  current_age = age
  if current_age < ramp_up_time
    (health * (current_age / ramp_up_time.to_f)).to_i
  else
    health
  end
end

#ageObject



15
16
17
# File 'lib/big_brother/node.rb', line 15

def age
  Time.now.to_i - @start_time
end

#incorporate_state(another_node) ⇒ Object



19
20
21
22
23
24
# File 'lib/big_brother/node.rb', line 19

def incorporate_state(another_node)
  if another_node
    @weight = another_node.weight
    @start_time = another_node.start_time
  end
end

#invalidate_weight!Object



26
27
28
# File 'lib/big_brother/node.rb', line 26

def invalidate_weight!
  @weight = nil
end

#monitor(cluster) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/big_brother/node.rb', line 30

def monitor(cluster)
  new_weight = _determine_weight(cluster)
  return unless cluster.monitored?
  if new_weight != @weight
    BigBrother.ipvs.edit_node(cluster.fwmark, address, new_weight)
    @weight = new_weight
  end
end