Class: Cassandra::LoadBalancing::Policies::TokenAware

Inherits:
Cassandra::LoadBalancing::Policy show all
Extended by:
Forwardable
Defined in:
lib/cassandra/load_balancing/policies/token_aware.rb

Instance Method Summary collapse

Constructor Details

#initialize(wrapped_policy, shuffle = true) ⇒ TokenAware

Note:

If replicas are not shuffled (shuffle = false), then it is possibile to create hotspots in a write-heavy scenario, where most of the write requests will be handled by the same node(s). The default behavior of shuffling replicas helps mitigate this by universally distributing write load between replicas. However, it under-utilizes read caching and forces multiple replicas to cache the same read statements.

Returns a new instance of TokenAware.

Parameters:

  • wrapped_policy (Cassandra::LoadBalancing::Policy)

    actual policy to filter

  • shuffle (Boolean) (defaults to: true)

    (true) whether or not to shuffle the replicas



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/cassandra/load_balancing/policies/token_aware.rb', line 101

def initialize(wrapped_policy, shuffle = true)
  methods = [:host_up, :host_down, :host_found, :host_lost, :setup, :teardown,
             :distance, :plan]

  Util.assert_responds_to_all(methods, wrapped_policy) do
    "supplied policy must respond to #{methods.inspect}, but doesn't"
  end

  @policy  = wrapped_policy
  @shuffle = !!shuffle
end

Instance Method Details

#distance(host) ⇒ Object

Delegates to wrapped policy



88
# File 'lib/cassandra/load_balancing/policies/token_aware.rb', line 88

def_delegators :@policy, :distance, :host_found, :host_up, :host_down, :host_lost

#host_down(host) ⇒ Object

Delegates to wrapped policy



88
# File 'lib/cassandra/load_balancing/policies/token_aware.rb', line 88

def_delegators :@policy, :distance, :host_found, :host_up, :host_down, :host_lost

#host_found(host) ⇒ Object

Delegates to wrapped policy



88
# File 'lib/cassandra/load_balancing/policies/token_aware.rb', line 88

def_delegators :@policy, :distance, :host_found, :host_up, :host_down, :host_lost

#host_lost(host) ⇒ Object

Delegates to wrapped policy



88
# File 'lib/cassandra/load_balancing/policies/token_aware.rb', line 88

def_delegators :@policy, :distance, :host_found, :host_up, :host_down, :host_lost

#host_up(host) ⇒ Object

Delegates to wrapped policy



88
# File 'lib/cassandra/load_balancing/policies/token_aware.rb', line 88

def_delegators :@policy, :distance, :host_found, :host_up, :host_down, :host_lost

#plan(keyspace, statement, options) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/cassandra/load_balancing/policies/token_aware.rb', line 125

def plan(keyspace, statement, options)
  return @policy.plan(keyspace, statement, options) unless @cluster

  replicas = @cluster.find_replicas(keyspace, statement)
  return @policy.plan(keyspace, statement, options) if replicas.empty?

  replicas = if @shuffle
               replicas.shuffle
             else
               replicas.dup
             end

  Plan.new(replicas, @policy, keyspace, statement, options)
end

#setup(cluster) ⇒ Object



113
114
115
116
117
# File 'lib/cassandra/load_balancing/policies/token_aware.rb', line 113

def setup(cluster)
  @cluster = cluster
  @policy.setup(cluster)
  nil
end

#teardown(cluster) ⇒ Object



119
120
121
122
123
# File 'lib/cassandra/load_balancing/policies/token_aware.rb', line 119

def teardown(cluster)
  @cluster = nil
  @policy.teardown(cluster)
  nil
end