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

Inherits:
Base 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, Log::Mixin::UNDELEGATED

Instance Method Summary collapse

Methods included from Log::Mixin

default_logger, default_logger=, included

Methods inherited from Base

#health_check

Constructor Details

#initialize(options = {}) ⇒ HealthCheck

Returns a new instance of HealthCheck.



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

def initialize(options = {})
  super
  @stack = nil
end

Instance Method Details

#bad(endpoint, t0, t1) ⇒ Object



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

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

#get_statsObject

Proxy to EndpointStack



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

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

#good(endpoint, t0, t1) ⇒ Object



165
166
167
168
# File 'lib/right_support/net/lb/health_check.rb', line 165

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

#nextObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/right_support/net/lb/health_check.rb', line 150

def next
  # avoid red endpoints until they timeout, which may leave us with none
  # temporarily. the idea is to have multiple endpoints such that one or
  # more can go down while others continue to respond.
  result = nil
  yg_eps = @stack.sweep_and_return_yellow_and_green
  unless yg_eps.empty?
    @endpoints = yg_eps.keys
    if result = super
      result[1] = yg_eps[result[0]][:n_level] != 0
    end
  end
  result
end

#set_endpoints(endpoints) ⇒ Object



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

def set_endpoints(endpoints)
  if @stack
    @stack.update!(endpoints)
  else
    @stack = EndpointsStack.new(self, endpoints, @options[:yellow_states], @options[:reset_time], @options[:on_health_change])
  end
  true
end