-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update the tests to run solid_queue on SQLite. They can be triggered by: ``` TARGET_DB=sqlite rails test ``` The adapter is prone to raising `SQLite3::BusyException`s when concurrent transactions occur. Preventing this requires a couple of patches to the adapter. 1. Implement retry with backoff - add a `retries` config setting to the adapter and sleep progressively longer for each retry. This setting is currently in the Rails main branch, but that implementation has no backoff. That doesn't work well in our case. 1. Always create immediate transactions - SQLite by default creates deferred transactions, which don't take a write lock. Then later if there is a write it tries to upgrade the lock. This won't work if the transaction has a stale read so retrying the write by itself is not possible. Starting with an immediate transaction moves the write lock to that point and ensures that we only get blocked on a retryable transaction.
- Loading branch information
Showing
6 changed files
with
61 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -117,6 +117,7 @@ def start_runner(runner) | |
|
||
pid = fork do | ||
runner.start | ||
exit! | ||
end | ||
|
||
forks[pid] = runner | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module SqliteImmediateTransactions | ||
def begin_db_transaction | ||
log("begin immediate transaction", "TRANSACTION") do | ||
with_raw_connection(allow_retry: true, materialize_transactions: false) do |conn| | ||
conn.transaction(:immediate) | ||
end | ||
end | ||
end | ||
end | ||
|
||
module SQLite3Configuration | ||
private | ||
def configure_connection | ||
super | ||
|
||
if @config[:retries] | ||
retries = self.class.type_cast_config_to_integer(@config[:retries]) | ||
raw_connection.busy_handler do |count| | ||
(count <= retries).tap { |result| sleep count * 0.001 if result } | ||
end | ||
end | ||
end | ||
end | ||
|
||
ActiveSupport.on_load :active_record do | ||
if defined?(ActiveRecord::ConnectionAdapters::SQLite3Adapter) | ||
ActiveRecord::ConnectionAdapters::SQLite3Adapter.prepend SqliteImmediateTransactions | ||
ActiveRecord::ConnectionAdapters::SQLite3Adapter.prepend SQLite3Configuration | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters