karafka 2.4.18 → 2.5.0.beta1
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
- checksums.yaml.gz.sig +0 -0
- data/.github/CODEOWNERS +3 -0
- data/.github/workflows/ci.yml +58 -14
- data/.github/workflows/verify-action-pins.yml +16 -0
- data/.ruby-version +1 -1
- data/CHANGELOG.md +53 -0
- data/Gemfile +3 -3
- data/Gemfile.lock +55 -58
- data/LICENSE-COMM +2 -2
- data/bin/clean_kafka +43 -0
- data/bin/integrations +17 -5
- data/bin/rspecs +15 -3
- data/bin/verify_kafka_warnings +35 -0
- data/bin/verify_topics_naming +27 -0
- data/config/locales/errors.yml +3 -0
- data/config/locales/pro_errors.yml +13 -2
- data/docker-compose.yml +1 -1
- data/examples/payloads/json/enrollment_event.json +579 -0
- data/examples/payloads/json/ingestion_event.json +30 -0
- data/examples/payloads/json/transaction_event.json +17 -0
- data/examples/payloads/json/user_event.json +11 -0
- data/karafka.gemspec +3 -3
- data/lib/karafka/active_job/current_attributes.rb +1 -1
- data/lib/karafka/admin/acl.rb +5 -1
- data/lib/karafka/admin.rb +51 -19
- data/lib/karafka/base_consumer.rb +17 -8
- data/lib/karafka/cli/base.rb +8 -2
- data/lib/karafka/connection/client.rb +20 -7
- data/lib/karafka/connection/listener.rb +24 -12
- data/lib/karafka/connection/messages_buffer.rb +1 -1
- data/lib/karafka/connection/proxy.rb +3 -0
- data/lib/karafka/contracts/config.rb +3 -0
- data/lib/karafka/contracts/topic.rb +1 -1
- data/lib/karafka/errors.rb +11 -0
- data/lib/karafka/helpers/async.rb +3 -1
- data/lib/karafka/instrumentation/callbacks/rebalance.rb +5 -1
- data/lib/karafka/instrumentation/logger_listener.rb +86 -23
- data/lib/karafka/instrumentation/proctitle_listener.rb +5 -1
- data/lib/karafka/instrumentation/vendors/datadog/metrics_listener.rb +2 -2
- data/lib/karafka/messages/builders/batch_metadata.rb +1 -1
- data/lib/karafka/pro/cleaner.rb +8 -0
- data/lib/karafka/pro/cli/parallel_segments/base.rb +89 -0
- data/lib/karafka/pro/cli/parallel_segments/collapse.rb +164 -0
- data/lib/karafka/pro/cli/parallel_segments/distribute.rb +164 -0
- data/lib/karafka/pro/cli/parallel_segments.rb +60 -0
- data/lib/karafka/pro/connection/manager.rb +5 -8
- data/lib/karafka/pro/encryption.rb +8 -0
- data/lib/karafka/pro/instrumentation/performance_tracker.rb +1 -1
- data/lib/karafka/pro/iterator/expander.rb +5 -3
- data/lib/karafka/pro/iterator/tpl_builder.rb +23 -0
- data/lib/karafka/pro/loader.rb +10 -0
- data/lib/karafka/pro/processing/coordinator.rb +4 -1
- data/lib/karafka/pro/processing/coordinators/errors_tracker.rb +27 -3
- data/lib/karafka/pro/processing/coordinators/filters_applier.rb +11 -0
- data/lib/karafka/pro/processing/filters/base.rb +10 -2
- data/lib/karafka/pro/processing/filters/expirer.rb +5 -0
- data/lib/karafka/pro/processing/filters/inline_insights_delayer.rb +2 -2
- data/lib/karafka/pro/processing/filters/virtual_limiter.rb +5 -0
- data/lib/karafka/pro/processing/parallel_segments/filters/base.rb +73 -0
- data/lib/karafka/pro/processing/parallel_segments/filters/default.rb +85 -0
- data/lib/karafka/pro/processing/parallel_segments/filters/mom.rb +66 -0
- data/lib/karafka/pro/processing/partitioner.rb +1 -13
- data/lib/karafka/pro/processing/piping/consumer.rb +13 -13
- data/lib/karafka/pro/processing/strategies/aj/dlq_ftr_lrj_mom.rb +1 -1
- data/lib/karafka/pro/processing/strategies/aj/dlq_ftr_lrj_mom_vp.rb +1 -1
- data/lib/karafka/pro/processing/strategies/aj/dlq_lrj_mom.rb +1 -1
- data/lib/karafka/pro/processing/strategies/aj/dlq_lrj_mom_vp.rb +1 -1
- data/lib/karafka/pro/processing/strategies/aj/ftr_lrj_mom_vp.rb +1 -1
- data/lib/karafka/pro/processing/strategies/aj/lrj_mom_vp.rb +1 -1
- data/lib/karafka/pro/processing/strategies/default.rb +36 -8
- data/lib/karafka/pro/processing/strategies/dlq/default.rb +14 -10
- data/lib/karafka/pro/processing/strategies/dlq/ftr_lrj.rb +1 -1
- data/lib/karafka/pro/processing/strategies/dlq/ftr_lrj_mom.rb +1 -1
- data/lib/karafka/pro/processing/strategies/dlq/lrj.rb +3 -1
- data/lib/karafka/pro/processing/strategies/dlq/lrj_mom.rb +1 -1
- data/lib/karafka/pro/processing/strategies/ftr/default.rb +1 -1
- data/lib/karafka/pro/processing/strategies/lrj/default.rb +4 -1
- data/lib/karafka/pro/processing/strategies/lrj/ftr.rb +1 -1
- data/lib/karafka/pro/processing/strategies/lrj/ftr_mom.rb +1 -1
- data/lib/karafka/pro/processing/strategies/lrj/mom.rb +1 -1
- data/lib/karafka/pro/processing/virtual_partitions/distributors/balanced.rb +50 -0
- data/lib/karafka/pro/processing/virtual_partitions/distributors/base.rb +29 -0
- data/lib/karafka/pro/processing/virtual_partitions/distributors/consistent.rb +27 -0
- data/lib/karafka/pro/recurring_tasks/contracts/config.rb +8 -4
- data/lib/karafka/pro/recurring_tasks/dispatcher.rb +3 -3
- data/lib/karafka/pro/recurring_tasks/setup/config.rb +7 -2
- data/lib/karafka/pro/recurring_tasks.rb +13 -0
- data/lib/karafka/pro/routing/features/dead_letter_queue/topic.rb +1 -1
- data/lib/karafka/pro/routing/features/multiplexing/config.rb +1 -0
- data/lib/karafka/pro/routing/features/multiplexing/contracts/topic.rb +17 -0
- data/lib/karafka/pro/routing/features/multiplexing/proxy.rb +5 -2
- data/lib/karafka/pro/routing/features/multiplexing/subscription_group.rb +8 -1
- data/lib/karafka/pro/routing/features/parallel_segments/builder.rb +47 -0
- data/lib/karafka/pro/routing/features/parallel_segments/config.rb +27 -0
- data/lib/karafka/pro/routing/features/parallel_segments/consumer_group.rb +83 -0
- data/lib/karafka/pro/routing/features/parallel_segments/contracts/consumer_group.rb +49 -0
- data/lib/karafka/pro/routing/features/parallel_segments/topic.rb +43 -0
- data/lib/karafka/pro/routing/features/parallel_segments.rb +24 -0
- data/lib/karafka/pro/routing/features/patterns/pattern.rb +1 -1
- data/lib/karafka/pro/routing/features/recurring_tasks/builder.rb +2 -2
- data/lib/karafka/pro/routing/features/scheduled_messages/builder.rb +10 -6
- data/lib/karafka/pro/routing/features/virtual_partitions/config.rb +20 -2
- data/lib/karafka/pro/routing/features/virtual_partitions/contracts/topic.rb +1 -0
- data/lib/karafka/pro/routing/features/virtual_partitions/topic.rb +8 -2
- data/lib/karafka/pro/scheduled_messages/consumer.rb +14 -15
- data/lib/karafka/pro/scheduled_messages/daily_buffer.rb +9 -6
- data/lib/karafka/pro/scheduled_messages/deserializers/headers.rb +7 -1
- data/lib/karafka/pro/scheduled_messages/max_epoch.rb +15 -6
- data/lib/karafka/pro/scheduled_messages.rb +13 -0
- data/lib/karafka/processing/coordinators_buffer.rb +1 -0
- data/lib/karafka/processing/strategies/default.rb +4 -4
- data/lib/karafka/routing/features/dead_letter_queue/contracts/topic.rb +1 -0
- data/lib/karafka/routing/subscription_group.rb +1 -1
- data/lib/karafka/runner.rb +7 -1
- data/lib/karafka/server.rb +5 -0
- data/lib/karafka/setup/attributes_map.rb +2 -0
- data/lib/karafka/setup/config.rb +22 -1
- data/lib/karafka/setup/defaults_injector.rb +26 -1
- data/lib/karafka/status.rb +6 -1
- data/lib/karafka/swarm/node.rb +31 -0
- data/lib/karafka/swarm/supervisor.rb +4 -0
- data/lib/karafka/templates/karafka.rb.erb +14 -1
- data/lib/karafka/version.rb +1 -1
- data/lib/karafka.rb +17 -9
- data/renovate.json +14 -2
- data.tar.gz.sig +0 -0
- metadata +36 -11
- metadata.gz.sig +0 -0
@@ -14,12 +14,15 @@ module Karafka
|
|
14
14
|
# disabling dynamic multiplexing
|
15
15
|
# @param max [Integer] max multiplexing count
|
16
16
|
# @param boot [Integer] how many listeners should we start during boot by default
|
17
|
-
|
17
|
+
# @param scale_delay [Integer] number of ms of delay before applying any scale
|
18
|
+
# operation to a consumer group
|
19
|
+
def multiplexing(min: nil, max: 1, boot: nil, scale_delay: 60_000)
|
18
20
|
@target.current_subscription_group_details.merge!(
|
19
21
|
multiplexing_min: min || max,
|
20
22
|
multiplexing_max: max,
|
21
23
|
# Picks half of max by default as long as possible. Otherwise goes with min
|
22
|
-
multiplexing_boot: boot || [min || max, (max / 2)].max
|
24
|
+
multiplexing_boot: boot || [min || max, (max / 2)].max,
|
25
|
+
multiplexing_scale_delay: scale_delay
|
23
26
|
)
|
24
27
|
end
|
25
28
|
end
|
@@ -16,9 +16,16 @@ module Karafka
|
|
16
16
|
max = @details.fetch(:multiplexing_max, 1)
|
17
17
|
min = @details.fetch(:multiplexing_min, max)
|
18
18
|
boot = @details.fetch(:multiplexing_boot, max / 2)
|
19
|
+
scale_delay = @details.fetch(:multiplexing_scale_delay, 60_000)
|
19
20
|
active = max > 1
|
20
21
|
|
21
|
-
Config.new(
|
22
|
+
Config.new(
|
23
|
+
active: active,
|
24
|
+
min: min,
|
25
|
+
max: max,
|
26
|
+
boot: boot,
|
27
|
+
scale_delay: scale_delay
|
28
|
+
)
|
22
29
|
end
|
23
30
|
end
|
24
31
|
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# This code is part of Karafka Pro, a commercial component not licensed under LGPL.
|
4
|
+
# See LICENSE for details.
|
5
|
+
|
6
|
+
module Karafka
|
7
|
+
module Pro
|
8
|
+
module Routing
|
9
|
+
module Features
|
10
|
+
class ParallelSegments < Base
|
11
|
+
# Expansions for the routing builder
|
12
|
+
module Builder
|
13
|
+
# Builds and saves given consumer group
|
14
|
+
# @param group_id [String, Symbol] name for consumer group
|
15
|
+
# @param block [Proc] proc that should be executed in the proxy context
|
16
|
+
def consumer_group(group_id, &block)
|
17
|
+
consumer_group = find { |cg| cg.name == group_id.to_s }
|
18
|
+
|
19
|
+
# Re-opening a CG should not change its parallel setup
|
20
|
+
if consumer_group
|
21
|
+
super
|
22
|
+
else
|
23
|
+
# We build a temp consumer group and a target to check if it has parallel segments
|
24
|
+
# enabled and if so, we do not add it to the routing but instead we build the
|
25
|
+
# appropriate number of parallel segment groups
|
26
|
+
temp_consumer_group = ::Karafka::Routing::ConsumerGroup.new(group_id.to_s)
|
27
|
+
temp_target = Karafka::Routing::Proxy.new(temp_consumer_group, &block).target
|
28
|
+
config = temp_target.parallel_segments
|
29
|
+
|
30
|
+
if config.active?
|
31
|
+
config.count.times do |i|
|
32
|
+
sub_name = [group_id, config.merge_key, i.to_s].join
|
33
|
+
sub_consumer_group = Karafka::Routing::ConsumerGroup.new(sub_name)
|
34
|
+
self << Karafka::Routing::Proxy.new(sub_consumer_group, &block).target
|
35
|
+
end
|
36
|
+
# If not parallel segments are not active we go with the default flow
|
37
|
+
else
|
38
|
+
super
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# This code is part of Karafka Pro, a commercial component not licensed under LGPL.
|
4
|
+
# See LICENSE for details.
|
5
|
+
|
6
|
+
module Karafka
|
7
|
+
module Pro
|
8
|
+
module Routing
|
9
|
+
module Features
|
10
|
+
class ParallelSegments < Base
|
11
|
+
# Config for parallel segments.
|
12
|
+
# @note Used on the consumer level, not per topic
|
13
|
+
Config = Struct.new(
|
14
|
+
:active,
|
15
|
+
:count,
|
16
|
+
:partitioner,
|
17
|
+
:reducer,
|
18
|
+
:merge_key,
|
19
|
+
keyword_init: true
|
20
|
+
) do
|
21
|
+
alias_method :active?, :active
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# This code is part of Karafka Pro, a commercial component not licensed under LGPL.
|
4
|
+
# See LICENSE for details.
|
5
|
+
|
6
|
+
module Karafka
|
7
|
+
module Pro
|
8
|
+
module Routing
|
9
|
+
module Features
|
10
|
+
class ParallelSegments < Base
|
11
|
+
# Parallel segments are defined on the consumer group (since it creates many), thus we
|
12
|
+
# define them on the consumer group.
|
13
|
+
# This module adds extra methods needed there to make it work
|
14
|
+
module ConsumerGroup
|
15
|
+
# @return [Config] parallel segments config
|
16
|
+
def parallel_segments
|
17
|
+
# We initialize it as disabled if not configured by the user
|
18
|
+
public_send(:parallel_segments=, count: 1)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Allows setting parallel segments configuration
|
22
|
+
#
|
23
|
+
# @param count [Integer] number of parallel segments (number of parallel consumer
|
24
|
+
# groups that will be created)
|
25
|
+
# @param partitioner [nil, #call] nil or callable partitioner
|
26
|
+
# @param reducer [nil, #call] reducer for parallel key. It allows for using a custom
|
27
|
+
# reducer to achieve enhanced parallelization when the default reducer is not enough.
|
28
|
+
# @param merge_key [String] key used to build the parallel segment consumer groups
|
29
|
+
#
|
30
|
+
# @note This method is an assignor but the API is actually via the `#parallel_segments`
|
31
|
+
# method. Our `Routing::Proxy` normalizes that the way we want to have it exposed
|
32
|
+
# for the end users.
|
33
|
+
def parallel_segments=(
|
34
|
+
count: 1,
|
35
|
+
partitioner: nil,
|
36
|
+
reducer: nil,
|
37
|
+
merge_key: '-parallel-'
|
38
|
+
)
|
39
|
+
@parallel_segments ||= Config.new(
|
40
|
+
active: count > 1,
|
41
|
+
count: count,
|
42
|
+
partitioner: partitioner,
|
43
|
+
reducer: reducer || ->(parallel_key) { parallel_key.to_s.sum % count },
|
44
|
+
merge_key: merge_key
|
45
|
+
)
|
46
|
+
end
|
47
|
+
|
48
|
+
# @return [Boolean] are parallel segments active
|
49
|
+
def parallel_segments?
|
50
|
+
parallel_segments.active?
|
51
|
+
end
|
52
|
+
|
53
|
+
# @return [Integer] id of the segment (0 or bigger) or -1 if parallel segments are not
|
54
|
+
# active
|
55
|
+
def segment_id
|
56
|
+
return @segment_id if @segment_id
|
57
|
+
|
58
|
+
@segment_id = if parallel_segments?
|
59
|
+
name.split(parallel_segments.merge_key).last.to_i
|
60
|
+
else
|
61
|
+
-1
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# @return [String] original segment consumer group name
|
66
|
+
def segment_origin
|
67
|
+
name.split(parallel_segments.merge_key).first
|
68
|
+
end
|
69
|
+
|
70
|
+
# @return [Hash] consumer group setup with the parallel segments definition in it
|
71
|
+
def to_h
|
72
|
+
super.merge(
|
73
|
+
parallel_segments: parallel_segments.to_h.merge(
|
74
|
+
segment_id: segment_id
|
75
|
+
)
|
76
|
+
).freeze
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# This code is part of Karafka Pro, a commercial component not licensed under LGPL.
|
4
|
+
# See LICENSE for details.
|
5
|
+
|
6
|
+
module Karafka
|
7
|
+
module Pro
|
8
|
+
module Routing
|
9
|
+
module Features
|
10
|
+
class ParallelSegments < Base
|
11
|
+
# Namespace for parallel segments contracts
|
12
|
+
module Contracts
|
13
|
+
# Contract to validate configuration of the parallel segments feature
|
14
|
+
class ConsumerGroup < Karafka::Contracts::Base
|
15
|
+
configure do |config|
|
16
|
+
config.error_messages = YAML.safe_load(
|
17
|
+
File.read(
|
18
|
+
File.join(Karafka.gem_root, 'config', 'locales', 'pro_errors.yml')
|
19
|
+
)
|
20
|
+
).fetch('en').fetch('validations').fetch('consumer_group')
|
21
|
+
|
22
|
+
nested(:parallel_segments) do
|
23
|
+
required(:active) { |val| [true, false].include?(val) }
|
24
|
+
required(:partitioner) { |val| val.nil? || val.respond_to?(:call) }
|
25
|
+
required(:reducer) { |val| val.respond_to?(:call) }
|
26
|
+
required(:count) { |val| val.is_a?(Integer) && val >= 1 }
|
27
|
+
required(:merge_key) { |val| val.is_a?(String) && val.size >= 1 }
|
28
|
+
end
|
29
|
+
|
30
|
+
# When parallel segments are defined, partitioner needs to respond to `#call` and
|
31
|
+
# it cannot be nil
|
32
|
+
virtual do |data, errors|
|
33
|
+
next unless errors.empty?
|
34
|
+
|
35
|
+
parallel_segments = data[:parallel_segments]
|
36
|
+
|
37
|
+
next unless parallel_segments[:active]
|
38
|
+
next if parallel_segments[:partitioner].respond_to?(:call)
|
39
|
+
|
40
|
+
[[%i[parallel_segments partitioner], :respond_to_call]]
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# This code is part of Karafka Pro, a commercial component not licensed under LGPL.
|
4
|
+
# See LICENSE for details.
|
5
|
+
|
6
|
+
module Karafka
|
7
|
+
module Pro
|
8
|
+
module Routing
|
9
|
+
module Features
|
10
|
+
class ParallelSegments < Base
|
11
|
+
# Parallel segments related expansions to the topic building flow
|
12
|
+
module Topic
|
13
|
+
# Injects the parallel segments filter as the first filter during building of each of
|
14
|
+
# the topics in case parallel segments are enabled.
|
15
|
+
#
|
16
|
+
# @param args [Object] anything accepted by the topic initializer
|
17
|
+
def initialize(*args)
|
18
|
+
super
|
19
|
+
|
20
|
+
return unless consumer_group.parallel_segments?
|
21
|
+
|
22
|
+
builder = lambda do |topic, _partition|
|
23
|
+
mom = topic.manual_offset_management?
|
24
|
+
|
25
|
+
# We have two filters for mom and non-mom scenario not to mix this logic
|
26
|
+
filter_scope = Karafka::Pro::Processing::ParallelSegments::Filters
|
27
|
+
filter_class = mom ? filter_scope::Mom : filter_scope::Default
|
28
|
+
|
29
|
+
filter_class.new(
|
30
|
+
segment_id: consumer_group.segment_id,
|
31
|
+
partitioner: consumer_group.parallel_segments.partitioner,
|
32
|
+
reducer: consumer_group.parallel_segments.reducer
|
33
|
+
)
|
34
|
+
end
|
35
|
+
|
36
|
+
filter(builder)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# This code is part of Karafka Pro, a commercial component not licensed under LGPL.
|
4
|
+
# See LICENSE for details.
|
5
|
+
|
6
|
+
module Karafka
|
7
|
+
module Pro
|
8
|
+
module Routing
|
9
|
+
module Features
|
10
|
+
# Feature that allows parallelizing message processing within a single consumer group by
|
11
|
+
# creating multiple consumer group instances. It enables processing messages from each
|
12
|
+
# partition in parallel by distributing them to separate consumer group instances based on
|
13
|
+
# a partitioning key. Useful for both CPU and IO bound operations.
|
14
|
+
#
|
15
|
+
# Each parallel segment operates as an independent consumer group instance, processing
|
16
|
+
# messages that are assigned to it based on the configured partitioner and reducer.
|
17
|
+
# This allows for better resource utilization and increased processing throughput without
|
18
|
+
# requiring changes to the topic's partition count.
|
19
|
+
class ParallelSegments < Base
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -45,7 +45,7 @@ module Karafka
|
|
45
45
|
# topic but this minimizes simple mistakes
|
46
46
|
#
|
47
47
|
# This sub-part of sh1 should be unique enough and short-enough to use it here
|
48
|
-
digest = Digest::
|
48
|
+
digest = Digest::SHA256.hexdigest(safe_regexp.source)[8..16]
|
49
49
|
@name = name ? name.to_s : "karafka-pattern-#{digest}"
|
50
50
|
@config = config
|
51
51
|
end
|
@@ -29,7 +29,7 @@ module Karafka
|
|
29
29
|
consumer_group tasks_cfg.group_id do
|
30
30
|
# Registers the primary topic that we use to control schedules execution. This is
|
31
31
|
# the one that we use to trigger recurring tasks.
|
32
|
-
schedules_topic = topic(topics_cfg.schedules) do
|
32
|
+
schedules_topic = topic(topics_cfg.schedules.name) do
|
33
33
|
consumer tasks_cfg.consumer_class
|
34
34
|
deserializer tasks_cfg.deserializer
|
35
35
|
# Because the topic method name as well as builder proxy method name is the same
|
@@ -83,7 +83,7 @@ module Karafka
|
|
83
83
|
|
84
84
|
# This topic is to store logs that we can then inspect either from the admin or via
|
85
85
|
# the Web UI
|
86
|
-
logs_topic = topic(topics_cfg.logs) do
|
86
|
+
logs_topic = topic(topics_cfg.logs.name) do
|
87
87
|
active(false)
|
88
88
|
deserializer tasks_cfg.deserializer
|
89
89
|
target.recurring_tasks(true)
|
@@ -12,14 +12,14 @@ module Karafka
|
|
12
12
|
module Builder
|
13
13
|
# Enabled scheduled messages operations and adds needed topics and other stuff.
|
14
14
|
#
|
15
|
-
# @param
|
15
|
+
# @param topic_name [String, false] name for scheduled messages topic that is also used
|
16
16
|
# as a group identifier. Users can have multiple schedule topics flows to prevent key
|
17
17
|
# collisions, prioritize and do other stuff. `false` if not active.
|
18
18
|
# @param block [Proc] optional reconfiguration of the topics definitions.
|
19
19
|
# @note Namespace for topics should include the divider as it is not automatically
|
20
20
|
# added.
|
21
|
-
def scheduled_messages(
|
22
|
-
return unless
|
21
|
+
def scheduled_messages(topic_name = false, &block)
|
22
|
+
return unless topic_name
|
23
23
|
|
24
24
|
# Load zlib only if user enables scheduled messages
|
25
25
|
require 'zlib'
|
@@ -32,7 +32,7 @@ module Karafka
|
|
32
32
|
consumer_group msg_cfg.group_id do
|
33
33
|
# Registers the primary topic that we use to control schedules execution. This is
|
34
34
|
# the one that we use to trigger scheduled messages.
|
35
|
-
messages_topic = topic(
|
35
|
+
messages_topic = topic(topic_name) do
|
36
36
|
instance_eval(&block) if block && block.arity.zero?
|
37
37
|
|
38
38
|
consumer msg_cfg.consumer_class
|
@@ -54,7 +54,11 @@ module Karafka
|
|
54
54
|
consumer_persistence(true)
|
55
55
|
|
56
56
|
# This needs to be enabled for the eof to work correctly
|
57
|
-
kafka(
|
57
|
+
kafka(
|
58
|
+
'enable.partition.eof': true,
|
59
|
+
'auto.offset.reset': 'earliest',
|
60
|
+
inherit: true
|
61
|
+
)
|
58
62
|
eofed(true)
|
59
63
|
|
60
64
|
# Since this is a topic that gets replayed because of schedule management, we do
|
@@ -96,7 +100,7 @@ module Karafka
|
|
96
100
|
# Holds states of scheduler per each of the partitions since they tick
|
97
101
|
# independently. We only hold future statistics not to have to deal with
|
98
102
|
# any type of state restoration
|
99
|
-
states_topic = topic("#{
|
103
|
+
states_topic = topic("#{topic_name}#{msg_cfg.states_postfix}") do
|
100
104
|
active(false)
|
101
105
|
target.scheduled_messages(true)
|
102
106
|
config(
|
@@ -8,15 +8,33 @@ module Karafka
|
|
8
8
|
module Routing
|
9
9
|
module Features
|
10
10
|
class VirtualPartitions < Base
|
11
|
-
#
|
11
|
+
# Configuration for virtual partitions feature
|
12
12
|
Config = Struct.new(
|
13
13
|
:active,
|
14
14
|
:partitioner,
|
15
15
|
:max_partitions,
|
16
16
|
:offset_metadata_strategy,
|
17
17
|
:reducer,
|
18
|
+
:distribution,
|
18
19
|
keyword_init: true
|
19
|
-
)
|
20
|
+
) do
|
21
|
+
# @return [Boolean] is this feature active
|
22
|
+
def active?
|
23
|
+
active
|
24
|
+
end
|
25
|
+
|
26
|
+
# @return [Object] distributor instance for the current distribution
|
27
|
+
def distributor
|
28
|
+
@distributor ||= case distribution
|
29
|
+
when :balanced
|
30
|
+
Processing::VirtualPartitions::Distributors::Balanced.new(self)
|
31
|
+
when :consistent
|
32
|
+
Processing::VirtualPartitions::Distributors::Consistent.new(self)
|
33
|
+
else
|
34
|
+
raise Karafka::Errors::UnsupportedCaseError, distribution
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
20
38
|
end
|
21
39
|
end
|
22
40
|
end
|
@@ -26,6 +26,7 @@ module Karafka
|
|
26
26
|
required(:reducer) { |val| val.respond_to?(:call) }
|
27
27
|
required(:max_partitions) { |val| val.is_a?(Integer) && val >= 1 }
|
28
28
|
required(:offset_metadata_strategy) { |val| %i[exact current].include?(val) }
|
29
|
+
required(:distribution) { |val| %i[consistent balanced].include?(val) }
|
29
30
|
end
|
30
31
|
|
31
32
|
# When virtual partitions are defined, partitioner needs to respond to `#call` and it
|
@@ -20,13 +20,18 @@ module Karafka
|
|
20
20
|
# the most recently reported metadata
|
21
21
|
# @param reducer [nil, #call] reducer for VPs key. It allows for using a custom
|
22
22
|
# reducer to achieve enhanced parallelization when the default reducer is not enough.
|
23
|
+
# @param distribution [Symbol] the strategy to use for virtual partitioning. Can be
|
24
|
+
# either `:consistent` or `:balanced`. The `:balanced` strategy ensures balanced
|
25
|
+
# distribution of work across available workers while maintaining message order
|
26
|
+
# within groups.
|
23
27
|
# @return [VirtualPartitions] method that allows to set the virtual partitions details
|
24
28
|
# during the routing configuration and then allows to retrieve it
|
25
29
|
def virtual_partitions(
|
26
30
|
max_partitions: Karafka::App.config.concurrency,
|
27
31
|
partitioner: nil,
|
28
32
|
offset_metadata_strategy: :current,
|
29
|
-
reducer: nil
|
33
|
+
reducer: nil,
|
34
|
+
distribution: :consistent
|
30
35
|
)
|
31
36
|
@virtual_partitions ||= Config.new(
|
32
37
|
active: !partitioner.nil?,
|
@@ -35,7 +40,8 @@ module Karafka
|
|
35
40
|
offset_metadata_strategy: offset_metadata_strategy,
|
36
41
|
# If no reducer provided, we use this one. It just runs a modulo on the sum of
|
37
42
|
# a stringified version, providing fairly good distribution.
|
38
|
-
reducer: reducer || ->(virtual_key) { virtual_key.to_s.sum % max_partitions }
|
43
|
+
reducer: reducer || ->(virtual_key) { virtual_key.to_s.sum % max_partitions },
|
44
|
+
distribution: distribution
|
39
45
|
)
|
40
46
|
end
|
41
47
|
|
@@ -52,6 +52,9 @@ module Karafka
|
|
52
52
|
|
53
53
|
# If end of the partition is reached, it always means all data is loaded
|
54
54
|
@state.loaded!
|
55
|
+
|
56
|
+
tags.add(:state, @state.to_s)
|
57
|
+
|
55
58
|
@states_reporter.call
|
56
59
|
end
|
57
60
|
|
@@ -64,7 +67,6 @@ module Karafka
|
|
64
67
|
return unless @state.loaded?
|
65
68
|
|
66
69
|
keys = []
|
67
|
-
epochs = []
|
68
70
|
|
69
71
|
# We first collect all the data for dispatch and then dispatch and **only** after
|
70
72
|
# dispatch that is sync is successful we remove those messages from the daily buffer
|
@@ -72,16 +74,13 @@ module Karafka
|
|
72
74
|
# with timeouts, etc, we need to be sure it wen through prior to deleting those messages
|
73
75
|
# from the daily buffer. That way we ensure the at least once delivery and in case of
|
74
76
|
# a transactional producer, exactly once delivery.
|
75
|
-
@daily_buffer.for_dispatch do |
|
76
|
-
epochs << epoch
|
77
|
+
@daily_buffer.for_dispatch do |message|
|
77
78
|
keys << message.key
|
78
79
|
@dispatcher << message
|
79
80
|
end
|
80
81
|
|
81
82
|
@dispatcher.flush
|
82
83
|
|
83
|
-
@max_epoch.update(epochs.max)
|
84
|
-
|
85
84
|
keys.each { |key| @daily_buffer.delete(key) }
|
86
85
|
|
87
86
|
@states_reporter.call
|
@@ -93,14 +92,6 @@ module Karafka
|
|
93
92
|
# accumulator and time related per-message operations.
|
94
93
|
# @param message [Karafka::Messages::Message]
|
95
94
|
def process_message(message)
|
96
|
-
# If we started to receive messages younger than the moment we created the consumer for
|
97
|
-
# the given day, it means we have loaded all the history and we are no longer in the
|
98
|
-
# loading phase.
|
99
|
-
if message.timestamp.to_i > @today.created_at
|
100
|
-
@state.loaded!
|
101
|
-
tags.add(:state, @state.to_s)
|
102
|
-
end
|
103
|
-
|
104
95
|
# If this is a schedule message we need to check if this is for today. Tombstone events
|
105
96
|
# are always considered immediate as they indicate, that a message with a given key
|
106
97
|
# was already dispatched or that user decided not to dispatch and cancelled the dispatch
|
@@ -120,6 +111,14 @@ module Karafka
|
|
120
111
|
end
|
121
112
|
end
|
122
113
|
|
114
|
+
# Tombstone events are only published after we have dispatched given message. This means
|
115
|
+
# that we've got that far in the dispatching time. This allows us (with a certain buffer)
|
116
|
+
# to quickly reject older messages (older in sense of being scheduled for previous times)
|
117
|
+
# instead of loading them into memory until they are expired
|
118
|
+
if message.headers['schedule_source_type'] == 'tombstone'
|
119
|
+
@max_epoch.update(message.headers['schedule_target_epoch'])
|
120
|
+
end
|
121
|
+
|
123
122
|
# Add to buffer all tombstones and messages for the same day
|
124
123
|
@daily_buffer << message
|
125
124
|
end
|
@@ -129,7 +128,7 @@ module Karafka
|
|
129
128
|
# If this is a new assignment we always need to seek from beginning to load the data
|
130
129
|
if @state.fresh?
|
131
130
|
clear!
|
132
|
-
seek(
|
131
|
+
seek(:earliest)
|
133
132
|
|
134
133
|
return true
|
135
134
|
end
|
@@ -140,7 +139,7 @@ module Karafka
|
|
140
139
|
# If day has ended we reload and start new day with new schedules
|
141
140
|
if @today.ended?
|
142
141
|
clear!
|
143
|
-
seek(
|
142
|
+
seek(:earliest)
|
144
143
|
|
145
144
|
return true
|
146
145
|
end
|
@@ -45,19 +45,22 @@ module Karafka
|
|
45
45
|
|
46
46
|
# Yields messages that should be dispatched (sent) to Kafka
|
47
47
|
#
|
48
|
-
# @yieldparam [
|
49
|
-
#
|
50
|
-
#
|
51
|
-
# @note We yield epoch alongside of the message so we do not have to extract it several
|
52
|
-
# times later on. This simplifies the API
|
48
|
+
# @yieldparam [Karafka::Messages::Message] messages to be dispatched sorted from the once
|
49
|
+
# that are the oldest (lowest epoch)
|
53
50
|
def for_dispatch
|
54
51
|
dispatch = Time.now.to_i
|
55
52
|
|
53
|
+
selected = []
|
54
|
+
|
56
55
|
@accu.each_value do |epoch, message|
|
57
56
|
next unless epoch <= dispatch
|
58
57
|
|
59
|
-
|
58
|
+
selected << [epoch, message]
|
60
59
|
end
|
60
|
+
|
61
|
+
selected
|
62
|
+
.sort_by!(&:first)
|
63
|
+
.each { |_, message| yield(message) }
|
61
64
|
end
|
62
65
|
|
63
66
|
# Removes given key from the accumulator
|
@@ -10,6 +10,12 @@ module Karafka
|
|
10
10
|
module Deserializers
|
11
11
|
# Converts certain pieces of headers into their integer form for messages
|
12
12
|
class Headers
|
13
|
+
# We only directly operate on epoch and other details for schedules and tombstones.
|
14
|
+
# cancel requests don't have to be deserialized that way since they don't have epoch
|
15
|
+
WORKABLE_TYPES = %w[schedule tombstone].freeze
|
16
|
+
|
17
|
+
private_constant :WORKABLE_TYPES
|
18
|
+
|
13
19
|
# @param metadata [Karafka::aMessages::Metadata]
|
14
20
|
# @return [Hash] headers
|
15
21
|
def call(metadata)
|
@@ -19,7 +25,7 @@ module Karafka
|
|
19
25
|
|
20
26
|
# tombstone and cancellation events are not operable, thus we do not have to cast any
|
21
27
|
# of the headers pieces
|
22
|
-
return raw_headers unless type
|
28
|
+
return raw_headers unless WORKABLE_TYPES.include?(type)
|
23
29
|
|
24
30
|
headers = raw_headers.dup
|
25
31
|
headers['schedule_target_epoch'] = headers['schedule_target_epoch'].to_i
|
@@ -10,22 +10,31 @@ module Karafka
|
|
10
10
|
# until which messages were dispatched by us. This allows us to quickly skip those messages
|
11
11
|
# during recovery, because we do know, they were dispatched.
|
12
12
|
class MaxEpoch
|
13
|
+
# We always give a bit of a buffer when using the max dispatch epoch because while we
|
14
|
+
# are dispatching messages, we could also later receive data for time close to our
|
15
|
+
# dispatch times. This is why when reloading days we give ourselves one hour of a window
|
16
|
+
# that we will keep until tombstones expire them. This prevents edge cases race-conditions
|
17
|
+
# when multiple scheduled events scheduled close to each other would bump epoch in such a
|
18
|
+
# way, that it would end up ignoring certain events.
|
19
|
+
GRACE_PERIOD = 60 * 60
|
20
|
+
|
21
|
+
private_constant :GRACE_PERIOD
|
22
|
+
|
23
|
+
# @return [Integer] max epoch recorded
|
24
|
+
attr_reader :to_i
|
25
|
+
|
13
26
|
def initialize
|
14
27
|
@max = -1
|
28
|
+
@to_i = @max
|
15
29
|
end
|
16
30
|
|
17
31
|
# Updates epoch if bigger than current max
|
18
32
|
# @param new_max [Integer] potential new max epoch
|
19
33
|
def update(new_max)
|
20
|
-
return unless new_max
|
21
34
|
return unless new_max > @max
|
22
35
|
|
23
36
|
@max = new_max
|
24
|
-
|
25
|
-
|
26
|
-
# @return [Integer] max epoch recorded
|
27
|
-
def to_i
|
28
|
-
@max
|
37
|
+
@to_i = @max - GRACE_PERIOD
|
29
38
|
end
|
30
39
|
end
|
31
40
|
end
|
@@ -53,6 +53,19 @@ module Karafka
|
|
53
53
|
def post_setup(config)
|
54
54
|
RecurringTasks::Contracts::Config.new.validate!(config.to_h)
|
55
55
|
end
|
56
|
+
|
57
|
+
# Basically since we may have custom producers configured that are not the same as the
|
58
|
+
# default one, we hold a reference to old pre-fork producer. This means, that when we
|
59
|
+
# initialize it again in post-fork, as long as user uses defaults we should re-inherit
|
60
|
+
# it from the default config.
|
61
|
+
#
|
62
|
+
# @param config [Karafka::Core::Configurable::Node]
|
63
|
+
# @param pre_fork_producer [WaterDrop::Producer]
|
64
|
+
def post_fork(config, pre_fork_producer)
|
65
|
+
return unless config.scheduled_messages.producer == pre_fork_producer
|
66
|
+
|
67
|
+
config.scheduled_messages.producer = config.producer
|
68
|
+
end
|
56
69
|
end
|
57
70
|
end
|
58
71
|
end
|