class Orientdb4r::LBStrategy

Base class for implementation of load balancing strategy.

Constants

DEFAULT_RECOVER_TIME

After what time [s] can be a failed node reused in load balancing.

Attributes

bad_nodes[R]
nodes_count[R]
recover_time[RW]

Public Class Methods

new(nodes_count) click to toggle source

Constructor.

# File lib/orientdb4r/load_balancing.rb, line 15
def initialize nodes_count
  @nodes_count = nodes_count
  @bad_nodes = {}
  @recover_time = DEFAULT_RECOVER_TIME
end

Public Instance Methods

bad_one(idx) click to toggle source

Marks an index as bad that means it will be not used until:

  • there is other ‘good’ node

  • timeout

# File lib/orientdb4r/load_balancing.rb, line 38
def bad_one(idx)
  @bad_nodes[idx] = Time.now
end
good_one(idx) click to toggle source

Marks an index as good that means it can be used for next server calls.

# File lib/orientdb4r/load_balancing.rb, line 30
def good_one(idx)
  @bad_nodes.delete idx
end
node_index() click to toggle source

Gets index of node to be used for next request or ‘nil’ if there is no one next.

# File lib/orientdb4r/load_balancing.rb, line 24
def node_index
  raise NotImplementedError, 'this should be overridden in subclass'
end

Protected Instance Methods

search_next_good(bad_idx) click to toggle source

Tries to find a new node if the given failed. Returns nil if no one found

# File lib/orientdb4r/load_balancing.rb, line 47
def search_next_good(bad_idx)
  Orientdb4r::logger.warn "identified bad node, idx=#{bad_idx}, age=#{Time.now - @bad_nodes[bad_idx]} [s]"

  # alternative nodes of not found a good one
  timeout_candidate = nil

  # first round - try to find a first good one
  1.upto(nodes_count) do |i|
    candidate = (i + bad_idx) % nodes_count

    if @bad_nodes.include? candidate
      failure_time = @bad_nodes[candidate]
      now = Time.now
      # timeout candidate
      if (now - failure_time) > recover_time
        timeout_candidate = candidate
        Orientdb4r::logger.debug "node timeout recovery, idx=#{candidate}"
        good_one(candidate)
      end
    else
      Orientdb4r::logger.debug "found good node, idx=#{candidate}"
      return candidate
    end
  end

  # no good index found -> try timeouted one
  unless timeout_candidate.nil?
    Orientdb4r::logger.debug "good node not found, delivering timeouted one, idx=#{timeout_candidate}"
    return timeout_candidate
  end

  Orientdb4r::logger.error 'no nodes more, all invalid'
  nil
end