Class: Cassandra::LoadBalancing::Policies::RoundRobin

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

Instance Method Summary collapse

Methods inherited from Cassandra::LoadBalancing::Policy

#inspect, #setup, #teardown

Constructor Details

#initializeRoundRobin

Returns a new instance of RoundRobin.



49
50
51
52
53
54
# File 'lib/cassandra/load_balancing/policies/round_robin.rb', line 49

def initialize
  @hosts    = ::Array.new
  @position = 0

  mon_initialize
end

Instance Method Details

#distance(host) ⇒ Symbol

Returns distance to host. All hosts in rotation are considered :local, all other hosts - :ignore.

Parameters:

Returns:

  • (Symbol)

    :local for all hosts in rotation and :ignore for all other hosts.

See Also:



106
107
108
# File 'lib/cassandra/load_balancing/policies/round_robin.rb', line 106

def distance(host)
  @hosts.include?(host) ? :local : :ignore
end

#host_down(host) ⇒ Cassandra::LoadBalancing::Policies::RoundRobin

Removes this host from rotation

Parameters:

Returns:

See Also:



72
73
74
75
76
77
78
79
# File 'lib/cassandra/load_balancing/policies/round_robin.rb', line 72

def host_down(host)
  synchronize do
    @hosts = @hosts.dup
    @hosts.delete(host)
  end

  self
end

#host_found(host) ⇒ Cassandra::LoadBalancing::Policies::RoundRobin

Noop

Parameters:

Returns:

See Also:



86
87
88
# File 'lib/cassandra/load_balancing/policies/round_robin.rb', line 86

def host_found(host)
  self
end

#host_lost(host) ⇒ Cassandra::LoadBalancing::Policies::RoundRobin

Noop

Parameters:

Returns:

See Also:



95
96
97
# File 'lib/cassandra/load_balancing/policies/round_robin.rb', line 95

def host_lost(host)
  self
end

#host_up(host) ⇒ Cassandra::LoadBalancing::Policies::RoundRobin

Adds this host to rotation

Parameters:

Returns:

See Also:



61
62
63
64
65
# File 'lib/cassandra/load_balancing/policies/round_robin.rb', line 61

def host_up(host)
  synchronize { @hosts = @hosts.dup.push(host) }

  self
end

#plan(keyspace, statement, options) ⇒ Cassandra::LoadBalancing::Plan

Returns a load balancing plan that rotates hosts by 1 each time a plan is requested.

Parameters:

Returns:

See Also:



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/cassandra/load_balancing/policies/round_robin.rb', line 120

def plan(keyspace, statement, options)
  hosts = @hosts
  total = hosts.size

  return EMPTY_PLAN if total == 0

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

  Plan.new(hosts, position)
end