dionysus-rb 1.1.0 → 1.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d190bbc3fd4b2ca0aa7c7d1be7c0ddaa6a3b6bdcbd6556b90e4752c2e875bfc6
4
- data.tar.gz: 33379f5ccc97bceee7caad1d9b3383e84c3073e48b15bda1d0f592e765c39352
3
+ metadata.gz: 715bda096eaf87ea615134206607a11d22ff3d673b5cbe9625c584599fb5696b
4
+ data.tar.gz: d4c5741f509d02f0c5a28a4332f34b08b28f46ca1c7d026d59f59bff2e37638f
5
5
  SHA512:
6
- metadata.gz: cafb4cb76ad00f5dbbc45f9c3a578284ef554aaf680310286961d1f8251620bc67c10e190f301ac06fca1c8e55be9fab2e75977cc85ecf80f21503504558da20
7
- data.tar.gz: 78e8c07d652f3dcc46cb3a527834cb72b2290b119266e4362cde3ddbf1d5679c147cb03cce34acb6682451c6453fb58532faab8a7bf5be43a6db6be791e7e1e2
6
+ metadata.gz: c101ce0cfa0f3ddb58339bdf28b3b479838fb3b66107d8223d4a795888e5048cc8f044db232fe7ae03899cf558d1b8100a8119119d8a8d3faac3996082671618
7
+ data.tar.gz: be3c63dddbc4b1c3ae58134b050370c62a0940d3227ca3e1820cce72a1bba36cb932de2906d42a7fe723f811ac30dbc4431f668c5832787fd0a27fb521fc5594
data/.rubocop.yml CHANGED
@@ -170,6 +170,7 @@ Naming/AccessorMethodName:
170
170
  RSpec/DescribeClass:
171
171
  Exclude:
172
172
  - 'spec/integration_spec.rb'
173
+ - 'spec/dedup_serialization_time_integration_spec.rb'
173
174
 
174
175
  Style/MultilineBlockChain:
175
176
  Exclude:
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [1.2.0]
4
+
5
+ ### Fixed
6
+
7
+ - Rank duplicate messages in a consumer batch by when their payload was serialized, not by Kafka offset. The previous release ranked on the highest offset, on the premise that offsets strictly increase in publish order. They do - but publish order is not serialization order: `Publisher#publish` does `find_by` -> `generate_message` -> `responder.call`, so a message produced later can carry an earlier snapshot. Measured in production on one record, eight `updated` events inside 700 ms: the offset the batch ended on was published at `.905` carrying a payload read at `.398194` with `paid_amount 0.0`, while an earlier offset published at `.837` carried a payload read at `.671830` with the real value of `1447.0`. Ranking on offset kept the stale payload and the consumer's value stuck at `0.00` with nothing to self-heal it.
8
+ - Neither field already in the message is a freshness signal. `updated_at` is the record's own timestamp and does not move when only an associated record changes, so two payloads with different computed values can share one `updated_at` - that is the tie the previous release was written for. The offset is publish order, per above. The two known incidents are mirror images: in one the correct message has the higher offset and the older `updated_at`, in the other the lower offset and the newer `updated_at`, so no ordering over the existing fields fixes both.
9
+ - The producer therefore stamps `serialized_at` where the payload is actually built, and the consumer ranks on `[serialized_at, offset]`. Messages without the field fall back to `Time.at(0)` and are ordered among themselves by offset, which is exactly the previous behaviour; a stamped message wins over an unstamped one, which is correct because a stamp can only come from a newer producer.
10
+ - The stamp is gated on `config.include_serialized_at_in_payload`, default `false`, so published envelopes are unchanged until it is switched on. Roll consumers out first - with the fallback that deploy is a no-op - then enable it on the producer.
11
+
3
12
  ## [1.1.0]
4
13
 
5
14
  ### Fixed
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dionysus-rb (1.1.0)
4
+ dionysus-rb (1.2.0)
5
5
  activerecord (>= 5)
6
6
  activesupport (>= 3.2)
7
7
  concurrent-ruby
@@ -1,6 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "time"
4
+
3
5
  class Dionysus::Consumer::ParamsBatchTransformations::RemoveDuplicatesStrategy
6
+ SERIALIZED_AT_FORMAT = %r{\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z\z}
7
+
4
8
  def call(params_batch)
5
9
  return params_batch if duplicates_removal_not_applicable?(params_batch)
6
10
 
@@ -12,17 +16,37 @@ class Dionysus::Consumer::ParamsBatchTransformations::RemoveDuplicatesStrategy
12
16
  # the idea is following:
13
17
  # 1. group messages by event and id - for a given model we can expect unique messages for _created and _deleted events
14
18
  # but we can have multiple _updated events, so this is where we are interested in removing duplicates
15
- # 2. from each group keep the message published last (highest offset) - payloads are serialized at
16
- # publish time, so that is the freshest state; "updated_at" does not track it
19
+ # 2. from each group keep the message whose payload was serialized last, with the offset as a tie-break -
20
+ # the offset alone is publish order, and a message published later can carry an earlier snapshot
17
21
  # 3. flatten the array of arrays as all groups will have a single item
18
22
  def transform_messages_array(params_batch)
19
23
  params_batch
20
24
  .to_a
21
25
  .group_by { |batch| grouping_key_by_event_and_id(batch) }
22
- .map { |_, group| group.max_by(&:offset) }
26
+ .map { |_, group| freshest(group) }
23
27
  .flatten
24
28
  end
25
29
 
30
+ # a group is ranked by its stamps only when every message in it carries one - during the
31
+ # producer rollout a batch can hold both, and a stamp says nothing about an unstamped neighbour
32
+ def freshest(group)
33
+ stamps = group.map { |message| serialized_at_for(message) }
34
+
35
+ return group.max_by(&:offset) if stamps.any?(&:nil?)
36
+
37
+ group.zip(stamps).max_by { |message, serialized_at| [serialized_at, message.offset] }.first
38
+ end
39
+
40
+ def serialized_at_for(message)
41
+ raw = message.payload.fetch("message").first["serialized_at"]
42
+
43
+ return unless raw.is_a?(String) && raw.match?(SERIALIZED_AT_FORMAT)
44
+
45
+ Time.parse(raw)
46
+ rescue
47
+ nil
48
+ end
49
+
26
50
  def grouping_key_by_event_and_id(batch)
27
51
  [
28
52
  batch.payload.fetch("message").first.fetch("event"),
@@ -8,7 +8,8 @@ class Dionysus::Producer::Config
8
8
  :lock_client, :lock_expiry_time, :error_handler, :outbox_publishing_batch_size,
9
9
  :transactional_outbox_enabled, :sidekiq_queue, :publisher_service_name,
10
10
  :genesis_consistency_safety_delay, :hermes_event_producer, :publish_after_commit, :outbox_worker_publishing_delay,
11
- :high_priority_sidekiq_queue, :observers_inline_maximum_size, :remove_consecutive_duplicates_before_publishing
11
+ :high_priority_sidekiq_queue, :observers_inline_maximum_size, :remove_consecutive_duplicates_before_publishing,
12
+ :include_serialized_at_in_payload
12
13
 
13
14
  def self.default_sidekiq_queue
14
15
  :dionysus
@@ -96,6 +97,14 @@ class Dionysus::Producer::Config
96
97
  @observers_inline_maximum_size || 1000
97
98
  end
98
99
 
100
+ # Off by default so consumers, which fall back to the offset when the field is absent, can be
101
+ # rolled out first.
102
+ def include_serialized_at_in_payload
103
+ return @include_serialized_at_in_payload if defined?(@include_serialized_at_in_payload)
104
+
105
+ false
106
+ end
107
+
99
108
  def remove_consecutive_duplicates_before_publishing
100
109
  return @remove_consecutive_duplicates_before_publishing if defined?(@remove_consecutive_duplicates_before_publishing)
101
110
 
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "active_support/core_ext/string"
4
+ require "time"
4
5
 
5
6
  class Dionysus::Producer::KarafkaResponderGenerator
6
7
  TOMBSTONE = nil
@@ -45,13 +46,18 @@ class Dionysus::Producer::KarafkaResponderGenerator
45
46
 
46
47
  record = records.sample
47
48
 
49
+ # the offset alone is publish order, and a message published later can carry an
50
+ # earlier snapshot, so consumers need to know when this payload was actually read
51
+ serialized_at = config.include_serialized_at_in_payload ? Time.now.utc : nil
48
52
  payload = serialize_to_payload(records, topic, batch_options)
49
53
 
50
- {
54
+ event_payload = {
51
55
  event: event,
52
56
  model_name: record.model_name.name,
53
57
  data: payload
54
58
  }
59
+ event_payload[:serialized_at] = serialized_at.iso8601(6) if serialized_at
60
+ event_payload
55
61
  end
56
62
  unless genesis_only?(options)
57
63
  respond_to topic_name, { message: message }, **final_options
@@ -3,5 +3,5 @@
3
3
  module Dionysus
4
4
  module Version
5
5
  end
6
- VERSION = "1.1.0"
6
+ VERSION = "1.2.0"
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dionysus-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karol Galanciak
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-08-18 00:00:00.000000000 Z
11
+ date: 2026-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord