-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Semian + Trilogy: Introduce adapter for Trilogy AR Adapter (#468)
* Install adapter gem, bump to Rails edge * Introduce Trilogy Active Record adapter for Semian * Rubocop: Ignore multiple assertions per test * Add gemfiles for trilogy, CI steps, update changelog * Add test for Trilogy client default timeout
- Loading branch information
1 parent
57d2e0d
commit 98d8601
Showing
10 changed files
with
677 additions
and
14 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,16 @@ | ||
# frozen_string_literal: true | ||
|
||
source "https://rubygems.org" | ||
|
||
gem "rake" | ||
gem "rake-compiler" | ||
gem "minitest" | ||
gem "mocha" | ||
gem "toxiproxy" | ||
gem "webrick" | ||
|
||
gem "trilogy", github: "github/trilogy", branch: "main", glob: "contrib/ruby/*.gemspec" | ||
gem "activerecord-trilogy-adapter", github: "github/activerecord-trilogy-adapter", branch: "main" | ||
gem "activerecord", github: "rails/rails" | ||
|
||
gemspec path: "../" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,120 @@ | ||
# frozen_string_literal: true | ||
|
||
require "semian/adapter" | ||
require "activerecord-trilogy-adapter" | ||
require "active_record/connection_adapters/trilogy_adapter" | ||
|
||
module ActiveRecord | ||
module ConnectionAdapters | ||
class TrilogyAdapter | ||
ActiveRecord::ActiveRecordError.include(::Semian::AdapterError) | ||
|
||
class SemianError < StatementInvalid | ||
def initialize(semian_identifier, *args) | ||
super(*args) | ||
@semian_identifier = semian_identifier | ||
end | ||
end | ||
|
||
ResourceBusyError = Class.new(SemianError) | ||
CircuitOpenError = Class.new(SemianError) | ||
end | ||
end | ||
end | ||
|
||
module Semian | ||
module ActiveRecordTrilogyAdapter | ||
include Semian::Adapter | ||
|
||
ResourceBusyError = ::ActiveRecord::ConnectionAdapters::TrilogyAdapter::ResourceBusyError | ||
CircuitOpenError = ::ActiveRecord::ConnectionAdapters::TrilogyAdapter::CircuitOpenError | ||
|
||
attr_reader :raw_semian_options, :semian_identifier | ||
|
||
def initialize(*options) | ||
*, config = options | ||
@raw_semian_options = config.delete(:semian) | ||
@semian_identifier = begin | ||
name = semian_options && semian_options[:name] | ||
unless name | ||
host = config[:host] || "localhost" | ||
port = config[:port] || 3306 | ||
name = "#{host}:#{port}" | ||
end | ||
:"mysql_#{name}" | ||
end | ||
super | ||
end | ||
|
||
def execute(sql, name = nil, async: false, allow_retry: false) | ||
if query_allowlisted?(sql) | ||
super(sql, name, async: async, allow_retry: allow_retry) | ||
else | ||
acquire_semian_resource(adapter: :trilogy_adapter, scope: :execute) do | ||
super(sql, name, async: async, allow_retry: allow_retry) | ||
end | ||
end | ||
end | ||
|
||
def active? | ||
acquire_semian_resource(adapter: :trilogy_adapter, scope: :ping) do | ||
super | ||
end | ||
rescue ResourceBusyError, CircuitOpenError | ||
false | ||
end | ||
|
||
def with_resource_timeout(temp_timeout) | ||
if connection.nil? | ||
prev_read_timeout = @config[:read_timeout] || 0 | ||
@config.merge!(read_timeout: temp_timeout) # Create new client with temp_timeout for read timeout | ||
else | ||
prev_read_timeout = connection.read_timeout | ||
connection.read_timeout = temp_timeout | ||
end | ||
yield | ||
ensure | ||
@config.merge!(read_timeout: prev_read_timeout) | ||
connection&.read_timeout = prev_read_timeout | ||
end | ||
|
||
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] | ||
end | ||
|
||
# TODO: share this with Mysql2 | ||
QUERY_ALLOWLIST = Regexp.union( | ||
%r{\A(?:/\*.*?\*/)?\s*ROLLBACK}i, | ||
%r{\A(?:/\*.*?\*/)?\s*COMMIT}i, | ||
%r{\A(?:/\*.*?\*/)?\s*RELEASE\s+SAVEPOINT}i, | ||
) | ||
|
||
def query_allowlisted?(sql, *) | ||
QUERY_ALLOWLIST.match?(sql) | ||
rescue ArgumentError | ||
return false unless sql.valid_encoding? | ||
|
||
raise | ||
end | ||
|
||
def connect(*args) | ||
acquire_semian_resource(adapter: :trilogy_adapter, scope: :connection) do | ||
super | ||
end | ||
end | ||
end | ||
end | ||
|
||
ActiveRecord::ConnectionAdapters::TrilogyAdapter.prepend(Semian::ActiveRecordTrilogyAdapter) |
Oops, something went wrong.