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

#inspect, #setup, #teardown

Constructor Details

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

Returns a new instance of DCAwareRoundRobin.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb', line 59

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

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

  unless max_remote_hosts_to_use.nil?
    Util.assert(max_remote_hosts_to_use >= 0) { "max_remote_hosts_to_use must be nil or >= 0" }
  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



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

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



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb', line 98

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



114
115
116
# File 'lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb', line 114

def host_found(host)
  self
end

#host_lost(host) ⇒ Object



118
119
120
# File 'lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb', line 118

def host_lost(host)
  self
end

#host_up(host) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb', line 82

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

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

  self
end

#plan(keyspace, statement, options) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb', line 130

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

  if LOCAL_CONSISTENCIES.include?(options.consistency) && !@use_remote
    remote = EMPTY_ARRAY
  else
    remote = @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