Class: Switchman::DatabaseServer

Inherits:
Object
  • Object
show all
Extended by:
Enumerable
Defined in:
lib/switchman/database_server.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = nil, config = {}) ⇒ DatabaseServer

Returns a new instance of DatabaseServer.



102
103
104
105
106
107
# File 'lib/switchman/database_server.rb', line 102

def initialize(id = nil, config = {})
  @id = id
  @config = config.deep_symbolize_keys
  @configs = {}
  @roles = [:primary]
end

Class Attribute Details

.all_rolesObject (readonly)

Returns the value of attribute all_roles.



11
12
13
# File 'lib/switchman/database_server.rb', line 11

def all_roles
  @all_roles
end

.creating_new_shardObject

Returns the value of attribute creating_new_shard.



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

def creating_new_shard
  @creating_new_shard
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/switchman/database_server.rb', line 7

def id
  @id
end

#rolesObject (readonly)

Returns the value of attribute roles.



100
101
102
# File 'lib/switchman/database_server.rb', line 100

def roles
  @roles
end

Class Method Details

.allObject



17
18
19
# File 'lib/switchman/database_server.rb', line 17

def all
  database_servers.values
end

.create(settings = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/switchman/database_server.rb', line 28

def create(settings = {})
  raise "database servers should be set up in database.yml" unless ::Rails.env.test?

  id = settings[:id]
  unless id
    @id ||= 0
    @id += 1
    id = @id
  end
  server = DatabaseServer.new(id.to_s, settings)
  server.instance_variable_set(:@fake, true)
  database_servers[server.id] = server
  ::ActiveRecord::Base.configurations.configurations <<
    ::ActiveRecord::DatabaseConfigurations::HashConfig.new(::Rails.env, "#{server.id}/primary", settings)
  Shard.send(:configure_connects_to)
  server
end

.find(id_or_all) ⇒ Object



21
22
23
24
25
26
# File 'lib/switchman/database_server.rb', line 21

def find(id_or_all)
  return all if id_or_all == :all
  return id_or_all.filter_map { |id| database_servers[id || ::Rails.env] }.uniq if id_or_all.is_a?(Array)

  database_servers[id_or_all || ::Rails.env]
end

.guard_serversObject



53
54
55
# File 'lib/switchman/database_server.rb', line 53

def guard_servers
  all.each { |db| db.guard! if db.config[:prefer_secondary] }
end

.regionsObject



57
58
59
# File 'lib/switchman/database_server.rb', line 57

def regions
  @regions ||= all.filter_map(&:region).uniq.sort
end

.server_for_new_shardObject



46
47
48
49
50
51
# File 'lib/switchman/database_server.rb', line 46

def server_for_new_shard
  servers = all.select { |s| s.config[:open] }
  return find(nil) if servers.empty?

  servers[rand(servers.length)]
end

Instance Method Details

#cache_storeObject



288
289
290
291
# File 'lib/switchman/database_server.rb', line 288

def cache_store
  @cache_store ||= Switchman.config[:cache_map][id] || Switchman.config[:cache_map][::Rails.env]
  @cache_store
end

#config(environment = :primary) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/switchman/database_server.rb', line 134

def config(environment = :primary)
  @configs[environment] ||=
    case @config[environment]
    when Array
      @config[environment].map do |config|
        config = @config.merge((config || {}).symbolize_keys)
        # make sure GuardRail doesn't get any brilliant ideas about choosing the first possible server
        config.delete(environment)
        config
      end
    when Hash
      @config.merge(@config[environment])
    else
      @config
    end
end

#connects_to_hashObject



109
110
111
112
113
114
115
116
117
# File 'lib/switchman/database_server.rb', line 109

def connects_to_hash
  self.class.all_roles.to_h do |role|
    config_role = role
    config_role = :primary unless roles.include?(role)
    config_name = :"#{id}/#{config_role}"
    config_name = :primary if id == ::Rails.env && config_role == :primary
    [role.to_sym, config_name]
  end
end

#create_new_shard(id: nil, name: nil, schema: true) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/switchman/database_server.rb', line 207

def create_new_shard(id: nil, name: nil, schema: true)
  unless Shard.default.is_a?(Shard)
    raise NotImplementedError,
          "Cannot create new shards when sharding isn't initialized"
  end

  create_statement = -> { "CREATE SCHEMA #{name}" }
  password = " PASSWORD #{::ActiveRecord::Base.connection.quote(config[:password])}" if config[:password]
  sharding_config = Switchman.config
  config_create_statement = sharding_config[config[:adapter]]&.[](:create_statement)
  config_create_statement ||= sharding_config[:create_statement]
  if config_create_statement
    create_commands = Array(config_create_statement).dup
    create_statement = lambda {
      create_commands.map { |statement| format(statement, name: name, password: password) }
    }
  end

  id ||= begin
    id_seq = Shard.connection.quote(Shard.connection.quote_table_name("switchman_shards_id_seq"))
    next_id = Shard.connection.select_value("SELECT nextval(#{id_seq})")
    next_id.to_i
  end

  name ||= "#{config[:database]}_shard_#{id}"

  schema_already_existed = false
  shard = nil
  Shard.connection.transaction do
    self.class.creating_new_shard = true
    DatabaseServer.send(:reference_role, :deploy)
    ::ActiveRecord::Base.connected_to(shard: self.id.to_sym, role: :deploy) do
      shard = Shard.create!(id: id,
                            name: name,
                            database_server_id: self.id)
      if create_statement
        if ::ActiveRecord::Base.connection.select_value(
          "SELECT 1 FROM pg_namespace WHERE nspname=#{::ActiveRecord::Base.connection.quote(name)}"
        )
          schema_already_existed = true
          raise "This schema already exists; cannot overwrite"
        end
        Array(create_statement.call).each do |stmt|
          ::ActiveRecord::Base.connection.execute(stmt)
        end
      end
      if config[:adapter] == "postgresql"
        old_proc = ::ActiveRecord::Base.connection.raw_connection.set_notice_processor {}
      end
      old_verbose = ::ActiveRecord::Migration.verbose
      ::ActiveRecord::Migration.verbose = false

      unless schema == false
        shard.activate do
          ::ActiveRecord::Base.connection.transaction(requires_new: true) do
            if ::Rails.version < "7.1"
              ::ActiveRecord::Base.connection.migration_context.migrate
            else
              ::ActiveRecord::MigrationContext.new(::ActiveRecord::Migrator.migrations_paths).migrate
            end
          end

          ::ActiveRecord::Base.descendants.reject do |m|
            m <= UnshardedRecord || !m.table_exists?
          end.each(&:define_attribute_methods)
        end
      end
    ensure
      ::ActiveRecord::Migration.verbose = old_verbose
      ::ActiveRecord::Base.connection.raw_connection.set_notice_processor(&old_proc) if old_proc
    end
    shard
  rescue
    shard&.destroy
    shard&.drop_database rescue nil unless schema_already_existed
    raise
  ensure
    self.class.creating_new_shard = false
  end
end

#destroyObject



119
120
121
122
123
124
125
126
127
128
# File 'lib/switchman/database_server.rb', line 119

def destroy
  self.class.send(:database_servers).delete(id) if id
  Shard.sharded_models.each do |klass|
    self.class.all_roles.each do |role|
      klass.connection_handler.remove_connection_pool(klass.connection_specification_name,
                                                      role: role,
                                                      shard: id.to_sym)
    end
  end
end

#fake?Boolean

Returns:

  • (Boolean)


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

def fake?
  @fake
end

#guard!(environment = :secondary) ⇒ Object

locks this db to a specific environment, except for when doing writes (then it falls back to the current value of GuardRail.environment)



177
178
179
180
181
# File 'lib/switchman/database_server.rb', line 177

def guard!(environment = :secondary)
  DatabaseServer.send(:reference_role, environment)
  ::ActiveRecord::Base.connected_to_stack << { shard_roles: { id.to_sym => environment },
                                               klasses: [::ActiveRecord::Base] }
end

#in_current_region?Boolean

Returns true if the database server doesn’t have a region, Switchman is not configured with a region, or the database server’s region matches Switchman’s current region.

Returns:

  • (Boolean)

    true if the database server doesn’t have a region, Switchman is not configured with a region, or the database server’s region matches Switchman’s current region



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

def in_current_region?
  unless instance_variable_defined?(:@in_current_region)
    @in_current_region = !region ||
                         !Switchman.region ||
                         region == Switchman.region
  end
  @in_current_region
end

#in_region?(region) ⇒ Boolean

Returns true if the database server doesn’t have a region, or it matches the specified region.

Parameters:

  • region (String, Array<String>)

    the region(s) to check against

Returns:

  • (Boolean)

    true if the database server doesn’t have a region, or it matches the specified region



158
159
160
# File 'lib/switchman/database_server.rb', line 158

def in_region?(region)
  !self.region || (region.is_a?(Array) ? region.include?(self.region) : self.region == region)
end

#primary_shardObject



305
306
307
308
309
# File 'lib/switchman/database_server.rb', line 305

def primary_shard
  return nil unless primary_shard_id

  Shard.lookup(primary_shard_id)
end

#primary_shard_idObject



311
312
313
314
315
316
317
318
# File 'lib/switchman/database_server.rb', line 311

def primary_shard_id
  unless instance_variable_defined?(:@primary_shard_id)
    # if sharding isn't fully set up yet, we may not be able to query the shards table
    @primary_shard_id = Shard.default.id if Shard.default.database_server == self
    @primary_shard_id ||= shards.where(name: nil).first&.id
  end
  @primary_shard_id
end

#regionObject



151
152
153
# File 'lib/switchman/database_server.rb', line 151

def region
  config[:region]
end

#shard_name(shard) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
# File 'lib/switchman/database_server.rb', line 293

def shard_name(shard)
  return config[:shard_name] if config[:shard_name]

  if shard == :bootstrap
    # rescue nil because the database may not exist yet; if it doesn't,
    # it will shortly, and this will be re-invoked
    ::ActiveRecord::Base.connection.current_schemas.first rescue nil
  else
    shard.activate { ::ActiveRecord::Base.connection_pool.default_schema }
  end
end

#shardsObject



199
200
201
202
203
204
205
# File 'lib/switchman/database_server.rb', line 199

def shards
  if id == ::Rails.env
    Shard.where("database_server_id IS NULL OR database_server_id=?", id)
  else
    Shard.where(database_server_id: id)
  end
end

#unguardObject



188
189
190
191
192
193
194
195
196
197
# File 'lib/switchman/database_server.rb', line 188

def unguard
  return yield unless ::ActiveRecord::Base.role_overriden?(id.to_sym)

  begin
    unguard!
    yield
  ensure
    ::ActiveRecord::Base.connected_to_stack.pop
  end
end

#unguard!Object



183
184
185
186
# File 'lib/switchman/database_server.rb', line 183

def unguard!
  ::ActiveRecord::Base.connected_to_stack << { shard_roles: { id.to_sym => :_switchman_inherit },
                                               klasses: [::ActiveRecord::Base] }
end