Module: ActiveRecord::Turntable::ClusterHelperMethods::ClassMethods

Defined in:
lib/active_record/turntable/cluster_helper_methods.rb

Instance Method Summary collapse

Instance Method Details

#all_cluster_transaction(options = {}) ⇒ Object



53
54
55
56
# File 'lib/active_record/turntable/cluster_helper_methods.rb', line 53

def all_cluster_transaction(options = {})
  clusters = turntable_clusters.values
  recursive_cluster_transaction(clusters) { yield }
end

#force_connect_all_shards!Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/active_record/turntable/cluster_helper_methods.rb', line 32

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



14
15
16
17
18
19
# File 'lib/active_record/turntable/cluster_helper_methods.rb', line 14

def force_transaction_all_shards!(options={}, &block)
  force_connect_all_shards!
  shards = turntable_connections.values
  shards += [ActiveRecord::Base.connection_pool]
  recursive_transaction(shards, options, &block)
end

#recursive_cluster_transaction(clusters, options = {}, &block) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/active_record/turntable/cluster_helper_methods.rb', line 58

def recursive_cluster_transaction(clusters, options = {}, &block)
  current_cluster = clusters.shift
  current_cluster.shards_transaction do
    if clusters.present?
      recursive_cluster_transaction(clusters, options, &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, options, &block)
  pool = pools.shift
  if pools.present?
    pool.connection.transaction(options) do
      recursive_transaction(pools, options, &block)
    end
  else
    pool.connection.transaction(options, &block)
  end
end

#turntable_define_cluster_class_methods(cluster_name) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/active_record/turntable/cluster_helper_methods.rb', line 73

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



69
70
71
# File 'lib/active_record/turntable/cluster_helper_methods.rb', line 69

def turntable_define_cluster_methods(cluster_name)
  turntable_define_cluster_class_methods(cluster_name)
end

#weighted_random_shard_with(*klasses, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/active_record/turntable/cluster_helper_methods.rb', line 43

def weighted_random_shard_with(*klasses, &block)
  shards_weight = self.turntable_cluster.weighted_shards(self.current_sequence)
  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