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



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

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

Instance Method Details

#bad(endpoint, t0, t1) ⇒ Object



176
177
178
# File 'lib/right_support/net/lb/health_check.rb', line 176

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

#get_statsObject

Proxy to EndpointStack



198
199
200
# File 'lib/right_support/net/lb/health_check.rb', line 198

def get_stats
  @stack.get_stats
end

#good(endpoint, t0, t1) ⇒ Object



172
173
174
# File 'lib/right_support/net/lb/health_check.rb', line 172

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

#health_check(endpoint) ⇒ Object



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

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



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

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



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

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