active_record_proxy_adapters 0.1.9 → 0.2.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 +4 -4
- data/.rspec +4 -0
- data/.rubocop.yml +16 -0
- data/CHANGELOG.md +26 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/Dockerfile +18 -0
- data/README.md +0 -2
- data/db/postgresql_structure.sql +76 -0
- data/docker/postgres_replica/cmd.sh +9 -0
- data/docker-compose.yml +78 -0
- data/lib/active_record/connection_adapters/postgresql_proxy_adapter.rb +1 -1
- data/lib/active_record_proxy_adapters/active_record_context.rb +0 -18
- data/lib/active_record_proxy_adapters/configuration.rb +22 -4
- data/lib/active_record_proxy_adapters/log_subscriber.rb +41 -0
- data/lib/active_record_proxy_adapters/postgresql_proxy.rb +1 -1
- data/lib/active_record_proxy_adapters/primary_replica_proxy.rb +10 -73
- data/lib/active_record_proxy_adapters/version.rb +1 -1
- data/postgres_primary.dockerfile +34 -0
- data/postgres_replica.dockerfile +15 -0
- data/sig/active_record_proxy_adapters.rbs +4 -0
- metadata +19 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5312effcf68a3305eb5e7804440112433f5b6837d6b71e7d122145eac4741619
|
4
|
+
data.tar.gz: 50586657daa02e76dfffebbe1faf8df5093198471faf847cec97cd16634c0c17
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 707b80a3618328639126d13f3f76bb8123105019b3e30c75f15c66a66a88d49aa5de4301fe70b25ff5356ef37e54ebff896f4cab0160cadbb79d08ae9cf0dc4e
|
7
|
+
data.tar.gz: 6b84984e33cf87abe9a12f5553bccffc32e2a9563a0c859c488cdae4be4c3a019ffce01242648620c67bb85a16dc61d982f0be8ecba83943f01c718e067743dc
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,16 @@
|
|
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
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
## [Unreleased]
|
2
|
+
|
3
|
+
## [0.2.0] - 2024-12-24
|
4
|
+
|
5
|
+
- Add custom log subscriber to tag queries based on the adapter being used (68b8c1f4191388eb957bf12e0f84289da667e940)
|
6
|
+
|
7
|
+
## [0.1.3] - 2024-12-24
|
8
|
+
|
9
|
+
- Fix replica connection pool getter when database configurations have multiple replicas (ea5a33997da45ac073f166b3fbd2d12426053cd6)
|
10
|
+
- Retrieve replica pool without checking out a connection (6470ef58e851082ae1f7a860ecdb5b451ef903c8)
|
11
|
+
|
12
|
+
## [0.1.2] - 2024-12-16
|
13
|
+
|
14
|
+
- Fix CTE regex matcher (4b1d10bfd952fb1f5b102de8cc1a5bd05d25f5e9)
|
15
|
+
|
16
|
+
## [0.1.1] - 2024-11-27
|
17
|
+
|
18
|
+
- Enable RubyGems MFA (2a71b1f4354fb966cc0aa68231ca5837814e07ee)
|
19
|
+
|
20
|
+
## [0.1.0] - 2024-11-19
|
21
|
+
|
22
|
+
- Add PostgreSQLProxyAdapter (2b3bb9f7359139519b32af3018ceb07fed8c6b33)
|
23
|
+
|
24
|
+
## [0.1.0.rc2] - 2024-10-28
|
25
|
+
|
26
|
+
- Add PostgreSQLProxyAdapter (2b3bb9f7359139519b32af3018ceb07fed8c6b33)
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -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/README.md
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
# ActiveRecordProxyAdapters
|
2
2
|
|
3
|
-
[](https://github.com/Nasdaq/active_record_proxy_adapters/actions/workflows/test.yml)
|
4
|
-
|
5
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.
|
6
4
|
|
7
5
|
## Installation
|
@@ -0,0 +1,76 @@
|
|
1
|
+
SET statement_timeout = 0;
|
2
|
+
SET lock_timeout = 0;
|
3
|
+
SET idle_in_transaction_session_timeout = 0;
|
4
|
+
SET client_encoding = 'UTF8';
|
5
|
+
SET standard_conforming_strings = on;
|
6
|
+
SELECT pg_catalog.set_config('search_path', '', false);
|
7
|
+
SET check_function_bodies = false;
|
8
|
+
SET xmloption = content;
|
9
|
+
SET client_min_messages = warning;
|
10
|
+
SET row_security = off;
|
11
|
+
|
12
|
+
--
|
13
|
+
-- Name: public; Type: SCHEMA; Schema: -; Owner: -
|
14
|
+
--
|
15
|
+
|
16
|
+
-- *not* creating schema, since initdb creates it
|
17
|
+
|
18
|
+
|
19
|
+
SET default_tablespace = '';
|
20
|
+
|
21
|
+
SET default_table_access_method = heap;
|
22
|
+
|
23
|
+
--
|
24
|
+
-- Name: users; Type: TABLE; Schema: public; Owner: -
|
25
|
+
--
|
26
|
+
|
27
|
+
CREATE TABLE public.users (
|
28
|
+
id integer NOT NULL,
|
29
|
+
name text NOT NULL,
|
30
|
+
email text NOT NULL,
|
31
|
+
created_at timestamp without time zone DEFAULT now() NOT NULL,
|
32
|
+
updated_at timestamp without time zone DEFAULT now() NOT NULL
|
33
|
+
);
|
34
|
+
|
35
|
+
|
36
|
+
--
|
37
|
+
-- Name: users_id_seq; Type: SEQUENCE; Schema: public; Owner: -
|
38
|
+
--
|
39
|
+
|
40
|
+
CREATE SEQUENCE public.users_id_seq
|
41
|
+
AS integer
|
42
|
+
START WITH 1
|
43
|
+
INCREMENT BY 1
|
44
|
+
NO MINVALUE
|
45
|
+
NO MAXVALUE
|
46
|
+
CACHE 1;
|
47
|
+
|
48
|
+
|
49
|
+
--
|
50
|
+
-- Name: users_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
|
51
|
+
--
|
52
|
+
|
53
|
+
ALTER SEQUENCE public.users_id_seq OWNED BY public.users.id;
|
54
|
+
|
55
|
+
|
56
|
+
--
|
57
|
+
-- Name: users id; Type: DEFAULT; Schema: public; Owner: -
|
58
|
+
--
|
59
|
+
|
60
|
+
ALTER TABLE ONLY public.users ALTER COLUMN id SET DEFAULT nextval('public.users_id_seq'::regclass);
|
61
|
+
|
62
|
+
|
63
|
+
--
|
64
|
+
-- Name: users users_pkey; Type: CONSTRAINT; Schema: public; Owner: -
|
65
|
+
--
|
66
|
+
|
67
|
+
ALTER TABLE ONLY public.users
|
68
|
+
ADD CONSTRAINT users_pkey PRIMARY KEY (id);
|
69
|
+
|
70
|
+
|
71
|
+
--
|
72
|
+
-- PostgreSQL database dump complete
|
73
|
+
--
|
74
|
+
|
75
|
+
SET search_path TO "$user", public;
|
76
|
+
|
@@ -0,0 +1,9 @@
|
|
1
|
+
until pg_basebackup --pgdata=/var/lib/postgresql/data -R --slot=$PRIMARY_REPLICATION_SLOT --host=$PRIMARY_DATABASE_HOST --port=$PRIMARY_DATABASE_PORT
|
2
|
+
do
|
3
|
+
echo 'Waiting for primary to connect...'
|
4
|
+
sleep 1s
|
5
|
+
done
|
6
|
+
|
7
|
+
echo 'Backup done, starting replica...'
|
8
|
+
chmod 0700 /var/lib/postgresql/data
|
9
|
+
postgres
|
data/docker-compose.yml
ADDED
@@ -0,0 +1,78 @@
|
|
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.3.6}
|
19
|
+
- RAILS_VERSION=${RAILS_VERSION:-8.0.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
|
+
container_name: postgres_primary
|
54
|
+
environment:
|
55
|
+
POSTGRES_DB: postgres
|
56
|
+
POSTGRES_USER: postgres_primary_test
|
57
|
+
POSTGRES_PASSWORD: postgres_primary_test
|
58
|
+
POSTGRES_HOST_AUTH_METHOD: "scram-sha-256\nhost replication all 0.0.0.0/0 md5"
|
59
|
+
POSTGRES_INITDB_ARGS: "--auth-host=scram-sha-256"
|
60
|
+
|
61
|
+
postgres_replica:
|
62
|
+
<<: *postgres-common
|
63
|
+
build:
|
64
|
+
context: .
|
65
|
+
dockerfile: postgres_replica.dockerfile
|
66
|
+
container_name: postgres_replica
|
67
|
+
environment:
|
68
|
+
PGUSER: replicator
|
69
|
+
PGPASSWORD: replicator
|
70
|
+
PRIMARY_DATABASE_HOST: postgres_primary
|
71
|
+
depends_on:
|
72
|
+
- postgres_primary
|
73
|
+
networks:
|
74
|
+
app:
|
75
|
+
postgres:
|
76
|
+
|
77
|
+
volumes:
|
78
|
+
postgres_primary:
|
@@ -15,7 +15,7 @@ module ActiveRecord
|
|
15
15
|
|
16
16
|
ADAPTER_NAME = "PostgreSQLProxy"
|
17
17
|
|
18
|
-
delegate_to_proxy
|
18
|
+
delegate_to_proxy :execute, :exec_query
|
19
19
|
|
20
20
|
unless ActiveRecordProxyAdapters::ActiveRecordContext.active_record_v8_0_or_greater?
|
21
21
|
delegate_to_proxy :exec_no_cache, :exec_cache
|
@@ -23,8 +23,6 @@ module ActiveRecordProxyAdapters
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def connection_class_for(connection)
|
26
|
-
return connection.connection_descriptor.name.constantize if active_record_v8_0_2_or_greater?
|
27
|
-
|
28
26
|
connection.connection_class || ActiveRecord::Base
|
29
27
|
end
|
30
28
|
|
@@ -35,18 +33,6 @@ module ActiveRecordProxyAdapters
|
|
35
33
|
ActiveRecord
|
36
34
|
end
|
37
35
|
|
38
|
-
def hijackable_methods
|
39
|
-
hijackable = %i[execute exec_query]
|
40
|
-
|
41
|
-
hijackable << :internal_exec_query if active_record_v8_0_or_greater?
|
42
|
-
|
43
|
-
hijackable
|
44
|
-
end
|
45
|
-
|
46
|
-
def active_record_v7?
|
47
|
-
active_record_version >= Gem::Version.new("7.0") && active_record_version < Gem::Version.new("8.0")
|
48
|
-
end
|
49
|
-
|
50
36
|
def active_record_v7_1_or_greater?
|
51
37
|
active_record_version >= Gem::Version.new("7.1")
|
52
38
|
end
|
@@ -58,9 +44,5 @@ module ActiveRecordProxyAdapters
|
|
58
44
|
def active_record_v8_0_or_greater?
|
59
45
|
active_record_version >= Gem::Version.new("8.0")
|
60
46
|
end
|
61
|
-
|
62
|
-
def active_record_v8_0_2_or_greater?
|
63
|
-
active_record_version >= Gem::Version.new("8.0.2")
|
64
|
-
end
|
65
47
|
end
|
66
48
|
end
|
@@ -5,8 +5,10 @@ require "active_support/core_ext/integer/time"
|
|
5
5
|
module ActiveRecordProxyAdapters
|
6
6
|
# Provides a global configuration object to configure how the proxy should behave.
|
7
7
|
class Configuration
|
8
|
-
PROXY_DELAY
|
9
|
-
CHECKOUT_TIMEOUT
|
8
|
+
PROXY_DELAY = 2.seconds.freeze
|
9
|
+
CHECKOUT_TIMEOUT = 2.seconds.freeze
|
10
|
+
LOG_SUBSCRIBER_PRIMARY_PREFIX = proc { |event| "#{event.payload[:connection].class::ADAPTER_NAME} Primary" }.freeze
|
11
|
+
LOG_SUBSCRIBER_REPLICA_PREFIX = proc { |event| "#{event.payload[:connection].class::ADAPTER_NAME} Replica" }.freeze
|
10
12
|
|
11
13
|
# @return [ActiveSupport::Duration] How long the proxy should reroute all read requests to the primary database
|
12
14
|
# since the latest write. Defaults to PROXY_DELAY.
|
@@ -15,9 +17,25 @@ module ActiveRecordProxyAdapters
|
|
15
17
|
# Defaults to CHECKOUT_TIMEOUT.
|
16
18
|
attr_accessor :checkout_timeout
|
17
19
|
|
20
|
+
# @return [Proc] Prefix for the log subscriber when the primary database is used.
|
21
|
+
attr_reader :log_subscriber_primary_prefix
|
22
|
+
|
23
|
+
# @return [Proc] Prefix for the log subscriber when the replica database is used.
|
24
|
+
attr_reader :log_subscriber_replica_prefix
|
25
|
+
|
18
26
|
def initialize
|
19
|
-
self.proxy_delay
|
20
|
-
self.checkout_timeout
|
27
|
+
self.proxy_delay = PROXY_DELAY
|
28
|
+
self.checkout_timeout = CHECKOUT_TIMEOUT
|
29
|
+
self.log_subscriber_primary_prefix = LOG_SUBSCRIBER_PRIMARY_PREFIX
|
30
|
+
self.log_subscriber_replica_prefix = LOG_SUBSCRIBER_REPLICA_PREFIX
|
31
|
+
end
|
32
|
+
|
33
|
+
def log_subscriber_primary_prefix=(prefix)
|
34
|
+
@log_subscriber_primary_prefix = prefix.is_a?(Proc) ? prefix : proc { prefix.to_s }
|
35
|
+
end
|
36
|
+
|
37
|
+
def log_subscriber_replica_prefix=(prefix)
|
38
|
+
@log_subscriber_replica_prefix = prefix.is_a?(Proc) ? prefix : proc { prefix.to_s }
|
21
39
|
end
|
22
40
|
end
|
23
41
|
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecordProxyAdapters
|
4
|
+
class LogSubscriber < ActiveRecord::LogSubscriber # rubocop:disable Style/Documentation
|
5
|
+
attach_to :active_record
|
6
|
+
|
7
|
+
IGNORE_PAYLOAD_NAMES = %w[SCHEMA EXPLAIN].freeze
|
8
|
+
|
9
|
+
def sql(event)
|
10
|
+
payload = event.payload
|
11
|
+
name = payload[:name]
|
12
|
+
unless IGNORE_PAYLOAD_NAMES.include?(name)
|
13
|
+
name = [database_instance_prefix_for(event), name].compact.join(" ")
|
14
|
+
payload[:name] = name
|
15
|
+
end
|
16
|
+
super
|
17
|
+
end
|
18
|
+
|
19
|
+
protected
|
20
|
+
|
21
|
+
def database_instance_prefix_for(event)
|
22
|
+
connection = event.payload[:connection]
|
23
|
+
config = connection.instance_variable_get(:@config)
|
24
|
+
prefix = if config[:replica] || config["replica"]
|
25
|
+
log_subscriber_replica_prefix
|
26
|
+
else
|
27
|
+
log_subscriber_primary_prefix
|
28
|
+
end
|
29
|
+
|
30
|
+
"[#{prefix.call(event)}]"
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
delegate :log_subscriber_primary_prefix, :log_subscriber_replica_prefix, to: :config
|
36
|
+
|
37
|
+
def config
|
38
|
+
ActiveRecordProxyAdapters.config
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -7,7 +7,7 @@ module ActiveRecordProxyAdapters
|
|
7
7
|
# Proxy to the original PostgreSQLAdapter, allowing the use of the ActiveRecordProxyAdapters::PrimaryReplicaProxy.
|
8
8
|
class PostgreSQLProxy < PrimaryReplicaProxy
|
9
9
|
# ActiveRecord::PostgreSQLAdapter methods that should be proxied.
|
10
|
-
hijack_method
|
10
|
+
hijack_method :execute, :exec_query
|
11
11
|
|
12
12
|
hijack_method :exec_no_cache, :exec_cache unless ActiveRecordContext.active_record_v8_0_or_greater?
|
13
13
|
end
|
@@ -15,29 +15,17 @@ module ActiveRecordProxyAdapters
|
|
15
15
|
/\A\s*select.+for update\Z/i, /select.+lock in share mode\Z/i,
|
16
16
|
/\A\s*select.+(nextval|currval|lastval|get_lock|release_lock|pg_advisory_lock|pg_advisory_unlock)\(/i
|
17
17
|
].map(&:freeze).freeze
|
18
|
-
|
19
|
-
CTE_MATCHER = /\A\s*WITH\s+(?<CTE>\S+\s+AS\s+\(\s?[\s\S]*\))/i
|
20
18
|
# All queries that match these patterns should be sent to the replica database
|
21
|
-
SQL_REPLICA_MATCHERS
|
22
|
-
/\A\s*(select)\s/i,
|
23
|
-
/#{CTE_MATCHER.source}\s*select/i
|
24
|
-
].map(&:freeze).freeze
|
19
|
+
SQL_REPLICA_MATCHERS = [/\A\s*(select|with\s[\s\S]*\)\s*select)\s/i].map(&:freeze).freeze
|
25
20
|
# All queries that match these patterns should be sent to all databases
|
26
21
|
SQL_ALL_MATCHERS = [/\A\s*set\s/i].map(&:freeze).freeze
|
27
22
|
# Local sets queries should not be sent to all datbases
|
28
23
|
SQL_SKIP_ALL_MATCHERS = [/\A\s*set\s+local\s/i].map(&:freeze).freeze
|
29
24
|
# These patterns define which database statments are considered write statments, so we can shortly re-route all
|
30
25
|
# requests to the primary database so the replica has time to replicate
|
31
|
-
WRITE_STATEMENT_MATCHERS = [
|
32
|
-
|
33
|
-
|
34
|
-
/INSERT\s[\s\S]*INTO\s[\s\S]*/i,
|
35
|
-
/UPDATE\s[\s\S]*/i,
|
36
|
-
/DELETE\s[\s\S]*FROM\s[\s\S]*/i,
|
37
|
-
/DROP\s/i
|
38
|
-
].map(&:freeze).freeze
|
39
|
-
|
40
|
-
UNPROXIED_METHOD_SUFFIX = "_unproxied"
|
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"
|
41
29
|
|
42
30
|
# Defines which methods should be hijacked from the original adapter and use the proxy
|
43
31
|
# @param method_names [Array<Symbol>] the list of method names from the adapter
|
@@ -74,42 +62,17 @@ module ActiveRecordProxyAdapters
|
|
74
62
|
|
75
63
|
attr_reader :primary_connection, :last_write_at, :active_record_context
|
76
64
|
|
77
|
-
delegate :connection_handler, to: :connection_class
|
65
|
+
delegate :connection_handler, :connected_to_stack, to: :connection_class
|
78
66
|
delegate :reading_role, :writing_role, to: :active_record_context
|
79
67
|
|
80
|
-
# We need to call .verify! to ensure `configure_connection` is called on the instance before attempting to use it.
|
81
|
-
# This is necessary because the connection may have been lazily initialized and is an unintended side effect from a
|
82
|
-
# change in Rails to defer connection verification: https://github.com/rails/rails/pull/44576
|
83
|
-
# verify! cannot be called before the object is initialized and because of how the proxy hooks into the connection
|
84
|
-
# instance, it has to be done lazily (hence the memoization). Ideally, we shouldn't have to worry about this at all
|
85
|
-
# But there is tight coupling between methods in ActiveRecord::ConnectionAdapters::AbstractAdapter and
|
86
|
-
# its descendants which will require significant refactoring to be decoupled.
|
87
|
-
# See https://github.com/rails/rails/issues/51780
|
88
|
-
def verified_primary_connection
|
89
|
-
@verified_primary_connection ||= begin
|
90
|
-
connected_to(role: writing_role) { primary_connection.verify! }
|
91
|
-
|
92
|
-
primary_connection
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
68
|
def replica_pool_unavailable?
|
97
69
|
!replica_pool
|
98
70
|
end
|
99
71
|
|
100
72
|
def replica_pool
|
101
|
-
# use default handler if the connection pool for specific class is not found
|
102
|
-
specific_replica_pool || default_replica_pool
|
103
|
-
end
|
104
|
-
|
105
|
-
def specific_replica_pool
|
106
73
|
connection_handler.retrieve_connection_pool(connection_class.name, role: reading_role)
|
107
74
|
end
|
108
75
|
|
109
|
-
def default_replica_pool
|
110
|
-
connection_handler.retrieve_connection_pool(ActiveRecord::Base.name, role: reading_role)
|
111
|
-
end
|
112
|
-
|
113
76
|
def connection_class
|
114
77
|
active_record_context.connection_class_for(primary_connection)
|
115
78
|
end
|
@@ -148,26 +111,11 @@ module ActiveRecordProxyAdapters
|
|
148
111
|
[reading_role, writing_role].include?(role) ? role : nil
|
149
112
|
end
|
150
113
|
|
151
|
-
def connected_to_stack
|
152
|
-
return connection_class.connected_to_stack if connection_class.respond_to?(:connected_to_stack)
|
153
|
-
|
154
|
-
# handle Rails 7.2+ pending migrations Connection
|
155
|
-
return [{ role: writing_role }] if pending_migration_connection?
|
156
|
-
|
157
|
-
[]
|
158
|
-
end
|
159
|
-
|
160
|
-
def pending_migration_connection?
|
161
|
-
active_record_context.active_record_v7_1_or_greater? &&
|
162
|
-
connection_class.name == "ActiveRecord::PendingMigrationConnection"
|
163
|
-
end
|
164
|
-
|
165
114
|
def connection_for(role, sql_string)
|
166
|
-
connection =
|
167
|
-
|
115
|
+
connection = primary_connection if role == writing_role || replica_pool_unavailable?
|
168
116
|
connection ||= checkout_replica_connection
|
169
117
|
|
170
|
-
result =
|
118
|
+
result = yield(connection)
|
171
119
|
|
172
120
|
update_primary_latest_write_timestamp if !replica_connection?(connection) && write_statement?(sql_string)
|
173
121
|
|
@@ -176,12 +124,6 @@ module ActiveRecordProxyAdapters
|
|
176
124
|
replica_connection?(connection) && replica_pool.checkin(connection)
|
177
125
|
end
|
178
126
|
|
179
|
-
def connected_to(role:, &block)
|
180
|
-
return block.call unless connection_class.respond_to?(:connected_to)
|
181
|
-
|
182
|
-
connection_class.connected_to(role:, &block)
|
183
|
-
end
|
184
|
-
|
185
127
|
def replica_connection?(connection)
|
186
128
|
connection && connection != primary_connection
|
187
129
|
end
|
@@ -198,21 +140,16 @@ module ActiveRecordProxyAdapters
|
|
198
140
|
# @return [TrueClass] if there has been a write within the last {#proxy_delay} seconds
|
199
141
|
# @return [TrueClass] if sql_string matches a write statement (i.e. INSERT, UPDATE, DELETE, SELECT FOR UPDATE)
|
200
142
|
# @return [FalseClass] if sql_string matches a read statement (i.e. SELECT)
|
201
|
-
def need_primary?(sql_string)
|
202
|
-
return true
|
143
|
+
def need_primary?(sql_string)
|
144
|
+
return true if recent_write_to_primary?
|
145
|
+
|
203
146
|
return true if in_transaction?
|
204
|
-
return true if cte_for_write?(sql_string)
|
205
147
|
return true if SQL_PRIMARY_MATCHERS.any?(&match_sql?(sql_string))
|
206
148
|
return false if SQL_REPLICA_MATCHERS.any?(&match_sql?(sql_string))
|
207
149
|
|
208
150
|
true
|
209
151
|
end
|
210
152
|
|
211
|
-
def cte_for_write?(sql_string)
|
212
|
-
CTE_MATCHER.match?(sql_string) &&
|
213
|
-
WRITE_STATEMENT_MATCHERS.any?(&match_sql?(sql_string))
|
214
|
-
end
|
215
|
-
|
216
153
|
def need_all?(sql_string)
|
217
154
|
return false if SQL_SKIP_ALL_MATCHERS.any?(&match_sql?(sql_string))
|
218
155
|
|
@@ -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,15 @@
|
|
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
|
+
|
8
|
+
COPY docker/postgres_replica/cmd.sh cmd.sh
|
9
|
+
|
10
|
+
USER root
|
11
|
+
RUN chown -R postgres:postgres cmd.sh
|
12
|
+
USER postgres
|
13
|
+
RUN chmod u+x cmd.sh
|
14
|
+
|
15
|
+
CMD [ "./cmd.sh" ]
|
metadata
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_proxy_adapters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Cruz
|
8
|
+
autorequire:
|
8
9
|
bindir: exe
|
9
10
|
cert_chain: []
|
10
|
-
date:
|
11
|
+
date: 2024-12-24 00:00:00.000000000 Z
|
11
12
|
dependencies:
|
12
13
|
- !ruby/object:Gem::Dependency
|
13
14
|
name: activerecord
|
@@ -57,12 +58,18 @@ email:
|
|
57
58
|
- matt.cruz@nasdaq.com
|
58
59
|
executables: []
|
59
60
|
extensions: []
|
60
|
-
extra_rdoc_files:
|
61
|
-
- README.md
|
62
|
-
- LICENSE.txt
|
61
|
+
extra_rdoc_files: []
|
63
62
|
files:
|
63
|
+
- ".rspec"
|
64
|
+
- ".rubocop.yml"
|
65
|
+
- CHANGELOG.md
|
66
|
+
- CODE_OF_CONDUCT.md
|
67
|
+
- Dockerfile
|
64
68
|
- LICENSE.txt
|
65
69
|
- README.md
|
70
|
+
- db/postgresql_structure.sql
|
71
|
+
- docker-compose.yml
|
72
|
+
- docker/postgres_replica/cmd.sh
|
66
73
|
- lib/active_record/connection_adapters/postgresql_proxy_adapter.rb
|
67
74
|
- lib/active_record/tasks/postgresql_proxy_database_tasks.rb
|
68
75
|
- lib/active_record_proxy_adapters.rb
|
@@ -70,10 +77,14 @@ files:
|
|
70
77
|
- lib/active_record_proxy_adapters/configuration.rb
|
71
78
|
- lib/active_record_proxy_adapters/connection_handling.rb
|
72
79
|
- lib/active_record_proxy_adapters/hijackable.rb
|
80
|
+
- lib/active_record_proxy_adapters/log_subscriber.rb
|
73
81
|
- lib/active_record_proxy_adapters/postgresql_proxy.rb
|
74
82
|
- lib/active_record_proxy_adapters/primary_replica_proxy.rb
|
75
83
|
- lib/active_record_proxy_adapters/railtie.rb
|
76
84
|
- lib/active_record_proxy_adapters/version.rb
|
85
|
+
- postgres_primary.dockerfile
|
86
|
+
- postgres_replica.dockerfile
|
87
|
+
- sig/active_record_proxy_adapters.rbs
|
77
88
|
homepage: https://github.com/Nasdaq/active_record_proxy_adapters
|
78
89
|
licenses:
|
79
90
|
- MIT
|
@@ -83,6 +94,7 @@ metadata:
|
|
83
94
|
source_code_uri: https://github.com/Nasdaq/active_record_proxy_adapters
|
84
95
|
changelog_uri: https://github.com/Nasdaq/active_record_proxy_adapters/blob/main/CHANGELOG.md
|
85
96
|
rubygems_mfa_required: 'true'
|
97
|
+
post_install_message:
|
86
98
|
rdoc_options: []
|
87
99
|
require_paths:
|
88
100
|
- lib
|
@@ -97,7 +109,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
109
|
- !ruby/object:Gem::Version
|
98
110
|
version: '0'
|
99
111
|
requirements: []
|
100
|
-
rubygems_version: 3.
|
112
|
+
rubygems_version: 3.5.11
|
113
|
+
signing_key:
|
101
114
|
specification_version: 4
|
102
115
|
summary: Read replica proxy adapters for ActiveRecord!
|
103
116
|
test_files: []
|