Class: Octopus::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/octopus/proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = Octopus.config) ⇒ Proxy

Returns a new instance of Proxy.



19
20
21
# File 'lib/octopus/proxy.rb', line 19

def initialize(config = Octopus.config)
  self.proxy_config = Octopus::ProxyConfig.new(config)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

def method_missing(method, *args, &block)
  legacy_method_missing_logic(method, *args, &block)
end

Instance Attribute Details

#proxy_configObject

Returns the value of attribute proxy_config.



7
8
9
# File 'lib/octopus/proxy.rb', line 7

def proxy_config
  @proxy_config
end

Instance Method Details

#check_schema_migrations(shard) ⇒ Object



113
114
115
116
117
# File 'lib/octopus/proxy.rb', line 113

def check_schema_migrations(shard)
  OctopusModel.using(shard).connection.table_exists?(
    ActiveRecord::Migrator.schema_migrations_table_name,
  ) || OctopusModel.using(shard).connection.initialize_schema_migrations_table
end

#clean_connection_proxyObject



106
107
108
109
110
111
# File 'lib/octopus/proxy.rb', line 106

def clean_connection_proxy
  self.current_shard = Octopus.master_shard
  self.current_model = nil
  self.current_group = nil
  self.block = nil
end

#clear_active_connections!Object



154
155
156
# File 'lib/octopus/proxy.rb', line 154

def clear_active_connections!
  with_each_healthy_shard(&:release_connection)
end

#clear_all_connections!Object



158
159
160
# File 'lib/octopus/proxy.rb', line 158

def clear_all_connections!
  with_each_healthy_shard(&:disconnect!)
end

#clear_query_cacheObject



150
151
152
# File 'lib/octopus/proxy.rb', line 150

def clear_query_cache
  with_each_healthy_shard { |v| v.connected? && safe_connection(v).clear_query_cache }
end

#connected?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/octopus/proxy.rb', line 162

def connected?
  shards.any? { |_k, v| v.connected? }
end

#connection_poolObject



137
138
139
# File 'lib/octopus/proxy.rb', line 137

def connection_pool
  shards[current_shard]
end

#delete(*args, &block) ⇒ Object



54
55
56
# File 'lib/octopus/proxy.rb', line 54

def delete(*args, &block)
  legacy_method_missing_logic('delete', *args, &block)
end

#disable_query_cache!Object



146
147
148
# File 'lib/octopus/proxy.rb', line 146

def disable_query_cache!
  with_each_healthy_shard { |v| v.connected? && safe_connection(v).disable_query_cache! }
end

#enable_query_cache!Object



141
142
143
144
# File 'lib/octopus/proxy.rb', line 141

def enable_query_cache!
  clear_query_cache
  with_each_healthy_shard { |v| v.connected? && safe_connection(v).enable_query_cache! }
end

#execute(sql, name = nil) ⇒ Object



36
37
38
39
40
# File 'lib/octopus/proxy.rb', line 36

def execute(sql, name = nil)
  conn = select_connection
  clean_connection_proxy
  conn.execute(sql, name)
end

#insert(arel, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = []) ⇒ Object



42
43
44
45
46
# File 'lib/octopus/proxy.rb', line 42

def insert(arel, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = [])
  conn = select_connection
  clean_connection_proxy
  conn.insert(arel, name, pk, id_value, sequence_name, binds)
end

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

Returns:

  • (Boolean)


133
134
135
# File 'lib/octopus/proxy.rb', line 133

def respond_to?(method, include_private = false)
  super || select_connection.respond_to?(method, include_private)
end

#run_queries_on_shard(shard, &_block) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/octopus/proxy.rb', line 82

def run_queries_on_shard(shard, &_block)
  keeping_connection_proxy(shard) do
    using_shard(shard) do
      yield
    end
  end
end

#safe_connection(connection_pool) ⇒ Object

Rails 3.1 sets automatic_reconnect to false when it removes connection pool. Octopus can potentially retain a reference to a closed connection pool. Previously, that would work since the pool would just reconnect, but in Rails 3.1 the flag prevents this.



70
71
72
73
74
75
76
# File 'lib/octopus/proxy.rb', line 70

def safe_connection(connection_pool)
  connection_pool.automatic_reconnect ||= true
  if !connection_pool.connected? && shards[Octopus.master_shard].connection.query_cache_enabled
    connection_pool.connection.enable_query_cache!
  end
  connection_pool.connection
end

#select_all(*args, &block) ⇒ Object



58
59
60
# File 'lib/octopus/proxy.rb', line 58

def select_all(*args, &block)
  legacy_method_missing_logic('select_all', *args, &block)
end

#select_connectionObject



78
79
80
# File 'lib/octopus/proxy.rb', line 78

def select_connection
  safe_connection(shards[shard_name])
end

#select_value(*args, &block) ⇒ Object



62
63
64
# File 'lib/octopus/proxy.rb', line 62

def select_value(*args, &block)
  legacy_method_missing_logic('select_value', *args, &block)
end

#send_queries_to_all_shards(&block) ⇒ Object



102
103
104
# File 'lib/octopus/proxy.rb', line 102

def send_queries_to_all_shards(&block)
  send_queries_to_multiple_shards(shard_names.uniq { |shard_name| shards[shard_name] }, &block)
end

#send_queries_to_group(group, &block) ⇒ Object



96
97
98
99
100
# File 'lib/octopus/proxy.rb', line 96

def send_queries_to_group(group, &block)
  using_group(group) do
    send_queries_to_multiple_shards(shards_for_group(group), &block)
  end
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.map do |shard|
    run_queries_on_shard(shard, &block)
  end
end

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



170
171
172
# File 'lib/octopus/proxy.rb', line 170

def send_queries_to_shard_slave_group(method, *args, &block)
  send_queries_to_balancer(shards_slave_groups[current_shard][current_slave_group], method, *args, &block)
end

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



178
179
180
# File 'lib/octopus/proxy.rb', line 178

def send_queries_to_slave_group(method, *args, &block)
  send_queries_to_balancer(slave_groups[current_slave_group], method, *args, &block)
end

#should_send_queries_to_shard_slave_group?(method) ⇒ Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/octopus/proxy.rb', line 166

def should_send_queries_to_shard_slave_group?(method)
  should_use_slaves_for_method?(method) && shards_slave_groups.try(:[], current_shard).try(:[], current_slave_group).present?
end

#should_send_queries_to_slave_group?(method) ⇒ Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/octopus/proxy.rb', line 174

def should_send_queries_to_slave_group?(method)
  should_use_slaves_for_method?(method) && slave_groups.try(:[], current_slave_group).present?
end

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



119
120
121
122
123
124
125
126
127
# File 'lib/octopus/proxy.rb', line 119

def transaction(options = {}, &block)
  if !sharded && current_model_replicated?
    run_queries_on_shard(Octopus.master_shard) do
      select_connection.transaction(options, &block)
    end
  else
    select_connection.transaction(options, &block)
  end
end

#update(arel, name = nil, binds = []) ⇒ Object



48
49
50
51
52
# File 'lib/octopus/proxy.rb', line 48

def update(arel, name = nil, binds = [])
  conn = select_connection
  clean_connection_proxy
  conn.update(arel, name, binds)
end