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



56
57
58
59
# File 'lib/active_record/turntable/cluster_helper_methods.rb', line 56

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

#force_connect_all_shards!Object



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

def force_connect_all_shards!
  conf = configurations[::ActiveRecord::ConnectionHandling::DEFAULT_ENV.call.to_sym]
  return unless conf

  shards = HashWithIndifferentAccess.new
  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



61
62
63
64
65
66
67
68
69
70
# File 'lib/active_record/turntable/cluster_helper_methods.rb', line 61

def recursive_cluster_transaction(clusters, options = {}, &block)
  current_cluster = clusters.shift
  current_cluster.shards_transaction([], options) 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



76
77
78
79
80
81
82
83
84
85
# File 'lib/active_record/turntable/cluster_helper_methods.rb', line 76

def turntable_define_cluster_class_methods(cluster_name)
  (class << ActiveRecord::Base; self; end).class_eval "    unless respond_to?(:\#{cluster_name}_transaction)\n      def \#{cluster_name}_transaction(shards = [], options = {})\n        cluster = turntable_clusters[\#{cluster_name.inspect}]\n        cluster.shards_transaction(shards, options) { yield }\n      end\n    end\n  EOD\nend\n"

#turntable_define_cluster_methods(cluster_name) ⇒ Object



72
73
74
# File 'lib/active_record/turntable/cluster_helper_methods.rb', line 72

def turntable_define_cluster_methods(cluster_name)
  turntable_define_cluster_class_methods(cluster_name)
end

#weighted_random_shard_with(*klasses, &block) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/active_record/turntable/cluster_helper_methods.rb', line 45

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
  }
  shard ||= shards_weight.keys.first
  self.connection.with_recursive_shards(shard.name, *klasses, &block)
end