mongo_ha 1.12.4 → 2.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c98aae542b09bfdcb6a8cf4af14cd8eacb3f29fe
4
- data.tar.gz: 6213123e29784aba1116471a878ed9bcdb9572da
3
+ metadata.gz: c07613ccc0db614856311ea723d0bb980bd53113
4
+ data.tar.gz: f449fd924fd37daac5bfd82d3d13950422d895d9
5
5
  SHA512:
6
- metadata.gz: c3f18e4efa572627a4efb3c8da4d7a67e18f21040049c1e68c4cdb89837148303a51dc255740f65f1352c577237609490a07e99a64d34de8c8630102f265465e
7
- data.tar.gz: f3afdbe8d28adfcc6cfbf5a02da254eaf83cd89ec4b54bd6f2261fc1fcb5f692783514cba1e8f0ca351ac4727f71c1882f122ce5cb13a63353c2b038773c64b0
6
+ metadata.gz: 4e65723c5ba6bb1ac48c2095c353ee6ca048d49016e92db7329f8820a568b95a63949151f0831a3a3379f477b9673d1c9f999749f2e4d623e56a5a3f2651abbe
7
+ data.tar.gz: 83e9ccb94dc81194df0c87b8558def4f5d17efff66297071c43ab3ed4b48c52d019a4175921b87418fcd44e42d9543b282c765d3e88bcbb8e91c8b1e5b192e8a
data/LICENSE.txt ADDED
@@ -0,0 +1,202 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright {yyyy} {name of copyright owner}
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
202
+
data/README.md CHANGED
@@ -4,8 +4,7 @@ High availability for the mongo ruby driver. Automatic reconnects and recovery w
4
4
 
5
5
  ## Status
6
6
 
7
- Production Ready: Used every day in an enterprise environment across
8
- remote data centers.
7
+ Production Ready: Used every day in an enterprise environment across remote data centers.
9
8
 
10
9
  ## Overview
11
10
 
@@ -23,49 +22,9 @@ In the event of a connection failure, only one thread will attempt to re-establi
23
22
  connectivity to the Mongo server(s). This is to prevent swamping the mongo
24
23
  servers with reconnect attempts.
25
24
 
26
- Retries are initially performed quickly in case it is a brief network issue
27
- and then backs off to give the replica-set time to elect a new master.
25
+ Supports Ruby Mongo driver V1 and V2
28
26
 
29
- Supports Ruby Mongo driver V1.x.x
30
-
31
- `mongo_ha` transparently supports `MongoMapper` since it uses the mongo ruby driver
32
- that is patched by loading this gem. Earlier versions of Mongoid will also benefit
33
- from `mongo_ha`, the latest version of Mongoid uses Moped that should be avoided
34
- since it is due to be replaced by the Ruby Mongo driver V2 when it is released.
35
-
36
- Mongo Router processes will often return a connection failure on their side
37
- as an OperationFailure. This code will also retry automatically when the router
38
- has errors talking to a sharded cluster.
39
-
40
- ## Mongo Cursors
41
-
42
- Any operations that return a cursor need to be handled in your own code
43
- since the retry cannot be handled transparently.
44
- For example: `find` returns a cursor, whereas `find_one` is handled because
45
- it returns the data returned rather than a cursor
46
-
47
- Example
48
-
49
- ```ruby
50
- # Wrap existing cursor based calls with a retry on connection failure block
51
- results_collection.retry_on_connection_failure do
52
- results_collection.find({}, sort: '_id', timeout: false) do |cursor|
53
- cursor.each do |record|
54
- puts "Record: #{record.inspect}"
55
- end
56
- end
57
- end
58
- ```
59
-
60
- ### Note
61
-
62
- In the above example the block will be repeated from the _beginning_ of the
63
- collection should a connection failure occur. Without appropriate handling it
64
- is possible to read the same records twice.
65
-
66
- If the collection cannot be processed twice, it may be better to just let the
67
- `Mongo::ConnectionFailure` flow up into the application for it to deal with at
68
- a higher level.
27
+ `mongo_ha` transparently supports `MongoMapper` and `Mongoid` which use the mongo ruby driver.
69
28
 
70
29
  ## Installation
71
30
 
@@ -85,70 +44,6 @@ If you are also using SemanticLogger, place `mongo_ha` below `semantic_logger`
85
44
  and/or `rails_semantic_logger` in the `Gemfile`. This way it will create a logger
86
45
  just for `Mongo::MongoClient` to improve the log output during connection recovery.
87
46
 
88
- ## Configuration
89
-
90
- mongo_ha adds several new configuration options to fine tune the reconnect behavior
91
- for any environment.
92
-
93
- Sample mongo.yml:
94
-
95
- ```yaml
96
- default_options: &default_options
97
- :w: 1
98
- :pool_size: 5
99
- :pool_timeout: 5
100
- :connect_timeout: 5
101
- :reconnect_attempts: 53
102
- :reconnect_retry_seconds: 0.1
103
- :reconnect_retry_multiplier: 2
104
- :reconnect_max_retry_seconds: 5
105
-
106
- development: &development
107
- uri: mongodb://localhost:27017/development
108
- options:
109
- <<: *default_options
110
-
111
- test:
112
- uri: mongodb://localhost:27017/test
113
- options:
114
- <<: *default_options
115
-
116
- # Sample Production Settings
117
- production:
118
- uri: mongodb://mongo1.site.com:27017,mongo2.site.com:27017/production
119
- options:
120
- <<: *default_options
121
- :pool_size: 50
122
- :pool_timeout: 5
123
- ```
124
-
125
- The following options can be specified in the Mongo configuration options
126
- to tune the retry intervals during a connection failure
127
-
128
- ### :reconnect_attempts
129
-
130
- * Number of times to attempt to reconnect.
131
- * Default: 53
132
-
133
- ### :reconnect_retry_seconds
134
-
135
- * Initial delay before retrying
136
- * Default: 0.1
137
-
138
- ### :reconnect_retry_multiplier
139
-
140
- * Multiply delay by this number with each retry to prevent overwhelming the server
141
- * Default: 2
142
-
143
- ### :reconnect_max_retry_seconds
144
-
145
- * Maximum number of seconds to wait before retrying again
146
- * Default: 5
147
-
148
- Using the above default values, will result in retry connects at the following intervals
149
-
150
- 0.1 0.2 0.4 0.8 1.6 3.2 5 5 5 5 ....
151
-
152
47
  ## Testing
153
48
 
154
49
  There is really only one place to test something like `mongo_ha` and that is in
@@ -157,16 +52,3 @@ This gem was created and tested with MongoDB running in an
157
52
  enterprise production environment with hundreds of connections to Mongo servers
158
53
  in remote data centers across a WAN. It adds high availability to standalone
159
54
  MongoDB servers, replica-sets, and sharded clusters.
160
-
161
- ## Issues
162
-
163
- If the following output appears after adding the above connection options:
164
-
165
- ```shell
166
- reconnect_attempts is not a valid option for Mongo::MongoClient
167
- reconnect_retry_seconds is not a valid option for Mongo::MongoClient
168
- reconnect_retry_multiplier is not a valid option for Mongo::MongoClient
169
- reconnect_max_retry_seconds is not a valid option for Mongo::MongoClient
170
- ```
171
-
172
- Then the `mongo_ha` gem was not loaded prior to connecting to Mongo
@@ -0,0 +1,34 @@
1
+ require 'mongo/error/operation_failure'
2
+
3
+ module Mongo
4
+ class Error
5
+ class OperationFailure
6
+ WRITE_RETRY_MESSAGES = [
7
+ 'no master',
8
+ 'not master',
9
+ 'could not contact primary',
10
+ 'Not primary'
11
+ ]
12
+
13
+ remove_const :RETRY_MESSAGES
14
+ RETRY_MESSAGES = WRITE_RETRY_MESSAGES + [
15
+ 'transport error',
16
+ 'socket exception',
17
+ "can't connect",
18
+ 'connect failed',
19
+ 'error querying',
20
+ 'could not get last error',
21
+ 'connection attempt failed',
22
+ 'interrupted at shutdown',
23
+ 'unknown replica set',
24
+ 'dbclient error communicating with server'
25
+ ]
26
+
27
+ def write_retryable?
28
+ WRITE_RETRY_MESSAGES.any? { |m| message.include?(m) }
29
+ end
30
+
31
+ end
32
+ end
33
+ end
34
+
@@ -0,0 +1,62 @@
1
+ require 'mongo/retryable'
2
+
3
+ module Mongo
4
+ module Retryable
5
+
6
+ def read_with_retry(attempt = 0, &block)
7
+ begin
8
+ block.call
9
+ rescue Error::SocketError, Error::SocketTimeoutError => e
10
+ retry_operation(e, &block)
11
+ rescue Error::OperationFailure => e
12
+ # TODO: Non sharded, retryable due to Replicaset primary change
13
+
14
+ if cluster.sharded? && e.retryable?
15
+ if attempt < cluster.max_read_retries
16
+ # We don't scan the cluster in this case as Mongos always returns
17
+ # ready after a ping no matter what the state behind it is.
18
+ sleep(cluster.read_retry_interval)
19
+ read_with_retry(attempt + 1, &block)
20
+ else
21
+ raise e
22
+ end
23
+ else
24
+ raise e
25
+ end
26
+ end
27
+ end
28
+
29
+ def read_with_one_retry(&block)
30
+ block.call
31
+ rescue Error::SocketError, Error::SocketTimeoutError => e
32
+ Logger.logger.warn "Single retry due to: #{e.class.name} #{e.message}"
33
+ block.call
34
+ end
35
+
36
+ def write_with_retry(&block)
37
+ begin
38
+ block.call
39
+ rescue Error::SocketError => e
40
+ # During a master move in a replica-set the master closes existing client connections.
41
+ # Note: Small possibility the write occurs twice.
42
+ retry_operation(e, &block)
43
+ rescue Error::OperationFailure => e
44
+ if e.write_retryable?
45
+ retry_operation(e, &block)
46
+ else
47
+ raise e
48
+ end
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ # Log a warning on retry to prevent appearance of "hanging" during a failover.
55
+ def retry_operation(e, &block)
56
+ Logger.logger.warn "Retry due to: #{e.class.name} #{e.message}"
57
+ cluster.scan!
58
+ block.call
59
+ end
60
+
61
+ end
62
+ end
@@ -1,3 +1,3 @@
1
1
  module MongoHA #:nodoc
2
- VERSION = '1.12.4'
2
+ VERSION = '2.0.0'
3
3
  end
data/lib/mongo_ha.rb CHANGED
@@ -1,28 +1,3 @@
1
1
  require 'mongo'
2
- require 'mongo_ha/version'
3
- require 'mongo_ha/mongo_client'
4
-
5
- # Add in retry methods
6
- Mongo::MongoClient.send(:include, MongoHA::MongoClient::InstanceMethods)
7
-
8
- # Wrap critical Mongo methods with retry_on_connection_failure
9
- {
10
- # Most calls use a cursor under the covers to return the result
11
- # If the primary is lost and it connects to a different server an expired cursor exception is raised
12
- Mongo::Cursor => [:refresh],
13
-
14
- # These methods do not use a Cursor
15
- Mongo::Collection => [:insert, :remove, :update]
16
- }.each_pair do |klass, methods|
17
- methods.each do |method|
18
- original_method = "#{method}_original".to_sym
19
- klass.send(:alias_method, original_method, method)
20
- klass.send(:define_method, method) do |*args|
21
- @connection.retry_on_connection_failure { send(original_method, *args) }
22
- end
23
- end
24
- end
25
-
26
- # Drop the max ping time to a more respectable time. Assuming it is in ms.
27
- Mongo::Pool.send(:remove_const, :MAX_PING_TIME)
28
- Mongo::Pool::MAX_PING_TIME = 5_000
2
+ require 'mongo_ha/operation_failure'
3
+ require 'mongo_ha/retryable'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo_ha
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.12.4
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Reid Morrison
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-07 00:00:00.000000000 Z
11
+ date: 2016-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongo
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.10'
19
+ version: '2.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.10'
26
+ version: '2.0'
27
27
  description: Automatic reconnects and recovery when replica-set changes, or connections
28
28
  are lost, with transparent recovery
29
29
  email:
@@ -32,12 +32,12 @@ executables: []
32
32
  extensions: []
33
33
  extra_rdoc_files: []
34
34
  files:
35
+ - LICENSE.txt
35
36
  - README.md
36
- - Rakefile
37
37
  - lib/mongo_ha.rb
38
- - lib/mongo_ha/mongo_client.rb
38
+ - lib/mongo_ha/operation_failure.rb
39
+ - lib/mongo_ha/retryable.rb
39
40
  - lib/mongo_ha/version.rb
40
- - test/readme.md
41
41
  homepage: https://github.com/reidmorrison/mongo_ha
42
42
  licenses:
43
43
  - Apache-2.0
@@ -58,9 +58,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
58
58
  version: '0'
59
59
  requirements: []
60
60
  rubyforge_project:
61
- rubygems_version: 2.5.1
61
+ rubygems_version: 2.6.8
62
62
  signing_key:
63
63
  specification_version: 4
64
64
  summary: High availability for the mongo ruby driver
65
- test_files:
66
- - test/readme.md
65
+ test_files: []
data/Rakefile DELETED
@@ -1,28 +0,0 @@
1
- require 'rake/clean'
2
- require 'rake/testtask'
3
-
4
- $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
5
- require 'mongo_ha/version'
6
-
7
- task :gem do
8
- system "gem build mongo_ha.gemspec"
9
- end
10
-
11
- task :publish => :gem do
12
- system "git tag -a v#{MongoHA::VERSION} -m 'Tagging #{MongoHA::VERSION}'"
13
- system "git push --tags"
14
- system "gem push mongo_ha-#{MongoHA::VERSION}.gem"
15
- system "rm mongo_ha-#{MongoHA::VERSION}.gem"
16
- end
17
-
18
- desc "Run Test Suite"
19
- task :test do
20
- Rake::TestTask.new(:functional) do |t|
21
- t.test_files = FileList['test/*_test.rb']
22
- t.verbose = true
23
- end
24
-
25
- Rake::Task['functional'].invoke
26
- end
27
-
28
- task :default => :test
@@ -1,205 +0,0 @@
1
- require 'mongo'
2
- module MongoHA
3
- module MongoClient
4
- CONNECTION_RETRY_OPTS = [:reconnect_attempts, :reconnect_retry_seconds, :reconnect_retry_multiplier, :reconnect_max_retry_seconds]
5
-
6
- # The following errors occur when mongos cannot connect to the shard
7
- # They require a retry to resolve them
8
- # This list was created through painful experience. Add any new ones as they are discovered
9
- # 9001: socket exception
10
- # Operation failed with the following exception: Unknown error - Connection reset by peer:Unknown error - Connection reset by peer
11
- # DBClientBase::findOne: transport error
12
- # : db assertion failure
13
- # 8002: 8002 all servers down!
14
- # Operation failed with the following exception: stream closed
15
- # Operation failed with the following exception: Bad file descriptor - Bad file descriptor:Bad file descriptor - Bad file descriptor
16
- # Failed to connect to primary node.
17
- # 10009: ReplicaSetMonitor no master found for set: mdbb
18
- OPERATION_FAILURE_ERRORS = [
19
- 'socket exception',
20
- 'Connection reset by peer',
21
- 'transport error',
22
- 'db assertion failure',
23
- '8002',
24
- 'stream closed',
25
- 'Bad file descriptor',
26
- 'Failed to connect',
27
- '10009',
28
- 'no master found',
29
- 'not master',
30
- 'Timed out waiting on socket',
31
- "didn't get writeback",
32
- 'interrupted at shutdown'
33
- ]
34
-
35
- module InstanceMethods
36
- # Add retry logic to MongoClient
37
- def self.included(base)
38
- base.class_eval do
39
- # Give MongoClient a class-specific logger if SemanticLogger V2.12 or above is available
40
- # to give better logging information during a connection recovery scenario
41
- if defined?(SemanticLogger::DebugAsTraceLogger)
42
- # Map Debug level calls to trace to reduce log file clutter
43
- @@logger = SemanticLogger::DebugAsTraceLogger.new(self)
44
-
45
- def self.logger
46
- @@logger
47
- end
48
-
49
- def logger
50
- self.class.logger
51
- end
52
- end
53
-
54
- alias_method :valid_opts_original, :valid_opts
55
- alias_method :setup_original, :setup
56
-
57
- attr_accessor *CONNECTION_RETRY_OPTS
58
-
59
- # Prevent multiple threads from trying to reconnect at the same time during
60
- # connection failures
61
- @@failover_mutex = Mutex.new
62
-
63
- private
64
-
65
- def valid_opts(*args)
66
- valid_opts_original(*args) + CONNECTION_RETRY_OPTS
67
- end
68
-
69
- def setup(opts)
70
- self.reconnect_attempts = (opts.delete(:reconnect_attempts) || 53).to_i
71
- self.reconnect_retry_seconds = (opts.delete(:reconnect_retry_seconds) || 0.1).to_f
72
- self.reconnect_retry_multiplier = (opts.delete(:reconnect_retry_multiplier) || 2).to_f
73
- self.reconnect_max_retry_seconds = (opts.delete(:reconnect_max_retry_seconds) || 5).to_f
74
- setup_original(opts)
75
- end
76
-
77
- end
78
- end
79
-
80
- # Retry the supplied block when a Mongo::ConnectionFailure occurs
81
- #
82
- # Note: Check for Duplicate Key on inserts
83
- #
84
- # Returns the result of the block
85
- #
86
- # Example:
87
- # connection.retry_on_connection_failure { |retried| connection.ping }
88
- def retry_on_connection_failure(&block)
89
- raise 'Missing mandatory block parameter on call to Mongo::Connection#retry_on_connection_failure' unless block
90
- # No need to double retry calls
91
- return block.call(false) if Thread.current[:mongo_ha_active?]
92
- retried = false
93
- mongos_retries = 0
94
- begin
95
- Thread.current[:mongo_ha_active?] = true
96
- result = block.call(retried)
97
- retried = false
98
- result
99
- rescue Mongo::ConnectionFailure => exc
100
- # Retry if reconnected, but only once to prevent an infinite loop
101
- logger.warn "Connection Failure: '#{exc.message}' [#{exc.error_code}]"
102
- if !retried && _reconnect
103
- retried = true
104
- retry
105
- end
106
- raise exc
107
- rescue Mongo::AuthenticationError => exc
108
- # Retry once due to rare failures during authentication against MongoDB V3 servers
109
- logger.warn "Authentication Failure: '#{exc.message}' [#{exc.error_code}]"
110
- if !retried && _reconnect
111
- retried = true
112
- retry
113
- end
114
- raise exc
115
- rescue Mongo::OperationTimeout => exc
116
- logger.warn "OperationTimeout: #{exc.message}"
117
- if !retried && _reconnect
118
- retried = true
119
- retry
120
- end
121
- raise exc
122
- rescue Mongo::OperationFailure => exc
123
- # Workaround not master issue. Disconnect connection when we get a not master
124
- # error message. Master checks for an exact match on "not master", whereas
125
- # it sometimes gets: "not master and slaveok=false"
126
- if exc.result
127
- error = exc.result['err'] || exc.result['errmsg']
128
- close if error && error.include?('not master')
129
- end
130
-
131
- # These get returned when connected to a local mongos router when it in turn
132
- # has connection failures talking to the remote shards. All we do is retry the same operation
133
- # since it's connections to multiple remote shards may have failed.
134
- # Disconnecting the current connection will not help since it is just to the mongos router
135
- # First make sure it is connected to the mongos router
136
- raise exc unless (OPERATION_FAILURE_ERRORS.any? { |err| exc.message.include?(err) }) || (exc.message.strip == ':')
137
-
138
- mongos_retries += 1
139
- if mongos_retries <= 60
140
- retried = true
141
- Kernel.sleep(0.5)
142
- logger.warn "[#{primary.inspect}] Router Connection Failure. Retry ##{mongos_retries}. Exc: '#{exc.message}' [#{exc.error_code}]"
143
- retry
144
- end
145
- raise exc
146
- ensure
147
- Thread.current[:mongo_ha_active?] = false
148
- end
149
- end
150
-
151
- private
152
-
153
- # Call this method whenever a Mongo::ConnectionFailure Exception
154
- # has been raised to re-establish the connection
155
- #
156
- # This method is thread-safe and ensure that only one thread at a time
157
- # per connection will attempt to re-establish the connection
158
- #
159
- # Returns whether the connection is connected again
160
- def _reconnect
161
- logger.debug 'Going to reconnect'
162
-
163
- # Prevent other threads from invoking reconnect logic at the same time
164
- @@failover_mutex.synchronize do
165
- # Another thread may have already failed over the connection by the
166
- # time this threads gets in
167
- begin
168
- ping
169
- rescue Mongo::ConnectionFailure
170
- # Connection still not available, run code below
171
- end
172
-
173
- if active?
174
- logger.info "Connected to: #{primary.inspect}"
175
- return true
176
- end
177
-
178
- if reconnect_attempts > 0
179
- # Wait for other threads to finish working on their sockets
180
- retries = 1
181
- retry_seconds = reconnect_retry_seconds
182
- begin
183
- logger.warn "Connection unavailable. Waiting: #{retry_seconds} seconds before retrying"
184
- sleep retry_seconds
185
- ping
186
- rescue Mongo::ConnectionFailure => exc
187
- if retries < reconnect_attempts
188
- retries += 1
189
- retry_seconds *= reconnect_retry_multiplier
190
- retry_seconds = reconnect_max_retry_seconds if retry_seconds > reconnect_max_retry_seconds
191
- retry
192
- end
193
-
194
- logger.error "Auto-reconnect giving up after #{retries} reconnect attempts"
195
- raise exc
196
- end
197
- logger.info "Successfully reconnected to: #{primary.inspect}"
198
- end
199
- connected?
200
- end
201
- end
202
-
203
- end
204
- end
205
- end
data/test/readme.md DELETED
@@ -1,48 +0,0 @@
1
- ## Testing
2
-
3
- Unfortunately the only way to properly test mongo_ha is to startup a console with connections
4
- active and to stop / restart the mongo servers in the replicaset as follows:
5
-
6
-
7
- #### Run the following code in a console
8
-
9
- ```ruby
10
- collection = Cache::Identity.database['test']
11
- collection.drop
12
- threads = 5.times.collect do |i|
13
- Thread.new do
14
- 100.times do |j|
15
- 1_000.times do |k|
16
- collection.insert(_id: "#{i}-#{j}-#{k}")
17
- collection.find_one(_id: "#{i}-#{j}-#{k}")
18
- puts("#{i}-#{j}-#{k}") if k % 1000 == 0
19
- end
20
- puts "#{i}-#{j} pausing"
21
- sleep 5
22
- end
23
- puts "#{i} Complete"
24
- end
25
- end
26
- ```
27
-
28
- #### Steps
29
-
30
- While running the above code in the console
31
-
32
- * Stop 1 slave server
33
-
34
- Nothing should appear in the logs and everything should process fine
35
-
36
- * Stop another slave
37
-
38
- The logs should show retries
39
-
40
- * Start up one of the 2 slaves that were stopped
41
-
42
- The processing should resume successfully
43
-
44
- #### To stop the test
45
-
46
- ```ruby
47
- threads.each(&:kill)
48
- ```