karafka 1.4.0 → 2.0.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (172) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/.github/workflows/ci.yml +89 -18
  4. data/.ruby-version +1 -1
  5. data/CHANGELOG.md +365 -1
  6. data/CONTRIBUTING.md +10 -19
  7. data/Gemfile +6 -0
  8. data/Gemfile.lock +56 -112
  9. data/LICENSE +17 -0
  10. data/LICENSE-COMM +89 -0
  11. data/LICENSE-LGPL +165 -0
  12. data/README.md +61 -68
  13. data/bin/benchmarks +85 -0
  14. data/bin/create_token +22 -0
  15. data/bin/integrations +272 -0
  16. data/bin/karafka +10 -0
  17. data/bin/scenario +29 -0
  18. data/bin/stress_many +13 -0
  19. data/bin/stress_one +13 -0
  20. data/certs/cert_chain.pem +26 -0
  21. data/certs/karafka-pro.pem +11 -0
  22. data/config/errors.yml +59 -38
  23. data/docker-compose.yml +10 -3
  24. data/karafka.gemspec +18 -21
  25. data/lib/active_job/karafka.rb +21 -0
  26. data/lib/active_job/queue_adapters/karafka_adapter.rb +26 -0
  27. data/lib/karafka/active_job/consumer.rb +26 -0
  28. data/lib/karafka/active_job/dispatcher.rb +38 -0
  29. data/lib/karafka/active_job/job_extensions.rb +34 -0
  30. data/lib/karafka/active_job/job_options_contract.rb +21 -0
  31. data/lib/karafka/active_job/routing/extensions.rb +33 -0
  32. data/lib/karafka/admin.rb +63 -0
  33. data/lib/karafka/app.rb +15 -20
  34. data/lib/karafka/base_consumer.rb +197 -31
  35. data/lib/karafka/cli/info.rb +44 -10
  36. data/lib/karafka/cli/install.rb +22 -12
  37. data/lib/karafka/cli/server.rb +17 -42
  38. data/lib/karafka/cli.rb +4 -3
  39. data/lib/karafka/connection/client.rb +379 -89
  40. data/lib/karafka/connection/listener.rb +250 -38
  41. data/lib/karafka/connection/listeners_batch.rb +24 -0
  42. data/lib/karafka/connection/messages_buffer.rb +84 -0
  43. data/lib/karafka/connection/pauses_manager.rb +46 -0
  44. data/lib/karafka/connection/raw_messages_buffer.rb +101 -0
  45. data/lib/karafka/connection/rebalance_manager.rb +78 -0
  46. data/lib/karafka/contracts/base.rb +17 -0
  47. data/lib/karafka/contracts/config.rb +88 -11
  48. data/lib/karafka/contracts/consumer_group.rb +21 -184
  49. data/lib/karafka/contracts/consumer_group_topic.rb +35 -11
  50. data/lib/karafka/contracts/server_cli_options.rb +19 -18
  51. data/lib/karafka/contracts.rb +1 -1
  52. data/lib/karafka/env.rb +46 -0
  53. data/lib/karafka/errors.rb +21 -21
  54. data/lib/karafka/helpers/async.rb +33 -0
  55. data/lib/karafka/helpers/colorize.rb +20 -0
  56. data/lib/karafka/helpers/multi_delegator.rb +2 -2
  57. data/lib/karafka/instrumentation/callbacks/error.rb +40 -0
  58. data/lib/karafka/instrumentation/callbacks/statistics.rb +41 -0
  59. data/lib/karafka/instrumentation/logger.rb +6 -10
  60. data/lib/karafka/instrumentation/logger_listener.rb +174 -0
  61. data/lib/karafka/instrumentation/monitor.rb +13 -61
  62. data/lib/karafka/instrumentation/notifications.rb +53 -0
  63. data/lib/karafka/instrumentation/proctitle_listener.rb +3 -3
  64. data/lib/karafka/instrumentation/vendors/datadog/dashboard.json +1 -0
  65. data/lib/karafka/instrumentation/vendors/datadog/listener.rb +232 -0
  66. data/lib/karafka/instrumentation.rb +21 -0
  67. data/lib/karafka/licenser.rb +75 -0
  68. data/lib/karafka/messages/batch_metadata.rb +45 -0
  69. data/lib/karafka/messages/builders/batch_metadata.rb +39 -0
  70. data/lib/karafka/messages/builders/message.rb +39 -0
  71. data/lib/karafka/messages/builders/messages.rb +34 -0
  72. data/lib/karafka/{params/params.rb → messages/message.rb} +7 -12
  73. data/lib/karafka/messages/messages.rb +64 -0
  74. data/lib/karafka/{params → messages}/metadata.rb +4 -6
  75. data/lib/karafka/messages/seek.rb +9 -0
  76. data/lib/karafka/patches/rdkafka/consumer.rb +22 -0
  77. data/lib/karafka/pro/active_job/consumer.rb +46 -0
  78. data/lib/karafka/pro/active_job/dispatcher.rb +61 -0
  79. data/lib/karafka/pro/active_job/job_options_contract.rb +32 -0
  80. data/lib/karafka/pro/base_consumer.rb +107 -0
  81. data/lib/karafka/pro/contracts/base.rb +21 -0
  82. data/lib/karafka/pro/contracts/consumer_group.rb +34 -0
  83. data/lib/karafka/pro/contracts/consumer_group_topic.rb +69 -0
  84. data/lib/karafka/pro/loader.rb +76 -0
  85. data/lib/karafka/pro/performance_tracker.rb +80 -0
  86. data/lib/karafka/pro/processing/coordinator.rb +85 -0
  87. data/lib/karafka/pro/processing/jobs/consume_non_blocking.rb +38 -0
  88. data/lib/karafka/pro/processing/jobs_builder.rb +32 -0
  89. data/lib/karafka/pro/processing/partitioner.rb +58 -0
  90. data/lib/karafka/pro/processing/scheduler.rb +56 -0
  91. data/lib/karafka/pro/routing/builder_extensions.rb +30 -0
  92. data/lib/karafka/pro/routing/topic_extensions.rb +74 -0
  93. data/lib/karafka/pro.rb +13 -0
  94. data/lib/karafka/process.rb +1 -0
  95. data/lib/karafka/processing/coordinator.rb +103 -0
  96. data/lib/karafka/processing/coordinators_buffer.rb +54 -0
  97. data/lib/karafka/processing/executor.rb +126 -0
  98. data/lib/karafka/processing/executors_buffer.rb +88 -0
  99. data/lib/karafka/processing/jobs/base.rb +55 -0
  100. data/lib/karafka/processing/jobs/consume.rb +47 -0
  101. data/lib/karafka/processing/jobs/revoked.rb +22 -0
  102. data/lib/karafka/processing/jobs/shutdown.rb +23 -0
  103. data/lib/karafka/processing/jobs_builder.rb +29 -0
  104. data/lib/karafka/processing/jobs_queue.rb +144 -0
  105. data/lib/karafka/processing/partitioner.rb +22 -0
  106. data/lib/karafka/processing/result.rb +37 -0
  107. data/lib/karafka/processing/scheduler.rb +22 -0
  108. data/lib/karafka/processing/worker.rb +91 -0
  109. data/lib/karafka/processing/workers_batch.rb +27 -0
  110. data/lib/karafka/railtie.rb +127 -0
  111. data/lib/karafka/routing/builder.rb +26 -23
  112. data/lib/karafka/routing/consumer_group.rb +37 -17
  113. data/lib/karafka/routing/consumer_mapper.rb +1 -2
  114. data/lib/karafka/routing/proxy.rb +9 -16
  115. data/lib/karafka/routing/router.rb +1 -1
  116. data/lib/karafka/routing/subscription_group.rb +53 -0
  117. data/lib/karafka/routing/subscription_groups_builder.rb +54 -0
  118. data/lib/karafka/routing/topic.rb +65 -24
  119. data/lib/karafka/routing/topics.rb +38 -0
  120. data/lib/karafka/runner.rb +51 -0
  121. data/lib/karafka/serialization/json/deserializer.rb +6 -15
  122. data/lib/karafka/server.rb +67 -26
  123. data/lib/karafka/setup/config.rb +153 -175
  124. data/lib/karafka/status.rb +14 -5
  125. data/lib/karafka/templates/example_consumer.rb.erb +16 -0
  126. data/lib/karafka/templates/karafka.rb.erb +17 -55
  127. data/lib/karafka/time_trackers/base.rb +19 -0
  128. data/lib/karafka/time_trackers/pause.rb +92 -0
  129. data/lib/karafka/time_trackers/poll.rb +65 -0
  130. data/lib/karafka/version.rb +1 -1
  131. data/lib/karafka.rb +46 -16
  132. data.tar.gz.sig +0 -0
  133. metadata +145 -171
  134. metadata.gz.sig +0 -0
  135. data/.github/FUNDING.yml +0 -3
  136. data/MIT-LICENCE +0 -18
  137. data/certs/mensfeld.pem +0 -25
  138. data/lib/karafka/attributes_map.rb +0 -62
  139. data/lib/karafka/backends/inline.rb +0 -16
  140. data/lib/karafka/base_responder.rb +0 -226
  141. data/lib/karafka/cli/flow.rb +0 -48
  142. data/lib/karafka/code_reloader.rb +0 -67
  143. data/lib/karafka/connection/api_adapter.rb +0 -161
  144. data/lib/karafka/connection/batch_delegator.rb +0 -55
  145. data/lib/karafka/connection/builder.rb +0 -18
  146. data/lib/karafka/connection/message_delegator.rb +0 -36
  147. data/lib/karafka/consumers/batch_metadata.rb +0 -10
  148. data/lib/karafka/consumers/callbacks.rb +0 -71
  149. data/lib/karafka/consumers/includer.rb +0 -64
  150. data/lib/karafka/consumers/responders.rb +0 -24
  151. data/lib/karafka/consumers/single_params.rb +0 -15
  152. data/lib/karafka/contracts/responder_usage.rb +0 -54
  153. data/lib/karafka/fetcher.rb +0 -42
  154. data/lib/karafka/helpers/class_matcher.rb +0 -88
  155. data/lib/karafka/helpers/config_retriever.rb +0 -46
  156. data/lib/karafka/helpers/inflector.rb +0 -26
  157. data/lib/karafka/instrumentation/stdout_listener.rb +0 -140
  158. data/lib/karafka/params/batch_metadata.rb +0 -26
  159. data/lib/karafka/params/builders/batch_metadata.rb +0 -30
  160. data/lib/karafka/params/builders/params.rb +0 -38
  161. data/lib/karafka/params/builders/params_batch.rb +0 -25
  162. data/lib/karafka/params/params_batch.rb +0 -60
  163. data/lib/karafka/patches/ruby_kafka.rb +0 -47
  164. data/lib/karafka/persistence/client.rb +0 -29
  165. data/lib/karafka/persistence/consumers.rb +0 -45
  166. data/lib/karafka/persistence/topics.rb +0 -48
  167. data/lib/karafka/responders/builder.rb +0 -36
  168. data/lib/karafka/responders/topic.rb +0 -55
  169. data/lib/karafka/routing/topic_mapper.rb +0 -53
  170. data/lib/karafka/serialization/json/serializer.rb +0 -31
  171. data/lib/karafka/setup/configurators/water_drop.rb +0 -36
  172. data/lib/karafka/templates/application_responder.rb.erb +0 -11
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Karafka
4
+ module Processing
5
+ module Jobs
6
+ # The main job type. It runs the executor that triggers given topic partition messages
7
+ # processing in an underlying consumer instance.
8
+ class Consume < Base
9
+ # @return [Array<Rdkafka::Consumer::Message>] array with messages
10
+ attr_reader :messages
11
+
12
+ # @param executor [Karafka::Processing::Executor] executor that is suppose to run a given
13
+ # job
14
+ # @param messages [Karafka::Messages::Messages] karafka messages batch
15
+ # @param coordinator [Karafka::Processing::Coordinator] processing coordinator
16
+ # @return [Consume]
17
+ def initialize(executor, messages, coordinator)
18
+ @executor = executor
19
+ @messages = messages
20
+ @coordinator = coordinator
21
+ super()
22
+ end
23
+
24
+ # Runs all the preparation code on the executor that needs to happen before the job is
25
+ # enqueued.
26
+ def before_enqueue
27
+ executor.before_enqueue(@messages, @coordinator)
28
+ end
29
+
30
+ # Runs the before consumption preparations on the executor
31
+ def before_call
32
+ executor.before_consume
33
+ end
34
+
35
+ # Runs the given executor
36
+ def call
37
+ executor.consume
38
+ end
39
+
40
+ # Runs any error handling and other post-consumption stuff on the executor
41
+ def after_call
42
+ executor.after_consume
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Karafka
4
+ module Processing
5
+ module Jobs
6
+ # Job that runs the revoked operation when we loose a partition on a consumer that lost it.
7
+ class Revoked < Base
8
+ # @param executor [Karafka::Processing::Executor] executor that is suppose to run the job
9
+ # @return [Revoked]
10
+ def initialize(executor)
11
+ @executor = executor
12
+ super()
13
+ end
14
+
15
+ # Runs the revoking job via an executor.
16
+ def call
17
+ executor.revoked
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Karafka
4
+ module Processing
5
+ module Jobs
6
+ # Job that runs on each active consumer upon process shutdown (one job per consumer).
7
+ class Shutdown < Base
8
+ # @param executor [Karafka::Processing::Executor] executor that is suppose to run a given
9
+ # job on an active consumer
10
+ # @return [Shutdown]
11
+ def initialize(executor)
12
+ @executor = executor
13
+ super()
14
+ end
15
+
16
+ # Runs the shutdown job via an executor.
17
+ def call
18
+ executor.shutdown
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Karafka
4
+ module Processing
5
+ # Class responsible for deciding what type of job should we build to run a given command and
6
+ # for building a proper job for it.
7
+ class JobsBuilder
8
+ # @param executor [Karafka::Processing::Executor]
9
+ # @param messages [Karafka::Messages::Messages] messages batch to be consumed
10
+ # @param coordinator [Karafka::Processing::Coordinator]
11
+ # @return [Karafka::Processing::Jobs::Consume] consumption job
12
+ def consume(executor, messages, coordinator)
13
+ Jobs::Consume.new(executor, messages, coordinator)
14
+ end
15
+
16
+ # @param executor [Karafka::Processing::Executor]
17
+ # @return [Karafka::Processing::Jobs::Revoked] revocation job
18
+ def revoked(executor)
19
+ Jobs::Revoked.new(executor)
20
+ end
21
+
22
+ # @param executor [Karafka::Processing::Executor]
23
+ # @return [Karafka::Processing::Jobs::Shutdown] shutdown job
24
+ def shutdown(executor)
25
+ Jobs::Shutdown.new(executor)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,144 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Karafka
4
+ module Processing
5
+ # This is the key work component for Karafka jobs distribution. It provides API for running
6
+ # jobs in parallel while operating within more than one subscription group.
7
+ #
8
+ # We need to take into consideration fact, that more than one subscription group can operate
9
+ # on this queue, that's why internally we keep track of processing per group.
10
+ #
11
+ # We work with the assumption, that partitions data is evenly distributed.
12
+ class JobsQueue
13
+ # @return [Karafka::Processing::JobsQueue]
14
+ def initialize
15
+ @queue = Queue.new
16
+ # Those queues will act as semaphores internally. Since we need an indicator for waiting
17
+ # we could use Thread.pass but this is expensive. Instead we can just lock until any
18
+ # of the workers finishes their work and we can re-check. This means that in the worse
19
+ # scenario, we will context switch 10 times per poll instead of getting this thread
20
+ # scheduled by Ruby hundreds of thousands of times per group.
21
+ # We cannot use a single semaphore as it could potentially block in listeners that should
22
+ # process with their data and also could unlock when a given group needs to remain locked
23
+ @semaphores = Hash.new { |h, k| h[k] = Queue.new }
24
+ @in_processing = Hash.new { |h, k| h[k] = [] }
25
+ @mutex = Mutex.new
26
+ end
27
+
28
+ # Returns number of jobs that are either enqueued or in processing (but not finished)
29
+ # @return [Integer] number of elements in the queue
30
+ # @note Using `#pop` won't decrease this number as only marking job as completed does this
31
+ def size
32
+ @in_processing.values.map(&:size).sum
33
+ end
34
+
35
+ # Adds the job to the internal main queue, scheduling it for execution in a worker and marks
36
+ # this job as in processing pipeline.
37
+ #
38
+ # @param job [Jobs::Base] job that we want to run
39
+ def <<(job)
40
+ # We do not push the job if the queue is closed as it means that it would anyhow not be
41
+ # executed
42
+ return if @queue.closed?
43
+
44
+ @mutex.synchronize do
45
+ group = @in_processing[job.group_id]
46
+
47
+ raise(Errors::JobsQueueSynchronizationError, job.group_id) if group.include?(job)
48
+
49
+ group << job
50
+ end
51
+
52
+ @queue << job
53
+ end
54
+
55
+ # @return [Jobs::Base, nil] waits for a job from the main queue and returns it once available
56
+ # or returns nil if the queue has been stopped and there won't be anything more to process
57
+ # ever.
58
+ # @note This command is blocking and will wait until any job is available on the main queue
59
+ def pop
60
+ @queue.pop
61
+ end
62
+
63
+ # Causes the wait lock to re-check the lock conditions and potential unlock.
64
+ # @param group_id [String] id of the group we want to unlock for one tick
65
+ # @note This does not release the wait lock. It just causes a conditions recheck
66
+ def tick(group_id)
67
+ @semaphores[group_id] << true
68
+ end
69
+
70
+ # Marks a given job from a given group as completed. When there are no more jobs from a given
71
+ # group to be executed, we won't wait.
72
+ #
73
+ # @param [Jobs::Base] job that was completed
74
+ def complete(job)
75
+ @mutex.synchronize do
76
+ @in_processing[job.group_id].delete(job)
77
+ tick(job.group_id)
78
+ end
79
+ end
80
+
81
+ # Clears the processing states for a provided group. Useful when a recovery happens and we
82
+ # need to clean up state but only for a given subscription group.
83
+ #
84
+ # @param group_id [String]
85
+ def clear(group_id)
86
+ @mutex.synchronize do
87
+ @in_processing[group_id].clear
88
+ # We unlock it just in case it was blocked when clearing started
89
+ tick(group_id)
90
+ end
91
+ end
92
+
93
+ # Stops the whole processing queue.
94
+ def close
95
+ @mutex.synchronize do
96
+ return if @queue.closed?
97
+
98
+ @queue.close
99
+ @semaphores.values.each(&:close)
100
+ end
101
+ end
102
+
103
+ # @param group_id [String]
104
+ #
105
+ # @return [Boolean] tell us if we have anything in the processing (or for processing) from
106
+ # a given group.
107
+ def empty?(group_id)
108
+ @in_processing[group_id].empty?
109
+ end
110
+
111
+ # Blocks when there are things in the queue in a given group and waits until all the blocking
112
+ # jobs from a given group are completed
113
+ #
114
+ # @param group_id [String] id of the group in which jobs we're interested.
115
+ # @note This method is blocking.
116
+ def wait(group_id)
117
+ # Go doing other things while we cannot process and wait for anyone to finish their work
118
+ # and re-check the wait status
119
+ @semaphores[group_id].pop while wait?(group_id)
120
+ end
121
+
122
+ # - `processing` - number of jobs that are currently being processed (active work)
123
+ # - `enqueued` - number of jobs in the queue that are waiting to be picked up by a worker
124
+ #
125
+ # @return [Hash] hash with basic usage statistics of this queue.
126
+ def statistics
127
+ {
128
+ processing: size - @queue.size,
129
+ enqueued: @queue.size
130
+ }.freeze
131
+ end
132
+
133
+ private
134
+
135
+ # @param group_id [String] id of the group in which jobs we're interested.
136
+ # @return [Boolean] should we keep waiting or not
137
+ # @note We do not wait for non-blocking jobs. Their flow should allow for `poll` running
138
+ # as they may exceed `max.poll.interval`
139
+ def wait?(group_id)
140
+ !@in_processing[group_id].all?(&:non_blocking?)
141
+ end
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Karafka
4
+ module Processing
5
+ # Basic partitioner for work division
6
+ # It does not divide any work.
7
+ class Partitioner
8
+ # @param subscription_group [Karafka::Routing::SubscriptionGroup] subscription group
9
+ def initialize(subscription_group)
10
+ @subscription_group = subscription_group
11
+ end
12
+
13
+ # @param _topic [String] topic name
14
+ # @param messages [Array<Karafka::Messages::Message>] karafka messages
15
+ # @yieldparam [Integer] group id
16
+ # @yieldparam [Array<Karafka::Messages::Message>] karafka messages
17
+ def call(_topic, messages)
18
+ yield(0, messages)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Karafka
4
+ module Processing
5
+ # A simple object that allows us to keep track of processing state.
6
+ # It allows to indicate if given thing moved from success to a failure or the other way around
7
+ # Useful for tracking consumption state
8
+ class Result
9
+ attr_reader :cause
10
+
11
+ def initialize
12
+ @success = true
13
+ @cause = false
14
+ end
15
+
16
+ # @return [Boolean]
17
+ def success?
18
+ @success
19
+ end
20
+
21
+ # Marks state as successful
22
+ def success!
23
+ @success = true
24
+ # We set cause to false so the previous error that occurred does not leak when error is
25
+ # no longer present
26
+ @cause = false
27
+ end
28
+
29
+ # Marks state as failure
30
+ # @param cause [StandardError] error that occurred and caused failure
31
+ def failure!(cause)
32
+ @success = false
33
+ @cause = cause
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Karafka
4
+ module Processing
5
+ # FIFO scheduler for messages coming from various topics and partitions
6
+ class Scheduler
7
+ # Schedules jobs in the fifo order
8
+ #
9
+ # @param queue [Karafka::Processing::JobsQueue] queue where we want to put the jobs
10
+ # @param jobs_array [Array<Karafka::Processing::Jobs::Base>] jobs we want to schedule
11
+ def schedule_consumption(queue, jobs_array)
12
+ jobs_array.each do |job|
13
+ queue << job
14
+ end
15
+ end
16
+
17
+ # Both revocation and shutdown jobs can also run in fifo by default
18
+ alias schedule_revocation schedule_consumption
19
+ alias schedule_shutdown schedule_consumption
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Karafka
4
+ module Processing
5
+ # Workers are used to run jobs in separate threads.
6
+ # Workers are the main processing units of the Karafka framework.
7
+ #
8
+ # Each job runs in three stages:
9
+ # - prepare - here we can run any code that we would need to run blocking before we allow
10
+ # the job to run fully async (non blocking). This will always run in a blocking
11
+ # way and can be used to make sure all the resources and external dependencies
12
+ # are satisfied before going async.
13
+ #
14
+ # - call - actual processing logic that can run sync or async
15
+ #
16
+ # - teardown - it should include any code that we want to run after we executed the user
17
+ # code. This can be used to unlock certain resources or do other things that are
18
+ # not user code but need to run after user code base is executed.
19
+ class Worker
20
+ include Helpers::Async
21
+
22
+ # @return [String] id of this worker
23
+ attr_reader :id
24
+
25
+ # @param jobs_queue [JobsQueue]
26
+ # @return [Worker]
27
+ def initialize(jobs_queue)
28
+ @id = SecureRandom.uuid
29
+ @jobs_queue = jobs_queue
30
+ end
31
+
32
+ private
33
+
34
+ # Runs processing of jobs in a loop
35
+ # Stops when queue is closed.
36
+ def call
37
+ loop { break unless process }
38
+ end
39
+
40
+ # Fetches a single job, processes it and marks as completed.
41
+ #
42
+ # @note We do not have error handling here, as no errors should propagate this far. If they
43
+ # do, it is a critical error and should bubble up.
44
+ #
45
+ # @note Upon closing the jobs queue, worker will close it's thread
46
+ def process
47
+ job = @jobs_queue.pop
48
+
49
+ instrument_details = { caller: self, job: job, jobs_queue: @jobs_queue }
50
+
51
+ if job
52
+ Karafka.monitor.instrument('worker.process', instrument_details)
53
+
54
+ Karafka.monitor.instrument('worker.processed', instrument_details) do
55
+ job.before_call
56
+
57
+ # If a job is marked as non blocking, we can run a tick in the job queue and if there
58
+ # are no other blocking factors, the job queue will be unlocked.
59
+ # If this does not run, all the things will be blocking and job queue won't allow to
60
+ # pass it until done.
61
+ @jobs_queue.tick(job.group_id) if job.non_blocking?
62
+
63
+ job.call
64
+
65
+ job.after_call
66
+
67
+ true
68
+ end
69
+ else
70
+ false
71
+ end
72
+ # We signal critical exceptions, notify and do not allow worker to fail
73
+ # rubocop:disable Lint/RescueException
74
+ rescue Exception => e
75
+ # rubocop:enable Lint/RescueException
76
+ Karafka.monitor.instrument(
77
+ 'error.occurred',
78
+ caller: self,
79
+ error: e,
80
+ type: 'worker.process.error'
81
+ )
82
+ ensure
83
+ # job can be nil when the queue is being closed
84
+ @jobs_queue.complete(job) if job
85
+
86
+ # Always publish info, that we completed all the work despite its result
87
+ Karafka.monitor.instrument('worker.completed', instrument_details)
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Karafka
4
+ module Processing
5
+ # Abstraction layer around workers batch.
6
+ class WorkersBatch
7
+ include Enumerable
8
+
9
+ # @param jobs_queue [JobsQueue]
10
+ # @return [WorkersBatch]
11
+ def initialize(jobs_queue)
12
+ @batch = Array.new(App.config.concurrency) { Processing::Worker.new(jobs_queue) }
13
+ end
14
+
15
+ # Iterates over available workers and yields each worker
16
+ # @param block [Proc] block we want to run
17
+ def each(&block)
18
+ @batch.each(&block)
19
+ end
20
+
21
+ # @return [Integer] number of workers in the batch
22
+ def size
23
+ @batch.size
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,127 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file contains Railtie for auto-configuration
4
+
5
+ rails = false
6
+
7
+ begin
8
+ require 'rails'
9
+
10
+ rails = true
11
+ rescue LoadError
12
+ # Without defining this in any way, Zeitwerk ain't happy so we do it that way
13
+ module Karafka
14
+ class Railtie
15
+ end
16
+ end
17
+ end
18
+
19
+ if rails
20
+ # Load Karafka
21
+ require 'karafka'
22
+
23
+ # Load ActiveJob adapter
24
+ require 'active_job/karafka'
25
+
26
+ # Setup env if configured (may be configured later by .net, etc)
27
+ ENV['KARAFKA_ENV'] ||= ENV['RAILS_ENV'] if ENV.key?('RAILS_ENV')
28
+
29
+ module Karafka
30
+ # Railtie for setting up Rails integration
31
+ class Railtie < Rails::Railtie
32
+ railtie_name :karafka
33
+
34
+ initializer 'karafka.active_job_integration' do
35
+ ActiveSupport.on_load(:active_job) do
36
+ # Extend ActiveJob with some Karafka specific ActiveJob magic
37
+ extend ::Karafka::ActiveJob::JobExtensions
38
+ end
39
+ end
40
+
41
+ # This lines will make Karafka print to stdout like puma or unicorn when we run karafka
42
+ # server + will support code reloading with each fetched loop. We do it only for karafka
43
+ # based commands as Rails processes and console will have it enabled already
44
+ initializer 'karafka.configure_rails_logger' do
45
+ # Make Karafka use Rails logger
46
+ ::Karafka::App.config.logger = Rails.logger
47
+
48
+ next unless Rails.env.development?
49
+ next unless ENV.key?('KARAFKA_CLI')
50
+
51
+ logger = ActiveSupport::Logger.new($stdout)
52
+ # Inherit the logger level from Rails, otherwise would always run with the debug level
53
+ logger.level = Rails.logger.level
54
+
55
+ Rails.logger.extend(
56
+ ActiveSupport::Logger.broadcast(
57
+ logger
58
+ )
59
+ )
60
+ end
61
+
62
+ initializer 'karafka.configure_rails_auto_load_paths' do |app|
63
+ # Consumers should autoload by default in the Rails app so they are visible
64
+ app.config.autoload_paths += %w[app/consumers]
65
+ end
66
+
67
+ initializer 'karafka.configure_rails_code_reloader' do
68
+ # There are components that won't work with older Rails version, so we check it and
69
+ # provide a failover
70
+ rails6plus = Rails.gem_version >= Gem::Version.new('6.0.0')
71
+
72
+ next unless Rails.env.development?
73
+ next unless ENV.key?('KARAFKA_CLI')
74
+ next unless rails6plus
75
+
76
+ # We can have many listeners, but it does not matter in which we will reload the code
77
+ # as long as all the consumers will be re-created as Rails reload is thread-safe
78
+ ::Karafka::App.monitor.subscribe('connection.listener.fetch_loop') do
79
+ # Reload code each time there is a change in the code
80
+ next unless Rails.application.reloaders.any?(&:updated?)
81
+
82
+ Rails.application.reloader.reload!
83
+ end
84
+
85
+ ::Karafka::App.monitor.subscribe('worker.completed') do
86
+ # Skip in case someone is using Rails without ActiveRecord
87
+ next unless Object.const_defined?('ActiveRecord::Base')
88
+
89
+ # Always release the connection after processing is done. Otherwise thread may hang
90
+ # blocking the reload and further processing
91
+ # @see https://github.com/rails/rails/issues/44183
92
+ ActiveRecord::Base.connection_pool.release_connection
93
+ end
94
+ end
95
+
96
+ initializer 'karafka.require_karafka_boot_file' do |app|
97
+ rails6plus = Rails.gem_version >= Gem::Version.new('6.0.0')
98
+
99
+ # If the boot file location is set to "false", we should not raise an exception and we
100
+ # should just not load karafka stuff. Setting this explicitly to false indicates, that
101
+ # karafka is part of the supply chain but it is not a first class citizen of a given
102
+ # system (may be just a dependency of a dependency), thus railtie should not kick in to
103
+ # load the non-existing boot file
104
+ next if Karafka.boot_file.to_s == 'false'
105
+
106
+ karafka_boot_file = Rails.root.join(Karafka.boot_file.to_s).to_s
107
+
108
+ # Provide more comprehensive error for when no boot file
109
+ unless File.exist?(karafka_boot_file)
110
+ raise(Karafka::Errors::MissingBootFileError, karafka_boot_file)
111
+ end
112
+
113
+ if rails6plus
114
+ app.reloader.to_prepare do
115
+ # Load Karafka boot file, so it can be used in Rails server context
116
+ require karafka_boot_file
117
+ end
118
+ else
119
+ # Load Karafka main setup for older Rails versions
120
+ app.config.after_initialize do
121
+ require karafka_boot_file
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
@@ -10,13 +10,9 @@ module Karafka
10
10
  # end
11
11
  # end
12
12
  class Builder < Concurrent::Array
13
- # Consumer group consistency checking contract
14
- CONTRACT = Karafka::Contracts::ConsumerGroup.new.freeze
15
-
16
- private_constant :CONTRACT
17
-
18
13
  def initialize
19
14
  @draws = Concurrent::Array.new
15
+ super
20
16
  end
21
17
 
22
18
  # Used to draw routes for Karafka
@@ -37,11 +33,7 @@ module Karafka
37
33
  instance_eval(&block)
38
34
 
39
35
  each do |consumer_group|
40
- hashed_group = consumer_group.to_h
41
- validation_result = CONTRACT.call(hashed_group)
42
- next if validation_result.success?
43
-
44
- raise Errors::InvalidConfigurationError, validation_result.errors.to_h
36
+ Contracts::ConsumerGroup.new.validate!(consumer_group.to_h)
45
37
  end
46
38
  end
47
39
 
@@ -58,30 +50,41 @@ module Karafka
58
50
  super
59
51
  end
60
52
 
61
- # Redraws all the routes for the in-process code reloading.
62
- # @note This won't allow registration of new topics without process restart but will trigger
63
- # cache invalidation so all the classes, etc are re-fetched after code reload
64
- def reload
65
- draws = @draws.dup
66
- clear
67
- draws.each { |block| draw(&block) }
68
- end
69
-
70
53
  private
71
54
 
72
55
  # Builds and saves given consumer group
73
56
  # @param group_id [String, Symbol] name for consumer group
74
57
  # @param block [Proc] proc that should be executed in the proxy context
75
58
  def consumer_group(group_id, &block)
76
- consumer_group = ConsumerGroup.new(group_id.to_s)
77
- self << Proxy.new(consumer_group, &block).target
59
+ consumer_group = find { |cg| cg.name == group_id.to_s }
60
+
61
+ if consumer_group
62
+ Proxy.new(consumer_group, &block).target
63
+ else
64
+ consumer_group = ConsumerGroup.new(group_id.to_s)
65
+ self << Proxy.new(consumer_group, &block).target
66
+ end
67
+ end
68
+
69
+ # Handles the simple routing case where we create one consumer group and allow for further
70
+ # subscription group customization
71
+ # @param subscription_group_name [String, Symbol] subscription group id. When not provided,
72
+ # a random uuid will be used
73
+ # @param block [Proc] further topics definitions
74
+ def subscription_group(subscription_group_name = SecureRandom.uuid, &block)
75
+ consumer_group('app') do
76
+ target.public_send(:subscription_group=, subscription_group_name, &block)
77
+ end
78
78
  end
79
79
 
80
+ # In case we use simple style of routing, all topics will be assigned to the same consumer
81
+ # group that will be based on the client_id
82
+ #
80
83
  # @param topic_name [String, Symbol] name of a topic from which we want to consumer
81
84
  # @param block [Proc] proc we want to evaluate in the topic context
82
85
  def topic(topic_name, &block)
83
- consumer_group(topic_name) do
84
- topic(topic_name, &block).tap(&:build)
86
+ consumer_group('app') do
87
+ topic(topic_name, &block)
85
88
  end
86
89
  end
87
90
  end