orfeas_lyra 1.0.0 → 1.0.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 +4 -4
- data/CHANGELOG.md +387 -0
- data/README.md +20 -8
- data/Rakefile +1 -1
- data/lib/generators/lyra/install/install_generator.rb +49 -0
- data/lib/generators/lyra/install/templates/create_lyra_tables.rb.tt +163 -0
- data/lib/generators/lyra/install/templates/initializer.rb.tt +22 -0
- data/lib/lyra/associations/habtm_links.rb +4 -0
- data/lib/lyra/bypass_events.rb +37 -5
- data/lib/lyra/command_handler.rb +5 -0
- data/lib/lyra/configuration.rb +32 -3
- data/lib/lyra/dual_view.rb +5 -2
- data/lib/lyra/effect_check.rb +52 -0
- data/lib/lyra/engine.rb +13 -0
- data/lib/lyra/erasure.rb +4 -4
- data/lib/lyra/event_store_adapter.rb +59 -0
- data/lib/lyra/genesis.rb +16 -4
- data/lib/lyra/interceptors/crud_interceptor.rb +70 -8
- data/lib/lyra/interceptors/lazy_reads.rb +54 -1
- data/lib/lyra/mode_sync.rb +86 -0
- data/lib/lyra/mode_transition.rb +96 -3
- data/lib/lyra/projections/async_checkpoint.rb +14 -1
- data/lib/lyra/projections/cached_relation.rb +194 -3
- data/lib/lyra/projections/lazy_projection.rb +496 -68
- data/lib/lyra/projections/model_projection.rb +12 -5
- data/lib/lyra/projections/rebuild.rb +163 -6
- data/lib/lyra/projections/unique_indexes.rb +193 -13
- data/lib/lyra/projections/uniqueness.rb +65 -2
- data/lib/lyra/repair.rb +3 -1
- data/lib/lyra/stream_heads.rb +309 -0
- data/lib/lyra/strict_data_access.rb +12 -4
- data/lib/lyra/verification/async_lock_order_net.rb +327 -0
- data/lib/lyra/verification/bypass_workflow.rb +3 -2
- data/lib/lyra/verification/concurrent_writers_net.rb +350 -0
- data/lib/lyra/verification/gate_settle_net.rb +208 -0
- data/lib/lyra/verification/lazy_catch_up_net.rb +228 -0
- data/lib/lyra/verification/lazy_lock_order_net.rb +221 -0
- data/lib/lyra/verification/multi_record_lock_net.rb +271 -0
- data/lib/lyra/verification/template_invariants.rb +71 -0
- data/lib/lyra/verification/trace_replay.rb +85 -0
- data/lib/lyra/version.rb +1 -1
- data/lib/lyra.rb +19 -0
- data/lib/tasks/lyra_projections.rake +9 -3
- metadata +14 -1
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Lyra's own tables, and the index on Rails Event Store's
|
|
4
|
+
# event_store_events_in_streams that serves Lyra's backward reads by id.
|
|
5
|
+
# Generated by `bin/rails generate lyra:install`.
|
|
6
|
+
#
|
|
7
|
+
# Without this migration Lyra creates each table the first time it needs it,
|
|
8
|
+
# and it still does wherever one is missing. Every statement here is "if not
|
|
9
|
+
# exists": where Lyra already created a table at run time, it is left as it is,
|
|
10
|
+
# and a mode-transitions table created by an earlier version gets the columns
|
|
11
|
+
# later versions added, as Lyra would add them at run time.
|
|
12
|
+
class CreateLyraTables < ActiveRecord::Migration[<%= migration_version %>]
|
|
13
|
+
# On PostgreSQL the index on event_store_events_in_streams, which may be
|
|
14
|
+
# large, is built concurrently, so writes go on while it is built; that
|
|
15
|
+
# cannot run in a transaction, so each of Lyra's tables is created in a
|
|
16
|
+
# transaction of its own. (A concurrent build that failed leaves an invalid
|
|
17
|
+
# index with the same name, which "if not exists" then keeps: drop it and
|
|
18
|
+
# run the migration again.)
|
|
19
|
+
disable_ddl_transaction!
|
|
20
|
+
|
|
21
|
+
def up
|
|
22
|
+
# The application's mode switches, the gate's checks and the touch
|
|
23
|
+
# registry (Lyra::ModeTransition, Lyra::TouchRegistry).
|
|
24
|
+
transaction do
|
|
25
|
+
create_table :lyra_mode_transitions, if_not_exists: true do |t|
|
|
26
|
+
t.string :kind, null: false
|
|
27
|
+
t.string :from_config
|
|
28
|
+
t.string :to_config, null: false
|
|
29
|
+
t.bigint :position, null: false, default: 0
|
|
30
|
+
t.integer :checked, null: false, default: 0
|
|
31
|
+
t.datetime :created_at, null: false
|
|
32
|
+
t.string :actor
|
|
33
|
+
t.string :os_user
|
|
34
|
+
t.string :host
|
|
35
|
+
t.integer :pid
|
|
36
|
+
t.boolean :forced, null: false, default: false
|
|
37
|
+
t.bigint :certificate_id
|
|
38
|
+
t.datetime :started_at
|
|
39
|
+
t.datetime :seen_at
|
|
40
|
+
t.datetime :ended_at
|
|
41
|
+
end
|
|
42
|
+
add_column :lyra_mode_transitions, :actor, :string, if_not_exists: true
|
|
43
|
+
add_column :lyra_mode_transitions, :os_user, :string, if_not_exists: true
|
|
44
|
+
add_column :lyra_mode_transitions, :host, :string, if_not_exists: true
|
|
45
|
+
add_column :lyra_mode_transitions, :pid, :integer, if_not_exists: true
|
|
46
|
+
add_column :lyra_mode_transitions, :forced, :boolean, null: false, default: false, if_not_exists: true
|
|
47
|
+
add_column :lyra_mode_transitions, :certificate_id, :bigint, if_not_exists: true
|
|
48
|
+
add_column :lyra_mode_transitions, :started_at, :datetime, if_not_exists: true
|
|
49
|
+
add_column :lyra_mode_transitions, :seen_at, :datetime, if_not_exists: true
|
|
50
|
+
add_column :lyra_mode_transitions, :ended_at, :datetime, if_not_exists: true
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# ES-Lazy's catch-up checkpoint (Lyra::Projections::LazyProjection).
|
|
54
|
+
transaction do
|
|
55
|
+
create_table :lyra_projection_checkpoints, id: false, if_not_exists: true do |t|
|
|
56
|
+
t.string :name, null: false
|
|
57
|
+
t.bigint :position, null: false, default: 0
|
|
58
|
+
t.text :gaps
|
|
59
|
+
t.datetime :updated_at
|
|
60
|
+
t.string :event_id
|
|
61
|
+
end
|
|
62
|
+
add_column :lyra_projection_checkpoints, :event_id, :string, if_not_exists: true
|
|
63
|
+
add_index :lyra_projection_checkpoints, :name, unique: true, if_not_exists: true
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# ES-Async's projected position per stream (Lyra::Projections::AsyncCheckpoint).
|
|
67
|
+
transaction do
|
|
68
|
+
create_table :lyra_async_checkpoints, id: false, if_not_exists: true do |t|
|
|
69
|
+
t.string :stream, null: false
|
|
70
|
+
t.string :event_id, null: false
|
|
71
|
+
t.datetime :updated_at
|
|
72
|
+
end
|
|
73
|
+
add_index :lyra_async_checkpoints, :stream, unique: true, if_not_exists: true
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Data-subject requests: restrictions and objections (Lyra::Rights::Store).
|
|
77
|
+
transaction do
|
|
78
|
+
create_table :lyra_rights, if_not_exists: true do |t|
|
|
79
|
+
t.string :request_id, limit: 36, null: false
|
|
80
|
+
t.string :kind, null: false
|
|
81
|
+
t.string :subject_key, null: false
|
|
82
|
+
t.string :subject_type
|
|
83
|
+
t.string :subject_id
|
|
84
|
+
t.string :policy
|
|
85
|
+
t.string :purpose
|
|
86
|
+
t.text :fields
|
|
87
|
+
t.string :ground
|
|
88
|
+
t.string :article
|
|
89
|
+
t.string :decision_grounds
|
|
90
|
+
t.string :objection_id, limit: 36
|
|
91
|
+
t.string :restriction_id, limit: 36
|
|
92
|
+
t.string :state, null: false
|
|
93
|
+
t.boolean :in_force, null: false, default: false
|
|
94
|
+
t.datetime :requested_at, precision: 6
|
|
95
|
+
t.datetime :recorded_at, precision: 6
|
|
96
|
+
t.datetime :lift_notified_at, precision: 6
|
|
97
|
+
t.datetime :lifted_at, precision: 6
|
|
98
|
+
t.datetime :decided_at, precision: 6
|
|
99
|
+
t.datetime :withdrawn_at, precision: 6
|
|
100
|
+
t.integer :stream_version, null: false
|
|
101
|
+
t.string :last_event_id, limit: 36
|
|
102
|
+
t.datetime :updated_at, precision: 6, null: false
|
|
103
|
+
end
|
|
104
|
+
add_index :lyra_rights, :request_id, unique: true, if_not_exists: true
|
|
105
|
+
add_index :lyra_rights, %i[in_force subject_key], if_not_exists: true
|
|
106
|
+
add_index :lyra_rights, :subject_key, if_not_exists: true
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# Consent per subject and purpose (Lyra::Rights::ConsentStore).
|
|
110
|
+
transaction do
|
|
111
|
+
create_table :lyra_consents, if_not_exists: true do |t|
|
|
112
|
+
t.string :subject_key, null: false
|
|
113
|
+
t.string :subject_type
|
|
114
|
+
t.string :subject_id
|
|
115
|
+
t.string :purpose, null: false
|
|
116
|
+
t.string :state, null: false
|
|
117
|
+
t.datetime :requested_at, precision: 6
|
|
118
|
+
t.datetime :granted_at, precision: 6
|
|
119
|
+
t.datetime :expires_at, precision: 6
|
|
120
|
+
t.datetime :withdrawn_at, precision: 6
|
|
121
|
+
t.integer :stream_version, null: false
|
|
122
|
+
t.datetime :updated_at, precision: 6, null: false
|
|
123
|
+
end
|
|
124
|
+
add_index :lyra_consents, %i[subject_key purpose], unique: true, if_not_exists: true
|
|
125
|
+
add_index :lyra_consents, %i[purpose subject_type], if_not_exists: true
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# Each record's head: whether it is live, and its last event
|
|
129
|
+
# (Lyra::StreamHeads). Built from the events the first time a stream is
|
|
130
|
+
# met, so an existing event store needs no backfill.
|
|
131
|
+
transaction do
|
|
132
|
+
create_table :lyra_stream_heads, id: false, if_not_exists: true do |t|
|
|
133
|
+
t.string :stream, null: false
|
|
134
|
+
t.string :status, null: false
|
|
135
|
+
t.string :event_id
|
|
136
|
+
t.datetime :updated_at, null: false
|
|
137
|
+
end
|
|
138
|
+
add_index :lyra_stream_heads, :stream, unique: true, name: "index_lyra_stream_heads_on_stream",
|
|
139
|
+
if_not_exists: true
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# Rails Event Store 3's own migration creates this index for new
|
|
143
|
+
# installations; event stores created by earlier versions lack it.
|
|
144
|
+
add_index :event_store_events_in_streams, %i[stream id],
|
|
145
|
+
name: "index_event_store_events_in_streams_on_stream_and_id",
|
|
146
|
+
algorithm: (:concurrently if connection.adapter_name.match?(/postg/i)),
|
|
147
|
+
if_not_exists: true
|
|
148
|
+
# Rails Event Store 3's own migration creates this index too (t.references
|
|
149
|
+
# :event); an event store set up otherwise can lack it. ES-NoProj's
|
|
150
|
+
# reference links join this table to itself by event_id (a record's links
|
|
151
|
+
# to the records it names), which without the index reads every link row:
|
|
152
|
+
# a destroy that checks its dependents slowed with the store (1.0.1).
|
|
153
|
+
add_index :event_store_events_in_streams, :event_id,
|
|
154
|
+
name: "index_event_store_events_in_streams_on_event_id",
|
|
155
|
+
algorithm: (:concurrently if connection.adapter_name.match?(/postg/i)),
|
|
156
|
+
if_not_exists: true
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# Nothing is undone. Lyra uses its tables with or without this migration
|
|
160
|
+
# (it would create them again on first use), they hold the mode history and
|
|
161
|
+
# the data-subject records, and the index may be Rails Event Store's own.
|
|
162
|
+
def down; end
|
|
163
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Lyra (generated by `bin/rails generate lyra:install`). The settings are in
|
|
4
|
+
# the API reference; the path from an existing application, mode by mode, is
|
|
5
|
+
# in the Migration Guide.
|
|
6
|
+
Lyra.configure do |config|
|
|
7
|
+
# Start in Monitor (non-intrusive; also the default).
|
|
8
|
+
config.mode = :monitor
|
|
9
|
+
|
|
10
|
+
# Optional: the engine uses RailsEventStore::Client.new when none is set. If
|
|
11
|
+
# you configure a JSON serializer, use Lyra::EventSerializer, not JSON: JSON
|
|
12
|
+
# drops fractional seconds from times.
|
|
13
|
+
# config.event_store = RailsEventStore::Client.new(
|
|
14
|
+
# repository: RubyEventStore::ActiveRecord::EventRepository.new(serializer: Lyra::EventSerializer)
|
|
15
|
+
# )
|
|
16
|
+
|
|
17
|
+
# Optional: monitor models without editing them (or add monitor_with_lyra to each).
|
|
18
|
+
# config.models = %w[Order Customer]
|
|
19
|
+
|
|
20
|
+
# Optional: who made each change, merged into every event's metadata.
|
|
21
|
+
# config.metadata_proc = ->(record, operation) { { user_id: Current.user&.id } }
|
|
22
|
+
end
|
|
@@ -295,6 +295,10 @@ module Lyra
|
|
|
295
295
|
end
|
|
296
296
|
end
|
|
297
297
|
|
|
298
|
+
# The tables of +model_class+'s HABTM associations, which a rebuild
|
|
299
|
+
# clears and restores from their link events.
|
|
300
|
+
def join_tables(model_class) = join_models(model_class).values.map(&:table_name).uniq
|
|
301
|
+
|
|
298
302
|
# Remove the rows of +model_class+'s join tables (before the model's
|
|
299
303
|
# table is cleared, which their foreign keys may reference).
|
|
300
304
|
def clear(model_class)
|
data/lib/lyra/bypass_events.rb
CHANGED
|
@@ -177,7 +177,8 @@ module Lyra
|
|
|
177
177
|
# the write began with: atomically) is replayed onto its row
|
|
178
178
|
# (LazyProjection.refresh_records), so the write starts from the row
|
|
179
179
|
# as the stream has it: an update committed in between is not lost
|
|
180
|
-
# under it.
|
|
180
|
+
# under it. In ES-Async the events their jobs have not applied are
|
|
181
|
+
# applied, under the streams' locks (AsyncProjectionJob.project_pending). The whole catch-up is not run again there: its lock is
|
|
181
182
|
# taken before the aggregate locks by every writer that reads first,
|
|
182
183
|
# and taking it after them deadlocked. Then the streams of the rows
|
|
183
184
|
# locked are read, in one query where it can be
|
|
@@ -198,8 +199,14 @@ module Lyra
|
|
|
198
199
|
lock_streams!(model_class, added, write: write)
|
|
199
200
|
locked.merge(added)
|
|
200
201
|
# ES-Lazy: their rows as their streams have them now, under the
|
|
201
|
-
# locks, before the rows are read and written.
|
|
202
|
+
# locks, before the rows are read and written. ES-Async: the events
|
|
203
|
+
# their jobs have not applied yet, applied (under the streams'
|
|
204
|
+
# locks, as an upsert_all's are), so an update_all's SQL
|
|
205
|
+
# expression, an update_counters or an increment! starts from the
|
|
206
|
+
# row as the stream has it: an update pending in a job is not lost
|
|
207
|
+
# under it (1.0.1).
|
|
202
208
|
Lyra::Projections::LazyProjection.refresh_records(model_class, added)
|
|
209
|
+
Lyra::Projections::AsyncProjectionJob.project_pending(model_class, added) if projects_pending?
|
|
203
210
|
end
|
|
204
211
|
|
|
205
212
|
before = nil
|
|
@@ -350,7 +357,20 @@ module Lyra
|
|
|
350
357
|
|
|
351
358
|
data = Lyra::EventData.build(model_class, id, operation, attributes: attributes, changes: changes)
|
|
352
359
|
event = event_class.new(data: data, metadata: Lyra::Privacy.stamp(model_class, data, metadata))
|
|
353
|
-
|
|
360
|
+
begin
|
|
361
|
+
Lyra.append_events(event, stream_name: Lyra.stream_name(model_class, id))
|
|
362
|
+
rescue Lyra::RecordDestroyedError
|
|
363
|
+
# A touch of a record whose stream ends in a destroy while its row
|
|
364
|
+
# stays: a record soft-deleted under 1.0.0, which recorded a soft
|
|
365
|
+
# delete as a Destroyed (from 1.0.1 it is the update it is, and its
|
|
366
|
+
# stream stays live). Its row is written as plain Active Record
|
|
367
|
+
# writes it, and nothing is recorded, as an update after a destroy
|
|
368
|
+
# is a history Hypothesis H excludes (Lyra::StreamHeads). Monitor
|
|
369
|
+
# records it, as it records what its table did.
|
|
370
|
+
raise unless source.to_s == "touch"
|
|
371
|
+
|
|
372
|
+
return nil
|
|
373
|
+
end
|
|
354
374
|
|
|
355
375
|
# With projections disabled, reads come from the cached event-stream
|
|
356
376
|
# reconstruction, which would otherwise keep serving the old state.
|
|
@@ -385,7 +405,7 @@ module Lyra
|
|
|
385
405
|
keys = relation.reselect(model_class.arel_table[pk])
|
|
386
406
|
scope = model_class.unscoped.where(pk => keys).lock
|
|
387
407
|
end
|
|
388
|
-
rows = Lyra::PurposeBoundReads.internal(INTERNAL) { scope.to_a }
|
|
408
|
+
rows = Lyra::PurposeBoundReads.internal(INTERNAL) { lock ? Lyra::Projections::LazyProjection.own_locking_read { scope.to_a } : scope.to_a }
|
|
389
409
|
rows.to_h { |record| [record_key(model_class, record), record.attributes] }.tap { |by_id| admit(model_class, by_id.keys) }
|
|
390
410
|
end
|
|
391
411
|
|
|
@@ -461,10 +481,22 @@ module Lyra
|
|
|
461
481
|
return published if ids.blank?
|
|
462
482
|
|
|
463
483
|
pk = model_class.primary_key
|
|
484
|
+
# A row the upsert updated although no key was given for it: its
|
|
485
|
+
# generated key met a live record (a sequence behind the table). The
|
|
486
|
+
# database updated that record, so its event is an update, not a second
|
|
487
|
+
# create (1.0.0 recorded a create, in every mode that records events).
|
|
488
|
+
# Its old values are the replay of its stream.
|
|
489
|
+
unknown = ids.reject { before.key?(_1) }
|
|
490
|
+
met = model_class.with_connection { |c| Lyra::StreamHeads.live_streams(unknown.map { Lyra.stream_name(model_class, _1) }, connection: c) }
|
|
464
491
|
Lyra::PurposeBoundReads.internal(INTERNAL) do
|
|
465
492
|
model_class.unscoped.where(pk => ids).find_each do |record|
|
|
466
493
|
id = record_key(model_class, record)
|
|
467
|
-
|
|
494
|
+
old_attrs = before[id]
|
|
495
|
+
if old_attrs.nil? && met.include?(Lyra.stream_name(model_class, id))
|
|
496
|
+
state = Lyra::StateProjection.rebuild_state(model_class, id).transform_keys(&:to_s)
|
|
497
|
+
old_attrs = model_class.instantiate(state.slice(*model_class.column_names)).attributes
|
|
498
|
+
end
|
|
499
|
+
if old_attrs
|
|
468
500
|
changes = diff(old_attrs, record.attributes)
|
|
469
501
|
next if changes.empty?
|
|
470
502
|
|
data/lib/lyra/command_handler.rb
CHANGED
|
@@ -29,6 +29,11 @@ module Lyra
|
|
|
29
29
|
# Fail-closed: a write whose event cannot be stored fails with the
|
|
30
30
|
# error, as in event sourcing, rather than as a refused save.
|
|
31
31
|
raise
|
|
32
|
+
rescue StreamHeads::LiveKeyError, StreamHeads::MissingRecordError, RecordDestroyedError
|
|
33
|
+
# What Hypothesis H excludes (Lyra::StreamHeads) fails as Monitor's write
|
|
34
|
+
# fails: RecordNotUnique for a create of a live key, RecordNotFound for
|
|
35
|
+
# a record that has none or was destroyed.
|
|
36
|
+
raise
|
|
32
37
|
rescue => e
|
|
33
38
|
CommandResult.failure(error: e.message)
|
|
34
39
|
end
|
data/lib/lyra/configuration.rb
CHANGED
|
@@ -45,6 +45,9 @@ module Lyra
|
|
|
45
45
|
attr_accessor :mode_transition_gate
|
|
46
46
|
# How long a clean check (rake lyra:mode:check) certifies a switch.
|
|
47
47
|
attr_accessor :mode_transition_certificate_ttl
|
|
48
|
+
# Seconds the gate waits for the writes in flight at its check, or at
|
|
49
|
+
# the switch, to end before it refuses (ModeTransition#settle; 1.0.1).
|
|
50
|
+
attr_accessor :mode_transition_settle_timeout
|
|
48
51
|
# Share of writes checked by DualView after commit (0.0, the default, is
|
|
49
52
|
# off; the thesis suggests 0.01-0.05), and what to call on a discrepancy.
|
|
50
53
|
attr_accessor :dual_view_sample_rate, :dual_view_discrepancy_handler
|
|
@@ -62,6 +65,14 @@ module Lyra
|
|
|
62
65
|
# data it carries (Lyra::Privacy.stamp). Off by default: it adds work to
|
|
63
66
|
# every write, measured separately.
|
|
64
67
|
attr_accessor :annotate_privacy
|
|
68
|
+
# Enforce the history clauses of Hypothesis H where events are appended
|
|
69
|
+
# (Lyra::StreamHeads, from 1.0.1). false: 1.0.0's behaviour, for comparison.
|
|
70
|
+
attr_accessor :stream_heads
|
|
71
|
+
# Check the effect clause of Hypothesis H (H2) at each write through
|
|
72
|
+
# callbacks in Hijack and ES-Sync: the row against its events, in the
|
|
73
|
+
# write's transaction (Lyra::EffectCheck, from 1.0.1). Off by default;
|
|
74
|
+
# LYRA_CHECK_EFFECTS=1 turns it on.
|
|
75
|
+
attr_accessor :check_effects
|
|
65
76
|
# Record every access the privacy policy validates as an event
|
|
66
77
|
# (Lyra::AccessLog). Off by default: reads can outnumber writes by orders
|
|
67
78
|
# of magnitude, so its cost is measured separately.
|
|
@@ -201,8 +212,8 @@ module Lyra
|
|
|
201
212
|
# User tracking - custom metadata proc called for every event
|
|
202
213
|
# Signature: ->(record, operation) { { user_id: ..., ... } }
|
|
203
214
|
@metadata_proc = nil
|
|
204
|
-
# Genesis: :auto (event-sourcing mode
|
|
205
|
-
# mode) or false. See Lyra::Genesis.
|
|
215
|
+
# Genesis: :auto (Hijack and event-sourcing mode), true (every
|
|
216
|
+
# event-producing mode, Monitor too) or false. See Lyra::Genesis.
|
|
206
217
|
@genesis = :auto
|
|
207
218
|
@genesis_unrepresentable = :refuse
|
|
208
219
|
# Reference links: :auto (ES-NoProj only), true (every event-producing
|
|
@@ -212,6 +223,7 @@ module Lyra
|
|
|
212
223
|
@unique_index_refresh = 60
|
|
213
224
|
@mode_transition_gate = nil
|
|
214
225
|
@mode_transition_certificate_ttl = 3600
|
|
226
|
+
@mode_transition_settle_timeout = 30
|
|
215
227
|
@dual_view_sample_rate = 0.0
|
|
216
228
|
@dual_view_discrepancy_handler = nil
|
|
217
229
|
@mode_sync = nil
|
|
@@ -236,18 +248,25 @@ module Lyra
|
|
|
236
248
|
@dashboard_purpose = nil
|
|
237
249
|
@dashboard_max_events = 10_000
|
|
238
250
|
@record_touches = true
|
|
251
|
+
@stream_heads = true
|
|
252
|
+
@check_effects = ENV["LYRA_CHECK_EFFECTS"] == "1"
|
|
239
253
|
end
|
|
240
254
|
|
|
241
|
-
# One of MODES; a string (from ENV, say) is taken as its symbol.
|
|
255
|
+
# One of MODES; a string (from ENV, say) is taken as its symbol. Leaving
|
|
256
|
+
# Hijack clears hijack_enabled, which enable_hijack!, a switch and an
|
|
257
|
+
# adoption set with it; set on its own, outside Hijack, the flag stays.
|
|
242
258
|
def mode=(mode)
|
|
243
259
|
mode = validate_choice!(:mode, mode, MODES)
|
|
244
260
|
refuse_unrecorded_touches!(mode)
|
|
261
|
+
@hijack_enabled = false if @mode == :hijack && mode != :hijack
|
|
245
262
|
@mode = mode
|
|
263
|
+
mode_set!
|
|
246
264
|
end
|
|
247
265
|
|
|
248
266
|
# One of PROJECTION_MODES; a string is taken as its symbol.
|
|
249
267
|
def projection_mode=(mode)
|
|
250
268
|
@projection_mode = validate_choice!(:projection_mode, mode, PROJECTION_MODES)
|
|
269
|
+
mode_set!
|
|
251
270
|
end
|
|
252
271
|
|
|
253
272
|
# One of GENESIS_UNREPRESENTABLE; a string is taken as its symbol.
|
|
@@ -320,6 +339,7 @@ module Lyra
|
|
|
320
339
|
def hijack_enabled=(value)
|
|
321
340
|
refuse_unrecorded_touches!(:hijack) if value
|
|
322
341
|
@hijack_enabled = value
|
|
342
|
+
mode_set!
|
|
323
343
|
end
|
|
324
344
|
|
|
325
345
|
# Why +mode+ (a mode, or a ModeTransition label such as
|
|
@@ -491,6 +511,7 @@ module Lyra
|
|
|
491
511
|
refuse_unrecorded_touches!(:hijack)
|
|
492
512
|
@hijack_enabled = true
|
|
493
513
|
@mode = :hijack
|
|
514
|
+
mode_set!
|
|
494
515
|
end
|
|
495
516
|
|
|
496
517
|
# Enable monitor mode (only log events, don't override)
|
|
@@ -499,6 +520,7 @@ module Lyra
|
|
|
499
520
|
|
|
500
521
|
@hijack_enabled = false
|
|
501
522
|
@mode = :monitor
|
|
523
|
+
mode_set!
|
|
502
524
|
end
|
|
503
525
|
|
|
504
526
|
# Enable event sourcing mode (events as source of truth, no direct DB writes)
|
|
@@ -508,16 +530,23 @@ module Lyra
|
|
|
508
530
|
refuse_unrecorded_touches!(:event_sourcing)
|
|
509
531
|
@mode = :event_sourcing
|
|
510
532
|
@hijack_enabled = false
|
|
533
|
+
mode_set!
|
|
511
534
|
end
|
|
512
535
|
|
|
513
536
|
# Disable Lyra completely
|
|
514
537
|
def disable!
|
|
515
538
|
@mode = :disabled
|
|
516
539
|
@hijack_enabled = false
|
|
540
|
+
mode_set!
|
|
517
541
|
end
|
|
518
542
|
|
|
519
543
|
private
|
|
520
544
|
|
|
545
|
+
# After boot, a set of this process's own (Lyra::ModeSync.mode_set!).
|
|
546
|
+
def mode_set!
|
|
547
|
+
Lyra::ModeSync.mode_set! if defined?(Lyra::ModeSync) && Lyra.config.equal?(self)
|
|
548
|
+
end
|
|
549
|
+
|
|
521
550
|
def refuse_unrecorded_touches!(mode)
|
|
522
551
|
refusal = touch_refusal(mode)
|
|
523
552
|
raise ArgumentError, refusal if refusal
|
data/lib/lyra/dual_view.rb
CHANGED
|
@@ -29,9 +29,12 @@ module Lyra
|
|
|
29
29
|
# reads_without_purpose :deny it needs a purpose that allows the declared
|
|
30
30
|
# fields, as any read does. Lyra's own comparisons (the mode gate, the
|
|
31
31
|
# sampler, repair) read as Lyra. The row is looked up by the model's
|
|
32
|
-
# primary key, whatever its column is called
|
|
32
|
+
# primary key, whatever its column is called, and without the model's
|
|
33
|
+
# default scope (from 1.0.1): the comparison is of the two stores, and a
|
|
34
|
+
# soft-deleted row (default_scope { where(deleted_at: nil) }) is still in
|
|
35
|
+
# the table, as its events say.
|
|
33
36
|
def crud_state
|
|
34
|
-
record = model_class.find_by(Lyra.key_column(model_class) => model_id)
|
|
37
|
+
record = model_class.unscoped { model_class.find_by(Lyra.key_column(model_class) => model_id) }
|
|
35
38
|
|
|
36
39
|
return { exists: false } unless record
|
|
37
40
|
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lyra
|
|
4
|
+
# A write whose row, as the database left it, differs from what its record's
|
|
5
|
+
# events replay to (Lyra::EffectCheck). Raised in the write's transaction,
|
|
6
|
+
# which rolls back with its event.
|
|
7
|
+
class EffectMismatchError < ActiveRecord::ActiveRecordError
|
|
8
|
+
attr_reader :discrepancy
|
|
9
|
+
|
|
10
|
+
def initialize(discrepancy)
|
|
11
|
+
@discrepancy = discrepancy
|
|
12
|
+
super("Lyra: the write's effect differs from its events: #{discrepancy}")
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# The effect clause of Hypothesis H (H2), checked at the write, opt-in
|
|
17
|
+
# (config.check_effects, from 1.0.1; off by default). In Hijack and ES-Sync
|
|
18
|
+
# the row is written in the same transaction as the event: after a create,
|
|
19
|
+
# update or destroy through callbacks, the record's row is compared with the
|
|
20
|
+
# replay of its stream, as the mode gate and DualView compare them
|
|
21
|
+
# (ModeTransition.discrepancy), and a difference (a trigger, a default the
|
|
22
|
+
# database computes, a type that does not round-trip) raises
|
|
23
|
+
# EffectMismatchError before the transaction commits. Monitor's event is
|
|
24
|
+
# taken from what its table did, and the other modes write the row later or
|
|
25
|
+
# never, so the check does not apply there. Writes that skip callbacks
|
|
26
|
+
# (update_columns, update_all, insert_all) are not checked. The cost is a
|
|
27
|
+
# read of the row and of the record's stream, and a replay, per write.
|
|
28
|
+
module EffectCheck
|
|
29
|
+
module_function
|
|
30
|
+
|
|
31
|
+
def applies?
|
|
32
|
+
return false unless Lyra.config.check_effects
|
|
33
|
+
|
|
34
|
+
Lyra.hijack_mode? || (Lyra.event_sourcing_mode? && Lyra.config.projection_mode == :sync)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Not checked: a model on another database than the event store's (the
|
|
38
|
+
# comparison reads the log through ActiveRecord::Base). A soft delete
|
|
39
|
+
# needs no exception: it is recorded as the update it is. A record of
|
|
40
|
+
# an STI subclass is compared as its stream names it, by its base class,
|
|
41
|
+
# so a change of its type is not taken for a missing row.
|
|
42
|
+
def verify!(record)
|
|
43
|
+
model = record.class
|
|
44
|
+
return unless model.connection_db_config == ActiveRecord::Base.connection_db_config
|
|
45
|
+
|
|
46
|
+
owner = Lyra.stream_owner_name(model).safe_constantize
|
|
47
|
+
model = owner if owner.is_a?(Class) && model <= owner
|
|
48
|
+
problem = Lyra::ModeTransition.discrepancy(model, record.id)
|
|
49
|
+
raise EffectMismatchError, problem if problem
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
data/lib/lyra/engine.rb
CHANGED
|
@@ -193,6 +193,19 @@ module Lyra
|
|
|
193
193
|
end
|
|
194
194
|
end
|
|
195
195
|
|
|
196
|
+
# Lyra::ModeSync: where the application's record stood when the process
|
|
197
|
+
# booted, to tell a mode it sets later from the one it was configured
|
|
198
|
+
# with (a code reload's to_prepare blocks apply that configuration again,
|
|
199
|
+
# and are no set: ModeSync::Reloads). It must be read after every
|
|
200
|
+
# after_initialize block, the application's included, so it is the
|
|
201
|
+
# application's last initializer, appended to the Finisher. An engine's
|
|
202
|
+
# initializer cannot go there: with after: :finisher_hook, Rails would
|
|
203
|
+
# run the Finisher ahead of the application's own initializers.
|
|
204
|
+
::Rails::Application::Finisher.initializer "lyra.mode_sync_boot" do |app|
|
|
205
|
+
app.reloader.singleton_class.prepend(Lyra::ModeSync::Reloads)
|
|
206
|
+
Lyra::ModeSync.remember_boot!
|
|
207
|
+
end
|
|
208
|
+
|
|
196
209
|
# Rails loads every lib/tasks/*.rake under an engine's root into the
|
|
197
210
|
# host application. In a development checkout that root also holds its own
|
|
198
211
|
# maintenance tasks (gems:, public:, stats:, ...), and the Lyra task
|
data/lib/lyra/erasure.rb
CHANGED
|
@@ -625,7 +625,7 @@ module Lyra
|
|
|
625
625
|
model.with_connection do |connection|
|
|
626
626
|
Lyra::AdvisoryLock.xact_lock(connection, "lyra_aggregate/#{stream}", purpose: "erasure")
|
|
627
627
|
end
|
|
628
|
-
model.unscoped.lock.find_by(model.primary_key => id) unless events_only?
|
|
628
|
+
Lyra::Projections::LazyProjection.own_locking_read { model.unscoped.lock.find_by(model.primary_key => id) } unless events_only?
|
|
629
629
|
end
|
|
630
630
|
|
|
631
631
|
# The record's row, every column of it scrubbed with the rule its events
|
|
@@ -638,7 +638,7 @@ module Lyra
|
|
|
638
638
|
def erase_row(model, id, replacements, resolve)
|
|
639
639
|
return [false, []] if events_only?
|
|
640
640
|
|
|
641
|
-
row = model.unscoped.lock.find_by(model.primary_key => id)
|
|
641
|
+
row = Lyra::Projections::LazyProjection.own_locking_read { model.unscoped.lock.find_by(model.primary_key => id) }
|
|
642
642
|
return [false, []] unless row
|
|
643
643
|
|
|
644
644
|
columns = scrub_row(model, row, resolve)
|
|
@@ -938,7 +938,7 @@ module Lyra
|
|
|
938
938
|
# connection the lock did not hold back), that stream is read again and
|
|
939
939
|
# rewritten instead, and no import is claimed.
|
|
940
940
|
def erase_and_import(model, id, stream, resolve, reason, erased_by)
|
|
941
|
-
row = model.unscoped.lock.find_by(model.primary_key => id)
|
|
941
|
+
row = Lyra::Projections::LazyProjection.own_locking_read { model.unscoped.lock.find_by(model.primary_key => id) }
|
|
942
942
|
return [false, false] unless row
|
|
943
943
|
|
|
944
944
|
columns = scrub_row(model, row, resolve)
|
|
@@ -1144,7 +1144,7 @@ module Lyra
|
|
|
1144
1144
|
def erase_copied_columns(model, id, resolve)
|
|
1145
1145
|
return [] if events_only?
|
|
1146
1146
|
|
|
1147
|
-
row = model.unscoped.lock.find_by(model.primary_key => id)
|
|
1147
|
+
row = Lyra::Projections::LazyProjection.own_locking_read { model.unscoped.lock.find_by(model.primary_key => id) }
|
|
1148
1148
|
return [] unless row
|
|
1149
1149
|
|
|
1150
1150
|
columns = scrub_row(model, row, resolve)
|
|
@@ -22,6 +22,64 @@ module Lyra
|
|
|
22
22
|
# EventStoreUnavailableError if the store fails. Every event Lyra writes
|
|
23
23
|
# goes through here.
|
|
24
24
|
def self.append_events(events, stream_name:, store: config.event_store)
|
|
25
|
+
# Each replayed CRUD event first moves its record stream's head, in this
|
|
26
|
+
# transaction: what Hypothesis H excludes is refused here, before anything
|
|
27
|
+
# is stored (Lyra::StreamHeads). Outside the rescue below, so a refusal
|
|
28
|
+
# stays the error the application expects (RecordNotUnique, RecordNotFound).
|
|
29
|
+
if StreamHeads.enabled?
|
|
30
|
+
Array(events).each do |event|
|
|
31
|
+
operation = record_operation(event)
|
|
32
|
+
next unless operation
|
|
33
|
+
|
|
34
|
+
# On the record's own database, where Lyra keeps its event store (the
|
|
35
|
+
# same transaction as the append), not necessarily ActiveRecord::Base's.
|
|
36
|
+
model = record_model(event)
|
|
37
|
+
if model
|
|
38
|
+
model.with_connection { StreamHeads.advance!(stream_name, operation, event.event_id, connection: _1) }
|
|
39
|
+
else
|
|
40
|
+
StreamHeads.advance!(stream_name, operation, event.event_id)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
stored = store_events(events, stream_name: stream_name, store: store)
|
|
45
|
+
note_own_writes(events) if Projections::LazyProjection.active?
|
|
46
|
+
# ES-NoProj: a relation built before this write is read again (CachedRelation, Freshness).
|
|
47
|
+
Projections::CachedRelation.written! if Projections::CachedRelation.tracks_writes? && Array(events).any? { record_operation(_1) }
|
|
48
|
+
stored
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# ES-Lazy, design c: the records this transaction appended events of, which
|
|
52
|
+
# a catch-up on a connection of its own cannot see before the commit, so the
|
|
53
|
+
# transaction's own reads replay them (LazyProjection.own_write!).
|
|
54
|
+
def self.note_own_writes(events)
|
|
55
|
+
Array(events).each do |event|
|
|
56
|
+
next unless record_operation(event)
|
|
57
|
+
|
|
58
|
+
model = record_model(event) or next
|
|
59
|
+
data = event.data
|
|
60
|
+
Projections::LazyProjection.own_write!(model, data[:model_id] || data["model_id"], event.event_id)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
private_class_method :note_own_writes
|
|
64
|
+
|
|
65
|
+
# The replayed CRUD operation of a record's event (Lyra::EventData), or nil
|
|
66
|
+
# for any other event: domain events added with also:, links, erasures,
|
|
67
|
+
# access-log and rights events.
|
|
68
|
+
def self.record_operation(event)
|
|
69
|
+
data = event.data.is_a?(Hash) ? event.data : {}
|
|
70
|
+
return nil if (data[:model_class] || data["model_class"]).nil? || (data[:model_id] || data["model_id"]).nil?
|
|
71
|
+
|
|
72
|
+
Lyra::Event.operation_of(event)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# The Active Record class an event of a record names, or nil.
|
|
76
|
+
def self.record_model(event)
|
|
77
|
+
data = event.data.is_a?(Hash) ? event.data : {}
|
|
78
|
+
klass = (data[:model_class] || data["model_class"]).to_s.safe_constantize
|
|
79
|
+
klass if klass.is_a?(Class) && klass < ActiveRecord::Base
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def self.store_events(events, stream_name:, store:)
|
|
25
83
|
# Encrypted attributes go to the log as ciphertext, as they go to the row.
|
|
26
84
|
Array(events).each { |event| EncryptedAttributes.seal!(event) }
|
|
27
85
|
store.publish(events, stream_name: stream_name)
|
|
@@ -31,6 +89,7 @@ module Lyra
|
|
|
31
89
|
rescue => e
|
|
32
90
|
raise EventStoreUnavailableError, "could not store events in #{stream_name}: #{e.class}: #{e.message}"
|
|
33
91
|
end
|
|
92
|
+
private_class_method :store_events
|
|
34
93
|
|
|
35
94
|
# Create one of Lyra's own tables (+name+, defined by the block, given a
|
|
36
95
|
# connection) so that it outlives the caller's transaction. Created inside
|
data/lib/lyra/genesis.rb
CHANGED
|
@@ -35,9 +35,10 @@ module Lyra
|
|
|
35
35
|
# free), the import runs in the caller's transaction and the model counts as
|
|
36
36
|
# imported only once it commits (Lyra::OwnConnection).
|
|
37
37
|
#
|
|
38
|
-
# Enabled with config.genesis: :auto (the default) runs it in
|
|
39
|
-
# mode,
|
|
40
|
-
#
|
|
38
|
+
# Enabled with config.genesis: :auto (the default) runs it in Hijack and
|
|
39
|
+
# event-sourcing mode, whose stream heads refuse an update of a record with no
|
|
40
|
+
# history (Hijack from 1.0.1); true also runs it in Monitor (for DualView and
|
|
41
|
+
# Rebuild); false never runs it.
|
|
41
42
|
#
|
|
42
43
|
# Values the model cannot represent (from 1.0.0). The Imported event holds
|
|
43
44
|
# the row as the model reads it. A stored value the model cannot read --
|
|
@@ -165,11 +166,17 @@ module Lyra
|
|
|
165
166
|
@mutex = Mutex.new
|
|
166
167
|
|
|
167
168
|
class << self
|
|
169
|
+
# :auto (the default): in Hijack and the event-sourcing modes, whose
|
|
170
|
+
# stream heads refuse an update or destroy of a record with no history
|
|
171
|
+
# (from 1.0.1, Hijack included): the rows that predate Lyra are imported
|
|
172
|
+
# before a model's first use there, whether or not the mode gate ran.
|
|
173
|
+
# Monitor records a row it finds without history as it writes it, and
|
|
174
|
+
# imports only with config.genesis true.
|
|
168
175
|
def enabled?
|
|
169
176
|
case Lyra.config.genesis
|
|
170
177
|
when true then !Lyra.disabled_mode?
|
|
171
178
|
when false, nil then false
|
|
172
|
-
else Lyra.event_sourcing_mode?
|
|
179
|
+
else Lyra.event_sourcing_mode? || Lyra.hijack_mode?
|
|
173
180
|
end
|
|
174
181
|
end
|
|
175
182
|
|
|
@@ -179,6 +186,11 @@ module Lyra
|
|
|
179
186
|
# holds values the model cannot represent (admit!).
|
|
180
187
|
def first_use(model_class, id = nil)
|
|
181
188
|
return 0 unless enabled?
|
|
189
|
+
# Hijack records a model with a composite primary key, which Genesis
|
|
190
|
+
# cannot import (a stream is named by one key): its first use there goes
|
|
191
|
+
# on without an import, as before Hijack imported (1.0.1). The
|
|
192
|
+
# event-sourcing modes still refuse such a model at its first use.
|
|
193
|
+
return 0 if model_class.primary_key.is_a?(Array) && !Lyra.event_sourcing_mode?
|
|
182
194
|
return 0 if Thread.current[:lyra_genesis_running]
|
|
183
195
|
|
|
184
196
|
count = 0
|