Class: XBar::Proxy

Inherits:
Object
  • Object
show all
Includes:
Colors, Mapper
Defined in:
lib/xbar/proxy.rb

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

Methods included from Mapper::ClassMethods

#app_env, #config, #config_file_name, #config_from_file, #connection_file_name, #disconnect_all!, #environments, #initialize_options, #register, #reset, #shards=, #unregister, #xbar_env

Constructor Details

#initializeProxy

Returns a new instance of Proxy.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/xbar/proxy.rb', line 24

def initialize
  if XBar.debug
    puts "Proxy##{ BLUE_TEXT}{initialize}#{RESET_COLORS}: Initializing new proxy."
  end
  @reset = false
  @pause = false
  register
  reset_shards
  clean_proxy
  @adapters = XBar::Mapper.adapters
  @mylock = Mutex.new
  @pause_lock = Mutex.new
  @pause_cv = ConditionVariable.new
  @paused = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/xbar/proxy.rb', line 152

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? # should clean connection
    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

#adaptersObject (readonly)

Returns the value of attribute adapters.



20
21
22
# File 'lib/xbar/proxy.rb', line 20

def adapters
  @adapters
end

#current_modelObject

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_shardObject

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_shardObject (readonly)

No setter method.



15
16
17
# File 'lib/xbar/proxy.rb', line 15

def last_current_shard
  @last_current_shard
end

#shard_listObject (readonly)

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_allowedObject

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_for_pauseObject



230
231
232
233
234
235
236
237
238
# File 'lib/xbar/proxy.rb', line 230

def check_for_pause
  if @pause && (open_transactions == 0)
    @pause_lock.synchronize do
      @pause = true
      @pause_cv.wait(@pause_lock)
      @pause = false
    end
 end
end

#check_for_resetObject



269
270
271
272
273
# File 'lib/xbar/proxy.rb', line 269

def check_for_reset
  if @reset && (open_transactions == 0)
    do_reset
  end
end

#check_schema_migrations(shard_name) ⇒ Object



129
130
131
# File 'lib/xbar/proxy.rb', line 129

def check_schema_migrations(shard_name)
  @shard_list[shard_name].check_schema_migrations
end

#clean_proxyObject

Called from migration.



41
42
43
44
45
46
47
48
# File 'lib/xbar/proxy.rb', line 41

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_scopeObject



88
89
90
91
# File 'lib/xbar/proxy.rb', line 88

def clear_block_scope
  @in_block = @old_in_block = false
  @depth = 0
end

#clear_cache!Object



147
148
149
150
# File 'lib/xbar/proxy.rb', line 147

def clear_cache!
  check_for_reset    
  select_shard.run_queries(:clear_cache!)
end

#connection_poolObject



172
173
174
175
176
177
178
# File 'lib/xbar/proxy.rb', line 172

def connection_pool
  # Or we could make a case for selecting master replica from
  # the master shard, rather than the current shard. XXX.
  cp = select_shard.master
  cp.automatic_reconnect = true if XBar.rails31?
  cp
end

#do_resetObject



259
260
261
262
263
264
265
266
267
# File 'lib/xbar/proxy.rb', line 259

def do_reset
  reset_shards
  @adapters = adapters
  @mylock.synchronize do
    @reset = false
    clean_proxy if @hard_reset
    @hard_reset = false
  end
end

#enter_block_scopeObject



72
73
74
75
76
# File 'lib/xbar/proxy.rb', line 72

def enter_block_scope
  @old_in_block = @in_block
  @depth += 1
  @in_block = true
end

#enter_statistics(shard_name, config, method) ⇒ Object



180
181
182
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
# File 'lib/xbar/proxy.rb', line 180

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

    # We have an AbstractAdapter.  We must insert statistics using this
    # adapter, not the Proxy, because otherwise the insertion triggers
    # 'enter_statistics' again and we would be in an infinite loop.

=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

Returns:

  • (Boolean)


84
85
86
# File 'lib/xbar/proxy.rb', line 84

def in_block_scope?
  @in_block
end

#leave_block_scopeObject



78
79
80
81
82
# File 'lib/xbar/proxy.rb', line 78

def leave_block_scope
  @in_block = @old_in_block
  @old_in_block = false
  @depth -= 1
end

#paused?Boolean

Returns:

  • (Boolean)


226
227
228
# File 'lib/xbar/proxy.rb', line 226

def paused?
  @pause
end

#quote_table_name(table_name) ⇒ Object



143
144
145
# File 'lib/xbar/proxy.rb', line 143

def quote_table_name(table_name)
  select_shard.quote_table_name(table_name)
end

#request_pauseObject



220
221
222
223
224
# File 'lib/xbar/proxy.rb', line 220

def request_pause
  @pause_lock.synchronize do
    @pause = true
  end
end

#request_reset(opts = {}) ⇒ Object



244
245
246
247
248
249
250
251
# File 'lib/xbar/proxy.rb', line 244

def request_reset(opts = {})
  @mylock.synchronize do
    @reset = true
    if opts[:hard_reset]
      @hard_reset = true
    end
  end
end

#reset_complete?Boolean

Returns:

  • (Boolean)


253
254
255
256
257
# File 'lib/xbar/proxy.rb', line 253

def reset_complete?
  @mylock.synchronize do
    @reset == false
  end
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/xbar/proxy.rb', line 168

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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/xbar/proxy.rb', line 101

def run_queries_on_shard(shard_name, use_scope = true)
  check_for_reset
  check_for_pause
  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_cacheObject



139
140
141
# File 'lib/xbar/proxy.rb', line 139

def schema_cache
  select_shard.schema_cache
end

#send_queries_to_multiple_shards(shard_names, &block) ⇒ Object



122
123
124
125
126
127
# File 'lib/xbar/proxy.rb', line 122

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

Returns:

  • (Boolean)


93
94
95
# File 'lib/xbar/proxy.rb', line 93

def should_clean_table_name?
  @adapters.size > 1
end

#transaction(options = {}, &block) ⇒ Object



133
134
135
136
137
# File 'lib/xbar/proxy.rb', line 133

def transaction(options = {}, &block)
  check_for_reset
  check_for_pause
  select_shard.transaction(options, &block)
end

#unpauseObject



240
241
242
# File 'lib/xbar/proxy.rb', line 240

def unpause
  @pause_cv.signal
end

#verify_connectionObject



97
98
99
# File 'lib/xbar/proxy.rb', line 97

def verify_connection
  XBar::Mapper.options[:verify_connection]
end