Module: Octopus

Defined in:
lib/octopus/log_subscriber.rb,
lib/octopus.rb,
lib/octopus/model.rb,
lib/octopus/proxy.rb,
lib/octopus/version.rb,
lib/octopus/exception.rb,
lib/octopus/migration.rb,
lib/octopus/migration.rb,
lib/octopus/migration.rb,
lib/octopus/migration.rb,
lib/octopus/association.rb,
lib/octopus/persistence.rb,
lib/octopus/scope_proxy.rb,
lib/octopus/slave_group.rb,
lib/octopus/proxy_config.rb,
lib/octopus/load_balancing.rb,
lib/octopus/relation_proxy.rb,
lib/octopus/shard_tracking.rb,
lib/octopus/abstract_adapter.rb,
lib/octopus/collection_proxy.rb,
lib/octopus/singular_association.rb,
lib/octopus/collection_association.rb,
lib/octopus/query_cache_for_shards.rb,
lib/octopus/shard_tracking/dynamic.rb,
lib/octopus/shard_tracking/attribute.rb,
lib/octopus/association_shard_tracking.rb,
lib/octopus/load_balancing/round_robin.rb

Overview

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

Defined Under Namespace

Modules: AbstractAdapter, Association, AssociationShardTracking, CollectionAssociation, CollectionProxy, ConnectionPool, LoadBalancing, LogSubscriber, Migration, MigrationProxy, Migrator, Model, Persistence, ResultPatch, ShardTracking, SingularAssociation, UnknownMigrationVersionError Classes: Exception, Proxy, ProxyConfig, RelationProxy, ScopeProxy, SlaveGroup

Constant Summary collapse

VERSION =
'0.11.3'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#logger=(value) ⇒ Object (writeonly)

Sets the attribute logger

Parameters:

  • value

    the value to set the attribute logger to.



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

def logger=(value)
  @logger = value
end

Class Method Details

.atleast_rails50?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/octopus.rb', line 106

def self.atleast_rails50?
  ActiveRecord::VERSION::MAJOR >= 5
end

.atleast_rails51?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/octopus.rb', line 122

def self.atleast_rails51?
  ActiveRecord::VERSION::MAJOR > 5 || (ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR >= 1)
end

.atleast_rails52?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/octopus.rb', line 126

def self.atleast_rails52?
  ActiveRecord::VERSION::MAJOR > 5 || (ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR > 1)
end

.configObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/octopus.rb', line 18

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

.directoryObject

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



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

def self.directory
  @directory ||= defined?(::Rails.root) ? Rails.root.to_s : Dir.pwd
end

.enabled?Boolean

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

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
# File 'lib/octopus.rb', line 49

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

.envObject



10
11
12
# File 'lib/octopus.rb', line 10

def self.env
  @env ||= 'octopus'
end

.environmentsObject



76
77
78
# File 'lib/octopus.rb', line 76

def self.environments
  @environments ||= config['environments'] || ['production']
end

.environments=(environments) ⇒ Object



72
73
74
# File 'lib/octopus.rb', line 72

def self.environments=(environments)
  @environments = environments.map(&:to_s)
end

.fully_replicated(&_block) ⇒ Object



175
176
177
178
179
180
181
# File 'lib/octopus.rb', line 175

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_balancerObject



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

def self.load_balancer
  @load_balancer ||= Octopus::LoadBalancing::RoundRobin
end

.load_balancer=(balancer) ⇒ Object



32
33
34
# File 'lib/octopus.rb', line 32

def self.load_balancer=(balancer)
  @load_balancer = balancer
end

.loggerObject



132
133
134
135
136
137
138
# File 'lib/octopus.rb', line 132

def self.logger
  if defined?(Rails.logger)
    @logger ||= Rails.logger
  else
    @logger ||= Logger.new($stderr)
  end
end

.master_shardObject



40
41
42
# File 'lib/octopus.rb', line 40

def self.master_shard
  ((config && config[:master_shard]) || :master).to_sym
end

.rails42?Boolean

Returns:

  • (Boolean)


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

def self.rails42?
  rails4? && ActiveRecord::VERSION::MINOR == 2
end

.rails4?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/octopus.rb', line 94

def self.rails4?
  ActiveRecord::VERSION::MAJOR == 4
end

.rails50?Boolean

Returns:

  • (Boolean)


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

def self.rails50?
  ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR == 0
end

.rails51?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/octopus.rb', line 110

def self.rails51?
  ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR == 1
end

.rails52?Boolean

Returns:

  • (Boolean)


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

def self.rails52?
  ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR == 2
end

.rails60?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/octopus.rb', line 118

def self.rails60?
  ActiveRecord::VERSION::MAJOR > 6 || ActiveRecord::VERSION::MAJOR == 6
end

.rails_envObject



14
15
16
# File 'lib/octopus.rb', line 14

def self.rails_env
  @rails_env ||= defined?(::Rails.env) ? Rails.env.to_s : 'shards'
end

.robust_environment?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/octopus.rb', line 90

def self.robust_environment?
  robust_environments.include? rails_env
end

.robust_environmentsObject

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



86
87
88
# File 'lib/octopus.rb', line 86

def self.robust_environments
  @robust_environments ||= config['robust_environments'] || ['production']
end

.robust_environments=(environments) ⇒ Object



80
81
82
# File 'lib/octopus.rb', line 80

def self.robust_environments=(environments)
  @robust_environments = environments.map(&:to_s)
end

.setup {|_self| ... } ⇒ Object

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

Yields:

  • (_self)

Yield Parameters:

  • _self (Octopus)

    the object that the method was called on



68
69
70
# File 'lib/octopus.rb', line 68

def self.setup
  yield self
end

.shards=(shards) ⇒ Object



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

def self.shards=(shards)
  config[rails_env] = HashWithIndifferentAccess.new(shards)
  ActiveRecord::Base.connection.initialize_shards(@config)
end

.using(shard, &block) ⇒ Object



145
146
147
148
149
150
151
152
153
# File 'lib/octopus.rb', line 145

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(&block) ⇒ Object



165
166
167
168
169
170
171
172
173
# File 'lib/octopus.rb', line 165

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, &block) ⇒ Object



155
156
157
158
159
160
161
162
163
# File 'lib/octopus.rb', line 155

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