Class: RightSupport::Net::LB::EndpointsStack

Inherits:
Object
  • Object
show all
Defined in:
lib/right_support/net/lb/health_check.rb

Overview

TODO refactor this class. We store too much unstructured data about EPs; should have a simple class representing EP state, and then perhaps move what logic remains into the HealthCheck class instead of putting it here.

Constant Summary collapse

DEFAULT_YELLOW_STATES =
4
DEFAULT_RESET_TIME =
300

Instance Method Summary collapse

Constructor Details

#initialize(policy, endpoints, yellow_states = nil, reset_time = nil, on_health_change = nil) ⇒ EndpointsStack

Returns a new instance of EndpointsStack.



34
35
36
37
38
39
40
41
42
# File 'lib/right_support/net/lb/health_check.rb', line 34

def initialize(policy, endpoints, yellow_states=nil, reset_time=nil, on_health_change=nil)
  @policy = policy
  @endpoints = Hash.new
  @yellow_states = yellow_states || DEFAULT_YELLOW_STATES
  @reset_time = reset_time || DEFAULT_RESET_TIME
  @on_health_change = on_health_change
  @min_n_level = 0
  endpoints.each { |ep| @endpoints[ep] = {:n_level => @min_n_level, :timestamp => 0} }
end

Instance Method Details

#decrease_state(endpoint, t0, t1) ⇒ Object



53
54
55
# File 'lib/right_support/net/lb/health_check.rb', line 53

def decrease_state(endpoint, t0, t1)
  update_state(endpoint, -1, t1) unless @endpoints[endpoint][:n_level] == 0
end

#get_statsObject

Returns a hash of endpoints and their colored health status Useful for logging and debugging



82
83
84
85
86
# File 'lib/right_support/net/lb/health_check.rb', line 82

def get_stats
  stats = {}
  @endpoints.each { |k, v| stats[k] = state_color(v[:n_level]) }
  stats
end

#increase_state(endpoint, t0, t1) ⇒ Object



57
58
59
# File 'lib/right_support/net/lb/health_check.rb', line 57

def increase_state(endpoint, t0, t1)
  update_state(endpoint, 1, t1) unless @endpoints[endpoint][:n_level] == @yellow_states
end

#loggerObject

Return the logger that our surrounding policy uses



94
95
96
# File 'lib/right_support/net/lb/health_check.rb', line 94

def logger
  @policy.logger
end

#state_color(n_level) ⇒ Object



73
74
75
76
77
78
# File 'lib/right_support/net/lb/health_check.rb', line 73

def state_color(n_level)
  color = 'green' if n_level == 0
  color = 'red' if n_level == @yellow_states
  color = "yellow-#{n_level}" if n_level > 0 && n_level < @yellow_states
  color
end

#sweepObject



44
45
46
# File 'lib/right_support/net/lb/health_check.rb', line 44

def sweep
  @endpoints.each { |k,v| decrease_state(k, 0, Time.now) if Float(Time.now - v[:timestamp]) > @reset_time }
end

#sweep_and_return_yellow_and_greenObject



48
49
50
51
# File 'lib/right_support/net/lb/health_check.rb', line 48

def sweep_and_return_yellow_and_green
  sweep
  @endpoints.select { |k,v| v[:n_level] < @yellow_states }
end

#update!(endpoints) ⇒ Object



88
89
90
91
# File 'lib/right_support/net/lb/health_check.rb', line 88

def update!(endpoints)
  @endpoints.each { |k,v| endpoints.include?(k) ? endpoints.delete(k) : @endpoints.delete(k) }
  endpoints.each  { |ep| @endpoints[ep] = {:n_level => @min_n_level, :timestamp => 0} }
end

#update_state(endpoint, change, t1) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/right_support/net/lb/health_check.rb', line 61

def update_state(endpoint, change, t1)
  @endpoints[endpoint][:timestamp] = t1
  n_level = @endpoints[endpoint][:n_level] += change
  logger.info("RequestBalancer: Health of endpoint '#{endpoint}' #{change < 0 ? 'improved' : 'worsened'} to '#{state_color(n_level)}'")
  if @on_health_change &&
     (n_level < @min_n_level ||
     (n_level > @min_n_level && n_level == @endpoints.map { |(k, v)| v[:n_level] }.min))
    @min_n_level = n_level
    @on_health_change.call(state_color(n_level))
  end
end