Module: ActiveRecord::Turntable::Base::ClassMethods
- Defined in:
- lib/active_record/turntable/base.rb
Instance Method Summary collapse
- #clear_all_connections! ⇒ Object
- #current_last_shard ⇒ Object
- #current_sequence ⇒ Object
- #force_connect_all_shards! ⇒ Object
- #force_transaction_all_shards!(options = {}, &block) ⇒ Object
- #recursive_transaction(pools, options, &block) ⇒ Object
- #sequencer(sequence_name, *args) ⇒ Object
- #sequencer_enabled? ⇒ Boolean
- #spec_for(config) ⇒ Object
- #turntable(cluster_name, shard_key_name, options = {}) ⇒ Object
- #turntable_enabled? ⇒ Boolean
- #turntable_replace_connection_pool ⇒ Object
- #weighted_random_shard_with(*klasses, &block) ⇒ Object
- #with_shard(any_shard) ⇒ Object
Instance Method Details
#clear_all_connections! ⇒ Object
88 89 90 91 92 |
# File 'lib/active_record/turntable/base.rb', line 88 def clear_all_connections! turntable_connections.values.each do |pool| pool.disconnect! end end |
#current_last_shard ⇒ Object
113 114 115 |
# File 'lib/active_record/turntable/base.rb', line 113 def current_last_shard turntable_cluster.select_shard(current_sequence) if sequencer_enabled? end |
#current_sequence ⇒ Object
109 110 111 |
# File 'lib/active_record/turntable/base.rb', line 109 def current_sequence connection.current_sequence_value(self.sequence_name) if sequencer_enabled? end |
#force_connect_all_shards! ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/active_record/turntable/base.rb', line 59 def force_connect_all_shards! conf = configurations[Rails.env] shards = {} shards = shards.merge(conf["shards"]) if conf["shards"] shards = shards.merge(conf["seq"]) if conf["seq"] shards.each do |name, config| turntable_connections[name] ||= ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec_for(config)) end end |
#force_transaction_all_shards!(options = {}, &block) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/active_record/turntable/base.rb', line 41 def force_transaction_all_shards!(={}, &block) force_connect_all_shards! shards = turntable_connections.values shards += [ActiveRecord::Base.connection_pool] recursive_transaction(shards, , &block) end |
#recursive_transaction(pools, options, &block) ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/active_record/turntable/base.rb', line 48 def recursive_transaction(pools, , &block) pool = pools.shift if pools.present? pool.connection.transaction() do recursive_transaction(pools, , &block) end else pool.connection.transaction(, &block) end end |
#sequencer(sequence_name, *args) ⇒ Object
94 95 96 97 98 99 |
# File 'lib/active_record/turntable/base.rb', line 94 def sequencer(sequence_name, *args) class_attribute :turntable_sequencer self.turntable_sequencer_enabled = true self.turntable_sequencer = ActiveRecord::Turntable::Sequencer.build(self, sequence_name, *args) end |
#sequencer_enabled? ⇒ Boolean
105 106 107 |
# File 'lib/active_record/turntable/base.rb', line 105 def sequencer_enabled? turntable_sequencer_enabled end |
#spec_for(config) ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/active_record/turntable/base.rb', line 78 def spec_for(config) begin require "active_record/connection_adapters/#{config['adapter']}_adapter" rescue LoadError => e raise "Please install the #{config['adapter']} adapter: `gem install activerecord-#{config['adapter']}-adapter` (#{e})" end adapter_method = "#{config['adapter']}_connection" ActiveRecord::ConnectionAdapters::ConnectionSpecification.new(config, adapter_method) end |
#turntable(cluster_name, shard_key_name, options = {}) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/active_record/turntable/base.rb', line 22 def turntable(cluster_name, shard_key_name, = {}) class_attribute :turntable_shard_key, :turntable_cluster, :turntable_cluster_name self.turntable_enabled = true self.turntable_cluster_name = cluster_name self.turntable_shard_key = shard_key_name self.turntable_cluster = Cluster.new( self, turntable_config[:clusters][cluster_name], ) self.turntable_clusters[cluster_name][self] = turntable_cluster turntable_replace_connection_pool turntable_define_cluster_methods(cluster_name) end |
#turntable_enabled? ⇒ Boolean
101 102 103 |
# File 'lib/active_record/turntable/base.rb', line 101 def turntable_enabled? turntable_enabled end |
#turntable_replace_connection_pool ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/active_record/turntable/base.rb', line 70 def turntable_replace_connection_pool ch = connection_handler cp = turntable_cluster.connection_proxy pp = PoolProxy.new(cp) ch.class_to_pool.clear if defined?(ch.class_to_pool) ch.send(:class_to_pool)[name] = ch.send(:owner_to_pool)[name] = pp end |
#weighted_random_shard_with(*klasses, &block) ⇒ Object
117 118 119 120 121 122 123 124 125 |
# File 'lib/active_record/turntable/base.rb', line 117 def weighted_random_shard_with(*klasses, &block) shards_weight = self.turntable_cluster.weighted_shards sum = shards_weight.values.inject(&:+) idx = rand(sum) shard, weight = shards_weight.find {|k,v| (idx -= v) < 0 } self.connection.with_recursive_shards(shard.name, *klasses, &block) end |
#with_shard(any_shard) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/active_record/turntable/base.rb', line 127 def with_shard(any_shard) shard = case any_shard when Numeric turntable_cluster.shard_for(any_shard) when ActiveRecord::Base turntable_cluster.shard_for(any_shard.send(any_shard.turntable_shard_key)) else shard_or_key end connection.with_shard(shard) { yield } end |