karafka 2.0.41 → 2.1.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.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/.github/workflows/ci.yml +2 -2
  4. data/CHANGELOG.md +20 -1
  5. data/Gemfile.lock +2 -1
  6. data/config/locales/errors.yml +10 -0
  7. data/config/locales/pro_errors.yml +0 -2
  8. data/lib/karafka/active_job/consumer.rb +16 -11
  9. data/lib/karafka/active_job/current_attributes/loading.rb +36 -0
  10. data/lib/karafka/active_job/current_attributes/persistence.rb +28 -0
  11. data/lib/karafka/active_job/current_attributes.rb +42 -0
  12. data/lib/karafka/active_job/dispatcher.rb +8 -2
  13. data/lib/karafka/connection/client.rb +1 -1
  14. data/lib/karafka/errors.rb +3 -0
  15. data/lib/karafka/instrumentation/vendors/kubernetes/liveness_listener.rb +166 -0
  16. data/lib/karafka/pro/active_job/consumer.rb +1 -10
  17. data/lib/karafka/pro/active_job/dispatcher.rb +2 -2
  18. data/lib/karafka/pro/processing/coordinator.rb +20 -1
  19. data/lib/karafka/pro/processing/filters/virtual_limiter.rb +52 -0
  20. data/lib/karafka/pro/processing/filters_applier.rb +4 -0
  21. data/lib/karafka/pro/processing/strategies/aj/dlq_ftr_mom_vp.rb +1 -1
  22. data/lib/karafka/pro/processing/strategies/aj/dlq_lrj_mom.rb +3 -1
  23. data/lib/karafka/pro/processing/strategies/aj/dlq_mom_vp.rb +2 -2
  24. data/lib/karafka/pro/processing/strategies/aj/lrj_mom_vp.rb +2 -0
  25. data/lib/karafka/pro/processing/strategies/aj/mom_vp.rb +1 -1
  26. data/lib/karafka/pro/processing/strategies/dlq/ftr.rb +1 -1
  27. data/lib/karafka/pro/processing/strategies/dlq/ftr_lrj_mom.rb +3 -6
  28. data/lib/karafka/pro/processing/strategies/dlq/ftr_lrj_mom_vp.rb +43 -0
  29. data/lib/karafka/pro/processing/strategies/dlq/ftr_lrj_vp.rb +1 -0
  30. data/lib/karafka/pro/processing/strategies/dlq/ftr_mom.rb +3 -7
  31. data/lib/karafka/pro/processing/strategies/dlq/ftr_mom_vp.rb +41 -0
  32. data/lib/karafka/pro/processing/strategies/dlq/ftr_vp.rb +1 -0
  33. data/lib/karafka/pro/processing/strategies/dlq/lrj_mom.rb +3 -6
  34. data/lib/karafka/pro/processing/strategies/dlq/lrj_mom_vp.rb +36 -0
  35. data/lib/karafka/pro/processing/strategies/dlq/lrj_vp.rb +1 -0
  36. data/lib/karafka/pro/processing/strategies/dlq/mom.rb +8 -7
  37. data/lib/karafka/pro/processing/strategies/dlq/mom_vp.rb +37 -0
  38. data/lib/karafka/pro/processing/strategies/lrj/default.rb +2 -0
  39. data/lib/karafka/pro/processing/strategies/lrj/ftr_mom_vp.rb +40 -0
  40. data/lib/karafka/pro/processing/strategies/lrj/mom.rb +2 -0
  41. data/lib/karafka/pro/processing/strategies/lrj/mom_vp.rb +38 -0
  42. data/lib/karafka/pro/processing/strategies/mom/ftr_vp.rb +37 -0
  43. data/lib/karafka/pro/{base_consumer.rb → processing/strategies/mom/vp.rb} +17 -7
  44. data/lib/karafka/pro/processing/strategies/vp/default.rb +51 -0
  45. data/lib/karafka/pro/processing/virtual_offset_manager.rb +147 -0
  46. data/lib/karafka/pro/routing/features/virtual_partitions/contract.rb +0 -17
  47. data/lib/karafka/processing/strategies/default.rb +2 -0
  48. data/lib/karafka/processing/strategies/dlq_mom.rb +9 -7
  49. data/lib/karafka/version.rb +1 -1
  50. data/lib/karafka.rb +5 -0
  51. data.tar.gz.sig +0 -0
  52. metadata +16 -4
  53. metadata.gz.sig +0 -0
  54. data/lib/karafka/instrumentation/vendors/datadog/listener.rb +0 -16
@@ -34,6 +34,8 @@ module Karafka
34
34
 
35
35
  # No actions needed for the standard flow here
36
36
  def handle_before_enqueue
37
+ super
38
+
37
39
  coordinator.on_enqueued do
38
40
  pause(coordinator.seek_offset, Strategies::Lrj::Default::MAX_PAUSE_TIME, false)
39
41
  end
@@ -20,8 +20,8 @@ module Karafka
20
20
  # Manual offset management enabled
21
21
  # Virtual Partitions enabled
22
22
  module MomVp
23
- include Strategies::Vp::Default
24
23
  include Strategies::Default
24
+ include Strategies::Vp::Default
25
25
 
26
26
  # Features for this strategy
27
27
  FEATURES = %i[
@@ -20,7 +20,7 @@ module Karafka
20
20
  # - Ftr
21
21
  module Ftr
22
22
  include Strategies::Ftr::Default
23
- include Strategies::Dlq::Vp
23
+ include Strategies::Dlq::Default
24
24
 
25
25
  # Features for this strategy
26
26
  FEATURES = %i[
@@ -55,14 +55,11 @@ module Karafka
55
55
 
56
56
  return resume if revoked?
57
57
 
58
- skippable_message, marked = find_skippable_message
58
+ skippable_message, _marked = find_skippable_message
59
59
  dispatch_to_dlq(skippable_message) if dispatch_to_dlq?
60
60
 
61
- if marked
62
- pause(coordinator.seek_offset, nil, false)
63
- else
64
- pause(skippable_message.offset + 1, nil, false)
65
- end
61
+ coordinator.seek_offset = skippable_message.offset + 1
62
+ pause(coordinator.seek_offset, nil, false)
66
63
  end
67
64
  end
68
65
  end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This Karafka component is a Pro component under a commercial license.
4
+ # This Karafka component is NOT licensed under LGPL.
5
+ #
6
+ # All of the commercial components are present in the lib/karafka/pro directory of this
7
+ # repository and their usage requires commercial license agreement.
8
+ #
9
+ # Karafka has also commercial-friendly license, commercial support and commercial components.
10
+ #
11
+ # By sending a pull request to the pro components, you are agreeing to transfer the copyright of
12
+ # your code to Maciej Mensfeld.
13
+
14
+ module Karafka
15
+ module Pro
16
+ module Processing
17
+ module Strategies
18
+ module Dlq
19
+ # DLQ enabled
20
+ # Ftr enabled
21
+ # LRJ enabled
22
+ # MoM enabled
23
+ # VP enabled
24
+ module FtrLrjMomVp
25
+ include Strategies::Vp::Default
26
+ include Strategies::Dlq::Vp
27
+ # Same as non VP because of the coordinator post-execution lock
28
+ include Strategies::Dlq::FtrLrjMom
29
+
30
+ # Features for this strategy
31
+ FEATURES = %i[
32
+ dead_letter_queue
33
+ filtering
34
+ long_running_job
35
+ manual_offset_management
36
+ virtual_partitions
37
+ ].freeze
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -22,6 +22,7 @@ module Karafka
22
22
  # Virtual Partitions
23
23
  module FtrLrjVp
24
24
  include Strategies::Vp::Default
25
+ include Strategies::Dlq::Vp
25
26
  # Same as non VP because of the coordinator post-execution lock
26
27
  include Strategies::Dlq::FtrLrj
27
28
 
@@ -53,15 +53,11 @@ module Karafka
53
53
  # We reset the pause to indicate we will now consider it as "ok".
54
54
  coordinator.pause_tracker.reset
55
55
 
56
- skippable_message, marked = find_skippable_message
56
+ skippable_message, _marked = find_skippable_message
57
57
  dispatch_to_dlq(skippable_message) if dispatch_to_dlq?
58
58
 
59
- # Move beyond the broken message
60
- if marked
61
- pause(coordinator.seek_offset, nil, false)
62
- else
63
- pause(skippable_message.offset + 1, nil, false)
64
- end
59
+ coordinator.seek_offset = skippable_message.offset + 1
60
+ pause(coordinator.seek_offset, nil, false)
65
61
  end
66
62
  end
67
63
  end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This Karafka component is a Pro component under a commercial license.
4
+ # This Karafka component is NOT licensed under LGPL.
5
+ #
6
+ # All of the commercial components are present in the lib/karafka/pro directory of this
7
+ # repository and their usage requires commercial license agreement.
8
+ #
9
+ # Karafka has also commercial-friendly license, commercial support and commercial components.
10
+ #
11
+ # By sending a pull request to the pro components, you are agreeing to transfer the copyright of
12
+ # your code to Maciej Mensfeld.
13
+
14
+ module Karafka
15
+ module Pro
16
+ module Processing
17
+ module Strategies
18
+ module Dlq
19
+ # - DLQ
20
+ # - Ftr
21
+ # - Mom
22
+ # - VP
23
+ # We can use the `Strategies::FtrMom` because VPs collapse prior to DLQ
24
+ module FtrMomVp
25
+ include Strategies::Mom::Vp
26
+ include Strategies::Dlq::Vp
27
+ include Strategies::Dlq::FtrMom
28
+
29
+ # Features for this strategy
30
+ FEATURES = %i[
31
+ dead_letter_queue
32
+ filtering
33
+ manual_offset_management
34
+ virtual_partitions
35
+ ].freeze
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -23,6 +23,7 @@ module Karafka
23
23
  # Behaves same as non-VP due to coordinator lock
24
24
  module FtrVp
25
25
  include Strategies::Vp::Default
26
+ include Strategies::Dlq::Vp
26
27
  include Strategies::Dlq::Ftr
27
28
 
28
29
  # Features for this strategy
@@ -47,14 +47,11 @@ module Karafka
47
47
 
48
48
  return resume if revoked?
49
49
 
50
- skippable_message, marked = find_skippable_message
50
+ skippable_message, _marked = find_skippable_message
51
51
  dispatch_to_dlq(skippable_message) if dispatch_to_dlq?
52
52
 
53
- if marked
54
- pause(coordinator.seek_offset, nil, false)
55
- else
56
- pause(skippable_message.offset + 1, nil, false)
57
- end
53
+ coordinator.seek_offset = skippable_message.offset + 1
54
+ pause(coordinator.seek_offset, nil, false)
58
55
  end
59
56
  end
60
57
  end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This Karafka component is a Pro component under a commercial license.
4
+ # This Karafka component is NOT licensed under LGPL.
5
+ #
6
+ # All of the commercial components are present in the lib/karafka/pro directory of this
7
+ # repository and their usage requires commercial license agreement.
8
+ #
9
+ # Karafka has also commercial-friendly license, commercial support and commercial components.
10
+ #
11
+ # By sending a pull request to the pro components, you are agreeing to transfer the copyright of
12
+ # your code to Maciej Mensfeld.
13
+
14
+ module Karafka
15
+ module Pro
16
+ module Processing
17
+ module Strategies
18
+ module Dlq
19
+ # Strategy for supporting DLQ with Mom, LRJ and VP enabled
20
+ module LrjMomVp
21
+ # Feature set for this strategy
22
+ FEATURES = %i[
23
+ dead_letter_queue
24
+ long_running_job
25
+ manual_offset_management
26
+ virtual_partitions
27
+ ].freeze
28
+
29
+ include Strategies::Dlq::Vp
30
+ include Strategies::Dlq::LrjMom
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -22,6 +22,7 @@ module Karafka
22
22
  module LrjVp
23
23
  # Same flow as the Dlq Lrj because VP collapses on errors, so DlqLrj can kick in
24
24
  include Strategies::Vp::Default
25
+ include Strategies::Dlq::Vp
25
26
  include Strategies::Dlq::Lrj
26
27
 
27
28
  # Features for this strategy
@@ -47,15 +47,16 @@ module Karafka
47
47
  # We reset the pause to indicate we will now consider it as "ok".
48
48
  coordinator.pause_tracker.reset
49
49
 
50
- skippable_message, marked = find_skippable_message
50
+ skippable_message, = find_skippable_message
51
51
  dispatch_to_dlq(skippable_message) if dispatch_to_dlq?
52
52
 
53
- # Move beyond the broken message
54
- if marked
55
- pause(coordinator.seek_offset, nil, false)
56
- else
57
- pause(skippable_message.offset + 1, nil, false)
58
- end
53
+ # Save the next offset we want to go with after moving given message to DLQ
54
+ # Without this, we would not be able to move forward and we would end up
55
+ # in an infinite loop trying to un-pause from the message we've already processed
56
+ # Of course, since it's a MoM a rebalance or kill, will move it back as no
57
+ # offsets are being committed
58
+ coordinator.seek_offset = skippable_message.offset + 1
59
+ pause(coordinator.seek_offset, nil, false)
59
60
  end
60
61
  end
61
62
  end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This Karafka component is a Pro component under a commercial license.
4
+ # This Karafka component is NOT licensed under LGPL.
5
+ #
6
+ # All of the commercial components are present in the lib/karafka/pro directory of this
7
+ # repository and their usage requires commercial license agreement.
8
+ #
9
+ # Karafka has also commercial-friendly license, commercial support and commercial components.
10
+ #
11
+ # By sending a pull request to the pro components, you are agreeing to transfer the copyright of
12
+ # your code to Maciej Mensfeld.
13
+
14
+ module Karafka
15
+ module Pro
16
+ module Processing
17
+ module Strategies
18
+ module Dlq
19
+ # Dead-Letter Queue enabled
20
+ # MoM enabled
21
+ # Virtual Partitions enabled
22
+ module MomVp
23
+ include Strategies::Dlq::Vp
24
+ include Strategies::Dlq::Mom
25
+
26
+ # Features for this strategy
27
+ FEATURES = %i[
28
+ dead_letter_queue
29
+ manual_offset_management
30
+ virtual_partitions
31
+ ].freeze
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -30,6 +30,8 @@ module Karafka
30
30
 
31
31
  # We always need to pause prior to doing any jobs for LRJ
32
32
  def handle_before_enqueue
33
+ super
34
+
33
35
  # This ensures that when running LRJ with VP, things operate as expected run only
34
36
  # once for all the virtual partitions collectively
35
37
  coordinator.on_enqueued do
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This Karafka component is a Pro component under a commercial license.
4
+ # This Karafka component is NOT licensed under LGPL.
5
+ #
6
+ # All of the commercial components are present in the lib/karafka/pro directory of this
7
+ # repository and their usage requires commercial license agreement.
8
+ #
9
+ # Karafka has also commercial-friendly license, commercial support and commercial components.
10
+ #
11
+ # By sending a pull request to the pro components, you are agreeing to transfer the copyright of
12
+ # your code to Maciej Mensfeld.
13
+
14
+ module Karafka
15
+ module Pro
16
+ module Processing
17
+ module Strategies
18
+ # Filtering related init strategies
19
+ module Lrj
20
+ # Filtering enabled
21
+ # LRJ enabled
22
+ # MoM enabled
23
+ # VPs enabled
24
+ module FtrMomVp
25
+ # Filtering + LRJ + Mom + VPs
26
+ FEATURES = %i[
27
+ filtering
28
+ long_running_job
29
+ manual_offset_management
30
+ virtual_partitions
31
+ ].freeze
32
+
33
+ include Strategies::Lrj::MomVp
34
+ include Strategies::Lrj::FtrMom
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -30,6 +30,8 @@ module Karafka
30
30
 
31
31
  # We always need to pause prior to doing any jobs for LRJ
32
32
  def handle_before_enqueue
33
+ super
34
+
33
35
  # This ensures that when running LRJ with VP, things operate as expected run only
34
36
  # once for all the virtual partitions collectively
35
37
  coordinator.on_enqueued do
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This Karafka component is a Pro component under a commercial license.
4
+ # This Karafka component is NOT licensed under LGPL.
5
+ #
6
+ # All of the commercial components are present in the lib/karafka/pro directory of this
7
+ # repository and their usage requires commercial license agreement.
8
+ #
9
+ # Karafka has also commercial-friendly license, commercial support and commercial components.
10
+ #
11
+ # By sending a pull request to the pro components, you are agreeing to transfer the copyright of
12
+ # your code to Maciej Mensfeld.
13
+
14
+ module Karafka
15
+ module Pro
16
+ module Processing
17
+ module Strategies
18
+ # Namespace for all the LRJ starting strategies
19
+ module Lrj
20
+ # Long-Running Job enabled
21
+ # Manual offset management enabled
22
+ # Virtual Partitions enabled
23
+ module MomVp
24
+ include Strategies::Mom::Vp
25
+ include Strategies::Lrj::Mom
26
+
27
+ # Features for this strategy
28
+ FEATURES = %i[
29
+ long_running_job
30
+ manual_offset_management
31
+ virtual_partitions
32
+ ].freeze
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This Karafka component is a Pro component under a commercial license.
4
+ # This Karafka component is NOT licensed under LGPL.
5
+ #
6
+ # All of the commercial components are present in the lib/karafka/pro directory of this
7
+ # repository and their usage requires commercial license agreement.
8
+ #
9
+ # Karafka has also commercial-friendly license, commercial support and commercial components.
10
+ #
11
+ # By sending a pull request to the pro components, you are agreeing to transfer the copyright of
12
+ # your code to Maciej Mensfeld.
13
+
14
+ module Karafka
15
+ module Pro
16
+ module Processing
17
+ module Strategies
18
+ module Mom
19
+ # - Mom enabled
20
+ # - Ftr enabled
21
+ # - Vp enabled
22
+ module FtrVp
23
+ include Strategies::Mom::Vp
24
+ include Strategies::Mom::Ftr
25
+
26
+ # Features of this strategy
27
+ FEATURES = %i[
28
+ filtering
29
+ manual_offset_management
30
+ virtual_partitions
31
+ ].freeze
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -13,13 +13,23 @@
13
13
 
14
14
  module Karafka
15
15
  module Pro
16
- # Karafka PRO consumer.
17
- #
18
- # If you use PRO, all your consumers should inherit (indirectly) from it.
19
- #
20
- # @note In case of using lrj, manual pausing may not be the best idea as resume needs to happen
21
- # after each batch is processed.
22
- class BaseConsumer < Karafka::BaseConsumer
16
+ module Processing
17
+ module Strategies
18
+ module Mom
19
+ # - Mom enabled
20
+ # - Vp enabled
21
+ module Vp
22
+ include Strategies::Vp::Default
23
+ include Strategies::Mom::Default
24
+
25
+ # Features of this strategy
26
+ FEATURES = %i[
27
+ manual_offset_management
28
+ virtual_partitions
29
+ ].freeze
30
+ end
31
+ end
32
+ end
23
33
  end
24
34
  end
25
35
  end
@@ -28,6 +28,44 @@ module Karafka
28
28
  virtual_partitions
29
29
  ].freeze
30
30
 
31
+ # @param message [Karafka::Messages::Message] marks message as consumed
32
+ # @note This virtual offset management uses a regular default marking API underneath.
33
+ # We do not alter the "real" marking API, as VPs are just one of many cases we want
34
+ # to support and we do not want to impact them with collective offsets management
35
+ def mark_as_consumed(message)
36
+ return super if collapsed?
37
+
38
+ manager = coordinator.virtual_offset_manager
39
+
40
+ coordinator.synchronize do
41
+ manager.mark(message)
42
+ # If this is last marking on a finished flow, we can use the original
43
+ # last message and in order to do so, we need to mark all previous messages as
44
+ # consumed as otherwise the computed offset could be different
45
+ # We mark until our offset just in case of a DLQ flow or similar, where we do not
46
+ # want to mark all but until the expected location
47
+ manager.mark_until(message) if coordinator.finished?
48
+
49
+ return revoked? unless manager.markable?
50
+ end
51
+
52
+ manager.markable? ? super(manager.markable) : revoked?
53
+ end
54
+
55
+ # @param message [Karafka::Messages::Message] blocking marks message as consumed
56
+ def mark_as_consumed!(message)
57
+ return super if collapsed?
58
+
59
+ manager = coordinator.virtual_offset_manager
60
+
61
+ coordinator.synchronize do
62
+ manager.mark(message)
63
+ manager.mark_until(message) if coordinator.finished?
64
+ end
65
+
66
+ manager.markable? ? super(manager.markable) : revoked?
67
+ end
68
+
31
69
  # @return [Boolean] is the virtual processing collapsed in the context of given
32
70
  # consumer.
33
71
  def collapsed?
@@ -45,6 +83,19 @@ module Karafka
45
83
  def failing?
46
84
  coordinator.failure?
47
85
  end
86
+
87
+ private
88
+
89
+ # Prior to adding work to the queue, registers all the messages offsets into the
90
+ # virtual offset group.
91
+ #
92
+ # @note This can be done without the mutex, because it happens from the same thread
93
+ # for all the work (listener thread)
94
+ def handle_before_enqueue
95
+ coordinator.virtual_offset_manager.register(
96
+ messages.map(&:offset)
97
+ )
98
+ end
48
99
  end
49
100
  end
50
101
  end