Skip to content

Commit

Permalink
Merge branch 'feature/comment' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
gussan committed Nov 9, 2014
2 parents acd872b + af6aa38 commit 58afd3e
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module AbstractAdapter
included do
protected

# @note override for logging current shard name
def log(sql, name = "SQL", binds = [])
@instrumenter.instrument(
"sql.active_record",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module ActiveRecord::Turntable
module ActiveRecordExt
# activerecord-import extension
module ActiverecordImportExt
extend ActiveSupport::Concern

Expand All @@ -9,6 +10,7 @@ module ActiverecordImportExt

private

# @note override for sequencer injection
def values_sql_for_columns_and_attributes_with_turntable(columns, array_of_attributes)
connection_memo = connection
array_of_attributes.map do |arr|
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
begin
require 'acts_as_archive'

# acts_as_archive extension
class ActsAsArchive
class << self
# @note use the same shard which `from` shard using
def move_with_turntable(config, where, merge_options={})
if [config[:to], config[:from]].all? { |k| k.try(:turntable_enabled?) }
current_shard = config[:from].connection.current_shard.name.to_sym
Expand Down
2 changes: 2 additions & 0 deletions lib/active_record/turntable/active_record_ext/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module SingularAssociationExt

private

# @note Override to add sharding condition for singular association
if ActiveRecord::Turntable.rails41_later?
def find_target_with_turntable
current_scope = scope
Expand Down Expand Up @@ -50,6 +51,7 @@ module CollectionAssociationExt

private

# @note Override to add sharding condition for collection association
def find_target_with_turntable
records =
if options[:finder_sql]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module AssociationPreloader
alias_method_chain :records_for, :turntable
end

# @note Override to add sharding condition on preload
def records_for_with_turntable(ids)
returning_scope = records_for_without_turntable(ids)
if should_use_shard_key?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module ConnectionHandlerExtension

private

# @note Override not to establish_connection destroy existing connection pool proxy object
def pool_for_with_turntable(owner)
owner_to_pool.fetch(owner.name) {
if ancestor_pool = pool_from_any_process_for(owner)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ActiveRecord::Turntable::ActiveRecordExt
module LockingOptimistic

# @note Override to add sharding condition on optimistic locking
::ActiveRecord::Locking::Optimistic.class_eval do

ar_version = ActiveRecord::VERSION::STRING
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module LogSubscriber

protected

# @note Override to add shard name logging
def sql_with_turntable(event)
self.class.runtime += event.duration
return unless logger.debug?
Expand Down
5 changes: 5 additions & 0 deletions lib/active_record/turntable/active_record_ext/persistence.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module ActiveRecord::Turntable::ActiveRecordExt
module Persistence
::ActiveRecord::Persistence.class_eval do
# @note Override to add sharding scope on reloading
def reload(options = nil)
clear_aggregation_cache
clear_association_cache
Expand All @@ -26,6 +27,7 @@ def reload(options = nil)
self
end

# @note Override to add sharding scope on `touch`
def touch(name = nil)
raise ActiveRecordError, "can not touch on a new record object" unless persisted?

Expand Down Expand Up @@ -58,6 +60,7 @@ def touch(name = nil)

private

# @note Override to add sharding scope on destroying
def relation_for_destroy
pk = self.class.primary_key
column = self.class.columns_hash[pk]
Expand All @@ -74,6 +77,8 @@ def relation_for_destroy
relation
end


# @note Override to add sharding scope on updating
ar_version = ActiveRecord::VERSION::STRING
if ar_version < "4.1"
method_name = ar_version =~ /\A4\.0\.[0-5]\z/ ? "update_record" : "_update_record"
Expand Down
1 change: 1 addition & 0 deletions lib/active_record/turntable/active_record_ext/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module Relation
end
end

# @note Override to add sharding scope on updating
def _update_record_with_turntable(values, id, id_was, turntable_scope = nil) # :nodoc:
substitutes, binds = substitute_values values
condition_scope = @klass.unscoped.where(@klass.arel_table[@klass.primary_key].eq(id_was || id))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module SchemaDumper

private

# @note Override to dump database sequencer method
def table_with_turntable(table, stream)
columns = @connection.columns(table)
begin
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module ActiveRecord::Turntable
module ActiveRecordExt
module Transactions
# @note Override to start transaction on current shard
def with_transaction_returning_status
if self.class.turntable_enabled?
status = nil
Expand Down
6 changes: 6 additions & 0 deletions lib/active_record/turntable/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class << self
end

module ClassMethods
# @param [Symbol] cluster_name cluster name for this class
# @param [Symbol] shard_key_name shard key attribute name
# @param [Hash] options
def turntable(cluster_name, shard_key_name, options = {})
class_attribute :turntable_shard_key,
:turntable_cluster, :turntable_cluster_name
Expand All @@ -34,6 +37,7 @@ def turntable(cluster_name, shard_key_name, options = {})
turntable_define_cluster_methods(cluster_name)
end

#
def force_transaction_all_shards!(options={}, &block)
force_connect_all_shards!
shards = turntable_connections.values
Expand Down Expand Up @@ -172,10 +176,12 @@ def shards_transaction(options = {}, &block)
self.class.shards_transaction(options, &block)
end

# @return [ActiveRecord::Turntable::Shard] current shard for self
def turntable_shard
turntable_cluster.shard_for(self.send(turntable_shard_key))
end

# @see ActiveRecord::Turntable::ConnectionProxy#with_shard
def with_shard(shard)
self.class.connection.with_shard(shard) { yield }
end
Expand Down
12 changes: 12 additions & 0 deletions lib/active_record/turntable/connection_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,22 @@ def current_shard=(shard)
current_shard_entry[object_id] = shard
end

# @return connection of current shard
def connection
current_shard.connection
end

# @return connection_pool of current shard
def connection_pool
current_shard.connection_pool
end

# Fix connection to given shard in block
# @param [ActiveRecord::Base, Symbol, ActiveRecord::Turntable::Shard, Numeric, String] shard which you want to fix
# @param shard [ActiveRecord::Base] AR Object
# @param shard [Symbol] shard name symbol that defined in turntable.yml
# @param shard [ActiveRecord::Turntable::Shard] Shard object
# @param shard [String, Numeric] Raw sharding id
def with_shard(shard)
shard = cluster.to_shard(shard)

Expand All @@ -142,6 +150,8 @@ def with_recursive_shards(connection_name, *klasses, &block)
end
end

# Send queries to all shards in this cluster
# @param [Boolean] continue_on_error when a shard raises error, ignore exception and continue
def with_all(continue_on_error = false)
@cluster.shards.values.map do |shard|
begin
Expand All @@ -157,6 +167,8 @@ def with_all(continue_on_error = false)
end
end

# Send queries to master connection and all shards in this cluster
# @param [Boolean] continue_on_error when a shard raises error, ignore exception and continue
def with_master_and_all(continue_on_error = false)
([@cluster.master] + @cluster.shards.values).map do |shard|
begin
Expand Down

0 comments on commit 58afd3e

Please sign in to comment.