Class: Cassandra::LoadBalancing::Policies::DCAwareRoundRobin

Inherits:
Cassandra::LoadBalancing::Policy show all
Includes:
MonitorMixin
Defined in:
lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb

Instance Method Summary collapse

Methods inherited from Cassandra::LoadBalancing::Policy

#setup, #teardown

Constructor Details

#initialize(datacenter = nil, max_remote_hosts_to_use = 0, use_remote_hosts_for_local_consistency = false) ⇒ DCAwareRoundRobin

Returns a new instance of DCAwareRoundRobin.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb', line 64

def initialize(datacenter = nil,
               max_remote_hosts_to_use = 0,
               use_remote_hosts_for_local_consistency = false)
  datacenter              &&= String(datacenter)
  max_remote_hosts_to_use &&= Integer(max_remote_hosts_to_use)

  Util.assert_not_empty(datacenter) { 'datacenter cannot be empty' } unless datacenter.nil?

  unless max_remote_hosts_to_use.nil?
    Util.assert(max_remote_hosts_to_use >= 0) do
      'max_remote_hosts_to_use must be nil or >= 0'
    end
  end

  # If use_remote* is true, max_remote* must be > 0
  if use_remote_hosts_for_local_consistency
    Util.assert(max_remote_hosts_to_use.nil? || max_remote_hosts_to_use > 0,
                'max_remote_hosts_to_use must be nil (meaning unlimited) or > 0 when ' \
                'use_remote_hosts_for_local_consistency is true')
  end

  @datacenter = datacenter
  @max_remote = max_remote_hosts_to_use
  @local      = ::Array.new
  @remote     = ::Array.new
  @position   = 0

  @use_remote = !!use_remote_hosts_for_local_consistency

  mon_initialize
end

Instance Method Details

#distance(host) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb', line 132

def distance(host)
  if host.datacenter.nil? || host.datacenter == @datacenter
    @local.include?(host) ? :local : :ignore
  else
    @remote.include?(host) ? :remote : :ignore
  end
end

#host_down(host) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb', line 108

def host_down(host)
  if host.datacenter.nil? || host.datacenter == @datacenter
    synchronize do
      @local = @local.dup
      @local.delete(host)
    end
  else
    synchronize do
      @remote = @remote.dup
      @remote.delete(host)
    end
  end

  self
end

#host_found(host) ⇒ Object



124
125
126
# File 'lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb', line 124

def host_found(host)
  self
end

#host_lost(host) ⇒ Object



128
129
130
# File 'lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb', line 128

def host_lost(host)
  self
end

#host_up(host) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb', line 96

def host_up(host)
  @datacenter = host.datacenter if !@datacenter && host.datacenter

  if host.datacenter.nil? || host.datacenter == @datacenter
    synchronize { @local = @local.dup.push(host) }
  elsif @max_remote.nil? || @remote.size < @max_remote
    synchronize { @remote = @remote.dup.push(host) }
  end

  self
end

#plan(keyspace, statement, options) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb', line 140

def plan(keyspace, statement, options)
  local = @local

  remote = if LOCAL_CONSISTENCIES.include?(options.consistency) && !@use_remote
             EMPTY_ARRAY
           else
             @remote
           end

  total = local.size + remote.size

  return EMPTY_PLAN if total == 0

  position  = @position % total
  @position = position + 1

  Plan.new(local, remote, position)
end