Class: Octopus::Proxy
- Inherits:
-
Object
show all
- Defined in:
- lib/octopus/proxy.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(config) ⇒ Proxy
Returns a new instance of Proxy.
4
5
6
7
|
# File 'lib/octopus/proxy.rb', line 4
def initialize(config)
initialize_shards(config)
initialize_replication(config) if !config.nil? && config["replicated"]
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/octopus/proxy.rb', line 109
def method_missing(method, *args, &block)
if should_clean_connection?(method)
conn = select_connection()
self.last_current_shard = self.current_shard
clean_proxy()
conn.send(method, *args, &block)
elsif should_send_queries_to_replicated_databases?(method)
send_queries_to_selected_slave(method, *args, &block)
else
select_connection().send(method, *args, &block)
end
end
|
Instance Attribute Details
#block ⇒ Object
Returns the value of attribute block.
2
3
4
|
# File 'lib/octopus/proxy.rb', line 2
def block
@block
end
|
#current_group ⇒ Object
Returns the value of attribute current_group.
2
3
4
|
# File 'lib/octopus/proxy.rb', line 2
def current_group
@current_group
end
|
#current_model ⇒ Object
Returns the value of attribute current_model.
2
3
4
|
# File 'lib/octopus/proxy.rb', line 2
def current_model
@current_model
end
|
#current_shard ⇒ Object
Returns the value of attribute current_shard.
2
3
4
|
# File 'lib/octopus/proxy.rb', line 2
def current_shard
@current_shard
end
|
#last_current_shard ⇒ Object
Returns the value of attribute last_current_shard.
2
3
4
|
# File 'lib/octopus/proxy.rb', line 2
def last_current_shard
@last_current_shard
end
|
#using_enabled ⇒ Object
Returns the value of attribute using_enabled.
2
3
4
|
# File 'lib/octopus/proxy.rb', line 2
def using_enabled
@using_enabled
end
|
Instance Method Details
#check_schema_migrations(shard) ⇒ Object
103
104
105
106
107
|
# File 'lib/octopus/proxy.rb', line 103
def check_schema_migrations(shard)
if !ActiveRecord::Base.using(shard).connection.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name())
ActiveRecord::Base.using(shard).connection.initialize_schema_migrations_table
end
end
|
#clean_proxy ⇒ Object
96
97
98
99
100
101
|
# File 'lib/octopus/proxy.rb', line 96
def clean_proxy()
@using_enabled = nil
@current_shard = :master
@current_group = nil
@block = false
end
|
#initialize_replication(config) ⇒ Object
38
39
40
41
42
43
|
# File 'lib/octopus/proxy.rb', line 38
def initialize_replication(config)
@replicated = true
@entire_replicated = config["entire_replicated"]
@slaves_list = @shards.keys.map {|sym| sym.to_s}.sort
@slaves_list.delete('master')
end
|
#initialize_shards(config) ⇒ Object
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/octopus/proxy.rb', line 9
def initialize_shards(config)
@shards = {}
@groups = {}
@shards[:master] = ActiveRecord::Base.connection_pool()
@current_shard = :master
if !config.nil?
@entire_sharded = config['entire_sharded']
shards_config = config[Octopus.rails_env()]
end
shards_config ||= []
shards_config.each do |key, value|
if value.has_key?("adapter")
initialize_adapter(value['adapter'])
@shards[key.to_sym] = connection_pool_for(value, "#{value['adapter']}_connection")
else
@groups[key.to_sym] = []
value.each do |k, v|
raise "You have duplicated shard names!" if @shards.has_key?(k.to_sym)
initialize_adapter(v['adapter'])
@shards[k.to_sym] = connection_pool_for(v, "#{v['adapter']}_connection")
@groups[key.to_sym] << k.to_sym
end
end
end
end
|
#run_queries_on_shard(shard, &block) ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/octopus/proxy.rb', line 77
def run_queries_on_shard(shard, &block)
older_shard = self.current_shard
self.block = true
self.current_shard = shard
begin
yield
ensure
self.block = false
self.current_shard = older_shard
end
end
|
#select_connection ⇒ Object
69
70
71
|
# File 'lib/octopus/proxy.rb', line 69
def select_connection()
@shards[shard_name].connection()
end
|
#send_queries_to_multiple_shards(shards, &block) ⇒ Object
90
91
92
93
94
|
# File 'lib/octopus/proxy.rb', line 90
def send_queries_to_multiple_shards(shards, &block)
shards.each do |shard|
self.run_queries_on_shard(shard, &block)
end
end
|
#shard_name ⇒ Object
73
74
75
|
# File 'lib/octopus/proxy.rb', line 73
def shard_name
current_shard.is_a?(Array) ? current_shard.first : current_shard
end
|