mongo 2.8.0 → 2.9.0.rc0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (276) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/Rakefile +12 -0
  5. data/lib/mongo.rb +15 -1
  6. data/lib/mongo/address/ipv6.rb +0 -2
  7. data/lib/mongo/auth/scram/conversation.rb +0 -3
  8. data/lib/mongo/bulk_write/result_combiner.rb +12 -2
  9. data/lib/mongo/client.rb +59 -6
  10. data/lib/mongo/cluster.rb +19 -8
  11. data/lib/mongo/cluster/reapers/cursor_reaper.rb +0 -2
  12. data/lib/mongo/cluster/reapers/socket_reaper.rb +12 -9
  13. data/lib/mongo/collection.rb +1 -1
  14. data/lib/mongo/collection/view/aggregation.rb +5 -1
  15. data/lib/mongo/collection/view/builder/map_reduce.rb +1 -1
  16. data/lib/mongo/collection/view/change_stream.rb +30 -10
  17. data/lib/mongo/collection/view/iterable.rb +13 -6
  18. data/lib/mongo/collection/view/map_reduce.rb +12 -10
  19. data/lib/mongo/collection/view/readable.rb +19 -14
  20. data/lib/mongo/cursor.rb +12 -8
  21. data/lib/mongo/database.rb +10 -7
  22. data/lib/mongo/database/view.rb +18 -11
  23. data/lib/mongo/error.rb +2 -2
  24. data/lib/mongo/error/connection_check_out_timeout.rb +49 -0
  25. data/lib/mongo/error/operation_failure.rb +9 -9
  26. data/lib/mongo/error/parser.rb +25 -3
  27. data/lib/mongo/error/pool_closed_error.rb +43 -0
  28. data/lib/mongo/error/sdam_error_detection.rb +18 -0
  29. data/lib/mongo/grid/file/chunk.rb +0 -2
  30. data/lib/mongo/grid/fs_bucket.rb +26 -12
  31. data/lib/mongo/grid/stream/read.rb +36 -21
  32. data/lib/mongo/index/view.rb +11 -7
  33. data/lib/mongo/logger.rb +0 -2
  34. data/lib/mongo/monitoring.rb +31 -0
  35. data/lib/mongo/monitoring/cmap_log_subscriber.rb +53 -0
  36. data/lib/mongo/monitoring/event.rb +1 -0
  37. data/lib/mongo/monitoring/event/cmap.rb +25 -0
  38. data/lib/mongo/monitoring/event/cmap/base.rb +28 -0
  39. data/lib/mongo/monitoring/event/cmap/connection_check_out_failed.rb +78 -0
  40. data/lib/mongo/monitoring/event/cmap/connection_check_out_started.rb +56 -0
  41. data/lib/mongo/monitoring/event/cmap/connection_checked_in.rb +63 -0
  42. data/lib/mongo/monitoring/event/cmap/connection_checked_out.rb +64 -0
  43. data/lib/mongo/monitoring/event/cmap/connection_closed.rb +103 -0
  44. data/lib/mongo/monitoring/event/cmap/connection_created.rb +64 -0
  45. data/lib/mongo/monitoring/event/cmap/connection_ready.rb +64 -0
  46. data/lib/mongo/monitoring/event/cmap/pool_cleared.rb +57 -0
  47. data/lib/mongo/monitoring/event/cmap/pool_closed.rb +57 -0
  48. data/lib/mongo/monitoring/event/cmap/pool_created.rb +63 -0
  49. data/lib/mongo/monitoring/event/command_started.rb +12 -3
  50. data/lib/mongo/monitoring/publishable.rb +10 -2
  51. data/lib/mongo/operation.rb +0 -1
  52. data/lib/mongo/operation/find/legacy/result.rb +1 -0
  53. data/lib/mongo/operation/list_collections/result.rb +7 -1
  54. data/lib/mongo/operation/result.rb +10 -1
  55. data/lib/mongo/operation/shared/executable.rb +15 -0
  56. data/lib/mongo/operation/shared/result/use_legacy_error_parser.rb +29 -0
  57. data/lib/mongo/operation/shared/specifiable.rb +0 -16
  58. data/lib/mongo/operation/update/legacy/result.rb +1 -0
  59. data/lib/mongo/protocol/compressed.rb +0 -2
  60. data/lib/mongo/protocol/msg.rb +25 -2
  61. data/lib/mongo/retryable.rb +171 -33
  62. data/lib/mongo/server.rb +26 -7
  63. data/lib/mongo/server/app_metadata.rb +0 -2
  64. data/lib/mongo/server/connectable.rb +8 -2
  65. data/lib/mongo/server/connection.rb +83 -13
  66. data/lib/mongo/server/connection_base.rb +1 -1
  67. data/lib/mongo/server/connection_pool.rb +439 -43
  68. data/lib/mongo/server/monitor/connection.rb +4 -1
  69. data/lib/mongo/session.rb +37 -5
  70. data/lib/mongo/session/session_pool.rb +2 -2
  71. data/lib/mongo/socket.rb +0 -2
  72. data/lib/mongo/socket/ssl.rb +0 -2
  73. data/lib/mongo/uri.rb +127 -66
  74. data/lib/mongo/uri/srv_protocol.rb +35 -13
  75. data/lib/mongo/version.rb +1 -1
  76. data/spec/README.md +190 -63
  77. data/spec/integration/change_stream_spec.rb +64 -0
  78. data/spec/integration/command_spec.rb +0 -7
  79. data/spec/integration/error_detection_spec.rb +39 -0
  80. data/spec/integration/read_concern.rb +83 -0
  81. data/spec/integration/retryable_writes_spec.rb +6 -50
  82. data/spec/integration/sdam_error_handling_spec.rb +60 -7
  83. data/spec/integration/ssl_uri_options_spec.rb +24 -0
  84. data/spec/integration/step_down_spec.rb +197 -0
  85. data/spec/lite_spec_helper.rb +4 -0
  86. data/spec/mongo/client_construction_spec.rb +42 -17
  87. data/spec/mongo/client_spec.rb +32 -1
  88. data/spec/mongo/cluster/socket_reaper_spec.rb +2 -2
  89. data/spec/mongo/cluster_spec.rb +36 -2
  90. data/spec/mongo/collection/view/aggregation_spec.rb +2 -0
  91. data/spec/mongo/collection/view/change_stream_spec.rb +28 -28
  92. data/spec/mongo/collection/view/readable_spec.rb +1 -1
  93. data/spec/mongo/collection/view_spec.rb +3 -1
  94. data/spec/mongo/cursor_spec.rb +5 -5
  95. data/spec/mongo/error/parser_spec.rb +61 -1
  96. data/spec/mongo/grid/stream/read_spec.rb +2 -2
  97. data/spec/mongo/monitoring/event/cmap/connection_check_out_failed_spec.rb +23 -0
  98. data/spec/mongo/monitoring/event/cmap/connection_check_out_started_spec.rb +19 -0
  99. data/spec/mongo/monitoring/event/cmap/connection_checked_in_spec.rb +23 -0
  100. data/spec/mongo/monitoring/event/cmap/connection_checked_out_spec.rb +23 -0
  101. data/spec/mongo/monitoring/event/cmap/connection_closed_spec.rb +27 -0
  102. data/spec/mongo/monitoring/event/cmap/connection_created_spec.rb +24 -0
  103. data/spec/mongo/monitoring/event/cmap/connection_ready_spec.rb +24 -0
  104. data/spec/mongo/monitoring/event/cmap/pool_cleared_spec.rb +19 -0
  105. data/spec/mongo/monitoring/event/cmap/pool_closed_spec.rb +19 -0
  106. data/spec/mongo/monitoring/event/cmap/pool_created_spec.rb +26 -0
  107. data/spec/mongo/operation/delete/bulk_spec.rb +1 -6
  108. data/spec/mongo/operation/delete/command_spec.rb +1 -1
  109. data/spec/mongo/operation/delete/op_msg_spec.rb +1 -1
  110. data/spec/mongo/operation/delete_spec.rb +4 -4
  111. data/spec/mongo/operation/insert/bulk_spec.rb +1 -1
  112. data/spec/mongo/operation/insert/command_spec.rb +1 -1
  113. data/spec/mongo/operation/insert/op_msg_spec.rb +1 -1
  114. data/spec/mongo/operation/update/bulk_spec.rb +1 -1
  115. data/spec/mongo/operation/update/command_spec.rb +2 -2
  116. data/spec/mongo/operation/update/op_msg_spec.rb +2 -2
  117. data/spec/mongo/protocol/msg_spec.rb +11 -0
  118. data/spec/mongo/retryable_spec.rb +78 -25
  119. data/spec/mongo/server/connection_pool_spec.rb +661 -126
  120. data/spec/mongo/server/connection_spec.rb +55 -7
  121. data/spec/mongo/server_spec.rb +5 -0
  122. data/spec/mongo/uri/srv_protocol_spec.rb +135 -2
  123. data/spec/mongo/uri_option_parsing_spec.rb +511 -0
  124. data/spec/mongo/uri_spec.rb +42 -6
  125. data/spec/spec_helper.rb +1 -84
  126. data/spec/spec_tests/cmap_spec.rb +50 -0
  127. data/spec/spec_tests/command_monitoring_spec.rb +7 -18
  128. data/spec/spec_tests/crud_spec.rb +3 -49
  129. data/spec/spec_tests/data/cmap/connection-must-have-id.yml +21 -0
  130. data/spec/spec_tests/data/cmap/connection-must-order-ids.yml +21 -0
  131. data/spec/spec_tests/data/cmap/pool-checkin-destroy-closed.yml +24 -0
  132. data/spec/spec_tests/data/cmap/pool-checkin-destroy-stale.yml +24 -0
  133. data/spec/spec_tests/data/cmap/pool-checkin-make-available.yml +21 -0
  134. data/spec/spec_tests/data/cmap/pool-checkin.yml +18 -0
  135. data/spec/spec_tests/data/cmap/pool-checkout-connection.yml +13 -0
  136. data/spec/spec_tests/data/cmap/pool-checkout-error-closed.yml +28 -0
  137. data/spec/spec_tests/data/cmap/pool-checkout-multiple.yml +34 -0
  138. data/spec/spec_tests/data/cmap/pool-checkout-no-idle.yml +31 -0
  139. data/spec/spec_tests/data/cmap/pool-checkout-no-stale.yml +29 -0
  140. data/spec/spec_tests/data/cmap/pool-close-destroy-conns.yml +26 -0
  141. data/spec/spec_tests/data/cmap/pool-close.yml +11 -0
  142. data/spec/spec_tests/data/cmap/pool-create-max-size.yml +56 -0
  143. data/spec/spec_tests/data/cmap/pool-create-min-size.yml +27 -0
  144. data/spec/spec_tests/data/cmap/pool-create-with-options.yml +20 -0
  145. data/spec/spec_tests/data/cmap/pool-create.yml +12 -0
  146. data/spec/spec_tests/data/cmap/wait-queue-fairness.yml +94 -0
  147. data/spec/spec_tests/data/cmap/wait-queue-timeout.yml +41 -0
  148. data/spec/spec_tests/data/retryable_reads/aggregate-serverErrors.yml +157 -0
  149. data/spec/spec_tests/data/retryable_reads/aggregate.yml +87 -0
  150. data/spec/spec_tests/data/retryable_reads/changeStreams-client.watch-serverErrors.yml +149 -0
  151. data/spec/spec_tests/data/retryable_reads/changeStreams-client.watch.yml +61 -0
  152. data/spec/spec_tests/data/retryable_reads/changeStreams-db.coll.watch-serverErrors.yml +149 -0
  153. data/spec/spec_tests/data/retryable_reads/changeStreams-db.coll.watch.yml +65 -0
  154. data/spec/spec_tests/data/retryable_reads/changeStreams-db.watch-serverErrors.yml +153 -0
  155. data/spec/spec_tests/data/retryable_reads/changeStreams-db.watch.yml +61 -0
  156. data/spec/spec_tests/data/retryable_reads/count-serverErrors.yml +150 -0
  157. data/spec/spec_tests/data/retryable_reads/count.yml +64 -0
  158. data/spec/spec_tests/data/retryable_reads/countDocuments-serverErrors.yml +150 -0
  159. data/spec/spec_tests/data/retryable_reads/countDocuments.yml +64 -0
  160. data/spec/spec_tests/data/retryable_reads/distinct-serverErrors.yml +156 -0
  161. data/spec/spec_tests/data/retryable_reads/distinct.yml +71 -0
  162. data/spec/spec_tests/data/retryable_reads/estimatedDocumentCount-serverErrors.yml +148 -0
  163. data/spec/spec_tests/data/retryable_reads/estimatedDocumentCount.yml +62 -0
  164. data/spec/spec_tests/data/retryable_reads/find-serverErrors.yml +160 -0
  165. data/spec/spec_tests/data/retryable_reads/find.yml +86 -0
  166. data/spec/spec_tests/data/retryable_reads/findOne-serverErrors.yml +154 -0
  167. data/spec/spec_tests/data/retryable_reads/findOne.yml +68 -0
  168. data/spec/spec_tests/data/retryable_reads/gridfs-download-serverErrors.yml +173 -0
  169. data/spec/spec_tests/data/retryable_reads/gridfs-download.yml +79 -0
  170. data/spec/spec_tests/data/retryable_reads/gridfs-downloadByName-serverErrors.yml +174 -0
  171. data/spec/spec_tests/data/retryable_reads/gridfs-downloadByName.yml +79 -0
  172. data/spec/spec_tests/data/retryable_reads/listCollectionNames-serverErrors.yml +143 -0
  173. data/spec/spec_tests/data/retryable_reads/listCollectionNames.yml +59 -0
  174. data/spec/spec_tests/data/retryable_reads/listCollectionObjects-serverErrors.yml +144 -0
  175. data/spec/spec_tests/data/retryable_reads/listCollectionObjects.yml +59 -0
  176. data/spec/spec_tests/data/retryable_reads/listCollections-serverErrors.yml +143 -0
  177. data/spec/spec_tests/data/retryable_reads/listCollections.yml +59 -0
  178. data/spec/spec_tests/data/retryable_reads/listDatabaseNames-serverErrors.yml +143 -0
  179. data/spec/spec_tests/data/retryable_reads/listDatabaseNames.yml +59 -0
  180. data/spec/spec_tests/data/retryable_reads/listDatabaseObjects-serverErrors.yml +144 -0
  181. data/spec/spec_tests/data/retryable_reads/listDatabaseObjects.yml +59 -0
  182. data/spec/spec_tests/data/retryable_reads/listDatabases-serverErrors.yml +144 -0
  183. data/spec/spec_tests/data/retryable_reads/listDatabases.yml +59 -0
  184. data/spec/spec_tests/data/retryable_reads/listIndexNames-serverErrors.yml +144 -0
  185. data/spec/spec_tests/data/retryable_reads/listIndexNames.yml +60 -0
  186. data/spec/spec_tests/data/retryable_reads/listIndexes-serverErrors.yml +145 -0
  187. data/spec/spec_tests/data/retryable_reads/listIndexes.yml +60 -0
  188. data/spec/spec_tests/data/retryable_reads/mapReduce.yml +60 -0
  189. data/spec/spec_tests/data/retryable_writes/bulkWrite-serverErrors.yml +10 -7
  190. data/spec/spec_tests/data/retryable_writes/bulkWrite.yml +15 -22
  191. data/spec/spec_tests/data/retryable_writes/deleteMany.yml +22 -0
  192. data/spec/spec_tests/data/retryable_writes/deleteOne-serverErrors.yml +8 -7
  193. data/spec/spec_tests/data/retryable_writes/deleteOne.yml +5 -8
  194. data/spec/spec_tests/data/retryable_writes/findOneAndDelete-serverErrors.yml +8 -7
  195. data/spec/spec_tests/data/retryable_writes/findOneAndDelete.yml +5 -8
  196. data/spec/spec_tests/data/retryable_writes/findOneAndReplace-serverErrors.yml +8 -7
  197. data/spec/spec_tests/data/retryable_writes/findOneAndReplace.yml +5 -8
  198. data/spec/spec_tests/data/retryable_writes/findOneAndUpdate-serverErrors.yml +8 -7
  199. data/spec/spec_tests/data/retryable_writes/findOneAndUpdate.yml +5 -8
  200. data/spec/spec_tests/data/retryable_writes/insertMany-serverErrors.yml +8 -7
  201. data/spec/spec_tests/data/retryable_writes/insertMany.yml +5 -8
  202. data/spec/spec_tests/data/retryable_writes/insertOne-serverErrors.yml +10 -45
  203. data/spec/spec_tests/data/retryable_writes/insertOne.yml +5 -8
  204. data/spec/spec_tests/data/retryable_writes/replaceOne-serverErrors.yml +8 -7
  205. data/spec/spec_tests/data/retryable_writes/replaceOne.yml +5 -8
  206. data/spec/spec_tests/data/retryable_writes/updateMany.yml +27 -0
  207. data/spec/spec_tests/data/retryable_writes/updateOne-serverErrors.yml +8 -7
  208. data/spec/spec_tests/data/retryable_writes/updateOne.yml +5 -14
  209. data/spec/spec_tests/data/transactions/abort.yml +7 -2
  210. data/spec/spec_tests/data/transactions/bulk.yml +7 -2
  211. data/spec/spec_tests/data/transactions/causal-consistency.yml +11 -4
  212. data/spec/spec_tests/data/transactions/commit.yml +11 -4
  213. data/spec/spec_tests/data/transactions/count.yml +64 -0
  214. data/spec/spec_tests/data/transactions/delete.yml +7 -2
  215. data/spec/spec_tests/data/transactions/error-labels.yml +8 -2
  216. data/spec/spec_tests/data/transactions/errors.yml +7 -2
  217. data/spec/spec_tests/data/transactions/findOneAndDelete.yml +7 -2
  218. data/spec/spec_tests/data/transactions/findOneAndReplace.yml +7 -2
  219. data/spec/spec_tests/data/transactions/findOneAndUpdate.yml +7 -2
  220. data/spec/spec_tests/data/transactions/insert.yml +9 -2
  221. data/spec/spec_tests/data/transactions/isolation.yml +7 -2
  222. data/spec/spec_tests/data/transactions/read-concern.yml +15 -6
  223. data/spec/spec_tests/data/transactions/read-pref.yml +7 -2
  224. data/spec/spec_tests/data/transactions/reads.yml +8 -48
  225. data/spec/spec_tests/data/transactions/retryable-abort.yml +7 -2
  226. data/spec/spec_tests/data/transactions/retryable-commit.yml +7 -2
  227. data/spec/spec_tests/data/transactions/retryable-writes.yml +7 -2
  228. data/spec/spec_tests/data/transactions/run-command.yml +7 -2
  229. data/spec/spec_tests/data/transactions/transaction-options.yml +7 -2
  230. data/spec/spec_tests/data/transactions/update.yml +7 -2
  231. data/spec/spec_tests/data/transactions/write-concern.yml +7 -2
  232. data/spec/spec_tests/data/transactions_api/callback-aborts.yml +6 -1
  233. data/spec/spec_tests/data/transactions_api/callback-commits.yml +6 -1
  234. data/spec/spec_tests/data/transactions_api/callback-retry.yml +6 -1
  235. data/spec/spec_tests/data/transactions_api/commit-retry.yml +6 -1
  236. data/spec/spec_tests/data/transactions_api/commit-transienttransactionerror-4.2.yml +6 -3
  237. data/spec/spec_tests/data/transactions_api/commit-transienttransactionerror.yml +6 -1
  238. data/spec/spec_tests/data/transactions_api/commit-writeconcernerror.yml +6 -1
  239. data/spec/spec_tests/data/transactions_api/commit.yml +6 -1
  240. data/spec/spec_tests/data/transactions_api/transaction-options.yml +6 -1
  241. data/spec/spec_tests/retryable_reads_spec.rb +11 -0
  242. data/spec/spec_tests/retryable_writes_spec.rb +4 -69
  243. data/spec/spec_tests/transactions_api_spec.rb +42 -37
  244. data/spec/spec_tests/transactions_spec.rb +42 -33
  245. data/spec/support/authorization.rb +12 -0
  246. data/spec/support/change_streams/operation.rb +1 -1
  247. data/spec/support/client_registry.rb +20 -0
  248. data/spec/support/cluster_config.rb +16 -15
  249. data/spec/support/cluster_tools.rb +346 -0
  250. data/spec/support/cmap.rb +367 -0
  251. data/spec/support/cmap/verifier.rb +46 -0
  252. data/spec/support/command_monitoring.rb +4 -6
  253. data/spec/support/common_shortcuts.rb +6 -0
  254. data/spec/support/connection_string.rb +2 -2
  255. data/spec/support/crud.rb +171 -184
  256. data/spec/support/crud/operation.rb +43 -0
  257. data/spec/support/crud/outcome.rb +53 -0
  258. data/spec/support/crud/read.rb +102 -12
  259. data/spec/support/crud/requirement.rb +69 -0
  260. data/spec/support/crud/spec.rb +68 -0
  261. data/spec/support/crud/test.rb +141 -0
  262. data/spec/support/crud/verifier.rb +96 -18
  263. data/spec/support/crud/write.rb +18 -3
  264. data/spec/support/event_subscriber.rb +15 -0
  265. data/spec/support/primary_socket.rb +2 -2
  266. data/spec/support/spec_config.rb +89 -20
  267. data/spec/support/transactions.rb +2 -306
  268. data/spec/support/transactions/operation.rb +7 -7
  269. data/spec/support/transactions/spec.rb +28 -0
  270. data/spec/support/transactions/test.rb +191 -0
  271. data/spec/support/utils.rb +123 -0
  272. metadata +202 -9
  273. metadata.gz.sig +0 -0
  274. data/lib/mongo/server/connection_pool/queue.rb +0 -359
  275. data/spec/mongo/server/connection_pool/queue_spec.rb +0 -353
  276. data/spec/support/transactions/verifier.rb +0 -97
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9696938332fb0fee42821edacd510e1a83217adf91e8418655d54da3c14edbc3
4
- data.tar.gz: 12bd18e5b2f36f28f8efca9d5cd03851f0ca593a3bc0e72f57404479bd837e34
3
+ metadata.gz: bdc48b8fb67060880d69ada836cc9c468abe7fd2b2e959cffaeee5edb410f68a
4
+ data.tar.gz: c8a365dac958eea89f939f78a668474f9f2e5d1ddb94e8cf772d8eeccb52d6a2
5
5
  SHA512:
6
- metadata.gz: bf455a48c0b6def9c3e509660b3eacfe8a5cba5578cfd181f621edbaeef497fbd04da6706e80edcf5a879aedd54fdc95fa3f94e2a4d3f64e311291a53cd0c5b5
7
- data.tar.gz: 290186d4c7f26812685b26543a392dc5fe82847c8f2071746ba1d1d83af44c9a6ecedc69ebd41df09f96d7950844c41f44cdd1b02c5f1cc870a84b0416479684
6
+ metadata.gz: 6984456cca54c0a352903f1336a6a5c3674eacdd6abf7f9558900cc11d33806d9c32aa7700bcc950a0a8ca973ea5c7f5ad223dbd49c267c04e68703cfb98eda3
7
+ data.tar.gz: 1ea50f9927a8bb622bf0188c1b158d00f80d35fe0fae72e81c6bac9a94775a3be3f7404b6674962db97833d3449f3bed448fff177527c853fd2e8610c78c783a
Binary file
data.tar.gz.sig CHANGED
Binary file
data/Rakefile CHANGED
@@ -33,6 +33,18 @@ namespace :spec do
33
33
  SpecSetup.new.run
34
34
  end
35
35
 
36
+ desc 'Prints configuration used by the test suite'
37
+ task :config do
38
+ $: << File.join(File.dirname(__FILE__), 'spec')
39
+
40
+ # Since this task is usually used for troubleshooting of test suite
41
+ # configuration, leave driver log level at the default of debug to
42
+ # have connection diagnostics printed during handshakes and such.
43
+ require 'support/spec_config'
44
+ require 'support/client_registry'
45
+ SpecConfig.instance.print_summary
46
+ end
47
+
36
48
  task :ci => ['spec:prepare', :spec]
37
49
  end
38
50
 
@@ -12,9 +12,23 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+ require 'base64'
15
16
  require 'forwardable'
16
- require 'bson'
17
+ require 'ipaddr'
18
+ require 'logger'
17
19
  require 'openssl'
20
+ require 'rbconfig'
21
+ require 'resolv'
22
+ require 'securerandom'
23
+ require 'set'
24
+ require 'socket'
25
+ require 'stringio'
26
+ require 'timeout'
27
+ require 'uri'
28
+ require 'zlib'
29
+
30
+ require 'bson'
31
+
18
32
  require 'mongo/id'
19
33
  require 'mongo/bson'
20
34
  require 'mongo/semaphore'
@@ -12,8 +12,6 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- require 'ipaddr'
16
-
17
15
  module Mongo
18
16
  class Address
19
17
 
@@ -12,9 +12,6 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- require 'securerandom'
16
- require 'base64'
17
-
18
15
  module Mongo
19
16
  module Auth
20
17
  class SCRAM
@@ -53,8 +53,18 @@ module Mongo
53
53
  #
54
54
  # @since 2.1.0
55
55
  def combine!(result, count)
56
- combine_counts!(result)
57
- combine_ids!(result)
56
+ # Errors can be communicated by the server in a variety of fields:
57
+ # writeError, writeErrors, writeConcernError, writeConcernErrors.
58
+ # Currently only errors given in writeConcernErrors will cause
59
+ # counts not to be added, because this behavior is covered by the
60
+ # retryable writes tests. It is possible that some or all of the
61
+ # other errors should also be excluded when combining counts and
62
+ # ids, and it is also possible that only a subset of these error
63
+ # fields is actually possible in the context of bulk writes.
64
+ unless result.write_concern_error?
65
+ combine_counts!(result)
66
+ combine_ids!(result)
67
+ end
58
68
  combine_errors!(result)
59
69
  @count += count
60
70
  end
@@ -26,7 +26,10 @@ module Mongo
26
26
  # subcomponents.
27
27
  #
28
28
  # @since 2.1.0
29
- CRUD_OPTIONS = [ :database, :read, :write, :retry_writes ].freeze
29
+ CRUD_OPTIONS = [
30
+ :database, :read, :write, :retry_reads, :retry_writes,
31
+ :max_read_retries, :read_retry_interval, :max_write_retries,
32
+ ].freeze
30
33
 
31
34
  # Valid client options.
32
35
  #
@@ -47,6 +50,7 @@ module Mongo
47
50
  :max_idle_time,
48
51
  :max_pool_size,
49
52
  :max_read_retries,
53
+ :max_write_retries,
50
54
  :min_pool_size,
51
55
  :monitoring,
52
56
  :monitoring_io,
@@ -56,6 +60,7 @@ module Mongo
56
60
  :read_concern,
57
61
  :read_retry_interval,
58
62
  :replica_set,
63
+ :retry_reads,
59
64
  :retry_writes,
60
65
  :scan,
61
66
  :sdam_proc,
@@ -170,7 +175,7 @@ module Mongo
170
175
  # # connect to the replica set if given the address of a server in
171
176
  # # a replica set
172
177
  #
173
- # @param [ Array<String>, String ] addresses_or_uri The array of server addresses in the
178
+ # @param [ Array<String> | String ] addresses_or_uri The array of server addresses in the
174
179
  # form of host:port or a MongoDB URI connection string.
175
180
  # @param [ Hash ] options The options to be used by the client.
176
181
  #
@@ -204,7 +209,9 @@ module Mongo
204
209
  # @option options [ Integer ] :max_pool_size The maximum size of the
205
210
  # connection pool.
206
211
  # @option options [ Integer ] :max_read_retries The maximum number of read
207
- # retries on mongos query failures.
212
+ # retries when legacy read retries are in use.
213
+ # @option options [ Integer ] :max_write_retries The maximum number of write
214
+ # retries when legacy write retries are in use.
208
215
  # @option options [ Integer ] :min_pool_size The minimum size of the
209
216
  # connection pool.
210
217
  # @option options [ true, false ] :monitoring If false is given, the
@@ -238,9 +245,13 @@ module Mongo
238
245
  # in which reads on a mongos are retried.
239
246
  # @option options [ Symbol ] :replica_set The name of the replica set to
240
247
  # connect to. Servers not in this replica set will be ignored.
241
- # @option options [ true, false ] :retry_writes Retry writes once when
248
+ # @option options [ true | false ] :retry_reads If true, modern retryable
249
+ # reads are enabled (which is the default). If false, modern retryable
250
+ # reads are disabled and legacy retryable reads are enabled.
251
+ # @option options [ true | false ] :retry_writes Retry writes once when
242
252
  # connected to a replica set or sharded cluster versions 3.6 and up.
243
- # @option options [ true, false ] :scan Whether to scan all seeds
253
+ # (Default is true.)
254
+ # @option options [ true | false ] :scan Whether to scan all seeds
244
255
  # in constructor. The default in driver version 2.x is to do so;
245
256
  # driver version 3.x will not scan seeds in constructor. Opt in to the
246
257
  # new behavior by setting this option to false. *Note:* setting
@@ -318,6 +329,12 @@ module Mongo
318
329
  else
319
330
  options = {}
320
331
  end
332
+ unless options[:retry_reads] == false
333
+ options[:retry_reads] = true
334
+ end
335
+ unless options[:retry_writes] == false
336
+ options[:retry_writes] = true
337
+ end
321
338
  Lint.validate_underscore_read_preference(options[:read])
322
339
  Lint.validate_read_concern_option(options[:read_concern])
323
340
  if addresses_or_uri.is_a?(::String)
@@ -357,9 +374,45 @@ module Mongo
357
374
  server_selection_semaphore: @server_selection_semaphore,
358
375
  # but need to put the database back in for auth...
359
376
  database: options[:database],
377
+
378
+ # Put these options in for legacy compatibility, but note that
379
+ # their values on the client and the cluster do not have to match -
380
+ # applications should read these values from client, not from cluster
381
+ max_read_retries: options[:max_read_retries],
382
+ read_retry_interval: options[:read_retry_interval],
360
383
  )
361
384
  end
362
385
 
386
+ # Get the maximum number of times the client can retry a read operation
387
+ # when using legacy read retries.
388
+ #
389
+ # @return [ Integer ] The maximum number of retries.
390
+ #
391
+ # @api private
392
+ def max_read_retries
393
+ options[:max_read_retries] || Cluster::MAX_READ_RETRIES
394
+ end
395
+
396
+ # Get the interval, in seconds, in which read retries when using legacy
397
+ # read retries.
398
+ #
399
+ # @return [ Float ] The interval.
400
+ #
401
+ # @api private
402
+ def read_retry_interval
403
+ options[:read_retry_interval] || Cluster::READ_RETRY_INTERVAL
404
+ end
405
+
406
+ # Get the maximum number of times the client can retry a write operation
407
+ # when using legacy write retries.
408
+ #
409
+ # @return [ Integer ] The maximum number of retries.
410
+ #
411
+ # @api private
412
+ def max_write_retries
413
+ options[:max_write_retries] || Cluster::MAX_WRITE_RETRIES
414
+ end
415
+
363
416
  # Get an inspection of the client as a string.
364
417
  #
365
418
  # @example Inspect the client.
@@ -714,7 +767,7 @@ module Mongo
714
767
 
715
768
  def validate_max_min_pool_size!(option, opts)
716
769
  if option == :min_pool_size && opts[:min_pool_size]
717
- max = opts[:max_pool_size] || Server::ConnectionPool::Queue::MAX_SIZE
770
+ max = opts[:max_pool_size] || Server::ConnectionPool::DEFAULT_MAX_SIZE
718
771
  raise Error::InvalidMinPoolSize.new(opts[:min_pool_size], max) unless opts[:min_pool_size] <= max
719
772
  end
720
773
  true
@@ -29,17 +29,18 @@ module Mongo
29
29
  include Event::Subscriber
30
30
  include Loggable
31
31
 
32
- # The default number of mongos read retries.
32
+ # The default number of legacy read retries.
33
33
  #
34
34
  # @since 2.1.1
35
35
  MAX_READ_RETRIES = 1
36
36
 
37
- # The default number of mongos write retries.
37
+ # The default number of legacy write retries.
38
38
  #
39
39
  # @since 2.4.2
40
40
  MAX_WRITE_RETRIES = 1
41
41
 
42
- # The default mongos read retry interval, in seconds.
42
+ # The default read retry interval, in seconds, when using legacy read
43
+ # retries.
43
44
  #
44
45
  # @since 2.1.1
45
46
  READ_RETRY_INTERVAL = 5
@@ -246,21 +247,30 @@ module Mongo
246
247
  :single?, :unknown?
247
248
  def_delegators :@cursor_reaper, :register_cursor, :schedule_kill_cursor, :unregister_cursor
248
249
 
249
- # Get the maximum number of times the cluster can retry a read operation on
250
- # a mongos.
250
+ # Get the maximum number of times the client can retry a read operation
251
+ # when using legacy read retries.
252
+ #
253
+ # @note max_read_retries should be retrieved from the Client instance,
254
+ # not from a Cluster instance, because clusters may be shared between
255
+ # clients with different values for max read retries.
251
256
  #
252
257
  # @example Get the max read retries.
253
258
  # cluster.max_read_retries
254
259
  #
255
- # @return [ Integer ] The maximum retries.
260
+ # @return [ Integer ] The maximum number of retries.
256
261
  #
257
262
  # @since 2.1.1
263
+ # @deprecated
258
264
  def max_read_retries
259
265
  options[:max_read_retries] || MAX_READ_RETRIES
260
266
  end
261
267
 
262
- # Get the interval, in seconds, in which a mongos read operation is
263
- # retried.
268
+ # Get the interval, in seconds, in which read retries when using legacy
269
+ # read retries.
270
+ #
271
+ # @note read_retry_interval should be retrieved from the Client instance,
272
+ # not from a Cluster instance, because clusters may be shared between
273
+ # clients with different values for the read retry interval.
264
274
  #
265
275
  # @example Get the read retry interval.
266
276
  # cluster.read_retry_interval
@@ -268,6 +278,7 @@ module Mongo
268
278
  # @return [ Float ] The interval.
269
279
  #
270
280
  # @since 2.1.1
281
+ # @deprecated
271
282
  def read_retry_interval
272
283
  options[:read_retry_interval] || READ_RETRY_INTERVAL
273
284
  end
@@ -12,8 +12,6 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- require 'set'
16
-
17
15
  module Mongo
18
16
 
19
17
  class Cluster
@@ -16,7 +16,7 @@ module Mongo
16
16
 
17
17
  class Cluster
18
18
 
19
- # A manager that calls a method on each of a cluster's pools to close stale
19
+ # A manager that calls a method on each of a cluster's pools to close idle
20
20
  # sockets.
21
21
  #
22
22
  # @api private
@@ -29,7 +29,7 @@ module Mongo
29
29
  # @example Initialize the socket reaper.
30
30
  # SocketReaper.new(cluster)
31
31
  #
32
- # @param [ Mongo::Cluster ] cluster The cluster whose pools' stale sockets
32
+ # @param [ Mongo::Cluster ] cluster The cluster whose pools' idle sockets
33
33
  # need to be reaped at regular intervals.
34
34
  #
35
35
  # @since 2.5.0
@@ -37,23 +37,26 @@ module Mongo
37
37
  @cluster = cluster
38
38
  end
39
39
 
40
- # Execute the operation to close the pool's stale sockets.
40
+ # Execute the operation to close the pool's idle sockets.
41
41
  #
42
- # @example Close the stale sockets in each of the cluster's pools.
42
+ # @example Close the idle sockets in each of the cluster's pools.
43
43
  # socket_reaper.execute
44
44
  #
45
45
  # @since 2.5.0
46
46
  def execute
47
47
  @cluster.servers.each do |server|
48
- server.pool.close_stale_sockets!
49
- end and true
48
+ server.pool.close_idle_sockets
49
+ end
50
+ true
50
51
  end
51
52
 
52
- # When the socket reaper is garbage-collected, there's no need to close stale sockets;
53
- # sockets will be closed anyway when the pools are garbage-collected.
53
+ # When the socket reaper is garbage-collected, there's no need to close
54
+ # idle sockets; sockets will be closed anyway when the pools are
55
+ # garbage collected.
54
56
  #
55
57
  # @since 2.5.0
56
- def flush; end
58
+ def flush
59
+ end
57
60
  end
58
61
  end
59
62
  end
@@ -97,7 +97,7 @@ module Mongo
97
97
  #
98
98
  # @since 2.2.0
99
99
  def read_concern
100
- options[:read_concern]
100
+ options[:read_concern] || database.read_concern
101
101
  end
102
102
 
103
103
  # Get the server selector on this collection.
@@ -110,8 +110,12 @@ module Mongo
110
110
  server.standalone? || server.mongos? || server.primary? || secondary_ok?
111
111
  end
112
112
 
113
+ def out?
114
+ pipeline.any? { |op| op.key?('$out') || op.key?(:$out) }
115
+ end
116
+
113
117
  def secondary_ok?
114
- pipeline.none? { |op| op.key?('$out') || op.key?(:$out) }
118
+ !out?
115
119
  end
116
120
 
117
121
  def send_initial_query(server, session)
@@ -133,7 +133,7 @@ module Mongo
133
133
 
134
134
  def map_reduce_command
135
135
  command = BSON::Document.new(
136
- :mapreduce => collection.name,
136
+ :mapReduce => collection.name,
137
137
  :map => map,
138
138
  :reduce => reduce,
139
139
  :query => filter,
@@ -81,6 +81,12 @@ module Mongo
81
81
  # return changes that occurred at or after the specified timestamp. Any
82
82
  # command run against the server will return a cluster time that can
83
83
  # be used here. Only recognized by server versions 4.0+.
84
+ # @option options [ Bson::Document, Hash ] :start_after Similar to :resume_after, this
85
+ # option takes a resume token and starts a new change stream returning the first
86
+ # notification after the token. This will allow users to watch collections that have been
87
+ # dropped and recreated or newly renamed collections without missing any notifications.
88
+ #
89
+ # The server will report an error if `startAfter` and `resumeAfter` are both specified.
84
90
  #
85
91
  # @since 2.5.0
86
92
  def initialize(view, pipeline, changes_for, options = {})
@@ -89,6 +95,7 @@ module Mongo
89
95
  @change_stream_filters = pipeline && pipeline.dup
90
96
  @options = options && options.dup.freeze
91
97
  @resume_token = @options[:resume_after]
98
+ @start_after = @options[:start_after]
92
99
  create_cursor!
93
100
 
94
101
  # We send different parameters when we resume a change stream
@@ -263,17 +270,22 @@ module Mongo
263
270
  @start_at_operation_time_supported = nil
264
271
 
265
272
  session = client.send(:get_session, @options)
266
- server = server_selector.select_server(cluster)
267
- result = send_initial_query(server, session)
268
- if doc = result.replies.first && result.replies.first.documents.first
269
- @start_at_operation_time = doc['operationTime']
270
- else
271
- # The above may set @start_at_operation_time to nil
272
- # if it was not in the document for some reason,
273
- # for consistency set it to nil here as well
274
- @start_at_operation_time = nil
273
+ start_at_operation_time = nil
274
+ @cursor = read_with_retry_cursor(session, server_selector, view) do |server|
275
+ result = send_initial_query(server, session)
276
+ if doc = result.replies.first && result.replies.first.documents.first
277
+ start_at_operation_time = doc['operationTime']
278
+ else
279
+ # The above may set @start_at_operation_time to nil
280
+ # if it was not in the document for some reason,
281
+ # for consistency set it to nil here as well.
282
+ # NB: since this block may be executed more than once, each
283
+ # execution must write to start_at_operation_time either way.
284
+ start_at_operation_time = nil
285
+ end
286
+ result
275
287
  end
276
- @cursor = Cursor.new(view, result, server, disable_retry: true, session: session)
288
+ @start_at_operation_time = start_at_operation_time
277
289
  end
278
290
 
279
291
  def pipeline
@@ -296,6 +308,10 @@ module Mongo
296
308
  if @resume_token
297
309
  # Spec says we need to remove startAtOperationTime if
298
310
  # one was passed in by user, thus we won't forward it
311
+ elsif @start_after
312
+ # The spec says to set `resumeAfter` to the `startAfter` token and not to send
313
+ # either `startAfter` or `startAtOperationTime`.
314
+ @resume_token = @start_after
299
315
  elsif start_at_operation_time_supported? && @start_at_operation_time
300
316
  # It is crucial to check @start_at_operation_time_supported
301
317
  # here - we may have switched to an older server that
@@ -309,6 +325,10 @@ module Mongo
309
325
  raise Mongo::Error::MissingResumeToken
310
326
  end
311
327
  else
328
+ if @start_after
329
+ doc[:startAfter] = @start_after
330
+ end
331
+
312
332
  if options[:start_at_operation_time]
313
333
  doc[:startAtOperationTime] = time_to_bson_timestamp(
314
334
  options[:start_at_operation_time])