Class: ActiveRecord::Turntable::Cluster

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/turntable/cluster.rb

Constant Summary collapse

DEFAULT_CONFIG =
{
  "shards" => [],
  "algorithm" => "range",
}.with_indifferent_access

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCluster

Returns a new instance of Cluster.



13
14
15
# File 'lib/active_record/turntable/cluster.rb', line 13

def initialize
  @slave_enabled = Concurrent::ThreadLocalVar.new(false)
end

Instance Attribute Details

#algorithmObject

Returns the value of attribute algorithm.



11
12
13
# File 'lib/active_record/turntable/cluster.rb', line 11

def algorithm
  @algorithm
end

#sequencer_registryObject

Returns the value of attribute sequencer_registry.



11
12
13
# File 'lib/active_record/turntable/cluster.rb', line 11

def sequencer_registry
  @sequencer_registry
end

#shard_registryObject

Returns the value of attribute shard_registry.



11
12
13
# File 'lib/active_record/turntable/cluster.rb', line 11

def shard_registry
  @shard_registry
end

Class Method Details

.build(sequencer_registry) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/active_record/turntable/cluster.rb', line 17

def self.build(sequencer_registry)
  self.new.tap do |instance|
    instance.shard_registry = ShardRegistry.new(instance)
    instance.sequencer_registry = sequencer_registry
    yield instance
  end
end

Instance Method Details

#select_shard(key) ⇒ Object



34
35
36
37
# File 'lib/active_record/turntable/cluster.rb', line 34

def select_shard(key)
  ActiveSupport::Deprecation.warn "Cluster#select_shard is deprecated, use shard_for() instead.", caller
  shard_for(key)
end

#sequencer(name) ⇒ Object



86
87
88
# File 'lib/active_record/turntable/cluster.rb', line 86

def sequencer(name)
  sequencers[name]
end

#sequencersObject



82
83
84
# File 'lib/active_record/turntable/cluster.rb', line 82

def sequencers
  sequencer_registry.all
end

#set_slave_enabled(enabled) ⇒ Object



78
79
80
# File 'lib/active_record/turntable/cluster.rb', line 78

def set_slave_enabled(enabled)
  @slave_enabled.value = enabled
end

#shard_for(key) ⇒ Object



27
28
29
30
31
32
# File 'lib/active_record/turntable/cluster.rb', line 27

def shard_for(key)
  algorithm.choose(shard_maps, key)
rescue
  raise ActiveRecord::Turntable::CannotSpecifyShardError,
        "cannot select shard for key:#{key.inspect}"
end

#shards_transaction(shards = [], options = {}, in_recursion = false, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/active_record/turntable/cluster.rb', line 39

def shards_transaction(shards = [], options = {}, in_recursion = false, &block)
  unless in_recursion
    shards = Array.wrap(shards).dup
    if shards.blank?
      shards = self.shards.dup
    end
  end
  shard = to_shard(shards.shift)
  if shards.present?
    shard.connection.transaction(options) do
      shards_transaction(shards, options, true, &block)
    end
  else
    shard.connection.transaction(options) do
      yield
    end
  end
end

#slave_enabled?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/active_record/turntable/cluster.rb', line 74

def slave_enabled?
  @slave_enabled.value
end

#to_shard(shard_or_object) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/active_record/turntable/cluster.rb', line 58

def to_shard(shard_or_object)
  case shard_or_object
  when ActiveRecord::Turntable::Shard
    shard_or_object
  when ActiveRecord::Base
    shard_or_object.turntable_shard
  when Numeric, String
    shard_for(shard_or_object)
  when Symbol
    shards[shard_or_object]
  else
    raise ActiveRecord::Turntable::TurntableError,
          "transaction cannot call to object: #{shard_or_object}"
  end
end

#weighted_shards(key = nil) ⇒ Object



90
91
92
# File 'lib/active_record/turntable/cluster.rb', line 90

def weighted_shards(key = nil)
  @algorithm.shard_weights(shard_maps, key)
end