sendproof-core 0.1.0.alpha.1 → 0.1.0.alpha.2
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 +4 -4
- data/lib/sendproof/configuration.rb +31 -0
- data/lib/sendproof/data_store.rb +3 -0
- data/lib/sendproof/handler.rb +3 -1
- data/lib/sendproof/journeys/code_document.rb +305 -0
- data/lib/sendproof/journeys.rb +121 -0
- data/lib/sendproof/middleware.rb +7 -3
- data/lib/sendproof/mongo/engine.rb +121 -24
- data/lib/sendproof/mongo/held_session.rb +67 -0
- data/lib/sendproof/mongo/store.rb +1 -0
- data/lib/sendproof/mongo.rb +1 -0
- data/lib/sendproof/mongoid/store.rb +15 -5
- data/lib/sendproof/preview_request.rb +4 -1
- data/lib/sendproof/preview_sessions.rb +257 -0
- data/lib/sendproof/preview_transport.rb +86 -9
- data/lib/sendproof/protocol.rb +2 -2
- data/lib/sendproof/provider.rb +8 -0
- data/lib/sendproof/stores/tables.rb +213 -0
- data/lib/sendproof/transaction_control.rb +3 -2
- data/lib/sendproof/ui/api.js +2 -1
- data/lib/sendproof/ui/app.css +612 -1
- data/lib/sendproof/ui/app.js +89 -13
- data/lib/sendproof/ui/bar-fit.js +91 -0
- data/lib/sendproof/ui/commands.js +27 -4
- data/lib/sendproof/ui/dialog.css +125 -0
- data/lib/sendproof/ui/dialog.js +191 -0
- data/lib/sendproof/ui/fields/lookup-source.js +5 -2
- data/lib/sendproof/ui/fields/lookup.js +3 -2
- data/lib/sendproof/ui/i18n.js +13 -2
- data/lib/sendproof/ui/icons.js +18 -0
- data/lib/sendproof/ui/index.html +1 -0
- data/lib/sendproof/ui/journeys/compare.js +225 -0
- data/lib/sendproof/ui/journeys/expressions.js +89 -0
- data/lib/sendproof/ui/journeys/index.js +7 -0
- data/lib/sendproof/ui/journeys/run.js +152 -0
- data/lib/sendproof/ui/journeys/steps.js +163 -0
- data/lib/sendproof/ui/journeys-code.js +193 -0
- data/lib/sendproof/ui/journeys-dialog-model.js +172 -0
- data/lib/sendproof/ui/journeys-dialogs.js +301 -0
- data/lib/sendproof/ui/journeys-model.js +259 -0
- data/lib/sendproof/ui/journeys-runs.js +109 -0
- data/lib/sendproof/ui/journeys-session.js +135 -0
- data/lib/sendproof/ui/journeys-view.js +961 -0
- data/lib/sendproof/ui/journeys.js +962 -0
- data/lib/sendproof/ui/locales/ar-journeys.js +468 -0
- data/lib/sendproof/ui/locales/ar.js +2 -0
- data/lib/sendproof/ui/locales/cs-journeys.js +468 -0
- data/lib/sendproof/ui/locales/cs.js +2 -0
- data/lib/sendproof/ui/locales/da-journeys.js +465 -0
- data/lib/sendproof/ui/locales/da.js +2 -0
- data/lib/sendproof/ui/locales/de-journeys.js +465 -0
- data/lib/sendproof/ui/locales/de.js +2 -0
- data/lib/sendproof/ui/locales/el-journeys.js +465 -0
- data/lib/sendproof/ui/locales/el.js +2 -0
- data/lib/sendproof/ui/locales/en-journeys.js +467 -0
- data/lib/sendproof/ui/locales/en.js +2 -0
- data/lib/sendproof/ui/locales/es-journeys.js +467 -0
- data/lib/sendproof/ui/locales/es.js +2 -0
- data/lib/sendproof/ui/locales/fa-journeys.js +465 -0
- data/lib/sendproof/ui/locales/fa.js +2 -0
- data/lib/sendproof/ui/locales/fi-journeys.js +465 -0
- data/lib/sendproof/ui/locales/fi.js +2 -0
- data/lib/sendproof/ui/locales/fr-journeys.js +467 -0
- data/lib/sendproof/ui/locales/fr.js +2 -0
- data/lib/sendproof/ui/locales/he-journeys.js +468 -0
- data/lib/sendproof/ui/locales/he.js +2 -0
- data/lib/sendproof/ui/locales/hi-journeys.js +465 -0
- data/lib/sendproof/ui/locales/hi.js +2 -0
- data/lib/sendproof/ui/locales/id-journeys.js +466 -0
- data/lib/sendproof/ui/locales/id.js +2 -0
- data/lib/sendproof/ui/locales/it-journeys.js +467 -0
- data/lib/sendproof/ui/locales/it.js +2 -0
- data/lib/sendproof/ui/locales/ja-journeys.js +466 -0
- data/lib/sendproof/ui/locales/ja.js +2 -0
- data/lib/sendproof/ui/locales/ko-journeys.js +465 -0
- data/lib/sendproof/ui/locales/ko.js +2 -0
- data/lib/sendproof/ui/locales/nb-journeys.js +465 -0
- data/lib/sendproof/ui/locales/nb.js +2 -0
- data/lib/sendproof/ui/locales/nl-journeys.js +465 -0
- data/lib/sendproof/ui/locales/nl.js +2 -0
- data/lib/sendproof/ui/locales/pl-journeys.js +468 -0
- data/lib/sendproof/ui/locales/pl.js +2 -0
- data/lib/sendproof/ui/locales/pt-BR-journeys.js +467 -0
- data/lib/sendproof/ui/locales/pt-BR.js +2 -0
- data/lib/sendproof/ui/locales/pt-PT-journeys.js +467 -0
- data/lib/sendproof/ui/locales/pt-PT.js +2 -0
- data/lib/sendproof/ui/locales/ru-journeys.js +468 -0
- data/lib/sendproof/ui/locales/ru.js +2 -0
- data/lib/sendproof/ui/locales/sv-journeys.js +465 -0
- data/lib/sendproof/ui/locales/sv.js +2 -0
- data/lib/sendproof/ui/locales/th-journeys.js +466 -0
- data/lib/sendproof/ui/locales/th.js +2 -0
- data/lib/sendproof/ui/locales/tr-journeys.js +468 -0
- data/lib/sendproof/ui/locales/tr.js +2 -0
- data/lib/sendproof/ui/locales/uk-journeys.js +468 -0
- data/lib/sendproof/ui/locales/uk.js +2 -0
- data/lib/sendproof/ui/locales/vi-journeys.js +466 -0
- data/lib/sendproof/ui/locales/vi.js +2 -0
- data/lib/sendproof/ui/locales/zh-Hans-journeys.js +466 -0
- data/lib/sendproof/ui/locales/zh-Hans.js +2 -0
- data/lib/sendproof/ui/locales/zh-Hant-journeys.js +466 -0
- data/lib/sendproof/ui/locales/zh-Hant.js +2 -0
- data/lib/sendproof/ui/manifest.json +349 -104
- data/lib/sendproof/ui/markdown.js +27 -2
- data/lib/sendproof/ui/request-editor.js +7 -1
- data/lib/sendproof/ui/result-states.js +2 -2
- data/lib/sendproof/ui/results.js +41 -14
- data/lib/sendproof/ui/router.js +25 -1
- data/lib/sendproof/ui/seeded-menu.js +2 -1
- data/lib/sendproof/ui/shortcuts-sheet.js +3 -1
- data/lib/sendproof/ui/shortcuts.js +6 -0
- data/lib/sendproof/ui/sidebar.js +54 -5
- data/lib/sendproof/ui/signin.js +6 -3
- data/lib/sendproof/ui/styles.css +8 -2
- data/lib/sendproof/ui/toast.js +29 -0
- data/lib/sendproof/ui/vendor/arazzo-runtime-expression.js +136 -0
- data/lib/sendproof/ui/vendor/json-pointer.js +48 -0
- data/lib/sendproof/ui/vendor/jsonpath-rfc9535.js +15 -0
- data/lib/sendproof/ui/view.js +11 -3
- data/lib/sendproof/ui/views.css +7 -0
- data/lib/sendproof/version.rb +2 -2
- data/lib/sendproof/workbench.rb +11 -1
- data/lib/sendproof.rb +2 -0
- metadata +55 -2
|
@@ -66,13 +66,13 @@ module Sendproof
|
|
|
66
66
|
# that ends a transaction whose abort was lost; it ends the session itself once done, so the session
|
|
67
67
|
# id isn't reused meanwhile.
|
|
68
68
|
State = Struct.new(:mode, :session, :lsid, :used, :opened, :finished, :expired, :candidates, :filters, :start_time,
|
|
69
|
-
:timer, :lock, :killer, keyword_init: true)
|
|
69
|
+
:timer, :lock, :killer, :held, keyword_init: true)
|
|
70
70
|
|
|
71
71
|
# One document a Preview may have changed, and how.
|
|
72
72
|
Candidate = Struct.new(:namespace, :collection, :id, :via)
|
|
73
73
|
|
|
74
74
|
# A filter whose matches, before and inside the transaction, are change candidates.
|
|
75
|
-
Filter = Struct.new(:namespace, :collection, :filter, :via)
|
|
75
|
+
Filter = Struct.new(:namespace, :collection, :filter, :via, :collation)
|
|
76
76
|
|
|
77
77
|
# A command seen by monitoring, kept until its reply arrives.
|
|
78
78
|
Pending = Struct.new(:capture, :preview, :name, :namespace, :target, :statement, :started, :command)
|
|
@@ -234,12 +234,7 @@ module Sendproof
|
|
|
234
234
|
recorder.set_store_state(self, State.new(mode: mode))
|
|
235
235
|
return
|
|
236
236
|
end
|
|
237
|
-
|
|
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)
|
|
237
|
+
state = start_state(recorder)
|
|
243
238
|
recorder.set_store_state(self, state)
|
|
244
239
|
# A handler still running at the limit loses its transaction: later commands fail, nothing commits,
|
|
245
240
|
# and the Preview answers 500 when it ends.
|
|
@@ -256,9 +251,7 @@ module Sendproof
|
|
|
256
251
|
# is ever committed: no +commitTransaction+ is ever sent for the Preview session.
|
|
257
252
|
def finish_preview(recorder)
|
|
258
253
|
current = recorder.store_state(self)
|
|
259
|
-
|
|
260
|
-
Thread.current[:sendproof_mongo_refused]&.clear
|
|
261
|
-
Thread.current[:sendproof_mongo_pending]&.clear
|
|
254
|
+
clear_monitoring
|
|
262
255
|
return unless current&.session
|
|
263
256
|
|
|
264
257
|
failure = nil
|
|
@@ -285,6 +278,59 @@ module Sendproof
|
|
|
285
278
|
raise failure if failure
|
|
286
279
|
end
|
|
287
280
|
|
|
281
|
+
def begin_held_preview(&validate_client)
|
|
282
|
+
unless active? && hooked?(@client) && detect[:mode] == :preview
|
|
283
|
+
raise ConfigurationError, "MongoDB cannot hold a Preview transaction on this client."
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
state = start_state(nil)
|
|
287
|
+
state.held = HeldSession.new(self, state, @timeout, validate_client)
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
def attach_held(state, recorder)
|
|
291
|
+
raise PreviewSessions::StoreEnded unless active? && hooked?(@client)
|
|
292
|
+
|
|
293
|
+
state.used = false
|
|
294
|
+
state.candidates.clear
|
|
295
|
+
state.filters.clear
|
|
296
|
+
recorder.set_store_state(self, state)
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
def capture_held(state, recorder)
|
|
300
|
+
internal { capture_changes(recorder, state) } if state.used
|
|
301
|
+
rescue ::Mongo::Error::OperationFailure => error
|
|
302
|
+
raise PreviewSessions::StoreEnded if error.code == NO_SUCH_TRANSACTION
|
|
303
|
+
|
|
304
|
+
raise
|
|
305
|
+
ensure
|
|
306
|
+
clear_monitoring
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
def restore_held(state, images)
|
|
310
|
+
internal do
|
|
311
|
+
# Clear the changed documents before restoring their versions. Two documents can
|
|
312
|
+
# have exchanged values of a unique field during a step.
|
|
313
|
+
images.each do |candidate, _|
|
|
314
|
+
@client.use(candidate.namespace)[candidate.collection].delete_one({ _id: candidate.id }, session: state.session)
|
|
315
|
+
end
|
|
316
|
+
images.each do |candidate, before|
|
|
317
|
+
next unless before
|
|
318
|
+
|
|
319
|
+
@client.use(candidate.namespace)[candidate.collection].insert_one(before, session: state.session)
|
|
320
|
+
end
|
|
321
|
+
end
|
|
322
|
+
rescue ::Mongo::Error::OperationFailure => error
|
|
323
|
+
raise PreviewSessions::StoreEnded if error.code == NO_SUCH_TRANSACTION
|
|
324
|
+
|
|
325
|
+
raise
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
def close_held(state)
|
|
329
|
+
clear_monitoring
|
|
330
|
+
end_session(state)
|
|
331
|
+
raise PreviewTransactionControl, "MongoDB did not acknowledge the session's rollback." if state.killer
|
|
332
|
+
end
|
|
333
|
+
|
|
288
334
|
# ---- Monitoring ----
|
|
289
335
|
|
|
290
336
|
# Called by the subscriber before a command is written. During a Preview it classifies the command
|
|
@@ -368,8 +414,12 @@ module Sendproof
|
|
|
368
414
|
|
|
369
415
|
tripwire(event)
|
|
370
416
|
pending = take(event) or return
|
|
417
|
+
current = pending.preview && state_for(pending.preview)
|
|
418
|
+
if current&.held && (failure["code"] == NO_SUCH_TRANSACTION || Array(failure["errorLabels"]).include?("TransientTransactionError"))
|
|
419
|
+
current.held.server_ended!
|
|
420
|
+
end
|
|
371
421
|
# The server refused something a transaction can't do and applied nothing: a block, not a crash.
|
|
372
|
-
if
|
|
422
|
+
if current && failure["code"] == 263
|
|
373
423
|
pending.preview.block(store: "mongodb", operation: pending.name, detail: Mongo.refused_reason(pending.name))
|
|
374
424
|
end
|
|
375
425
|
log(pending, event.duration, nil, failure["codeName"] || "Error")
|
|
@@ -377,6 +427,24 @@ module Sendproof
|
|
|
377
427
|
|
|
378
428
|
private
|
|
379
429
|
|
|
430
|
+
def start_state(recorder)
|
|
431
|
+
session = @client.start_session(causal_consistency: false)
|
|
432
|
+
lift_direct_connection_checks(session)
|
|
433
|
+
session.start_transaction(read_concern: { level: :snapshot }, write_concern: { w: :majority })
|
|
434
|
+
join_transaction(session, recorder)
|
|
435
|
+
State.new(mode: :preview, session: session, lsid: hex(session.session_id), used: false, opened: false,
|
|
436
|
+
finished: false, expired: false, candidates: {}, filters: [], lock: Mutex.new)
|
|
437
|
+
rescue StandardError
|
|
438
|
+
release(session) if session
|
|
439
|
+
raise
|
|
440
|
+
end
|
|
441
|
+
|
|
442
|
+
def clear_monitoring
|
|
443
|
+
# Refused commands were never sent, so there is no reply to remove their monitoring entry.
|
|
444
|
+
Thread.current[:sendproof_mongo_refused]&.clear
|
|
445
|
+
Thread.current[:sendproof_mongo_pending]&.clear
|
|
446
|
+
end
|
|
447
|
+
|
|
380
448
|
def state_for(recorder) = recorder.store_state(self)&.then { |state| state.session ? state : nil }
|
|
381
449
|
|
|
382
450
|
# The driver hides the outcome of +abortTransaction+, so monitoring notes it while the engine aborts:
|
|
@@ -408,7 +476,14 @@ module Sendproof
|
|
|
408
476
|
if (refused = classification.refused_in_transaction)
|
|
409
477
|
Stores::Guard.guard("mongodb", name, :write, Mongo.refused_reason(refused))
|
|
410
478
|
end
|
|
411
|
-
|
|
479
|
+
if classification.kind == :write
|
|
480
|
+
if state&.held && !%w[insert update delete findandmodify bulkwrite].include?(name.downcase)
|
|
481
|
+
Stores::Guard.guard("mongodb", name, :write, Mongo.refused_reason(name))
|
|
482
|
+
end
|
|
483
|
+
verify_in_transaction(preview, state, event, command)
|
|
484
|
+
elsif state&.held && classification.kind == :read && hex(command["lsid"]) != state.lsid
|
|
485
|
+
raise refuse(preview, "A MongoDB #{name} read escaped the held Preview transaction.")
|
|
486
|
+
end
|
|
412
487
|
else
|
|
413
488
|
Stores::Guard.guard("mongodb", name, classification.kind)
|
|
414
489
|
end
|
|
@@ -453,13 +528,15 @@ module Sendproof
|
|
|
453
528
|
when "update"
|
|
454
529
|
Array(command["updates"]).each do |update|
|
|
455
530
|
via = update["upsert"] ? :upsert : (replaces?(update["u"]) ? :replace : nil)
|
|
456
|
-
state.filters << Filter.new(namespace, collection, update["q"] || {}, via)
|
|
531
|
+
state.filters << Filter.new(namespace, collection, update["q"] || {}, via, update["collation"])
|
|
457
532
|
end
|
|
458
533
|
when "delete"
|
|
459
|
-
Array(command["deletes"]).each
|
|
534
|
+
Array(command["deletes"]).each do |remove|
|
|
535
|
+
state.filters << Filter.new(namespace, collection, remove["q"] || {}, nil, remove["collation"])
|
|
536
|
+
end
|
|
460
537
|
when "findandmodify"
|
|
461
538
|
via = command["upsert"] ? :upsert : (replaces?(command["update"]) ? :replace : nil)
|
|
462
|
-
state.filters << Filter.new(namespace, collection, command["query"] || {}, via)
|
|
539
|
+
state.filters << Filter.new(namespace, collection, command["query"] || {}, via, command["collation"])
|
|
463
540
|
when "bulkwrite"
|
|
464
541
|
collect_bulk(state, namespace, command)
|
|
465
542
|
end
|
|
@@ -476,7 +553,7 @@ module Sendproof
|
|
|
476
553
|
if op["document"]
|
|
477
554
|
candidate(state, Candidate.new(database, target.to_s, op["document"]["_id"], nil))
|
|
478
555
|
else
|
|
479
|
-
state.filters << Filter.new(database, target.to_s, op["filter"] || {}, op["upsert"] ? :upsert : nil)
|
|
556
|
+
state.filters << Filter.new(database, target.to_s, op["filter"] || {}, op["upsert"] ? :upsert : nil, op["collation"])
|
|
480
557
|
end
|
|
481
558
|
end
|
|
482
559
|
end
|
|
@@ -484,11 +561,13 @@ module Sendproof
|
|
|
484
561
|
def candidate(state, candidate)
|
|
485
562
|
return if candidate.id.nil?
|
|
486
563
|
|
|
487
|
-
key =
|
|
564
|
+
key = candidate_key(candidate)
|
|
488
565
|
known = state.candidates[key]
|
|
489
566
|
state.candidates[key] = candidate if known.nil? || (known.via.nil? && candidate.via)
|
|
490
567
|
end
|
|
491
568
|
|
|
569
|
+
def candidate_key(candidate) = [candidate.namespace, candidate.collection, Plain.call(candidate.id).to_json]
|
|
570
|
+
|
|
492
571
|
# Whether an update document replaces the whole document rather than applying operators.
|
|
493
572
|
def replaces?(update)
|
|
494
573
|
update.is_a?(Hash) && !update.empty? && !update.keys.first.to_s.start_with?("$")
|
|
@@ -530,7 +609,7 @@ module Sendproof
|
|
|
530
609
|
def join_transaction(session, recorder)
|
|
531
610
|
refuse_call = lambda do |what|
|
|
532
611
|
error = PreviewTransactionControl.new("Preview owns the MongoDB transaction, so the handler's #{what} cannot run during Preview.")
|
|
533
|
-
recorder.refuse(error)
|
|
612
|
+
(recorder || Capture.preview)&.refuse(error)
|
|
534
613
|
error
|
|
535
614
|
end
|
|
536
615
|
session.define_singleton_method(:start_transaction) { |*_args| nil }
|
|
@@ -642,11 +721,13 @@ module Sendproof
|
|
|
642
721
|
# is a raw +find+ command. Without a start time (no reply carried one), it reads the majority-committed
|
|
643
722
|
# view, which the spec allows: nothing the Preview wrote is committed, so the only difference is a
|
|
644
723
|
# concurrent commit by another request.
|
|
645
|
-
def at_start(state, namespace, collection, filter, projection = nil)
|
|
724
|
+
def at_start(state, namespace, collection, filter, projection = nil, collation: nil)
|
|
646
725
|
read_concern = state.start_time.nil? ? { level: "majority" } : { level: "snapshot", atClusterTime: state.start_time }
|
|
647
|
-
|
|
726
|
+
limit = state.held ? CANDIDATE_LIMIT + 1 : CANDIDATE_LIMIT
|
|
727
|
+
command = { find: collection, filter: filter, batchSize: limit, limit:, singleBatch: true,
|
|
648
728
|
readConcern: read_concern }
|
|
649
729
|
command[:projection] = projection if projection
|
|
730
|
+
command[:collation] = collation if collation
|
|
650
731
|
reply = @client.use(namespace).database.command(command).first
|
|
651
732
|
reply.dig("cursor", "firstBatch") || []
|
|
652
733
|
rescue ::Mongo::Error::OperationFailure => error
|
|
@@ -661,12 +742,22 @@ module Sendproof
|
|
|
661
742
|
session = state.session
|
|
662
743
|
state.filters.each do |filter|
|
|
663
744
|
target = @client.use(filter.namespace)[filter.collection]
|
|
664
|
-
|
|
745
|
+
options = { projection: { _id: 1 }, session: session }
|
|
746
|
+
options[:collation] = filter.collation if filter.collation
|
|
747
|
+
options[:limit] = CANDIDATE_LIMIT + 1 if state.held
|
|
748
|
+
matches = target.find(filter.filter, **options).to_a +
|
|
749
|
+
at_start(state, filter.namespace, filter.collection, filter.filter, { _id: 1 }, collation: filter.collation)
|
|
750
|
+
# An earlier step may have inserted a document that this step deletes. It is absent
|
|
751
|
+
# both from the original snapshot and from this step's final view.
|
|
752
|
+
state.held.known_candidates(filter).each { |item| candidate(state, item) } if state.held
|
|
665
753
|
matches.each do |document|
|
|
666
754
|
candidate(state, Candidate.new(filter.namespace, filter.collection, document["_id"], filter.via))
|
|
667
|
-
break if state.candidates.size >= CANDIDATE_LIMIT
|
|
755
|
+
break if !state.held && state.candidates.size >= CANDIDATE_LIMIT
|
|
668
756
|
end
|
|
669
757
|
end
|
|
758
|
+
if state.held && state.candidates.size > CANDIDATE_LIMIT
|
|
759
|
+
raise PreviewSessions::StoreEnded, "A MongoDB Preview step exceeded the before-image limit."
|
|
760
|
+
end
|
|
670
761
|
read = 0
|
|
671
762
|
state.candidates.each_value do |candidate|
|
|
672
763
|
if (read += 1) > CANDIDATE_LIMIT
|
|
@@ -674,12 +765,18 @@ module Sendproof
|
|
|
674
765
|
break
|
|
675
766
|
end
|
|
676
767
|
target = @client.use(candidate.namespace)[candidate.collection]
|
|
677
|
-
|
|
768
|
+
key = candidate_key(candidate)
|
|
769
|
+
before = if state.held&.known?(key)
|
|
770
|
+
state.held.known_image(key)
|
|
771
|
+
else
|
|
772
|
+
at_start(state, candidate.namespace, candidate.collection, { _id: candidate.id }).first
|
|
773
|
+
end
|
|
678
774
|
after = target.find({ _id: candidate.id }, session: session).first
|
|
679
775
|
was = before && Plain.call(before)
|
|
680
776
|
now = after && Plain.call(after)
|
|
681
777
|
next if was == now
|
|
682
778
|
|
|
779
|
+
state.held&.record(key, candidate, before, after)
|
|
683
780
|
operation = was.nil? ? "insert" : (now.nil? ? "delete" : "update")
|
|
684
781
|
change = { operation: operation, store: "mongodb", namespace: candidate.namespace, table: candidate.collection,
|
|
685
782
|
key: { "_id" => Plain.call(candidate.id) }, before: was, after: now }
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Sendproof
|
|
4
|
+
module Mongo
|
|
5
|
+
# MongoDB has no savepoints. The latest images give a step its starting view,
|
|
6
|
+
# and the per-step before images let rewind restore that view inside the transaction.
|
|
7
|
+
class HeldSession
|
|
8
|
+
def initialize(engine, state, timeout, validate_client)
|
|
9
|
+
@engine, @state = engine, state
|
|
10
|
+
@timeout = timeout
|
|
11
|
+
@validate_client = validate_client
|
|
12
|
+
@images, @steps = {}, {}
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def begin_step(step, recorder)
|
|
16
|
+
check_deadline
|
|
17
|
+
@validate_client&.call
|
|
18
|
+
@step = step
|
|
19
|
+
@steps[@step] = {}
|
|
20
|
+
@engine.attach_held(@state, recorder)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def capture_step(recorder)
|
|
24
|
+
check_deadline
|
|
25
|
+
@engine.capture_held(@state, recorder)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def known?(key) = @images.key?(key)
|
|
29
|
+
def known_image(key) = @images[key].last
|
|
30
|
+
|
|
31
|
+
def known_candidates(filter)
|
|
32
|
+
@images.values.filter_map do |candidate, image|
|
|
33
|
+
candidate if image && candidate.namespace == filter.namespace && candidate.collection == filter.collection
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def record(key, candidate, before, after)
|
|
38
|
+
@steps[@step][key] = [candidate, before]
|
|
39
|
+
@images[key] = [candidate, after]
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def rewind(step)
|
|
43
|
+
check_deadline
|
|
44
|
+
@steps.keys.select { |number| number >= step }.sort.reverse_each do |number|
|
|
45
|
+
images = @steps.delete(number)
|
|
46
|
+
@engine.restore_held(@state, images.values)
|
|
47
|
+
@images.merge!(images)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def server_ended! = @ended = true
|
|
52
|
+
|
|
53
|
+
def close = @engine.close_held(@state)
|
|
54
|
+
|
|
55
|
+
def expires_at
|
|
56
|
+
@deadline ||= Clock.now + @timeout if @state.opened
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
private
|
|
60
|
+
|
|
61
|
+
def check_deadline
|
|
62
|
+
deadline = expires_at
|
|
63
|
+
raise PreviewSessions::StoreEnded if @ended || (deadline && Clock.now >= deadline)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
data/lib/sendproof/mongo.rb
CHANGED
|
@@ -21,6 +21,7 @@ module Sendproof
|
|
|
21
21
|
# the command is sent, any write during a Preview that doesn't carry the Preview's session and
|
|
22
22
|
# transaction, so a rollback is never claimed for a write that escaped.
|
|
23
23
|
module Mongo
|
|
24
|
+
autoload :HeldSession, "sendproof/mongo/held_session"
|
|
24
25
|
autoload :Engine, "sendproof/mongo/engine"
|
|
25
26
|
autoload :Guarded, "sendproof/mongo/guarded"
|
|
26
27
|
autoload :Plain, "sendproof/mongo/plain"
|
|
@@ -41,18 +41,28 @@ module Sendproof
|
|
|
41
41
|
# Refuses to start when Mongoid now uses a client the store never hooked (one it built after
|
|
42
42
|
# +Mongoid::Clients.clear+): that client's writes would be neither guarded nor rolled back.
|
|
43
43
|
def begin_preview(_context, recorder)
|
|
44
|
-
|
|
45
|
-
raise ConfigurationError, "Mongoid rebuilt its #{@client_name.inspect} client after Sendproof hooked it; " \
|
|
46
|
-
"create the Sendproof::Mongoid::Store after Mongoid's clients are configured."
|
|
47
|
-
end
|
|
48
|
-
|
|
44
|
+
check_client
|
|
49
45
|
@engine.begin_preview(recorder)
|
|
50
46
|
end
|
|
51
47
|
|
|
48
|
+
def begin_held_preview(_context)
|
|
49
|
+
check_client
|
|
50
|
+
@engine.begin_held_preview { check_client }
|
|
51
|
+
end
|
|
52
|
+
|
|
52
53
|
def finish_preview(recorder) = @engine.finish_preview(recorder)
|
|
53
54
|
|
|
54
55
|
# Turns the hook off: nothing is guarded or logged afterwards.
|
|
55
56
|
def unhook! = @engine.unhook!
|
|
57
|
+
|
|
58
|
+
private
|
|
59
|
+
|
|
60
|
+
def check_client
|
|
61
|
+
if @engine.active? && !@engine.hooked?(::Mongoid::Clients.with_name(@client_name))
|
|
62
|
+
raise ConfigurationError, "Mongoid rebuilt its #{@client_name.inspect} client after Sendproof hooked it; " \
|
|
63
|
+
"create the Sendproof::Mongoid::Store after Mongoid's clients are configured."
|
|
64
|
+
end
|
|
65
|
+
end
|
|
56
66
|
end
|
|
57
67
|
end
|
|
58
68
|
end
|
|
@@ -7,14 +7,17 @@ module Sendproof
|
|
|
7
7
|
# @return [#rollback!, nil] the provider session opened before the handler ran
|
|
8
8
|
attr_accessor :session
|
|
9
9
|
|
|
10
|
-
def initialize(recorder:, clock:)
|
|
10
|
+
def initialize(recorder:, clock:, session_get: false)
|
|
11
11
|
@recorder = recorder
|
|
12
12
|
@clock = clock
|
|
13
13
|
@invoked = false
|
|
14
14
|
@completed = false
|
|
15
15
|
@rejected = false
|
|
16
|
+
@session_get = session_get
|
|
16
17
|
end
|
|
17
18
|
|
|
19
|
+
def session_get? = @session_get
|
|
20
|
+
|
|
18
21
|
# @return [Boolean] whether the endpoint handler started
|
|
19
22
|
def invoked? = @invoked
|
|
20
23
|
# @return [Boolean] whether the endpoint handler returned normally
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Sendproof
|
|
4
|
+
# The process-local Preview session registry and its Rack endpoints.
|
|
5
|
+
class PreviewSessions
|
|
6
|
+
ROUTE = %r{\A/sendproof/_api/sessions(?:/([^/]+)(/rewind)?)?\z}
|
|
7
|
+
HEADER = "HTTP_SENDPROOF_SESSION"
|
|
8
|
+
|
|
9
|
+
class StoreBusy < StandardError; end
|
|
10
|
+
class StoreEnded < StandardError; end
|
|
11
|
+
class StepUndone < StandardError; end
|
|
12
|
+
|
|
13
|
+
def initialize(config, idle_timeout: 300, max_lifetime: 1800)
|
|
14
|
+
@config = config
|
|
15
|
+
@idle_timeout = idle_timeout
|
|
16
|
+
@max_lifetime = max_lifetime
|
|
17
|
+
@mutex = Mutex.new
|
|
18
|
+
@sessions = {}
|
|
19
|
+
@stopped = false
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def call(env, request, id, rewind)
|
|
23
|
+
return open(env, request) if id.nil? && request.post?
|
|
24
|
+
return with_session(request, id, &:end!) if id && !rewind && request.delete?
|
|
25
|
+
return with_session(request, id) { |session| rewind_request(request, session) } if rewind && request.post?
|
|
26
|
+
|
|
27
|
+
Protocol.problem(405, "This method is unavailable for preview sessions.")
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def with_session(request, id, &block)
|
|
31
|
+
owner = owner_of(request) if Protocol.same_origin?(request)
|
|
32
|
+
session = @mutex.synchronize { @sessions[id] }
|
|
33
|
+
return not_found unless owner && session&.owner == owner
|
|
34
|
+
|
|
35
|
+
session.run(&block)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Close on the server's shutdown hook, or as part of orderly Ruby process shutdown.
|
|
39
|
+
def shutdown
|
|
40
|
+
sessions = @mutex.synchronize do
|
|
41
|
+
@stopped = true
|
|
42
|
+
@sessions.values
|
|
43
|
+
end
|
|
44
|
+
sessions.each(&:stop)
|
|
45
|
+
sessions.each(&:join)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def self.not_found = Protocol.problem(404, "Preview session not found.", code: "session_not_found")
|
|
49
|
+
def self.failed = Protocol.problem(409, "The preview session could not hold all required stores.", code: "session_failed")
|
|
50
|
+
def self.no_content = [204, { "cache-control" => "no-store" }, []]
|
|
51
|
+
|
|
52
|
+
private
|
|
53
|
+
|
|
54
|
+
def not_found = self.class.not_found
|
|
55
|
+
|
|
56
|
+
def owner_of(request)
|
|
57
|
+
identity = @config.identity_for(request)
|
|
58
|
+
identity ? identity.owner(request.script_name) : [:anonymous, request.script_name]
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def open(env, request)
|
|
62
|
+
return Protocol.problem(403, "Preview sessions must be opened from the same origin.") unless Protocol.same_origin?(request)
|
|
63
|
+
|
|
64
|
+
owner = owner_of(request)
|
|
65
|
+
return Protocol.problem(403, "A preview session needs one stable subject for the signed-in caller.") unless owner
|
|
66
|
+
|
|
67
|
+
provider = @config.preview_provider
|
|
68
|
+
return Protocol.problem(409, "Preview sessions are unavailable.") unless @config.preview_sessions?(provider)
|
|
69
|
+
|
|
70
|
+
context = ProviderContext.new(env:, request:, config: @config)
|
|
71
|
+
session = @mutex.synchronize do
|
|
72
|
+
return self.class.failed if @stopped
|
|
73
|
+
if @sessions.size >= 16 || @sessions.values.count { |item| item.owner == owner } >= 4
|
|
74
|
+
return Protocol.problem(429, "Too many preview sessions are open. End one first.", code: "session_limit")
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
unless @shutdown_registered
|
|
78
|
+
at_exit { shutdown }
|
|
79
|
+
@shutdown_registered = true
|
|
80
|
+
end
|
|
81
|
+
id = SecureRandom.hex(16)
|
|
82
|
+
@sessions[id] = Worker.new(id, owner, context, provider, @idle_timeout, @max_lifetime) do
|
|
83
|
+
@mutex.synchronize { @sessions.delete(id) }
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
session.opened
|
|
87
|
+
rescue StoreBusy => error
|
|
88
|
+
Protocol.problem(409, "Another Preview session is holding #{error.message}.",
|
|
89
|
+
code: "store_busy", idleTimeoutSeconds: @idle_timeout,
|
|
90
|
+
detail: "That session ends by itself after its idle timeout (#{@idle_timeout} seconds), or when it is ended.")
|
|
91
|
+
rescue StandardError => error
|
|
92
|
+
@config.logger&.error("A Sendproof preview session could not open: #{error.class}: #{error.message}")
|
|
93
|
+
self.class.failed
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def rewind_request(request, session)
|
|
97
|
+
body = request.body&.read(1025).to_s
|
|
98
|
+
return invalid_step(session) if body.bytesize > 1024
|
|
99
|
+
|
|
100
|
+
document = JSON.parse(body)
|
|
101
|
+
step = document["step"] if document.is_a?(Hash)
|
|
102
|
+
return invalid_step(session) unless step.is_a?(Integer) && step.between?(1, session.next_step)
|
|
103
|
+
|
|
104
|
+
session.rewind(step)
|
|
105
|
+
self.class.no_content
|
|
106
|
+
rescue JSON::ParserError
|
|
107
|
+
invalid_step(session)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def invalid_step(session)
|
|
111
|
+
Protocol.problem(400, "Rewind to a step from 1 to #{session.next_step}, the next step this session runs.")
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# A worker leases all connections and runs each step on their owning thread. Its mailbox also
|
|
115
|
+
# wakes up at the next deadline, so abandoned sessions release their locks without another request.
|
|
116
|
+
class Worker
|
|
117
|
+
attr_reader :id, :owner, :next_step, :sql, :stores, :guards
|
|
118
|
+
|
|
119
|
+
def initialize(id, owner, context, provider, idle_timeout, max_lifetime, &remove)
|
|
120
|
+
@id, @owner = id, owner
|
|
121
|
+
@config = context.config
|
|
122
|
+
@idle_timeout = idle_timeout
|
|
123
|
+
@max_lifetime = max_lifetime
|
|
124
|
+
@next_step = 1
|
|
125
|
+
@parts, @stores, @guards = [], [], []
|
|
126
|
+
@mutex = Mutex.new
|
|
127
|
+
@jobs, @opened = Queue.new, Queue.new
|
|
128
|
+
@busy = true
|
|
129
|
+
@thread = Thread.new { work(context, provider, remove) }
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def opened = receive(@opened)
|
|
133
|
+
|
|
134
|
+
def run(&block)
|
|
135
|
+
result = Queue.new
|
|
136
|
+
@mutex.synchronize do
|
|
137
|
+
return PreviewSessions.not_found if @stopping || (@deadline && Clock.now >= @deadline) ||
|
|
138
|
+
(!@busy && Clock.now >= @expires)
|
|
139
|
+
return Protocol.problem(409, "The preview session is serving another request.", code: "session_busy") if @busy
|
|
140
|
+
|
|
141
|
+
@busy = true
|
|
142
|
+
@jobs << [@config.session_job(&block), result]
|
|
143
|
+
end
|
|
144
|
+
receive(result)
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def advance = @next_step += 1
|
|
148
|
+
|
|
149
|
+
def undo_step = @parts.reverse_each { |part| part.rewind(@next_step) }
|
|
150
|
+
|
|
151
|
+
def rewind(step)
|
|
152
|
+
return if step == @next_step
|
|
153
|
+
|
|
154
|
+
@parts.reverse_each { |part| part.rewind(step) }
|
|
155
|
+
@next_step = step
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def end!
|
|
159
|
+
@ending = true
|
|
160
|
+
stop
|
|
161
|
+
PreviewSessions.no_content
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def stop
|
|
165
|
+
@mutex.synchronize do
|
|
166
|
+
return if @stopping
|
|
167
|
+
|
|
168
|
+
@stopping = true
|
|
169
|
+
@jobs.close
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def join = @thread.join
|
|
174
|
+
|
|
175
|
+
private
|
|
176
|
+
|
|
177
|
+
def work(context, provider, remove)
|
|
178
|
+
result = @opened
|
|
179
|
+
response = nil
|
|
180
|
+
begin
|
|
181
|
+
@sql = provider.begin_held_preview(context)
|
|
182
|
+
raise StoreEnded unless @sql
|
|
183
|
+
|
|
184
|
+
@parts << @sql
|
|
185
|
+
@config.data_stores.each do |store|
|
|
186
|
+
if store.describe[:mode].to_s == "guard"
|
|
187
|
+
@guards << store
|
|
188
|
+
else
|
|
189
|
+
part = store.begin_held_preview(context)
|
|
190
|
+
raise StoreEnded unless part
|
|
191
|
+
|
|
192
|
+
@parts << part
|
|
193
|
+
@stores << part
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
now = Clock.now
|
|
197
|
+
@deadline = now + @max_lifetime
|
|
198
|
+
raise StoreEnded if release(now)
|
|
199
|
+
result << Protocol.json(201, { id:, idleTimeoutSeconds: @idle_timeout, maxLifetimeSeconds: @max_lifetime },
|
|
200
|
+
{ "cache-control" => "no-store" })
|
|
201
|
+
result = nil
|
|
202
|
+
while (job = @jobs.pop(timeout: [@expires - Clock.now, 0].max))
|
|
203
|
+
block, result = job
|
|
204
|
+
response = Attribution.request { block.call(self) }
|
|
205
|
+
break if release(Clock.now)
|
|
206
|
+
|
|
207
|
+
result << response
|
|
208
|
+
result = nil
|
|
209
|
+
end
|
|
210
|
+
rescue StoreEnded => error
|
|
211
|
+
response = result == @opened ? error : PreviewSessions.failed
|
|
212
|
+
rescue Exception => error # every acquisition and request unwinding must return its connections
|
|
213
|
+
response = result == @opened || !error.is_a?(StandardError) ? error : fail_request(error)
|
|
214
|
+
ensure
|
|
215
|
+
stop
|
|
216
|
+
failure = close_parts
|
|
217
|
+
remove.call
|
|
218
|
+
response = fail_request(failure) if failure && @ending
|
|
219
|
+
result << response if result
|
|
220
|
+
@jobs.pop&.last&.push(PreviewSessions.not_found)
|
|
221
|
+
end
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
def release(now)
|
|
225
|
+
@mutex.synchronize do
|
|
226
|
+
store_deadlines = @stores.filter_map { |store| store.expires_at if store.respond_to?(:expires_at) }
|
|
227
|
+
@expires = [now + @idle_timeout, @deadline, *store_deadlines].min
|
|
228
|
+
@busy = false
|
|
229
|
+
@stopping || now >= @deadline
|
|
230
|
+
end
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
def close_parts
|
|
234
|
+
failure = nil
|
|
235
|
+
@parts.reverse_each do |part|
|
|
236
|
+
part.close
|
|
237
|
+
rescue Exception => error # even a failed store close must release the other held stores
|
|
238
|
+
@config.logger&.error("A held Preview transaction could not be rolled back: #{error.class}: #{error.message}")
|
|
239
|
+
failure ||= error
|
|
240
|
+
end
|
|
241
|
+
failure
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
def fail_request(error)
|
|
245
|
+
@config.logger&.error("A Sendproof preview session could not continue: #{error.class}: #{error.message}")
|
|
246
|
+
Protocol.problem(500, "The preview session could not continue safely and was closed.")
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
def receive(queue)
|
|
250
|
+
value = @config.wait_for_session { queue.pop }
|
|
251
|
+
raise value if value.is_a?(Exception)
|
|
252
|
+
|
|
253
|
+
value
|
|
254
|
+
end
|
|
255
|
+
end
|
|
256
|
+
end
|
|
257
|
+
end
|