Class: XBar::Shard

Inherits:
Object
  • Object
show all
Includes:
Colors
Defined in:
lib/xbar/shard.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

Constructor Details

#initialize(proxy, name, master, slaves) ⇒ Shard

Returns a new instance of Shard.



15
16
17
18
19
20
21
# File 'lib/xbar/shard.rb', line 15

def initialize(proxy, name, master, slaves)
  @master = master # a connection pool
  @shard_name = name
  @slaves = slaves # an array of connection pools
  @proxy = proxy # our parent proxy
  @slave_index = 0
end

Instance Attribute Details

#masterObject (readonly)

Returns the value of attribute master.



13
14
15
# File 'lib/xbar/shard.rb', line 13

def master
  @master
end

#proxyObject (readonly)

Returns the value of attribute proxy.



13
14
15
# File 'lib/xbar/shard.rb', line 13

def proxy
  @proxy
end

#shard_nameObject (readonly)

Returns the value of attribute shard_name.



13
14
15
# File 'lib/xbar/shard.rb', line 13

def shard_name
  @shard_name
end

#slavesObject (readonly)

Returns the value of attribute slaves.



13
14
15
# File 'lib/xbar/shard.rb', line 13

def slaves
  @slaves
end

Instance Method Details

#open_transactionsObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/xbar/shard.rb', line 61

def open_transactions
  tr_count = 0
  @master.connections.inject(0) {|s, c| s + c.open_transactions}
  @master.connections.each do |c|
    val = c.instance_variable_get(:@_current_transaction_records) # nil or array
    tr_count += val.size if val
  end
  @slaves.each do |s|
    s.connections.each do |c|
      val = c.instance_variable_get(:@_current_transaction_records) # nil or array
      tr_count += val.size if val
    end
  end
  tr_count
end

#quote_table_name(table_name) ⇒ Object



57
58
59
# File 'lib/xbar/shard.rb', line 57

def quote_table_name(table_name)
  master.connection.quote_table_name(table_name)
end

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



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

def run_queries(method, *args, &block)
  if XBar.debug
    puts("Shard##{BLUE_TEXT}run_queries#{RESET_COLORS}: " + 
      "method = #{RED_TEXT}#{method}#{RESET_COLORS}, " +
      "shard= #{shard_name}, slave_read=#{!!proxy.slave_read_allowed}, " +
      "block_scope = #{in_block_scope?}")
  end
  if slave_read_allowed(method)
    proxy.slave_read_allowed = false # just once
    # OK to send the query to a slave
    run_queries_on_slave(method, *args, &block)
  else
    # Use the master.
    run_queries_on_replica(master, method, *args, &block)
  end
end

#schema_cacheObject



52
53
54
55
# File 'lib/xbar/shard.rb', line 52

def schema_cache
  # defined by attr_reader in AbstractAdapter class.
  master.connection.schema_cache
end

#transaction(options, &block) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/xbar/shard.rb', line 40

def transaction(options, &block)
  if XBar.debug
    config = master.spec.config
    puts("\nShard##{BLUE_TEXT}transaction#{RESET_COLORS}: " +
      "shard_name=master, shard=#{shard_name}, " +
      "Host=#{config[:host]}, Port=#{config[:port]}, Database=#{config[:database]}")
  end
  proxy.enter_statistics(@shard_name, master.spec.config, 'transaction')
  prepare_connection_pool(master)
  master.connection.transaction(options, &block)
end