activerecord 8.0.3 → 8.1.0

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 (160) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +538 -512
  3. data/README.rdoc +1 -1
  4. data/lib/active_record/association_relation.rb +1 -1
  5. data/lib/active_record/associations/association.rb +1 -1
  6. data/lib/active_record/associations/belongs_to_association.rb +2 -0
  7. data/lib/active_record/associations/builder/association.rb +16 -5
  8. data/lib/active_record/associations/builder/belongs_to.rb +17 -4
  9. data/lib/active_record/associations/builder/collection_association.rb +7 -3
  10. data/lib/active_record/associations/builder/has_one.rb +1 -1
  11. data/lib/active_record/associations/builder/singular_association.rb +33 -5
  12. data/lib/active_record/associations/collection_proxy.rb +22 -4
  13. data/lib/active_record/associations/deprecation.rb +88 -0
  14. data/lib/active_record/associations/errors.rb +3 -0
  15. data/lib/active_record/associations/join_dependency.rb +2 -0
  16. data/lib/active_record/associations/preloader/branch.rb +1 -0
  17. data/lib/active_record/associations.rb +159 -21
  18. data/lib/active_record/attribute_methods/serialization.rb +16 -3
  19. data/lib/active_record/attribute_methods/time_zone_conversion.rb +10 -2
  20. data/lib/active_record/attributes.rb +3 -0
  21. data/lib/active_record/autosave_association.rb +1 -1
  22. data/lib/active_record/base.rb +0 -2
  23. data/lib/active_record/coders/json.rb +14 -5
  24. data/lib/active_record/connection_adapters/abstract/connection_handler.rb +1 -3
  25. data/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb +16 -3
  26. data/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb +51 -12
  27. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +405 -72
  28. data/lib/active_record/connection_adapters/abstract/database_statements.rb +55 -40
  29. data/lib/active_record/connection_adapters/abstract/query_cache.rb +19 -3
  30. data/lib/active_record/connection_adapters/abstract/quoting.rb +15 -24
  31. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +7 -2
  32. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +26 -34
  33. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +2 -1
  34. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +85 -22
  35. data/lib/active_record/connection_adapters/abstract/transaction.rb +25 -3
  36. data/lib/active_record/connection_adapters/abstract_adapter.rb +86 -20
  37. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +43 -13
  38. data/lib/active_record/connection_adapters/column.rb +17 -4
  39. data/lib/active_record/connection_adapters/mysql/database_statements.rb +4 -4
  40. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +2 -0
  41. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +42 -5
  42. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +26 -4
  43. data/lib/active_record/connection_adapters/mysql2/database_statements.rb +27 -22
  44. data/lib/active_record/connection_adapters/mysql2_adapter.rb +1 -2
  45. data/lib/active_record/connection_adapters/postgresql/column.rb +4 -0
  46. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +17 -15
  47. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +2 -2
  48. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +1 -1
  49. data/lib/active_record/connection_adapters/postgresql/quoting.rb +21 -10
  50. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +8 -6
  51. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +8 -21
  52. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +67 -31
  53. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +81 -48
  54. data/lib/active_record/connection_adapters/postgresql_adapter.rb +23 -7
  55. data/lib/active_record/connection_adapters/schema_cache.rb +2 -2
  56. data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +37 -25
  57. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +0 -8
  58. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +4 -13
  59. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +56 -32
  60. data/lib/active_record/connection_adapters/trilogy/database_statements.rb +4 -3
  61. data/lib/active_record/connection_adapters/trilogy_adapter.rb +1 -1
  62. data/lib/active_record/connection_adapters.rb +1 -0
  63. data/lib/active_record/connection_handling.rb +14 -9
  64. data/lib/active_record/core.rb +5 -4
  65. data/lib/active_record/counter_cache.rb +33 -8
  66. data/lib/active_record/database_configurations/database_config.rb +5 -1
  67. data/lib/active_record/database_configurations/hash_config.rb +53 -9
  68. data/lib/active_record/database_configurations/url_config.rb +13 -3
  69. data/lib/active_record/database_configurations.rb +7 -3
  70. data/lib/active_record/delegated_type.rb +1 -1
  71. data/lib/active_record/dynamic_matchers.rb +54 -69
  72. data/lib/active_record/encryption/encryptable_record.rb +4 -4
  73. data/lib/active_record/encryption/encrypted_attribute_type.rb +1 -1
  74. data/lib/active_record/encryption/encryptor.rb +12 -0
  75. data/lib/active_record/encryption/scheme.rb +1 -1
  76. data/lib/active_record/enum.rb +24 -8
  77. data/lib/active_record/errors.rb +20 -4
  78. data/lib/active_record/explain.rb +1 -1
  79. data/lib/active_record/explain_registry.rb +51 -2
  80. data/lib/active_record/filter_attribute_handler.rb +73 -0
  81. data/lib/active_record/fixtures.rb +2 -2
  82. data/lib/active_record/gem_version.rb +2 -2
  83. data/lib/active_record/inheritance.rb +1 -1
  84. data/lib/active_record/insert_all.rb +12 -7
  85. data/lib/active_record/locking/optimistic.rb +7 -0
  86. data/lib/active_record/locking/pessimistic.rb +5 -0
  87. data/lib/active_record/log_subscriber.rb +2 -6
  88. data/lib/active_record/middleware/shard_selector.rb +34 -17
  89. data/lib/active_record/migration/command_recorder.rb +14 -1
  90. data/lib/active_record/migration/compatibility.rb +34 -24
  91. data/lib/active_record/migration/default_schema_versions_formatter.rb +30 -0
  92. data/lib/active_record/migration.rb +26 -16
  93. data/lib/active_record/model_schema.rb +36 -10
  94. data/lib/active_record/nested_attributes.rb +2 -0
  95. data/lib/active_record/persistence.rb +34 -3
  96. data/lib/active_record/query_cache.rb +22 -15
  97. data/lib/active_record/query_logs.rb +3 -7
  98. data/lib/active_record/railtie.rb +32 -3
  99. data/lib/active_record/railties/controller_runtime.rb +11 -6
  100. data/lib/active_record/railties/databases.rake +15 -3
  101. data/lib/active_record/railties/job_checkpoints.rb +15 -0
  102. data/lib/active_record/railties/job_runtime.rb +10 -11
  103. data/lib/active_record/reflection.rb +35 -0
  104. data/lib/active_record/relation/batches.rb +25 -11
  105. data/lib/active_record/relation/calculations.rb +20 -9
  106. data/lib/active_record/relation/delegation.rb +0 -1
  107. data/lib/active_record/relation/finder_methods.rb +27 -11
  108. data/lib/active_record/relation/predicate_builder/association_query_value.rb +9 -9
  109. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +7 -7
  110. data/lib/active_record/relation/predicate_builder.rb +9 -7
  111. data/lib/active_record/relation/query_attribute.rb +3 -1
  112. data/lib/active_record/relation/query_methods.rb +40 -29
  113. data/lib/active_record/relation/where_clause.rb +1 -8
  114. data/lib/active_record/relation.rb +24 -12
  115. data/lib/active_record/result.rb +44 -21
  116. data/lib/active_record/runtime_registry.rb +41 -58
  117. data/lib/active_record/sanitization.rb +2 -0
  118. data/lib/active_record/schema_dumper.rb +12 -10
  119. data/lib/active_record/scoping.rb +0 -1
  120. data/lib/active_record/signed_id.rb +43 -15
  121. data/lib/active_record/statement_cache.rb +13 -9
  122. data/lib/active_record/store.rb +44 -19
  123. data/lib/active_record/structured_event_subscriber.rb +85 -0
  124. data/lib/active_record/table_metadata.rb +5 -20
  125. data/lib/active_record/tasks/abstract_tasks.rb +76 -0
  126. data/lib/active_record/tasks/database_tasks.rb +25 -34
  127. data/lib/active_record/tasks/mysql_database_tasks.rb +3 -40
  128. data/lib/active_record/tasks/postgresql_database_tasks.rb +5 -39
  129. data/lib/active_record/tasks/sqlite_database_tasks.rb +14 -26
  130. data/lib/active_record/test_databases.rb +14 -4
  131. data/lib/active_record/test_fixtures.rb +27 -2
  132. data/lib/active_record/testing/query_assertions.rb +8 -2
  133. data/lib/active_record/timestamp.rb +4 -2
  134. data/lib/active_record/transaction.rb +2 -5
  135. data/lib/active_record/transactions.rb +32 -10
  136. data/lib/active_record/type/hash_lookup_type_map.rb +2 -1
  137. data/lib/active_record/type/internal/timezone.rb +7 -0
  138. data/lib/active_record/type/json.rb +15 -2
  139. data/lib/active_record/type/serialized.rb +11 -4
  140. data/lib/active_record/type/type_map.rb +1 -1
  141. data/lib/active_record/type_caster/connection.rb +2 -1
  142. data/lib/active_record/validations/associated.rb +1 -1
  143. data/lib/active_record.rb +65 -3
  144. data/lib/arel/alias_predication.rb +2 -0
  145. data/lib/arel/crud.rb +6 -11
  146. data/lib/arel/nodes/count.rb +2 -2
  147. data/lib/arel/nodes/function.rb +4 -10
  148. data/lib/arel/nodes/named_function.rb +2 -2
  149. data/lib/arel/nodes/node.rb +1 -1
  150. data/lib/arel/nodes.rb +0 -2
  151. data/lib/arel/select_manager.rb +7 -2
  152. data/lib/arel/visitors/dot.rb +0 -3
  153. data/lib/arel/visitors/postgresql.rb +55 -0
  154. data/lib/arel/visitors/sqlite.rb +55 -8
  155. data/lib/arel/visitors/to_sql.rb +3 -21
  156. data/lib/arel.rb +3 -1
  157. data/lib/rails/generators/active_record/application_record/USAGE +1 -1
  158. metadata +14 -10
  159. data/lib/active_record/explain_subscriber.rb +0 -34
  160. data/lib/active_record/normalization.rb +0 -163
@@ -8,18 +8,13 @@ require "active_record/connection_adapters/abstract/connection_pool/reaper"
8
8
 
9
9
  module ActiveRecord
10
10
  module ConnectionAdapters
11
- module AbstractPool # :nodoc:
12
- end
13
-
14
11
  class NullPool # :nodoc:
15
- include ConnectionAdapters::AbstractPool
16
-
17
- class NullConfig # :nodoc:
12
+ class NullConfig
18
13
  def method_missing(...)
19
14
  nil
20
15
  end
21
16
  end
22
- NULL_CONFIG = NullConfig.new # :nodoc:
17
+ NULL_CONFIG = NullConfig.new
23
18
 
24
19
  def initialize
25
20
  super()
@@ -49,6 +44,11 @@ module ActiveRecord
49
44
  def dirties_query_cache
50
45
  true
51
46
  end
47
+
48
+ def pool_transaction_isolation_level; end
49
+ def pool_transaction_isolation_level=(isolation_level)
50
+ raise NotImplementedError, "This method should never be called"
51
+ end
52
52
  end
53
53
 
54
54
  # = Active Record Connection Pool
@@ -101,13 +101,21 @@ module ActiveRecord
101
101
  # There are several connection-pooling-related options that you can add to
102
102
  # your database connection configuration:
103
103
  #
104
- # * +pool+: maximum number of connections the pool may manage (default 5).
105
- # * +idle_timeout+: number of seconds that a connection will be kept
106
- # unused in the pool before it is automatically disconnected (default
107
- # 300 seconds). Set this to zero to keep connections forever.
108
104
  # * +checkout_timeout+: number of seconds to wait for a connection to
109
105
  # become available before giving up and raising a timeout error (default
110
106
  # 5 seconds).
107
+ # * +idle_timeout+: number of seconds that a connection will be kept
108
+ # unused in the pool before it is automatically disconnected (default
109
+ # 300 seconds). Set this to zero to keep connections forever.
110
+ # * +keepalive+: number of seconds between keepalive checks if the
111
+ # connection has been idle (default 600 seconds).
112
+ # * +max_age+: number of seconds the pool will allow the connection to
113
+ # exist before retiring it at next checkin. (default Float::INFINITY).
114
+ # * +max_connections+: maximum number of connections the pool may manage (default 5).
115
+ # Set to +nil+ or -1 for unlimited connections.
116
+ # * +min_connections+: minimum number of connections the pool will open and maintain (default 0).
117
+ # * +pool_jitter+: maximum reduction factor to apply to +max_age+ and
118
+ # +keepalive+ intervals (default 0.2; range 0.0-1.0).
111
119
  #
112
120
  #--
113
121
  # Synchronization policy:
@@ -115,6 +123,7 @@ module ActiveRecord
115
123
  # * access to these instance variables needs to be in +synchronize+:
116
124
  # * @connections
117
125
  # * @now_connecting
126
+ # * @maintaining
118
127
  # * private methods that require being called in a +synchronize+ blocks
119
128
  # are now explicitly documented
120
129
  class ConnectionPool
@@ -171,21 +180,30 @@ module ActiveRecord
171
180
  end
172
181
  end
173
182
 
174
- class LeaseRegistry # :nodoc:
175
- def initialize
176
- @mutex = Mutex.new
177
- @map = WeakThreadKeyMap.new
183
+ if RUBY_ENGINE == "ruby"
184
+ # Thanks to the GVL, the LeaseRegistry doesn't need to be synchronized on MRI
185
+ class LeaseRegistry < WeakThreadKeyMap # :nodoc:
186
+ def [](context)
187
+ super || (self[context] = Lease.new)
188
+ end
178
189
  end
190
+ else
191
+ class LeaseRegistry # :nodoc:
192
+ def initialize
193
+ @mutex = Mutex.new
194
+ @map = WeakThreadKeyMap.new
195
+ end
179
196
 
180
- def [](context)
181
- @mutex.synchronize do
182
- @map[context] ||= Lease.new
197
+ def [](context)
198
+ @mutex.synchronize do
199
+ @map[context] ||= Lease.new
200
+ end
183
201
  end
184
- end
185
202
 
186
- def clear
187
- @mutex.synchronize do
188
- @map.clear
203
+ def clear
204
+ @mutex.synchronize do
205
+ @map.clear
206
+ end
189
207
  end
190
208
  end
191
209
  end
@@ -217,10 +235,10 @@ module ActiveRecord
217
235
 
218
236
  include MonitorMixin
219
237
  prepend QueryCache::ConnectionPoolConfiguration
220
- include ConnectionAdapters::AbstractPool
221
238
 
222
239
  attr_accessor :automatic_reconnect, :checkout_timeout
223
- attr_reader :db_config, :size, :reaper, :pool_config, :async_executor, :role, :shard
240
+ attr_reader :db_config, :max_connections, :min_connections, :max_age, :keepalive, :reaper, :pool_config, :async_executor, :role, :shard
241
+ alias :size :max_connections
224
242
 
225
243
  delegate :schema_reflection, :server_version, to: :pool_config
226
244
 
@@ -240,7 +258,10 @@ module ActiveRecord
240
258
 
241
259
  @checkout_timeout = db_config.checkout_timeout
242
260
  @idle_timeout = db_config.idle_timeout
243
- @size = db_config.pool
261
+ @max_connections = db_config.max_connections
262
+ @min_connections = db_config.min_connections
263
+ @max_age = db_config.max_age
264
+ @keepalive = db_config.keepalive
244
265
 
245
266
  # This variable tracks the cache of threads mapped to reserved connections, with the
246
267
  # sole purpose of speeding up the +connection+ method. It is not the authoritative
@@ -262,6 +283,12 @@ module ActiveRecord
262
283
  # currently in the process of independently establishing connections to the DB.
263
284
  @now_connecting = 0
264
285
 
286
+ # Sometimes otherwise-idle connections are temporarily held by the Reaper for
287
+ # maintenance. This variable tracks the number of connections currently in that
288
+ # state -- if a thread requests a connection and there are none available, it
289
+ # will await any in-maintenance connections in preference to creating a new one.
290
+ @maintaining = 0
291
+
265
292
  @threads_blocking_new_connections = 0
266
293
 
267
294
  @available = ConnectionLeasingQueue.new self
@@ -272,13 +299,17 @@ module ActiveRecord
272
299
 
273
300
  @schema_cache = nil
274
301
 
302
+ @activated = false
303
+ @original_context = ActiveSupport::IsolatedExecutionState.context
304
+
305
+ @reaper_lock = Monitor.new
275
306
  @reaper = Reaper.new(self, db_config.reaping_frequency)
276
307
  @reaper.run
277
308
  end
278
309
 
279
310
  def inspect # :nodoc:
280
- name_field = " name=#{db_config.name.inspect}" unless db_config.name == "primary"
281
- shard_field = " shard=#{@shard.inspect}" unless @shard == :default
311
+ name_field = " name=#{name_inspect}" if name_inspect
312
+ shard_field = " shard=#{shard_inspect}" if shard_inspect
282
313
 
283
314
  "#<#{self.class.name} env_name=#{db_config.env_name.inspect}#{name_field} role=#{role.inspect}#{shard_field}>"
284
315
  end
@@ -308,6 +339,14 @@ module ActiveRecord
308
339
  InternalMetadata.new(self)
309
340
  end
310
341
 
342
+ def activate
343
+ @activated = true
344
+ end
345
+
346
+ def activated?
347
+ @activated
348
+ end
349
+
311
350
  # Retrieve the connection associated with the current thread, or call
312
351
  # #checkout to obtain one if necessary.
313
352
  #
@@ -390,6 +429,8 @@ module ActiveRecord
390
429
  # #lease_connection or #with_connection methods, connections obtained through
391
430
  # #checkout will not be automatically released.
392
431
  def release_connection(existing_lease = nil)
432
+ return if self.discarded?
433
+
393
434
  if conn = connection_lease.release
394
435
  checkin conn
395
436
  return true
@@ -427,6 +468,24 @@ module ActiveRecord
427
468
  end
428
469
  end
429
470
 
471
+ def with_pool_transaction_isolation_level(isolation_level, transaction_open, &block) # :nodoc:
472
+ if !ActiveRecord.default_transaction_isolation_level.nil?
473
+ begin
474
+ if transaction_open && self.pool_transaction_isolation_level != ActiveRecord.default_transaction_isolation_level
475
+ raise ActiveRecord::TransactionIsolationError, "cannot set default isolation level while transaction is open"
476
+ end
477
+
478
+ old_level = self.pool_transaction_isolation_level
479
+ self.pool_transaction_isolation_level = isolation_level
480
+ yield
481
+ ensure
482
+ self.pool_transaction_isolation_level = old_level
483
+ end
484
+ else
485
+ yield
486
+ end
487
+ end
488
+
430
489
  # Returns true if a connection has already been opened.
431
490
  def connected?
432
491
  synchronize { @connections.any?(&:connected?) }
@@ -454,18 +513,26 @@ module ActiveRecord
454
513
  # connections in the pool within a timeout interval (default duration is
455
514
  # <tt>spec.db_config.checkout_timeout * 2</tt> seconds).
456
515
  def disconnect(raise_on_acquisition_timeout = true)
457
- with_exclusively_acquired_all_connections(raise_on_acquisition_timeout) do
458
- synchronize do
459
- @connections.each do |conn|
460
- if conn.in_use?
461
- conn.steal!
462
- checkin conn
516
+ @reaper_lock.synchronize do
517
+ return if self.discarded?
518
+
519
+ with_exclusively_acquired_all_connections(raise_on_acquisition_timeout) do
520
+ synchronize do
521
+ return if self.discarded?
522
+ @connections.each do |conn|
523
+ if conn.in_use?
524
+ conn.steal!
525
+ checkin conn
526
+ end
527
+ conn.disconnect!
463
528
  end
464
- conn.disconnect!
529
+ @connections = []
530
+ @leases.clear
531
+ @available.clear
532
+
533
+ # Stop maintaining the minimum size until reactivated
534
+ @activated = false
465
535
  end
466
- @connections = []
467
- @leases.clear
468
- @available.clear
469
536
  end
470
537
  end
471
538
  end
@@ -486,12 +553,14 @@ module ActiveRecord
486
553
  #
487
554
  # See AbstractAdapter#discard!
488
555
  def discard! # :nodoc:
489
- synchronize do
490
- return if self.discarded?
491
- @connections.each do |conn|
492
- conn.discard!
556
+ @reaper_lock.synchronize do
557
+ synchronize do
558
+ return if self.discarded?
559
+ @connections.each do |conn|
560
+ conn.discard!
561
+ end
562
+ @connections = @available = @leases = nil
493
563
  end
494
- @connections = @available = @leases = nil
495
564
  end
496
565
  end
497
566
 
@@ -499,6 +568,16 @@ module ActiveRecord
499
568
  @connections.nil?
500
569
  end
501
570
 
571
+ def maintainable? # :nodoc:
572
+ synchronize do
573
+ @connections&.size&.> 0 || (activated? && @min_connections > 0)
574
+ end
575
+ end
576
+
577
+ def reaper_lock(&block) # :nodoc:
578
+ @reaper_lock.synchronize(&block)
579
+ end
580
+
502
581
  # Clears reloadable connections from the pool and re-connects connections that
503
582
  # require reloading.
504
583
  #
@@ -582,11 +661,7 @@ module ActiveRecord
582
661
  conn.lock.synchronize do
583
662
  synchronize do
584
663
  connection_lease.clear(conn)
585
-
586
- conn._run_checkin_callbacks do
587
- conn.expire
588
- end
589
-
664
+ conn.expire
590
665
  @available.add conn
591
666
  end
592
667
  end
@@ -604,7 +679,7 @@ module ActiveRecord
604
679
  @available.delete conn
605
680
 
606
681
  # @available.any_waiting? => true means that prior to removing this
607
- # conn, the pool was at its max size (@connections.size == @size).
682
+ # conn, the pool was at its max size (@connections.size == @max_connections).
608
683
  # This would mean that any threads stuck waiting in the queue wouldn't
609
684
  # know they could checkout_new_connection, so let's do it for them.
610
685
  # Because condition-wait loop is encapsulated in the Queue class
@@ -612,14 +687,14 @@ module ActiveRecord
612
687
  # that are "stuck" there are helpless. They have no way of creating
613
688
  # new connections and are completely reliant on us feeding available
614
689
  # connections into the Queue.
615
- needs_new_connection = @available.any_waiting?
690
+ needs_new_connection = @available.num_waiting > @maintaining
616
691
  end
617
692
 
618
693
  # This is intentionally done outside of the synchronized section as we
619
694
  # would like not to hold the main mutex while checking out new connections.
620
695
  # Thus there is some chance that needs_new_connection information is now
621
696
  # stale, we can live with that (bulk_make_new_connections will make
622
- # sure not to exceed the pool's @size limit).
697
+ # sure not to exceed the pool's @max_connections limit).
623
698
  bulk_make_new_connections(1) if needs_new_connection
624
699
  end
625
700
 
@@ -652,11 +727,27 @@ module ActiveRecord
652
727
  def flush(minimum_idle = @idle_timeout)
653
728
  return if minimum_idle.nil?
654
729
 
655
- idle_connections = synchronize do
730
+ removed_connections = synchronize do
656
731
  return if self.discarded?
657
- @connections.select do |conn|
732
+
733
+ idle_connections = @connections.select do |conn|
658
734
  !conn.in_use? && conn.seconds_idle >= minimum_idle
659
- end.each do |conn|
735
+ end.sort_by { |conn| -conn.seconds_idle } # sort longest idle first
736
+
737
+ # Don't go below our configured pool minimum unless we're flushing
738
+ # everything
739
+ idles_to_retain =
740
+ if minimum_idle > 0
741
+ @min_connections - (@connections.size - idle_connections.size)
742
+ else
743
+ 0
744
+ end
745
+
746
+ if idles_to_retain > 0
747
+ idle_connections.pop idles_to_retain
748
+ end
749
+
750
+ idle_connections.each do |conn|
660
751
  conn.lease
661
752
 
662
753
  @available.delete conn
@@ -664,22 +755,109 @@ module ActiveRecord
664
755
  end
665
756
  end
666
757
 
667
- idle_connections.each do |conn|
758
+ removed_connections.each do |conn|
668
759
  conn.disconnect!
669
760
  end
670
761
  end
671
762
 
672
763
  # Disconnect all currently idle connections. Connections currently checked
673
- # out are unaffected.
764
+ # out are unaffected. The pool will stop maintaining its minimum size until
765
+ # it is reactivated (such as by a subsequent checkout).
674
766
  def flush!
675
767
  reap
676
768
  flush(-1)
769
+
770
+ # Stop maintaining the minimum size until reactivated
771
+ @activated = false
772
+ end
773
+
774
+ # Ensure that the pool contains at least the configured minimum number of
775
+ # connections.
776
+ def prepopulate
777
+ need_new_connections = nil
778
+
779
+ synchronize do
780
+ return if self.discarded?
781
+
782
+ # We don't want to start prepopulating until we know the pool is wanted,
783
+ # so we can avoid maintaining full pools in one-off scripts etc.
784
+ return unless @activated
785
+
786
+ need_new_connections = @connections.size < @min_connections
787
+ end
788
+
789
+ if need_new_connections
790
+ while new_conn = try_to_checkout_new_connection { @connections.size < @min_connections }
791
+ new_conn.allow_preconnect = true
792
+ checkin(new_conn)
793
+ end
794
+ end
795
+ end
796
+
797
+ def retire_old_connections(max_age = @max_age)
798
+ max_age ||= Float::INFINITY
799
+
800
+ sequential_maintenance -> c { c.connection_age&.>= c.pool_jitter(max_age) } do |conn|
801
+ # Disconnect, then return the adapter to the pool. Preconnect will
802
+ # handle the rest.
803
+ conn.disconnect!
804
+ end
805
+ end
806
+
807
+ # Preconnect all connections in the pool. This saves pool users from
808
+ # having to wait for a connection to be established when first using it
809
+ # after checkout.
810
+ def preconnect
811
+ sequential_maintenance -> c { (!c.connected? || !c.verified?) && c.allow_preconnect } do |conn|
812
+ conn.connect!
813
+ rescue
814
+ # Wholesale rescue: there's nothing we can do but move on. The
815
+ # connection will go back to the pool, and the next consumer will
816
+ # presumably try to connect again -- which will either work, or
817
+ # fail and they'll be able to report the exception.
818
+ end
819
+ end
820
+
821
+ # Prod any connections that have been idle for longer than the configured
822
+ # keepalive time. This will incidentally verify the connection is still
823
+ # alive, but the main purpose is to show the server (and any intermediate
824
+ # network hops) that we're still here and using the connection.
825
+ def keep_alive(threshold = @keepalive)
826
+ return if threshold.nil?
827
+
828
+ sequential_maintenance -> c { (c.seconds_since_last_activity || 0) > c.pool_jitter(threshold) } do |conn|
829
+ # conn.active? will cause some amount of network activity, which is all
830
+ # we need to provide a keepalive signal.
831
+ #
832
+ # If it returns false, the connection is already broken; disconnect,
833
+ # so it can be found and repaired.
834
+ conn.disconnect! unless conn.active?
835
+ end
836
+ end
837
+
838
+ # Immediately mark all current connections as due for replacement,
839
+ # equivalent to them having reached +max_age+ -- even if there is
840
+ # no +max_age+ configured.
841
+ def recycle!
842
+ synchronize do
843
+ return if self.discarded?
844
+
845
+ @connections.each do |conn|
846
+ conn.force_retirement
847
+ end
848
+ end
849
+
850
+ retire_old_connections
677
851
  end
678
852
 
679
853
  def num_waiting_in_queue # :nodoc:
680
854
  @available.num_waiting
681
855
  end
682
856
 
857
+ def num_available_in_queue # :nodoc:
858
+ @available.size
859
+ end
860
+
683
861
  # Returns the connection pool's usage statistic.
684
862
  #
685
863
  # ActiveRecord::Base.connection_pool.stat # => { size: 15, connections: 1, busy: 1, dead: 0, idle: 0, waiting: 0, checkout_timeout: 5 }
@@ -710,6 +888,16 @@ module ActiveRecord
710
888
  raise ex.set_pool(self)
711
889
  end
712
890
 
891
+ def pool_transaction_isolation_level
892
+ isolation_level_key = "activerecord_pool_transaction_isolation_level_#{db_config.name}"
893
+ ActiveSupport::IsolatedExecutionState[isolation_level_key]
894
+ end
895
+
896
+ def pool_transaction_isolation_level=(isolation_level)
897
+ isolation_level_key = "activerecord_pool_transaction_isolation_level_#{db_config.name}"
898
+ ActiveSupport::IsolatedExecutionState[isolation_level_key] = isolation_level
899
+ end
900
+
713
901
  private
714
902
  def connection_lease
715
903
  @leases[ActiveSupport::IsolatedExecutionState.context]
@@ -719,7 +907,9 @@ module ActiveRecord
719
907
  case ActiveRecord.async_query_executor
720
908
  when :multi_thread_pool
721
909
  if @db_config.max_threads > 0
910
+ name_with_shard = [name_inspect, shard_inspect].join("-").tr("_", "-")
722
911
  Concurrent::ThreadPoolExecutor.new(
912
+ name: "ActiveRecord-#{name_with_shard}-async-query-executor",
723
913
  min_threads: @db_config.min_threads,
724
914
  max_threads: @db_config.max_threads,
725
915
  max_queue: @db_config.max_queue,
@@ -731,11 +921,109 @@ module ActiveRecord
731
921
  end
732
922
  end
733
923
 
924
+ # Perform maintenance work on pool connections. This method will
925
+ # select a connection to work on by calling the +candidate_selector+
926
+ # proc while holding the pool lock. If a connection is selected, it
927
+ # will be checked out for maintenance and passed to the
928
+ # +maintenance_work+ proc. The connection will always be returned to
929
+ # the pool after the proc completes.
930
+ #
931
+ # If the pool has async threads, all work will be scheduled there.
932
+ # Otherwise, this method will block until all work is complete.
933
+ #
934
+ # Each connection will only be processed once per call to this method,
935
+ # but (particularly in the async case) there is no protection against
936
+ # a second call to this method starting to work through the list
937
+ # before the first call has completed. (Though regular pool behavior
938
+ # will prevent two instances from working on the same specific
939
+ # connection at the same time.)
940
+ def sequential_maintenance(candidate_selector, &maintenance_work)
941
+ # This hash doesn't need to be synchronized, because it's only
942
+ # used by one thread at a time: the +perform_work+ block gives
943
+ # up its right to +connections_visited+ when it schedules the
944
+ # next iteration.
945
+ connections_visited = Hash.new(false)
946
+ connections_visited.compare_by_identity
947
+
948
+ perform_work = lambda do
949
+ connection_to_maintain = nil
950
+
951
+ synchronize do
952
+ unless self.discarded?
953
+ if connection_to_maintain = @connections.select { |conn| !conn.in_use? }.select(&candidate_selector).sort_by(&:seconds_idle).find { |conn| !connections_visited[conn] }
954
+ checkout_for_maintenance connection_to_maintain
955
+ end
956
+ end
957
+ end
958
+
959
+ if connection_to_maintain
960
+ connections_visited[connection_to_maintain] = true
961
+
962
+ # If we're running async, we can schedule the next round of work
963
+ # as soon as we've grabbed a connection to work on.
964
+ @async_executor&.post(&perform_work)
965
+
966
+ begin
967
+ maintenance_work.call connection_to_maintain
968
+ ensure
969
+ return_from_maintenance connection_to_maintain
970
+ end
971
+
972
+ true
973
+ end
974
+ end
975
+
976
+ if @async_executor
977
+ @async_executor.post(&perform_work)
978
+ else
979
+ nil while perform_work.call
980
+ end
981
+ end
982
+
983
+ # Directly check a specific connection out of the pool. Skips callbacks.
984
+ #
985
+ # The connection must later either #return_from_maintenance or
986
+ # #remove_from_maintenance, or the pool will hang.
987
+ def checkout_for_maintenance(conn)
988
+ synchronize do
989
+ @maintaining += 1
990
+ @available.delete(conn)
991
+ conn.lease
992
+ conn
993
+ end
994
+ end
995
+
996
+ # Return a connection to the pool after it has been checked out for
997
+ # maintenance. Does not update the connection's idle time, and skips
998
+ # callbacks.
999
+ #--
1000
+ # We assume that a connection that has required maintenance is less
1001
+ # desirable (either it's been idle for a long time, or it was just
1002
+ # created and hasn't been used yet). We'll put it at the back of the
1003
+ # queue.
1004
+ def return_from_maintenance(conn)
1005
+ synchronize do
1006
+ conn.expire(false)
1007
+ @available.add_back(conn)
1008
+ @maintaining -= 1
1009
+ end
1010
+ end
1011
+
1012
+ # Remove a connection from the pool after it has been checked out for
1013
+ # maintenance. It will be automatically replaced with a new connection if
1014
+ # necessary.
1015
+ def remove_from_maintenance(conn)
1016
+ synchronize do
1017
+ @maintaining -= 1
1018
+ remove conn
1019
+ end
1020
+ end
1021
+
734
1022
  #--
735
1023
  # this is unfortunately not concurrent
736
1024
  def bulk_make_new_connections(num_new_conns_needed)
737
1025
  num_new_conns_needed.times do
738
- # try_to_checkout_new_connection will not exceed pool's @size limit
1026
+ # try_to_checkout_new_connection will not exceed pool's @max_connections limit
739
1027
  if new_conn = try_to_checkout_new_connection
740
1028
  # make the new_conn available to the starving threads stuck @available Queue
741
1029
  checkin(new_conn)
@@ -748,9 +1036,11 @@ module ActiveRecord
748
1036
  # wrap it in +synchronize+ because some pool's actions are allowed
749
1037
  # to be performed outside of the main +synchronize+ block.
750
1038
  def with_exclusively_acquired_all_connections(raise_on_acquisition_timeout = true)
751
- with_new_connections_blocked do
752
- attempt_to_checkout_all_existing_connections(raise_on_acquisition_timeout)
753
- yield
1039
+ @reaper_lock.synchronize do
1040
+ with_new_connections_blocked do
1041
+ attempt_to_checkout_all_existing_connections(raise_on_acquisition_timeout)
1042
+ yield
1043
+ end
754
1044
  end
755
1045
  end
756
1046
 
@@ -870,13 +1160,13 @@ module ActiveRecord
870
1160
  # <tt>synchronize { conn.lease }</tt> in this method, but by leaving it to <tt>@available.poll</tt>
871
1161
  # and +try_to_checkout_new_connection+ we can piggyback on +synchronize+ sections
872
1162
  # of the said methods and avoid an additional +synchronize+ overhead.
873
- if conn = @available.poll || try_to_checkout_new_connection
1163
+ if conn = @available.poll || try_to_queue_for_background_connection(checkout_timeout) || try_to_checkout_new_connection
874
1164
  conn
875
1165
  else
876
1166
  reap
877
1167
  # Retry after reaping, which may return an available connection,
878
1168
  # remove an inactive connection, or both
879
- if conn = @available.poll || try_to_checkout_new_connection
1169
+ if conn = @available.poll || try_to_queue_for_background_connection(checkout_timeout) || try_to_checkout_new_connection
880
1170
  conn
881
1171
  else
882
1172
  @available.poll(checkout_timeout)
@@ -886,6 +1176,31 @@ module ActiveRecord
886
1176
  raise ex.set_pool(self)
887
1177
  end
888
1178
 
1179
+ #--
1180
+ # If new connections are already being established in the background,
1181
+ # and there are fewer threads already waiting than the number of
1182
+ # upcoming connections, we can just get in queue and wait to be handed a
1183
+ # connection. This avoids us overshooting the required connection count
1184
+ # by starting a new connection ourselves, and is likely to be faster
1185
+ # too (because at least some of the time it takes to establish a new
1186
+ # connection must have already passed).
1187
+ #
1188
+ # If background connections are available, this method will block and
1189
+ # return a connection. If no background connections are available, it
1190
+ # will immediately return +nil+.
1191
+ def try_to_queue_for_background_connection(checkout_timeout)
1192
+ return unless @maintaining > 0
1193
+
1194
+ synchronize do
1195
+ return unless @maintaining > @available.num_waiting
1196
+
1197
+ # We are guaranteed the "maintaining" thread will return its promised
1198
+ # connection within one maintenance-unit of time. Thus we can safely
1199
+ # do a blocking wait with (functionally) no timeout.
1200
+ @available.poll(100)
1201
+ end
1202
+ end
1203
+
889
1204
  #--
890
1205
  # if owner_thread param is omitted, this must be called in synchronize block
891
1206
  def remove_connection_from_thread_cache(conn, owner_thread = conn.owner)
@@ -895,17 +1210,26 @@ module ActiveRecord
895
1210
  end
896
1211
  alias_method :release, :remove_connection_from_thread_cache
897
1212
 
898
- # If the pool is not at a <tt>@size</tt> limit, establish new connection. Connecting
1213
+ # If the pool is not at a <tt>@max_connections</tt> limit, establish new connection. Connecting
899
1214
  # to the DB is done outside main synchronized section.
1215
+ #
1216
+ # If a block is supplied, it is an additional constraint (checked while holding the
1217
+ # pool lock) on whether a new connection should be established.
900
1218
  #--
901
1219
  # Implementation constraint: a newly established connection returned by this
902
1220
  # method must be in the +.leased+ state.
903
1221
  def try_to_checkout_new_connection
904
1222
  # first in synchronized section check if establishing new conns is allowed
905
- # and increment @now_connecting, to prevent overstepping this pool's @size
1223
+ # and increment @now_connecting, to prevent overstepping this pool's @max_connections
906
1224
  # constraint
907
1225
  do_checkout = synchronize do
908
- if @threads_blocking_new_connections.zero? && (@connections.size + @now_connecting) < @size
1226
+ return if self.discarded?
1227
+
1228
+ if @threads_blocking_new_connections.zero? && (@max_connections.nil? || (@connections.size + @now_connecting) < @max_connections) && (!block_given? || yield)
1229
+ if @connections.size > 0 || @original_context != ActiveSupport::IsolatedExecutionState.context
1230
+ @activated = true
1231
+ end
1232
+
909
1233
  @now_connecting += 1
910
1234
  end
911
1235
  end
@@ -916,12 +1240,16 @@ module ActiveRecord
916
1240
  conn = checkout_new_connection
917
1241
  ensure
918
1242
  synchronize do
1243
+ @now_connecting -= 1
919
1244
  if conn
920
- adopt_connection(conn)
921
- # returned conn needs to be already leased
922
- conn.lease
1245
+ if self.discarded?
1246
+ conn.discard!
1247
+ else
1248
+ adopt_connection(conn)
1249
+ # returned conn needs to be already leased
1250
+ conn.lease
1251
+ end
923
1252
  end
924
- @now_connecting -= 1
925
1253
  end
926
1254
  end
927
1255
  end
@@ -944,15 +1272,20 @@ module ActiveRecord
944
1272
  end
945
1273
 
946
1274
  def checkout_and_verify(c)
947
- c._run_checkout_callbacks do
948
- c.clean!
949
- end
950
- c
1275
+ c.clean!
951
1276
  rescue Exception
952
1277
  remove c
953
1278
  c.disconnect!
954
1279
  raise
955
1280
  end
1281
+
1282
+ def name_inspect
1283
+ db_config.name.inspect unless db_config.name == "primary"
1284
+ end
1285
+
1286
+ def shard_inspect
1287
+ shard.inspect unless shard == :default
1288
+ end
956
1289
  end
957
1290
  end
958
1291
  end