Skip to content

Commit

Permalink
AR Trilogy Adapter: Handle AdapterTimeout and ConnectionFailed errs (#…
Browse files Browse the repository at this point in the history
…497)

* Update AR trilogy lockfile
* AR Trilogy Adapter: Handle AdapterTimeout and ConnectionFailed errs

This commit ensures that ActiveRecord::AdapterTimeout errors are
handled for the AR Trilogy Adapter. As of rails/rails@785f656,
TimeoutErrors are translated to AdapterTimeout errors in Rails. As such,
we no longer need special handling in `#acquire_semian_resource` for
such errors.

This commit also ensures that ActiveRecord::ConnectionFailed errors are
classified as resource exceptions. These are network-related exceptions
and should be handled by Semian.
  • Loading branch information
adrianna-chang-shopify authored Jun 1, 2023
1 parent 6871dd8 commit d6ba307
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Unreleased

* Ensure Active Record Trilogy adapter handles AdapterTimeout and ConnectionFailed exceptions. (#497)

# v0.19.1

* Active Record Trilogy adapter needs to patch `#raw_execute` instead of `#execute` for queries. (#494)
Expand Down
6 changes: 3 additions & 3 deletions gemfiles/activerecord_trilogy_adapter.gemfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 5 additions & 11 deletions lib/semian/activerecord_trilogy_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,12 @@ def with_resource_timeout(temp_timeout)

private

def acquire_semian_resource(**)
super
rescue ActiveRecord::StatementInvalid => error
if error.cause.is_a?(Trilogy::TimeoutError)
semian_resource.mark_failed(error)
error.semian_identifier = semian_identifier
end
raise
end

def resource_exceptions
[ActiveRecord::ConnectionNotEstablished]
[
ActiveRecord::AdapterTimeout,
ActiveRecord::ConnectionFailed,
ActiveRecord::ConnectionNotEstablished,
]
end

# TODO: share this with Mysql2
Expand Down
10 changes: 10 additions & 0 deletions test/adapters/activerecord_trilogy_adapter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,16 @@ def test_network_errors_are_tagged_with_the_resource_identifier
end
end

def test_connection_failed_errors_are_tagged_with_the_resource_identifier
@adapter.send(:raw_connection).close

error = assert_raises(ActiveRecord::ConnectionFailed) do
@adapter.execute("SELECT 1 + 1;")
end

assert_equal(@adapter.semian_identifier, error.semian_identifier)
end

def test_other_mysql_errors_are_not_tagged_with_the_resource_identifier
error = assert_raises(ActiveRecord::StatementInvalid) do
@adapter.execute("SYNTAX ERROR!")
Expand Down

0 comments on commit d6ba307

Please sign in to comment.