Class: DatastaxRails::LoadBalancing::Policies::StickyDcAwareRoundRobin

Inherits:
Cassandra::LoadBalancing::Policies::DCAwareRoundRobin
  • Object
show all
Defined in:
lib/datastax_rails/load_balancing/policies/sticky_dc_aware_round_robin.rb

Overview

In Datastax Enterprise, there is a small amount of time between when a record is updated on the local node and when that update is available in the Solr index on other nodes within the datacenter. As a result, we want to stick to a particular node for a number of requests before we roll on to the next node. This minimizes the chance of data not being where we expect it to be.

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of StickyDcAwareRoundRobin


11
12
13
14
15
16
17
18
19
# File 'lib/datastax_rails/load_balancing/policies/sticky_dc_aware_round_robin.rb', line 11

def initialize(max_requests,
               datacenter = nil,
               max_remote_hosts_to_use = nil,
               use_remote_hosts_for_local_consistency = false)
  @max_requests = max_requests
  super(datacenter, max_remote_hosts_to_use, use_remote_hosts_for_local_consistency)
  Thread.current[:position] = 0
  Thread.current[:sticky_count] = 0
end

Instance Method Details

#plan(_keyspace, _statement, options) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/datastax_rails/load_balancing/policies/sticky_dc_aware_round_robin.rb', line 21

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

  Thread.current[:position] ||= rand(total)
  Thread.current[:sticky_count] ||= 0

  if Thread.current[:sticky_count] >= @max_requests
    Thread.current[:position] = (Thread.current[:position] + 1) % total
    Thread.current[:sticky_count] = 0
  else
    Thread.current[:sticky_count] += 1
  end

  Plan.new(local, remote, Thread.current[:position])
end