aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/connection_pool_test.rb
Commit message (Collapse)AuthorAgeFilesLines
* Make ActiveRecord `ConnectionPool.connections` thread-safe. (#36473)jeffdoering2019-06-131-0/+22
| | | | | | | | | | | | | | | | | | | | | | | * Make ActiveRecord `ConnectionPool.connections` thread-safe. ConnectionPool documentation is clear on the need to synchronize access to @connections but also states that public methods do not require synchronization. Existing code exposed @connections directly via attr_reader. The fix uses synchronize() to lock @connections then returns a copy to the caller using Array.dup(). Includes comments on the connections method that thread-safe access to the connections array does not imply thread-safety of accessing methods on the actual connections. Adds a test-case that modifies the pool using a supported method in one thread while a second thread accesses pool.connections. The test fails without this patch. Fixes #36465. * Update activerecord/test/cases/connection_pool_test.rb [jeffdoering + Rafael Mendonça França]
* Move schema cache from connection to pooleileencodes2019-06-051-1/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This PR proposes moving the schema cache from the connection to the pool so the connection can ask the pool for the cache. In a future PR our goal is to be able to read the yaml file from the pool so we can get rid of the `active_record.check_schema_cache_dump` initializer. This will fix the issues surrounding dumping the schema cache and mulitple databases. Why do we want to get rid of the initializer you ask? Well I was looking at #34449 and trying to make it work for our usecase and it revealed A LOT of problems. There are a few issues that I will fix in remaining PRs with SchemaMigration, but there's a big glaring issue with this initializer. When you have an application with multiple databases we'll need to loop through all the configurations and set the schema cache on those connections. The problem is on initialization we only have one connection - the one for Ar::Base. This is fine in a single db application but not fine in multi-db. If we follow the pattern in #34449 and establish a connection to those other dbs we will end up setting the cache on the _connection object_ rather than on all connections that connect for that config. So even though we looped through the configs and assigned the cache the cache will not be set (or will be set wrong) once the app is booted because the connection objects after boot are _different_ than the connection objects we assigned the cache to. After trying many different ways to set the schema cache `@tenderlove` and I came to the conclusion that the initializer is problematic, as is setting the schema cache twice. This is part 1 to move the cache to the pool so the cache can read from the schema cache yaml file instead of setting it when initializing the app. To do this we have created a `NullPool` that initializes an empty cache. I put the `get_schema_cache` and `set_schema_cache` in an `AbstractPool` so we can share code between `ConnectionPool` and `NullPool` instead of duplicating code. Now we only need to set the schema_cache on the pool rather than the connection. In `discard!` we need to unset the connection from the schema_cache - we still want the cache just not the connection.
* Enable `Style/RedundantBegin` cop to avoid newly adding redundant begin blockRyuta Kamizono2018-12-211-16/+14
| | | | | | | | | | Currently we sometimes find a redundant begin block in code review (e.g. https://github.com/rails/rails/pull/33604#discussion_r209784205). I'd like to enable `Style/RedundantBegin` cop to avoid that, since rescue/else/ensure are allowed inside do/end blocks in Ruby 2.5 (https://bugs.ruby-lang.org/issues/12906), so we'd probably meets with that situation than before.
* Change the empty block style to have space inside of the blockRafael Mendonça França2018-09-251-1/+1
|
* Merge pull request #31696 from BrentWheeldon/bmw-connection-pool-load-deadlockMatthew Draper2018-08-241-0/+38
|\ | | | | Prevent deadlocks when waiting for connection from pool.
| * Prevent deadlocks when waiting for connection from pool.Brent Wheeldon2018-03-231-0/+38
| | | | | | | | | | When a thread that had the load interlock but was blocked waiting to check a connection out of the connection pool but all of the threads using the available connections were blocked waiting to obtain the load interlock an `ActiveRecord::ConnectionTimeoutError` exception was be thrown by the thread waiting for the connection. When waiting for the connection to check out we should allow loading to proceed to avoid this deadlock.
* | Speed up slow ConnectionPool test case.Guo Xiang Tan2018-08-201-11/+19
| |
* | Improve tests for ActiveRecord::ConnectionAdapters::ConnectionPool ↵Guo Xiang Tan2018-08-191-0/+37
|/ | | | configurations.
* Remove unnecessary `respond_to?(:report_on_exception)` checkingyuuji.yaginuma2018-03-021-2/+2
| | | | Since Rails 6 requires Ruby 2.4.1+.
* Use assert_predicate and assert_not_predicateDaniel Colson2018-01-251-13/+13
|
* Merge pull request #25456 from ojab/masterRyuta Kamizono2018-01-031-1/+1
|\ | | | | | | Remove dormant check
* | Suppress expected exceptions by `report_on_exception` = `false` in Ruby 2.5Yasuo Honda2017-12-131-0/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | * Ruby 2.4 introduces `report_on_exception` to control if it reports exceptions in thread, this default value has been `false` in Ruby 2.4. Refer https://www.ruby-lang.org/en/news/2016/11/09/ruby-2-4-0-preview3-released/ * Ruby 2.5 changes `report_on_exception` default value to `true` since this commit https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?revision=61183&view=revision This pull request suppresses expected exceptions by setting `report_on_exception` = `false` it also supports Ruby 2.3 which does not have`report_on_exception`.
* | Flush idle database connectionsMatthew Draper2017-11-261-0/+47
| |
* | Merge pull request #28742 from quixoten/stack_conn_poolMatthew Draper2017-11-171-0/+8
|\ \ | | | | | | Switch to LIFO for the connection pool
| * | Fix typosDevin Christensen2017-04-131-1/+1
| | |
| * | Improve documentation and add testDevin Christensen2017-04-131-0/+8
| | |
* | | Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-191-0/+2
| | |
* | | Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"Matthew Draper2017-07-021-1/+0
| | | | | | | | | | | | | | | This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
* | | Enforce frozen string in RubocopKir Shatrov2017-07-011-0/+1
| | |
* | | Remove debug printRyuta Kamizono2017-06-181-13/+0
|/ / | | | | | | | | This debug print was added in 87f5e5e for finding the regression caused by d314646 and the regression has already been fixed in 969339b.
* | Refactor connection_pool_testKir Shatrov2017-02-261-1/+4
| |
* | Correct spellingBenjamin Fleischer2017-02-051-1/+1
| | | | | | | | | | | | | | ``` go get -u github.com/client9/misspell/cmd/misspell misspell -w -error -source=text . ```
* | Reduce string objects by using \ instead of + or << for concatenating stringsAkira Matsuda2017-01-121-1/+1
| | | | | | | | (I personally prefer writing one string in one line no matter how long it is, though)
* | Reconnecting AR::Base's connection breaks sqlite3_mem testsAkira Matsuda2017-01-101-2/+6
| | | | | | | | so let's test with a tiny inner-class model instead
* | Fix Rubocop violations and fix documentation visibilityRafael Mendonça França2016-12-281-2/+2
| | | | | | | | | | | | Some methods were added to public API in 5b14129d8d4ad302b4e11df6bd5c7891b75f393c and they should be not part of the public API.
* | Make the test that seems to be getting stuck noisierMatthew Draper2016-11-261-35/+55
| | | | | | | | | | I assume it's upset because of the change in d314646c965b045724e6bdb9d61dcecfabc0ba8f, but I don't yet understand why.
* | Distribute connections to previously blocked threads when we're doneMatthew Draper2016-11-251-6/+0
| | | | | | | | | | | | Two methods block new connections; we were already doing the right thing for clear_reloadable_connections, but it's better placed in with_new_connections_blocked, where it can work for disconnect too.
* | Add ActiveRecord::Base.connection_pool.statPavel2016-11-091-0/+20
| |
* | Use different name for main and thread connection variable.Charles Oliver Nutter2016-11-041-5/+5
| | | | | | | | | | | | Under JRuby, the updates of the one shared variable interleaved, causing threads to pick up each others' connections. I'm amazed this worked on MRI.
* | normalizes indentation and whitespace across the projectXavier Noria2016-08-061-7/+7
| |
* | applies new string literal convention in activerecord/testXavier Noria2016-08-061-6/+6
| | | | | | | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* | Fix `payload[:class_name]` to `payload[:spec_name]`Ryuta Kamizono2016-07-171-2/+2
| | | | | | | | | | | | Follow up to #20818. `retrieve_connection` is passed `spec_name` instead of `klass` since #24844.
* | Create connection.active_record notification and use that to ensure that lazy-Jeremy Wadsack2016-07-121-0/+12
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | loaded model classes have their connections wrapped in transactions. See #17776 In Rails 4 config.eager_load was changed to false in the test environment. This means that model classes that connect to alternate databases with establish_connection are not loaded at start up. If use_transactional_fixtures is enabled, transactions are wrapped around the connections that have been established only at the start of the test suite. So model classes loaded later don't have transactions causing data created in the alternate database not to be removed. This change resolves that by creating a new connection.active_record notification that gets fired whenever a connection is established. I then added a subscriber after we set up transactions in the test environment to listen for additional connections and wrap those in transactions as well.
* | Make connection stealing more explicitMatthew Draper2016-07-061-1/+4
|/
* fix testArthur Neves2016-05-051-8/+7
|
* Refactor connection handlerArthur Neves2016-05-051-8/+8
| | | | | | | | | | | | | | | ConnectionHandler will not have any knowlodge of AR models now, it will only know about the specs. Like that we can decouple the two, and allow the same model to use more than one connection. Historically, folks used to create abstract AR classes on the fly in order to have multiple connections for the same model, and override the connection methods. With this, now we can override the `specificiation_id` method in the model, to return a key, that will be used to find the connection_pool from the handler.
* Require only necessary concurrent-ruby classes.Jerry D'Antonio2015-11-041-1/+1
|
* Replaced `ActiveSupport::Concurrency::Latch` with concurrent-ruby.Jerry D'Antonio2015-07-131-14/+14
| | | | | | | | | | The concurrent-ruby gem is a toolset containing many concurrency utilities. Many of these utilities include runtime-specific optimizations when possible. Rather than clutter the Rails codebase with concurrency utilities separate from the core task, such tools can be superseded by similar tools in the more specialized gem. This commit replaces `ActiveSupport::Concurrency::Latch` with `Concurrent::CountDownLatch`, which is functionally equivalent.
* sqlite3_mem has an existing connectionMatthew Draper2015-05-181-1/+3
| | | | | We must account for receiving one less call to #new_connection, but the test otherwise remains valid.
* AR::ConPool - remove synchronization around connection cache.thedarkone2015-05-141-4/+132
| | | | | | | | | | | | | | | | | | | | Renamed `@reserved_connections` -> `@thread_cached_conns`. New name clearly conveys the purpose of the cache, which is to speed-up `#connection` method. The new `@thread_cached_conns` now also uses `Thread` objects as keys (instead of previously `Thread.current.object_id`). Since there is no longer any synchronization around `@thread_cached_conns`, `disconnect!` and `clear_reloadable_connections!` methods now pre-emptively obtain ownership (via `checkout`) of all existing connections, before modifying internal data structures. A private method `release` has been renamed `thread_conn_uncache` to clear-up its purpose. Fixed some brittle `thread.status == "sleep"` tests (threads can go into sleep even without locks).
* AR::ConPool - establish connections outside of critical section.thedarkone2015-05-141-0/+34
|
* Apply schema cache dump when creating connectionsEugene Kenny2015-04-291-0/+15
| | | | | | | | | | | | | | | The `db:schema:cache:dump` rake task dumps the database schema structure to `db/schema_cache.dump`. If this file is present, the schema details are loaded into the currently checked out connection by a railtie while Rails is booting, to avoid having to query the database for its schema. The schema cache dump is only applied to the initial connection used to boot the application though; other connections from the same pool are created with an empty schema cache, and still have to load the structure of each table directly from the database. With this change, a copy of the schema cache is associated with the connection pool and applied to connections as they are created.
* Fix typos and improve the documentationJon Atack2015-04-151-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is a squash of the following commits, from first to last: - Fix minor, random things I’ve come across lately that individually did not seem worth making a PR for, so I saved them for one commit. One common error is using “it’s” (which is an abbreviation of “it is”) when the possessive “its” should be used for indicating possession. - Changes include the name of a test, so remove the `[skip ci]` (thanks @senny). - Line wrap the changes at 80 chars and add one more doc fix. - Add a missing line wrap in the Contributing to Ruby on Rails Guide. - Line wrap the `TIP` section in the Contributing to Ruby on Rails Guide as well. Rendering the guide locally with `bundle exec rake guides:generate` did not show any change in on-screen formatting after adding the line wrap. The HTML generated is (extra line added to illustrate where the line wrap takes place): <div class="info"><p>Please squash your commits into a single commit when appropriate. This simplifies future cherry picks and also keeps the git log clean.</p></div> - Squash commits.
* test should only pass if the pool.size+1 checkout failsAaron Patterson2014-03-171-3/+2
| | | | | Previously, any of the connection checkouts could have failed, and this test would pass.
* use a latch to avoid busy loopsAaron Patterson2014-03-171-3/+4
|
* Reap connections based on owning-thread deathMatthew Draper2014-03-181-10/+13
| | | | | | | | | | | | | | | | .. not a general timeout. Now, if a thread checks out a connection then dies, we can immediately recover that connection and re-use it. This should alleviate the pool exhaustion discussed in #12867. More importantly, it entirely avoids the potential issues of the reaper attempting to check whether connections are still active: as long as the owning thread is alive, the connection is its business alone. As a no-op reap is now trivial (only entails checking a thread status per connection), we can also perform one in-line any time we decide to sleep for a connection.
* Use teardown helper method.Guo Xiang Tan2014-03-141-2/+1
| | | | | | | | Follow-Up to https://github.com/rails/rails/pull/14348 Ensure that SQLCounter.clear_log is called after each test. This is a step to prevent side effects when running tests. This will allow us to run them in random order.
* Reaper has access to threadsafe active? callKevin Casey2014-02-081-1/+1
|
* if we remove the connection from the pool, we should close itAaron Patterson2013-07-081-0/+1
|
* fix typosAngelo Capilleri2013-06-151-1/+1
|