Module: ActiveRecord::Turntable::ClusterHelperMethods::ClassMethods
- Defined in:
- lib/active_record/turntable/cluster_helper_methods.rb
Instance Method Summary collapse
- #all_cluster_transaction(options = {}) ⇒ Object
- #force_connect_all_shards! ⇒ Object
- #force_transaction_all_shards!(options = {}, &block) ⇒ Object
- #recursive_cluster_transaction(clusters, options = {}, &block) ⇒ Object
- #recursive_transaction(pools, options, &block) ⇒ Object
- #spec_for(config) ⇒ Object
- #turntable_define_cluster_class_methods(cluster_name) ⇒ Object
- #turntable_define_cluster_methods(cluster_name) ⇒ Object
- #weighted_random_shard_with(*klasses, &block) ⇒ Object
Instance Method Details
#all_cluster_transaction(options = {}) ⇒ Object
57 58 59 60 |
# File 'lib/active_record/turntable/cluster_helper_methods.rb', line 57 def all_cluster_transaction( = {}) clusters = turntable_clusters.values recursive_cluster_transaction(clusters, ) { yield } end |
#force_connect_all_shards! ⇒ Object
32 33 34 |
# File 'lib/active_record/turntable/cluster_helper_methods.rb', line 32 def force_connect_all_shards! turntable_pool_list.each(&:connection) end |
#force_transaction_all_shards!(options = {}, &block) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/active_record/turntable/cluster_helper_methods.rb', line 14 def force_transaction_all_shards!( = {}, &block) force_connect_all_shards! pools = turntable_pool_list pools += [ActiveRecord::Base.connection_pool] recursive_transaction(pools, , &block) end |
#recursive_cluster_transaction(clusters, options = {}, &block) ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/active_record/turntable/cluster_helper_methods.rb', line 62 def recursive_cluster_transaction(clusters, = {}, &block) current_cluster = clusters.shift current_cluster.shards_transaction([], ) do if clusters.present? recursive_cluster_transaction(clusters, , &block) else yield end end end |
#recursive_transaction(pools, options, &block) ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/active_record/turntable/cluster_helper_methods.rb', line 21 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 |
#spec_for(config) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/active_record/turntable/cluster_helper_methods.rb', line 36 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_define_cluster_class_methods(cluster_name) ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/active_record/turntable/cluster_helper_methods.rb', line 77 def turntable_define_cluster_class_methods(cluster_name) (class << ActiveRecord::Base; self; end).class_eval <<-EOD unless respond_to?(:#{cluster_name}_transaction) def #{cluster_name}_transaction(shards = [], options = {}) cluster = turntable_clusters[#{cluster_name.inspect}] cluster.shards_transaction(shards, options) { yield } end end EOD end |
#turntable_define_cluster_methods(cluster_name) ⇒ Object
73 74 75 |
# File 'lib/active_record/turntable/cluster_helper_methods.rb', line 73 def turntable_define_cluster_methods(cluster_name) turntable_define_cluster_class_methods(cluster_name) end |
#weighted_random_shard_with(*klasses, &block) ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/active_record/turntable/cluster_helper_methods.rb', line 46 def weighted_random_shard_with(*klasses, &block) shards_weight = self.turntable_cluster.weighted_shards(self.current_sequence_value(sequence_name)) sum = shards_weight.values.inject(&:+) idx = rand(sum) shard, _weight = shards_weight.find { |_k, v| (idx -= v) < 0 } shard ||= shards_weight.keys.first self.connection.with_recursive_shards(shard.name, *klasses, &block) end |