|
| 1 | +# rbs_inline: enabled |
| 2 | + |
| 3 | +# An executable proof for the at-least-once clause: a contract clause |
| 4 | +# nobody can observe firing is decoration. Run with: |
| 5 | +# |
| 6 | +# bundle exec rake at_least_once |
| 7 | +# |
| 8 | +# Phase one crashes an effect worker between the external sink write and |
| 9 | +# the acknowledgement, restarts one, and shows the sink reading 2 with |
| 10 | +# deduplication off. Both deliveries carry the same stable effect id. |
| 11 | +# Phase two repeats the crash with a guard on that id; the sink reads 1. |
| 12 | +# The actor state commits exactly once in both phases. |
| 13 | + |
| 14 | +require_relative "boot" |
| 15 | +require_relative "actor" |
| 16 | +require_relative "sink" |
| 17 | +require "fileutils" |
| 18 | +require "json" |
| 19 | +require "rbconfig" |
| 20 | +require "tmpdir" |
| 21 | + |
| 22 | +directory = Dir.mktmpdir("solid_objects_at_least_once_") |
| 23 | +database_path = File.join(directory, "state.sqlite3") |
| 24 | +AtLeastOnceBoot.call(database_path) |
| 25 | + |
| 26 | +# @rbs (String message) -> void |
| 27 | +def prove(message) |
| 28 | + raise "proof failed: #{message}" unless yield |
| 29 | +end |
| 30 | + |
| 31 | +# @rbs (String actor_id) -> void |
| 32 | +def stage_one_delivery(actor_id) |
| 33 | + DeliveryCounter.ref(actor_id).async.deliver |
| 34 | + worker = SolidObjects::Worker.new |
| 35 | + begin |
| 36 | + worker.run_until_idle |
| 37 | + ensure |
| 38 | + worker.stop |
| 39 | + end |
| 40 | +end |
| 41 | + |
| 42 | +# @rbs (database_path: String, sink_path: String, mode: String, deduplication: String) -> Integer? |
| 43 | +def run_effect_worker(database_path:, sink_path:, mode:, deduplication:) |
| 44 | + script = File.expand_path("effect_worker.rb", __dir__) |
| 45 | + pid = Process.spawn( |
| 46 | + RbConfig.ruby, script, database_path, sink_path, mode, deduplication, |
| 47 | + chdir: AtLeastOnceBoot::ROOT |
| 48 | + ) |
| 49 | + _pid, status = Process.wait2(pid) |
| 50 | + status.exitstatus |
| 51 | +end |
| 52 | + |
| 53 | +# @rbs (database_path: String, sink_path: String, deduplication: String) -> void |
| 54 | +def crash_then_recover(database_path:, sink_path:, deduplication:) |
| 55 | + crash = run_effect_worker(database_path:, sink_path:, mode: "crash", deduplication:) |
| 56 | + prove("the first delivery crashed before acknowledgement") { crash == 1 } |
| 57 | + sleep 0.4 |
| 58 | + recovery = run_effect_worker(database_path:, sink_path:, mode: "complete", deduplication:) |
| 59 | + prove("the second delivery completed and acknowledged") { recovery == 0 } |
| 60 | +end |
| 61 | + |
| 62 | +begin |
| 63 | + sink_off = File.join(directory, "sink-dedup-off.json") |
| 64 | + stage_one_delivery("dedup-off") |
| 65 | + crash_then_recover(database_path:, sink_path: sink_off, deduplication: "off") |
| 66 | + deliveries = AtLeastOnceSink.read(sink_off) |
| 67 | + effect_ids = deliveries.map { |delivery| delivery.fetch("effect_id") } |
| 68 | + state_off = SolidObjects::Instance.find_by!(actor_id: "dedup-off").state.fetch("count") |
| 69 | + prove("the state commit happened exactly once") { state_off == 1 } |
| 70 | + prove("the sink observed the duplicate") { deliveries.length == 2 } |
| 71 | + prove("both deliveries carried the same stable effect id") { effect_ids.uniq.length == 1 } |
| 72 | + |
| 73 | + sink_on = File.join(directory, "sink-dedup-on.json") |
| 74 | + stage_one_delivery("dedup-on") |
| 75 | + crash_then_recover(database_path:, sink_path: sink_on, deduplication: "on") |
| 76 | + guarded = AtLeastOnceSink.read(sink_on) |
| 77 | + state_on = SolidObjects::Instance.find_by!(actor_id: "dedup-on").state.fetch("count") |
| 78 | + prove("the state commit happened exactly once") { state_on == 1 } |
| 79 | + prove("the stable effect id absorbed the duplicate") { guarded.length == 1 } |
| 80 | + |
| 81 | + puts JSON.pretty_generate( |
| 82 | + duplicate: { |
| 83 | + state_commits: state_off, |
| 84 | + sink_deliveries: deliveries.length, |
| 85 | + same_effect_id: effect_ids.uniq.length == 1, |
| 86 | + attempts: deliveries.map { |delivery| delivery.fetch("attempt") } |
| 87 | + }, |
| 88 | + remedy: { state_commits: state_on, sink_deliveries: guarded.length } |
| 89 | + ) |
| 90 | +ensure |
| 91 | + FileUtils.remove_entry(directory) if directory |
| 92 | +end |
0 commit comments