mongo 2.14.0 → 2.15.0.alpha

Sign up to get free protection for your applications and to get access to all the features.
Files changed (230) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/README.md +4 -1
  5. data/Rakefile +8 -15
  6. data/lib/mongo.rb +23 -0
  7. data/lib/mongo/auth/aws/conversation.rb +1 -4
  8. data/lib/mongo/auth/base.rb +13 -7
  9. data/lib/mongo/auth/conversation_base.rb +32 -0
  10. data/lib/mongo/auth/cr/conversation.rb +6 -29
  11. data/lib/mongo/auth/gssapi/conversation.rb +4 -15
  12. data/lib/mongo/auth/ldap/conversation.rb +3 -14
  13. data/lib/mongo/auth/sasl_conversation_base.rb +1 -13
  14. data/lib/mongo/auth/scram_conversation_base.rb +7 -34
  15. data/lib/mongo/auth/user/view.rb +16 -9
  16. data/lib/mongo/auth/x509/conversation.rb +4 -25
  17. data/lib/mongo/background_thread.rb +11 -0
  18. data/lib/mongo/bulk_write.rb +21 -18
  19. data/lib/mongo/client.rb +82 -6
  20. data/lib/mongo/cluster.rb +19 -28
  21. data/lib/mongo/cluster/reapers/cursor_reaper.rb +6 -2
  22. data/lib/mongo/cluster/sdam_flow.rb +14 -0
  23. data/lib/mongo/collection.rb +8 -6
  24. data/lib/mongo/collection/view/aggregation.rb +1 -1
  25. data/lib/mongo/collection/view/change_stream.rb +1 -1
  26. data/lib/mongo/collection/view/iterable.rb +1 -1
  27. data/lib/mongo/collection/view/map_reduce.rb +2 -2
  28. data/lib/mongo/collection/view/readable.rb +42 -20
  29. data/lib/mongo/collection/view/writable.rb +14 -14
  30. data/lib/mongo/cursor.rb +2 -2
  31. data/lib/mongo/database.rb +22 -5
  32. data/lib/mongo/database/view.rb +1 -1
  33. data/lib/mongo/error.rb +9 -1
  34. data/lib/mongo/error/bulk_write_error.rb +17 -3
  35. data/lib/mongo/error/internal_driver_error.rb +22 -0
  36. data/lib/mongo/error/operation_failure.rb +21 -2
  37. data/lib/mongo/error/parser.rb +65 -12
  38. data/lib/mongo/error/server_api_conflict.rb +23 -0
  39. data/lib/mongo/error/server_api_not_supported.rb +24 -0
  40. data/lib/mongo/error/unmet_dependency.rb +21 -0
  41. data/lib/mongo/grid/fs_bucket.rb +37 -37
  42. data/lib/mongo/index/view.rb +21 -11
  43. data/lib/mongo/monitoring.rb +13 -4
  44. data/lib/mongo/monitoring/event/server_heartbeat_failed.rb +27 -16
  45. data/lib/mongo/monitoring/event/server_heartbeat_succeeded.rb +26 -15
  46. data/lib/mongo/operation.rb +2 -2
  47. data/lib/mongo/operation/collections_info.rb +18 -1
  48. data/lib/mongo/operation/collections_info/command.rb +2 -2
  49. data/lib/mongo/operation/context.rb +99 -0
  50. data/lib/mongo/operation/indexes.rb +15 -1
  51. data/lib/mongo/operation/insert/command.rb +2 -2
  52. data/lib/mongo/operation/insert/legacy.rb +2 -2
  53. data/lib/mongo/operation/insert/op_msg.rb +2 -2
  54. data/lib/mongo/operation/list_collections/result.rb +4 -1
  55. data/lib/mongo/operation/result.rb +2 -0
  56. data/lib/mongo/operation/shared/executable.rb +24 -14
  57. data/lib/mongo/operation/shared/executable_no_validate.rb +2 -2
  58. data/lib/mongo/operation/shared/op_msg_or_command.rb +1 -7
  59. data/lib/mongo/operation/shared/op_msg_or_find_command.rb +1 -7
  60. data/lib/mongo/operation/shared/polymorphic_operation.rb +39 -0
  61. data/lib/mongo/operation/shared/response_handling.rb +23 -23
  62. data/lib/mongo/operation/shared/sessions_supported.rb +13 -2
  63. data/lib/mongo/operation/shared/write.rb +8 -18
  64. data/lib/mongo/protocol/compressed.rb +51 -5
  65. data/lib/mongo/protocol/message.rb +20 -2
  66. data/lib/mongo/protocol/msg.rb +36 -11
  67. data/lib/mongo/query_cache.rb +30 -0
  68. data/lib/mongo/retryable.rb +1 -1
  69. data/lib/mongo/server.rb +7 -15
  70. data/lib/mongo/server/app_metadata.rb +52 -18
  71. data/lib/mongo/server/connection.rb +5 -0
  72. data/lib/mongo/server/connection_base.rb +13 -10
  73. data/lib/mongo/server/connection_pool.rb +6 -4
  74. data/lib/mongo/server/description.rb +4 -0
  75. data/lib/mongo/server/description/features.rb +9 -8
  76. data/lib/mongo/server/monitor.rb +20 -1
  77. data/lib/mongo/server/monitor/app_metadata.rb +1 -1
  78. data/lib/mongo/server/monitor/connection.rb +9 -10
  79. data/lib/mongo/server/pending_connection.rb +24 -6
  80. data/lib/mongo/server/push_monitor.rb +11 -1
  81. data/lib/mongo/session.rb +2 -2
  82. data/lib/mongo/session/session_pool.rb +4 -2
  83. data/lib/mongo/socket.rb +29 -4
  84. data/lib/mongo/socket/ssl.rb +8 -0
  85. data/lib/mongo/srv/monitor.rb +0 -11
  86. data/lib/mongo/uri/options_mapper.rb +38 -0
  87. data/lib/mongo/utils.rb +15 -0
  88. data/lib/mongo/version.rb +1 -1
  89. data/spec/README.md +24 -1
  90. data/spec/integration/auth_spec.rb +25 -15
  91. data/spec/integration/bulk_write_error_message_spec.rb +41 -0
  92. data/spec/integration/change_stream_spec.rb +4 -4
  93. data/spec/integration/command_monitoring_spec.rb +2 -2
  94. data/spec/integration/connection_spec.rb +2 -0
  95. data/spec/integration/docs_examples_spec.rb +8 -1
  96. data/spec/integration/fork_reconnect_spec.rb +4 -1
  97. data/spec/integration/ocsp_verifier_spec.rb +13 -7
  98. data/spec/integration/operation_failure_code_spec.rb +1 -1
  99. data/spec/integration/operation_failure_message_spec.rb +90 -0
  100. data/spec/integration/reconnect_spec.rb +1 -1
  101. data/spec/integration/sdam_error_handling_spec.rb +1 -1
  102. data/spec/integration/sdam_events_spec.rb +3 -5
  103. data/spec/integration/snappy_compression_spec.rb +25 -0
  104. data/spec/integration/srv_monitoring_spec.rb +1 -1
  105. data/spec/integration/transactions_examples_spec.rb +6 -0
  106. data/spec/integration/zlib_compression_spec.rb +1 -1
  107. data/spec/integration/zstd_compression_spec.rb +26 -0
  108. data/spec/lite_spec_helper.rb +7 -1
  109. data/spec/mongo/address_spec.rb +15 -11
  110. data/spec/mongo/auth/ldap/conversation_spec.rb +1 -1
  111. data/spec/mongo/auth/ldap_spec.rb +5 -1
  112. data/spec/mongo/auth/scram_negotiation_spec.rb +1 -1
  113. data/spec/mongo/auth/scram_spec.rb +1 -1
  114. data/spec/mongo/auth/x509/conversation_spec.rb +3 -3
  115. data/spec/mongo/client_construction_spec.rb +207 -33
  116. data/spec/mongo/client_spec.rb +17 -0
  117. data/spec/mongo/cluster_spec.rb +3 -18
  118. data/spec/mongo/collection/view/explainable_spec.rb +1 -1
  119. data/spec/mongo/collection/view/readable_spec.rb +33 -19
  120. data/spec/mongo/collection_crud_spec.rb +4357 -0
  121. data/spec/mongo/collection_ddl_spec.rb +534 -0
  122. data/spec/mongo/collection_spec.rb +5 -4859
  123. data/spec/mongo/database_spec.rb +66 -4
  124. data/spec/mongo/error/bulk_write_error_spec.rb +3 -3
  125. data/spec/mongo/error/parser_spec.rb +37 -6
  126. data/spec/mongo/index/view_spec.rb +8 -2
  127. data/spec/mongo/monitoring/event/server_heartbeat_failed_spec.rb +1 -1
  128. data/spec/mongo/monitoring/event/server_heartbeat_succeeded_spec.rb +1 -1
  129. data/spec/mongo/operation/aggregate_spec.rb +2 -1
  130. data/spec/mongo/operation/collections_info_spec.rb +4 -1
  131. data/spec/mongo/operation/command_spec.rb +6 -3
  132. data/spec/mongo/operation/create_index_spec.rb +6 -3
  133. data/spec/mongo/operation/create_user_spec.rb +6 -3
  134. data/spec/mongo/operation/delete/bulk_spec.rb +9 -6
  135. data/spec/mongo/operation/delete_spec.rb +11 -7
  136. data/spec/mongo/operation/drop_index_spec.rb +6 -2
  137. data/spec/mongo/operation/find/legacy_spec.rb +3 -1
  138. data/spec/mongo/operation/get_more_spec.rb +3 -1
  139. data/spec/mongo/operation/indexes_spec.rb +5 -1
  140. data/spec/mongo/operation/insert/bulk_spec.rb +10 -7
  141. data/spec/mongo/operation/insert_spec.rb +15 -12
  142. data/spec/mongo/operation/map_reduce_spec.rb +5 -2
  143. data/spec/mongo/operation/remove_user_spec.rb +6 -3
  144. data/spec/mongo/operation/result_spec.rb +1 -1
  145. data/spec/mongo/operation/update/bulk_spec.rb +9 -6
  146. data/spec/mongo/operation/update_spec.rb +10 -7
  147. data/spec/mongo/operation/update_user_spec.rb +4 -1
  148. data/spec/mongo/protocol/compressed_spec.rb +26 -12
  149. data/spec/mongo/query_cache_middleware_spec.rb +55 -0
  150. data/spec/mongo/retryable_spec.rb +3 -2
  151. data/spec/mongo/server/app_metadata_spec.rb +2 -0
  152. data/spec/mongo/server/connection_pool/populator_spec.rb +3 -1
  153. data/spec/mongo/server/connection_pool_spec.rb +1 -1
  154. data/spec/mongo/server/connection_spec.rb +24 -17
  155. data/spec/mongo/server/monitor/connection_spec.rb +17 -7
  156. data/spec/mongo/server/monitor_spec.rb +9 -1
  157. data/spec/mongo/server_spec.rb +15 -2
  158. data/spec/mongo/socket/ssl_spec.rb +40 -0
  159. data/spec/mongo/socket_spec.rb +2 -2
  160. data/spec/mongo/tls_context_hooks_spec.rb +37 -0
  161. data/spec/runners/connection_string.rb +0 -4
  162. data/spec/runners/crud/requirement.rb +40 -3
  163. data/spec/runners/crud/verifier.rb +8 -0
  164. data/spec/runners/transactions/operation.rb +13 -2
  165. data/spec/runners/transactions/test.rb +1 -0
  166. data/spec/runners/unified.rb +96 -0
  167. data/spec/runners/unified/assertions.rb +249 -0
  168. data/spec/runners/unified/change_stream_operations.rb +26 -0
  169. data/spec/runners/unified/crud_operations.rb +199 -0
  170. data/spec/runners/unified/ddl_operations.rb +96 -0
  171. data/spec/runners/unified/entity_map.rb +39 -0
  172. data/spec/runners/unified/error.rb +25 -0
  173. data/spec/runners/unified/event_subscriber.rb +91 -0
  174. data/spec/runners/unified/exceptions.rb +21 -0
  175. data/spec/runners/unified/grid_fs_operations.rb +55 -0
  176. data/spec/runners/unified/support_operations.rb +250 -0
  177. data/spec/runners/unified/test.rb +393 -0
  178. data/spec/runners/unified/test_group.rb +28 -0
  179. data/spec/runners/unified/using_hash.rb +31 -0
  180. data/spec/shared/bin/get-mongodb-download-url +17 -0
  181. data/spec/shared/lib/mrss/cluster_config.rb +218 -0
  182. data/spec/shared/lib/mrss/constraints.rb +43 -0
  183. data/spec/shared/lib/mrss/docker_runner.rb +262 -0
  184. data/spec/shared/lib/mrss/server_version_registry.rb +112 -0
  185. data/spec/shared/lib/mrss/utils.rb +15 -0
  186. data/spec/shared/share/Dockerfile.erb +231 -0
  187. data/spec/shared/shlib/distro.sh +73 -0
  188. data/spec/shared/shlib/server.sh +290 -0
  189. data/spec/shared/shlib/set_env.sh +128 -0
  190. data/spec/solo/clean_exit_spec.rb +21 -0
  191. data/spec/spec_helper.rb +4 -1
  192. data/spec/spec_tests/crud_unified_spec.rb +10 -0
  193. data/spec/spec_tests/data/change_streams/change-streams.yml +0 -1
  194. data/spec/spec_tests/data/crud_unified/estimatedDocumentCount.yml +267 -0
  195. data/spec/spec_tests/data/retryable_reads/estimatedDocumentCount-4.9.yml +60 -0
  196. data/spec/spec_tests/data/retryable_reads/{estimatedDocumentCount.yml → estimatedDocumentCount-pre4.9.yml} +2 -0
  197. data/spec/spec_tests/data/retryable_reads/estimatedDocumentCount-serverErrors-4.9.yml +146 -0
  198. data/spec/spec_tests/data/retryable_reads/{estimatedDocumentCount-serverErrors.yml → estimatedDocumentCount-serverErrors-pre4.9.yml} +2 -0
  199. data/spec/spec_tests/data/retryable_reads/listIndexNames.yml +1 -1
  200. data/spec/spec_tests/data/unified/valid-fail/operation-failure.yml +31 -0
  201. data/spec/spec_tests/data/unified/valid-pass/poc-change-streams.yml +220 -0
  202. data/spec/spec_tests/data/unified/valid-pass/poc-command-monitoring.yml +102 -0
  203. data/spec/spec_tests/data/unified/valid-pass/poc-crud.yml +184 -0
  204. data/spec/spec_tests/data/unified/valid-pass/poc-gridfs.yml +155 -0
  205. data/spec/spec_tests/data/unified/valid-pass/poc-retryable-reads.yml +193 -0
  206. data/spec/spec_tests/data/unified/valid-pass/poc-retryable-writes.yml +210 -0
  207. data/spec/spec_tests/data/unified/valid-pass/poc-sessions.yml +215 -0
  208. data/spec/spec_tests/data/unified/valid-pass/poc-transactions-convenient-api.yml +235 -0
  209. data/spec/spec_tests/data/unified/valid-pass/poc-transactions-mongos-pin-auto.yml +169 -0
  210. data/spec/spec_tests/data/unified/valid-pass/poc-transactions.yml +170 -0
  211. data/spec/spec_tests/data/uri_options/compression-options.yml +1 -1
  212. data/spec/spec_tests/data/versioned_api/crud-api-version-1-strict.yml +416 -0
  213. data/spec/spec_tests/data/versioned_api/crud-api-version-1.yml +409 -0
  214. data/spec/spec_tests/data/versioned_api/runcommand-helper-no-api-version-declared.yml +67 -0
  215. data/spec/spec_tests/data/versioned_api/test-commands-deprecation-errors.yml +47 -0
  216. data/spec/spec_tests/data/versioned_api/test-commands-strict-mode.yml +44 -0
  217. data/spec/spec_tests/data/versioned_api/transaction-handling.yml +180 -0
  218. data/spec/spec_tests/unified_spec.rb +15 -0
  219. data/spec/spec_tests/uri_options_spec.rb +16 -0
  220. data/spec/spec_tests/versioned_api_spec.rb +10 -0
  221. data/spec/support/common_shortcuts.rb +15 -1
  222. data/spec/support/shared/session.rb +2 -2
  223. data/spec/support/spec_config.rb +46 -3
  224. data/spec/support/spec_setup.rb +48 -38
  225. data/spec/support/utils.rb +64 -3
  226. metadata +1104 -992
  227. metadata.gz.sig +0 -0
  228. data/lib/mongo/operation/shared/collections_info_or_list_collections.rb +0 -58
  229. data/lib/mongo/operation/shared/op_msg_or_list_indexes_command.rb +0 -47
  230. data/spec/support/cluster_config.rb +0 -207
@@ -34,34 +34,13 @@ module Mongo
34
34
  # @param [ Server::Connection ] connection The connection being
35
35
  # authenticated.
36
36
  #
37
- # @return [ Protocol::Query ] The first X.509 conversation message.
37
+ # @return [ Protocol::Message ] The first X.509 conversation message.
38
38
  #
39
39
  # @since 2.0.0
40
40
  def start(connection)
41
- login = client_first_document
42
- if connection && connection.features.op_msg_enabled?
43
- selector = login
44
- # The only valid database for X.509 authentication is $external.
45
- if user.auth_source != '$external'
46
- user_name_msg = if user.name
47
- " #{user.name}"
48
- else
49
- ''
50
- end
51
- raise Auth::InvalidConfiguration, "User#{user_name_msg} specifies auth source '#{user.auth_source}', but the only valid auth source for X.509 is '$external'"
52
- end
53
- selector[Protocol::Msg::DATABASE_IDENTIFIER] = '$external'
54
- cluster_time = connection.mongos? && connection.cluster_time
55
- selector[Operation::CLUSTER_TIME] = cluster_time if cluster_time
56
- Protocol::Msg.new([], {}, selector)
57
- else
58
- Protocol::Query.new(
59
- Auth::EXTERNAL,
60
- Database::COMMAND,
61
- login,
62
- limit: -1
63
- )
64
- end
41
+ validate_external_auth_source
42
+ selector = client_first_document
43
+ build_message(connection, '$external', selector)
65
44
  end
66
45
 
67
46
  # Returns the hash to provide to the server in the handshake
@@ -20,6 +20,17 @@ module Mongo
20
20
  # compatibility reasons. However using these methods outside of the driver
21
21
  # is deprecated.
22
22
  #
23
+ # @note Do not start or stop background threads in finalizers. See
24
+ # https://jira.mongodb.org/browse/RUBY-2453 and
25
+ # https://bugs.ruby-lang.org/issues/16288. When interpreter exits,
26
+ # background threads are stopped first and finalizers are invoked next,
27
+ # and MRI's internal data structures are basically corrupt at this point
28
+ # if threads are being referenced. Prior to interpreter shutdown this
29
+ # means threads cannot be stopped by objects going out of scope, but
30
+ # most likely the threads hold references to said objects anyway if
31
+ # work is being performed thus the objects wouldn't go out of scope in
32
+ # the first place.
33
+ #
23
34
  # @api private
24
35
  module BackgroundThread
25
36
  include Loggable
@@ -58,6 +58,7 @@ module Mongo
58
58
  operations = op_combiner.combine
59
59
 
60
60
  client.send(:with_session, @options) do |session|
61
+ context = Operation::Context.new(client: client, session: session)
61
62
  operations.each do |operation|
62
63
  if single_statement?(operation)
63
64
  write_concern = write_concern(session)
@@ -67,6 +68,7 @@ module Mongo
67
68
  operation.keys.first,
68
69
  operation.values.flatten,
69
70
  connection,
71
+ context,
70
72
  operation_id,
71
73
  result_combiner,
72
74
  session,
@@ -80,6 +82,7 @@ module Mongo
80
82
  operation.keys.first,
81
83
  operation.values.flatten,
82
84
  connection,
85
+ context,
83
86
  operation_id,
84
87
  result_combiner,
85
88
  session)
@@ -177,19 +180,19 @@ module Mongo
177
180
  }
178
181
  end
179
182
 
180
- def execute_operation(name, values, connection, operation_id, result_combiner, session, txn_num = nil)
183
+ def execute_operation(name, values, connection, context, operation_id, result_combiner, session, txn_num = nil)
181
184
  validate_collation!(connection)
182
185
  validate_array_filters!(connection)
183
186
  validate_hint!(connection)
184
187
 
185
188
  unpin_maybe(session) do
186
189
  if values.size > connection.description.max_write_batch_size
187
- split_execute(name, values, connection, operation_id, result_combiner, session, txn_num)
190
+ split_execute(name, values, connection, context, operation_id, result_combiner, session, txn_num)
188
191
  else
189
- result = send(name, values, connection, operation_id, session, txn_num)
192
+ result = send(name, values, connection, context, operation_id, session, txn_num)
190
193
 
191
194
  add_server_diagnostics(connection) do
192
- add_error_labels(client, connection, session) do
195
+ add_error_labels(connection, context) do
193
196
  result_combiner.combine!(result, values.size)
194
197
  end
195
198
  end
@@ -205,7 +208,7 @@ module Mongo
205
208
  rescue Error::MaxBSONSize, Error::MaxMessageSize => e
206
209
  raise e if values.size <= 1
207
210
  unpin_maybe(session) do
208
- split_execute(name, values, connection, operation_id, result_combiner, session, txn_num)
211
+ split_execute(name, values, connection, context, operation_id, result_combiner, session, txn_num)
209
212
  end
210
213
  end
211
214
 
@@ -213,47 +216,47 @@ module Mongo
213
216
  @op_combiner ||= ordered? ? OrderedCombiner.new(requests) : UnorderedCombiner.new(requests)
214
217
  end
215
218
 
216
- def split_execute(name, values, connection, operation_id, result_combiner, session, txn_num)
217
- execute_operation(name, values.shift(values.size / 2), connection, operation_id, result_combiner, session, txn_num)
219
+ def split_execute(name, values, connection, context, operation_id, result_combiner, session, txn_num)
220
+ execute_operation(name, values.shift(values.size / 2), connection, context, operation_id, result_combiner, session, txn_num)
218
221
 
219
222
  txn_num = session.next_txn_num if txn_num
220
- execute_operation(name, values, connection, operation_id, result_combiner, session, txn_num)
223
+ execute_operation(name, values, connection, context, operation_id, result_combiner, session, txn_num)
221
224
  end
222
225
 
223
- def delete_one(documents, connection, operation_id, session, txn_num)
226
+ def delete_one(documents, connection, context, operation_id, session, txn_num)
224
227
  QueryCache.clear_namespace(collection.namespace)
225
228
 
226
229
  spec = base_spec(operation_id, session).merge(:deletes => documents, :txn_num => txn_num)
227
- Operation::Delete.new(spec).bulk_execute(connection, client: client)
230
+ Operation::Delete.new(spec).bulk_execute(connection, context: context)
228
231
  end
229
232
 
230
- def delete_many(documents, connection, operation_id, session, txn_num)
233
+ def delete_many(documents, connection, context, operation_id, session, txn_num)
231
234
  QueryCache.clear_namespace(collection.namespace)
232
235
 
233
236
  spec = base_spec(operation_id, session).merge(:deletes => documents)
234
- Operation::Delete.new(spec).bulk_execute(connection, client: client)
237
+ Operation::Delete.new(spec).bulk_execute(connection, context: context)
235
238
  end
236
239
 
237
- def insert_one(documents, connection, operation_id, session, txn_num)
240
+ def insert_one(documents, connection, context, operation_id, session, txn_num)
238
241
  QueryCache.clear_namespace(collection.namespace)
239
242
 
240
243
  spec = base_spec(operation_id, session).merge(:documents => documents, :txn_num => txn_num)
241
- Operation::Insert.new(spec).bulk_execute(connection, client: client)
244
+ Operation::Insert.new(spec).bulk_execute(connection, context: context)
242
245
  end
243
246
 
244
- def update_one(documents, connection, operation_id, session, txn_num)
247
+ def update_one(documents, connection, context, operation_id, session, txn_num)
245
248
  QueryCache.clear_namespace(collection.namespace)
246
249
 
247
250
  spec = base_spec(operation_id, session).merge(:updates => documents, :txn_num => txn_num)
248
- Operation::Update.new(spec).bulk_execute(connection, client: client)
251
+ Operation::Update.new(spec).bulk_execute(connection, context: context)
249
252
  end
250
253
  alias :replace_one :update_one
251
254
 
252
- def update_many(documents, connection, operation_id, session, txn_num)
255
+ def update_many(documents, connection, context, operation_id, session, txn_num)
253
256
  QueryCache.clear_namespace(collection.namespace)
254
257
 
255
258
  spec = base_spec(operation_id, session).merge(:updates => documents)
256
- Operation::Update.new(spec).bulk_execute(connection, client: client)
259
+ Operation::Update.new(spec).bulk_execute(connection, context: context)
257
260
  end
258
261
 
259
262
  private
data/lib/mongo/client.rb CHANGED
@@ -85,6 +85,7 @@ module Mongo
85
85
  :retry_writes,
86
86
  :scan,
87
87
  :sdam_proc,
88
+ :server_api,
88
89
  :server_selection_timeout,
89
90
  :socket_timeout,
90
91
  :ssl,
@@ -114,7 +115,16 @@ module Mongo
114
115
  # The compression algorithms supported by the driver.
115
116
  #
116
117
  # @since 2.5.0
117
- VALID_COMPRESSORS = [ Mongo::Protocol::Compressed::ZLIB ].freeze
118
+ VALID_COMPRESSORS = [
119
+ Mongo::Protocol::Compressed::ZSTD,
120
+ Mongo::Protocol::Compressed::SNAPPY,
121
+ Mongo::Protocol::Compressed::ZLIB
122
+ ].freeze
123
+
124
+ # The known server API versions.
125
+ VALID_SERVER_API_VERSIONS = %w(
126
+ 1
127
+ ).freeze
118
128
 
119
129
  # @return [ Mongo::Cluster ] cluster The cluster of servers for the client.
120
130
  attr_reader :cluster
@@ -223,7 +233,7 @@ module Mongo
223
233
  # @option options [ Array<String> ] :compressors A list of potential
224
234
  # compressors to use, in order of preference. The driver chooses the
225
235
  # first compressor that is also supported by the server. Currently the
226
- # driver only supports 'zlib'.
236
+ # driver only supports 'zstd, 'snappy' and 'zlib'.
227
237
  # @option options [ true | false ] :direct_connection Whether to connect
228
238
  # directly to the specified seed, bypassing topology discovery. Exactly
229
239
  # one seed must be provided.
@@ -312,6 +322,11 @@ module Mongo
312
322
  # in particular the cluster is nil at this time. sdam_proc should
313
323
  # limit itself to calling #subscribe and #unsubscribe methods on the
314
324
  # client only.
325
+ # @option options [ Hash ] :server_api The requested server API version.
326
+ # This hash can have the following items:
327
+ # - *:version* -- string
328
+ # - *:strict* -- boolean
329
+ # - *:deprecation_errors* -- boolean
315
330
  # @option options [ Integer ] :server_selection_timeout The timeout in seconds
316
331
  # for selecting a server for an operation.
317
332
  # @option options [ Float ] :socket_timeout The timeout, in seconds, to
@@ -465,6 +480,18 @@ module Mongo
465
480
 
466
481
  options = self.class.canonicalize_ruby_options(options)
467
482
 
483
+ # The server API version is specified to be a string.
484
+ # However, it is very annoying to always provide the number 1 as a string,
485
+ # therefore cast to the string type here.
486
+ if server_api = options[:server_api]
487
+ if server_api.is_a?(Hash)
488
+ server_api = Options::Redacted.new(server_api)
489
+ if (version = server_api[:version]).is_a?(Integer)
490
+ options[:server_api] = server_api.merge(version: version.to_s)
491
+ end
492
+ end
493
+ end
494
+
468
495
  # Special handling for sdam_proc as it is only used during client
469
496
  # construction
470
497
  sdam_proc = options.delete(:sdam_proc)
@@ -499,7 +526,9 @@ module Mongo
499
526
  validate_options!(addresses)
500
527
  validate_authentication_options!
501
528
 
502
- @database = Database.new(self, @options[:database], @options)
529
+ database_options = @options.dup
530
+ database_options.delete(:server_api)
531
+ @database = Database.new(self, @options[:database], database_options)
503
532
 
504
533
  # Temporarily set monitoring so that event subscriptions can be
505
534
  # set up without there being a cluster
@@ -697,9 +726,9 @@ module Mongo
697
726
  # @return [ Mongo::Client ] A new client instance.
698
727
  #
699
728
  # @since 2.0.0
700
- def with(new_options = Options::Redacted.new)
729
+ def with(new_options = nil)
701
730
  clone.tap do |client|
702
- opts = client.update_options(new_options)
731
+ opts = client.update_options(new_options || Options::Redacted.new)
703
732
  Database.create(client)
704
733
  # We can't use the same cluster if some options that would affect it
705
734
  # have changed.
@@ -726,6 +755,8 @@ module Mongo
726
755
  def update_options(new_options)
727
756
  old_options = @options
728
757
 
758
+ new_options = self.class.canonicalize_ruby_options(new_options || {})
759
+
729
760
  validate_new_options!(new_options).tap do |opts|
730
761
  # Our options are frozen
731
762
  options = @options.dup
@@ -1127,7 +1158,7 @@ module Mongo
1127
1158
  # The argument may contain a subset of options that the client will
1128
1159
  # eventually have; this method validates each of the provided options
1129
1160
  # but does not check for interactions between combinations of options.
1130
- def validate_new_options!(opts = Options::Redacted.new)
1161
+ def validate_new_options!(opts)
1131
1162
  return Options::Redacted.new unless opts
1132
1163
  if opts[:read_concern]
1133
1164
  # Raise an error for non user-settable options
@@ -1146,6 +1177,23 @@ module Mongo
1146
1177
  end
1147
1178
  end
1148
1179
 
1180
+ if server_api = opts[:server_api]
1181
+ unless server_api.is_a?(Hash)
1182
+ raise ArgumentError, ":server_api value must be a hash: #{server_api}"
1183
+ end
1184
+
1185
+ extra_keys = server_api.keys - %w(version strict deprecation_errors)
1186
+ unless extra_keys.empty?
1187
+ raise ArgumentError, "Unknown keys under :server_api: #{extra_keys.map(&:inspect).join(', ')}"
1188
+ end
1189
+
1190
+ if version = server_api[:version]
1191
+ unless VALID_SERVER_API_VERSIONS.include?(version)
1192
+ raise ArgumentError, "Unknown server API version: #{version}"
1193
+ end
1194
+ end
1195
+ end
1196
+
1149
1197
  Lint.validate_underscore_read_preference(opts[:read])
1150
1198
  Lint.validate_read_concern_option(opts[:read_concern])
1151
1199
  opts.each.inject(Options::Redacted.new) do |_options, (k, v)|
@@ -1155,6 +1203,15 @@ module Mongo
1155
1203
  validate_read!(key, opts)
1156
1204
  if key == :compressors
1157
1205
  compressors = valid_compressors(v)
1206
+
1207
+ if compressors.include?('snappy')
1208
+ validate_snappy_compression!
1209
+ end
1210
+
1211
+ if compressors.include?('zstd')
1212
+ validate_zstd_compression!
1213
+ end
1214
+
1158
1215
  _options[key] = compressors unless compressors.empty?
1159
1216
  else
1160
1217
  _options[key] = v
@@ -1311,11 +1368,30 @@ module Mongo
1311
1368
  "This compressor will not be used.")
1312
1369
  false
1313
1370
  else
1371
+
1314
1372
  true
1315
1373
  end
1316
1374
  end
1317
1375
  end
1318
1376
 
1377
+ def validate_snappy_compression!
1378
+ return if defined?(Snappy)
1379
+ require 'snappy'
1380
+ rescue LoadError => e
1381
+ raise Error::UnmetDependency, "Cannot enable snappy compression because the snappy gem " \
1382
+ "has not been installed. Add \"gem 'snappy'\" to your Gemfile and run " \
1383
+ "\"bundle install\" to install the gem. (#{e.class}: #{e})"
1384
+ end
1385
+
1386
+ def validate_zstd_compression!
1387
+ return if defined?(Zstd)
1388
+ require 'zstd-ruby'
1389
+ rescue LoadError => e
1390
+ raise Error::UnmetDependency, "Cannot enable zstd compression because the zstd-ruby gem " \
1391
+ "has not been installed. Add \"gem 'zstd-ruby'\" to your Gemfile and run " \
1392
+ "\"bundle install\" to install the gem. (#{e.class}: #{e})"
1393
+ end
1394
+
1319
1395
  def validate_max_min_pool_size!(option, opts)
1320
1396
  if option == :min_pool_size && opts[:min_pool_size]
1321
1397
  max = opts[:max_pool_size] || Server::ConnectionPool::DEFAULT_MAX_SIZE
data/lib/mongo/cluster.rb CHANGED
@@ -107,6 +107,11 @@ module Mongo
107
107
  # for the server monitor to refresh its description via ismaster.
108
108
  # @option options [ Hash ] :resolv_options For internal driver use only.
109
109
  # Options to pass through to Resolv::DNS constructor for SRV lookups.
110
+ # @option options [ Hash ] :server_api The requested server API version.
111
+ # This hash can have the following items:
112
+ # - *:version* -- string
113
+ # - *:strict* -- boolean
114
+ # - *:deprecation_errors* -- boolean
110
115
  #
111
116
  # @since 2.0.0
112
117
  def initialize(seeds, monitoring, options = Options::Redacted.new)
@@ -129,7 +134,9 @@ module Mongo
129
134
  @servers = []
130
135
  @monitoring = monitoring
131
136
  @event_listeners = Event::Listeners.new
132
- @app_metadata = Server::AppMetadata.new(@options)
137
+ @app_metadata = Server::AppMetadata.new(@options.merge(purpose: :application))
138
+ @monitor_app_metadata = Server::Monitor::AppMetadata.new(@options.merge(purpose: :monitor))
139
+ @push_monitor_app_metadata = Server::Monitor::AppMetadata.new(@options.merge(purpose: :push_monitor))
133
140
  @cluster_time_lock = Mutex.new
134
141
  @cluster_time = nil
135
142
  @srv_monitor_lock = Mutex.new
@@ -199,9 +206,6 @@ module Mongo
199
206
  @cursor_reaper, @socket_reaper,
200
207
  ], options)
201
208
 
202
- ObjectSpace.define_finalizer(self, self.class.finalize(
203
- {}, @periodic_executor, @session_pool))
204
-
205
209
  @periodic_executor.run!
206
210
  end
207
211
 
@@ -244,7 +248,11 @@ module Mongo
244
248
  # the servers list above and the wait call below, we should not
245
249
  # wait for the full remaining time - wait for up to 1 second, then
246
250
  # recheck the state.
247
- server_selection_semaphore.wait([time_remaining, 1].min)
251
+ begin
252
+ server_selection_semaphore.wait([time_remaining, 1].min)
253
+ rescue ::Timeout::Error
254
+ # nothing
255
+ end
248
256
  end
249
257
  end
250
258
 
@@ -288,6 +296,12 @@ module Mongo
288
296
  # @since 2.4.0
289
297
  attr_reader :app_metadata
290
298
 
299
+ # @api private
300
+ attr_reader :monitor_app_metadata
301
+
302
+ # @api private
303
+ attr_reader :push_monitor_app_metadata
304
+
291
305
  # @return [ Array<String> ] The addresses of seed servers. Contains
292
306
  # addresses that were given to Cluster when it was instantiated, not
293
307
  # current addresses that the cluster is using as a result of SDAM.
@@ -445,25 +459,6 @@ module Mongo
445
459
  # @api private
446
460
  attr_reader :server_selection_semaphore
447
461
 
448
- # Finalize the cluster for garbage collection.
449
- #
450
- # @example Finalize the cluster.
451
- # Cluster.finalize(pools)
452
- #
453
- # @param [ Hash<Address, Server::ConnectionPool> ] pools Ignored.
454
- # @param [ PeriodicExecutor ] periodic_executor The periodic executor.
455
- # @param [ SessionPool ] session_pool The session pool.
456
- #
457
- # @return [ Proc ] The Finalizer.
458
- #
459
- # @since 2.2.0
460
- def self.finalize(pools, periodic_executor, session_pool)
461
- proc do
462
- session_pool.end_sessions
463
- periodic_executor.stop!
464
- end
465
- end
466
-
467
462
  # Closes the cluster.
468
463
  #
469
464
  # @note Applications should call Client#close to disconnect from
@@ -953,10 +948,6 @@ module Mongo
953
948
  monitor_options = Utils.shallow_symbolize_keys(options.merge(
954
949
  timeout: options[:connect_timeout] || Server::CONNECT_TIMEOUT))
955
950
  @srv_monitor = _srv_monitor = Srv::Monitor.new(self, **monitor_options)
956
- finalizer = lambda do
957
- _srv_monitor.stop!
958
- end
959
- ObjectSpace.define_finalizer(self, finalizer)
960
951
  end
961
952
  @srv_monitor.run!
962
953
  end
@@ -130,16 +130,20 @@ module Mongo
130
130
  end
131
131
 
132
132
  to_kill_copy.each do |server, op_specs|
133
+ options = {
134
+ server_api: server.options[:server_api],
135
+ }
136
+ context = Operation::Context.new(options: options)
133
137
  op_specs.each do |op_spec|
134
138
  if server.features.find_command_enabled?
135
139
  Cursor::Builder::KillCursorsCommand.update_cursors(op_spec, active_cursors_copy.to_a)
136
140
  if Cursor::Builder::KillCursorsCommand.get_cursors_list(op_spec).size > 0
137
- Operation::KillCursors.new(op_spec).execute(server, client: nil)
141
+ Operation::KillCursors.new(op_spec).execute(server, context: context)
138
142
  end
139
143
  else
140
144
  Cursor::Builder::OpKillCursors.update_cursors(op_spec, active_cursors_copy.to_a)
141
145
  if Cursor::Builder::OpKillCursors.get_cursors_list(op_spec).size > 0
142
- Operation::KillCursors.new(op_spec).execute(server, client: nil)
146
+ Operation::KillCursors.new(op_spec).execute(server, context: context)
143
147
  end
144
148
  end
145
149
  end