sendproof-core 0.1.0.alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +55 -0
- data/lib/sendproof/assets.rb +70 -0
- data/lib/sendproof/attribution.rb +98 -0
- data/lib/sendproof/capture.rb +66 -0
- data/lib/sendproof/clock.rb +54 -0
- data/lib/sendproof/configuration.rb +241 -0
- data/lib/sendproof/data_store.rb +34 -0
- data/lib/sendproof/dynamodb/store.rb +183 -0
- data/lib/sendproof/dynamodb.rb +17 -0
- data/lib/sendproof/elasticsearch/store.rb +24 -0
- data/lib/sendproof/elasticsearch.rb +17 -0
- data/lib/sendproof/firestore/interceptor.rb +38 -0
- data/lib/sendproof/firestore/store.rb +150 -0
- data/lib/sendproof/firestore.rb +99 -0
- data/lib/sendproof/handler.rb +23 -0
- data/lib/sendproof/identity.rb +37 -0
- data/lib/sendproof/limits.rb +113 -0
- data/lib/sendproof/middleware.rb +68 -0
- data/lib/sendproof/mongo/engine.rb +692 -0
- data/lib/sendproof/mongo/guarded.rb +100 -0
- data/lib/sendproof/mongo/plain.rb +45 -0
- data/lib/sendproof/mongo/store.rb +54 -0
- data/lib/sendproof/mongo/subscriber.rb +21 -0
- data/lib/sendproof/mongo.rb +41 -0
- data/lib/sendproof/mongoid/store.rb +58 -0
- data/lib/sendproof/mongoid.rb +65 -0
- data/lib/sendproof/observed_request.rb +108 -0
- data/lib/sendproof/opensearch/store.rb +21 -0
- data/lib/sendproof/opensearch.rb +17 -0
- data/lib/sendproof/operation_matcher.rb +28 -0
- data/lib/sendproof/pool_hooks.rb +80 -0
- data/lib/sendproof/preview_request.rb +44 -0
- data/lib/sendproof/preview_transport.rb +331 -0
- data/lib/sendproof/protocol.rb +81 -0
- data/lib/sendproof/provider.rb +83 -0
- data/lib/sendproof/recorder.rb +298 -0
- data/lib/sendproof/redaction.rb +98 -0
- data/lib/sendproof/redis/commands.rb +76 -0
- data/lib/sendproof/redis/store.rb +205 -0
- data/lib/sendproof/redis.rb +18 -0
- data/lib/sendproof/search/middleware.rb +39 -0
- data/lib/sendproof/search/store.rb +213 -0
- data/lib/sendproof/search.rb +82 -0
- data/lib/sendproof/stores/classify.rb +346 -0
- data/lib/sendproof/stores/guard.rb +127 -0
- data/lib/sendproof/stores/tables.rb +1328 -0
- data/lib/sendproof/stores.rb +12 -0
- data/lib/sendproof/trace_store.rb +110 -0
- data/lib/sendproof/transaction_control.rb +187 -0
- data/lib/sendproof/ui/action-bar.js +91 -0
- data/lib/sendproof/ui/api.js +239 -0
- data/lib/sendproof/ui/app.css +848 -0
- data/lib/sendproof/ui/app.js +742 -0
- data/lib/sendproof/ui/autofill.js +137 -0
- data/lib/sendproof/ui/code-copy.js +71 -0
- data/lib/sendproof/ui/code-picker.js +149 -0
- data/lib/sendproof/ui/commands.js +197 -0
- data/lib/sendproof/ui/components.js +77 -0
- data/lib/sendproof/ui/context-bar.js +82 -0
- data/lib/sendproof/ui/copy.js +92 -0
- data/lib/sendproof/ui/coverage-popover.js +35 -0
- data/lib/sendproof/ui/crumb-fit.js +80 -0
- data/lib/sendproof/ui/deprecation.js +164 -0
- data/lib/sendproof/ui/display-settings.js +175 -0
- data/lib/sendproof/ui/doc-diff.js +209 -0
- data/lib/sendproof/ui/docs-view.js +347 -0
- data/lib/sendproof/ui/favicon.png +0 -0
- data/lib/sendproof/ui/favicon.svg +1 -0
- data/lib/sendproof/ui/field-help.js +52 -0
- data/lib/sendproof/ui/field-modules.js +23 -0
- data/lib/sendproof/ui/fields/controls.js +179 -0
- data/lib/sendproof/ui/fields/dom.js +36 -0
- data/lib/sendproof/ui/fields/infer.js +71 -0
- data/lib/sendproof/ui/fields/lookup-source.js +63 -0
- data/lib/sendproof/ui/fields/lookup.js +285 -0
- data/lib/sendproof/ui/fields/render.js +496 -0
- data/lib/sendproof/ui/fields/select.js +175 -0
- data/lib/sendproof/ui/fields/validate.js +363 -0
- data/lib/sendproof/ui/fields/validation-formats.js +164 -0
- data/lib/sendproof/ui/fields/validation-references.js +68 -0
- data/lib/sendproof/ui/fields/validation-values.js +137 -0
- data/lib/sendproof/ui/generate-menu.js +44 -0
- data/lib/sendproof/ui/generate.js +184 -0
- data/lib/sendproof/ui/header-editor.js +777 -0
- data/lib/sendproof/ui/headers.js +188 -0
- data/lib/sendproof/ui/highlight.js +257 -0
- data/lib/sendproof/ui/history-popover.js +24 -0
- data/lib/sendproof/ui/i18n.js +253 -0
- data/lib/sendproof/ui/icons.js +87 -0
- data/lib/sendproof/ui/index.html +70 -0
- data/lib/sendproof/ui/issues.js +187 -0
- data/lib/sendproof/ui/json-view.js +166 -0
- data/lib/sendproof/ui/lang-icons.js +62 -0
- data/lib/sendproof/ui/layout.js +262 -0
- data/lib/sendproof/ui/listbox.js +334 -0
- data/lib/sendproof/ui/locales/ar-capture.js +366 -0
- data/lib/sendproof/ui/locales/ar-stores.js +116 -0
- data/lib/sendproof/ui/locales/ar.js +561 -0
- data/lib/sendproof/ui/locales/cs-capture.js +442 -0
- data/lib/sendproof/ui/locales/cs-stores.js +116 -0
- data/lib/sendproof/ui/locales/cs.js +689 -0
- data/lib/sendproof/ui/locales/da-capture.js +347 -0
- data/lib/sendproof/ui/locales/da-stores.js +116 -0
- data/lib/sendproof/ui/locales/da.js +522 -0
- data/lib/sendproof/ui/locales/de-capture.js +347 -0
- data/lib/sendproof/ui/locales/de-stores.js +116 -0
- data/lib/sendproof/ui/locales/de.js +519 -0
- data/lib/sendproof/ui/locales/el-capture.js +347 -0
- data/lib/sendproof/ui/locales/el-stores.js +116 -0
- data/lib/sendproof/ui/locales/el.js +520 -0
- data/lib/sendproof/ui/locales/en-capture.js +349 -0
- data/lib/sendproof/ui/locales/en-stores.js +95 -0
- data/lib/sendproof/ui/locales/en.js +522 -0
- data/lib/sendproof/ui/locales/es-capture.js +353 -0
- data/lib/sendproof/ui/locales/es-stores.js +116 -0
- data/lib/sendproof/ui/locales/es.js +525 -0
- data/lib/sendproof/ui/locales/fa-capture.js +348 -0
- data/lib/sendproof/ui/locales/fa-stores.js +116 -0
- data/lib/sendproof/ui/locales/fa.js +521 -0
- data/lib/sendproof/ui/locales/fi-capture.js +347 -0
- data/lib/sendproof/ui/locales/fi-stores.js +116 -0
- data/lib/sendproof/ui/locales/fi.js +523 -0
- data/lib/sendproof/ui/locales/fr-capture.js +353 -0
- data/lib/sendproof/ui/locales/fr-stores.js +116 -0
- data/lib/sendproof/ui/locales/fr.js +525 -0
- data/lib/sendproof/ui/locales/he-capture.js +358 -0
- data/lib/sendproof/ui/locales/he-stores.js +116 -0
- data/lib/sendproof/ui/locales/he.js +529 -0
- data/lib/sendproof/ui/locales/hi-capture.js +348 -0
- data/lib/sendproof/ui/locales/hi-stores.js +116 -0
- data/lib/sendproof/ui/locales/hi.js +523 -0
- data/lib/sendproof/ui/locales/id-capture.js +347 -0
- data/lib/sendproof/ui/locales/id-stores.js +116 -0
- data/lib/sendproof/ui/locales/id.js +521 -0
- data/lib/sendproof/ui/locales/it-capture.js +361 -0
- data/lib/sendproof/ui/locales/it-stores.js +116 -0
- data/lib/sendproof/ui/locales/it.js +523 -0
- data/lib/sendproof/ui/locales/ja-capture.js +346 -0
- data/lib/sendproof/ui/locales/ja-stores.js +116 -0
- data/lib/sendproof/ui/locales/ja.js +521 -0
- data/lib/sendproof/ui/locales/ko-capture.js +346 -0
- data/lib/sendproof/ui/locales/ko-stores.js +116 -0
- data/lib/sendproof/ui/locales/ko.js +522 -0
- data/lib/sendproof/ui/locales/nb-capture.js +349 -0
- data/lib/sendproof/ui/locales/nb-stores.js +116 -0
- data/lib/sendproof/ui/locales/nb.js +521 -0
- data/lib/sendproof/ui/locales/nl-capture.js +355 -0
- data/lib/sendproof/ui/locales/nl-stores.js +116 -0
- data/lib/sendproof/ui/locales/nl.js +520 -0
- data/lib/sendproof/ui/locales/pl-capture.js +432 -0
- data/lib/sendproof/ui/locales/pl-stores.js +116 -0
- data/lib/sendproof/ui/locales/pl.js +528 -0
- data/lib/sendproof/ui/locales/pt-BR-capture.js +350 -0
- data/lib/sendproof/ui/locales/pt-BR-stores.js +116 -0
- data/lib/sendproof/ui/locales/pt-BR.js +523 -0
- data/lib/sendproof/ui/locales/pt-PT-capture.js +350 -0
- data/lib/sendproof/ui/locales/pt-PT-stores.js +116 -0
- data/lib/sendproof/ui/locales/pt-PT.js +523 -0
- data/lib/sendproof/ui/locales/ru-capture.js +437 -0
- data/lib/sendproof/ui/locales/ru-stores.js +116 -0
- data/lib/sendproof/ui/locales/ru.js +624 -0
- data/lib/sendproof/ui/locales/sv-capture.js +349 -0
- data/lib/sendproof/ui/locales/sv-stores.js +116 -0
- data/lib/sendproof/ui/locales/sv.js +518 -0
- data/lib/sendproof/ui/locales/th-capture.js +347 -0
- data/lib/sendproof/ui/locales/th-stores.js +116 -0
- data/lib/sendproof/ui/locales/th.js +521 -0
- data/lib/sendproof/ui/locales/tr-capture.js +348 -0
- data/lib/sendproof/ui/locales/tr-stores.js +116 -0
- data/lib/sendproof/ui/locales/tr.js +521 -0
- data/lib/sendproof/ui/locales/uk-capture.js +442 -0
- data/lib/sendproof/ui/locales/uk-stores.js +116 -0
- data/lib/sendproof/ui/locales/uk.js +630 -0
- data/lib/sendproof/ui/locales/vi-capture.js +346 -0
- data/lib/sendproof/ui/locales/vi-stores.js +116 -0
- data/lib/sendproof/ui/locales/vi.js +523 -0
- data/lib/sendproof/ui/locales/zh-Hans-capture.js +347 -0
- data/lib/sendproof/ui/locales/zh-Hans-stores.js +116 -0
- data/lib/sendproof/ui/locales/zh-Hans.js +523 -0
- data/lib/sendproof/ui/locales/zh-Hant-capture.js +346 -0
- data/lib/sendproof/ui/locales/zh-Hant-stores.js +116 -0
- data/lib/sendproof/ui/locales/zh-Hant.js +522 -0
- data/lib/sendproof/ui/manifest.json +866 -0
- data/lib/sendproof/ui/markdown.js +39 -0
- data/lib/sendproof/ui/model-view.js +182 -0
- data/lib/sendproof/ui/navigation.js +233 -0
- data/lib/sendproof/ui/operation-header.js +76 -0
- data/lib/sendproof/ui/operations.js +7 -0
- data/lib/sendproof/ui/overview-view.js +63 -0
- data/lib/sendproof/ui/personas.js +59 -0
- data/lib/sendproof/ui/popover.js +71 -0
- data/lib/sendproof/ui/preview-false.js +108 -0
- data/lib/sendproof/ui/request-editor.js +496 -0
- data/lib/sendproof/ui/response-metadata.js +354 -0
- data/lib/sendproof/ui/response-viewer.js +618 -0
- data/lib/sendproof/ui/result-blocked.js +155 -0
- data/lib/sendproof/ui/result-capture.js +25 -0
- data/lib/sendproof/ui/result-database.js +185 -0
- data/lib/sendproof/ui/result-effects.js +176 -0
- data/lib/sendproof/ui/result-headers.js +106 -0
- data/lib/sendproof/ui/result-logs.js +408 -0
- data/lib/sendproof/ui/result-states.js +213 -0
- data/lib/sendproof/ui/results.js +371 -0
- data/lib/sendproof/ui/router.js +118 -0
- data/lib/sendproof/ui/schema-model.js +336 -0
- data/lib/sendproof/ui/schema-tree.js +344 -0
- data/lib/sendproof/ui/schema.js +397 -0
- data/lib/sendproof/ui/security.js +31 -0
- data/lib/sendproof/ui/seeded-menu.js +69 -0
- data/lib/sendproof/ui/settings.js +93 -0
- data/lib/sendproof/ui/shortcuts-sheet.js +73 -0
- data/lib/sendproof/ui/shortcuts.js +190 -0
- data/lib/sendproof/ui/sidebar.js +289 -0
- data/lib/sendproof/ui/signin.js +259 -0
- data/lib/sendproof/ui/snippets.js +530 -0
- data/lib/sendproof/ui/store-marks.js +47 -0
- data/lib/sendproof/ui/stores.js +131 -0
- data/lib/sendproof/ui/styles.css +971 -0
- data/lib/sendproof/ui/toggletip.js +53 -0
- data/lib/sendproof/ui/uuid.js +10 -0
- data/lib/sendproof/ui/view.js +122 -0
- data/lib/sendproof/ui/views.css +653 -0
- data/lib/sendproof/version.rb +9 -0
- data/lib/sendproof/workbench.rb +102 -0
- data/lib/sendproof-core.rb +3 -0
- data/lib/sendproof.rb +121 -0
- metadata +292 -0
|
@@ -0,0 +1,692 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "set"
|
|
5
|
+
|
|
6
|
+
module Sendproof
|
|
7
|
+
module Mongo
|
|
8
|
+
# The per-client engine shared by {Store} (plain driver) and {Sendproof::Mongoid::Store}:
|
|
9
|
+
#
|
|
10
|
+
# - *Topology.* +hello+ decides the mode once. A replica set or mongos with logical sessions runs in
|
|
11
|
+
# +preview+ mode; anything else in +guard+ mode (spec/data-stores.md "Starting the transaction").
|
|
12
|
+
# - *Session.* Each +preview+-mode Preview gets one client session with a snapshot/majority
|
|
13
|
+
# transaction. The handler's own +start_transaction+ and +with_transaction+ join it; its
|
|
14
|
+
# +commit_transaction+ and +abort_transaction+ never reach the server and fail the Preview with 500.
|
|
15
|
+
# - *Guard and tripwire.* Every command the client sends is seen by {Subscriber#started} on the
|
|
16
|
+
# calling thread before it is written. During a Preview it is classified and refused: in +guard+
|
|
17
|
+
# mode every write blocks, in +preview+ mode what MongoDB can't run in a transaction blocks, and a
|
|
18
|
+
# write that doesn't carry the Preview's +lsid+ and +autocommit: false+ fails the Preview with 500.
|
|
19
|
+
# A reply with code 263 blocks, and every other command becomes a +Db+ log entry.
|
|
20
|
+
# - *Finish.* After the handler, changes are read (inside the transaction for +after+, and at the
|
|
21
|
+
# transaction's own snapshot time for +before+), then the transaction is aborted and the session
|
|
22
|
+
# ended. A Preview that outlives its time limit is aborted early and answers 500. An abort the
|
|
23
|
+
# server never acknowledged (the connection dropped) answers 500 at once and is followed, in the
|
|
24
|
+
# background, by +killSessions+, so the transaction doesn't hold its locks until the server's
|
|
25
|
+
# lifetime limit.
|
|
26
|
+
class Engine
|
|
27
|
+
# Commands the monitoring specification marks security-sensitive: never logged.
|
|
28
|
+
SENSITIVE = %w[authenticate saslstart saslcontinue getnonce createuser updateuser copydbgetnonce copydbsaslstart copydb].to_set.freeze
|
|
29
|
+
|
|
30
|
+
# Fields the driver adds to every command; they carry no meaning for the log and are dropped.
|
|
31
|
+
ENVELOPE = %w[lsid txnNumber autocommit startTransaction $clusterTime $db $readPreference readConcern writeConcern
|
|
32
|
+
apiVersion apiStrict apiDeprecationErrors].to_set.freeze
|
|
33
|
+
|
|
34
|
+
# Most candidate documents read back for change capture in one Preview.
|
|
35
|
+
CANDIDATE_LIMIT = 1000
|
|
36
|
+
|
|
37
|
+
# The default Preview time limit in seconds: below the server's 60-second
|
|
38
|
+
# +transactionLifetimeLimitSeconds+, so the binding, not the server, ends a Preview whose handler
|
|
39
|
+
# runs too long.
|
|
40
|
+
DEFAULT_TIMEOUT = 50.0
|
|
41
|
+
|
|
42
|
+
# Server error codes meaning the collection or snapshot didn't exist at the start time.
|
|
43
|
+
ABSENT_AT_START = [26, 246].freeze # NamespaceNotFound, SnapshotUnavailable
|
|
44
|
+
|
|
45
|
+
# Databases whose commands can't join a transaction.
|
|
46
|
+
SYSTEM_DATABASES = %w[admin config local].freeze
|
|
47
|
+
|
|
48
|
+
# +NoSuchTransaction+: the server has no such transaction, so nothing holds its locks.
|
|
49
|
+
NO_SUCH_TRANSACTION = 251
|
|
50
|
+
|
|
51
|
+
# How long, in seconds, the engine keeps trying to end a Preview transaction whose abort the server
|
|
52
|
+
# never acknowledged, and the backoff between tries: well inside the server's 60-second lifetime.
|
|
53
|
+
KILL_RETRY_LIMIT = 10.0
|
|
54
|
+
KILL_FIRST_DELAY = 0.1
|
|
55
|
+
KILL_MAX_DELAY = 2.0
|
|
56
|
+
# The most one +killSessions+ try may take, in seconds.
|
|
57
|
+
KILL_TRY_LIMIT = 2.0
|
|
58
|
+
|
|
59
|
+
# The state of one +preview+-mode Preview.
|
|
60
|
+
# +start_time+ is the transaction's snapshot time: the +operationTime+ of the first reply to a command
|
|
61
|
+
# the Preview session sent, so +before+ images are read at that time and a concurrent commit by
|
|
62
|
+
# another request never shows up as a Preview change. +lock+ makes the time limit and the finish take
|
|
63
|
+
# turns: only one of them ever aborts the transaction, and neither is interrupted while it does.
|
|
64
|
+
# +opened+ is set once a command carrying the Preview session was sent: from then on the server may
|
|
65
|
+
# hold the transaction, and its locks, until an abort reaches it. +killer+ is the background thread
|
|
66
|
+
# that ends a transaction whose abort was lost; it ends the session itself once done, so the session
|
|
67
|
+
# id isn't reused meanwhile.
|
|
68
|
+
State = Struct.new(:mode, :session, :lsid, :used, :opened, :finished, :expired, :candidates, :filters, :start_time,
|
|
69
|
+
:timer, :lock, :killer, keyword_init: true)
|
|
70
|
+
|
|
71
|
+
# One document a Preview may have changed, and how.
|
|
72
|
+
Candidate = Struct.new(:namespace, :collection, :id, :via)
|
|
73
|
+
|
|
74
|
+
# A filter whose matches, before and inside the transaction, are change candidates.
|
|
75
|
+
Filter = Struct.new(:namespace, :collection, :filter, :via)
|
|
76
|
+
|
|
77
|
+
# A command seen by monitoring, kept until its reply arrives.
|
|
78
|
+
Pending = Struct.new(:capture, :preview, :name, :namespace, :target, :statement, :started, :command)
|
|
79
|
+
|
|
80
|
+
# @return [Mongo::Client] the hooked client
|
|
81
|
+
attr_reader :client
|
|
82
|
+
# @return [Subscriber] the command subscriber on the hooked client and on every client derived from it
|
|
83
|
+
attr_reader :subscriber
|
|
84
|
+
|
|
85
|
+
# @param client [Mongo::Client] the client to hook; its command monitoring must be on (the default)
|
|
86
|
+
# @param reads [Set<String>, nil] host-declared reads, lower case
|
|
87
|
+
# @param preview_timeout [Numeric] seconds a Preview may hold its transaction
|
|
88
|
+
# @param hello [#call, nil] test seam: returns the +hello+ document instead of asking the server
|
|
89
|
+
# @param logger [#warn, nil] where a Preview transaction the engine couldn't end is reported;
|
|
90
|
+
# +Kernel#warn+ (standard error) when nil
|
|
91
|
+
def initialize(client, reads: nil, preview_timeout: DEFAULT_TIMEOUT, hello: nil, logger: nil)
|
|
92
|
+
raise ArgumentError, "preview_timeout must be a positive number of seconds." unless preview_timeout.is_a?(Numeric) && preview_timeout.positive?
|
|
93
|
+
|
|
94
|
+
# The guard sees commands only through monitoring: a client that doesn't publish them would run every
|
|
95
|
+
# write unguarded, so it is refused rather than hooked.
|
|
96
|
+
if client.options[:monitoring] == false
|
|
97
|
+
raise ArgumentError, "Create the Mongo::Client without monitoring: false: the Preview guard needs command monitoring."
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
@client = client
|
|
101
|
+
@reads = reads
|
|
102
|
+
@timeout = preview_timeout
|
|
103
|
+
@hello = hello
|
|
104
|
+
@logger = logger
|
|
105
|
+
@detect_lock = Mutex.new
|
|
106
|
+
@detected = nil
|
|
107
|
+
@active = true
|
|
108
|
+
@subscriber = Subscriber.new(self)
|
|
109
|
+
client.subscribe(::Mongo::Monitoring::COMMAND, @subscriber)
|
|
110
|
+
Engine.registry << self
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
class << self
|
|
114
|
+
# The engines that may own a Preview session, so Mongoid's ambient-session lookup can find them.
|
|
115
|
+
def registry = @registry ||= []
|
|
116
|
+
|
|
117
|
+
# Subscribes every hooked engine of +source+ to +derived+, a client +source.with+ returned. When
|
|
118
|
+
# +with+ changes an option outside the driver's CRUD set (+app_name+, +socket_timeout+,
|
|
119
|
+
# +monitoring+ ...), the driver builds a new cluster with a fresh Monitoring that has none of the
|
|
120
|
+
# source's subscribers, so the derived client's commands would bypass the guard and the tripwire.
|
|
121
|
+
# Its sessions belong to the new cluster, so in +preview+ mode its writes are refused as escapes.
|
|
122
|
+
def carry(source, derived)
|
|
123
|
+
target = derived.monitoring
|
|
124
|
+
return if target.equal?(source.monitoring)
|
|
125
|
+
|
|
126
|
+
subscribed = source.monitoring.subscribers[::Mongo::Monitoring::COMMAND] || []
|
|
127
|
+
registry.each do |engine|
|
|
128
|
+
next unless subscribed.include?(engine.subscriber)
|
|
129
|
+
next if (target.subscribers[::Mongo::Monitoring::COMMAND] || []).include?(engine.subscriber)
|
|
130
|
+
|
|
131
|
+
target.subscribe(::Mongo::Monitoring::COMMAND, engine.subscriber)
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# Prepended to +Mongo::Client+ so a client derived with {#with} stays hooked (see {Engine.carry}).
|
|
137
|
+
module DerivedClients
|
|
138
|
+
def with(new_options = nil)
|
|
139
|
+
super.tap { |derived| Engine.carry(self, derived) }
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
::Mongo::Client.prepend(DerivedClients)
|
|
143
|
+
|
|
144
|
+
# Turns the hook off: nothing is guarded or logged afterwards. The subscriber stays on the client.
|
|
145
|
+
def unhook!
|
|
146
|
+
@active = false
|
|
147
|
+
Engine.registry.delete(self)
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def active? = @active
|
|
151
|
+
|
|
152
|
+
# @return [Boolean] whether +client+'s commands reach this engine's subscriber
|
|
153
|
+
def hooked?(client)
|
|
154
|
+
(client.monitoring.subscribers[::Mongo::Monitoring::COMMAND] || []).include?(@subscriber)
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
# Runs engine-internal commands (topology, change capture, abort) without logging or tripping.
|
|
158
|
+
def internal
|
|
159
|
+
previous = Thread.current[:sendproof_mongo_internal]
|
|
160
|
+
Thread.current[:sendproof_mongo_internal] = true
|
|
161
|
+
yield
|
|
162
|
+
ensure
|
|
163
|
+
Thread.current[:sendproof_mongo_internal] = previous
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def internal? = Thread.current[:sendproof_mongo_internal] ? true : false
|
|
167
|
+
|
|
168
|
+
# Detects the topology once: +preview+ on a replica set or mongos with logical sessions, +guard+ with
|
|
169
|
+
# {STANDALONE_REASON} for a standalone server or any reply this doesn't recognize. When the +hello+
|
|
170
|
+
# can't be sent or answered, +guard+ with {UNREACHABLE_REASON} for now, and it is asked again next time.
|
|
171
|
+
# @return [Hash] +:mode+ and, in +guard+ mode, the +:reason+
|
|
172
|
+
def detect
|
|
173
|
+
return @detected if @detected
|
|
174
|
+
|
|
175
|
+
@detect_lock.synchronize do
|
|
176
|
+
return @detected if @detected
|
|
177
|
+
|
|
178
|
+
hello = begin
|
|
179
|
+
internal { @hello ? @hello.call : @client.use("admin").database.command(hello: 1).first }
|
|
180
|
+
rescue ::Mongo::Error::OperationFailure
|
|
181
|
+
nil # The server answered, with an error: not a reply that supports transactions.
|
|
182
|
+
rescue StandardError
|
|
183
|
+
return { mode: :guard, reason: UNREACHABLE_REASON }
|
|
184
|
+
end
|
|
185
|
+
transactions = hello.is_a?(Hash) && (hello["setName"] || hello["msg"] == "isdbgrid") &&
|
|
186
|
+
!hello["logicalSessionTimeoutMinutes"].nil?
|
|
187
|
+
@detected = transactions ? { mode: :preview } : { mode: :guard, reason: STANDALONE_REASON }
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
# @return [State, nil] the current Preview's state, when this code runs in a +preview+-mode Preview that began
|
|
192
|
+
def state(recorder = Capture.preview)
|
|
193
|
+
recorder&.store_state(self)&.then { |state| state.session ? state : nil }
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
# Raises the Preview's refusal when its MongoDB transaction is gone: the Preview has ended (handler
|
|
197
|
+
# finished) or expired (time limit). Guarded handles call it first, so a late call from a thread the
|
|
198
|
+
# handler left behind fails with the Preview's own error instead of reaching the driver.
|
|
199
|
+
# @raise [PreviewTransactionControl]
|
|
200
|
+
def refuse_if_ended(state)
|
|
201
|
+
return unless state.finished || state.expired
|
|
202
|
+
|
|
203
|
+
message = if state.expired
|
|
204
|
+
"The Preview ran longer than #{@timeout} s, so its MongoDB transaction was aborted."
|
|
205
|
+
else
|
|
206
|
+
"The Preview has ended, so its MongoDB session refuses every further call."
|
|
207
|
+
end
|
|
208
|
+
preview = Capture.preview
|
|
209
|
+
raise(preview ? refuse(preview, message) : PreviewTransactionControl.new(message))
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
# The Preview session for +client+ (any client sharing this engine's cluster), or nil. Mongoid asks
|
|
213
|
+
# this for every operation so its writes carry the Preview's session.
|
|
214
|
+
def preview_session_for(client)
|
|
215
|
+
return nil unless @active && client && (current = state)
|
|
216
|
+
return nil if current.finished || current.expired
|
|
217
|
+
|
|
218
|
+
client.cluster.equal?(@client.cluster) ? current.session : nil
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
# Prepares a Preview: detects the mode and, in +preview+ mode, starts the Preview's session and
|
|
222
|
+
# transaction. The session's own transaction methods are replaced so the handler joins the Preview
|
|
223
|
+
# transaction instead of ending it.
|
|
224
|
+
def begin_preview(recorder)
|
|
225
|
+
return unless @active && recorder.store_state(self).nil?
|
|
226
|
+
|
|
227
|
+
# The tripwire must be on the guarded client itself: without it, nothing would be guarded or rolled back.
|
|
228
|
+
unless hooked?(@client)
|
|
229
|
+
raise ConfigurationError, "The MongoDB client's command monitor was removed, so the Preview can't guard it."
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
mode = detect[:mode]
|
|
233
|
+
unless mode == :preview
|
|
234
|
+
recorder.set_store_state(self, State.new(mode: mode))
|
|
235
|
+
return
|
|
236
|
+
end
|
|
237
|
+
session = @client.start_session(causal_consistency: false)
|
|
238
|
+
lift_direct_connection_checks(session)
|
|
239
|
+
session.start_transaction(read_concern: { level: :snapshot }, write_concern: { w: :majority })
|
|
240
|
+
join_transaction(session, recorder)
|
|
241
|
+
state = State.new(mode: mode, session: session, lsid: hex(session.session_id), used: false, opened: false, finished: false,
|
|
242
|
+
expired: false, candidates: {}, filters: [], lock: Mutex.new)
|
|
243
|
+
recorder.set_store_state(self, state)
|
|
244
|
+
# A handler still running at the limit loses its transaction: later commands fail, nothing commits,
|
|
245
|
+
# and the Preview answers 500 when it ends.
|
|
246
|
+
state.timer = Thread.new do
|
|
247
|
+
Thread.current.report_on_exception = false
|
|
248
|
+
sleep @timeout
|
|
249
|
+
expire(recorder, state)
|
|
250
|
+
end
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
# After the handler: capture document changes, then abort the transaction and end the session. A
|
|
254
|
+
# capture read that fails for any reason but an already-aborted transaction raises, after the abort, so
|
|
255
|
+
# the Preview answers 500, as does an abort the server never acknowledged. Nothing the Preview wrote
|
|
256
|
+
# is ever committed: no +commitTransaction+ is ever sent for the Preview session.
|
|
257
|
+
def finish_preview(recorder)
|
|
258
|
+
current = recorder.store_state(self)
|
|
259
|
+
# Bookkeeping for commands this thread never saw finish (a refused command is never sent).
|
|
260
|
+
Thread.current[:sendproof_mongo_refused]&.clear
|
|
261
|
+
Thread.current[:sendproof_mongo_pending]&.clear
|
|
262
|
+
return unless current&.session
|
|
263
|
+
|
|
264
|
+
failure = nil
|
|
265
|
+
# Waits for a time limit that is aborting the transaction right now, rather than racing it.
|
|
266
|
+
current.lock.synchronize do
|
|
267
|
+
next if current.finished
|
|
268
|
+
|
|
269
|
+
begin
|
|
270
|
+
internal { capture_changes(recorder, current) } if current.used && !current.expired
|
|
271
|
+
rescue StandardError => error
|
|
272
|
+
# A failed command inside a transaction makes the server abort it: nothing to capture.
|
|
273
|
+
failure = error unless error.is_a?(::Mongo::Error::OperationFailure) && error.code == 251 # NoSuchTransaction
|
|
274
|
+
ensure
|
|
275
|
+
end_session(current)
|
|
276
|
+
end
|
|
277
|
+
if current.killer && !current.expired
|
|
278
|
+
# The server may still hold the transaction's locks; the session is killed in the background.
|
|
279
|
+
failure ||= PreviewTransactionControl.new("MongoDB didn't acknowledge the abort of the Preview transaction, " \
|
|
280
|
+
"so the rollback can't be confirmed.")
|
|
281
|
+
end
|
|
282
|
+
end
|
|
283
|
+
# The timer is asleep, waiting for the lock or done: never stopped in the middle of an abort.
|
|
284
|
+
current.timer&.kill unless current.timer.equal?(Thread.current)
|
|
285
|
+
raise failure if failure
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
# ---- Monitoring ----
|
|
289
|
+
|
|
290
|
+
# Called by the subscriber before a command is written. During a Preview it classifies the command
|
|
291
|
+
# and raises to refuse it: the driver propagates the exception, so no byte of it is sent.
|
|
292
|
+
def command_started(event)
|
|
293
|
+
# The driver sends abortTransaction only when a command was sent in the transaction.
|
|
294
|
+
aborting = Thread.current[:sendproof_mongo_aborting]
|
|
295
|
+
aborting[:sent] = true if aborting && event.command_name == "abortTransaction"
|
|
296
|
+
return unless @active && !internal?
|
|
297
|
+
|
|
298
|
+
name = event.command_name
|
|
299
|
+
lower = name.downcase
|
|
300
|
+
command = event.command
|
|
301
|
+
# The driver redacts a security-sensitive command to an empty document, which would classify as an
|
|
302
|
+
# unknown command, so a write. Classify by the command's name instead: an authentication exchange on
|
|
303
|
+
# a new pooled connection must never block a Preview.
|
|
304
|
+
command = { name => 1 } if command.nil? || command.empty?
|
|
305
|
+
classification = if SENSITIVE.include?(lower)
|
|
306
|
+
Stores::Classify::MongoResult.new(:session, nil)
|
|
307
|
+
else
|
|
308
|
+
Stores::Classify.mongo(command, @reads)
|
|
309
|
+
end
|
|
310
|
+
preview = Capture.preview
|
|
311
|
+
if preview
|
|
312
|
+
begin
|
|
313
|
+
check(preview, event, command, classification)
|
|
314
|
+
rescue Sendproof::PreviewWriteBlocked, Sendproof::PreviewTransactionControl
|
|
315
|
+
# If a driver ever swallowed this, the command would be sent: command_succeeded/failed then fault.
|
|
316
|
+
(Thread.current[:sendproof_mongo_refused] ||= Set.new) << event.request_id
|
|
317
|
+
raise
|
|
318
|
+
end
|
|
319
|
+
# Not refused, so it is sent: on the Preview session, the server now holds the transaction.
|
|
320
|
+
if (current = state_for(preview)) && !current.opened && hex(command["lsid"]) == current.lsid
|
|
321
|
+
current.opened = true
|
|
322
|
+
end
|
|
323
|
+
elsif classification.kind == :write
|
|
324
|
+
# Not this request's Preview: a write from outside any request is refused before it is sent while
|
|
325
|
+
# a Preview is open, and blocks every open Preview (409) in either mode, since nothing was sent.
|
|
326
|
+
Attribution.refuse_unattributed("mongodb", name)
|
|
327
|
+
end
|
|
328
|
+
return if classification.kind == :session || SENSITIVE.include?(lower)
|
|
329
|
+
|
|
330
|
+
target = command[name].is_a?(String) ? command[name] : nil
|
|
331
|
+
# Literals become "?" but the command's own target (the collection name) stays, as the spec's example shows.
|
|
332
|
+
body = command.reject { |field, _| ENVELOPE.include?(field.to_s) }
|
|
333
|
+
statement = Stores::Guard.sanitize_document(body)
|
|
334
|
+
statement[name] = target if target && statement.key?(name)
|
|
335
|
+
return unless Capture.current
|
|
336
|
+
|
|
337
|
+
(Thread.current[:sendproof_mongo_pending] ||= {})[event.request_id] =
|
|
338
|
+
Pending.new(Capture.current, preview, name, event.database_name, target,
|
|
339
|
+
JSON.generate(statement), Clock.now, command)
|
|
340
|
+
end
|
|
341
|
+
|
|
342
|
+
def command_succeeded(event)
|
|
343
|
+
acknowledge_abort(event, nil)
|
|
344
|
+
return unless @active
|
|
345
|
+
|
|
346
|
+
tripwire(event)
|
|
347
|
+
pending = take(event) or return
|
|
348
|
+
reply = event.reply || {}
|
|
349
|
+
state = pending.preview && state_for(pending.preview)
|
|
350
|
+
# The first reply in the transaction carries its snapshot time (the read timestamp).
|
|
351
|
+
if state && state.start_time.nil? && reply["operationTime"] && hex(pending.command["lsid"]) == state.lsid
|
|
352
|
+
state.start_time = reply["operationTime"]
|
|
353
|
+
end
|
|
354
|
+
track_reply(state, pending, reply) if state && pending.target
|
|
355
|
+
cursor = reply["cursor"]
|
|
356
|
+
rows = if cursor then (cursor["firstBatch"] || cursor["nextBatch"] || []).length
|
|
357
|
+
elsif reply["n"].is_a?(Integer) then reply["n"]
|
|
358
|
+
end
|
|
359
|
+
log(pending, event.duration, rows)
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
def command_failed(event)
|
|
363
|
+
failure = event.failure
|
|
364
|
+
failure = {} unless failure.respond_to?(:key?)
|
|
365
|
+
# A network error has no failure document: it proves nothing reached the server.
|
|
366
|
+
acknowledge_abort(event, failure["code"]) if failure["code"]
|
|
367
|
+
return unless @active
|
|
368
|
+
|
|
369
|
+
tripwire(event)
|
|
370
|
+
pending = take(event) or return
|
|
371
|
+
# The server refused something a transaction can't do and applied nothing: a block, not a crash.
|
|
372
|
+
if pending.preview && state_for(pending.preview) && failure["code"] == 263
|
|
373
|
+
pending.preview.block(store: "mongodb", operation: pending.name, detail: Mongo.refused_reason(pending.name))
|
|
374
|
+
end
|
|
375
|
+
log(pending, event.duration, nil, failure["codeName"] || "Error")
|
|
376
|
+
end
|
|
377
|
+
|
|
378
|
+
private
|
|
379
|
+
|
|
380
|
+
def state_for(recorder) = recorder.store_state(self)&.then { |state| state.session ? state : nil }
|
|
381
|
+
|
|
382
|
+
# The driver hides the outcome of +abortTransaction+, so monitoring notes it while the engine aborts:
|
|
383
|
+
# the abort counts when the server acknowledged it or answered that the transaction no longer exists.
|
|
384
|
+
def acknowledge_abort(event, code)
|
|
385
|
+
aborting = Thread.current[:sendproof_mongo_aborting]
|
|
386
|
+
return unless aborting && event.command_name == "abortTransaction"
|
|
387
|
+
|
|
388
|
+
aborting[:acknowledged] = true if code.nil? || code == NO_SUCH_TRANSACTION
|
|
389
|
+
end
|
|
390
|
+
|
|
391
|
+
def mode_of(recorder) = recorder.store_state(self)&.mode || :guard
|
|
392
|
+
|
|
393
|
+
def hex(lsid)
|
|
394
|
+
data = lsid && (lsid["id"] || lsid[:id])
|
|
395
|
+
data.respond_to?(:data) ? data.data.unpack1("H*") : nil
|
|
396
|
+
end
|
|
397
|
+
|
|
398
|
+
# Classifies a command during a Preview and refuses it.
|
|
399
|
+
def check(preview, event, command, classification)
|
|
400
|
+
name = event.command_name
|
|
401
|
+
state = state_for(preview)
|
|
402
|
+
if %w[committransaction aborttransaction].include?(name.downcase)
|
|
403
|
+
raise refuse(preview, "Preview owns the MongoDB transaction, so the handler's #{name} cannot run during Preview.")
|
|
404
|
+
end
|
|
405
|
+
raise refuse(preview, "The Preview ran longer than #{@timeout} s, so its MongoDB transaction was aborted.") if state&.expired
|
|
406
|
+
|
|
407
|
+
if mode_of(preview) == :preview && !SYSTEM_DATABASES.include?(event.database_name)
|
|
408
|
+
if (refused = classification.refused_in_transaction)
|
|
409
|
+
Stores::Guard.guard("mongodb", name, :write, Mongo.refused_reason(refused))
|
|
410
|
+
end
|
|
411
|
+
verify_in_transaction(preview, state, event, command) if classification.kind == :write
|
|
412
|
+
else
|
|
413
|
+
Stores::Guard.guard("mongodb", name, classification.kind)
|
|
414
|
+
end
|
|
415
|
+
end
|
|
416
|
+
|
|
417
|
+
# A write seen during a Preview: it must carry the Preview's session and transaction, or nothing can
|
|
418
|
+
# roll it back. Otherwise it is refused (and, were the driver ever to send it anyway, the fault stands).
|
|
419
|
+
def verify_in_transaction(preview, state, event, command)
|
|
420
|
+
name = event.command_name
|
|
421
|
+
unless state && !state.finished && hex(command["lsid"]) == state.lsid && command["autocommit"] == false
|
|
422
|
+
raise refuse(preview, "A MongoDB #{name} escaped the Preview session; the Preview can't be confirmed.")
|
|
423
|
+
end
|
|
424
|
+
|
|
425
|
+
state.used = true
|
|
426
|
+
collect(state, event.database_name, name.downcase, command)
|
|
427
|
+
end
|
|
428
|
+
|
|
429
|
+
def refuse(preview, message)
|
|
430
|
+
Stores::Guard.refuse_in_preview(message) || PreviewTransactionControl.new(message).tap { |error| preview.refuse(error) }
|
|
431
|
+
end
|
|
432
|
+
|
|
433
|
+
# If a driver ever sent a command the hook refused, the Preview can't be confirmed.
|
|
434
|
+
def tripwire(event)
|
|
435
|
+
refused = Thread.current[:sendproof_mongo_refused]
|
|
436
|
+
return unless refused&.delete?(event.request_id)
|
|
437
|
+
|
|
438
|
+
Capture.preview&.refuse(PreviewTransactionControl.new("A refused MongoDB #{event.command_name} reached the server; the Preview can't be confirmed."))
|
|
439
|
+
end
|
|
440
|
+
|
|
441
|
+
def take(event)
|
|
442
|
+
pending = Thread.current[:sendproof_mongo_pending]
|
|
443
|
+
pending&.delete(event.request_id)
|
|
444
|
+
end
|
|
445
|
+
|
|
446
|
+
# Collects the candidate documents of a write: ids named in the command, and filters whose matches
|
|
447
|
+
# (before and inside the transaction) are read back at the end.
|
|
448
|
+
def collect(state, namespace, name, command)
|
|
449
|
+
collection = command[command.keys.first].is_a?(String) ? command[command.keys.first] : ""
|
|
450
|
+
case name
|
|
451
|
+
when "insert"
|
|
452
|
+
Array(command["documents"]).each { |document| candidate(state, Candidate.new(namespace, collection, document["_id"], nil)) }
|
|
453
|
+
when "update"
|
|
454
|
+
Array(command["updates"]).each do |update|
|
|
455
|
+
via = update["upsert"] ? :upsert : (replaces?(update["u"]) ? :replace : nil)
|
|
456
|
+
state.filters << Filter.new(namespace, collection, update["q"] || {}, via)
|
|
457
|
+
end
|
|
458
|
+
when "delete"
|
|
459
|
+
Array(command["deletes"]).each { |remove| state.filters << Filter.new(namespace, collection, remove["q"] || {}, nil) }
|
|
460
|
+
when "findandmodify"
|
|
461
|
+
via = command["upsert"] ? :upsert : (replaces?(command["update"]) ? :replace : nil)
|
|
462
|
+
state.filters << Filter.new(namespace, collection, command["query"] || {}, via)
|
|
463
|
+
when "bulkwrite"
|
|
464
|
+
collect_bulk(state, namespace, command)
|
|
465
|
+
end
|
|
466
|
+
end
|
|
467
|
+
|
|
468
|
+
# The client-level bulkWrite command: each op names its namespace by index.
|
|
469
|
+
def collect_bulk(state, namespace, command)
|
|
470
|
+
return unless command["ops"].is_a?(Array) && command["nsInfo"].is_a?(Array)
|
|
471
|
+
|
|
472
|
+
namespaces = command["nsInfo"].map { |info| info["ns"].to_s.split(".", 2) }
|
|
473
|
+
command["ops"].each do |op|
|
|
474
|
+
index = op["insert"] || op["update"] || op["delete"]
|
|
475
|
+
database, target = namespaces[index] || [namespace, ""]
|
|
476
|
+
if op["document"]
|
|
477
|
+
candidate(state, Candidate.new(database, target.to_s, op["document"]["_id"], nil))
|
|
478
|
+
else
|
|
479
|
+
state.filters << Filter.new(database, target.to_s, op["filter"] || {}, op["upsert"] ? :upsert : nil)
|
|
480
|
+
end
|
|
481
|
+
end
|
|
482
|
+
end
|
|
483
|
+
|
|
484
|
+
def candidate(state, candidate)
|
|
485
|
+
return if candidate.id.nil?
|
|
486
|
+
|
|
487
|
+
key = [candidate.namespace, candidate.collection, Plain.call(candidate.id).to_json]
|
|
488
|
+
known = state.candidates[key]
|
|
489
|
+
state.candidates[key] = candidate if known.nil? || (known.via.nil? && candidate.via)
|
|
490
|
+
end
|
|
491
|
+
|
|
492
|
+
# Whether an update document replaces the whole document rather than applying operators.
|
|
493
|
+
def replaces?(update)
|
|
494
|
+
update.is_a?(Hash) && !update.empty? && !update.keys.first.to_s.start_with?("$")
|
|
495
|
+
end
|
|
496
|
+
|
|
497
|
+
# Upserted ids appear only in the reply; findAndModify returns the changed document.
|
|
498
|
+
def track_reply(state, pending, reply)
|
|
499
|
+
Array(reply["upserted"]).each do |upsert|
|
|
500
|
+
candidate(state, Candidate.new(pending.namespace, pending.target, upsert["_id"], :upsert))
|
|
501
|
+
end
|
|
502
|
+
value = reply["value"]
|
|
503
|
+
if pending.name.casecmp?("findAndModify") && value.is_a?(Hash) && !value["_id"].nil?
|
|
504
|
+
candidate(state, Candidate.new(pending.namespace, pending.target, value["_id"], nil))
|
|
505
|
+
end
|
|
506
|
+
upserted = reply.dig("lastErrorObject", "upserted") if reply["lastErrorObject"].is_a?(Hash)
|
|
507
|
+
candidate(state, Candidate.new(pending.namespace, pending.target, upserted, :upsert)) unless upserted.nil?
|
|
508
|
+
end
|
|
509
|
+
|
|
510
|
+
def log(pending, duration, rows, error = nil)
|
|
511
|
+
pending.capture&.db(system: "mongodb", operation: pending.name, namespace: pending.namespace, target: pending.target,
|
|
512
|
+
statement: pending.statement, rows_affected: rows, error: error,
|
|
513
|
+
duration_ms: duration ? duration * 1000.0 : Clock.elapsed_ms(pending.started))
|
|
514
|
+
end
|
|
515
|
+
|
|
516
|
+
# ---- Session and transaction ----
|
|
517
|
+
|
|
518
|
+
# A direct connection (one seed, no replica set name) is a "single" topology to the driver, which then
|
|
519
|
+
# refuses transactions and non-primary read preferences itself even though the server was verified as
|
|
520
|
+
# a replica set member by +hello+; those two client-side checks are lifted for the Preview session only.
|
|
521
|
+
def lift_direct_connection_checks(session)
|
|
522
|
+
return unless @client.cluster.single?
|
|
523
|
+
|
|
524
|
+
session.define_singleton_method(:check_transactions_supported!) { nil }
|
|
525
|
+
session.define_singleton_method(:validate_read_preference!) { |_command| nil }
|
|
526
|
+
end
|
|
527
|
+
|
|
528
|
+
# Makes the handler's own transaction calls join the Preview transaction, and refuses the ones that
|
|
529
|
+
# would end it. Call after the transaction has started: it replaces +start_transaction+.
|
|
530
|
+
def join_transaction(session, recorder)
|
|
531
|
+
refuse_call = lambda do |what|
|
|
532
|
+
error = PreviewTransactionControl.new("Preview owns the MongoDB transaction, so the handler's #{what} cannot run during Preview.")
|
|
533
|
+
recorder.refuse(error)
|
|
534
|
+
error
|
|
535
|
+
end
|
|
536
|
+
session.define_singleton_method(:start_transaction) { |*_args| nil }
|
|
537
|
+
session.define_singleton_method(:with_transaction) { |*_args, &block| block.call(self) }
|
|
538
|
+
session.define_singleton_method(:commit_transaction) { |*_args| raise refuse_call.call("commit_transaction") }
|
|
539
|
+
session.define_singleton_method(:abort_transaction) { |*_args| raise refuse_call.call("abort_transaction") }
|
|
540
|
+
session.define_singleton_method(:end_session) { nil }
|
|
541
|
+
end
|
|
542
|
+
|
|
543
|
+
# Aborts the Preview transaction and ends its session, once; call it holding +state.lock+. When the
|
|
544
|
+
# abort was lost, the background kill ends the session once it is done instead.
|
|
545
|
+
def end_session(state)
|
|
546
|
+
return if state.finished
|
|
547
|
+
|
|
548
|
+
state.finished = true
|
|
549
|
+
abort_or_kill(state)
|
|
550
|
+
release(state.session) unless state.killer
|
|
551
|
+
end
|
|
552
|
+
|
|
553
|
+
# The Preview outlived its time limit: it can never be confirmed, and its transaction is aborted now.
|
|
554
|
+
def expire(recorder, state)
|
|
555
|
+
state.lock.synchronize do
|
|
556
|
+
next if state.finished
|
|
557
|
+
|
|
558
|
+
state.expired = true
|
|
559
|
+
recorder.refuse(PreviewTransactionControl.new("The Preview ran longer than #{@timeout} s, so its MongoDB transaction was aborted."))
|
|
560
|
+
abort_or_kill(state)
|
|
561
|
+
end
|
|
562
|
+
end
|
|
563
|
+
|
|
564
|
+
# Aborts the Preview transaction. The driver ignores abort errors, as the transactions specification
|
|
565
|
+
# requires: an unacknowledged abort still commits nothing, because no +commitTransaction+ is ever sent
|
|
566
|
+
# for the Preview session. But the server then keeps the transaction, and its locks, until its
|
|
567
|
+
# 60-second lifetime limit. So when a command was sent on the session (+state.opened+, or the driver
|
|
568
|
+
# sent its abort) and no reply acknowledged the abort, a background thread ends the transaction with
|
|
569
|
+
# +killSessions+ ({#kill}) and sets +state.killer+. The driver may not even send the abort: a dropped
|
|
570
|
+
# connection can leave it no server to select.
|
|
571
|
+
def abort_or_kill(state)
|
|
572
|
+
session = state.session
|
|
573
|
+
return if state.killer || !session.in_transaction?
|
|
574
|
+
|
|
575
|
+
aborting = { sent: false, acknowledged: false }
|
|
576
|
+
previous = Thread.current[:sendproof_mongo_aborting]
|
|
577
|
+
Thread.current[:sendproof_mongo_aborting] = aborting
|
|
578
|
+
begin
|
|
579
|
+
internal { ::Mongo::Session.instance_method(:abort_transaction).bind(session).call }
|
|
580
|
+
rescue StandardError
|
|
581
|
+
nil
|
|
582
|
+
ensure
|
|
583
|
+
Thread.current[:sendproof_mongo_aborting] = previous
|
|
584
|
+
end
|
|
585
|
+
return if aborting[:acknowledged] || !(state.opened || aborting[:sent])
|
|
586
|
+
|
|
587
|
+
state.killer = Thread.new do
|
|
588
|
+
Thread.current.report_on_exception = false
|
|
589
|
+
internal { kill(session) }
|
|
590
|
+
ensure
|
|
591
|
+
release(session, discard: true)
|
|
592
|
+
end
|
|
593
|
+
end
|
|
594
|
+
|
|
595
|
+
# Ends a Preview transaction whose abort no reply acknowledged with +killSessions+ on its session,
|
|
596
|
+
# retrying with backoff while the server can't be reached. A final failure is logged with the session
|
|
597
|
+
# id: the server then ends the transaction, and releases its locks, at its lifetime limit.
|
|
598
|
+
def kill(session)
|
|
599
|
+
deadline = Clock.now + KILL_RETRY_LIMIT
|
|
600
|
+
delay = KILL_FIRST_DELAY
|
|
601
|
+
failure = begin
|
|
602
|
+
loop do
|
|
603
|
+
# Each try is bounded, so one can't wait out the client's own server selection timeout (30 s by
|
|
604
|
+
# default) and hold the window open. Database#command doesn't bound server selection by
|
|
605
|
+
# +timeout_ms+, so a bounded selection comes first; +timeout_ms+ then bounds the command itself.
|
|
606
|
+
try_ms = ([KILL_TRY_LIMIT, deadline - Clock.now].min * 1000).ceil
|
|
607
|
+
break "MongoDB couldn't be reached for #{KILL_RETRY_LIMIT.to_i} s" unless try_ms.positive?
|
|
608
|
+
|
|
609
|
+
::Mongo::ServerSelector.primary.select_server(@client.cluster, nil, nil, timeout: try_ms / 1000.0)
|
|
610
|
+
@client.use("admin").database.command({ killSessions: [session.session_id] }, timeout_ms: try_ms)
|
|
611
|
+
return
|
|
612
|
+
rescue ::Mongo::Error::OperationFailure => error
|
|
613
|
+
break error.message
|
|
614
|
+
rescue StandardError => error
|
|
615
|
+
# A network error or no server to select: try again shortly.
|
|
616
|
+
break "MongoDB couldn't be reached for #{KILL_RETRY_LIMIT.to_i} s (#{error.class})" if Clock.now + delay > deadline
|
|
617
|
+
|
|
618
|
+
sleep delay
|
|
619
|
+
delay = [delay * 2, KILL_MAX_DELAY].min
|
|
620
|
+
end
|
|
621
|
+
end
|
|
622
|
+
message = "Sendproof couldn't end a MongoDB Preview transaction (session #{hex(session.session_id)}): #{failure}. " \
|
|
623
|
+
"The server ends it, and releases its locks, at transactionLifetimeLimitSeconds."
|
|
624
|
+
@logger ? @logger.warn(message) : Kernel.warn(message)
|
|
625
|
+
end
|
|
626
|
+
|
|
627
|
+
# Ends the driver session. After a lost abort (+discard+) its server session is marked dirty, so the
|
|
628
|
+
# driver drops it rather than hand its id to another operation.
|
|
629
|
+
def release(session, discard: false)
|
|
630
|
+
internal do
|
|
631
|
+
session.dirty! if discard && session.respond_to?(:dirty!)
|
|
632
|
+
::Mongo::Session.instance_method(:end_session).bind(session).call
|
|
633
|
+
end
|
|
634
|
+
rescue StandardError
|
|
635
|
+
nil
|
|
636
|
+
end
|
|
637
|
+
|
|
638
|
+
# ---- Change capture ----
|
|
639
|
+
|
|
640
|
+
# Finds documents as they were when the Preview transaction started: a snapshot read at its start
|
|
641
|
+
# time, outside the transaction. The driver has no read-concern option for +atClusterTime+, so the read
|
|
642
|
+
# is a raw +find+ command. Without a start time (no reply carried one), it reads the majority-committed
|
|
643
|
+
# view, which the spec allows: nothing the Preview wrote is committed, so the only difference is a
|
|
644
|
+
# concurrent commit by another request.
|
|
645
|
+
def at_start(state, namespace, collection, filter, projection = nil)
|
|
646
|
+
read_concern = state.start_time.nil? ? { level: "majority" } : { level: "snapshot", atClusterTime: state.start_time }
|
|
647
|
+
command = { find: collection, filter: filter, batchSize: CANDIDATE_LIMIT, limit: CANDIDATE_LIMIT, singleBatch: true,
|
|
648
|
+
readConcern: read_concern }
|
|
649
|
+
command[:projection] = projection if projection
|
|
650
|
+
reply = @client.use(namespace).database.command(command).first
|
|
651
|
+
reply.dig("cursor", "firstBatch") || []
|
|
652
|
+
rescue ::Mongo::Error::OperationFailure => error
|
|
653
|
+
# The collection was created after the start time: nothing in it existed then.
|
|
654
|
+
return [] if ABSENT_AT_START.include?(error.code)
|
|
655
|
+
|
|
656
|
+
raise
|
|
657
|
+
end
|
|
658
|
+
|
|
659
|
+
# Compares each candidate's start-time and in-transaction versions and records the differences.
|
|
660
|
+
def capture_changes(recorder, state)
|
|
661
|
+
session = state.session
|
|
662
|
+
state.filters.each do |filter|
|
|
663
|
+
target = @client.use(filter.namespace)[filter.collection]
|
|
664
|
+
matches = target.find(filter.filter, projection: { _id: 1 }, session: session).to_a + at_start(state, filter.namespace, filter.collection, filter.filter, { _id: 1 })
|
|
665
|
+
matches.each do |document|
|
|
666
|
+
candidate(state, Candidate.new(filter.namespace, filter.collection, document["_id"], filter.via))
|
|
667
|
+
break if state.candidates.size >= CANDIDATE_LIMIT
|
|
668
|
+
end
|
|
669
|
+
end
|
|
670
|
+
read = 0
|
|
671
|
+
state.candidates.each_value do |candidate|
|
|
672
|
+
if (read += 1) > CANDIDATE_LIMIT
|
|
673
|
+
recorder.truncate_changes
|
|
674
|
+
break
|
|
675
|
+
end
|
|
676
|
+
target = @client.use(candidate.namespace)[candidate.collection]
|
|
677
|
+
before = at_start(state, candidate.namespace, candidate.collection, { _id: candidate.id }).first
|
|
678
|
+
after = target.find({ _id: candidate.id }, session: session).first
|
|
679
|
+
was = before && Plain.call(before)
|
|
680
|
+
now = after && Plain.call(after)
|
|
681
|
+
next if was == now
|
|
682
|
+
|
|
683
|
+
operation = was.nil? ? "insert" : (now.nil? ? "delete" : "update")
|
|
684
|
+
change = { operation: operation, store: "mongodb", namespace: candidate.namespace, table: candidate.collection,
|
|
685
|
+
key: { "_id" => Plain.call(candidate.id) }, before: was, after: now }
|
|
686
|
+
change[:via] = candidate.via.to_s if candidate.via && operation != "delete"
|
|
687
|
+
recorder.change(change)
|
|
688
|
+
end
|
|
689
|
+
end
|
|
690
|
+
end
|
|
691
|
+
end
|
|
692
|
+
end
|