activerecord 5.2.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activerecord might be problematic. Click here for more details.

Files changed (244) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +937 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +217 -0
  5. data/examples/performance.rb +185 -0
  6. data/examples/simple.rb +15 -0
  7. data/lib/active_record.rb +188 -0
  8. data/lib/active_record/aggregations.rb +283 -0
  9. data/lib/active_record/association_relation.rb +40 -0
  10. data/lib/active_record/associations.rb +1860 -0
  11. data/lib/active_record/associations/alias_tracker.rb +81 -0
  12. data/lib/active_record/associations/association.rb +299 -0
  13. data/lib/active_record/associations/association_scope.rb +168 -0
  14. data/lib/active_record/associations/belongs_to_association.rb +130 -0
  15. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +40 -0
  16. data/lib/active_record/associations/builder/association.rb +140 -0
  17. data/lib/active_record/associations/builder/belongs_to.rb +163 -0
  18. data/lib/active_record/associations/builder/collection_association.rb +82 -0
  19. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +135 -0
  20. data/lib/active_record/associations/builder/has_many.rb +17 -0
  21. data/lib/active_record/associations/builder/has_one.rb +30 -0
  22. data/lib/active_record/associations/builder/singular_association.rb +42 -0
  23. data/lib/active_record/associations/collection_association.rb +513 -0
  24. data/lib/active_record/associations/collection_proxy.rb +1131 -0
  25. data/lib/active_record/associations/foreign_association.rb +13 -0
  26. data/lib/active_record/associations/has_many_association.rb +144 -0
  27. data/lib/active_record/associations/has_many_through_association.rb +227 -0
  28. data/lib/active_record/associations/has_one_association.rb +120 -0
  29. data/lib/active_record/associations/has_one_through_association.rb +45 -0
  30. data/lib/active_record/associations/join_dependency.rb +262 -0
  31. data/lib/active_record/associations/join_dependency/join_association.rb +60 -0
  32. data/lib/active_record/associations/join_dependency/join_base.rb +23 -0
  33. data/lib/active_record/associations/join_dependency/join_part.rb +71 -0
  34. data/lib/active_record/associations/preloader.rb +193 -0
  35. data/lib/active_record/associations/preloader/association.rb +131 -0
  36. data/lib/active_record/associations/preloader/through_association.rb +107 -0
  37. data/lib/active_record/associations/singular_association.rb +73 -0
  38. data/lib/active_record/associations/through_association.rb +121 -0
  39. data/lib/active_record/attribute_assignment.rb +88 -0
  40. data/lib/active_record/attribute_decorators.rb +90 -0
  41. data/lib/active_record/attribute_methods.rb +492 -0
  42. data/lib/active_record/attribute_methods/before_type_cast.rb +78 -0
  43. data/lib/active_record/attribute_methods/dirty.rb +150 -0
  44. data/lib/active_record/attribute_methods/primary_key.rb +143 -0
  45. data/lib/active_record/attribute_methods/query.rb +42 -0
  46. data/lib/active_record/attribute_methods/read.rb +85 -0
  47. data/lib/active_record/attribute_methods/serialization.rb +90 -0
  48. data/lib/active_record/attribute_methods/time_zone_conversion.rb +91 -0
  49. data/lib/active_record/attribute_methods/write.rb +68 -0
  50. data/lib/active_record/attributes.rb +266 -0
  51. data/lib/active_record/autosave_association.rb +498 -0
  52. data/lib/active_record/base.rb +329 -0
  53. data/lib/active_record/callbacks.rb +353 -0
  54. data/lib/active_record/coders/json.rb +15 -0
  55. data/lib/active_record/coders/yaml_column.rb +50 -0
  56. data/lib/active_record/collection_cache_key.rb +53 -0
  57. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +1068 -0
  58. data/lib/active_record/connection_adapters/abstract/database_limits.rb +72 -0
  59. data/lib/active_record/connection_adapters/abstract/database_statements.rb +540 -0
  60. data/lib/active_record/connection_adapters/abstract/query_cache.rb +145 -0
  61. data/lib/active_record/connection_adapters/abstract/quoting.rb +200 -0
  62. data/lib/active_record/connection_adapters/abstract/savepoints.rb +23 -0
  63. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +146 -0
  64. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +685 -0
  65. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +95 -0
  66. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +1396 -0
  67. data/lib/active_record/connection_adapters/abstract/transaction.rb +283 -0
  68. data/lib/active_record/connection_adapters/abstract_adapter.rb +628 -0
  69. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +887 -0
  70. data/lib/active_record/connection_adapters/column.rb +91 -0
  71. data/lib/active_record/connection_adapters/connection_specification.rb +287 -0
  72. data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +33 -0
  73. data/lib/active_record/connection_adapters/mysql/column.rb +27 -0
  74. data/lib/active_record/connection_adapters/mysql/database_statements.rb +140 -0
  75. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +72 -0
  76. data/lib/active_record/connection_adapters/mysql/quoting.rb +44 -0
  77. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +73 -0
  78. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +87 -0
  79. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +80 -0
  80. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +148 -0
  81. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +35 -0
  82. data/lib/active_record/connection_adapters/mysql2_adapter.rb +129 -0
  83. data/lib/active_record/connection_adapters/postgresql/column.rb +44 -0
  84. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +163 -0
  85. data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +44 -0
  86. data/lib/active_record/connection_adapters/postgresql/oid.rb +34 -0
  87. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +92 -0
  88. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +56 -0
  89. data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +15 -0
  90. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +17 -0
  91. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +50 -0
  92. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +23 -0
  93. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +23 -0
  94. data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +15 -0
  95. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +21 -0
  96. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +71 -0
  97. data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +15 -0
  98. data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +15 -0
  99. data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +45 -0
  100. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +41 -0
  101. data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +15 -0
  102. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +65 -0
  103. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +97 -0
  104. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +18 -0
  105. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +111 -0
  106. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +23 -0
  107. data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +28 -0
  108. data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +30 -0
  109. data/lib/active_record/connection_adapters/postgresql/quoting.rb +168 -0
  110. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +43 -0
  111. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +65 -0
  112. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +206 -0
  113. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +50 -0
  114. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +774 -0
  115. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +39 -0
  116. data/lib/active_record/connection_adapters/postgresql/utils.rb +81 -0
  117. data/lib/active_record/connection_adapters/postgresql_adapter.rb +863 -0
  118. data/lib/active_record/connection_adapters/schema_cache.rb +118 -0
  119. data/lib/active_record/connection_adapters/sql_type_metadata.rb +34 -0
  120. data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +21 -0
  121. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +67 -0
  122. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +17 -0
  123. data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +19 -0
  124. data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +18 -0
  125. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +106 -0
  126. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +573 -0
  127. data/lib/active_record/connection_adapters/statement_pool.rb +61 -0
  128. data/lib/active_record/connection_handling.rb +145 -0
  129. data/lib/active_record/core.rb +559 -0
  130. data/lib/active_record/counter_cache.rb +218 -0
  131. data/lib/active_record/define_callbacks.rb +22 -0
  132. data/lib/active_record/dynamic_matchers.rb +122 -0
  133. data/lib/active_record/enum.rb +244 -0
  134. data/lib/active_record/errors.rb +380 -0
  135. data/lib/active_record/explain.rb +50 -0
  136. data/lib/active_record/explain_registry.rb +32 -0
  137. data/lib/active_record/explain_subscriber.rb +34 -0
  138. data/lib/active_record/fixture_set/file.rb +82 -0
  139. data/lib/active_record/fixtures.rb +1065 -0
  140. data/lib/active_record/gem_version.rb +17 -0
  141. data/lib/active_record/inheritance.rb +283 -0
  142. data/lib/active_record/integration.rb +155 -0
  143. data/lib/active_record/internal_metadata.rb +45 -0
  144. data/lib/active_record/legacy_yaml_adapter.rb +48 -0
  145. data/lib/active_record/locale/en.yml +48 -0
  146. data/lib/active_record/locking/optimistic.rb +198 -0
  147. data/lib/active_record/locking/pessimistic.rb +89 -0
  148. data/lib/active_record/log_subscriber.rb +137 -0
  149. data/lib/active_record/migration.rb +1378 -0
  150. data/lib/active_record/migration/command_recorder.rb +240 -0
  151. data/lib/active_record/migration/compatibility.rb +217 -0
  152. data/lib/active_record/migration/join_table.rb +17 -0
  153. data/lib/active_record/model_schema.rb +521 -0
  154. data/lib/active_record/nested_attributes.rb +600 -0
  155. data/lib/active_record/no_touching.rb +58 -0
  156. data/lib/active_record/null_relation.rb +68 -0
  157. data/lib/active_record/persistence.rb +763 -0
  158. data/lib/active_record/query_cache.rb +45 -0
  159. data/lib/active_record/querying.rb +70 -0
  160. data/lib/active_record/railtie.rb +226 -0
  161. data/lib/active_record/railties/console_sandbox.rb +7 -0
  162. data/lib/active_record/railties/controller_runtime.rb +56 -0
  163. data/lib/active_record/railties/databases.rake +377 -0
  164. data/lib/active_record/readonly_attributes.rb +24 -0
  165. data/lib/active_record/reflection.rb +1044 -0
  166. data/lib/active_record/relation.rb +629 -0
  167. data/lib/active_record/relation/batches.rb +287 -0
  168. data/lib/active_record/relation/batches/batch_enumerator.rb +69 -0
  169. data/lib/active_record/relation/calculations.rb +417 -0
  170. data/lib/active_record/relation/delegation.rb +147 -0
  171. data/lib/active_record/relation/finder_methods.rb +565 -0
  172. data/lib/active_record/relation/from_clause.rb +26 -0
  173. data/lib/active_record/relation/merger.rb +193 -0
  174. data/lib/active_record/relation/predicate_builder.rb +152 -0
  175. data/lib/active_record/relation/predicate_builder/array_handler.rb +48 -0
  176. data/lib/active_record/relation/predicate_builder/association_query_value.rb +46 -0
  177. data/lib/active_record/relation/predicate_builder/base_handler.rb +19 -0
  178. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +20 -0
  179. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +56 -0
  180. data/lib/active_record/relation/predicate_builder/range_handler.rb +42 -0
  181. data/lib/active_record/relation/predicate_builder/relation_handler.rb +19 -0
  182. data/lib/active_record/relation/query_attribute.rb +45 -0
  183. data/lib/active_record/relation/query_methods.rb +1231 -0
  184. data/lib/active_record/relation/record_fetch_warning.rb +51 -0
  185. data/lib/active_record/relation/spawn_methods.rb +77 -0
  186. data/lib/active_record/relation/where_clause.rb +186 -0
  187. data/lib/active_record/relation/where_clause_factory.rb +34 -0
  188. data/lib/active_record/result.rb +149 -0
  189. data/lib/active_record/runtime_registry.rb +24 -0
  190. data/lib/active_record/sanitization.rb +222 -0
  191. data/lib/active_record/schema.rb +70 -0
  192. data/lib/active_record/schema_dumper.rb +255 -0
  193. data/lib/active_record/schema_migration.rb +56 -0
  194. data/lib/active_record/scoping.rb +106 -0
  195. data/lib/active_record/scoping/default.rb +152 -0
  196. data/lib/active_record/scoping/named.rb +213 -0
  197. data/lib/active_record/secure_token.rb +40 -0
  198. data/lib/active_record/serialization.rb +22 -0
  199. data/lib/active_record/statement_cache.rb +121 -0
  200. data/lib/active_record/store.rb +211 -0
  201. data/lib/active_record/suppressor.rb +61 -0
  202. data/lib/active_record/table_metadata.rb +82 -0
  203. data/lib/active_record/tasks/database_tasks.rb +337 -0
  204. data/lib/active_record/tasks/mysql_database_tasks.rb +115 -0
  205. data/lib/active_record/tasks/postgresql_database_tasks.rb +143 -0
  206. data/lib/active_record/tasks/sqlite_database_tasks.rb +83 -0
  207. data/lib/active_record/timestamp.rb +153 -0
  208. data/lib/active_record/touch_later.rb +64 -0
  209. data/lib/active_record/transactions.rb +502 -0
  210. data/lib/active_record/translation.rb +24 -0
  211. data/lib/active_record/type.rb +79 -0
  212. data/lib/active_record/type/adapter_specific_registry.rb +136 -0
  213. data/lib/active_record/type/date.rb +9 -0
  214. data/lib/active_record/type/date_time.rb +9 -0
  215. data/lib/active_record/type/decimal_without_scale.rb +15 -0
  216. data/lib/active_record/type/hash_lookup_type_map.rb +25 -0
  217. data/lib/active_record/type/internal/timezone.rb +17 -0
  218. data/lib/active_record/type/json.rb +30 -0
  219. data/lib/active_record/type/serialized.rb +71 -0
  220. data/lib/active_record/type/text.rb +11 -0
  221. data/lib/active_record/type/time.rb +21 -0
  222. data/lib/active_record/type/type_map.rb +62 -0
  223. data/lib/active_record/type/unsigned_integer.rb +17 -0
  224. data/lib/active_record/type_caster.rb +9 -0
  225. data/lib/active_record/type_caster/connection.rb +33 -0
  226. data/lib/active_record/type_caster/map.rb +23 -0
  227. data/lib/active_record/validations.rb +93 -0
  228. data/lib/active_record/validations/absence.rb +25 -0
  229. data/lib/active_record/validations/associated.rb +60 -0
  230. data/lib/active_record/validations/length.rb +26 -0
  231. data/lib/active_record/validations/presence.rb +68 -0
  232. data/lib/active_record/validations/uniqueness.rb +238 -0
  233. data/lib/active_record/version.rb +10 -0
  234. data/lib/rails/generators/active_record.rb +19 -0
  235. data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
  236. data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +5 -0
  237. data/lib/rails/generators/active_record/migration.rb +35 -0
  238. data/lib/rails/generators/active_record/migration/migration_generator.rb +78 -0
  239. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +24 -0
  240. data/lib/rails/generators/active_record/migration/templates/migration.rb.tt +46 -0
  241. data/lib/rails/generators/active_record/model/model_generator.rb +48 -0
  242. data/lib/rails/generators/active_record/model/templates/model.rb.tt +13 -0
  243. data/lib/rails/generators/active_record/model/templates/module.rb.tt +7 -0
  244. metadata +333 -0
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module Coders # :nodoc:
5
+ class JSON # :nodoc:
6
+ def self.dump(obj)
7
+ ActiveSupport::JSON.encode(obj)
8
+ end
9
+
10
+ def self.load(json)
11
+ ActiveSupport::JSON.decode(json) unless json.blank?
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
4
+
5
+ module ActiveRecord
6
+ module Coders # :nodoc:
7
+ class YAMLColumn # :nodoc:
8
+ attr_accessor :object_class
9
+
10
+ def initialize(attr_name, object_class = Object)
11
+ @attr_name = attr_name
12
+ @object_class = object_class
13
+ check_arity_of_constructor
14
+ end
15
+
16
+ def dump(obj)
17
+ return if obj.nil?
18
+
19
+ assert_valid_value(obj, action: "dump")
20
+ YAML.dump obj
21
+ end
22
+
23
+ def load(yaml)
24
+ return object_class.new if object_class != Object && yaml.nil?
25
+ return yaml unless yaml.is_a?(String) && /^---/.match?(yaml)
26
+ obj = YAML.load(yaml)
27
+
28
+ assert_valid_value(obj, action: "load")
29
+ obj ||= object_class.new if object_class != Object
30
+
31
+ obj
32
+ end
33
+
34
+ def assert_valid_value(obj, action:)
35
+ unless obj.nil? || obj.is_a?(object_class)
36
+ raise SerializationTypeMismatch,
37
+ "can't #{action} `#{@attr_name}`: was supposed to be a #{object_class}, but was a #{obj.class}. -- #{obj.inspect}"
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ def check_arity_of_constructor
44
+ load(nil)
45
+ rescue ArgumentError
46
+ raise ArgumentError, "Cannot serialize #{object_class}. Classes passed to `serialize` must have a 0 argument constructor."
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module CollectionCacheKey
5
+ def collection_cache_key(collection = all, timestamp_column = :updated_at) # :nodoc:
6
+ query_signature = ActiveSupport::Digest.hexdigest(collection.to_sql)
7
+ key = "#{collection.model_name.cache_key}/query-#{query_signature}"
8
+
9
+ if collection.loaded? || collection.distinct_value
10
+ size = collection.records.size
11
+ if size > 0
12
+ timestamp = collection.max_by(&timestamp_column)._read_attribute(timestamp_column)
13
+ end
14
+ else
15
+ if collection.eager_loading?
16
+ collection = collection.send(:apply_join_dependency)
17
+ end
18
+ column_type = type_for_attribute(timestamp_column)
19
+ column = connection.column_name_from_arel_node(collection.arel_attribute(timestamp_column))
20
+ select_values = "COUNT(*) AS #{connection.quote_column_name("size")}, MAX(%s) AS timestamp"
21
+
22
+ if collection.has_limit_or_offset?
23
+ query = collection.select("#{column} AS collection_cache_key_timestamp")
24
+ subquery_alias = "subquery_for_cache_key"
25
+ subquery_column = "#{subquery_alias}.collection_cache_key_timestamp"
26
+ subquery = query.arel.as(subquery_alias)
27
+ arel = Arel::SelectManager.new(subquery).project(select_values % subquery_column)
28
+ else
29
+ query = collection.unscope(:order)
30
+ query.select_values = [select_values % column]
31
+ arel = query.arel
32
+ end
33
+
34
+ result = connection.select_one(arel, nil)
35
+
36
+ if result.blank?
37
+ size = 0
38
+ timestamp = nil
39
+ else
40
+ size = result["size"]
41
+ timestamp = column_type.deserialize(result["timestamp"])
42
+ end
43
+
44
+ end
45
+
46
+ if timestamp
47
+ "#{key}-#{size}-#{timestamp.utc.to_s(cache_timestamp_format)}"
48
+ else
49
+ "#{key}-#{size}"
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,1068 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "thread"
4
+ require "concurrent/map"
5
+ require "monitor"
6
+
7
+ module ActiveRecord
8
+ # Raised when a connection could not be obtained within the connection
9
+ # acquisition timeout period: because max connections in pool
10
+ # are in use.
11
+ class ConnectionTimeoutError < ConnectionNotEstablished
12
+ end
13
+
14
+ # Raised when a pool was unable to get ahold of all its connections
15
+ # to perform a "group" action such as
16
+ # {ActiveRecord::Base.connection_pool.disconnect!}[rdoc-ref:ConnectionAdapters::ConnectionPool#disconnect!]
17
+ # or {ActiveRecord::Base.clear_reloadable_connections!}[rdoc-ref:ConnectionAdapters::ConnectionHandler#clear_reloadable_connections!].
18
+ class ExclusiveConnectionTimeoutError < ConnectionTimeoutError
19
+ end
20
+
21
+ module ConnectionAdapters
22
+ # Connection pool base class for managing Active Record database
23
+ # connections.
24
+ #
25
+ # == Introduction
26
+ #
27
+ # A connection pool synchronizes thread access to a limited number of
28
+ # database connections. The basic idea is that each thread checks out a
29
+ # database connection from the pool, uses that connection, and checks the
30
+ # connection back in. ConnectionPool is completely thread-safe, and will
31
+ # ensure that a connection cannot be used by two threads at the same time,
32
+ # as long as ConnectionPool's contract is correctly followed. It will also
33
+ # handle cases in which there are more threads than connections: if all
34
+ # connections have been checked out, and a thread tries to checkout a
35
+ # connection anyway, then ConnectionPool will wait until some other thread
36
+ # has checked in a connection.
37
+ #
38
+ # == Obtaining (checking out) a connection
39
+ #
40
+ # Connections can be obtained and used from a connection pool in several
41
+ # ways:
42
+ #
43
+ # 1. Simply use {ActiveRecord::Base.connection}[rdoc-ref:ConnectionHandling.connection]
44
+ # as with Active Record 2.1 and
45
+ # earlier (pre-connection-pooling). Eventually, when you're done with
46
+ # the connection(s) and wish it to be returned to the pool, you call
47
+ # {ActiveRecord::Base.clear_active_connections!}[rdoc-ref:ConnectionAdapters::ConnectionHandler#clear_active_connections!].
48
+ # This will be the default behavior for Active Record when used in conjunction with
49
+ # Action Pack's request handling cycle.
50
+ # 2. Manually check out a connection from the pool with
51
+ # {ActiveRecord::Base.connection_pool.checkout}[rdoc-ref:#checkout]. You are responsible for
52
+ # returning this connection to the pool when finished by calling
53
+ # {ActiveRecord::Base.connection_pool.checkin(connection)}[rdoc-ref:#checkin].
54
+ # 3. Use {ActiveRecord::Base.connection_pool.with_connection(&block)}[rdoc-ref:#with_connection], which
55
+ # obtains a connection, yields it as the sole argument to the block,
56
+ # and returns it to the pool after the block completes.
57
+ #
58
+ # Connections in the pool are actually AbstractAdapter objects (or objects
59
+ # compatible with AbstractAdapter's interface).
60
+ #
61
+ # == Options
62
+ #
63
+ # There are several connection-pooling-related options that you can add to
64
+ # your database connection configuration:
65
+ #
66
+ # * +pool+: maximum number of connections the pool may manage (default 5).
67
+ # * +idle_timeout+: number of seconds that a connection will be kept
68
+ # unused in the pool before it is automatically disconnected (default
69
+ # 300 seconds). Set this to zero to keep connections forever.
70
+ # * +checkout_timeout+: number of seconds to wait for a connection to
71
+ # become available before giving up and raising a timeout error (default
72
+ # 5 seconds).
73
+ #
74
+ #--
75
+ # Synchronization policy:
76
+ # * all public methods can be called outside +synchronize+
77
+ # * access to these instance variables needs to be in +synchronize+:
78
+ # * @connections
79
+ # * @now_connecting
80
+ # * private methods that require being called in a +synchronize+ blocks
81
+ # are now explicitly documented
82
+ class ConnectionPool
83
+ # Threadsafe, fair, LIFO queue. Meant to be used by ConnectionPool
84
+ # with which it shares a Monitor.
85
+ class Queue
86
+ def initialize(lock = Monitor.new)
87
+ @lock = lock
88
+ @cond = @lock.new_cond
89
+ @num_waiting = 0
90
+ @queue = []
91
+ end
92
+
93
+ # Test if any threads are currently waiting on the queue.
94
+ def any_waiting?
95
+ synchronize do
96
+ @num_waiting > 0
97
+ end
98
+ end
99
+
100
+ # Returns the number of threads currently waiting on this
101
+ # queue.
102
+ def num_waiting
103
+ synchronize do
104
+ @num_waiting
105
+ end
106
+ end
107
+
108
+ # Add +element+ to the queue. Never blocks.
109
+ def add(element)
110
+ synchronize do
111
+ @queue.push element
112
+ @cond.signal
113
+ end
114
+ end
115
+
116
+ # If +element+ is in the queue, remove and return it, or +nil+.
117
+ def delete(element)
118
+ synchronize do
119
+ @queue.delete(element)
120
+ end
121
+ end
122
+
123
+ # Remove all elements from the queue.
124
+ def clear
125
+ synchronize do
126
+ @queue.clear
127
+ end
128
+ end
129
+
130
+ # Remove the head of the queue.
131
+ #
132
+ # If +timeout+ is not given, remove and return the head the
133
+ # queue if the number of available elements is strictly
134
+ # greater than the number of threads currently waiting (that
135
+ # is, don't jump ahead in line). Otherwise, return +nil+.
136
+ #
137
+ # If +timeout+ is given, block if there is no element
138
+ # available, waiting up to +timeout+ seconds for an element to
139
+ # become available.
140
+ #
141
+ # Raises:
142
+ # - ActiveRecord::ConnectionTimeoutError if +timeout+ is given and no element
143
+ # becomes available within +timeout+ seconds,
144
+ def poll(timeout = nil)
145
+ synchronize { internal_poll(timeout) }
146
+ end
147
+
148
+ private
149
+
150
+ def internal_poll(timeout)
151
+ no_wait_poll || (timeout && wait_poll(timeout))
152
+ end
153
+
154
+ def synchronize(&block)
155
+ @lock.synchronize(&block)
156
+ end
157
+
158
+ # Test if the queue currently contains any elements.
159
+ def any?
160
+ !@queue.empty?
161
+ end
162
+
163
+ # A thread can remove an element from the queue without
164
+ # waiting if and only if the number of currently available
165
+ # connections is strictly greater than the number of waiting
166
+ # threads.
167
+ def can_remove_no_wait?
168
+ @queue.size > @num_waiting
169
+ end
170
+
171
+ # Removes and returns the head of the queue if possible, or +nil+.
172
+ def remove
173
+ @queue.pop
174
+ end
175
+
176
+ # Remove and return the head the queue if the number of
177
+ # available elements is strictly greater than the number of
178
+ # threads currently waiting. Otherwise, return +nil+.
179
+ def no_wait_poll
180
+ remove if can_remove_no_wait?
181
+ end
182
+
183
+ # Waits on the queue up to +timeout+ seconds, then removes and
184
+ # returns the head of the queue.
185
+ def wait_poll(timeout)
186
+ @num_waiting += 1
187
+
188
+ t0 = Time.now
189
+ elapsed = 0
190
+ loop do
191
+ ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
192
+ @cond.wait(timeout - elapsed)
193
+ end
194
+
195
+ return remove if any?
196
+
197
+ elapsed = Time.now - t0
198
+ if elapsed >= timeout
199
+ msg = "could not obtain a connection from the pool within %0.3f seconds (waited %0.3f seconds); all pooled connections were in use" %
200
+ [timeout, elapsed]
201
+ raise ConnectionTimeoutError, msg
202
+ end
203
+ end
204
+ ensure
205
+ @num_waiting -= 1
206
+ end
207
+ end
208
+
209
+ # Adds the ability to turn a basic fair FIFO queue into one
210
+ # biased to some thread.
211
+ module BiasableQueue # :nodoc:
212
+ class BiasedConditionVariable # :nodoc:
213
+ # semantics of condition variables guarantee that +broadcast+, +broadcast_on_biased+,
214
+ # +signal+ and +wait+ methods are only called while holding a lock
215
+ def initialize(lock, other_cond, preferred_thread)
216
+ @real_cond = lock.new_cond
217
+ @other_cond = other_cond
218
+ @preferred_thread = preferred_thread
219
+ @num_waiting_on_real_cond = 0
220
+ end
221
+
222
+ def broadcast
223
+ broadcast_on_biased
224
+ @other_cond.broadcast
225
+ end
226
+
227
+ def broadcast_on_biased
228
+ @num_waiting_on_real_cond = 0
229
+ @real_cond.broadcast
230
+ end
231
+
232
+ def signal
233
+ if @num_waiting_on_real_cond > 0
234
+ @num_waiting_on_real_cond -= 1
235
+ @real_cond
236
+ else
237
+ @other_cond
238
+ end.signal
239
+ end
240
+
241
+ def wait(timeout)
242
+ if Thread.current == @preferred_thread
243
+ @num_waiting_on_real_cond += 1
244
+ @real_cond
245
+ else
246
+ @other_cond
247
+ end.wait(timeout)
248
+ end
249
+ end
250
+
251
+ def with_a_bias_for(thread)
252
+ previous_cond = nil
253
+ new_cond = nil
254
+ synchronize do
255
+ previous_cond = @cond
256
+ @cond = new_cond = BiasedConditionVariable.new(@lock, @cond, thread)
257
+ end
258
+ yield
259
+ ensure
260
+ synchronize do
261
+ @cond = previous_cond if previous_cond
262
+ new_cond.broadcast_on_biased if new_cond # wake up any remaining sleepers
263
+ end
264
+ end
265
+ end
266
+
267
+ # Connections must be leased while holding the main pool mutex. This is
268
+ # an internal subclass that also +.leases+ returned connections while
269
+ # still in queue's critical section (queue synchronizes with the same
270
+ # <tt>@lock</tt> as the main pool) so that a returned connection is already
271
+ # leased and there is no need to re-enter synchronized block.
272
+ class ConnectionLeasingQueue < Queue # :nodoc:
273
+ include BiasableQueue
274
+
275
+ private
276
+ def internal_poll(timeout)
277
+ conn = super
278
+ conn.lease if conn
279
+ conn
280
+ end
281
+ end
282
+
283
+ # Every +frequency+ seconds, the reaper will call +reap+ and +flush+ on
284
+ # +pool+. A reaper instantiated with a zero frequency will never reap
285
+ # the connection pool.
286
+ #
287
+ # Configure the frequency by setting +reaping_frequency+ in your database
288
+ # yaml file (default 60 seconds).
289
+ class Reaper
290
+ attr_reader :pool, :frequency
291
+
292
+ def initialize(pool, frequency)
293
+ @pool = pool
294
+ @frequency = frequency
295
+ end
296
+
297
+ def run
298
+ return unless frequency && frequency > 0
299
+ Thread.new(frequency, pool) { |t, p|
300
+ loop do
301
+ sleep t
302
+ p.reap
303
+ p.flush
304
+ end
305
+ }
306
+ end
307
+ end
308
+
309
+ include MonitorMixin
310
+ include QueryCache::ConnectionPoolConfiguration
311
+
312
+ attr_accessor :automatic_reconnect, :checkout_timeout, :schema_cache
313
+ attr_reader :spec, :connections, :size, :reaper
314
+
315
+ # Creates a new ConnectionPool object. +spec+ is a ConnectionSpecification
316
+ # object which describes database connection information (e.g. adapter,
317
+ # host name, username, password, etc), as well as the maximum size for
318
+ # this ConnectionPool.
319
+ #
320
+ # The default ConnectionPool maximum size is 5.
321
+ def initialize(spec)
322
+ super()
323
+
324
+ @spec = spec
325
+
326
+ @checkout_timeout = (spec.config[:checkout_timeout] && spec.config[:checkout_timeout].to_f) || 5
327
+ if @idle_timeout = spec.config.fetch(:idle_timeout, 300)
328
+ @idle_timeout = @idle_timeout.to_f
329
+ @idle_timeout = nil if @idle_timeout <= 0
330
+ end
331
+
332
+ # default max pool size to 5
333
+ @size = (spec.config[:pool] && spec.config[:pool].to_i) || 5
334
+
335
+ # This variable tracks the cache of threads mapped to reserved connections, with the
336
+ # sole purpose of speeding up the +connection+ method. It is not the authoritative
337
+ # registry of which thread owns which connection. Connection ownership is tracked by
338
+ # the +connection.owner+ attr on each +connection+ instance.
339
+ # The invariant works like this: if there is mapping of <tt>thread => conn</tt>,
340
+ # then that +thread+ does indeed own that +conn+. However, an absence of a such
341
+ # mapping does not mean that the +thread+ doesn't own the said connection. In
342
+ # that case +conn.owner+ attr should be consulted.
343
+ # Access and modification of <tt>@thread_cached_conns</tt> does not require
344
+ # synchronization.
345
+ @thread_cached_conns = Concurrent::Map.new(initial_capacity: @size)
346
+
347
+ @connections = []
348
+ @automatic_reconnect = true
349
+
350
+ # Connection pool allows for concurrent (outside the main +synchronize+ section)
351
+ # establishment of new connections. This variable tracks the number of threads
352
+ # currently in the process of independently establishing connections to the DB.
353
+ @now_connecting = 0
354
+
355
+ @threads_blocking_new_connections = 0
356
+
357
+ @available = ConnectionLeasingQueue.new self
358
+
359
+ @lock_thread = false
360
+
361
+ # +reaping_frequency+ is configurable mostly for historical reasons, but it could
362
+ # also be useful if someone wants a very low +idle_timeout+.
363
+ reaping_frequency = spec.config.fetch(:reaping_frequency, 60)
364
+ @reaper = Reaper.new(self, reaping_frequency && reaping_frequency.to_f)
365
+ @reaper.run
366
+ end
367
+
368
+ def lock_thread=(lock_thread)
369
+ if lock_thread
370
+ @lock_thread = Thread.current
371
+ else
372
+ @lock_thread = nil
373
+ end
374
+ end
375
+
376
+ # Retrieve the connection associated with the current thread, or call
377
+ # #checkout to obtain one if necessary.
378
+ #
379
+ # #connection can be called any number of times; the connection is
380
+ # held in a cache keyed by a thread.
381
+ def connection
382
+ @thread_cached_conns[connection_cache_key(@lock_thread || Thread.current)] ||= checkout
383
+ end
384
+
385
+ # Returns true if there is an open connection being used for the current thread.
386
+ #
387
+ # This method only works for connections that have been obtained through
388
+ # #connection or #with_connection methods. Connections obtained through
389
+ # #checkout will not be detected by #active_connection?
390
+ def active_connection?
391
+ @thread_cached_conns[connection_cache_key(Thread.current)]
392
+ end
393
+
394
+ # Signal that the thread is finished with the current connection.
395
+ # #release_connection releases the connection-thread association
396
+ # and returns the connection to the pool.
397
+ #
398
+ # This method only works for connections that have been obtained through
399
+ # #connection or #with_connection methods, connections obtained through
400
+ # #checkout will not be automatically released.
401
+ def release_connection(owner_thread = Thread.current)
402
+ if conn = @thread_cached_conns.delete(connection_cache_key(owner_thread))
403
+ checkin conn
404
+ end
405
+ end
406
+
407
+ # If a connection obtained through #connection or #with_connection methods
408
+ # already exists yield it to the block. If no such connection
409
+ # exists checkout a connection, yield it to the block, and checkin the
410
+ # connection when finished.
411
+ def with_connection
412
+ unless conn = @thread_cached_conns[connection_cache_key(Thread.current)]
413
+ conn = connection
414
+ fresh_connection = true
415
+ end
416
+ yield conn
417
+ ensure
418
+ release_connection if fresh_connection
419
+ end
420
+
421
+ # Returns true if a connection has already been opened.
422
+ def connected?
423
+ synchronize { @connections.any? }
424
+ end
425
+
426
+ # Disconnects all connections in the pool, and clears the pool.
427
+ #
428
+ # Raises:
429
+ # - ActiveRecord::ExclusiveConnectionTimeoutError if unable to gain ownership of all
430
+ # connections in the pool within a timeout interval (default duration is
431
+ # <tt>spec.config[:checkout_timeout] * 2</tt> seconds).
432
+ def disconnect(raise_on_acquisition_timeout = true)
433
+ with_exclusively_acquired_all_connections(raise_on_acquisition_timeout) do
434
+ synchronize do
435
+ @connections.each do |conn|
436
+ if conn.in_use?
437
+ conn.steal!
438
+ checkin conn
439
+ end
440
+ conn.disconnect!
441
+ end
442
+ @connections = []
443
+ @available.clear
444
+ end
445
+ end
446
+ end
447
+
448
+ # Disconnects all connections in the pool, and clears the pool.
449
+ #
450
+ # The pool first tries to gain ownership of all connections. If unable to
451
+ # do so within a timeout interval (default duration is
452
+ # <tt>spec.config[:checkout_timeout] * 2</tt> seconds), then the pool is forcefully
453
+ # disconnected without any regard for other connection owning threads.
454
+ def disconnect!
455
+ disconnect(false)
456
+ end
457
+
458
+ # Discards all connections in the pool (even if they're currently
459
+ # leased!), along with the pool itself. Any further interaction with the
460
+ # pool (except #spec and #schema_cache) is undefined.
461
+ #
462
+ # See AbstractAdapter#discard!
463
+ def discard! # :nodoc:
464
+ synchronize do
465
+ return if @connections.nil? # already discarded
466
+ @connections.each do |conn|
467
+ conn.discard!
468
+ end
469
+ @connections = @available = @thread_cached_conns = nil
470
+ end
471
+ end
472
+
473
+ # Clears the cache which maps classes and re-connects connections that
474
+ # require reloading.
475
+ #
476
+ # Raises:
477
+ # - ActiveRecord::ExclusiveConnectionTimeoutError if unable to gain ownership of all
478
+ # connections in the pool within a timeout interval (default duration is
479
+ # <tt>spec.config[:checkout_timeout] * 2</tt> seconds).
480
+ def clear_reloadable_connections(raise_on_acquisition_timeout = true)
481
+ with_exclusively_acquired_all_connections(raise_on_acquisition_timeout) do
482
+ synchronize do
483
+ @connections.each do |conn|
484
+ if conn.in_use?
485
+ conn.steal!
486
+ checkin conn
487
+ end
488
+ conn.disconnect! if conn.requires_reloading?
489
+ end
490
+ @connections.delete_if(&:requires_reloading?)
491
+ @available.clear
492
+ end
493
+ end
494
+ end
495
+
496
+ # Clears the cache which maps classes and re-connects connections that
497
+ # require reloading.
498
+ #
499
+ # The pool first tries to gain ownership of all connections. If unable to
500
+ # do so within a timeout interval (default duration is
501
+ # <tt>spec.config[:checkout_timeout] * 2</tt> seconds), then the pool forcefully
502
+ # clears the cache and reloads connections without any regard for other
503
+ # connection owning threads.
504
+ def clear_reloadable_connections!
505
+ clear_reloadable_connections(false)
506
+ end
507
+
508
+ # Check-out a database connection from the pool, indicating that you want
509
+ # to use it. You should call #checkin when you no longer need this.
510
+ #
511
+ # This is done by either returning and leasing existing connection, or by
512
+ # creating a new connection and leasing it.
513
+ #
514
+ # If all connections are leased and the pool is at capacity (meaning the
515
+ # number of currently leased connections is greater than or equal to the
516
+ # size limit set), an ActiveRecord::ConnectionTimeoutError exception will be raised.
517
+ #
518
+ # Returns: an AbstractAdapter object.
519
+ #
520
+ # Raises:
521
+ # - ActiveRecord::ConnectionTimeoutError no connection can be obtained from the pool.
522
+ def checkout(checkout_timeout = @checkout_timeout)
523
+ checkout_and_verify(acquire_connection(checkout_timeout))
524
+ end
525
+
526
+ # Check-in a database connection back into the pool, indicating that you
527
+ # no longer need this connection.
528
+ #
529
+ # +conn+: an AbstractAdapter object, which was obtained by earlier by
530
+ # calling #checkout on this pool.
531
+ def checkin(conn)
532
+ conn.lock.synchronize do
533
+ synchronize do
534
+ remove_connection_from_thread_cache conn
535
+
536
+ conn._run_checkin_callbacks do
537
+ conn.expire
538
+ end
539
+
540
+ @available.add conn
541
+ end
542
+ end
543
+ end
544
+
545
+ # Remove a connection from the connection pool. The connection will
546
+ # remain open and active but will no longer be managed by this pool.
547
+ def remove(conn)
548
+ needs_new_connection = false
549
+
550
+ synchronize do
551
+ remove_connection_from_thread_cache conn
552
+
553
+ @connections.delete conn
554
+ @available.delete conn
555
+
556
+ # @available.any_waiting? => true means that prior to removing this
557
+ # conn, the pool was at its max size (@connections.size == @size).
558
+ # This would mean that any threads stuck waiting in the queue wouldn't
559
+ # know they could checkout_new_connection, so let's do it for them.
560
+ # Because condition-wait loop is encapsulated in the Queue class
561
+ # (that in turn is oblivious to ConnectionPool implementation), threads
562
+ # that are "stuck" there are helpless. They have no way of creating
563
+ # new connections and are completely reliant on us feeding available
564
+ # connections into the Queue.
565
+ needs_new_connection = @available.any_waiting?
566
+ end
567
+
568
+ # This is intentionally done outside of the synchronized section as we
569
+ # would like not to hold the main mutex while checking out new connections.
570
+ # Thus there is some chance that needs_new_connection information is now
571
+ # stale, we can live with that (bulk_make_new_connections will make
572
+ # sure not to exceed the pool's @size limit).
573
+ bulk_make_new_connections(1) if needs_new_connection
574
+ end
575
+
576
+ # Recover lost connections for the pool. A lost connection can occur if
577
+ # a programmer forgets to checkin a connection at the end of a thread
578
+ # or a thread dies unexpectedly.
579
+ def reap
580
+ stale_connections = synchronize do
581
+ @connections.select do |conn|
582
+ conn.in_use? && !conn.owner.alive?
583
+ end.each do |conn|
584
+ conn.steal!
585
+ end
586
+ end
587
+
588
+ stale_connections.each do |conn|
589
+ if conn.active?
590
+ conn.reset!
591
+ checkin conn
592
+ else
593
+ remove conn
594
+ end
595
+ end
596
+ end
597
+
598
+ # Disconnect all connections that have been idle for at least
599
+ # +minimum_idle+ seconds. Connections currently checked out, or that were
600
+ # checked in less than +minimum_idle+ seconds ago, are unaffected.
601
+ def flush(minimum_idle = @idle_timeout)
602
+ return if minimum_idle.nil?
603
+
604
+ idle_connections = synchronize do
605
+ @connections.select do |conn|
606
+ !conn.in_use? && conn.seconds_idle >= minimum_idle
607
+ end.each do |conn|
608
+ conn.lease
609
+
610
+ @available.delete conn
611
+ @connections.delete conn
612
+ end
613
+ end
614
+
615
+ idle_connections.each do |conn|
616
+ conn.disconnect!
617
+ end
618
+ end
619
+
620
+ # Disconnect all currently idle connections. Connections currently checked
621
+ # out are unaffected.
622
+ def flush!
623
+ reap
624
+ flush(-1)
625
+ end
626
+
627
+ def num_waiting_in_queue # :nodoc:
628
+ @available.num_waiting
629
+ end
630
+
631
+ # Return connection pool's usage statistic
632
+ # Example:
633
+ #
634
+ # ActiveRecord::Base.connection_pool.stat # => { size: 15, connections: 1, busy: 1, dead: 0, idle: 0, waiting: 0, checkout_timeout: 5 }
635
+ def stat
636
+ synchronize do
637
+ {
638
+ size: size,
639
+ connections: @connections.size,
640
+ busy: @connections.count { |c| c.in_use? && c.owner.alive? },
641
+ dead: @connections.count { |c| c.in_use? && !c.owner.alive? },
642
+ idle: @connections.count { |c| !c.in_use? },
643
+ waiting: num_waiting_in_queue,
644
+ checkout_timeout: checkout_timeout
645
+ }
646
+ end
647
+ end
648
+
649
+ private
650
+ #--
651
+ # this is unfortunately not concurrent
652
+ def bulk_make_new_connections(num_new_conns_needed)
653
+ num_new_conns_needed.times do
654
+ # try_to_checkout_new_connection will not exceed pool's @size limit
655
+ if new_conn = try_to_checkout_new_connection
656
+ # make the new_conn available to the starving threads stuck @available Queue
657
+ checkin(new_conn)
658
+ end
659
+ end
660
+ end
661
+
662
+ #--
663
+ # From the discussion on GitHub:
664
+ # https://github.com/rails/rails/pull/14938#commitcomment-6601951
665
+ # This hook-in method allows for easier monkey-patching fixes needed by
666
+ # JRuby users that use Fibers.
667
+ def connection_cache_key(thread)
668
+ thread
669
+ end
670
+
671
+ # Take control of all existing connections so a "group" action such as
672
+ # reload/disconnect can be performed safely. It is no longer enough to
673
+ # wrap it in +synchronize+ because some pool's actions are allowed
674
+ # to be performed outside of the main +synchronize+ block.
675
+ def with_exclusively_acquired_all_connections(raise_on_acquisition_timeout = true)
676
+ with_new_connections_blocked do
677
+ attempt_to_checkout_all_existing_connections(raise_on_acquisition_timeout)
678
+ yield
679
+ end
680
+ end
681
+
682
+ def attempt_to_checkout_all_existing_connections(raise_on_acquisition_timeout = true)
683
+ collected_conns = synchronize do
684
+ # account for our own connections
685
+ @connections.select { |conn| conn.owner == Thread.current }
686
+ end
687
+
688
+ newly_checked_out = []
689
+ timeout_time = Time.now + (@checkout_timeout * 2)
690
+
691
+ @available.with_a_bias_for(Thread.current) do
692
+ loop do
693
+ synchronize do
694
+ return if collected_conns.size == @connections.size && @now_connecting == 0
695
+ remaining_timeout = timeout_time - Time.now
696
+ remaining_timeout = 0 if remaining_timeout < 0
697
+ conn = checkout_for_exclusive_access(remaining_timeout)
698
+ collected_conns << conn
699
+ newly_checked_out << conn
700
+ end
701
+ end
702
+ end
703
+ rescue ExclusiveConnectionTimeoutError
704
+ # <tt>raise_on_acquisition_timeout == false</tt> means we are directed to ignore any
705
+ # timeouts and are expected to just give up: we've obtained as many connections
706
+ # as possible, note that in a case like that we don't return any of the
707
+ # +newly_checked_out+ connections.
708
+
709
+ if raise_on_acquisition_timeout
710
+ release_newly_checked_out = true
711
+ raise
712
+ end
713
+ rescue Exception # if something else went wrong
714
+ # this can't be a "naked" rescue, because we have should return conns
715
+ # even for non-StandardErrors
716
+ release_newly_checked_out = true
717
+ raise
718
+ ensure
719
+ if release_newly_checked_out && newly_checked_out
720
+ # releasing only those conns that were checked out in this method, conns
721
+ # checked outside this method (before it was called) are not for us to release
722
+ newly_checked_out.each { |conn| checkin(conn) }
723
+ end
724
+ end
725
+
726
+ #--
727
+ # Must be called in a synchronize block.
728
+ def checkout_for_exclusive_access(checkout_timeout)
729
+ checkout(checkout_timeout)
730
+ rescue ConnectionTimeoutError
731
+ # this block can't be easily moved into attempt_to_checkout_all_existing_connections's
732
+ # rescue block, because doing so would put it outside of synchronize section, without
733
+ # being in a critical section thread_report might become inaccurate
734
+ msg = "could not obtain ownership of all database connections in #{checkout_timeout} seconds".dup
735
+
736
+ thread_report = []
737
+ @connections.each do |conn|
738
+ unless conn.owner == Thread.current
739
+ thread_report << "#{conn} is owned by #{conn.owner}"
740
+ end
741
+ end
742
+
743
+ msg << " (#{thread_report.join(', ')})" if thread_report.any?
744
+
745
+ raise ExclusiveConnectionTimeoutError, msg
746
+ end
747
+
748
+ def with_new_connections_blocked
749
+ synchronize do
750
+ @threads_blocking_new_connections += 1
751
+ end
752
+
753
+ yield
754
+ ensure
755
+ num_new_conns_required = 0
756
+
757
+ synchronize do
758
+ @threads_blocking_new_connections -= 1
759
+
760
+ if @threads_blocking_new_connections.zero?
761
+ @available.clear
762
+
763
+ num_new_conns_required = num_waiting_in_queue
764
+
765
+ @connections.each do |conn|
766
+ next if conn.in_use?
767
+
768
+ @available.add conn
769
+ num_new_conns_required -= 1
770
+ end
771
+ end
772
+ end
773
+
774
+ bulk_make_new_connections(num_new_conns_required) if num_new_conns_required > 0
775
+ end
776
+
777
+ # Acquire a connection by one of 1) immediately removing one
778
+ # from the queue of available connections, 2) creating a new
779
+ # connection if the pool is not at capacity, 3) waiting on the
780
+ # queue for a connection to become available.
781
+ #
782
+ # Raises:
783
+ # - ActiveRecord::ConnectionTimeoutError if a connection could not be acquired
784
+ #
785
+ #--
786
+ # Implementation detail: the connection returned by +acquire_connection+
787
+ # will already be "+connection.lease+ -ed" to the current thread.
788
+ def acquire_connection(checkout_timeout)
789
+ # NOTE: we rely on <tt>@available.poll</tt> and +try_to_checkout_new_connection+ to
790
+ # +conn.lease+ the returned connection (and to do this in a +synchronized+
791
+ # section). This is not the cleanest implementation, as ideally we would
792
+ # <tt>synchronize { conn.lease }</tt> in this method, but by leaving it to <tt>@available.poll</tt>
793
+ # and +try_to_checkout_new_connection+ we can piggyback on +synchronize+ sections
794
+ # of the said methods and avoid an additional +synchronize+ overhead.
795
+ if conn = @available.poll || try_to_checkout_new_connection
796
+ conn
797
+ else
798
+ reap
799
+ @available.poll(checkout_timeout)
800
+ end
801
+ end
802
+
803
+ #--
804
+ # if owner_thread param is omitted, this must be called in synchronize block
805
+ def remove_connection_from_thread_cache(conn, owner_thread = conn.owner)
806
+ @thread_cached_conns.delete_pair(connection_cache_key(owner_thread), conn)
807
+ end
808
+ alias_method :release, :remove_connection_from_thread_cache
809
+
810
+ def new_connection
811
+ Base.send(spec.adapter_method, spec.config).tap do |conn|
812
+ conn.schema_cache = schema_cache.dup if schema_cache
813
+ end
814
+ end
815
+
816
+ # If the pool is not at a <tt>@size</tt> limit, establish new connection. Connecting
817
+ # to the DB is done outside main synchronized section.
818
+ #--
819
+ # Implementation constraint: a newly established connection returned by this
820
+ # method must be in the +.leased+ state.
821
+ def try_to_checkout_new_connection
822
+ # first in synchronized section check if establishing new conns is allowed
823
+ # and increment @now_connecting, to prevent overstepping this pool's @size
824
+ # constraint
825
+ do_checkout = synchronize do
826
+ if @threads_blocking_new_connections.zero? && (@connections.size + @now_connecting) < @size
827
+ @now_connecting += 1
828
+ end
829
+ end
830
+ if do_checkout
831
+ begin
832
+ # if successfully incremented @now_connecting establish new connection
833
+ # outside of synchronized section
834
+ conn = checkout_new_connection
835
+ ensure
836
+ synchronize do
837
+ if conn
838
+ adopt_connection(conn)
839
+ # returned conn needs to be already leased
840
+ conn.lease
841
+ end
842
+ @now_connecting -= 1
843
+ end
844
+ end
845
+ end
846
+ end
847
+
848
+ def adopt_connection(conn)
849
+ conn.pool = self
850
+ @connections << conn
851
+ end
852
+
853
+ def checkout_new_connection
854
+ raise ConnectionNotEstablished unless @automatic_reconnect
855
+ new_connection
856
+ end
857
+
858
+ def checkout_and_verify(c)
859
+ c._run_checkout_callbacks do
860
+ c.verify!
861
+ end
862
+ c
863
+ rescue
864
+ remove c
865
+ c.disconnect!
866
+ raise
867
+ end
868
+ end
869
+
870
+ # ConnectionHandler is a collection of ConnectionPool objects. It is used
871
+ # for keeping separate connection pools that connect to different databases.
872
+ #
873
+ # For example, suppose that you have 5 models, with the following hierarchy:
874
+ #
875
+ # class Author < ActiveRecord::Base
876
+ # end
877
+ #
878
+ # class BankAccount < ActiveRecord::Base
879
+ # end
880
+ #
881
+ # class Book < ActiveRecord::Base
882
+ # establish_connection :library_db
883
+ # end
884
+ #
885
+ # class ScaryBook < Book
886
+ # end
887
+ #
888
+ # class GoodBook < Book
889
+ # end
890
+ #
891
+ # And a database.yml that looked like this:
892
+ #
893
+ # development:
894
+ # database: my_application
895
+ # host: localhost
896
+ #
897
+ # library_db:
898
+ # database: library
899
+ # host: some.library.org
900
+ #
901
+ # Your primary database in the development environment is "my_application"
902
+ # but the Book model connects to a separate database called "library_db"
903
+ # (this can even be a database on a different machine).
904
+ #
905
+ # Book, ScaryBook and GoodBook will all use the same connection pool to
906
+ # "library_db" while Author, BankAccount, and any other models you create
907
+ # will use the default connection pool to "my_application".
908
+ #
909
+ # The various connection pools are managed by a single instance of
910
+ # ConnectionHandler accessible via ActiveRecord::Base.connection_handler.
911
+ # All Active Record models use this handler to determine the connection pool that they
912
+ # should use.
913
+ #
914
+ # The ConnectionHandler class is not coupled with the Active models, as it has no knowledge
915
+ # about the model. The model needs to pass a specification name to the handler,
916
+ # in order to look up the correct connection pool.
917
+ class ConnectionHandler
918
+ def self.create_owner_to_pool # :nodoc:
919
+ Concurrent::Map.new(initial_capacity: 2) do |h, k|
920
+ # Discard the parent's connection pools immediately; we have no need
921
+ # of them
922
+ discard_unowned_pools(h)
923
+
924
+ h[k] = Concurrent::Map.new(initial_capacity: 2)
925
+ end
926
+ end
927
+
928
+ def self.unowned_pool_finalizer(pid_map) # :nodoc:
929
+ lambda do |_|
930
+ discard_unowned_pools(pid_map)
931
+ end
932
+ end
933
+
934
+ def self.discard_unowned_pools(pid_map) # :nodoc:
935
+ pid_map.each do |pid, pools|
936
+ pools.values.compact.each(&:discard!) unless pid == Process.pid
937
+ end
938
+ end
939
+
940
+ def initialize
941
+ # These caches are keyed by spec.name (ConnectionSpecification#name).
942
+ @owner_to_pool = ConnectionHandler.create_owner_to_pool
943
+
944
+ # Backup finalizer: if the forked child never needed a pool, the above
945
+ # early discard has not occurred
946
+ ObjectSpace.define_finalizer self, ConnectionHandler.unowned_pool_finalizer(@owner_to_pool)
947
+ end
948
+
949
+ def connection_pool_list
950
+ owner_to_pool.values.compact
951
+ end
952
+ alias :connection_pools :connection_pool_list
953
+
954
+ def establish_connection(config)
955
+ resolver = ConnectionSpecification::Resolver.new(Base.configurations)
956
+ spec = resolver.spec(config)
957
+
958
+ remove_connection(spec.name)
959
+
960
+ message_bus = ActiveSupport::Notifications.instrumenter
961
+ payload = {
962
+ connection_id: object_id
963
+ }
964
+ if spec
965
+ payload[:spec_name] = spec.name
966
+ payload[:config] = spec.config
967
+ end
968
+
969
+ message_bus.instrument("!connection.active_record", payload) do
970
+ owner_to_pool[spec.name] = ConnectionAdapters::ConnectionPool.new(spec)
971
+ end
972
+
973
+ owner_to_pool[spec.name]
974
+ end
975
+
976
+ # Returns true if there are any active connections among the connection
977
+ # pools that the ConnectionHandler is managing.
978
+ def active_connections?
979
+ connection_pool_list.any?(&:active_connection?)
980
+ end
981
+
982
+ # Returns any connections in use by the current thread back to the pool,
983
+ # and also returns connections to the pool cached by threads that are no
984
+ # longer alive.
985
+ def clear_active_connections!
986
+ connection_pool_list.each(&:release_connection)
987
+ end
988
+
989
+ # Clears the cache which maps classes.
990
+ #
991
+ # See ConnectionPool#clear_reloadable_connections! for details.
992
+ def clear_reloadable_connections!
993
+ connection_pool_list.each(&:clear_reloadable_connections!)
994
+ end
995
+
996
+ def clear_all_connections!
997
+ connection_pool_list.each(&:disconnect!)
998
+ end
999
+
1000
+ # Disconnects all currently idle connections.
1001
+ #
1002
+ # See ConnectionPool#flush! for details.
1003
+ def flush_idle_connections!
1004
+ connection_pool_list.each(&:flush!)
1005
+ end
1006
+
1007
+ # Locate the connection of the nearest super class. This can be an
1008
+ # active or defined connection: if it is the latter, it will be
1009
+ # opened and set as the active connection for the class it was defined
1010
+ # for (not necessarily the current class).
1011
+ def retrieve_connection(spec_name) #:nodoc:
1012
+ pool = retrieve_connection_pool(spec_name)
1013
+ raise ConnectionNotEstablished, "No connection pool with '#{spec_name}' found." unless pool
1014
+ pool.connection
1015
+ end
1016
+
1017
+ # Returns true if a connection that's accessible to this class has
1018
+ # already been opened.
1019
+ def connected?(spec_name)
1020
+ conn = retrieve_connection_pool(spec_name)
1021
+ conn && conn.connected?
1022
+ end
1023
+
1024
+ # Remove the connection for this class. This will close the active
1025
+ # connection and the defined connection (if they exist). The result
1026
+ # can be used as an argument for #establish_connection, for easily
1027
+ # re-establishing the connection.
1028
+ def remove_connection(spec_name)
1029
+ if pool = owner_to_pool.delete(spec_name)
1030
+ pool.automatic_reconnect = false
1031
+ pool.disconnect!
1032
+ pool.spec.config
1033
+ end
1034
+ end
1035
+
1036
+ # Retrieving the connection pool happens a lot, so we cache it in @owner_to_pool.
1037
+ # This makes retrieving the connection pool O(1) once the process is warm.
1038
+ # When a connection is established or removed, we invalidate the cache.
1039
+ def retrieve_connection_pool(spec_name)
1040
+ owner_to_pool.fetch(spec_name) do
1041
+ # Check if a connection was previously established in an ancestor process,
1042
+ # which may have been forked.
1043
+ if ancestor_pool = pool_from_any_process_for(spec_name)
1044
+ # A connection was established in an ancestor process that must have
1045
+ # subsequently forked. We can't reuse the connection, but we can copy
1046
+ # the specification and establish a new connection with it.
1047
+ establish_connection(ancestor_pool.spec.to_hash).tap do |pool|
1048
+ pool.schema_cache = ancestor_pool.schema_cache if ancestor_pool.schema_cache
1049
+ end
1050
+ else
1051
+ owner_to_pool[spec_name] = nil
1052
+ end
1053
+ end
1054
+ end
1055
+
1056
+ private
1057
+
1058
+ def owner_to_pool
1059
+ @owner_to_pool[Process.pid]
1060
+ end
1061
+
1062
+ def pool_from_any_process_for(spec_name)
1063
+ owner_to_pool = @owner_to_pool.values.reverse.find { |v| v[spec_name] }
1064
+ owner_to_pool && owner_to_pool[spec_name]
1065
+ end
1066
+ end
1067
+ end
1068
+ end