trilogy_aurora 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b68247e00782c8fd79fe8aacb267418d8615ec7cf355870df6c7a01669c44f13
4
+ data.tar.gz: d7387204da336b760389f2c2baa65a64332f399ff4940e0e6a27221cc3c31cb4
5
+ SHA512:
6
+ metadata.gz: 31cf25f9b38fc5f275d1c0fe0d61c47788fef802adb4359180b7c680f063a85aaf7c71f0b685005e522ac4a086fa424c4e631b853d994ee6d7b0f9dc68be45d5
7
+ data.tar.gz: 575264af4b39956126cba399667eb958359687d0a286427092e25f2dd74b54b228313e8d48dd94cec0f80e0ec5ba6cf59a754410600a1ef29d9c84e797893d85
data/.dockerignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rdoc_options ADDED
@@ -0,0 +1,33 @@
1
+ ---
2
+ encoding: UTF-8
3
+ static_path: []
4
+ rdoc_include: []
5
+ page_dir:
6
+ charset: UTF-8
7
+ exclude:
8
+ - "~\\z"
9
+ - "\\.orig\\z"
10
+ - "\\.rej\\z"
11
+ - "\\.bak\\z"
12
+ - "\\.gemspec\\z"
13
+ - "Dockerfile"
14
+ - "Gemfile"
15
+ - "Gemfile.lock"
16
+ - "bin"
17
+ - "coverage"
18
+ - "Rakefile"
19
+ hyperlink_all: false
20
+ line_numbers: false
21
+ locale:
22
+ locale_dir: locale
23
+ locale_name:
24
+ main_page:
25
+ markup: markdown
26
+ output_decoration: true
27
+ show_hash: false
28
+ skip_tests: true
29
+ tab_width: 8
30
+ template_stylesheets: []
31
+ title:
32
+ visibility: :protected
33
+ webcvs:
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,30 @@
1
+ require:
2
+ - rubocop-rake
3
+ - rubocop-rspec
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 3.0
7
+ NewCops: enable
8
+
9
+ Style/StringLiterals:
10
+ Enabled: true
11
+ EnforcedStyle: double_quotes
12
+
13
+ Style/StringLiteralsInInterpolation:
14
+ Enabled: true
15
+ EnforcedStyle: double_quotes
16
+
17
+ Layout/LineLength:
18
+ Max: 120
19
+
20
+ Metrics/MethodLength:
21
+ Max: 20
22
+
23
+ RSpec/MultipleExpectations:
24
+ Max: 2
25
+
26
+ RSpec/NestedGroups:
27
+ Max: 5
28
+
29
+ RSpec/MessageSpies:
30
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-09-03
4
+
5
+ - Initial release
data/Dockerfile ADDED
@@ -0,0 +1,16 @@
1
+ ARG RUBY_VERSION=3.2
2
+
3
+ FROM ruby:${RUBY_VERSION}-alpine
4
+
5
+ # Create application directory.
6
+ RUN mkdir /app
7
+ WORKDIR /app
8
+
9
+ # Install packages
10
+ RUN apk upgrade && apk add --update build-base git linux-headers libxml2-dev libxslt-dev mariadb-dev ruby-dev tzdata yaml-dev zlib-dev
11
+
12
+ # Deploy application
13
+ ADD . /app
14
+
15
+ # Install gems
16
+ RUN sh /app/bin/setup
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Aidan Lavis
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # ⚠️ Warning
2
+
3
+ As far as I'm aware, this hasn't been used in production. Please test before using!
4
+
5
+ If you use this gem in production, please let me know so I can update this README!
6
+
7
+ # TrilogyAurora
8
+
9
+ Modifies [Trilogy](https://github.com/trilogy-libraries/trilogy) to support AWS Aurora failover.
10
+
11
+ Essentially, the [mysql2-aurora](https://github.com/alfa-jpn/mysql2-aurora) gem but for Trilogy.
12
+
13
+ ## Installation
14
+
15
+ Install the gem and add to the application's Gemfile by executing:
16
+
17
+ $ bundle add trilogy_aurora
18
+
19
+ ## Usage
20
+
21
+ In addition to existing initialization options for `Trilogy`, you can now also use the `aurora_max_retry` and `aurora_disconnect_on_readonly` options.
22
+
23
+ ```ruby
24
+ Trilogy.new(
25
+ host: 'localhost',
26
+ username: 'root',
27
+ password: 'change_me',
28
+ aurora_max_retry: 5,
29
+ aurora_disconnect_on_readonly: true
30
+ )
31
+ ```
32
+
33
+ with Rails >= 7.1, in `database.yml`
34
+
35
+ ```yml
36
+ development:
37
+ adapter: trilogy
38
+ host: localhost
39
+ username: root
40
+ password: change_me
41
+ aurora_max_retry: 5
42
+ aurora_disconnect_on_readonly: true
43
+ ```
44
+
45
+ From the README of [mysql2-aurora](https://github.com/alfa-jpn/mysql2-aurora):
46
+ > There are essentially two methods for handling and RDS Aurora failover. When there is an Aurora RDS failover event the primary writable server can change it's role to become a read_only replica. This can happen without active database connections droppping.
47
+ > This leaves the connection in a state where writes will fail, but the application belives it's connected to a writeable server. Writes will now perpetually fail until the database connection is closed and re-established connecting back to the new primary.
48
+ >
49
+ > To provide automatic recovery from this method you can use either a graceful retry, or an immediate disconnection option.
50
+ > ### Retry
51
+ >
52
+ > Setting aurora_max_retry, mysql2 will not disconnect and automatically attempt re-connection to the database when a read_only error message is encountered.
53
+ > This has the benefit that to the application the error is transparent and the query will be re-run against the new primary when the connection succeeds.
54
+ >
55
+ > It is however not safe to use with transactions
56
+ >
57
+ > Consider:
58
+ >
59
+ > * Transaction is started on the primary server A
60
+ > * Failover event occurs, A is now readonly
61
+ > * Application issues a write statement, read_only exception is thrown
62
+ > * mysql2-aurora gem handles this by reconnecting transparently to the new primary B
63
+ > * Aplication continues issuing writes however on a new connection in auto-commit mode, no new transaction was started
64
+ >
65
+ > The application remains un-aware it is now operating outside of a transaction, this can leave data in an inconcistent state, and issuing a ROLLBACK, or COMMIT will not have the expected outcome.
66
+ >
67
+ > ### Immediate disconnect
68
+ >
69
+ > Setting aurora_disconnect_on_readonly to true, will cause mysql2 to close the connection to the database on read_only exception. The original exception will be thrown up the stack to the application.
70
+ > With the database connection disconnected, the next statement will hit the disconnected error and the application can handle this as it would normally when been disconnected from the database.
71
+ >
72
+ > This is safe with transactions.
73
+
74
+ ## Contributing
75
+
76
+ Bug reports and pull requests are welcome on GitHub at https://github.com/a-lavis/trilogy_aurora.
77
+
78
+ ## Testing
79
+
80
+ ```shell
81
+ # Image build
82
+ bin/docker-build
83
+
84
+ # Run tests
85
+ bin/docker-test
86
+ ```
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,23 @@
1
+ services:
2
+ app:
3
+ build:
4
+ context: .
5
+ environment:
6
+ TEST_DB_HOST: 'mysql'
7
+ TEST_DB_USER: 'root'
8
+ TEST_DB_PASS: ''
9
+ volumes:
10
+ - .:/app
11
+ depends_on:
12
+ mysql:
13
+ condition: service_healthy
14
+ mysql:
15
+ image: mysql:8.0
16
+ environment:
17
+ MYSQL_ROOT_PASSWORD: ''
18
+ MYSQL_ALLOW_EMPTY_PASSWORD: 1
19
+ healthcheck:
20
+ test: ["CMD-SHELL", "mysqladmin ping -h mysql"]
21
+ interval: 10s
22
+ timeout: 5s
23
+ retries: 3
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TrilogyAurora
4
+ # The version of this gem.
5
+ VERSION = "0.1.0"
6
+ end
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "trilogy_aurora/version"
4
+ require "trilogy"
5
+
6
+ # This module contains Trilogy, a wrapper around ::Trilogy that handles read-only errors from Aurora.
7
+ #
8
+ # It also contains the original ::Trilogy class as ORIGINAL_TRILOGY_CLASS.
9
+ module TrilogyAurora
10
+ # A wrapper around ::Trilogy that handles read-only errors from Aurora.
11
+ class Trilogy
12
+ # The ::Trilogy instance.
13
+ attr_reader :trilogy
14
+
15
+ # Pass in any options that ::Trilogy accepts.
16
+ #
17
+ # Additionally, you can pass in the `aurora_max_retry` and `aurora_disconnect_on_readonly` options:
18
+ #
19
+ # - `aurora_max_retry` is the number of times to retry a query when it fails due to a read-only error.
20
+ # The default is 5.
21
+ #
22
+ # - `aurora_disconnect_on_readonly` is a boolean that determines whether or not to disconnect from the database
23
+ # after a read-only error is encountered.
24
+ # The default is false.
25
+ def initialize(opts)
26
+ @opts = opts&.transform_keys(&:to_sym)
27
+ @max_retry = @opts.delete(:aurora_max_retry) || 5
28
+ @disconnect_only = @opts.delete(:aurora_disconnect_on_readonly) || false
29
+ reconnect!
30
+ end
31
+
32
+ # Execute a ::Trilogy query, disconnecting or reconnecting after read-only errors
33
+ # based on initialization options.
34
+ def query(...)
35
+ try_count = 0
36
+
37
+ begin
38
+ trilogy.query(...)
39
+ rescue TrilogyAurora::ORIGINAL_TRILOGY_CLASS::Error => e
40
+ raise e unless e.message&.include?("--read-only")
41
+
42
+ try_count += 1
43
+
44
+ if @disconnect_only
45
+ warn(
46
+ "[trilogy_aurora] Database is readonly, Aurora failover event likely occured, closing database connection"
47
+ )
48
+ disconnect!
49
+ elsif try_count <= @max_retry
50
+ retry_interval_seconds = [1.5 * (try_count - 1), 10].min
51
+
52
+ warn "[trilogy_aurora] Database is readonly. Retry after #{retry_interval_seconds}seconds"
53
+ sleep retry_interval_seconds
54
+ reconnect!
55
+ retry
56
+ end
57
+
58
+ raise e
59
+ end
60
+ end
61
+
62
+ # Disconnect and re-initialize ::Trilogy
63
+ def reconnect!
64
+ disconnect!
65
+
66
+ @trilogy = TrilogyAurora::ORIGINAL_TRILOGY_CLASS.new(@opts)
67
+ end
68
+
69
+ # Close ::Trilogy connection
70
+ def disconnect!
71
+ @trilogy&.close
72
+ rescue StandardError
73
+ nil
74
+ end
75
+
76
+ # Delegate instance method calls to ::Trilogy instance.
77
+ def method_missing(name, ...)
78
+ trilogy.public_send(name, ...)
79
+ end
80
+
81
+ # Delegate `respond_to?` to ::Trilogy instance.
82
+ def respond_to_missing?(name, ...)
83
+ trilogy.respond_to?(name, ...)
84
+ end
85
+
86
+ # Delegate class method calls to ::Trilogy.
87
+ def self.method_missing(name, ...)
88
+ TrilogyAurora::ORIGINAL_TRILOGY_CLASS.public_send(name, ...)
89
+ end
90
+
91
+ # Delegate `respond_to?` to ::Trilogy.
92
+ def self.respond_to_missing?(name, ...)
93
+ TrilogyAurora::ORIGINAL_TRILOGY_CLASS.respond_to?(name, ...)
94
+ end
95
+
96
+ # Delegate const reference to ::Trilogy.
97
+ def self.const_missing(name)
98
+ TrilogyAurora::ORIGINAL_TRILOGY_CLASS.const_get(name)
99
+ end
100
+ end
101
+
102
+ # The original ::Trilogy class.
103
+ ORIGINAL_TRILOGY_CLASS = ::Trilogy
104
+ # Swap out the original ::Trilogy class with our wrapper (Trilogy).
105
+ ::Trilogy = TrilogyAurora::Trilogy
106
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trilogy_aurora
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Aidan Lavis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-09-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: trilogy
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.5'
27
+ description:
28
+ email:
29
+ - aidanlavis@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".dockerignore"
35
+ - ".rdoc_options"
36
+ - ".rspec"
37
+ - ".rubocop.yml"
38
+ - CHANGELOG.md
39
+ - Dockerfile
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - docker-compose.yml
44
+ - lib/trilogy_aurora.rb
45
+ - lib/trilogy_aurora/version.rb
46
+ homepage: https://github.com/a-lavis/trilogy_aurora
47
+ licenses:
48
+ - MIT
49
+ metadata:
50
+ homepage_uri: https://github.com/a-lavis/trilogy_aurora
51
+ source_code_uri: https://github.com/a-lavis/trilogy_aurora
52
+ rubygems_mfa_required: 'true'
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 3.0.0
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubygems_version: 3.2.33
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: Adds AWS Aurora failover support to Trilogy.
72
+ test_files: []