class Cassandra::LoadBalancing::Policies::DCAwareRoundRobin

Constants

EMPTY_ARRAY

@private

LOCAL_CONSISTENCIES

@private

Public Class Methods

new(datacenter = nil, max_remote_hosts_to_use = 0, use_remote_hosts_for_local_consistency = false) click to toggle source
   # File lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb
64 def initialize(datacenter = nil,
65                max_remote_hosts_to_use = 0,
66                use_remote_hosts_for_local_consistency = false)
67   datacenter              &&= String(datacenter)
68   max_remote_hosts_to_use &&= Integer(max_remote_hosts_to_use)
69 
70   Util.assert_not_empty(datacenter) { 'datacenter cannot be empty' } unless datacenter.nil?
71 
72   unless max_remote_hosts_to_use.nil?
73     Util.assert(max_remote_hosts_to_use >= 0) do
74       'max_remote_hosts_to_use must be nil or >= 0'
75     end
76   end
77 
78   # If use_remote* is true, max_remote* must be > 0
79   if use_remote_hosts_for_local_consistency
80     Util.assert(max_remote_hosts_to_use.nil? || max_remote_hosts_to_use > 0,
81                 'max_remote_hosts_to_use must be nil (meaning unlimited) or > 0 when ' \
82                 'use_remote_hosts_for_local_consistency is true')
83   end
84 
85   @datacenter = datacenter
86   @max_remote = max_remote_hosts_to_use
87   @local      = ::Array.new
88   @remote     = ::Array.new
89   @position   = 0
90 
91   @use_remote = !!use_remote_hosts_for_local_consistency
92 
93   mon_initialize
94 end

Public Instance Methods

distance(host) click to toggle source
    # File lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb
132 def distance(host)
133   if host.datacenter.nil? || host.datacenter == @datacenter
134     @local.include?(host) ? :local : :ignore
135   else
136     @remote.include?(host) ? :remote : :ignore
137   end
138 end
host_down(host) click to toggle source
    # File lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb
108 def host_down(host)
109   if host.datacenter.nil? || host.datacenter == @datacenter
110     synchronize do
111       @local = @local.dup
112       @local.delete(host)
113     end
114   else
115     synchronize do
116       @remote = @remote.dup
117       @remote.delete(host)
118     end
119   end
120 
121   self
122 end
host_found(host) click to toggle source
    # File lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb
124 def host_found(host)
125   self
126 end
host_lost(host) click to toggle source
    # File lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb
128 def host_lost(host)
129   self
130 end
host_up(host) click to toggle source
    # File lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb
 96 def host_up(host)
 97   @datacenter = host.datacenter if !@datacenter && host.datacenter
 98 
 99   if host.datacenter.nil? || host.datacenter == @datacenter
100     synchronize { @local = @local.dup.push(host) }
101   elsif @max_remote.nil? || @remote.size < @max_remote
102     synchronize { @remote = @remote.dup.push(host) }
103   end
104 
105   self
106 end
inspect() click to toggle source

@private

    # File lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb
160 def inspect
161   "#<#{self.class.name}:0x#{object_id.to_s(16)} " \
162   "datacenter=#{@datacenter.inspect}, " \
163   "use_remote=#{@use_remote.inspect}, " \
164   "max_remote=#{@max_remote.inspect}, " \
165   "local=#{@local.inspect}, " \
166   "remote=#{@remote.inspect}, " \
167   "position=#{@position.inspect}>"
168 end
plan(keyspace, statement, options) click to toggle source
    # File lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb
140 def plan(keyspace, statement, options)
141   local = @local
142 
143   remote = if LOCAL_CONSISTENCIES.include?(options.consistency) && !@use_remote
144              EMPTY_ARRAY
145            else
146              @remote
147            end
148 
149   total = local.size + remote.size
150 
151   return EMPTY_PLAN if total == 0
152 
153   position  = @position % total
154   @position = position + 1
155 
156   Plan.new(local, remote, position)
157 end