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.



135
136
137
# File 'lib/right_support/net/lb/health_check.rb', line 135

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

Instance Method Details

#bad(endpoint, t0, t1) ⇒ Object



178
179
180
# File 'lib/right_support/net/lb/health_check.rb', line 178

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

#get_statsObject

Proxy to EndpointStack



200
201
202
203
204
205
206
# File 'lib/right_support/net/lb/health_check.rb', line 200

def get_stats
  if @stack
    @stack.get_stats
  else
    {}
  end
end

#good(endpoint, t0, t1) ⇒ Object



174
175
176
# File 'lib/right_support/net/lb/health_check.rb', line 174

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

#health_check(endpoint) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/right_support/net/lb/health_check.rb', line 182

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
end

#nextObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/right_support/net/lb/health_check.rb', line 150

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?

  # From the available set, use a RoundRobin-like algorithm to select the next endpoint.
  # When the size of the available set changes, try not to disturb our index into the list.
  @counter += 1 unless endpoints.size < @last_size
  @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



139
140
141
142
143
144
145
146
147
148
# File 'lib/right_support/net/lb/health_check.rb', line 139

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