Class: RightSupport::Net::LB::HealthCheck

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

Overview

has several levels (@yellow_states) to determine the health of the endpoint. The balancer works by avoiding “red” endpoints and retrying them after awhile. Here is a

* on success: remain green
* on failure: change state to yellow and set it's health to healthiest (1)
  • red: skip this server

    * after @reset_time passes change state to yellow and set it's health to
      sickest (@yellow_states)
    
  • yellow: last request was either successful or failed

    * on success: change state to green if it's health was healthiest (1), else
      retain yellow state and improve it's health
    * on failure: change state to red if it's health was sickest (@yellow_states), else
      retain yellow state and decrease it's health
    

A callback option is provided to receive notification of changes in the overall health of the endpoints. The overall health starts out green. When the last endpoint transitions from green to yellow, a callback is made to report the overall health as yellow (or level of yellow). When the last endpoint transitions from yellow to red, a callback is made to report the transition to red. Similarly transitions are reported on the way back down, e.g., yellow is reported as soon as the first endpoint transitions from red to yellow, and so on.

Constant Summary

Constants included from Log::Mixin

Log::Mixin::Decorator

Instance Method Summary collapse

Methods included from Log::Mixin

default_logger, default_logger=, included

Constructor Details

#initialize(options = {}) ⇒ HealthCheck

Returns a new instance of HealthCheck.



121
122
123
# File 'lib/right_support/net/lb/health_check.rb', line 121

def initialize(options = {})
  @options = options
end

Instance Method Details

#bad(endpoint, t0, t1) ⇒ Object



163
164
165
# File 'lib/right_support/net/lb/health_check.rb', line 163

def bad(endpoint, t0, t1)
  @stack.increase_state(endpoint, t0, t1)
end

#get_statsObject

Proxy to EndpointStack



185
186
187
# File 'lib/right_support/net/lb/health_check.rb', line 185

def get_stats
  @stack.get_stats
end

#good(endpoint, t0, t1) ⇒ Object



159
160
161
# File 'lib/right_support/net/lb/health_check.rb', line 159

def good(endpoint, t0, t1)
  @stack.decrease_state(endpoint, t0, t1)
end

#health_check(endpoint) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/right_support/net/lb/health_check.rb', line 167

def health_check(endpoint)
  t0 = Time.now
  result = @health_check.call(endpoint)
  t1 = Time.now
  if result
    @stack.decrease_state(endpoint, t0, t1)
    return true
  else
    @stack.increase_state(endpoint, t0, t1)
    return false
  end
rescue Exception => e
  t1 = Time.now
  @stack.increase_state(endpoint, t0, t1)
  raise e
end

#nextObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/right_support/net/lb/health_check.rb', line 136

def next
  # Returns the array of hashes which consists of yellow and green endpoints with the 
  # following structure: [ [EP1, {:n_level => ..., :timestamp => ... }], [EP2, ... ] ]
  endpoints = @stack.sweep_and_return_yellow_and_green
  return nil if endpoints.empty?

  # Selection of the next endpoint using RoundRobin
  @counter += 1 unless endpoints.size < @last_size
  @counter = 0 if @counter == endpoints.size
  @last_size = endpoints.size

  # Hash#select returns a Hash in ruby1.9, but an Array of pairs in ruby1.8.
  # This should really be encapsulated in EndpointsStack...
  if RUBY_VERSION >= '1.9'
    key = endpoints.keys[@counter]
    next_endpoint = [ key, endpoints[key][:n_level] != 0 ]
  else
    next_endpoint = [ endpoints[@counter][0], endpoints[@counter][1][:n_level] != 0 ]
  end

  next_endpoint
end

#set_endpoints(endpoints) ⇒ Object



125
126
127
128
129
130
131
132
133
134
# File 'lib/right_support/net/lb/health_check.rb', line 125

def set_endpoints(endpoints)
  if @stack
    @stack.update!(endpoints)
  else
    @health_check = @options.delete(:health_check)
    @counter = rand(0xffff) % endpoints.size
    @last_size = endpoints.size
    @stack = EndpointsStack.new(self, endpoints, @options[:yellow_states], @options[:reset_time], @options[:on_health_change])
  end
end