sequel-replica-failover 0.0.1
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 +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +54 -0
- data/Rakefile +1 -0
- data/lib/sequel-replica-failover.rb +7 -0
- data/lib/sequel-replica-failover/version.rb +5 -0
- data/lib/sequel/connection_pool/sharded_single_failover.rb +53 -0
- data/sequel-replica-failover.gemspec +27 -0
- data/spec/sequel/connection_pool/sharded_single_failover_spec.rb +69 -0
- data/spec/spec_helper.rb +20 -0
- metadata +128 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a338272e19a13d468284cedccabd633eaeb930d6
|
4
|
+
data.tar.gz: bd424f6b11b7eeb0637df0cabfb6e5e7a40d9175
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c07b25a6c843c8614eccdb633b9f6019fb30cb47b2ced0cf189c6f298c3831e1fd75d048408b73e7026d4ef36c69e9104164e660764cd8e5b4cf24622fbca0ef
|
7
|
+
data.tar.gz: 8e87d291618c292d845fa541f4765481432ffc5d69310a23d674e6c4adc17cdd7fed63b59352e7b3dd8b1bd2096c294660b68554805482abd556ba1f768efeac
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 TODO: Write your name
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# Automatic read-only failover for Sequel
|
2
|
+
|
3
|
+
This provides a NOT-THREADSAFE sharded connection pool for failing over between configured replicas.
|
4
|
+
|
5
|
+
The mechanisms it provides are as follows:
|
6
|
+
|
7
|
+
1. When a DatabaseDisconnectError or DatabaseConnectError occurs, the pool attempt to make another connection to the
|
8
|
+
:read_only server and retry.
|
9
|
+
2. The pool will retry `:pool_retry_count` times afterwhich it will raise the exception that triggered the failover.
|
10
|
+
3. The pool will stick to a working connection for `:pool_stick_timeout` (in seconds) after which it will
|
11
|
+
try connecting back to a replica.
|
12
|
+
4. Anytime a transaction has been started, the pool will NOT failover.
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
gem 'sequel-replica-failover'
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install sequel-replica-failover
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
When initializing a Sequel connection, set the pool class:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
DB = Sequel.connect({
|
34
|
+
:adapter => 'postgres',
|
35
|
+
:user => 'postgres',
|
36
|
+
:password => 'postgres',
|
37
|
+
:host => '127.0.0.1',
|
38
|
+
:database => 'postgres',
|
39
|
+
:port => 5432,
|
40
|
+
:pool_class => Sequel::ShardedSingleFailoverConnectionPool,
|
41
|
+
:pool_retry_count => 10,
|
42
|
+
:pool_stick_timeout => 30
|
43
|
+
})
|
44
|
+
```
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
## Contributing
|
49
|
+
|
50
|
+
1. Fork it
|
51
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
52
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
53
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
54
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'sequel'
|
2
|
+
require 'sequel/connection_pool/sharded_single'
|
3
|
+
|
4
|
+
class Sequel::ShardedSingleFailoverConnectionPool < Sequel::ShardedSingleConnectionPool
|
5
|
+
def initialize(db, opts = OPTS)
|
6
|
+
super
|
7
|
+
@pool_stick_timeout = opts[:pool_stick_timeout] || 15
|
8
|
+
@pool_retry_count = opts[:pool_retry_count] || 5
|
9
|
+
end
|
10
|
+
|
11
|
+
# Yields the connection to the supplied block for the given server.
|
12
|
+
# This method simulates the ConnectionPool#hold API.
|
13
|
+
def hold(server=:default, &block)
|
14
|
+
if server == :read_only &&
|
15
|
+
@stuck_at &&
|
16
|
+
Time.now.to_i - @stuck_at.to_i >= @pool_stick_timeout
|
17
|
+
unstick(:read_only)
|
18
|
+
end
|
19
|
+
|
20
|
+
super(server, &block)
|
21
|
+
rescue Sequel::DatabaseDisconnectError, Sequel::DatabaseConnectionError
|
22
|
+
if server == :read_only && !@db.in_transaction?(server: :read_only)
|
23
|
+
stick
|
24
|
+
|
25
|
+
if @stuck_times >= @pool_retry_count
|
26
|
+
raise
|
27
|
+
end
|
28
|
+
|
29
|
+
hold(server, &block)
|
30
|
+
else
|
31
|
+
raise
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def pool_type
|
36
|
+
:sharded_single_failover
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def unstick(server)
|
42
|
+
disconnect_server(server)
|
43
|
+
@conns[server] = nil
|
44
|
+
@stuck_at = nil
|
45
|
+
@stuck_times = nil
|
46
|
+
end
|
47
|
+
|
48
|
+
def stick
|
49
|
+
@stuck_at ||= Time.now
|
50
|
+
@stuck_times ||= 0
|
51
|
+
@stuck_times += 1
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sequel-replica-failover/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "sequel-replica-failover"
|
8
|
+
spec.version = Sequel::ReplicaFailover::VERSION
|
9
|
+
spec.authors = ["Paul Henry"]
|
10
|
+
spec.email = ["paul@wanelo.com"]
|
11
|
+
spec.description = %q{Automatically failover between replicas when they go down.}
|
12
|
+
spec.summary = %q{Automatically failover when replicas go down.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "sequel", ">= 4.3.0"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
spec.add_development_dependency "timecop"
|
27
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
CONNECTION_POOL_DEFAULTS = {:pool_retry_count => 3,
|
3
|
+
:pool_stick_timeout => 15,
|
4
|
+
:pool_timeout=>5,
|
5
|
+
:pool_sleep_time=>0.001,
|
6
|
+
:max_connections=>4,
|
7
|
+
:pool_class => Sequel::ShardedSingleFailoverConnectionPool,
|
8
|
+
:servers => { :read_only => {} } }
|
9
|
+
|
10
|
+
mock_db = lambda do |*a, &b|
|
11
|
+
db = Sequel.mock
|
12
|
+
(class << db; self end).send(:define_method, :connect){|c| b.arity == 1 ? b.call(c) : b.call} if b
|
13
|
+
if b2 = a.shift
|
14
|
+
(class << db; self end).send(:define_method, :disconnect_connection){|c| b2.arity == 1 ? b2.call(c) : b2.call}
|
15
|
+
end
|
16
|
+
db
|
17
|
+
end
|
18
|
+
|
19
|
+
describe Sequel::ShardedSingleFailoverConnectionPool do
|
20
|
+
describe '#hold' do
|
21
|
+
before do
|
22
|
+
msp = proc { @max_size=3 }
|
23
|
+
@connection_pool = Sequel::ConnectionPool.get_pool(mock_db.call(proc { |c| msp.call }) { :got_connection }, CONNECTION_POOL_DEFAULTS)
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'with read_only server' do
|
27
|
+
context 'when block raises a database connection error' do
|
28
|
+
it 'retries until the a successful connection is made' do
|
29
|
+
call_count = 0
|
30
|
+
proc { @connection_pool.hold(:read_only) { call_count += 1; raise Sequel::DatabaseDisconnectError if call_count == 1 } }.should_not raise_error
|
31
|
+
expect(call_count).to eq(2)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'only retries N number of times before actually raising the error' do
|
35
|
+
call_count = 0
|
36
|
+
proc { @connection_pool.hold(:read_only) { call_count += 1; raise Sequel::DatabaseDisconnectError } }.should raise_error(Sequel::DatabaseDisconnectError)
|
37
|
+
expect(call_count).to eq(3)
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'sticks for N number of seconds to a working connection' do
|
41
|
+
Timecop.freeze -16 do
|
42
|
+
call_count = 0
|
43
|
+
proc { @connection_pool.hold(:read_only) { call_count += 1; raise Sequel::DatabaseDisconnectError if call_count == 1 } }.should_not raise_error
|
44
|
+
expect(call_count).to eq(2)
|
45
|
+
expect(@connection_pool.size).to eq(1)
|
46
|
+
end
|
47
|
+
|
48
|
+
@connection_pool.should_receive(:make_new).once
|
49
|
+
@connection_pool.hold(:read_only) {}
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'when in a transaction' do
|
53
|
+
it 'raises an exception' do
|
54
|
+
Sequel::Mock::Database.any_instance.should_receive(:in_transaction?).and_return(true)
|
55
|
+
proc { @connection_pool.hold(:read_only) { raise Sequel::DatabaseDisconnectError } }.should raise_error(Sequel::DatabaseDisconnectError)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'with default or arbritrary server' do
|
62
|
+
it 'does no retry logic and raises error' do
|
63
|
+
call_count = 0
|
64
|
+
proc { @connection_pool.hold { call_count += 1; raise Sequel::DatabaseDisconnectError if call_count == 1 } }.should raise_error(Sequel::DatabaseDisconnectError)
|
65
|
+
expect(call_count).to eq(1)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
require 'sequel-replica-failover'
|
8
|
+
require 'timecop'
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
12
|
+
config.run_all_when_everything_filtered = true
|
13
|
+
config.filter_run :focus
|
14
|
+
|
15
|
+
# Run specs in random order to surface order dependencies. If you find an
|
16
|
+
# order dependency and want to debug it, you can fix the order by providing
|
17
|
+
# the seed, which is printed after each run.
|
18
|
+
# --seed 1234
|
19
|
+
config.order = 'random'
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sequel-replica-failover
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Paul Henry
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-11-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sequel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.3.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.3.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: timecop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Automatically failover between replicas when they go down.
|
84
|
+
email:
|
85
|
+
- paul@wanelo.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- .gitignore
|
91
|
+
- .rspec
|
92
|
+
- Gemfile
|
93
|
+
- LICENSE.txt
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- lib/sequel-replica-failover.rb
|
97
|
+
- lib/sequel-replica-failover/version.rb
|
98
|
+
- lib/sequel/connection_pool/sharded_single_failover.rb
|
99
|
+
- sequel-replica-failover.gemspec
|
100
|
+
- spec/sequel/connection_pool/sharded_single_failover_spec.rb
|
101
|
+
- spec/spec_helper.rb
|
102
|
+
homepage: ''
|
103
|
+
licenses:
|
104
|
+
- MIT
|
105
|
+
metadata: {}
|
106
|
+
post_install_message:
|
107
|
+
rdoc_options: []
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - '>='
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 2.0.7
|
123
|
+
signing_key:
|
124
|
+
specification_version: 4
|
125
|
+
summary: Automatically failover when replicas go down.
|
126
|
+
test_files:
|
127
|
+
- spec/sequel/connection_pool/sharded_single_failover_spec.rb
|
128
|
+
- spec/spec_helper.rb
|