Class: XBar::Proxy
Constant Summary
Constants included
from Colors
Colors::BLACK_BACK, Colors::BLACK_TEXT, Colors::BLINK_OFF, Colors::BLINK_ON, Colors::BLUE_BACK, Colors::BLUE_TEXT, Colors::BOLD_OFF, Colors::BOLD_ON, Colors::BROWN_BACK, Colors::BROWN_TEXT, Colors::CYAN_BACK, Colors::CYAN_TEXT, Colors::GRAY_TEXT, Colors::GREEN_BACK, Colors::GREEN_TEXT, Colors::MAGENTA_BACK, Colors::MAGENTA_TEXT, Colors::RED_BACK, Colors::RED_TEXT, Colors::RESET_COLORS, Colors::REVERSE_OFF, Colors::REVERSE_ON, Colors::WHITE_BACK
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Mapper
exports, #register, #reset_config
#app_env, #config, #config_file_name, #config_from_file, #connection_file_name, #environments, #initialize_options, #register, #reset, #shards=, #xbar_env
Constructor Details
#initialize ⇒ Proxy
Returns a new instance of Proxy.
24
25
26
27
28
29
|
# File 'lib/xbar/proxy.rb', line 24
def initialize
puts "Initializing new proxy."
register
reset_shards
clean_proxy
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
# File 'lib/xbar/proxy.rb', line 156
def method_missing(method, *args, &block)
if XBar.debug
puts("\nProxy##{BLUE_TEXT}method_missing#{RESET_COLORS}: " +
"method = #{RED_TEXT}#{method}#{RESET_COLORS}, " +
"current_shard=#{current_shard}, " +
"in_block_scope=#{in_block_scope?}")
end
if method.to_s =~ /insert|select|execute/ && !in_block_scope? shard = @last_current_shard = current_shard
clean_proxy
@shard_list[shard].run_queries(method, *args, &block)
else
select_shard.run_queries(method, *args, &block)
end
end
|
Instance Attribute Details
#current_model ⇒ Object
Setters for these are written by hand below.
18
19
20
|
# File 'lib/xbar/proxy.rb', line 18
def current_model
@current_model
end
|
#current_shard ⇒ Object
Setters for these are written by hand below.
18
19
20
|
# File 'lib/xbar/proxy.rb', line 18
def current_shard
@current_shard
end
|
#last_current_shard ⇒ Object
15
16
17
|
# File 'lib/xbar/proxy.rb', line 15
def last_current_shard
@last_current_shard
end
|
#shard_list ⇒ Object
Returns the value of attribute shard_list.
20
21
22
|
# File 'lib/xbar/proxy.rb', line 20
def shard_list
@shard_list
end
|
#slave_read_allowed ⇒ Object
Returns the value of attribute slave_read_allowed.
22
23
24
|
# File 'lib/xbar/proxy.rb', line 22
def slave_read_allowed
@slave_read_allowed
end
|
Instance Method Details
#check_schema_migrations(shard_name) ⇒ Object
140
141
142
|
# File 'lib/xbar/proxy.rb', line 140
def check_schema_migrations(shard_name)
@shard_list[shard_name].check_schema_migrations
end
|
#clean_proxy ⇒ Object
38
39
40
41
42
43
44
45
|
# File 'lib/xbar/proxy.rb', line 38
def clean_proxy
if XBar.debug
puts "Proxy##{BLUE_TEXT}clean_proxy=#{RESET_COLORS}: " +
"current shard = #{@current_shard}"
end
@current_shard = :master
clear_block_scope
end
|
#clear_block_scope ⇒ Object
101
102
103
104
|
# File 'lib/xbar/proxy.rb', line 101
def clear_block_scope
@in_block = @old_in_block = false
@depth = 0
end
|
#connection_pool ⇒ Object
177
178
179
180
181
|
# File 'lib/xbar/proxy.rb', line 177
def connection_pool
cp = shards[current_shard].first
cp.automatic_reconnect = true if XBar.rails31?
cp
end
|
#enter_block_scope ⇒ Object
85
86
87
88
89
|
# File 'lib/xbar/proxy.rb', line 85
def enter_block_scope
@old_in_block = @in_block
@depth += 1
@in_block = true
end
|
#enter_statistics(shard_name, config, method) ⇒ Object
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
# File 'lib/xbar/proxy.rb', line 183
def enter_statistics(shard_name, config, method)
return unless XBar.collect_stats?
saved_current_model = current_model
if UsageStatistics.connection.kind_of?(XBar::Proxy)
connection_pool = UsageStatistics.connection.shards[:master][0]
connection_pool.automatic_reconnect = true
connection = connection_pool.connection
return unless connection.table_exists? :usage_statistics
=begin
params = {
shard_name: shard_name,
method: method.to_s,
adapter: config[:adapter],
username: config[:username],
thread_id: Thread.current.object_id.to_s,
port: (config[:port] || "default"),
host: config[:host],
database_name: config[:database]}
insert(connection, params) -- alternatively
=end
sql = "INSERT INTO usage_statistics " +
"(shard_name, method, adapter, username, thread_id, port, host, database_name) " +
"VALUES (\'#{shard_name}\', \'#{method.to_s}\', \'#{config[:adapter]}\', " +
"\'#{config[:username]}\', \'#{Thread.current.object_id.to_s}\', " +
"#{config[:port] || "default"}, \'#{config[:host]}\', \'#{config[:database]}\')"
connection.execute(sql)
self.current_model = saved_current_model
end
end
|
#in_block_scope? ⇒ Boolean
97
98
99
|
# File 'lib/xbar/proxy.rb', line 97
def in_block_scope?
@in_block
end
|
#leave_block_scope ⇒ Object
91
92
93
94
95
|
# File 'lib/xbar/proxy.rb', line 91
def leave_block_scope
@in_block = @old_in_block
@old_in_block = false
@depth -= 1
end
|
#quote_table_name(table_name) ⇒ Object
152
153
154
|
# File 'lib/xbar/proxy.rb', line 152
def quote_table_name(table_name)
select_shard.quote_table_name(table_name)
end
|
#reset_proxy ⇒ Object
31
32
33
34
35
36
|
# File 'lib/xbar/proxy.rb', line 31
def reset_proxy
@current_shard = :master
puts ">>> reset_proxy: #{current_shard}" if XBar.debug
clear_block_scope
reset_shards
end
|
#respond_to?(method, include_private = false) ⇒ Boolean
173
174
175
|
# File 'lib/xbar/proxy.rb', line 173
def respond_to?(method, include_private = false)
super || current_shard.respond_to?(method, include_private)
end
|
#run_queries_on_shard(shard_name, use_scope = true) ⇒ Object
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/xbar/proxy.rb', line 114
def run_queries_on_shard(shard_name, use_scope = true)
older_shard = current_shard
enter_block_scope if use_scope
self.current_shard = shard_name
if XBar.debug
puts "Proxy##{BLUE_TEXT}run_queries_on_shard#{RESET_COLORS}: " +
"current shard = #{current_shard}, use_scope = #{use_scope}, " +
"previous shard = #{older_shard}"
end
result = yield
ensure
if XBar.debug
puts "Proxy##{BLUE_TEXT}run_queries_on_shard#{RESET_COLORS}: " +
"restoring previous shard = #{older_shard}"
end
leave_block_scope if use_scope
self.current_shard = older_shard
end
|
#schema_cache ⇒ Object
148
149
150
|
# File 'lib/xbar/proxy.rb', line 148
def schema_cache
select_shard.schema_cache
end
|
#select_shard ⇒ Object
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/xbar/proxy.rb', line 64
def select_shard
if current_shard.kind_of? Array
shard = current_shard.first
if current_shard.size != 1
puts "WARNING: selecting only first shard from array"
end
else
shard = current_shard
end
unless @shard_list[shard]
puts "Shard not found: current_shard = #{shard}, @shard_list = #{@shard_list.keys}"
end
@shard_list[shard]
end
|
#send_queries_to_multiple_shards(shard_names, &block) ⇒ Object
133
134
135
136
137
138
|
# File 'lib/xbar/proxy.rb', line 133
def send_queries_to_multiple_shards(shard_names, &block)
shard_names = Array(shard_names)
shard_names.each do |name|
run_queries_on_shard(name, &block)
end
end
|
#should_clean_table_name? ⇒ Boolean
106
107
108
|
# File 'lib/xbar/proxy.rb', line 106
def should_clean_table_name?
adapters.size > 1
end
|
#transaction(options = {}, &block) ⇒ Object
144
145
146
|
# File 'lib/xbar/proxy.rb', line 144
def transaction(options = {}, &block)
select_shard.transaction(options, &block)
end
|
#verify_connection ⇒ Object
110
111
112
|
# File 'lib/xbar/proxy.rb', line 110
def verify_connection
XBar::Mapper.options[:verify_connection]
end
|