Class: Sourced::Backends::SequelBackend

Inherits:
Object
  • Object
show all
Defined in:
lib/sourced/backends/sequel_backend.rb

Constant Summary collapse

Stats =
Data.define(:stream_count, :max_global_seq, :groups)

Instance Method Summary collapse

Constructor Details

#initialize(db, logger: Sourced.config.logger, prefix: 'sourced') ⇒ SequelBackend

Returns a new instance of SequelBackend.



13
14
15
16
17
18
19
20
21
22
# File 'lib/sourced/backends/sequel_backend.rb', line 13

def initialize(db, logger: Sourced.config.logger, prefix: 'sourced')
  @db = connect(db)
  @logger = logger
  @prefix = prefix
  @commands_table = table_name(:commands)
  @streams_table = table_name(:streams)
  @offsets_table = table_name(:offsets)
  @events_table = table_name(:events)
  logger.info("Connected to #{@db}")
end

Instance Method Details

#ack_on(group_id, event_id) ⇒ Object



119
120
121
122
123
124
125
126
127
128
# File 'lib/sourced/backends/sequel_backend.rb', line 119

def ack_on(group_id, event_id, &)
  db.transaction do
    row = db.fetch(sql_for_ack_on, group_id, event_id).first
    raise Sourced::ConcurrentAckError, "Stream for event #{event_id} is being concurrently processed by #{group_id}" unless row

    yield if block_given?

    ack_event(group_id, row[:stream_id_fk], row[:global_seq])
  end
end

#append_to_stream(stream_id, events) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/sourced/backends/sequel_backend.rb', line 75

def append_to_stream(stream_id, events)
  return if events.empty?

  if events.map(&:stream_id).uniq.size > 1
    raise ArgumentError, 'Events must all belong to the same stream'
  end

  db.transaction do
    seq = events.last.seq
    id = db[streams_table].insert_conflict(target: :stream_id, update: { seq:, updated_at: Time.now }).insert(stream_id:, seq:)
    rows = events.map { |e| serialize_event(e, id) }
    db[events_table].multi_insert(rows)
  end
  true
rescue Sequel::UniqueConstraintViolation => e
  raise Sourced::ConcurrentAppendError, e.message
end

#clear!Object

For tests only



183
184
185
186
187
188
189
190
191
# File 'lib/sourced/backends/sequel_backend.rb', line 183

def clear!
  raise 'Not in test environment' unless ENV['ENVIRONMENT'] == 'test'
  # Truncate and restart global_seq increment first
  db[events_table].truncate(cascade: true, only: true, restart: true)
  db[events_table].delete
  db[commands_table].delete
  db[offsets_table].delete
  db[streams_table].delete
end

#installObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/sourced/backends/sequel_backend.rb', line 193

def install
  if @db.class.name == 'Sequel::SQLite::Database'
    raise 'no SQLite support yet'
  end

  _streams_table = streams_table

  db.create_table?(streams_table) do
    primary_key :id
    String :stream_id, null: false, unique: true
    Time :updated_at, null: false, default: Sequel.function(:now)
    Bignum :seq, null: false
  end

  logger.info("Created table #{streams_table}")

  db.create_table?(offsets_table) do
    primary_key :id
    foreign_key :stream_id, _streams_table
    String :group_id, null: false, index: true
    Bignum :global_seq, null: false
    Time :created_at, null: false, default: Sequel.function(:now)
    index %i[group_id stream_id], unique: true
  end

  logger.info("Created table #{offsets_table}")

  db.create_table?(events_table) do
    primary_key :global_seq, type: :Bignum
    column :id, :uuid, unique: true
    foreign_key :stream_id, _streams_table
    Bignum :seq, null: false
    String :type, null: false
    Time :created_at, null: false
    column :causation_id, :uuid, index: true
    column :correlation_id, :uuid
    column :metadata, :jsonb
    column :payload, :jsonb
    index %i[stream_id seq], unique: true
  end

  logger.info("Created table #{events_table}")

  db.create_table?(commands_table) do
    column :id, :uuid, unique: true
    String :stream_id, null: false, index: true
    String :type, null: false
    Time :created_at, null: false, index: true
    column :causation_id, :uuid
    column :correlation_id, :uuid
    column :metadata, :jsonb
    column :payload, :jsonb
  end

  logger.info("Created table #{commands_table}")

  self
end

#installed?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/sourced/backends/sequel_backend.rb', line 24

def installed?
  db.table_exists?(events_table) && db.table_exists?(streams_table) && db.table_exists?(offsets_table) && db.table_exists?(commands_table)
end

#next_command(&reserve) ⇒ Object

 TODO: if the application raises an exception the command row is not deleted, so that it can be retried. However, if a command fails permanently there’s no point in keeping it in the queue, this ties with unresolved error handling in event handling, too.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sourced/backends/sequel_backend.rb', line 43

def next_command(&reserve)
  if block_given?
    db.transaction do
      row = db.fetch(sql_for_next_command, Time.now.utc).first
      return unless row

      cmd = deserialize_event(row)
      yield cmd
      db[commands_table].where(id: cmd.id).delete
      cmd
      # TODO: on failure, do we want to mark command as failed
      # and put it in a dead-letter queue?
    end
  else
    row = db[commands_table].order(:created_at).first
    row ? deserialize_event(row) : nil
  end
end

#read_correlation_batch(event_id) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/sourced/backends/sequel_backend.rb', line 156

def read_correlation_batch(event_id)
  correlation_subquery = db[events_table]
    .select(:correlation_id)
    .where(id: event_id)

  query = base_events_query
    .where(Sequel[events_table][:correlation_id] => correlation_subquery)
    .order(Sequel[events_table][:global_seq])

  query.map do |row|
    deserialize_event(row)
  end
end

#read_event_stream(stream_id, after: nil, upto: nil) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/sourced/backends/sequel_backend.rb', line 170

def read_event_stream(stream_id, after: nil, upto: nil)
  _events_table = events_table # need local variable for Sequel block

  query = base_events_query.where(Sequel[streams_table][:stream_id] => stream_id)

  query = query.where { Sequel[_events_table][:seq] > after } if after
  query = query.where { Sequel[_events_table][:seq] <= upto } if upto
  query.order(Sequel[_events_table][:global_seq]).map do |row|
    deserialize_event(row)
  end
end

#reserve_next_for_reactor(reactor) ⇒ Object

Parameters:



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/sourced/backends/sequel_backend.rb', line 94

def reserve_next_for_reactor(reactor, &)
  group_id = reactor.consumer_info.group_id
  handled_events = reactor.handled_events.map(&:type)

  db.transaction do
    start_from = reactor.consumer_info.start_from.call
    row = if start_from.is_a?(Time)
      db.fetch(sql_for_reserve_next_with_events(handled_events, true), group_id, group_id, start_from).first
    else
      db.fetch(sql_for_reserve_next_with_events(handled_events), group_id, group_id).first
    end
    return unless row

    event = deserialize_event(row)

    if block_given?
      yield(event)
      # ACK
      ack_event(group_id, row[:stream_id_fk], row[:global_seq])
    end

    event
  end
end

#schedule_commands(commands) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/sourced/backends/sequel_backend.rb', line 28

def schedule_commands(commands)
  return false if commands.empty?

  rows = commands.map { |c| serialize_command(c) }

  db.transaction do
    db[commands_table].multi_insert(rows)
  end
  true
end

#statsObject



64
65
66
67
68
69
# File 'lib/sourced/backends/sequel_backend.rb', line 64

def stats
  stream_count = db[streams_table].count
  max_global_seq = db[events_table].max(:global_seq)
  groups = db.fetch(sql_for_consumer_stats).all
  Stats.new(stream_count, max_global_seq, groups)
end

#transactionObject



71
72
73
# File 'lib/sourced/backends/sequel_backend.rb', line 71

def transaction(&)
  db.transaction(&)
end