module Octopus

Implementation courtesy of db-charmer.

The round-robin load balancing of slaves belonging to the same shard. It is a pool that contains slaves which queries are distributed to.

Implementation courtesy of db-charmer.

query cache methods are moved to ConnectionPool for Rails >= 5.0

Adds current_shard as an attribute; provide a default implementation of set_current_shard which considers only the current ActiveRecord::Base.connection_proxy

Constants

VERSION

Attributes

logger[W]

Public Class Methods

atleast_rails51?() click to toggle source
# File lib/octopus.rb, line 121
def self.atleast_rails51?
  ActiveRecord::VERSION::MAJOR > 5 || (ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR >= 1)
end
config() click to toggle source
# File lib/octopus.rb, line 17
def self.config
  @config ||= begin
    file_name = File.join(Octopus.directory, 'config/shards.yml').to_s

    if File.exist?(file_name) || File.symlink?(file_name)
      config ||= HashWithIndifferentAccess.new(YAML.load(ERB.new(File.read(file_name)).result))[Octopus.env]
    else
      config ||= HashWithIndifferentAccess.new
    end

    config
  end
end
directory() click to toggle source

Returns the Rails.root_to_s when you are using rails Running the current directory in a generic Ruby process

# File lib/octopus.rb, line 60
def self.directory
  @directory ||= defined?(::Rails.root) ? Rails.root.to_s : Dir.pwd
end
enabled?() click to toggle source

Public: Whether or not Octopus is configured and should hook into the current environment. Checks the environments config option for the Rails environment by default.

Returns a boolean

# File lib/octopus.rb, line 48
def self.enabled?
  if defined?(::Rails.env)
    Octopus.environments.include?(Rails.env.to_s)
  else
    # TODO: This doens't feel right but !Octopus.config.blank? is breaking a
    #       test. Also, Octopus.config is always returning a hash.
    Octopus.config
  end
end
env() click to toggle source
# File lib/octopus.rb, line 9
def self.env
  @env ||= 'octopus'
end
environments() click to toggle source
# File lib/octopus.rb, line 75
def self.environments
  @environments ||= config['environments'] || ['production']
end
environments=(environments) click to toggle source
# File lib/octopus.rb, line 71
def self.environments=(environments)
  @environments = environments.map(&:to_s)
end
fully_replicated() { || ... } click to toggle source
# File lib/octopus.rb, line 170
def self.fully_replicated(&_block)
  old_fully_replicated = Thread.current[Octopus::ProxyConfig::FULLY_REPLICATED_KEY]
  Thread.current[Octopus::ProxyConfig::FULLY_REPLICATED_KEY] = true
  yield
ensure
  Thread.current[Octopus::ProxyConfig::FULLY_REPLICATED_KEY] = old_fully_replicated
end
load_balancer() click to toggle source
# File lib/octopus.rb, line 35
def self.load_balancer
  @load_balancer ||= Octopus::LoadBalancing::RoundRobin
end
load_balancer=(balancer) click to toggle source
# File lib/octopus.rb, line 31
def self.load_balancer=(balancer)
  @load_balancer = balancer
end
logger() click to toggle source
# File lib/octopus.rb, line 127
def self.logger
  if defined?(Rails.logger)
    @logger ||= Rails.logger
  else
    @logger ||= Logger.new($stderr)
  end
end
master_shard() click to toggle source
# File lib/octopus.rb, line 39
def self.master_shard
  ((config && config[:master_shard]) || :master).to_sym
end
rails40?() click to toggle source
# File lib/octopus.rb, line 97
def self.rails40?
  rails4? && ActiveRecord::VERSION::MINOR == 0
end
rails41?() click to toggle source
# File lib/octopus.rb, line 105
def self.rails41?
  rails4? && ActiveRecord::VERSION::MINOR >= 1
end
rails41_only?() click to toggle source
# File lib/octopus.rb, line 101
def self.rails41_only?
  rails4? && ActiveRecord::VERSION::MINOR == 1
end
rails42?() click to toggle source
# File lib/octopus.rb, line 109
def self.rails42?
  rails4? && ActiveRecord::VERSION::MINOR == 2
end
rails4?() click to toggle source
# File lib/octopus.rb, line 93
def self.rails4?
  ActiveRecord::VERSION::MAJOR == 4
end
rails50?() click to toggle source
# File lib/octopus.rb, line 113
def self.rails50?
  ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR == 0
end
rails51?() click to toggle source
# File lib/octopus.rb, line 117
def self.rails51?
  ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR == 1
end
rails_env() click to toggle source
# File lib/octopus.rb, line 13
def self.rails_env
  @rails_env ||= defined?(::Rails.env) ? Rails.env.to_s : 'shards'
end
robust_environment?() click to toggle source
# File lib/octopus.rb, line 89
def self.robust_environment?
  robust_environments.include? rails_env
end
robust_environments() click to toggle source

Environments in which to swallow failures from a single shard when iterating through all.

# File lib/octopus.rb, line 85
def self.robust_environments
  @robust_environments ||= config['robust_environments'] || ['production']
end
robust_environments=(environments) click to toggle source
# File lib/octopus.rb, line 79
def self.robust_environments=(environments)
  @robust_environments = environments.map(&:to_s)
end
setup() { |self| ... } click to toggle source

This is the default way to do Octopus Setup Available variables: :enviroments => the enviroments that octopus will run. default: 'production'

# File lib/octopus.rb, line 67
def self.setup
  yield self
end
shards=(shards) click to toggle source
# File lib/octopus.rb, line 135
def self.shards=(shards)
  config[rails_env] = HashWithIndifferentAccess.new(shards)
  ActiveRecord::Base.connection.initialize_shards(@config)
end
using(shard) { || ... } click to toggle source
# File lib/octopus.rb, line 140
def self.using(shard, &block)
  conn = ActiveRecord::Base.connection

  if conn.is_a?(Octopus::Proxy)
    conn.run_queries_on_shard(shard, &block)
  else
    yield
  end
end
using_all() { || ... } click to toggle source
# File lib/octopus.rb, line 160
def self.using_all(&block)
  conn = ActiveRecord::Base.connection

  if conn.is_a?(Octopus::Proxy)
    conn.send_queries_to_all_shards(&block)
  else
    yield
  end
end
using_group(group) { || ... } click to toggle source
# File lib/octopus.rb, line 150
def self.using_group(group, &block)
  conn = ActiveRecord::Base.connection

  if conn.is_a?(Octopus::Proxy)
    conn.send_queries_to_group(group, &block)
  else
    yield
  end
end