fresh_connection 3.1.2 → 3.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +190 -152
  3. data/fresh_connection.gemspec +3 -3
  4. data/lib/fresh_connection/access_control.rb +144 -21
  5. data/lib/fresh_connection/connection_manager.rb +6 -2
  6. data/lib/fresh_connection/connection_specification/rails_70.rb +48 -0
  7. data/lib/fresh_connection/connection_specification.rb +10 -2
  8. data/lib/fresh_connection/deprecator.rb +10 -0
  9. data/lib/fresh_connection/executor_hook.rb +4 -1
  10. data/lib/fresh_connection/extend/adapters/base_adapter.rb +116 -11
  11. data/lib/fresh_connection/extend/adapters/m2_adapter.rb +2 -11
  12. data/lib/fresh_connection/extend/adapters/pg_adapter.rb +2 -14
  13. data/lib/fresh_connection/extend/adapters/trilogy_adapter.rb +15 -0
  14. data/lib/fresh_connection/extend/ar_base.rb +115 -3
  15. data/lib/fresh_connection/extend/ar_connection_handler.rb +42 -9
  16. data/lib/fresh_connection/extend/ar_relation.rb +105 -13
  17. data/lib/fresh_connection/extend/ar_relation_merger.rb +1 -1
  18. data/lib/fresh_connection/extend/ar_statement_cache.rb +39 -2
  19. data/lib/fresh_connection/extend.rb +49 -3
  20. data/lib/fresh_connection/rails_replica.rb +559 -0
  21. data/lib/fresh_connection/railtie.rb +13 -1
  22. data/lib/fresh_connection/replica_connection_handler.rb +1 -0
  23. data/lib/fresh_connection/version.rb +1 -1
  24. data/lib/fresh_connection.rb +46 -2
  25. metadata +22 -19
  26. data/.travis.yml +0 -24
  27. data/Appraisals +0 -12
  28. data/benchmarks/benchmark.rb +0 -25
  29. data/bin/test +0 -24
  30. data/gemfiles/rails52.gemfile +0 -8
  31. data/gemfiles/rails60.gemfile +0 -7
  32. data/gemfiles/rails61.gemfile +0 -7
@@ -3,22 +3,48 @@
3
3
  module FreshConnection
4
4
  class AccessControl
5
5
  class << self
6
- RETRY_LIMIT = 3
7
- private_constant :RETRY_LIMIT
6
+ def manage_access(model:, replica_access:, force_master: false, &block)
7
+ if model.master_db_only? || force_master
8
+ return with_current_model(model) do
9
+ force_master_access(&block)
10
+ end
11
+ end
8
12
 
9
- def manage_access(model:, replica_access:, &block)
10
- return force_master_access(&block) if model.master_db_only?
13
+ unless FreshConnection::RailsReplica.use_rails_replica?(model) ||
14
+ FreshConnection::RailsReplica.legacy_replica_enabled?(model)
15
+ return with_current_model(model) do
16
+ if FreshConnection::RailsReplica.in_reading_role?(model)
17
+ FreshConnection::RailsReplica.with_writing(model, &block)
18
+ else
19
+ yield
20
+ end
21
+ end
22
+ end
11
23
 
24
+ outermost = !managed?
25
+ will_use_replica = replica_access && access_db != :master
12
26
  retry_count = 0
13
27
  begin
14
- access(replica_access, &block)
15
- rescue *catch_exceptions
16
- if recovery?(model.replica_spec_name)
28
+ with_current_model(model) do
29
+ access(replica_access, &block)
30
+ end
31
+ rescue *catch_exceptions => e
32
+ raise if exhausted_exception?(e)
33
+
34
+ if will_use_replica &&
35
+ !non_retryable_exception?(e) &&
36
+ recovery_enabled? &&
37
+ (retry_count + 1 < FreshConnection.retry_limit) &&
38
+ with_last_exception(e) { recovery?(recovery_target(model)) }
39
+ state_set(:fresh_connection_exhausted_exception, nil)
17
40
  retry_count += 1
18
- retry if retry_count < RETRY_LIMIT
41
+ retry
19
42
  end
20
43
 
44
+ state_set(:fresh_connection_exhausted_exception, e)
21
45
  raise
46
+ ensure
47
+ state_set(:fresh_connection_exhausted_exception, nil) if outermost
22
48
  end
23
49
  end
24
50
 
@@ -26,6 +52,22 @@ module FreshConnection
26
52
  access_db == :replica
27
53
  end
28
54
 
55
+ def managed?
56
+ !access_db.nil?
57
+ end
58
+
59
+ def current_model
60
+ state_get(:fresh_connection_current_model)
61
+ end
62
+
63
+ def last_exception
64
+ state_get(:fresh_connection_last_exception)
65
+ end
66
+
67
+ def in_transaction?(model)
68
+ FreshConnection::RailsReplica.in_transaction?(model)
69
+ end
70
+
29
71
  private
30
72
 
31
73
  def force_master_access(&block)
@@ -33,43 +75,124 @@ module FreshConnection
33
75
  end
34
76
 
35
77
  def access(replica_access, &block)
36
- return yield if access_db
78
+ if access_db
79
+ return ensure_rails_role(access_db, &block)
80
+ end
37
81
 
38
82
  db = replica_access ? :replica : :master
39
83
  switch_to(db, &block)
40
84
  end
41
85
 
42
- def switch_to(new_db)
86
+ def switch_to(new_db, &block)
43
87
  old_db = access_db
44
88
  access_to(new_db)
45
- yield
89
+ ensure_rails_role(new_db, &block)
46
90
  ensure
47
91
  access_to(old_db)
48
92
  end
49
93
 
94
+ def ensure_rails_role(target_db, &block)
95
+ model = current_model
96
+ return yield unless model
97
+
98
+ if target_db == :replica
99
+ return yield unless FreshConnection::RailsReplica.use_rails_replica?(model)
100
+
101
+ FreshConnection::RailsReplica.with_reading(model, &block)
102
+ else
103
+ return yield unless FreshConnection::RailsReplica.supported? &&
104
+ (FreshConnection::RailsReplica.use_rails_replica?(model) ||
105
+ FreshConnection::RailsReplica.in_reading_role?(model) ||
106
+ FreshConnection::RailsReplica.any_reading_or_preventing_writes?)
107
+
108
+ FreshConnection::RailsReplica.with_writing(model, &block)
109
+ end
110
+ end
111
+
112
+ def with_current_model(model)
113
+ old_model = current_model
114
+ state_set(:fresh_connection_current_model, model)
115
+ yield
116
+ ensure
117
+ state_set(:fresh_connection_current_model, old_model)
118
+ end
119
+
120
+ def with_last_exception(exception)
121
+ old_exception = last_exception
122
+ state_set(:fresh_connection_last_exception, exception)
123
+ yield
124
+ ensure
125
+ state_set(:fresh_connection_last_exception, old_exception)
126
+ end
127
+
128
+ def exhausted_exception?(exception)
129
+ exhausted = state_get(:fresh_connection_exhausted_exception)
130
+ !exhausted.nil? && exhausted.equal?(exception)
131
+ end
132
+
50
133
  def access_db
51
- Thread.current[:fresh_connection_access_target]
134
+ state_get(:fresh_connection_access_target)
52
135
  end
53
136
 
54
137
  def access_to(db)
55
- Thread.current[:fresh_connection_access_target] = db
138
+ state_set(:fresh_connection_access_target, db)
139
+ end
140
+
141
+ def state_get(key)
142
+ if defined?(ActiveSupport::IsolatedExecutionState)
143
+ ActiveSupport::IsolatedExecutionState[key]
144
+ else
145
+ Thread.current[key]
146
+ end
56
147
  end
57
148
 
58
- def recovery?(spec_name)
59
- FreshConnection::ReplicaConnectionHandler.instance.recovery?(spec_name)
149
+ def state_set(key, value)
150
+ if defined?(ActiveSupport::IsolatedExecutionState)
151
+ ActiveSupport::IsolatedExecutionState[key] = value
152
+ else
153
+ Thread.current[key] = value
154
+ end
155
+ end
156
+
157
+ def recovery_enabled?
158
+ FreshConnection.recovery && FreshConnection.retry_limit > 0
159
+ end
160
+
161
+ def non_retryable_exception?(exception)
162
+ defined?(ActiveRecord::ConnectionNotDefined) && exception.is_a?(ActiveRecord::ConnectionNotDefined)
163
+ end
164
+
165
+ def recovery_target(model)
166
+ if FreshConnection::RailsReplica.use_rails_replica?(model)
167
+ model
168
+ else
169
+ model.replica_spec_name
170
+ end
171
+ end
172
+
173
+ def recovery?(target)
174
+ if target.is_a?(Class) && defined?(ActiveRecord::Base) && target <= ActiveRecord::Base
175
+ FreshConnection::RailsReplica.recovery?(target, exception: last_exception)
176
+ else
177
+ FreshConnection::ReplicaConnectionHandler.instance.recovery?(target)
178
+ end
60
179
  end
61
180
 
62
181
  def catch_exceptions
63
- return @catch_exceptions if defined?(@catch_exceptions)
64
- @catch_exceptions = [ActiveRecord::StatementInvalid]
65
- @catch_exceptions << ::Mysql2::Error if defined?(::Mysql2)
182
+ exceptions = [
183
+ ActiveRecord::StatementInvalid,
184
+ ActiveRecord::ConnectionNotEstablished
185
+ ]
186
+
187
+ exceptions << ::Mysql2::Error if defined?(::Mysql2::Error)
188
+ exceptions << ::Trilogy::Error if defined?(::Trilogy::Error)
66
189
 
67
190
  if defined?(::PG)
68
- @catch_exceptions << ::PG::Error
69
- @catch_exceptions << ::PGError if defined?(::PGError)
191
+ exceptions << ::PG::Error if defined?(::PG::Error)
192
+ exceptions << ::PGError if defined?(::PGError)
70
193
  end
71
194
 
72
- @catch_exceptions
195
+ exceptions
73
196
  end
74
197
  end
75
198
  end
@@ -12,7 +12,11 @@ module FreshConnection
12
12
  end
13
13
 
14
14
  def replica_connection
15
- @pool.connection
15
+ if @pool.respond_to?(:lease_connection)
16
+ @pool.lease_connection
17
+ else
18
+ @pool.connection
19
+ end
16
20
  end
17
21
 
18
22
  def put_aside!
@@ -21,8 +25,8 @@ module FreshConnection
21
25
  conn = replica_connection
22
26
  return if conn.transaction_open?
23
27
 
24
- @pool.release_connection
25
28
  @pool.remove(conn)
29
+ conn.expire if conn.respond_to?(:expire)
26
30
  conn.disconnect!
27
31
  end
28
32
 
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FreshConnection
4
+ class ConnectionSpecification
5
+ module Rails70
6
+ def spec
7
+ db_config = ActiveRecord::DatabaseConfigurations::HashConfig.new(
8
+ @spec_name.to_s,
9
+ base_db_config_name,
10
+ build_config
11
+ )
12
+
13
+ pool_config_klass = ActiveRecord::ConnectionAdapters::PoolConfig
14
+ if pool_config_klass.instance_method(:initialize).arity == 2
15
+ pool_config_klass.new(ActiveRecord::Base, db_config)
16
+ else
17
+ role = ActiveRecord.respond_to?(:reading_role) ? ActiveRecord.reading_role : :reading
18
+ shard = ActiveRecord::Base.respond_to?(:default_shard) ? ActiveRecord::Base.default_shard : :default
19
+ pool_config_klass.new(ActiveRecord::Base, db_config, role, shard)
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def config_from_url
26
+ ActiveRecord::DatabaseConfigurations::ConnectionUrlResolver.new(database_group_url).to_hash
27
+ end
28
+
29
+ def base_config
30
+ base_db_config.configuration_hash
31
+ end
32
+
33
+ def base_db_config
34
+ if ActiveRecord::Base.respond_to?(:connection_db_config)
35
+ ActiveRecord::Base.connection_db_config
36
+ else
37
+ ActiveRecord::Base.connection_pool.db_config
38
+ end
39
+ end
40
+
41
+ def base_db_config_name
42
+ base_db_config.name
43
+ rescue ActiveRecord::ConnectionNotEstablished
44
+ @spec_name.to_s
45
+ end
46
+ end
47
+ end
48
+ end
@@ -24,9 +24,17 @@ module FreshConnection
24
24
 
25
25
  def replica_config(config)
26
26
  if database_group_url
27
+ FreshConnection.deprecator.warn(
28
+ "Configuring read replicas via ENV['DATABASE_#{@spec_name.upcase}_URL'] is deprecated and will be removed in FreshConnection 3.2.0. " \
29
+ "Please configure read replicas using Rails' native `config/database.yml` and `connects_to` (database: { writing: ..., reading: ... }) instead."
30
+ )
27
31
  config_from_url
28
- else
29
- config[@spec_name]
32
+ elsif (nested_config = config[@spec_name])
33
+ FreshConnection.deprecator.warn(
34
+ "Configuring read replicas via the nested '#{@spec_name}' stanza in config/database.yml is deprecated and will be removed in FreshConnection 3.2.0. " \
35
+ "Please configure read replicas using Rails' native 3-tier `config/database.yml` and `connects_to` (database: { writing: ..., reading: ... }) instead."
36
+ )
37
+ nested_config
30
38
  end
31
39
  end
32
40
 
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+ require 'active_support/deprecation'
5
+
6
+ module FreshConnection
7
+ def self.deprecator
8
+ @deprecator ||= ActiveSupport::Deprecation.new("3.2.0", "FreshConnection")
9
+ end
10
+ end
@@ -6,8 +6,11 @@ module FreshConnection
6
6
  def run
7
7
  end
8
8
 
9
- def complete(*args)
9
+ def complete(*_args)
10
+ return unless FreshConnection.put_aside
11
+
10
12
  ReplicaConnectionHandler.instance.put_aside!
13
+ RailsReplica.put_aside!
11
14
  end
12
15
 
13
16
  def install_executor_hooks
@@ -7,25 +7,130 @@ module FreshConnection
7
7
  base.send :attr_writer, :model_class
8
8
  end
9
9
 
10
- def log(*args)
11
- args[1] = "[#{__replica_spec_name}] #{args[1]}" if __replica_spec_name
12
- super
13
- end
10
+ if RUBY_VERSION >= "3.0"
11
+ def log(*args, **kwargs, &block)
12
+ if (spec_name = __replica_spec_name)
13
+ args[1] = "[#{spec_name}] #{args[1] || 'SQL'}"
14
+ end
15
+ super(*args, **kwargs, &block)
16
+ end
14
17
 
15
- def select_all(*, **)
16
- __change_connection { super }
17
- end
18
+ def select_all(*args, **kwargs, &block)
19
+ __change_connection do
20
+ if kwargs.key?(:allow_retry) && __legacy_replica_active?
21
+ super(*args, **kwargs.merge(allow_retry: false), &block)
22
+ else
23
+ super(*args, **kwargs, &block)
24
+ end
25
+ end
26
+ end
27
+
28
+ def select_value(*, **, &block)
29
+ __change_connection { super }
30
+ end
31
+ else
32
+ def log(*args, &block)
33
+ if (spec_name = __replica_spec_name)
34
+ if args.length >= 2 && !args[1].is_a?(Hash)
35
+ args[1] = "[#{spec_name}] #{args[1] || 'SQL'}"
36
+ elsif args.length == 1
37
+ args[1] = "[#{spec_name}] SQL"
38
+ elsif args.length >= 2 && args[1].is_a?(Hash)
39
+ args.insert(1, "[#{spec_name}] SQL")
40
+ end
41
+ end
42
+ super(*args, &block)
43
+ end
18
44
 
19
- def select_value(*)
20
- __change_connection { super }
45
+ def select_all(*args, &block)
46
+ __change_connection { super(*args, &block) }
47
+ end
48
+
49
+ def select_value(*args, &block)
50
+ __change_connection { super(*args, &block) }
51
+ end
52
+
53
+ if respond_to?(:ruby2_keywords, true)
54
+ ruby2_keywords :log
55
+ ruby2_keywords :select_all
56
+ ruby2_keywords :select_value
57
+ end
21
58
  end
22
59
 
23
60
  private
24
61
 
62
+ def __target_model
63
+ FreshConnection::AccessControl.current_model || (defined?(@model_class) ? @model_class : nil)
64
+ end
65
+
66
+ def __legacy_replica_active?
67
+ return false unless FreshConnection::AccessControl.replica_access?
68
+
69
+ model = __target_model
70
+ !model.nil? && FreshConnection::RailsReplica.legacy_replica_enabled?(model)
71
+ end
72
+
25
73
  def __replica_spec_name
26
- return nil if !defined?(@model_class) || !@model_class
27
74
  return nil unless FreshConnection::AccessControl.replica_access?
28
- @model_class.replica_spec_name
75
+
76
+ model = __target_model
77
+ return nil unless model
78
+
79
+ if FreshConnection::RailsReplica.supported? &&
80
+ !FreshConnection.custom_connection_manager? &&
81
+ !model.explicit_fresh_connection?
82
+ pool = FreshConnection::RailsReplica.reading_pool_for(model)
83
+ if pool
84
+ return pool.respond_to?(:db_config) ? pool.db_config.name.to_s : "replica"
85
+ end
86
+ end
87
+
88
+ return nil unless FreshConnection::RailsReplica.legacy_replica_enabled?(model)
89
+
90
+ model.replica_spec_name
91
+ end
92
+
93
+ def __swap_raw_connection(swap_statements: false)
94
+ return yield unless FreshConnection::AccessControl.replica_access?
95
+
96
+ model = __target_model
97
+ return yield unless model
98
+ return yield unless FreshConnection::RailsReplica.legacy_replica_enabled?(model)
99
+
100
+ conn_ivar = __raw_connection_ivar
101
+ master_connection = instance_variable_get(conn_ivar)
102
+ has_statements = swap_statements && instance_variable_defined?(:@statements)
103
+ master_statements = instance_variable_get(:@statements) if has_statements
104
+ replica_raw = nil
105
+
106
+ begin
107
+ replica_connection = model.replica_connection
108
+ replica_raw = replica_connection.instance_variable_get(conn_ivar)
109
+ if replica_raw.nil?
110
+ replica_connection.connect! if replica_connection.respond_to?(:connect!)
111
+ replica_raw = replica_connection.instance_variable_get(conn_ivar) || replica_connection.raw_connection
112
+ end
113
+ instance_variable_set(conn_ivar, replica_raw)
114
+ if has_statements
115
+ instance_variable_set(:@statements, replica_connection.instance_variable_get(:@statements))
116
+ end
117
+ yield
118
+ ensure
119
+ current_raw = instance_variable_get(conn_ivar)
120
+ if current_raw && current_raw != replica_raw && current_raw != master_connection
121
+ current_raw.close if current_raw.respond_to?(:close) rescue nil
122
+ end
123
+ instance_variable_set(conn_ivar, master_connection)
124
+ instance_variable_set(:@statements, master_statements) if has_statements
125
+ end
126
+ end
127
+
128
+ def __raw_connection_ivar
129
+ if ActiveRecord::VERSION::MAJOR > 7 || (ActiveRecord::VERSION::MAJOR == 7 && ActiveRecord::VERSION::MINOR >= 1)
130
+ :@raw_connection
131
+ else
132
+ :@connection
133
+ end
29
134
  end
30
135
  end
31
136
  end
@@ -6,17 +6,8 @@ module FreshConnection
6
6
  module M2Adapter
7
7
  private
8
8
 
9
- def __change_connection
10
- return yield unless FreshConnection::AccessControl.replica_access?
11
-
12
- master_connection = @connection
13
- begin
14
- replica_connection = @model_class.replica_connection
15
- @connection = replica_connection.raw_connection
16
- yield
17
- ensure
18
- @connection = master_connection
19
- end
9
+ def __change_connection(&block)
10
+ __swap_raw_connection(swap_statements: true, &block)
20
11
  end
21
12
  end
22
13
  end
@@ -6,20 +6,8 @@ module FreshConnection
6
6
  module PgAdapter
7
7
  private
8
8
 
9
- def __change_connection
10
- return yield unless FreshConnection::AccessControl.replica_access?
11
-
12
- master_connection = @connection
13
- master_statements = @statements
14
- begin
15
- replica_connection = @model_class.replica_connection
16
- @connection = replica_connection.raw_connection
17
- @statements = replica_connection.instance_variable_get(:@statements)
18
- yield
19
- ensure
20
- @connection = master_connection
21
- @statements = master_statements
22
- end
9
+ def __change_connection(&block)
10
+ __swap_raw_connection(swap_statements: true, &block)
23
11
  end
24
12
  end
25
13
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fresh_connection/extend/adapters/base_adapter'
4
+
5
+ module FreshConnection
6
+ module Extend
7
+ module TrilogyAdapter
8
+ private
9
+
10
+ def __change_connection(&block)
11
+ __swap_raw_connection(swap_statements: true, &block)
12
+ end
13
+ end
14
+ end
15
+ end