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
Public Class Methods
# File lib/octopus.rb, line 105 def self.atleast_rails50? ActiveRecord::VERSION::MAJOR >= 5 end
# File lib/octopus.rb, line 117 def self.atleast_rails51? ActiveRecord::VERSION::MAJOR > 5 || (ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR >= 1) end
# File lib/octopus.rb, line 121 def self.atleast_rails52? ActiveRecord::VERSION::MAJOR > 5 || (ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR > 1) end
# 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
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
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
# File lib/octopus.rb, line 9 def self.env @env ||= 'octopus' end
# File lib/octopus.rb, line 75 def self.environments @environments ||= config['environments'] || ['production'] end
# File lib/octopus.rb, line 71 def self.environments=(environments) @environments = environments.map(&:to_s) end
# 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
# File lib/octopus.rb, line 35 def self.load_balancer @load_balancer ||= Octopus::LoadBalancing::RoundRobin end
# File lib/octopus.rb, line 31 def self.load_balancer=(balancer) @load_balancer = balancer end
# File lib/octopus.rb, line 127 def self.logger if defined?(Rails.logger) @logger ||= Rails.logger else @logger ||= Logger.new($stderr) end end
# File lib/octopus.rb, line 39 def self.master_shard ((config && config[:master_shard]) || :master).to_sym end
# File lib/octopus.rb, line 97 def self.rails42? rails4? && ActiveRecord::VERSION::MINOR == 2 end
# File lib/octopus.rb, line 93 def self.rails4? ActiveRecord::VERSION::MAJOR == 4 end
# File lib/octopus.rb, line 101 def self.rails50? ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR == 0 end
# File lib/octopus.rb, line 109 def self.rails51? ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR == 1 end
# File lib/octopus.rb, line 113 def self.rails52? ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR == 2 end
# File lib/octopus.rb, line 13 def self.rails_env @rails_env ||= defined?(::Rails.env) ? Rails.env.to_s : 'shards' end
# File lib/octopus.rb, line 89 def self.robust_environment? robust_environments.include? rails_env end
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
# File lib/octopus.rb, line 79 def self.robust_environments=(environments) @robust_environments = environments.map(&:to_s) end
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
# File lib/octopus.rb, line 135 def self.shards=(shards) config[rails_env] = HashWithIndifferentAccess.new(shards) ActiveRecord::Base.connection.initialize_shards(@config) end
# 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
# 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
# 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