Class: BigBrother::Node

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

Constant Summary collapse

INITIAL_WEIGHT =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Node

Returns a new instance of Node.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/big_brother/node.rb', line 9

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)
  @priority = attributes.fetch(:priority, 0)
  @interpol = attributes.fetch(:interpol, false)
  @max_weight = attributes[:max_weight]
  @down_tick_count = 0
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

#down_tick_countObject

Returns the value of attribute down_tick_count.



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

def down_tick_count
  @down_tick_count
end

#max_weightObject (readonly)

Returns the value of attribute max_weight.



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

def max_weight
  @max_weight
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

#priorityObject (readonly)

Returns the value of attribute priority.



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

def priority
  @priority
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

Returns the value of attribute weight.



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

def weight
  @weight
end

Instance Method Details

#<=>(other) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/big_brother/node.rb', line 49

def <=>(other)
  return 1 if self.weight.to_i.zero?
  return -1 if other.weight.to_i.zero?
  comparison = self.priority <=> other.priority
  if comparison.zero?
   self.address <=> other.address
  else
    comparison
  end
end

#==(other) ⇒ Object Also known as: eql?



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

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

#_cap_weight(health) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/big_brother/node.rb', line 70

def _cap_weight(health)
  if !@max_weight.nil? && @max_weight.is_a?(Integer) && @max_weight > 0 && @max_weight < health
    @max_weight
  else
    health
  end
end

#_weight_health(health, ramp_up_time) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/big_brother/node.rb', line 78

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



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

def age
  Time.now.to_i - @start_time
end

#hashObject



45
46
47
# File 'lib/big_brother/node.rb', line 45

def hash
  [@address, @port].hash
end

#incorporate_state(another_node) ⇒ Object



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

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

#interpol?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/big_brother/node.rb', line 36

def interpol?
  @interpol
end

#invalidate_weight!Object



32
33
34
# File 'lib/big_brother/node.rb', line 32

def invalidate_weight!
  @weight = nil
end

#monitor(cluster) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/big_brother/node.rb', line 60

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