Skip to content
This repository was archived by the owner on Aug 26, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
c706d92
Add ar:create task for padrino.
hanabokuro Jan 25, 2014
197685d
add generator for turntable with padrino.
hanabokuro Jan 25, 2014
2cb46b8
Add ActiveRecord::Turntable::RackupFramework to abstract rails, padri…
hanabokuro Jan 25, 2014
bbc920b
fix generator
hanabokuro Jan 25, 2014
9f5b220
Add ActiveRecord::Turntable::Algorithm::Modulo
hanabokuro Jan 25, 2014
5be8d15
change class name to ModuloAlgorithm.
hanabokuro Jan 25, 2014
2830656
get config value with string or symbol.
hanabokuro Jan 25, 2014
a47a874
commnet out including Seqncener for adding default_sequence_name method.
Jan 27, 2014
ad6170d
uncomment out to adding default database.
Jan 28, 2014
c9f7f71
.
Jan 30, 2014
3e9a15b
fix error message.
Jan 30, 2014
a3452d1
Fix order query in with_all
hirocaster Jan 31, 2014
e4ef26b
Merge pull request #1 from hirocaster/fix-with_all
hanabokuro Jan 31, 2014
80a7d2a
Fix order query in with_all
hirocaster Jan 31, 2014
2e245fb
Merge pull request #2 from hirocaster/fix-with_all2
hanabokuro Jan 31, 2014
94dee63
Increase sequence table performance.
Apr 15, 2014
0157a72
Restore default ENGINE to InnoDB.
Apr 16, 2014
74fd3be
Get current id with dummy update sql. because select can't work under…
Apr 21, 2014
86e075b
Merge pull request #4 from hanabokuro/increase-sequence-table-perform…
hanabokuro May 12, 2014
aff5c50
Fix loggin connection info, not use turntable case
hirocaster Aug 26, 2014
a1c6e38
Merge pull request #1 from monsterstrike/fix-loggin-connection-info
w1mvy Aug 26, 2014
7ef35b1
migrate only target sequencer
satosxi Nov 2, 2015
91a30e1
refactor migration.rb
satosxi Nov 4, 2015
d44e2fc
move defintation line of seqs variable
satosxi Nov 9, 2015
cc07fe4
fix validate
satosxi Nov 10, 2015
4a59307
move defintation line of shards variable
satosxi Nov 10, 2015
ab16578
Merge pull request #2 from monsterstrike/migrate-only-target-sequencer
hirocaster Nov 10, 2015
1acb18e
add offset option for next_sequence_value
mtokioka Jan 4, 2016
677fecb
Merge pull request #3 from monsterstrike/sequence_with_offset
satosxi Jan 4, 2016
912676c
for incompatibility issue with newrelic
phamvanhungmixi Jul 1, 2016
c834249
change log_without_newrelic_instrumentation method to log method
phamvanhungmixi Jul 1, 2016
4091477
use alias_method_chain
phamvanhungmixi Jul 4, 2016
164ce9f
use method_defined?
phamvanhungmixi Jul 4, 2016
8868047
Merge pull request #4 from phamvanhungmixi/tiepadrino
w1mvy Jul 4, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/active_record/turntable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ module ActiveRecord::Turntable
include Base
end

RackupFramework = Rails if defined?(Rails);
RackupFramework = Padrino if defined?(Padrino);

module ClassMethods
DEFAULT_PATH = File.dirname(File.dirname(__FILE__))

def turntable_config_file
@@turntable_config_file ||=
File.join(defined?(::Rails) ?
::Rails.root.to_s : DEFAULT_PATH, 'config/turntable.yml')
File.join(defined?(ActiveRecord::Turntable::RackupFramework) ?
ActiveRecord::Turntable::RackupFramework.root.to_s : DEFAULT_PATH, 'config/turntable.yml')
end

def turntable_config_file=(filename)
Expand All @@ -55,4 +58,5 @@ def turntable_config
end

require "active_record/turntable/railtie" if defined?(Rails)
require "active_record/turntable/padrinotie" if defined?(Padrino)
end
1 change: 1 addition & 0 deletions lib/active_record/turntable/active_record_ext.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
module ActiveRecord::Turntable
module ActiveRecordExt
extend ActiveSupport::Concern
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ def log(sql, name = "SQL", binds = [])
raise exception
end
end

alias_method_chain :log, :newrelic_instrumentation if method_defined?(:log_with_newrelic_instrumentation)

end

def turntable_shard_name=(name)
Expand Down
17 changes: 12 additions & 5 deletions lib/active_record/turntable/active_record_ext/log_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ def sql(event)
return if 'SCHEMA' == payload[:name]

name = '%s (%.1fms)' % [payload[:name], event.duration]
shard = '[Shard: %s]' % (event.payload[:turntable_shard_name] ? event.payload[:turntable_shard_name] : nil)

connection = if event.payload[:turntable_shard_name]
'[Shard: %s]' % event.payload[:turntable_shard_name]
elsif event.payload[:connection_name]
'[%s]' % event.payload[:connection_name]
else
'[unknown]'
end

sql = payload[:sql].squeeze(' ')
binds = nil

Expand All @@ -47,18 +55,17 @@ def sql(event)

if odd?
name = color(name, ActiveRecord::LogSubscriber::CYAN, true)
shard = color(shard, ActiveRecord::LogSubscriber::CYAN, true)
connection = color(connection, ActiveRecord::LogSubscriber::CYAN, true)
sql = color(sql, nil, true)
else
name = color(name, ActiveRecord::LogSubscriber::MAGENTA, true)
shard = color(shard, ActiveRecord::LogSubscriber::MAGENTA, true)
connection = color(connection, ActiveRecord::LogSubscriber::MAGENTA, true)
end

debug " #{name} #{shard} #{sql}#{binds}"
debug " #{name} #{connection} #{sql}#{binds}"
end
end
end
end
end
end

1 change: 1 addition & 0 deletions lib/active_record/turntable/algorithm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ module Algorithm
autoload :Base, "active_record/turntable/algorithm/base"
autoload :RangeAlgorithm, "active_record/turntable/algorithm/range_algorithm"
autoload :RangeBsearchAlgorithm, "active_record/turntable/algorithm/range_bsearch_algorithm"
autoload :ModuloAlgorithm, "active_record/turntable/algorithm/modulo_algorithm"
end
end
14 changes: 14 additions & 0 deletions lib/active_record/turntable/algorithm/modulo_algorithm.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# -*- coding: utf-8 -*-
module ActiveRecord::Turntable::Algorithm
class ModuloAlgorithm < Base
def initialize(config)
@config = config
end

def calculate(key)
@config["shards"][key % @config["shards"].size]["connection"]
rescue
raise ActiveRecord::Turntable::CannotSpecifyShardError, "cannot specify shard for key:#{key}"
end
end
end
2 changes: 1 addition & 1 deletion lib/active_record/turntable/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def recursive_transaction(pools, options, &block)
end

def force_connect_all_shards!
conf = configurations[Rails.env]
conf = configurations[ActiveRecord::Turntable::RackupFramework.env]
shards = conf["shards"]
shards = shards.merge(conf["seq"]) if conf["seq"]
shards.each do |name, config|
Expand Down
2 changes: 1 addition & 1 deletion lib/active_record/turntable/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def [](key)
@config[key]
end

def self.load!(config_file, env = (defined?(Rails) ? Rails.env : 'development'))
def self.load!(config_file, env = (defined?(ActiveRecord::Turntable::RackupFramework) ? ActiveRecord::Turntable::RackupFramework.env : 'development')) # FIXME
instance.load!(config_file, env)
end

Expand Down
49 changes: 29 additions & 20 deletions lib/active_record/turntable/migration.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
module ActiveRecord::Turntable::Migration
extend ActiveSupport::Concern

Expand All @@ -14,6 +15,7 @@ def announce_with_turntable(message)
end
base.class_eval do
class_inheritable_accessor :target_shards
class_inheritable_accessor :target_seqs
end
::ActiveRecord::ConnectionAdapters::AbstractAdapter.send(:include, SchemaStatementsExt)
end
Expand All @@ -22,6 +24,7 @@ def announce_with_turntable(message)
included do
extend ShardDefinition
class_attribute :target_shards
class_attribute :target_seqs
def announce_with_turntable(message)
announce_without_turntable("#{message} - #{get_current_shard}")
end
Expand All @@ -36,18 +39,17 @@ def announce_with_turntable(message)
module ShardDefinition
def clusters(*cluster_names)
config = ActiveRecord::Base.turntable_config
(self.target_shards ||= []) <<
if cluster_names.first == :all
config['clusters'].map do |name, cluster_conf|
cluster_conf["shards"].map {|shard| shard["connection"]}
end
else
cluster_names.map do |cluster_name|
config['clusters'][cluster_name]["shards"].map do |shard|
shard["connection"]
end
end
if cluster_names.first == :all
config['clusters'].map do |name, cluster_conf|
(self.target_shards ||= []) << cluster_conf["shards"].map { |shard| shard["connection"] }
(self.target_seqs ||= []) << cluster_conf["seq"]["connection"]
end
else
cluster_names.map do |cluster_name|
(self.target_shards ||= []) << config['clusters'][cluster_name]["shards"].map { |shard| shard["connection"] }
(self.target_seqs ||= []) << config['clusters'][cluster_name]["seq"]["connection"]
end
end
end

def shards(*connection_names)
Expand All @@ -62,19 +64,23 @@ def get_current_shard
def migrate_with_turntable(direction)
config = ActiveRecord::Base.configurations
@@current_shard = nil
shards = (self.class.target_shards||=[]).flatten.uniq.compact
if self.class.target_shards.blank?
if self.class.target_shards.blank? || self.class.target_seqs.blank?
return migrate_without_turntable(direction)
end

shards = (self.class.target_shards||=[]).flatten.uniq.compact
shards_conf = shards.map do |shard|
config[Rails.env||"development"]["shards"][shard]
config[ActiveRecord::Turntable::RackupFramework.env||"development"]["shards"][shard]
end
seqs = config[Rails.env||"development"]["seq"]
shards_conf += seqs.values
shards_conf << config[Rails.env||"development"]

seqs = (self.class.target_seqs||=[]).flatten.uniq.compact
seqs_conf = config[ActiveRecord::Turntable::RackupFramework.env||"development"]["seq"].select { |key, val| seqs.include?(key) }
shards_conf += seqs_conf.values

# SHOW FULL FIELDS FROM `users` を実行してテーブルの情報を取得するためにデフォルトのデータベースも追加する
shards_conf << config[ActiveRecord::Turntable::RackupFramework.env||"development"]
shards_conf.each_with_index do |conf, idx|
@@current_shard = (shards[idx] || seqs.keys[idx - shards.size] || "master")
@@current_shard = (shards[idx] || seqs_conf.keys[idx - shards.size] || "master")
ActiveRecord::Base.establish_connection(conf)
if !ActiveRecord::Base.connection.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name())
ActiveRecord::Base.connection.initialize_schema_migrations_table
Expand All @@ -85,11 +91,14 @@ def migrate_with_turntable(direction)

module SchemaStatementsExt
def create_sequence_for(table_name, options = { })
options = options.merge(:id => false)

# TODO: pkname should be pulled from table definitions
pkname = "id"
sequence_table_name = ActiveRecord::Turntable::Sequencer.sequence_name(table_name, "id")
create_table(sequence_table_name, options)
execute "ALTER TABLE #{quote_table_name(sequence_table_name)} MODIFY id bigint(20) DEFAULT NULL auto_increment NOT NULL;"
create_table(sequence_table_name, options) do |t|
t.integer :id, :limit => 8
end
execute "INSERT INTO #{quote_table_name(sequence_table_name)} (`id`) VALUES (0)"
end

Expand Down
8 changes: 4 additions & 4 deletions lib/active_record/turntable/mixer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def build_fader(method_name, query, *args, &block)
method, query, *args, &block)
end
rescue Exception => err
logger.warn { "[ActiveRecord::Turntable][BUG] Error on Building Fader: #{binded_query}, on_method: #{method_name}" }
logger.warn { "[ActiveRecord::Turntable][BUG] Error on Building Fader: #{binded_query}, on_method: #{method_name}, err: #{err}" }
raise err
end

Expand Down Expand Up @@ -139,8 +139,8 @@ def build_select_fader(tree, method, query, *args, &block)
return Fader::SpecifiedShard.new(@proxy,
{ @proxy.cluster.select_shard(shard_keys.first) => query },
method, query, *args, &block)
elsif tree.group_by or tree.order_by or tree.limit.try(:value).to_i > 1
raise CannotSpecifyShardError, "cannot specify shard for query: #{binded_query}"
elsif @proxy.current_shard.blank? and (tree.group_by or tree.order_by or tree.limit.try(:value).to_i > 1)
raise CannotSpecifyShardError, "cannot specify shard for query: #{query.to_sql}"
elsif shard_keys.present?
if SQLTree::Node::SelectDeclaration === tree.select.first and
SQLTree::Node::CountAggregrate === tree.select.first.expression
Expand Down Expand Up @@ -168,7 +168,7 @@ def build_select_fader(tree, method, query, *args, &block)
method, query, *args, &block
)
else
raise CannotSpecifyShardError, "cannot specify shard for query: #{binded_query}"
raise CannotSpecifyShardError, "cannot specify shard for query: #{query.to_sql}"
end
end
end
Expand Down
17 changes: 17 additions & 0 deletions lib/active_record/turntable/padrinotie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
module ActiveRecord::Turntable
class Padrinotie
Padrino::Tasks.files << File.dirname(__FILE__) + "/padrinoties/databases.rb"

# padrino loading hook
Padrino.before_load do
ActiveRecord::Base.send(:include, ActiveRecord::Turntable)

require 'generators/padrino/turntable/install_generators'
end
# # Swap QueryCache Middleware
# initializer "turntable.swap_query_cache_middleware" do |app|
# app.middleware.swap ActiveRecord::QueryCache, ActiveRecord::Turntable::Rack::QueryCache
# end
end
end
Loading