makara 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/gem-publish-public.yml +36 -0
- data/.travis.yml +36 -10
- data/CHANGELOG.md +64 -45
- data/Gemfile +1 -1
- data/README.md +6 -8
- data/gemfiles/ar-head.gemfile +1 -1
- data/gemfiles/ar52.gemfile +24 -0
- data/gemfiles/ar60.gemfile +24 -0
- data/lib/active_record/connection_adapters/makara_abstract_adapter.rb +106 -0
- data/lib/makara/config_parser.rb +2 -1
- data/lib/makara/connection_wrapper.rb +24 -0
- data/lib/makara/errors/blacklisted_while_in_transaction.rb +14 -0
- data/lib/makara/errors/invalid_shard.rb +16 -0
- data/lib/makara/pool.rb +47 -24
- data/lib/makara/proxy.rb +32 -4
- data/lib/makara/railtie.rb +0 -6
- data/lib/makara/strategies/shard_aware.rb +47 -0
- data/lib/makara/version.rb +1 -1
- data/lib/makara.rb +14 -4
- data/makara.gemspec +5 -1
- data/spec/active_record/connection_adapters/makara_postgresql_adapter_spec.rb +57 -0
- data/spec/cookie_spec.rb +3 -3
- data/spec/middleware_spec.rb +1 -1
- data/spec/pool_spec.rb +24 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/strategies/shard_aware_spec.rb +219 -0
- data/spec/support/mock_objects.rb +4 -0
- metadata +16 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0dae82f5608272a2e365b3f75561d7e0359a646101a54d769d854eb1437c49ef
|
4
|
+
data.tar.gz: b9de62fb9f9ef35029c71b32038ca21c05dd3922a4a6b1ed5ff86578fe5e30ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ba51b69a62b36132cdc4654e2207e2b46c0414386111bd3c9a31d8d7ce9960d51cd662f59b6c4fc2194b677c658c7aaff3d85930a3dbbc4cfe289c261d31311d
|
7
|
+
data.tar.gz: c0532643db2b517ce248dcead76a01092b9647350671c8794f6b2c19f257fce681579b278ab2c318735a9a6b7f84ebc9aa59ba897a36641645ebd631f1471868
|
@@ -0,0 +1,36 @@
|
|
1
|
+
name: Publish Public Gem
|
2
|
+
|
3
|
+
on:
|
4
|
+
release:
|
5
|
+
types:
|
6
|
+
- published
|
7
|
+
|
8
|
+
jobs:
|
9
|
+
build:
|
10
|
+
name: Build + Publish
|
11
|
+
runs-on: ubuntu-latest
|
12
|
+
|
13
|
+
steps:
|
14
|
+
- uses: actions/checkout@v2
|
15
|
+
- uses: actions/setup-ruby@v1
|
16
|
+
- name: Build
|
17
|
+
run: |
|
18
|
+
gem build *.gemspec
|
19
|
+
|
20
|
+
- name: Publish to Github
|
21
|
+
run: |
|
22
|
+
mkdir -p $HOME/.gem
|
23
|
+
touch $HOME/.gem/credentials
|
24
|
+
chmod 0600 $HOME/.gem/credentials
|
25
|
+
printf -- "---\n:github: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
|
26
|
+
gem push --verbose --key github --host https://rubygems.pkg.github.com/${OWNER} *.gem
|
27
|
+
env:
|
28
|
+
GEM_HOST_API_KEY: "Bearer ${{secrets.GITHUB_TOKEN}}"
|
29
|
+
OWNER: ${{ github.repository_owner }}
|
30
|
+
continue-on-error: true
|
31
|
+
|
32
|
+
- name: Publish to RubyGems
|
33
|
+
run: |
|
34
|
+
gem push --verbose *.gem
|
35
|
+
env:
|
36
|
+
GEM_HOST_API_KEY: ${{secrets.RUBYGEMS_TOKEN}}
|
data/.travis.yml
CHANGED
@@ -1,12 +1,18 @@
|
|
1
1
|
sudo: false
|
2
2
|
language: ruby
|
3
3
|
cache: bundler
|
4
|
-
dist:
|
4
|
+
dist: xenial
|
5
5
|
|
6
6
|
services:
|
7
7
|
- mysql
|
8
8
|
- postgresql
|
9
9
|
|
10
|
+
addons:
|
11
|
+
postgresql: 10
|
12
|
+
apt:
|
13
|
+
packages:
|
14
|
+
- postgresql-10-postgis-2.4
|
15
|
+
|
10
16
|
before_install:
|
11
17
|
- gem install bundler
|
12
18
|
|
@@ -16,24 +22,28 @@ before_script:
|
|
16
22
|
- psql -c 'create extension postgis;' -U postgres
|
17
23
|
|
18
24
|
rvm:
|
19
|
-
- 2.0
|
20
|
-
- 2.1
|
21
|
-
- 2.2
|
25
|
+
# - 2.0
|
26
|
+
# - 2.1
|
27
|
+
# - 2.2
|
22
28
|
- 2.3
|
23
29
|
- 2.4
|
24
30
|
- 2.5.0
|
31
|
+
- 2.6.0
|
32
|
+
- 2.7.0
|
25
33
|
- ruby-head
|
26
34
|
- jruby
|
27
35
|
|
28
36
|
gemfile:
|
29
|
-
- gemfiles/ar30.gemfile
|
30
|
-
- gemfiles/ar31.gemfile
|
31
|
-
- gemfiles/ar32.gemfile
|
32
|
-
- gemfiles/ar40.gemfile
|
33
|
-
- gemfiles/ar41.gemfile
|
34
|
-
- gemfiles/ar42.gemfile
|
37
|
+
# - gemfiles/ar30.gemfile
|
38
|
+
# - gemfiles/ar31.gemfile
|
39
|
+
# - gemfiles/ar32.gemfile
|
40
|
+
# - gemfiles/ar40.gemfile
|
41
|
+
# - gemfiles/ar41.gemfile
|
42
|
+
# - gemfiles/ar42.gemfile
|
35
43
|
- gemfiles/ar50.gemfile
|
36
44
|
- gemfiles/ar51.gemfile
|
45
|
+
- gemfiles/ar52.gemfile
|
46
|
+
- gemfiles/ar60.gemfile
|
37
47
|
- gemfiles/ar-head.gemfile
|
38
48
|
|
39
49
|
env:
|
@@ -88,6 +98,22 @@ matrix:
|
|
88
98
|
rvm: 2.1
|
89
99
|
- gemfile: gemfiles/ar51.gemfile
|
90
100
|
rvm: 2.2
|
101
|
+
- gemfile: gemfiles/ar52.gemfile
|
102
|
+
rvm: 2.0
|
103
|
+
- gemfile: gemfiles/ar52.gemfile
|
104
|
+
rvm: 2.1
|
105
|
+
- gemfile: gemfiles/ar52.gemfile
|
106
|
+
rvm: 2.2
|
107
|
+
- gemfile: gemfiles/ar60.gemfile
|
108
|
+
rvm: 2.0
|
109
|
+
- gemfile: gemfiles/ar60.gemfile
|
110
|
+
rvm: 2.1
|
111
|
+
- gemfile: gemfiles/ar60.gemfile
|
112
|
+
rvm: 2.2
|
113
|
+
- gemfile: gemfiles/ar60.gemfile
|
114
|
+
rvm: 2.3
|
115
|
+
- gemfile: gemfiles/ar60.gemfile
|
116
|
+
rvm: 2.4
|
91
117
|
- gemfile: gemfiles/ar-head.gemfile
|
92
118
|
rvm: 2.0
|
93
119
|
- gemfile: gemfiles/ar-head.gemfile
|
data/CHANGELOG.md
CHANGED
@@ -1,103 +1,122 @@
|
|
1
1
|
# Change Log
|
2
2
|
All notable changes to this project will be documented in this file.
|
3
3
|
|
4
|
+
## v0.5.0 - 2021-01-08
|
5
|
+
[Full Changelog](https://github.com/instacart/makara/compare/v0.4.1...v0.4.2)
|
6
|
+
- Replace deprecated URI.unescape with CGI.unescape [#252](https://github.com/instacart/makara/pull/252) Kevin Robatel
|
7
|
+
- Override equality operator for ActiveRecord connection wrapper [#269](https://github.com/instacart/makara/pull/269) Praveen Burgu
|
8
|
+
- Handle blacklisted connections in master pool while in transaction [#267] (https://github.com/instacart/makara/pull/267) Praveen Burgu
|
9
|
+
- Handle ActiveRecord connection pools correctly [#267] (https://github.com/instacart/makara/pull/267) Praveen Burgu
|
10
|
+
- Add preliminary support for sharded databases [#267] (https://github.com/instacart/makara/pull/267) Praveen Burgu
|
11
|
+
- Fix ActiveRecord connection pool exhaustion [#268] (https://github.com/instacart/makara/pull/268) Praveen Burgu
|
12
|
+
- Drop support for Ruby 2.0, 2.1 and 2.2 [#267] (https://github.com/instacart/makara/pull/267) Praveen Burgu
|
13
|
+
- Drop support ActiveRecord 3.x and 4.x [#267] (https://github.com/instacart/makara/pull/267) Praveen Burgu
|
14
|
+
- Set up automatic publishing to Github and Rubygems [#275](https://github.com/instacart/makara/pull/275) Matt Larraz
|
15
|
+
|
16
|
+
|
17
|
+
## v0.4.1 - 2019-03-25
|
18
|
+
[Full Changelog](https://github.com/instacart/makara/compare/v0.4.0...v0.4.1)
|
19
|
+
|
20
|
+
- Fix crash by requiring makara in the adapter [#54](https://github.com/instacart/makara/pull/54) Eric Saxby
|
21
|
+
- Add connection logging in non-Rails enviroments [#223](https://github.com/instacart/makara/pull/223) Andrew Kane
|
22
|
+
|
4
23
|
## v0.4.0 - 2018-04-01
|
5
|
-
[Full Changelog](https://github.com/
|
24
|
+
[Full Changelog](https://github.com/instacart/makara/compare/v0.3.10...v0.4.0)
|
6
25
|
|
7
26
|
This release is a major change to how we remember state between requests. A redis store is no longer needed. Everything is in the cookies.
|
8
|
-
- Implement stickiness for the duration of `master_ttl` via cookies [#194](https://github.com/
|
27
|
+
- Implement stickiness for the duration of `master_ttl` via cookies [#194](https://github.com/instacart/makara/pull/194) Rosa Gutierrez
|
9
28
|
|
10
29
|
|
11
30
|
## v0.3.10 - 2018-03-20
|
12
|
-
[Full Changelog](https://github.com/
|
31
|
+
[Full Changelog](https://github.com/instacart/makara/compare/v0.3.9...v0.3.10)
|
13
32
|
|
14
33
|
Fixed
|
15
|
-
- Send nextval queries to master and show queries to replicas for Postgres [#173](https://github.com/
|
16
|
-
- Fixes can't add a new key into hash during iteration error [#174](https://github.com/
|
17
|
-
- Fix: an application freezes when a slave is down [#180](https://github.com/
|
18
|
-
- Allow SELECTs that use common table expressions to go to replicas [#184](https://github.com/
|
19
|
-
- Send advisory lock requests to the master [#198](https://github.com/
|
20
|
-
- Postgres exists query [#199](https://github.com/
|
34
|
+
- Send nextval queries to master and show queries to replicas for Postgres [#173](https://github.com/instacart/makara/pull/173) Andrew Kane
|
35
|
+
- Fixes can't add a new key into hash during iteration error [#174](https://github.com/instacart/makara/pull/174) Andrew Kane
|
36
|
+
- Fix: an application freezes when a slave is down [#180](https://github.com/instacart/makara/pull/180) Alexey P
|
37
|
+
- Allow SELECTs that use common table expressions to go to replicas [#184](https://github.com/instacart/makara/pull/184) Andrew Kane
|
38
|
+
- Send advisory lock requests to the master [#198](https://github.com/instacart/makara/pull/198) George Claghorn
|
39
|
+
- Postgres exists query [#199](https://github.com/instacart/makara/pull/199) Brian Leonard
|
21
40
|
|
22
41
|
Documentation and Test
|
23
|
-
- Clarify README's "What goes where" [#187](https://github.com/
|
24
|
-
- Fix loading fixtures in Rails 5.2 [#192](https://github.com/
|
25
|
-
- Travis Upgrade [#199](https://github.com/
|
42
|
+
- Clarify README's "What goes where" [#187](https://github.com/instacart/makara/pull/187) Jan Sandbrink
|
43
|
+
- Fix loading fixtures in Rails 5.2 [#192](https://github.com/instacart/makara/pull/192) George Claghorn
|
44
|
+
- Travis Upgrade [#199](https://github.com/instacart/makara/pull/199) Brian Leonard
|
26
45
|
|
27
46
|
## v0.3.9 - 2017-08-14
|
28
|
-
[Full Changelog](https://github.com/
|
47
|
+
[Full Changelog](https://github.com/instacart/makara/compare/v0.3.8...v0.3.9)
|
29
48
|
|
30
49
|
Changed
|
31
|
-
- Add postgis support [#118](https://github.com/
|
50
|
+
- Add postgis support [#118](https://github.com/instacart/makara/pull/118) Kevin Bacha
|
32
51
|
|
33
52
|
## v0.3.8 - 2017-07-11
|
34
53
|
|
35
|
-
[Full Changelog](https://github.com/
|
54
|
+
[Full Changelog](https://github.com/instacart/makara/compare/v0.3.7...v0.3.8)
|
36
55
|
|
37
56
|
Changed
|
38
|
-
- Rails 5.1 compatibility [#150](https://github.com/
|
39
|
-
- Minimize redundant context cache requests [#157](https://github.com/
|
40
|
-
- thread-local cache for previous context stickiness [#158](https://github.com/
|
41
|
-
- Configurable cookie options [#159](https://github.com/
|
42
|
-
- Test against Rails 5.x and Ruby 2.x [#160](https://github.com/
|
57
|
+
- Rails 5.1 compatibility [#150](https://github.com/instacart/makara/pull/150) Jeremy Daer
|
58
|
+
- Minimize redundant context cache requests [#157](https://github.com/instacart/makara/issues/157) Greg Patrick
|
59
|
+
- thread-local cache for previous context stickiness [#158](https://github.com/instacart/makara/issues/158) Jeremy Daer
|
60
|
+
- Configurable cookie options [#159](https://github.com/instacart/makara/pull/159) Jeremy Daer
|
61
|
+
- Test against Rails 5.x and Ruby 2.x [#160](https://github.com/instacart/makara/pull/160) Jeremy Daer
|
43
62
|
|
44
63
|
## v0.3.7 - 2016-09-22
|
45
64
|
|
46
|
-
[Full Changelog](https://github.com/
|
65
|
+
[Full Changelog](https://github.com/instacart/makara/compare/v0.3.6...v0.3.7)
|
47
66
|
|
48
67
|
Changed
|
49
68
|
|
50
|
-
- Fix the hierarchy of the config file [#116](https://github.com/
|
51
|
-
- "Disable blacklist" parameter [#134](https://github.com/
|
52
|
-
- Fixes bug in `without_sticking` [#96](https://github.com/
|
53
|
-
- Always stick inside transactions [#96](https://github.com/
|
54
|
-
- Rails 5 support [#122](https://github.com/
|
69
|
+
- Fix the hierarchy of the config file [#116](https://github.com/instacart/makara/pull/116) Kevin Bacha
|
70
|
+
- "Disable blacklist" parameter [#134](https://github.com/instacart/makara/pull/134) Alex Tonkonozhenko
|
71
|
+
- Fixes bug in `without_sticking` [#96](https://github.com/instacart/makara/pull/96) Brian Leonard
|
72
|
+
- Always stick inside transactions [#96](https://github.com/instacart/makara/pull/96) Brian Leonard
|
73
|
+
- Rails 5 support [#122](https://github.com/instacart/makara/pull/122) Jonny McAllister
|
55
74
|
|
56
75
|
## v0.3.6 - 2016-04-21
|
57
76
|
|
58
|
-
[Full Changelog](https://github.com/
|
77
|
+
[Full Changelog](https://github.com/instacart/makara/compare/v0.3.5...v0.3.6)
|
59
78
|
|
60
79
|
Changed
|
61
80
|
|
62
|
-
- Allow different strategies such as `priority` and `round_robin` for pools [#105](https://github.com/
|
81
|
+
- Allow different strategies such as `priority` and `round_robin` for pools [#105](https://github.com/instacart/makara/pull/105) Brian Leonard
|
63
82
|
|
64
83
|
|
65
84
|
## v0.3.5 - 2016-01-08
|
66
85
|
|
67
|
-
[Full Changelog](https://github.com/
|
86
|
+
[Full Changelog](https://github.com/instacart/makara/compare/v0.3.4.rc1...v0.3.5)
|
68
87
|
|
69
88
|
Changed
|
70
89
|
|
71
|
-
- Raise `Makara::Errors::AllConnectionsBlacklisted` on timeout. [#104](https://github.com/
|
90
|
+
- Raise `Makara::Errors::AllConnectionsBlacklisted` on timeout. [#104](https://github.com/instacart/makara/pull/104) Brian Leonard
|
72
91
|
|
73
92
|
## v0.3.4.rc1 - 2016-01-06
|
74
93
|
|
75
|
-
[Full Changelog](https://github.com/
|
94
|
+
[Full Changelog](https://github.com/instacart/makara/compare/v0.3.3...v0.3.4.rc1)
|
76
95
|
|
77
96
|
Added
|
78
97
|
|
79
|
-
- Add `url` to database connections configurations. [#93](https://github.com/
|
98
|
+
- Add `url` to database connections configurations. [#93](https://github.com/instacart/makara/pull/93) Benjamin Fleischer
|
80
99
|
|
81
100
|
Changed
|
82
101
|
|
83
|
-
- Improve Postgresql compatibility and failover support, also fix [#78](https://github.com/
|
84
|
-
- Update README: Specify newrelic_rpm gem versions that will have the performance issue. [#95](https://github.com/
|
102
|
+
- Improve Postgresql compatibility and failover support, also fix [#78](https://github.com/instacart/makara/issues/78), [#79](https://github.com/instacart/makara/issues/79). [#87](https://github.com/instacart/makara/pull/87) Vlad
|
103
|
+
- Update README: Specify newrelic_rpm gem versions that will have the performance issue. [#95](https://github.com/instacart/makara/pull/95) Benjamin Fleischer
|
85
104
|
|
86
105
|
## v0.3.3 - 2015-05-20
|
87
106
|
|
88
|
-
[Full Changelog](https://github.com/
|
107
|
+
[Full Changelog](https://github.com/instacart/makara/compare/v0.3.2...v0.3.3)
|
89
108
|
|
90
109
|
Changed
|
91
110
|
|
92
|
-
- A context is local to the curent thread of execution. This will allow you to stick to master safely in a single thread in systems such as sidekiq, for instance. Fix [#83](https://github.com/
|
111
|
+
- A context is local to the curent thread of execution. This will allow you to stick to master safely in a single thread in systems such as sidekiq, for instance. Fix [#83](https://github.com/instacart/makara/issues/83). [#84](https://github.com/instacart/makara/pull/84) Matt Camuto
|
93
112
|
|
94
113
|
## v0.3.2 - 2015-05-16
|
95
114
|
|
96
|
-
[Full Changelog](https://github.com/
|
115
|
+
[Full Changelog](https://github.com/instacart/makara/compare/v0.3.1...v0.3.2)
|
97
116
|
|
98
117
|
Fixed
|
99
118
|
|
100
|
-
- Fix a `ArgumentError: not delegated` error for rails 3. [#82](https://github.com/
|
119
|
+
- Fix a `ArgumentError: not delegated` error for rails 3. [#82](https://github.com/instacart/makara/pull/82) Eric Saxby
|
101
120
|
|
102
121
|
Changed
|
103
122
|
|
@@ -105,7 +124,7 @@ Changed
|
|
105
124
|
|
106
125
|
## v0.3.1 - 2015-05-08
|
107
126
|
|
108
|
-
[Full Changelog](https://github.com/
|
127
|
+
[Full Changelog](https://github.com/instacart/makara/compare/v0.3.0...v0.3.1)
|
109
128
|
|
110
129
|
Changed
|
111
130
|
|
@@ -115,21 +134,21 @@ Changed
|
|
115
134
|
|
116
135
|
## v0.3.0 - 2015-04-27
|
117
136
|
|
118
|
-
[Full Changelog](https://github.com/
|
137
|
+
[Full Changelog](https://github.com/instacart/makara/compare/v0.3.0.rc3...v0.3.0)
|
119
138
|
|
120
139
|
Changed
|
121
140
|
|
122
|
-
- Reduce logging noise by using [the same rules as ActiveRecord uses](https://github.com/rails/rails/blob/b06f64c3480cd389d14618540d62da4978918af0/activerecord/lib/active_record/log_subscriber.rb#L33). [#76](https://github.com/
|
141
|
+
- Reduce logging noise by using [the same rules as ActiveRecord uses](https://github.com/rails/rails/blob/b06f64c3480cd389d14618540d62da4978918af0/activerecord/lib/active_record/log_subscriber.rb#L33). [#76](https://github.com/instacart/makara/pull/76) Andrew Kane
|
123
142
|
|
124
143
|
Fixed
|
125
144
|
|
126
|
-
- Fix an issue for postgres that would route all queries to master. [#72](https://github.com/
|
127
|
-
- Fix an edge case which would cause SET operations to send to all connections([#70](https://github.com/
|
128
|
-
- Fix performance regression with certain verions of [newrelic/rpm](https://github.com/newrelic/rpm)([#59](https://github.com/
|
145
|
+
- Fix an issue for postgres that would route all queries to master. [#72](https://github.com/instacart/makara/pull/72) Kali Donovan
|
146
|
+
- Fix an edge case which would cause SET operations to send to all connections([#70](https://github.com/instacart/makara/issues/70)). [#80](https://github.com/instacart/makara/pull/80) Michael Amor Righi
|
147
|
+
- Fix performance regression with certain verions of [newrelic/rpm](https://github.com/newrelic/rpm)([#59](https://github.com/instacart/makara/issues/59)). [#75](https://github.com/instacart/makara/pull/75) Mike Nelson
|
129
148
|
|
130
149
|
## 0.3.0.rc3 - 2014-09-02[YANKED]
|
131
150
|
|
132
|
-
[Full Changelog](https://github.com/
|
151
|
+
[Full Changelog](https://github.com/instacart/makara/compare/v0.3.0.rc2...v0.3.0.rc3)
|
133
152
|
|
134
153
|
Added
|
135
154
|
- Allow bypassing of stickiness
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -6,14 +6,6 @@
|
|
6
6
|
|
7
7
|
Makara is generic master/slave proxy. It handles the heavy lifting of managing, choosing, blacklisting, and cycling through connections. It comes with an ActiveRecord database adapter implementation.
|
8
8
|
|
9
|
-
#### Warning:
|
10
|
-
|
11
|
-
There is a potential performance issue when used alongside certain versions of
|
12
|
-
[newrelic/rpm](https://github.com/newrelic/rpm), [Issue #59](https://github.com/taskrabbit/makara/issues/59).
|
13
|
-
|
14
|
-
> Any newrelic_rpm `< 3.11.2` and `>= 3.7.2` will have the regression.
|
15
|
-
> I'd recommend upgrading newrelic_rpm to avoid the problem.
|
16
|
-
|
17
9
|
## Installation
|
18
10
|
|
19
11
|
Use the current version of the gem from [rubygems](https://rubygems.org/gems/makara) in your `Gemfile`.
|
@@ -67,6 +59,12 @@ Makara handles stickiness by keeping track of which proxies are stuck at any giv
|
|
67
59
|
|
68
60
|
To handle persistence of context across requests in a Rack app, makara provides a middleware. It lays a cookie named `_mkra_stck` which contains the current context. If the next request is executed before the cookie expires, that given context will be used. If something occurs which naturally requires master on the second request, the context is updated and stored again.
|
69
61
|
|
62
|
+
#### Stickiness Impact
|
63
|
+
|
64
|
+
When `sticky:true`, once a query as been sent to master, all queries for the rest of the request will also be sent to master. In addition, the cookie described above will be set client side with an expiration defined by time at end of original request + `master_ttl`. As long as the cookie is valid, all requests will send queries to master.
|
65
|
+
|
66
|
+
When `sticky:false`, only queries that need to go to master will go there. Subsequent read queries in the same request will go to slaves.
|
67
|
+
|
70
68
|
#### Releasing stuck connections (clearing context)
|
71
69
|
|
72
70
|
If you need to clear the current context, releasing any stuck connections, all you have to do is:
|
data/gemfiles/ar-head.gemfile
CHANGED
@@ -11,7 +11,7 @@ gem 'rack'
|
|
11
11
|
gem 'timecop'
|
12
12
|
gem 'mysql2', :platform => :ruby
|
13
13
|
gem 'activerecord-jdbcmysql-adapter', :platform => :jruby
|
14
|
-
gem 'pg',
|
14
|
+
gem 'pg', :platform => :ruby
|
15
15
|
gem 'activerecord-jdbcpostgresql-adapter', :platform => :jruby
|
16
16
|
|
17
17
|
|
@@ -0,0 +1,24 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in makara.gemspec
|
4
|
+
gemspec :path => '../'
|
5
|
+
|
6
|
+
|
7
|
+
gem 'rake'
|
8
|
+
gem 'activerecord', '~> 5.2.0'
|
9
|
+
gem 'rspec'
|
10
|
+
gem 'rack'
|
11
|
+
gem 'timecop'
|
12
|
+
gem 'mysql2', '~> 0.4.10', :platform => :ruby
|
13
|
+
gem 'activerecord-jdbcmysql-adapter', :platform => :jruby
|
14
|
+
gem 'pg', '0.21.0', :platform => :ruby
|
15
|
+
gem 'activerecord-jdbcpostgresql-adapter', :platform => :jruby
|
16
|
+
|
17
|
+
|
18
|
+
rmajor, rminor, rpatch = RUBY_VERSION.split(/[^\d]/)[0..2].map(&:to_i)
|
19
|
+
|
20
|
+
# 2.1 +
|
21
|
+
if rmajor > 2 || (rmajor == 2 && rminor >= 1)
|
22
|
+
gem 'activerecord-postgis-adapter', :platform => :ruby
|
23
|
+
gem 'rgeo', :platform => :ruby
|
24
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in makara.gemspec
|
4
|
+
gemspec :path => '../'
|
5
|
+
|
6
|
+
|
7
|
+
gem 'rake'
|
8
|
+
gem 'activerecord', '~> 6.0.0'
|
9
|
+
gem 'rspec'
|
10
|
+
gem 'rack'
|
11
|
+
gem 'timecop'
|
12
|
+
gem 'mysql2', '~> 0.4.10', :platform => :ruby
|
13
|
+
gem 'activerecord-jdbcmysql-adapter', :platform => :jruby
|
14
|
+
gem 'pg', '0.21.0', :platform => :ruby
|
15
|
+
gem 'activerecord-jdbcpostgresql-adapter', :platform => :jruby
|
16
|
+
|
17
|
+
|
18
|
+
rmajor, rminor, rpatch = RUBY_VERSION.split(/[^\d]/)[0..2].map(&:to_i)
|
19
|
+
|
20
|
+
# 2.1 +
|
21
|
+
if rmajor > 2 || (rmajor == 2 && rminor >= 1)
|
22
|
+
gem 'activerecord-postgis-adapter', :platform => :ruby
|
23
|
+
gem 'rgeo', :platform => :ruby
|
24
|
+
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'active_record'
|
2
|
+
require 'makara'
|
2
3
|
|
3
4
|
module ActiveRecord
|
4
5
|
module ConnectionAdapters
|
@@ -108,6 +109,10 @@ module ActiveRecord
|
|
108
109
|
hijack_method :execute, :exec_query, :exec_no_cache, :exec_cache, :transaction
|
109
110
|
send_to_all :connect, :reconnect!, :verify!, :clear_cache!, :reset!
|
110
111
|
|
112
|
+
control_method :close, :steal!, :expire, :lease, :in_use?, :owner, :schema_cache, :pool=, :pool,
|
113
|
+
:schema_cache=, :lock, :seconds_idle, :==
|
114
|
+
|
115
|
+
|
111
116
|
SQL_MASTER_MATCHERS = [/\A\s*select.+for update\Z/i, /select.+lock in share mode\Z/i, /\A\s*select.+(nextval|currval|lastval|get_lock|release_lock|pg_advisory_lock|pg_advisory_unlock)\(/i].map(&:freeze).freeze
|
112
117
|
SQL_SLAVE_MATCHERS = [/\A\s*(select|with.+\)\s*select)\s/i].map(&:freeze).freeze
|
113
118
|
SQL_ALL_MATCHERS = [/\A\s*set\s/i].map(&:freeze).freeze
|
@@ -136,6 +141,7 @@ module ActiveRecord
|
|
136
141
|
|
137
142
|
def initialize(config)
|
138
143
|
@error_handler = ::ActiveRecord::ConnectionAdapters::MakaraAbstractAdapter::ErrorHandler.new
|
144
|
+
@control = ActiveRecordPoolControl.new(self)
|
139
145
|
super(config)
|
140
146
|
end
|
141
147
|
|
@@ -205,7 +211,107 @@ module ActiveRecord
|
|
205
211
|
raise NotImplementedError
|
206
212
|
end
|
207
213
|
|
214
|
+
class ActiveRecordPoolControl
|
215
|
+
attr_reader :owner
|
216
|
+
alias :in_use? :owner
|
217
|
+
|
218
|
+
def initialize(proxy)
|
219
|
+
@proxy = proxy
|
220
|
+
@owner = nil
|
221
|
+
@pool = nil
|
222
|
+
@schema_cache = ActiveRecord::ConnectionAdapters::SchemaCache.new @proxy
|
223
|
+
@idle_since = Concurrent.monotonic_time
|
224
|
+
@adapter = ActiveRecord::ConnectionAdapters::AbstractAdapter.new(@proxy)
|
225
|
+
end
|
226
|
+
|
227
|
+
def close(*args)
|
228
|
+
@pool.checkin @proxy
|
229
|
+
end
|
230
|
+
|
231
|
+
# this method must only be called while holding connection pool's mutex
|
232
|
+
def lease(*args)
|
233
|
+
if in_use?
|
234
|
+
msg = +"Cannot lease connection, "
|
235
|
+
if @owner == Thread.current
|
236
|
+
msg << "it is already leased by the current thread."
|
237
|
+
else
|
238
|
+
msg << "it is already in use by a different thread: #{@owner}. " \
|
239
|
+
"Current thread: #{Thread.current}."
|
240
|
+
end
|
241
|
+
raise ActiveRecordError, msg
|
242
|
+
end
|
243
|
+
@owner = Thread.current
|
244
|
+
end
|
245
|
+
|
246
|
+
# this method must only be called while holding connection pool's mutex
|
247
|
+
def expire(*args)
|
248
|
+
if in_use?
|
249
|
+
if @owner != Thread.current
|
250
|
+
raise ActiveRecordError, "Cannot expire connection, " \
|
251
|
+
"it is owned by a different thread: #{@owner}. " \
|
252
|
+
"Current thread: #{Thread.current}."
|
253
|
+
end
|
254
|
+
|
255
|
+
@idle_since = Concurrent.monotonic_time
|
256
|
+
@owner = nil
|
257
|
+
else
|
258
|
+
raise ActiveRecordError, "Cannot expire connection, it is not currently leased."
|
259
|
+
end
|
260
|
+
end
|
261
|
+
|
262
|
+
# Seconds since this connection was returned to the pool
|
263
|
+
def seconds_idle(*args)
|
264
|
+
return 0 if in_use?
|
265
|
+
|
266
|
+
Concurrent.monotonic_time - @idle_since
|
267
|
+
end
|
268
|
+
|
269
|
+
# this method must only be called while holding connection pool's mutex (and a desire for segfaults)
|
270
|
+
def steal!(*args)
|
271
|
+
if in_use?
|
272
|
+
if @owner != Thread.current
|
273
|
+
@pool.send :remove_connection_from_thread_cache, @proxy, @owner
|
274
|
+
@owner = Thread.current
|
275
|
+
end
|
276
|
+
else
|
277
|
+
raise ActiveRecordError, "Cannot steal connection, it is not currently leased."
|
278
|
+
end
|
279
|
+
end
|
280
|
+
|
281
|
+
def schema_cache(*args)
|
282
|
+
if @pool.respond_to?(:get_schema_cache) # AR6
|
283
|
+
@pool.get_schema_cache(@proxy)
|
284
|
+
else
|
285
|
+
@schema_cache
|
286
|
+
end
|
287
|
+
end
|
288
|
+
|
289
|
+
def schema_cache=(*args)
|
290
|
+
cache = args[0]
|
291
|
+
cache.connection = @proxy
|
292
|
+
if @pool.respond_to?(:set_schema_cache) # AR6
|
293
|
+
@pool.set_schema_cache(cache)
|
294
|
+
else
|
295
|
+
@schema_cache = cache
|
296
|
+
end
|
297
|
+
end
|
298
|
+
|
299
|
+
def lock(*args)
|
300
|
+
@adapter.lock
|
301
|
+
end
|
302
|
+
|
303
|
+
def pool=(*args)
|
304
|
+
@pool = args[0]
|
305
|
+
end
|
306
|
+
|
307
|
+
def pool(*args)
|
308
|
+
@pool
|
309
|
+
end
|
208
310
|
|
311
|
+
def ==(*args)
|
312
|
+
@proxy.object_id == args[0].object_id
|
313
|
+
end
|
314
|
+
end
|
209
315
|
end
|
210
316
|
end
|
211
317
|
end
|
data/lib/makara/config_parser.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'digest/md5'
|
2
2
|
require 'active_support/core_ext/hash/keys'
|
3
3
|
require 'active_support/core_ext/hash/except'
|
4
|
+
require 'cgi'
|
4
5
|
|
5
6
|
# Convenience methods to grab subconfigs out of the primary configuration.
|
6
7
|
# Provides a way to generate a consistent ID based on a unique config.
|
@@ -63,7 +64,7 @@ module Makara
|
|
63
64
|
# Converts the given URL to a full connection hash.
|
64
65
|
def to_hash
|
65
66
|
config = raw_config.reject { |_,value| value.blank? }
|
66
|
-
config.map { |key,value| config[key] =
|
67
|
+
config.map { |key,value| config[key] = CGI.unescape(value) if value.is_a? String }
|
67
68
|
config
|
68
69
|
end
|
69
70
|
|
@@ -35,11 +35,19 @@ module Makara
|
|
35
35
|
@config[:name]
|
36
36
|
end
|
37
37
|
|
38
|
+
def _makara_shard_id
|
39
|
+
@config[:shard_id]
|
40
|
+
end
|
41
|
+
|
38
42
|
# has this node been blacklisted?
|
39
43
|
def _makara_blacklisted?
|
40
44
|
@blacklisted_until.present? && @blacklisted_until.to_i > Time.now.to_i
|
41
45
|
end
|
42
46
|
|
47
|
+
def _makara_in_transaction?
|
48
|
+
@connection && @connection.open_transactions > 0 ? true : false
|
49
|
+
end
|
50
|
+
|
43
51
|
# blacklist this node for @config[:blacklist_duration] seconds
|
44
52
|
def _makara_blacklist!
|
45
53
|
@connection.disconnect! if @connection
|
@@ -158,6 +166,22 @@ module Makara
|
|
158
166
|
}
|
159
167
|
end
|
160
168
|
|
169
|
+
# Control methods must always be passed to the
|
170
|
+
# Makara::Proxy control object for handling (typically
|
171
|
+
# related to ActiveRecord connection pool management)
|
172
|
+
@proxy.class.control_methods.each do |meth|
|
173
|
+
extension << %Q{
|
174
|
+
def #{meth}(*args, &block)
|
175
|
+
proxy = _makara
|
176
|
+
if proxy
|
177
|
+
proxy.control.#{meth}(*args=args, block)
|
178
|
+
else
|
179
|
+
super # Only if we are not wrapped any longer
|
180
|
+
end
|
181
|
+
end
|
182
|
+
}
|
183
|
+
end
|
184
|
+
|
161
185
|
# extend the instance
|
162
186
|
con.instance_eval(extension)
|
163
187
|
# set the makara context
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Makara
|
2
|
+
module Errors
|
3
|
+
class InvalidShard < MakaraError
|
4
|
+
|
5
|
+
attr_reader :role
|
6
|
+
attr_reader :shard_id
|
7
|
+
|
8
|
+
def initialize(role, shard_id)
|
9
|
+
@role = role
|
10
|
+
@shard_id = shard_id
|
11
|
+
super "[Makara] Invalid shard_id #{shard_id} for the #{role} pool"
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|