mongomapper_fallback 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +47 -0
- data/Rakefile +2 -0
- data/lib/mongomapper_fallback/version.rb +3 -0
- data/lib/mongomapper_fallback.rb +53 -0
- data/mongomapper_fallback.gemspec +23 -0
- data/spec/mongomapper_fallback/mongomapper_fallback_spec.rb +112 -0
- data/spec/spec_helper.rb +4 -0
- metadata +123 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
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,47 @@
|
|
1
|
+
# MongomapperFallback
|
2
|
+
|
3
|
+
At this time, MongoMapper not have any mechanism to properly handle Replicaset connection failures (commonly master falls down and arbiter was not selected any server yet).
|
4
|
+
|
5
|
+
MongomapperFallback is a alternative to handle these failures with a simple retry mechanism.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'mongomapper_fallback'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install mongomapper_fallback
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
# example.rb
|
24
|
+
class Example
|
25
|
+
include MongomapperFallback
|
26
|
+
|
27
|
+
def execute
|
28
|
+
logger = Logger.new('your_logfile_here.log') # or Slogger
|
29
|
+
|
30
|
+
# available options:
|
31
|
+
# - retry_limit: limit of retries that will be executed
|
32
|
+
# - sig_quit: sends a QUIT to current process before retry failed code again
|
33
|
+
|
34
|
+
replicaset_safe_run!(logger, retry_limit: 10, sig_quit: true) do
|
35
|
+
# perform any operation
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
## Contributing
|
42
|
+
|
43
|
+
1. Fork it
|
44
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
45
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
46
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
47
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'mongo_mapper'
|
2
|
+
require 'mongomapper_fallback/version'
|
3
|
+
|
4
|
+
module MongomapperFallback
|
5
|
+
def replicaset_safe_run!(logger, options = {})
|
6
|
+
sig_quit = options[:sig_quit] === true
|
7
|
+
|
8
|
+
retry_limit = options[:retry_limit].to_i
|
9
|
+
retry_limit = 5 if retry_limit == 0
|
10
|
+
|
11
|
+
attempts = 0
|
12
|
+
replicaset_errors = [ Mongo::ConnectionFailure, Mongo::OperationFailure ]
|
13
|
+
|
14
|
+
yield
|
15
|
+
rescue *replicaset_errors => e
|
16
|
+
error = e
|
17
|
+
|
18
|
+
begin
|
19
|
+
refresh! if error.class == Mongo::ConnectionFailure
|
20
|
+
reconnect! if error.class == Mongo::OperationFailure
|
21
|
+
quit_process! if sig_quit
|
22
|
+
|
23
|
+
stop_retry = true
|
24
|
+
rescue *replicaset_errors => ex
|
25
|
+
logger.error("Failed to reconnect to MongoDB: [#{ex}]")
|
26
|
+
|
27
|
+
attempts += 1
|
28
|
+
error = ex
|
29
|
+
raise(error) if attempts >= retry_limit
|
30
|
+
|
31
|
+
retry
|
32
|
+
end
|
33
|
+
|
34
|
+
logger.info('Successfully reconnected to MongoDB. Retrying now.')
|
35
|
+
|
36
|
+
retry
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def refresh!
|
42
|
+
MongoMapper.connection.hard_refresh!
|
43
|
+
end
|
44
|
+
|
45
|
+
def reconnect!
|
46
|
+
MongoMapper.connection.reconnect
|
47
|
+
end
|
48
|
+
|
49
|
+
def quit_process!
|
50
|
+
Process.kill('QUIT', Process.ppid)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/mongomapper_fallback/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Marcelo Correia Pinheiro"]
|
6
|
+
gem.email = ["salizzar@gmail.com"]
|
7
|
+
gem.description = %q{MongoMapper replicaset fallback mechanism}
|
8
|
+
gem.summary = %q{MongomapperFallback is a simple mechanism to handle common replicaset connection failures.}
|
9
|
+
gem.homepage = "https://github.com/salizzar/mongomapper_fallback"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "mongomapper_fallback"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = MongomapperFallback::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency 'rspec'
|
19
|
+
gem.add_development_dependency 'bson_ext'
|
20
|
+
gem.add_development_dependency 'debugger'
|
21
|
+
|
22
|
+
gem.add_runtime_dependency 'mongo_mapper'
|
23
|
+
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MongomapperFallback do
|
4
|
+
subject do
|
5
|
+
class Example
|
6
|
+
include MongomapperFallback
|
7
|
+
|
8
|
+
def execute
|
9
|
+
replicaset_safe_run!(Logger.new(StringIO.new)) { perform_a_operation }
|
10
|
+
end
|
11
|
+
|
12
|
+
def perform_a_operation ; end
|
13
|
+
end
|
14
|
+
|
15
|
+
Example.new
|
16
|
+
end
|
17
|
+
|
18
|
+
let(:connection) { double('MongoMapper database connection') }
|
19
|
+
|
20
|
+
before :each do
|
21
|
+
MongoMapper.stub(connection: connection)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'raises error when it is not related to a MongoDB connection error' do
|
25
|
+
error = Exception.new 'an error'
|
26
|
+
subject.should_receive(:perform_a_operation).and_raise(error)
|
27
|
+
|
28
|
+
expect { subject.execute }.to raise_error(error)
|
29
|
+
end
|
30
|
+
|
31
|
+
describe 'detecting MongoDB connection errors' do
|
32
|
+
describe 'detecting Mongo::ConnectionFailure error' do
|
33
|
+
it 'refreshs connection' do
|
34
|
+
subject.should_receive(:perform_a_operation).and_raise(Mongo::ConnectionFailure)
|
35
|
+
connection.should_receive(:hard_refresh!)
|
36
|
+
connection.should_not_receive(:reconnect)
|
37
|
+
subject.should_receive(:perform_a_operation).and_return(true)
|
38
|
+
|
39
|
+
expect { subject.execute }.to_not raise_error
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe 'detecting Mongo::OperationFailure error' do
|
44
|
+
it 'reconnects to database server' do
|
45
|
+
subject.should_receive(:perform_a_operation).and_raise(Mongo::OperationFailure)
|
46
|
+
connection.should_receive(:reconnect)
|
47
|
+
connection.should_not_receive(:hard_refresh!)
|
48
|
+
subject.should_receive(:perform_a_operation).and_return(true)
|
49
|
+
|
50
|
+
expect { subject.execute }.to_not raise_error
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe 'retrying if attempts raises an error' do
|
55
|
+
it 'raises if retry limit is reached' do
|
56
|
+
subject.stub(:perform_a_operation).and_raise(Mongo::ConnectionFailure)
|
57
|
+
|
58
|
+
connection.should_receive(:hard_refresh!).exactly(5).times.and_raise(Mongo::ConnectionFailure)
|
59
|
+
|
60
|
+
expect { subject.execute }.to raise_error(Mongo::ConnectionFailure)
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'performs a reconnection if hard refresh raises a operation failure' do
|
64
|
+
subject.should_receive(:perform_a_operation).and_raise(Mongo::ConnectionFailure)
|
65
|
+
connection.should_receive(:hard_refresh!).and_raise(Mongo::OperationFailure)
|
66
|
+
connection.should_receive(:reconnect).and_return(true)
|
67
|
+
subject.should_receive(:perform_a_operation).and_return(true)
|
68
|
+
|
69
|
+
expect { subject.execute }.to_not raise_error
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'performs a hard refresh if reconnection raises a connection failure' do
|
73
|
+
subject.should_receive(:perform_a_operation).and_raise(Mongo::OperationFailure)
|
74
|
+
connection.should_receive(:reconnect).and_raise(Mongo::ConnectionFailure)
|
75
|
+
connection.should_receive(:hard_refresh!).and_return(true)
|
76
|
+
subject.should_receive(:perform_a_operation).and_return(true)
|
77
|
+
|
78
|
+
expect { subject.execute }.to_not raise_error
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe 'custom configuration' do
|
84
|
+
before :each do
|
85
|
+
subject.class_eval do
|
86
|
+
def execute
|
87
|
+
replicaset_safe_run!(Logger.new(StringIO.new), sig_quit: true, retry_limit: 10) { perform_a_operation }
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'sends a SIGQUIT if flag is setted' do
|
93
|
+
ppid = 1234
|
94
|
+
Process.should_receive(:ppid).and_return(ppid)
|
95
|
+
Process.should_receive(:kill).with('QUIT', ppid)
|
96
|
+
|
97
|
+
subject.should_receive(:perform_a_operation).and_raise(Mongo::OperationFailure)
|
98
|
+
connection.should_receive(:reconnect)
|
99
|
+
subject.should_receive(:perform_a_operation).and_return(true)
|
100
|
+
|
101
|
+
expect { subject.execute }.to_not raise_error
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'retries based on informed value when informed' do
|
105
|
+
subject.should_receive(:perform_a_operation).and_raise(Mongo::ConnectionFailure)
|
106
|
+
connection.should_receive(:hard_refresh!).exactly(10).times.and_raise(Mongo::ConnectionFailure)
|
107
|
+
|
108
|
+
expect { subject.execute }.to raise_error(Mongo::ConnectionFailure)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongomapper_fallback
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Marcelo Correia Pinheiro
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-06-21 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bson_ext
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: debugger
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: mongo_mapper
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: MongoMapper replicaset fallback mechanism
|
79
|
+
email:
|
80
|
+
- salizzar@gmail.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- .rspec
|
87
|
+
- Gemfile
|
88
|
+
- LICENSE
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- lib/mongomapper_fallback.rb
|
92
|
+
- lib/mongomapper_fallback/version.rb
|
93
|
+
- mongomapper_fallback.gemspec
|
94
|
+
- spec/mongomapper_fallback/mongomapper_fallback_spec.rb
|
95
|
+
- spec/spec_helper.rb
|
96
|
+
homepage: https://github.com/salizzar/mongomapper_fallback
|
97
|
+
licenses: []
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
none: false
|
110
|
+
requirements:
|
111
|
+
- - ! '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 1.8.23
|
117
|
+
signing_key:
|
118
|
+
specification_version: 3
|
119
|
+
summary: MongomapperFallback is a simple mechanism to handle common replicaset connection
|
120
|
+
failures.
|
121
|
+
test_files:
|
122
|
+
- spec/mongomapper_fallback/mongomapper_fallback_spec.rb
|
123
|
+
- spec/spec_helper.rb
|