Module: Switchman::ActiveRecord::ConnectionHandler

Defined in:
lib/switchman/active_record/connection_handler.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.make_sharing_automagic(config, shard = Shard.current) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/switchman/active_record/connection_handler.rb', line 6

def self.make_sharing_automagic(config, shard = Shard.current)
  key = config[:adapter] == 'postgresql' ? :schema_search_path : :database

  # only load the shard name from the db if we have to
  if !config[:shard_name]
    # we may not be able to connect to this shard yet, cause it might be an empty database server
    shard = shard.call if shard.is_a?(Proc)
    shard_name = shard.name rescue nil
    return unless shard_name

    config[:shard_name] ||= shard_name
  end

  if !config[key] || config[key] == shard_name
    # this may truncate the schema_search_path if it was not specified in database.yml
    # but that's what our old behavior was anyway, so I guess it's okay
    config[key] = '%{shard_name}'
  end
end

Instance Method Details

#clear_idle_connections!(since_when) ⇒ Object



144
145
146
# File 'lib/switchman/active_record/connection_handler.rb', line 144

def clear_idle_connections!(since_when)
  connection_pool_list.each{ |pool| pool.clear_idle_connections!(since_when) }
end

#establish_connection(spec) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/switchman/active_record/connection_handler.rb', line 26

def establish_connection(spec)
  pool = super

  # this is the first place that the adapter would have been required; but now we
  # need this addition ASAP since it will be called when loading the default shard below
  if defined?(::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
    require "switchman/active_record/postgresql_adapter"
    ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(ActiveRecord::PostgreSQLAdapter)
  end

  first_time = !Shard.instance_variable_get(:@default)
  if first_time
    # Have to cache the default shard before we insert sharding, otherwise the first access
    # to sharding will recurse onto itself trying to access column information
    Shard.default

    # automatically change config to allow for sharing connections with simple config
    config = ::Rails.version < '5.1' ? spec.config : pool.spec.config
    ConnectionHandler.make_sharing_automagic(config)
    ConnectionHandler.make_sharing_automagic(Shard.default.database_server.config)

    if ::Rails.version < '5.1'
      ::ActiveRecord::Base.configurations[::Rails.env] = spec.instance_variable_get(:@config).stringify_keys
    else
      ::ActiveRecord::Base.configurations[::Rails.env] = config.stringify_keys
    end
  end

  @shard_connection_pools ||= { [:master, Shard.default.database_server.shareable? ? ::Rails.env : Shard.default] => pool}

  category = pool.spec.name.to_sym
  proxy = ConnectionPoolProxy.new(category,
                                  pool,
                                  @shard_connection_pools)
  owner_to_pool[pool.spec.name] = proxy

  if first_time
    if Shard.default.database_server.config[:prefer_slave]
      Shard.default.database_server.shackle!
    end

    if Shard.default.is_a?(DefaultShard) && Shard.default.database_server.config[:slave]
      Shard.default.database_server.shackle!
      Shard.default(true)
    end
  end

  # reload the default shard if we just got a new connection
  # to where the Shards table is
  # DON'T do it if we're not the current connection handler - that means
  # we're in the middle of switching environments, and we don't want to
  # establish a connection with incorrect settings
  if [:primary, :unsharded].include?(category) && self == ::ActiveRecord::Base.connection_handler && !first_time
    Shard.default(reload: true, with_fallback: true)
    proxy.disconnect!
  end

  if first_time
    # do the change for other database servers, now that we can switch shards
    if Shard.default.is_a?(Shard)
      DatabaseServer.all.each do |server|
        next if server == Shard.default.database_server

        shard = nil
        shard_proc = -> do
          shard ||= server.shards.where(:name => nil).first
          shard ||= Shard.new(:database_server => server)
          shard
        end
        ConnectionHandler.make_sharing_automagic(server.config, shard_proc)
        ConnectionHandler.make_sharing_automagic(proxy.current_pool.spec.config, shard_proc)
      end
    end
    # we may have established some connections above trying to infer the shard's name.
    # close them, so that someone that doesn't expect them doesn't try to fork
    # without closing them
    self.clear_all_connections!
  end

  proxy
end

#initialize_categories(model = ::ActiveRecord::Base) ⇒ Object



162
163
164
# File 'lib/switchman/active_record/connection_handler.rb', line 162

def initialize_categories(model = ::ActiveRecord::Base)
  class_to_pool.clear
end

#remove_connection(spec_name) ⇒ Object



108
109
110
111
112
# File 'lib/switchman/active_record/connection_handler.rb', line 108

def remove_connection(spec_name)
  pool = owner_to_pool[spec_name]
  owner_to_pool[spec_name] = pool.default_pool if pool.is_a?(ConnectionPoolProxy)
  super
end

#retrieve_connection_pool(spec_name) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/switchman/active_record/connection_handler.rb', line 114

def retrieve_connection_pool(spec_name)
  owner_to_pool.fetch(spec_name) do
    if ancestor_pool = pool_from_any_process_for(spec_name)
      # A connection was established in an ancestor process that must have
      # subsequently forked. We can't reuse the connection, but we can copy
      # the specification and establish a new connection with it.
      spec = if ancestor_pool.is_a?(ConnectionPoolProxy)
        ancestor_pool.default_pool.spec
      else
        ancestor_pool.spec
      end
      spec = spec.to_hash if ::Rails.version >= '5.1'
      pool = establish_connection spec
      pool.instance_variable_set(:@schema_cache, ancestor_pool.schema_cache) if ancestor_pool.schema_cache
      pool
    elsif spec_name != "primary"
      primary_pool = retrieve_connection_pool("primary")
      if primary_pool.is_a?(ConnectionPoolProxy)
        pool = ConnectionPoolProxy.new(spec_name.to_sym, primary_pool.default_pool, @shard_connection_pools)
        pool.schema_cache.copy_values(primary_pool.schema_cache)
        pool
      else
        primary_pool
      end
    else
      owner_to_pool[spec_name] = nil
    end
  end
end

#switchman_connection_pool_proxiesObject



148
149
150
# File 'lib/switchman/active_record/connection_handler.rb', line 148

def switchman_connection_pool_proxies
  owner_to_pool.values.uniq.select{|p| p.is_a?(ConnectionPoolProxy)}
end

#uninitialize_ar(model = ::ActiveRecord::Base) ⇒ Object



156
157
158
159
160
# File 'lib/switchman/active_record/connection_handler.rb', line 156

def uninitialize_ar(model = ::ActiveRecord::Base)
  # take the proxies out
  pool = owner_to_pool[model.name]
  owner_to_pool[model.name] = pool.default_pool if pool
end