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.10.2'

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.



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

def logger=(value)
  @logger = value
end

Class Method Details

.atleast_rails50?Boolean

Returns:

  • (Boolean)


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

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

.atleast_rails51?Boolean

Returns:

  • (Boolean)


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

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

.atleast_rails52?Boolean

Returns:

  • (Boolean)


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

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

.configObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# 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

.directoryObject

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



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

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)


48
49
50
51
52
53
54
55
56
# 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

.envObject



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

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

.environmentsObject



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

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

.environments=(environments) ⇒ Object



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

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

.fully_replicated(&_block) ⇒ Object



170
171
172
173
174
175
176
# 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_balancerObject



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

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

.load_balancer=(balancer) ⇒ Object



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

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

.loggerObject



127
128
129
130
131
132
133
# File 'lib/octopus.rb', line 127

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

.master_shardObject



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

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

.rails42?Boolean

Returns:

  • (Boolean)


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

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

.rails4?Boolean

Returns:

  • (Boolean)


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

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

.rails50?Boolean

Returns:

  • (Boolean)


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

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

.rails51?Boolean

Returns:

  • (Boolean)


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

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

.rails52?Boolean

Returns:

  • (Boolean)


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

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

.rails_envObject



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

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

.robust_environment?Boolean

Returns:

  • (Boolean)


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

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.



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

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

.robust_environments=(environments) ⇒ Object



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

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



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

def self.setup
  yield self
end

.shards=(shards) ⇒ Object



135
136
137
138
# 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, &block) ⇒ Object



140
141
142
143
144
145
146
147
148
# 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(&block) ⇒ Object



160
161
162
163
164
165
166
167
168
# 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, &block) ⇒ Object



150
151
152
153
154
155
156
157
158
# 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