active_record_proxy_adapters 0.1.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a84998cef36e3e7ba5c4e69909a41db4641a22aaa2b589988fe38f74abefa600
4
+ data.tar.gz: e0ab2ac9b05420d952c89f7259c6d3539eadf7b44fd791bb178dc28e91066b30
5
+ SHA512:
6
+ metadata.gz: 39d6c4508378596812e6dbdf523e05ff662b279ace17f6ec0ec2db24171645374e9447fe5aff6e6a9f9f2bfb227fce4a0dddc6deebe016b2d2a1f62234f198f9
7
+ data.tar.gz: 8b423c0d1464157eff63c0c161481acb2a2ba55e95f9337c4a27819c1acbfe41ccf83e7e59bdb98cea1e80419159b475060a166336251c59759d4298fe0c502f
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --format documentation
2
+ --color
3
+ --order random
4
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,20 @@
1
+ require:
2
+ - rubocop-rspec
3
+
4
+ AllCops:
5
+ TargetRubyVersion: 3.1
6
+ NewCops: enable
7
+
8
+ Style/StringLiterals:
9
+ EnforcedStyle: double_quotes
10
+
11
+ Style/StringLiteralsInInterpolation:
12
+ EnforcedStyle: double_quotes
13
+
14
+ RSpec/NestedGroups:
15
+ Enabled: true
16
+ Max: 5
17
+
18
+ # TODO: Enable MFA once we open source the gem
19
+ Gemspec/RequireMFA:
20
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [0.1.0.rc2] - 2024-10-28
2
+
3
+ - Add PostgreSQLProxyAdapter
4
+
5
+ ## [Unreleased]
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/Dockerfile ADDED
@@ -0,0 +1,18 @@
1
+ ARG RUBY_VERSION=3.2.3
2
+ ARG DOCKER_REGISTRY=docker.io
3
+ FROM $DOCKER_REGISTRY/ruby:$RUBY_VERSION-alpine
4
+ ARG RAILS_VERSION="~> 6.1.0"
5
+ ENV RAILS_VERSION=$RAILS_VERSION
6
+
7
+ RUN apk --update add \
8
+ build-base \
9
+ git \
10
+ postgresql-dev \
11
+ postgresql-client
12
+ RUN gem install bundler -v 2.5.13
13
+
14
+ COPY . /app
15
+ WORKDIR /app
16
+
17
+ RUN bundle install
18
+
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Nasdaq
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,114 @@
1
+ # ActiveRecordProxyAdapters
2
+
3
+ A set of ActiveRecord adapters that leverage Rails native multiple database setup to allow automatic connection switching from _one_ primary pool to _one_ replica pool at the database statement level.
4
+
5
+ ## Installation
6
+
7
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add 'UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG'
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
16
+
17
+ ## Usage
18
+
19
+ ### On Rails
20
+
21
+ In `config/database.yml`, use `postgresql_proxy` as the adapter for the `primary` database, and keep `postgresql` for the replica database.
22
+
23
+ ```yaml
24
+ # config/database.yml
25
+ development:
26
+ primary:
27
+ adapter: postgresql_proxy
28
+ # your primary credentials here
29
+
30
+ primary_replica:
31
+ adapter: postgresql
32
+ replica: true
33
+ # your replica credentials here
34
+ ```
35
+
36
+ ### Off Rails
37
+
38
+ ```ruby
39
+ # In your application setup
40
+ require "active_record_proxy_adapters"
41
+
42
+ ActiveSupport.on_load :active_record do
43
+ require "active_record_proxy_adapters/connection_handling"
44
+ ActiveRecord::Base.extend(ActiveRecordProxyAdapters::ConnectionHandling)
45
+ end
46
+
47
+ # in your base model
48
+ class ApplicationRecord << ActiveRecord::Base
49
+ establish_connection(
50
+ {
51
+ adapter: 'postgresql_proxy',
52
+ # your primary credentials here
53
+ },
54
+ role: :writing
55
+ )
56
+
57
+ establish_connection(
58
+ {
59
+ adapter: 'postgresql',
60
+ # your replica credentials here
61
+ },
62
+ role: :reading
63
+ )
64
+ end
65
+ ```
66
+
67
+ ### Configuration
68
+
69
+ The gem comes preconfigured out of the box. However, if default configuration does not suit your needs, you can modify them by using a `.configure` block:
70
+
71
+ ```ruby
72
+ # config/initializers/active_record_proxy_adapters.rb
73
+ ActiveRecordProxyAdapters.configure do |config|
74
+ # How long proxy should reroute all read requests to primary after a write
75
+ config.proxy_delay = 5.seconds # defaults to 2.seconds
76
+
77
+ # How long proxy should wait for replica to connect.
78
+ config.checkout_timeout = 5.seconds # defaults to 2.seconds
79
+ end
80
+ ```
81
+
82
+ ### How it works
83
+
84
+ The proxy will analyze each SQL string, using pattern matching, to decide the appropriate connection for it (i.e. if it should go to the primary or replica).
85
+
86
+ - All queries inside a transaction go to the primary
87
+ - All `SET` queries go to all connections
88
+ - All `INSERT`, `UPDATE` and `DELETE` queries go to the primary
89
+ - All `SELECT FOR UPDATE` queries go to the primary
90
+ - All `lock` queries (e.g `get_lock`) go the primary
91
+ - All sequence methods (e.g `nextval`) go the primary
92
+ - Everything else goes to the replica
93
+
94
+ #### TL;DR
95
+
96
+ All `SELECT` queries go to the _replica_, everything else goes to _primary_.
97
+
98
+ ## Development
99
+
100
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
101
+
102
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
103
+
104
+ ## Contributing
105
+
106
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nasdaq/active_record_proxy_adapters. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/nasdaq/active_record_proxy_adapters/blob/main/CODE_OF_CONDUCT.md).
107
+
108
+ ## License
109
+
110
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
111
+
112
+ ## Code of Conduct
113
+
114
+ Everyone interacting in the ActiveRecordProxyAdapters project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/nasdaq/active_record_proxy_adapters/blob/main/CODE_OF_CONDUCT.md).
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,77 @@
1
+ name: active_record_proxy_adapters
2
+
3
+ x-postgres-common: &postgres-common
4
+ restart: always
5
+ user: postgres
6
+ healthcheck:
7
+ test: 'pg_isready -U postgres_primary_test --dbname=postgres'
8
+ interval: 10s
9
+ timeout: 5s
10
+ retries: 5
11
+ networks:
12
+ - postgres
13
+
14
+ services:
15
+ app:
16
+ build:
17
+ args:
18
+ - RUBY_VERSION=${RUBY_VERSION:-3.2.3}
19
+ - RAILS_VERSION=${RAILS_VERSION:-~> 6.1.0}
20
+ container_name: app
21
+ image: active_record_proxy_adapters-app:${ENV_TAG:-latest}
22
+ tty: true
23
+ stdin_open: true
24
+ environment:
25
+ PGHOST: postgres_primary
26
+ PG_PRIMARY_USER: postgres_primary_test
27
+ PG_PRIMARY_PASSWORD: postgres_primary_test
28
+ PG_PRIMARY_HOST: postgres_primary
29
+ PG_PRIMARY_PORT: 5432
30
+ PG_REPLICA_USER: postgres_primary_test
31
+ PG_REPLICA_PASSWORD: postgres_primary_test
32
+ PG_REPLICA_HOST: postgres_replica
33
+ PG_REPLICA_PORT: 5432
34
+ depends_on:
35
+ - postgres_primary
36
+ - postgres_replica
37
+ networks:
38
+ - app
39
+ - postgres
40
+ volumes:
41
+ - .:/app
42
+ postgres_primary:
43
+ <<: *postgres-common
44
+ build:
45
+ context: .
46
+ dockerfile: postgres_primary.dockerfile
47
+ args:
48
+ - POSTGRES_LOGGING_COLLECTOR=${POSTGRES_LOGGING_COLLECTOR:-}
49
+ - POSTGRES_LOG_DESTINATION=${POSTGRES_LOG_DESTINATION:-}
50
+ - POSTGRES_LOG_STATEMENT=${POSTGRES_LOG_STATEMENT:-}
51
+ - REPLICA_USER=replicator
52
+ - REPLICA_PASSWORD=replicator
53
+ environment:
54
+ POSTGRES_DB: postgres
55
+ POSTGRES_USER: postgres_primary_test
56
+ POSTGRES_PASSWORD: postgres_primary_test
57
+ POSTGRES_HOST_AUTH_METHOD: "scram-sha-256\nhost replication all 0.0.0.0/0 md5"
58
+ POSTGRES_INITDB_ARGS: "--auth-host=scram-sha-256"
59
+
60
+ postgres_replica:
61
+ <<: *postgres-common
62
+ build:
63
+ context: .
64
+ dockerfile: postgres_replica.dockerfile
65
+ container_name: postgres_replica
66
+ environment:
67
+ PGUSER: replicator
68
+ PGPASSWORD: replicator
69
+ PRIMARY_DATABASE_HOST: postgres_primary
70
+ depends_on:
71
+ - postgres_primary
72
+ networks:
73
+ app:
74
+ postgres:
75
+
76
+ volumes:
77
+ postgres_primary:
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record/tasks/postgresql_proxy_database_tasks"
4
+ require "active_record/connection_adapters/postgresql_adapter"
5
+ require "active_record_proxy_adapters/postgresql_proxy"
6
+ require "active_record_proxy_adapters/hijackable"
7
+
8
+ module ActiveRecord
9
+ module ConnectionAdapters
10
+ # This adapter is a proxy to the original PostgreSQLAdapter, allowing the use of the
11
+ # ActiveRecordProxyAdapters::PrimaryReplicaProxy.
12
+ class PostgreSQLProxyAdapter < PostgreSQLAdapter
13
+ include ActiveRecordProxyAdapters::Hijackable
14
+
15
+ ADAPTER_NAME = "PostgreSQLProxy"
16
+
17
+ delegate_to_proxy :execute, :exec_query, :exec_no_cache, :exec_cache
18
+
19
+ def initialize(...)
20
+ @proxy = ActiveRecordProxyAdapters::PostgreSQLProxy.new(self)
21
+
22
+ super
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :proxy
28
+ end
29
+ end
30
+ end
31
+
32
+ if ActiveRecordProxyAdapters::ActiveRecordContext.active_record_v7_2?
33
+ ActiveRecord::ConnectionAdapters.register(
34
+ "postgresql_proxy",
35
+ "ActiveRecord::ConnectionAdapters::PostgreSQLProxyAdapter",
36
+ "active_record/connection_adapters/postgresql_proxy_adapter"
37
+ )
38
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module Tasks
5
+ # Defines the postgresql tasks for dropping, creating, loading schema and dumping schema.
6
+ # Bypasses all the proxy logic to send all requests to primary.
7
+ class PostgreSQLProxyDatabaseTasks < PostgreSQLDatabaseTasks
8
+ def create(...)
9
+ sticking_to_primary { super }
10
+ end
11
+
12
+ def drop(...)
13
+ sticking_to_primary { super }
14
+ end
15
+
16
+ def structure_dump(...)
17
+ sticking_to_primary { super }
18
+ end
19
+
20
+ def structure_load(...)
21
+ sticking_to_primary { super }
22
+ end
23
+
24
+ def purge(...)
25
+ sticking_to_primary { super }
26
+ end
27
+
28
+ private
29
+
30
+ def sticking_to_primary(&)
31
+ ActiveRecord::Base.connected_to(role: context.writing_role, &)
32
+ end
33
+
34
+ def context
35
+ ActiveRecordProxyAdapters::ActiveRecordContext.new
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ # Allow proxy adapter to run rake tasks, i.e. db:drop, db:create, db:schema:load db:migrate, etc...
42
+ ActiveRecord::Tasks::DatabaseTasks.register_task(
43
+ /postgresql_proxy/,
44
+ "ActiveRecord::Tasks::PostgreSQLProxyDatabaseTasks"
45
+ )
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecordProxyAdapters
4
+ # Collection of helpers to handle common active record methods that are defined in different places in different
5
+ # versions of rails.
6
+ class ActiveRecordContext
7
+ delegate :reading_role, :reading_role=, :writing_role, :writing_role=, to: :role_context
8
+ delegate :legacy_connection_handling, :legacy_connection_handling=, to: :connection_handling_context
9
+ delegate :version, to: :ActiveRecord, prefix: :active_record
10
+
11
+ class << self
12
+ delegate_missing_to :new
13
+ end
14
+
15
+ NullConnectionHandlingContext = Class.new do
16
+ def legacy_connection_handling
17
+ false
18
+ end
19
+
20
+ def legacy_connection_handling=(_value)
21
+ nil
22
+ end
23
+ end
24
+
25
+ def reading_role
26
+ role_context.reading_role
27
+ end
28
+
29
+ def writing_role
30
+ role_context.writing_role
31
+ end
32
+
33
+ def connection_class_for(connection)
34
+ klass = active_record_v6_1? ? connection.connection_klass : connection.connection_class
35
+
36
+ klass || ActiveRecord::Base
37
+ end
38
+
39
+ def connection_handling_context
40
+ # This config option has been removed in Rails 7.1+
41
+ return NullConnectionHandlingContext.new if active_record_v7_1? || active_record_v7_2?
42
+
43
+ global_configuration_context
44
+ end
45
+
46
+ def role_context
47
+ global_configuration_context
48
+ end
49
+
50
+ def global_configuration_context
51
+ active_record_v6_1? ? ActiveRecord::Base : ActiveRecord
52
+ end
53
+
54
+ def active_record_v6_1?
55
+ active_record_version >= Gem::Version.new("6.1") && active_record_version < Gem::Version.new("7.0")
56
+ end
57
+
58
+ def active_record_v7_1?
59
+ active_record_version >= Gem::Version.new("7.1") && active_record_version < Gem::Version.new("7.2")
60
+ end
61
+
62
+ def active_record_v7_2?
63
+ active_record_version >= Gem::Version.new("7.2") && active_record_version < Gem::Version.new("8.0")
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/integer/time"
4
+
5
+ module ActiveRecordProxyAdapters
6
+ # Provides a global configuration object to configure how the proxy should behave.
7
+ class Configuration
8
+ PROXY_DELAY = 2.seconds.freeze
9
+ CHECKOUT_TIMEOUT = 2.seconds.freeze
10
+
11
+ # @return [ActiveSupport::Duration] How long the proxy should reroute all read requests to the primary database
12
+ # since the latest write. Defaults to PROXY_DELAY.
13
+ attr_accessor :proxy_delay
14
+ # @return [ActiveSupport::Duration] How long the proxy should wait for a connection from the replica pool.
15
+ # Defaults to CHECKOUT_TIMEOUT.
16
+ attr_accessor :checkout_timeout
17
+
18
+ def initialize
19
+ self.proxy_delay = PROXY_DELAY
20
+ self.checkout_timeout = CHECKOUT_TIMEOUT
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record/connection_adapters/postgresql_proxy_adapter"
4
+
5
+ module ActiveRecordProxyAdapters
6
+ # Module to extend ActiveRecord::Base with the connection handling methods.
7
+ # Required to make adapter work in ActiveRecord versions <= 7.2.x
8
+ module ConnectionHandling
9
+ def postgresql_proxy_adapter_class
10
+ ::ActiveRecord::ConnectionAdapters::PostgreSQLProxyAdapter
11
+ end
12
+
13
+ # This method is a copy and paste from Rails' postgresql_connection,
14
+ # replacing PostgreSQLAdapter by PostgreSQLProxyAdapter
15
+ # This is required by ActiveRecord versions <= 7.2.x to establish a connection using the adapter.
16
+ def postgresql_proxy_connection(config) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
17
+ conn_params = config.symbolize_keys.compact
18
+
19
+ # Map ActiveRecords param names to PGs.
20
+ conn_params[:user] = conn_params.delete(:username) if conn_params[:username]
21
+ conn_params[:dbname] = conn_params.delete(:database) if conn_params[:database]
22
+
23
+ # Forward only valid config params to PG::Connection.connect.
24
+ valid_conn_param_keys = PG::Connection.conndefaults_hash.keys + [:requiressl]
25
+ conn_params.slice!(*valid_conn_param_keys)
26
+
27
+ postgresql_proxy_adapter_class.new(
28
+ postgresql_proxy_adapter_class.new_client(conn_params),
29
+ logger,
30
+ conn_params,
31
+ config
32
+ )
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record/tasks/postgresql_proxy_database_tasks"
4
+ require "active_record/connection_adapters/postgresql_adapter"
5
+ require "active_record_proxy_adapters/primary_replica_proxy"
6
+
7
+ module ActiveRecordProxyAdapters
8
+ # Defines mixins to delegate specific methods from the proxy to the adapter.
9
+ module Hijackable
10
+ extend ActiveSupport::Concern
11
+
12
+ class_methods do
13
+ # Renames the methods from the original Adapter using the proxy suffix (_unproxied)
14
+ # and delegate the original method name to the proxy.
15
+ # Example: delegate_to_proxy(:execute) creates a method `execute_unproxied`,
16
+ # while delegating :execute to the proxy.
17
+ # @param method_name [Array<Symbol>] the names of methods to be redefined.
18
+ def delegate_to_proxy(*method_names)
19
+ method_names.each do |method_name|
20
+ proxy_method_name = proxy_method_name_for(method_name)
21
+ proxy_method_private = private_method_defined?(method_name)
22
+
23
+ # some adapter methods are private. We need to make them public before aliasing.
24
+ public method_name if proxy_method_private
25
+
26
+ alias_method proxy_method_name, method_name
27
+
28
+ # If adapter method was originally private. We now make them private again.
29
+ private method_name, proxy_method_name if proxy_method_private
30
+ end
31
+
32
+ delegate(*method_names, to: :proxy)
33
+ end
34
+
35
+ private
36
+
37
+ def proxy_method_name_for(method_name)
38
+ :"#{method_name}#{ActiveRecordProxyAdapters::PrimaryReplicaProxy::UNPROXIED_METHOD_SUFFIX}"
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record_proxy_adapters/primary_replica_proxy"
4
+
5
+ module ActiveRecordProxyAdapters
6
+ # Proxy to the original PostgreSQLAdapter, allowing the use of the ActiveRecordProxyAdapters::PrimaryReplicaProxy.
7
+ class PostgreSQLProxy < PrimaryReplicaProxy
8
+ # ActiveRecord::PostgreSQLAdapter methods that should be proxied.
9
+ hijack_method :execute, :exec_query, :exec_no_cache, :exec_cache
10
+ end
11
+ end
@@ -0,0 +1,183 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record_proxy_adapters/configuration"
4
+ require "active_support/core_ext/module/delegation"
5
+ require "active_support/core_ext/object/blank"
6
+ require "concurrent-ruby"
7
+ require "active_record_proxy_adapters/active_record_context"
8
+
9
+ module ActiveRecordProxyAdapters
10
+ # This is the base class for all proxies. It defines the methods that should be proxied
11
+ # and the logic to determine which database to use.
12
+ class PrimaryReplicaProxy # rubocop:disable Metrics/ClassLength
13
+ # All queries that match these patterns should be sent to the primary database
14
+ SQL_PRIMARY_MATCHERS = [
15
+ /\A\s*select.+for update\Z/i, /select.+lock in share mode\Z/i,
16
+ /\A\s*select.+(nextval|currval|lastval|get_lock|release_lock|pg_advisory_lock|pg_advisory_unlock)\(/i
17
+ ].map(&:freeze).freeze
18
+ # All queries that match these patterns should be sent to the replica database
19
+ SQL_REPLICA_MATCHERS = [/\A\s*(select|with.+\)\s*select)\s/i].map(&:freeze).freeze
20
+ # All queries that match these patterns should be sent to all databases
21
+ SQL_ALL_MATCHERS = [/\A\s*set\s/i].map(&:freeze).freeze
22
+ # Local sets queries should not be sent to all datbases
23
+ SQL_SKIP_ALL_MATCHERS = [/\A\s*set\s+local\s/i].map(&:freeze).freeze
24
+ # These patterns define which database statments are considered write statments, so we can shortly re-route all
25
+ # requests to the primary database so the replica has time to replicate
26
+ WRITE_STATEMENT_MATCHERS = [/\ABEGIN/i, /\ACOMMIT/i, /INSERT\sINTO\s/i, /UPDATE\s/i, /DELETE\sFROM\s/i,
27
+ /DROP\s/i].map(&:freeze).freeze
28
+ UNPROXIED_METHOD_SUFFIX = "_unproxied"
29
+
30
+ # Defines which methods should be hijacked from the original adapter and use the proxy
31
+ # @param method_names [Array<Symbol>] the list of method names from the adapter
32
+ def self.hijack_method(*method_names)
33
+ method_names.each do |method_name|
34
+ define_method(method_name) do |*args, **kwargs, &block|
35
+ proxy_bypass_method = "#{method_name}#{UNPROXIED_METHOD_SUFFIX}"
36
+ sql_string = coerce_query_to_string(args.first)
37
+
38
+ appropriate_connection(sql_string) do |conn|
39
+ method_to_call = conn == primary_connection ? proxy_bypass_method : method_name
40
+
41
+ conn.send(method_to_call, *args, **kwargs, &block)
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ # @param primary_connection [ActiveRecord::ConnectionAdatpers::AbstractAdapter]
48
+ def initialize(primary_connection)
49
+ @primary_connection = primary_connection
50
+ @last_write_at = 0
51
+ @active_record_context = ActiveRecordContext.new
52
+ end
53
+
54
+ private
55
+
56
+ attr_reader :primary_connection, :last_write_at, :active_record_context
57
+
58
+ delegate :connected_to_stack, to: :connection_class
59
+ delegate :reading_role, :writing_role, to: :active_record_context
60
+
61
+ def connection_class
62
+ active_record_context.connection_class_for(primary_connection)
63
+ end
64
+
65
+ def replica_pool
66
+ ActiveRecord::Base.connected_to(role: reading_role) { ActiveRecord::Base.connection_pool }
67
+ end
68
+
69
+ def coerce_query_to_string(sql_or_arel)
70
+ sql_or_arel.respond_to?(:to_sql) ? sql_or_arel.to_sql : sql_or_arel.to_s
71
+ end
72
+
73
+ def appropriate_connection(sql_string, &block)
74
+ roles_for(sql_string).map do |role|
75
+ connection_for(role, sql_string) do |connection|
76
+ block.call(connection)
77
+ end
78
+ end.last
79
+ end
80
+
81
+ def roles_for(sql_string)
82
+ return [top_of_connection_stack_role] if top_of_connection_stack_role.present?
83
+
84
+ if need_all?(sql_string)
85
+ [reading_role, writing_role]
86
+ elsif need_primary?(sql_string)
87
+ [writing_role]
88
+ else
89
+ [reading_role]
90
+ end
91
+ end
92
+
93
+ def top_of_connection_stack_role
94
+ return if connected_to_stack.empty?
95
+
96
+ top = connected_to_stack.last
97
+ role = top[:role]
98
+ return unless role.present?
99
+
100
+ [reading_role, writing_role].include?(role) ? role : nil
101
+ end
102
+
103
+ def connection_for(role, sql_string) # rubocop:disable Metrics/MethodLength
104
+ connection = if role == writing_role
105
+ primary_connection
106
+ else
107
+ begin
108
+ replica_pool.checkout(checkout_timeout)
109
+ # rescue NoDatabaseError to avoid crashing when running db:create rake task
110
+ # rescue ConnectionNotEstablished to handle connectivity issues in the replica
111
+ # (for example, replication delay)
112
+ rescue ActiveRecord::NoDatabaseError, ActiveRecord::ConnectionNotEstablished
113
+ primary_connection
114
+ end
115
+ end
116
+
117
+ result = yield(connection)
118
+ update_primary_latest_write_timestamp if !replica_connection?(connection) && write_statement?(sql_string)
119
+
120
+ result
121
+ ensure
122
+ replica_connection?(connection) && replica_pool.checkin(connection)
123
+ end
124
+
125
+ def replica_connection?(connection)
126
+ connection != primary_connection
127
+ end
128
+
129
+ # @return [TrueClass] if there has been a write within the last {#proxy_delay} seconds
130
+ # @return [TrueClass] if sql_string matches a write statement (i.e. INSERT, UPDATE, DELETE, SELECT FOR UPDATE)
131
+ # @return [FalseClass] if sql_string matches a read statement (i.e. SELECT)
132
+ def need_primary?(sql_string)
133
+ return true if recent_write_to_primary?
134
+
135
+ return true if in_transaction?
136
+ return true if SQL_PRIMARY_MATCHERS.any?(&match_sql?(sql_string))
137
+ return false if SQL_REPLICA_MATCHERS.any?(&match_sql?(sql_string))
138
+
139
+ true
140
+ end
141
+
142
+ def need_all?(sql_string)
143
+ return false if SQL_SKIP_ALL_MATCHERS.any?(&match_sql?(sql_string))
144
+
145
+ SQL_ALL_MATCHERS.any?(&match_sql?(sql_string))
146
+ end
147
+
148
+ def write_statement?(sql_string)
149
+ WRITE_STATEMENT_MATCHERS.any?(&match_sql?(sql_string))
150
+ end
151
+
152
+ def match_sql?(sql_string)
153
+ proc { |matcher| matcher.match?(sql_string) }
154
+ end
155
+
156
+ # TODO: implement a context API to re-route requests to the primary database if a recent query was sent to it to
157
+ # avoid replication delay issues
158
+ # @return Boolean
159
+ def recent_write_to_primary?
160
+ Concurrent.monotonic_time - last_write_at < proxy_delay
161
+ end
162
+
163
+ def in_transaction?
164
+ primary_connection.open_transactions.positive?
165
+ end
166
+
167
+ def update_primary_latest_write_timestamp
168
+ @last_write_at = Concurrent.monotonic_time
169
+ end
170
+
171
+ def proxy_delay
172
+ proxy_config.proxy_delay
173
+ end
174
+
175
+ def checkout_timeout
176
+ proxy_config.checkout_timeout
177
+ end
178
+
179
+ def proxy_config
180
+ ActiveRecordProxyAdapters.config
181
+ end
182
+ end
183
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support"
4
+
5
+ module ActiveRecordProxyAdapters
6
+ # Hooks into rails boot process to extend ActiveRecord with the proxy adapter.
7
+ class Railtie < Rails::Railtie
8
+ ActiveSupport.on_load :active_record do
9
+ require "active_record_proxy_adapters/connection_handling"
10
+ ActiveRecord::Base.extend(ActiveRecordProxyAdapters::ConnectionHandling)
11
+ end
12
+
13
+ config.to_prepare do
14
+ Rails.autoloaders.each do |autoloader|
15
+ autoloader.inflector.inflect(
16
+ "postgresql_proxy_adapter" => "PostgreSQLProxyAdapter"
17
+ )
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecordProxyAdapters
4
+ VERSION = "0.1.0.rc2"
5
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record"
4
+ require "active_record_proxy_adapters/version"
5
+ require "active_record_proxy_adapters/configuration"
6
+
7
+ # The gem namespace.
8
+ module ActiveRecordProxyAdapters
9
+ class Error < StandardError; end
10
+
11
+ module_function
12
+
13
+ def configure
14
+ yield(config)
15
+ end
16
+
17
+ def config
18
+ @config ||= Configuration.new
19
+ end
20
+ end
21
+
22
+ require_relative "active_record_proxy_adapters/railtie" if defined?(Rails::Railtie)
@@ -0,0 +1,34 @@
1
+ FROM docker.io/postgres:14-alpine
2
+
3
+ ARG REPLICA_USER=replicator
4
+ ARG REPLICA_PASSWORD=replicator
5
+ ARG REPLICATION_SLOT_NAME=replication_slot
6
+ ARG INIT_SQL=00_init.sql
7
+ ARG POSTGRES_LOGGING_COLLECTOR=
8
+ ARG POSTGRES_LOG_DESTINATION=
9
+ ARG POSTGRES_LOG_STATEMENT=
10
+ ENV CONF_SAMPLE="/usr/local/share/postgresql/postgresql.conf.sample"
11
+
12
+ WORKDIR /docker-entrypoint-initdb.d
13
+
14
+ USER root
15
+
16
+ RUN touch $INIT_SQL
17
+ RUN chown -R postgres:postgres $INIT_SQL
18
+ RUN echo "CREATE USER ${REPLICA_USER} WITH REPLICATION ENCRYPTED PASSWORD '${REPLICA_PASSWORD}';" > $INIT_SQL
19
+ RUN echo "SELECT pg_create_physical_replication_slot('${REPLICATION_SLOT_NAME}');" >> $INIT_SQL
20
+
21
+ # Enable logging collector if given
22
+ RUN if [[ ! -z "${POSTGRES_LOGGING_COLLECTOR}" ]]; then sed -i "s/#\(logging_collector = \)off\(.*\)/\1${POSTGRES_LOGGING_COLLECTOR}\2/" ${CONF_SAMPLE}; fi
23
+
24
+ # Override default log destination if given
25
+ RUN if [[ ! -z "${POSTGRES_LOG_DESTINATION}" ]]; then sed -i "s/#\(log_destination = \)'stderr'\(.*\)/\1'${POSTGRES_LOG_DESTINATION}'\2/" ${CONF_SAMPLE}; fi
26
+
27
+ # Override log statement if given
28
+ RUN if [[ ! -z "${POSTGRES_LOG_STATEMENT}" ]]; then sed -i "s/#\(log_statement = \)'none'\(.*\)/\1'${POSTGRES_LOG_STATEMENT}'\2/" ${CONF_SAMPLE}; fi
29
+
30
+ WORKDIR /
31
+
32
+ USER postgres
33
+
34
+ CMD ["postgres", "-c", "wal_level=replica", "-c", "hot_standby=on", "-c", "max_wal_senders=10", "-c", "max_replication_slots=10", "-c", "hot_standby_feedback=on" ]
@@ -0,0 +1,23 @@
1
+ FROM docker.io/postgres:14-alpine
2
+
3
+ ENV PRIMARY_DATABASE_HOST=localhost
4
+ ENV PRIMARY_DATABASE_PORT=5432
5
+ ENV PRIMARY_REPLICATION_SLOT=replication_slot
6
+
7
+ USER root
8
+ RUN printf '' > cmd.sh
9
+
10
+ RUN echo 'until pg_basebackup --pgdata=/var/lib/postgresql/data -R --slot=$PRIMARY_REPLICATION_SLOT --host=$PRIMARY_DATABASE_HOST --port=$PRIMARY_DATABASE_PORT' >> cmd.sh
11
+ RUN echo 'do' >> cmd.sh
12
+ RUN echo "echo 'Waiting for primary to connect...'" >> cmd.sh
13
+ RUN echo 'sleep 1s' >> cmd.sh
14
+ RUN echo 'done' >> cmd.sh
15
+ RUN echo "echo 'Backup done, starting replica...'" >> cmd.sh
16
+ RUN echo 'chmod 0700 /var/lib/postgresql/data' >> cmd.sh
17
+ RUN echo 'postgres' >> cmd.sh
18
+
19
+ RUN chown -R postgres:postgres cmd.sh
20
+ USER postgres
21
+ RUN chmod u+rwx cmd.sh
22
+
23
+ CMD [ "./cmd.sh" ]
@@ -0,0 +1,4 @@
1
+ module ActiveRecordProxyAdapters
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record_proxy_adapters
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.rc2
5
+ platform: ruby
6
+ authors:
7
+ - Matt Cruz
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-11-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 6.1.a
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '8.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 6.1.a
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '8.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: activesupport
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 6.1.a
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '8.0'
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 6.1.a
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '8.0'
53
+ description: |-
54
+ This gem allows automatic connection switching between a primary and one read replica database in ActiveRecord.
55
+ It pattern matches the SQL statement being sent to decide whether it should go to the replica (SELECT) or the
56
+ primary (INSERT, UPDATE, DELETE).
57
+ email:
58
+ - matt.cruz@nasdaq.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".rspec"
64
+ - ".rubocop.yml"
65
+ - CHANGELOG.md
66
+ - CODE_OF_CONDUCT.md
67
+ - Dockerfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - docker-compose.yml
72
+ - lib/active_record/connection_adapters/postgresql_proxy_adapter.rb
73
+ - lib/active_record/tasks/postgresql_proxy_database_tasks.rb
74
+ - lib/active_record_proxy_adapters.rb
75
+ - lib/active_record_proxy_adapters/active_record_context.rb
76
+ - lib/active_record_proxy_adapters/configuration.rb
77
+ - lib/active_record_proxy_adapters/connection_handling.rb
78
+ - lib/active_record_proxy_adapters/hijackable.rb
79
+ - lib/active_record_proxy_adapters/postgresql_proxy.rb
80
+ - lib/active_record_proxy_adapters/primary_replica_proxy.rb
81
+ - lib/active_record_proxy_adapters/railtie.rb
82
+ - lib/active_record_proxy_adapters/version.rb
83
+ - postgres_primary.dockerfile
84
+ - postgres_replica.dockerfile
85
+ - sig/active_record_proxy_adapters.rbs
86
+ homepage: https://github.com/Nasdaq/active_record_proxy_adapters
87
+ licenses:
88
+ - MIT
89
+ metadata:
90
+ allowed_push_host: https://rubygems.org/
91
+ homepage_uri: https://github.com/Nasdaq/active_record_proxy_adapters
92
+ source_code_uri: https://github.com/Nasdaq/active_record_proxy_adapters
93
+ changelog_uri: https://github.com/Nasdaq/active_record_proxy_adapters/blob/main/CHANGELOG.md
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: 3.1.0
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubygems_version: 3.5.11
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Read replica proxy adapters for ActiveRecord!
113
+ test_files: []