karafka 1.4.4 → 2.1.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (315) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/.github/FUNDING.yml +1 -3
  4. data/.github/workflows/ci.yml +117 -36
  5. data/.rspec +4 -0
  6. data/.ruby-version +1 -1
  7. data/CHANGELOG.md +611 -578
  8. data/CONTRIBUTING.md +10 -19
  9. data/Gemfile +7 -0
  10. data/Gemfile.lock +59 -100
  11. data/LICENSE +17 -0
  12. data/LICENSE-COMM +89 -0
  13. data/LICENSE-LGPL +165 -0
  14. data/README.md +64 -66
  15. data/bin/benchmarks +85 -0
  16. data/bin/create_token +22 -0
  17. data/bin/integrations +297 -0
  18. data/bin/karafka +4 -12
  19. data/bin/rspecs +6 -0
  20. data/bin/scenario +29 -0
  21. data/bin/stress_many +13 -0
  22. data/bin/stress_one +13 -0
  23. data/bin/verify_license_integrity +37 -0
  24. data/certs/cert_chain.pem +26 -0
  25. data/certs/karafka-pro.pem +11 -0
  26. data/config/locales/errors.yml +84 -0
  27. data/config/locales/pro_errors.yml +39 -0
  28. data/docker-compose.yml +13 -3
  29. data/karafka.gemspec +27 -22
  30. data/lib/active_job/karafka.rb +17 -0
  31. data/lib/active_job/queue_adapters/karafka_adapter.rb +32 -0
  32. data/lib/karafka/active_job/consumer.rb +49 -0
  33. data/lib/karafka/active_job/current_attributes/loading.rb +36 -0
  34. data/lib/karafka/active_job/current_attributes/persistence.rb +28 -0
  35. data/lib/karafka/active_job/current_attributes.rb +42 -0
  36. data/lib/karafka/active_job/dispatcher.rb +69 -0
  37. data/lib/karafka/active_job/job_extensions.rb +34 -0
  38. data/lib/karafka/active_job/job_options_contract.rb +32 -0
  39. data/lib/karafka/admin.rb +286 -0
  40. data/lib/karafka/app.rb +47 -23
  41. data/lib/karafka/base_consumer.rb +247 -29
  42. data/lib/karafka/cli/base.rb +24 -4
  43. data/lib/karafka/cli/console.rb +13 -8
  44. data/lib/karafka/cli/info.rb +45 -10
  45. data/lib/karafka/cli/install.rb +22 -12
  46. data/lib/karafka/cli/server.rb +63 -41
  47. data/lib/karafka/cli/topics.rb +146 -0
  48. data/lib/karafka/cli.rb +4 -11
  49. data/lib/karafka/connection/client.rb +502 -89
  50. data/lib/karafka/connection/consumer_group_coordinator.rb +48 -0
  51. data/lib/karafka/connection/listener.rb +294 -38
  52. data/lib/karafka/connection/listeners_batch.rb +40 -0
  53. data/lib/karafka/connection/messages_buffer.rb +84 -0
  54. data/lib/karafka/connection/pauses_manager.rb +46 -0
  55. data/lib/karafka/connection/proxy.rb +92 -0
  56. data/lib/karafka/connection/raw_messages_buffer.rb +101 -0
  57. data/lib/karafka/connection/rebalance_manager.rb +90 -0
  58. data/lib/karafka/contracts/base.rb +17 -0
  59. data/lib/karafka/contracts/config.rb +88 -11
  60. data/lib/karafka/contracts/consumer_group.rb +32 -187
  61. data/lib/karafka/contracts/server_cli_options.rb +80 -19
  62. data/lib/karafka/contracts/topic.rb +65 -0
  63. data/lib/karafka/contracts.rb +1 -1
  64. data/lib/karafka/embedded.rb +36 -0
  65. data/lib/karafka/env.rb +46 -0
  66. data/lib/karafka/errors.rb +26 -21
  67. data/lib/karafka/helpers/async.rb +33 -0
  68. data/lib/karafka/helpers/colorize.rb +26 -0
  69. data/lib/karafka/helpers/multi_delegator.rb +2 -2
  70. data/lib/karafka/instrumentation/callbacks/error.rb +39 -0
  71. data/lib/karafka/instrumentation/callbacks/statistics.rb +51 -0
  72. data/lib/karafka/instrumentation/logger.rb +5 -9
  73. data/lib/karafka/instrumentation/logger_listener.rb +299 -0
  74. data/lib/karafka/instrumentation/monitor.rb +13 -61
  75. data/lib/karafka/instrumentation/notifications.rb +75 -0
  76. data/lib/karafka/instrumentation/proctitle_listener.rb +7 -16
  77. data/lib/karafka/instrumentation/vendors/datadog/dashboard.json +1 -0
  78. data/lib/karafka/instrumentation/vendors/datadog/logger_listener.rb +153 -0
  79. data/lib/karafka/instrumentation/vendors/datadog/metrics_listener.rb +264 -0
  80. data/lib/karafka/instrumentation/vendors/kubernetes/liveness_listener.rb +176 -0
  81. data/lib/karafka/licenser.rb +78 -0
  82. data/lib/karafka/messages/batch_metadata.rb +52 -0
  83. data/lib/karafka/messages/builders/batch_metadata.rb +40 -0
  84. data/lib/karafka/messages/builders/message.rb +36 -0
  85. data/lib/karafka/messages/builders/messages.rb +36 -0
  86. data/lib/karafka/{params/params.rb → messages/message.rb} +20 -13
  87. data/lib/karafka/messages/messages.rb +71 -0
  88. data/lib/karafka/{params → messages}/metadata.rb +4 -6
  89. data/lib/karafka/messages/parser.rb +14 -0
  90. data/lib/karafka/messages/seek.rb +12 -0
  91. data/lib/karafka/patches/rdkafka/bindings.rb +139 -0
  92. data/lib/karafka/pro/active_job/consumer.rb +47 -0
  93. data/lib/karafka/pro/active_job/dispatcher.rb +86 -0
  94. data/lib/karafka/pro/active_job/job_options_contract.rb +45 -0
  95. data/lib/karafka/pro/encryption/cipher.rb +58 -0
  96. data/lib/karafka/pro/encryption/contracts/config.rb +79 -0
  97. data/lib/karafka/pro/encryption/errors.rb +24 -0
  98. data/lib/karafka/pro/encryption/messages/middleware.rb +46 -0
  99. data/lib/karafka/pro/encryption/messages/parser.rb +56 -0
  100. data/lib/karafka/pro/encryption/setup/config.rb +48 -0
  101. data/lib/karafka/pro/encryption.rb +47 -0
  102. data/lib/karafka/pro/iterator/expander.rb +95 -0
  103. data/lib/karafka/pro/iterator/tpl_builder.rb +155 -0
  104. data/lib/karafka/pro/iterator.rb +170 -0
  105. data/lib/karafka/pro/loader.rb +102 -0
  106. data/lib/karafka/pro/performance_tracker.rb +84 -0
  107. data/lib/karafka/pro/processing/collapser.rb +62 -0
  108. data/lib/karafka/pro/processing/coordinator.rb +148 -0
  109. data/lib/karafka/pro/processing/filters/base.rb +61 -0
  110. data/lib/karafka/pro/processing/filters/delayer.rb +70 -0
  111. data/lib/karafka/pro/processing/filters/expirer.rb +51 -0
  112. data/lib/karafka/pro/processing/filters/throttler.rb +84 -0
  113. data/lib/karafka/pro/processing/filters/virtual_limiter.rb +52 -0
  114. data/lib/karafka/pro/processing/filters_applier.rb +105 -0
  115. data/lib/karafka/pro/processing/jobs/consume_non_blocking.rb +39 -0
  116. data/lib/karafka/pro/processing/jobs/revoked_non_blocking.rb +37 -0
  117. data/lib/karafka/pro/processing/jobs_builder.rb +50 -0
  118. data/lib/karafka/pro/processing/partitioner.rb +69 -0
  119. data/lib/karafka/pro/processing/scheduler.rb +75 -0
  120. data/lib/karafka/pro/processing/strategies/aj/dlq_ftr_lrj_mom.rb +70 -0
  121. data/lib/karafka/pro/processing/strategies/aj/dlq_ftr_lrj_mom_vp.rb +76 -0
  122. data/lib/karafka/pro/processing/strategies/aj/dlq_ftr_mom.rb +72 -0
  123. data/lib/karafka/pro/processing/strategies/aj/dlq_ftr_mom_vp.rb +76 -0
  124. data/lib/karafka/pro/processing/strategies/aj/dlq_lrj_mom.rb +66 -0
  125. data/lib/karafka/pro/processing/strategies/aj/dlq_lrj_mom_vp.rb +70 -0
  126. data/lib/karafka/pro/processing/strategies/aj/dlq_mom.rb +64 -0
  127. data/lib/karafka/pro/processing/strategies/aj/dlq_mom_vp.rb +69 -0
  128. data/lib/karafka/pro/processing/strategies/aj/ftr_lrj_mom.rb +38 -0
  129. data/lib/karafka/pro/processing/strategies/aj/ftr_lrj_mom_vp.rb +66 -0
  130. data/lib/karafka/pro/processing/strategies/aj/ftr_mom.rb +38 -0
  131. data/lib/karafka/pro/processing/strategies/aj/ftr_mom_vp.rb +58 -0
  132. data/lib/karafka/pro/processing/strategies/aj/lrj_mom.rb +37 -0
  133. data/lib/karafka/pro/processing/strategies/aj/lrj_mom_vp.rb +82 -0
  134. data/lib/karafka/pro/processing/strategies/aj/mom.rb +36 -0
  135. data/lib/karafka/pro/processing/strategies/aj/mom_vp.rb +52 -0
  136. data/lib/karafka/pro/processing/strategies/base.rb +26 -0
  137. data/lib/karafka/pro/processing/strategies/default.rb +105 -0
  138. data/lib/karafka/pro/processing/strategies/dlq/default.rb +131 -0
  139. data/lib/karafka/pro/processing/strategies/dlq/ftr.rb +61 -0
  140. data/lib/karafka/pro/processing/strategies/dlq/ftr_lrj.rb +75 -0
  141. data/lib/karafka/pro/processing/strategies/dlq/ftr_lrj_mom.rb +71 -0
  142. data/lib/karafka/pro/processing/strategies/dlq/ftr_lrj_mom_vp.rb +43 -0
  143. data/lib/karafka/pro/processing/strategies/dlq/ftr_lrj_vp.rb +41 -0
  144. data/lib/karafka/pro/processing/strategies/dlq/ftr_mom.rb +69 -0
  145. data/lib/karafka/pro/processing/strategies/dlq/ftr_mom_vp.rb +41 -0
  146. data/lib/karafka/pro/processing/strategies/dlq/ftr_vp.rb +40 -0
  147. data/lib/karafka/pro/processing/strategies/dlq/lrj.rb +64 -0
  148. data/lib/karafka/pro/processing/strategies/dlq/lrj_mom.rb +65 -0
  149. data/lib/karafka/pro/processing/strategies/dlq/lrj_mom_vp.rb +36 -0
  150. data/lib/karafka/pro/processing/strategies/dlq/lrj_vp.rb +39 -0
  151. data/lib/karafka/pro/processing/strategies/dlq/mom.rb +68 -0
  152. data/lib/karafka/pro/processing/strategies/dlq/mom_vp.rb +37 -0
  153. data/lib/karafka/pro/processing/strategies/dlq/vp.rb +40 -0
  154. data/lib/karafka/pro/processing/strategies/ftr/default.rb +111 -0
  155. data/lib/karafka/pro/processing/strategies/ftr/vp.rb +40 -0
  156. data/lib/karafka/pro/processing/strategies/lrj/default.rb +87 -0
  157. data/lib/karafka/pro/processing/strategies/lrj/ftr.rb +69 -0
  158. data/lib/karafka/pro/processing/strategies/lrj/ftr_mom.rb +67 -0
  159. data/lib/karafka/pro/processing/strategies/lrj/ftr_mom_vp.rb +40 -0
  160. data/lib/karafka/pro/processing/strategies/lrj/ftr_vp.rb +39 -0
  161. data/lib/karafka/pro/processing/strategies/lrj/mom.rb +82 -0
  162. data/lib/karafka/pro/processing/strategies/lrj/mom_vp.rb +38 -0
  163. data/lib/karafka/pro/processing/strategies/lrj/vp.rb +36 -0
  164. data/lib/karafka/pro/processing/strategies/mom/default.rb +46 -0
  165. data/lib/karafka/pro/processing/strategies/mom/ftr.rb +53 -0
  166. data/lib/karafka/pro/processing/strategies/mom/ftr_vp.rb +37 -0
  167. data/lib/karafka/pro/processing/strategies/mom/vp.rb +35 -0
  168. data/lib/karafka/pro/processing/strategies/vp/default.rb +104 -0
  169. data/lib/karafka/pro/processing/strategies.rb +22 -0
  170. data/lib/karafka/pro/processing/strategy_selector.rb +84 -0
  171. data/lib/karafka/pro/processing/virtual_offset_manager.rb +147 -0
  172. data/lib/karafka/pro/routing/features/base.rb +24 -0
  173. data/lib/karafka/pro/routing/features/dead_letter_queue/contract.rb +50 -0
  174. data/lib/karafka/pro/routing/features/dead_letter_queue.rb +27 -0
  175. data/lib/karafka/pro/routing/features/delaying/config.rb +27 -0
  176. data/lib/karafka/pro/routing/features/delaying/contract.rb +38 -0
  177. data/lib/karafka/pro/routing/features/delaying/topic.rb +59 -0
  178. data/lib/karafka/pro/routing/features/delaying.rb +29 -0
  179. data/lib/karafka/pro/routing/features/expiring/config.rb +27 -0
  180. data/lib/karafka/pro/routing/features/expiring/contract.rb +38 -0
  181. data/lib/karafka/pro/routing/features/expiring/topic.rb +59 -0
  182. data/lib/karafka/pro/routing/features/expiring.rb +27 -0
  183. data/lib/karafka/pro/routing/features/filtering/config.rb +40 -0
  184. data/lib/karafka/pro/routing/features/filtering/contract.rb +41 -0
  185. data/lib/karafka/pro/routing/features/filtering/topic.rb +51 -0
  186. data/lib/karafka/pro/routing/features/filtering.rb +27 -0
  187. data/lib/karafka/pro/routing/features/long_running_job/config.rb +28 -0
  188. data/lib/karafka/pro/routing/features/long_running_job/contract.rb +37 -0
  189. data/lib/karafka/pro/routing/features/long_running_job/topic.rb +42 -0
  190. data/lib/karafka/pro/routing/features/long_running_job.rb +28 -0
  191. data/lib/karafka/pro/routing/features/pausing/contract.rb +48 -0
  192. data/lib/karafka/pro/routing/features/pausing/topic.rb +44 -0
  193. data/lib/karafka/pro/routing/features/pausing.rb +25 -0
  194. data/lib/karafka/pro/routing/features/throttling/config.rb +32 -0
  195. data/lib/karafka/pro/routing/features/throttling/contract.rb +41 -0
  196. data/lib/karafka/pro/routing/features/throttling/topic.rb +69 -0
  197. data/lib/karafka/pro/routing/features/throttling.rb +30 -0
  198. data/lib/karafka/pro/routing/features/virtual_partitions/config.rb +30 -0
  199. data/lib/karafka/pro/routing/features/virtual_partitions/contract.rb +52 -0
  200. data/lib/karafka/pro/routing/features/virtual_partitions/topic.rb +56 -0
  201. data/lib/karafka/pro/routing/features/virtual_partitions.rb +27 -0
  202. data/lib/karafka/pro.rb +13 -0
  203. data/lib/karafka/process.rb +24 -8
  204. data/lib/karafka/processing/coordinator.rb +181 -0
  205. data/lib/karafka/processing/coordinators_buffer.rb +62 -0
  206. data/lib/karafka/processing/executor.rb +148 -0
  207. data/lib/karafka/processing/executors_buffer.rb +72 -0
  208. data/lib/karafka/processing/jobs/base.rb +55 -0
  209. data/lib/karafka/processing/jobs/consume.rb +45 -0
  210. data/lib/karafka/processing/jobs/idle.rb +24 -0
  211. data/lib/karafka/processing/jobs/revoked.rb +22 -0
  212. data/lib/karafka/processing/jobs/shutdown.rb +23 -0
  213. data/lib/karafka/processing/jobs_builder.rb +28 -0
  214. data/lib/karafka/processing/jobs_queue.rb +150 -0
  215. data/lib/karafka/processing/partitioner.rb +24 -0
  216. data/lib/karafka/processing/result.rb +42 -0
  217. data/lib/karafka/processing/scheduler.rb +22 -0
  218. data/lib/karafka/processing/strategies/aj_dlq_mom.rb +44 -0
  219. data/lib/karafka/processing/strategies/aj_mom.rb +21 -0
  220. data/lib/karafka/processing/strategies/base.rb +52 -0
  221. data/lib/karafka/processing/strategies/default.rb +158 -0
  222. data/lib/karafka/processing/strategies/dlq.rb +88 -0
  223. data/lib/karafka/processing/strategies/dlq_mom.rb +49 -0
  224. data/lib/karafka/processing/strategies/mom.rb +29 -0
  225. data/lib/karafka/processing/strategy_selector.rb +47 -0
  226. data/lib/karafka/processing/worker.rb +93 -0
  227. data/lib/karafka/processing/workers_batch.rb +27 -0
  228. data/lib/karafka/railtie.rb +125 -0
  229. data/lib/karafka/routing/activity_manager.rb +84 -0
  230. data/lib/karafka/routing/builder.rb +34 -23
  231. data/lib/karafka/routing/consumer_group.rb +47 -21
  232. data/lib/karafka/routing/consumer_mapper.rb +1 -12
  233. data/lib/karafka/routing/features/active_job/builder.rb +33 -0
  234. data/lib/karafka/routing/features/active_job/config.rb +15 -0
  235. data/lib/karafka/routing/features/active_job/contract.rb +41 -0
  236. data/lib/karafka/routing/features/active_job/topic.rb +33 -0
  237. data/lib/karafka/routing/features/active_job.rb +13 -0
  238. data/lib/karafka/routing/features/base/expander.rb +53 -0
  239. data/lib/karafka/routing/features/base.rb +34 -0
  240. data/lib/karafka/routing/features/dead_letter_queue/config.rb +19 -0
  241. data/lib/karafka/routing/features/dead_letter_queue/contract.rb +42 -0
  242. data/lib/karafka/routing/features/dead_letter_queue/topic.rb +41 -0
  243. data/lib/karafka/routing/features/dead_letter_queue.rb +16 -0
  244. data/lib/karafka/routing/features/declaratives/config.rb +18 -0
  245. data/lib/karafka/routing/features/declaratives/contract.rb +30 -0
  246. data/lib/karafka/routing/features/declaratives/topic.rb +44 -0
  247. data/lib/karafka/routing/features/declaratives.rb +14 -0
  248. data/lib/karafka/routing/features/manual_offset_management/config.rb +15 -0
  249. data/lib/karafka/routing/features/manual_offset_management/contract.rb +24 -0
  250. data/lib/karafka/routing/features/manual_offset_management/topic.rb +35 -0
  251. data/lib/karafka/routing/features/manual_offset_management.rb +18 -0
  252. data/lib/karafka/routing/proxy.rb +18 -20
  253. data/lib/karafka/routing/router.rb +28 -3
  254. data/lib/karafka/routing/subscription_group.rb +91 -0
  255. data/lib/karafka/routing/subscription_groups_builder.rb +58 -0
  256. data/lib/karafka/routing/topic.rb +77 -24
  257. data/lib/karafka/routing/topics.rb +46 -0
  258. data/lib/karafka/runner.rb +52 -0
  259. data/lib/karafka/serialization/json/deserializer.rb +7 -15
  260. data/lib/karafka/server.rb +108 -37
  261. data/lib/karafka/setup/attributes_map.rb +347 -0
  262. data/lib/karafka/setup/config.rb +183 -179
  263. data/lib/karafka/status.rb +54 -7
  264. data/lib/karafka/templates/example_consumer.rb.erb +16 -0
  265. data/lib/karafka/templates/karafka.rb.erb +34 -56
  266. data/lib/karafka/time_trackers/base.rb +14 -0
  267. data/lib/karafka/time_trackers/pause.rb +122 -0
  268. data/lib/karafka/time_trackers/poll.rb +69 -0
  269. data/lib/karafka/version.rb +1 -1
  270. data/lib/karafka.rb +90 -16
  271. data/renovate.json +6 -0
  272. data.tar.gz.sig +0 -0
  273. metadata +290 -172
  274. metadata.gz.sig +0 -0
  275. data/MIT-LICENCE +0 -18
  276. data/certs/mensfeld.pem +0 -25
  277. data/config/errors.yml +0 -41
  278. data/lib/karafka/assignment_strategies/round_robin.rb +0 -13
  279. data/lib/karafka/attributes_map.rb +0 -63
  280. data/lib/karafka/backends/inline.rb +0 -16
  281. data/lib/karafka/base_responder.rb +0 -226
  282. data/lib/karafka/cli/flow.rb +0 -48
  283. data/lib/karafka/cli/missingno.rb +0 -19
  284. data/lib/karafka/code_reloader.rb +0 -67
  285. data/lib/karafka/connection/api_adapter.rb +0 -159
  286. data/lib/karafka/connection/batch_delegator.rb +0 -55
  287. data/lib/karafka/connection/builder.rb +0 -23
  288. data/lib/karafka/connection/message_delegator.rb +0 -36
  289. data/lib/karafka/consumers/batch_metadata.rb +0 -10
  290. data/lib/karafka/consumers/callbacks.rb +0 -71
  291. data/lib/karafka/consumers/includer.rb +0 -64
  292. data/lib/karafka/consumers/responders.rb +0 -24
  293. data/lib/karafka/consumers/single_params.rb +0 -15
  294. data/lib/karafka/contracts/consumer_group_topic.rb +0 -19
  295. data/lib/karafka/contracts/responder_usage.rb +0 -54
  296. data/lib/karafka/fetcher.rb +0 -42
  297. data/lib/karafka/helpers/class_matcher.rb +0 -88
  298. data/lib/karafka/helpers/config_retriever.rb +0 -46
  299. data/lib/karafka/helpers/inflector.rb +0 -26
  300. data/lib/karafka/instrumentation/stdout_listener.rb +0 -140
  301. data/lib/karafka/params/batch_metadata.rb +0 -26
  302. data/lib/karafka/params/builders/batch_metadata.rb +0 -30
  303. data/lib/karafka/params/builders/params.rb +0 -38
  304. data/lib/karafka/params/builders/params_batch.rb +0 -25
  305. data/lib/karafka/params/params_batch.rb +0 -60
  306. data/lib/karafka/patches/ruby_kafka.rb +0 -47
  307. data/lib/karafka/persistence/client.rb +0 -29
  308. data/lib/karafka/persistence/consumers.rb +0 -45
  309. data/lib/karafka/persistence/topics.rb +0 -48
  310. data/lib/karafka/responders/builder.rb +0 -36
  311. data/lib/karafka/responders/topic.rb +0 -55
  312. data/lib/karafka/routing/topic_mapper.rb +0 -53
  313. data/lib/karafka/serialization/json/serializer.rb +0 -31
  314. data/lib/karafka/setup/configurators/water_drop.rb +0 -36
  315. data/lib/karafka/templates/application_responder.rb.erb +0 -11
@@ -0,0 +1,286 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Karafka
4
+ # Simple admin actions that we can perform via Karafka on our Kafka cluster
5
+ #
6
+ # @note It always initializes a new admin instance as we want to ensure it is always closed
7
+ # Since admin actions are not performed that often, that should be ok.
8
+ #
9
+ # @note It always uses the primary defined cluster and does not support multi-cluster work.
10
+ # If you need this, just replace the cluster info for the time you use this
11
+ module Admin
12
+ # We wait only for this amount of time before raising error as we intercept this error and
13
+ # retry after checking that the operation was finished or failed using external factor.
14
+ MAX_WAIT_TIMEOUT = 1
15
+
16
+ # How many times should be try. 1 x 60 => 60 seconds wait in total
17
+ MAX_ATTEMPTS = 60
18
+
19
+ # Defaults for config
20
+ CONFIG_DEFAULTS = {
21
+ 'group.id': 'karafka_admin',
22
+ # We want to know when there is no more data not to end up with an endless loop
23
+ 'enable.partition.eof': true,
24
+ 'statistics.interval.ms': 0,
25
+ # Fetch at most 5 MBs when using admin
26
+ 'fetch.message.max.bytes': 5 * 1_048_576,
27
+ # Do not commit offset automatically, this prevents offset tracking for operations involving
28
+ # a consumer instance
29
+ 'enable.auto.commit': false,
30
+ # Make sure that topic metadata lookups do not create topics accidentally
31
+ 'allow.auto.create.topics': false
32
+ }.freeze
33
+
34
+ private_constant :CONFIG_DEFAULTS, :MAX_WAIT_TIMEOUT, :MAX_ATTEMPTS
35
+
36
+ class << self
37
+ # Allows us to read messages from the topic
38
+ #
39
+ # @param name [String, Symbol] topic name
40
+ # @param partition [Integer] partition
41
+ # @param count [Integer] how many messages we want to get at most
42
+ # @param start_offset [Integer, Time] offset from which we should start. If -1 is provided
43
+ # (default) we will start from the latest offset. If time is provided, the appropriate
44
+ # offset will be resolved.
45
+ # @param settings [Hash] kafka extra settings (optional)
46
+ #
47
+ # @return [Array<Karafka::Messages::Message>] array with messages
48
+ def read_topic(name, partition, count, start_offset = -1, settings = {})
49
+ messages = []
50
+ tpl = Rdkafka::Consumer::TopicPartitionList.new
51
+ low_offset, high_offset = nil
52
+
53
+ with_consumer(settings) do |consumer|
54
+ # Convert the time offset (if needed)
55
+ start_offset = resolve_offset(consumer, name.to_s, partition, start_offset)
56
+
57
+ low_offset, high_offset = consumer.query_watermark_offsets(name, partition)
58
+
59
+ # Select offset dynamically if -1 or less
60
+ start_offset = high_offset - count if start_offset.negative?
61
+
62
+ # Build the requested range - since first element is on the start offset we need to
63
+ # subtract one from requested count to end up with expected number of elements
64
+ requested_range = (start_offset..start_offset + (count - 1))
65
+ # Establish theoretical available range. Note, that this does not handle cases related to
66
+ # log retention or compaction
67
+ available_range = (low_offset..(high_offset - 1))
68
+ # Select only offset that we can select. This will remove all the potential offsets that
69
+ # are below the low watermark offset
70
+ possible_range = requested_range.select { |offset| available_range.include?(offset) }
71
+
72
+ start_offset = possible_range.first
73
+ count = possible_range.count
74
+
75
+ tpl.add_topic_and_partitions_with_offsets(name, partition => start_offset)
76
+ consumer.assign(tpl)
77
+
78
+ # We should poll as long as we don't have all the messages that we need or as long as
79
+ # we do not read all the messages from the topic
80
+ loop do
81
+ # If we've got as many messages as we've wanted stop
82
+ break if messages.size >= count
83
+
84
+ message = consumer.poll(200)
85
+
86
+ next unless message
87
+
88
+ # If the message we've got is beyond the requested range, stop
89
+ break unless possible_range.include?(message.offset)
90
+
91
+ messages << message
92
+ rescue Rdkafka::RdkafkaError => e
93
+ # End of partition
94
+ break if e.code == :partition_eof
95
+
96
+ raise e
97
+ end
98
+ end
99
+
100
+ # Use topic from routes if we can match it or create a dummy one
101
+ # Dummy one is used in case we cannot match the topic with routes. This can happen
102
+ # when admin API is used to read topics that are not part of the routing
103
+ topic = ::Karafka::Routing::Router.find_or_initialize_by_name(name)
104
+
105
+ messages.map! do |message|
106
+ Messages::Builders::Message.call(
107
+ message,
108
+ topic,
109
+ Time.now
110
+ )
111
+ end
112
+ end
113
+
114
+ # Creates Kafka topic with given settings
115
+ #
116
+ # @param name [String] topic name
117
+ # @param partitions [Integer] number of partitions we expect
118
+ # @param replication_factor [Integer] number of replicas
119
+ # @param topic_config [Hash] topic config details as described here:
120
+ # https://kafka.apache.org/documentation/#topicconfigs
121
+ def create_topic(name, partitions, replication_factor, topic_config = {})
122
+ with_admin do |admin|
123
+ handler = admin.create_topic(name, partitions, replication_factor, topic_config)
124
+
125
+ with_re_wait(
126
+ -> { handler.wait(max_wait_timeout: MAX_WAIT_TIMEOUT) },
127
+ -> { topics_names.include?(name) }
128
+ )
129
+ end
130
+ end
131
+
132
+ # Deleted a given topic
133
+ #
134
+ # @param name [String] topic name
135
+ def delete_topic(name)
136
+ with_admin do |admin|
137
+ handler = admin.delete_topic(name)
138
+
139
+ with_re_wait(
140
+ -> { handler.wait(max_wait_timeout: MAX_WAIT_TIMEOUT) },
141
+ -> { !topics_names.include?(name) }
142
+ )
143
+ end
144
+ end
145
+
146
+ # Creates more partitions for a given topic
147
+ #
148
+ # @param name [String] topic name
149
+ # @param partitions [Integer] total number of partitions we expect to end up with
150
+ def create_partitions(name, partitions)
151
+ with_admin do |admin|
152
+ handler = admin.create_partitions(name, partitions)
153
+
154
+ with_re_wait(
155
+ -> { handler.wait(max_wait_timeout: MAX_WAIT_TIMEOUT) },
156
+ -> { topic(name).fetch(:partition_count) >= partitions }
157
+ )
158
+ end
159
+ end
160
+
161
+ # Fetches the watermark offsets for a given topic partition
162
+ #
163
+ # @param name [String, Symbol] topic name
164
+ # @param partition [Integer] partition
165
+ # @return [Array<Integer, Integer>] low watermark offset and high watermark offset
166
+ def read_watermark_offsets(name, partition)
167
+ with_consumer do |consumer|
168
+ consumer.query_watermark_offsets(name, partition)
169
+ end
170
+ end
171
+
172
+ # @return [Rdkafka::Metadata] cluster metadata info
173
+ def cluster_info
174
+ with_admin do |admin|
175
+ admin.instance_variable_get('@native_kafka').with_inner do |inner|
176
+ Rdkafka::Metadata.new(inner)
177
+ end
178
+ end
179
+ end
180
+
181
+ # Creates consumer instance and yields it. After usage it closes the consumer instance
182
+ # This API can be used in other pieces of code and allows for low-level consumer usage
183
+ #
184
+ # @param settings [Hash] extra settings to customize consumer
185
+ #
186
+ # @note We always ship and yield a proxied consumer because admin API performance is not
187
+ # that relevant. That is, there are no high frequency calls that would have to be delegated
188
+ def with_consumer(settings = {})
189
+ consumer = config(:consumer, settings).consumer
190
+ proxy = ::Karafka::Connection::Proxy.new(consumer)
191
+ yield(proxy)
192
+ ensure
193
+ # Always unsubscribe consumer just to be sure, that no metadata requests are running
194
+ # when we close the consumer. This in theory should prevent from some race-conditions
195
+ # that originate from librdkafka
196
+ begin
197
+ consumer&.unsubscribe
198
+ # Ignore any errors and continue to close consumer despite them
199
+ rescue Rdkafka::RdkafkaError
200
+ nil
201
+ end
202
+
203
+ consumer&.close
204
+ end
205
+
206
+ private
207
+
208
+ # @return [Array<String>] topics names
209
+ def topics_names
210
+ cluster_info.topics.map { |topic| topic.fetch(:topic_name) }
211
+ end
212
+
213
+ # Finds details about given topic
214
+ # @param name [String] topic name
215
+ # @return [Hash] topic details
216
+ def topic(name)
217
+ cluster_info.topics.find { |topic| topic[:topic_name] == name }
218
+ end
219
+
220
+ # Creates admin instance and yields it. After usage it closes the admin instance
221
+ def with_admin
222
+ admin = config(:producer, {}).admin
223
+ yield(admin)
224
+ ensure
225
+ admin&.close
226
+ end
227
+
228
+ # There are some cases where rdkafka admin operations finish successfully but without the
229
+ # callback being triggered to materialize the post-promise object. Until this is fixed we
230
+ # can figure out, that operation we wanted to do finished successfully by checking that the
231
+ # effect of the command (new topic, more partitions, etc) is handled. Exactly for that we
232
+ # use the breaker. It we get a timeout, we can check that what we wanted to achieve has
233
+ # happened via the breaker check, hence we do not need to wait any longer.
234
+ #
235
+ # @param handler [Proc] the wait handler operation
236
+ # @param breaker [Proc] extra condition upon timeout that indicates things were finished ok
237
+ def with_re_wait(handler, breaker)
238
+ attempt ||= 0
239
+ attempt += 1
240
+
241
+ handler.call
242
+ rescue Rdkafka::AbstractHandle::WaitTimeoutError
243
+ return if breaker.call
244
+
245
+ retry if attempt <= MAX_ATTEMPTS
246
+
247
+ raise
248
+ end
249
+
250
+ # @param type [Symbol] type of config we want
251
+ # @param settings [Hash] extra settings for config (if needed)
252
+ # @return [::Rdkafka::Config] rdkafka config
253
+ def config(type, settings)
254
+ config_hash = Karafka::Setup::AttributesMap.public_send(
255
+ type,
256
+ Karafka::App.config.kafka.dup.merge(CONFIG_DEFAULTS).merge!(settings)
257
+ )
258
+
259
+ ::Rdkafka::Config.new(config_hash)
260
+ end
261
+
262
+ # Resolves the offset if offset is in a time format. Otherwise returns the offset without
263
+ # resolving.
264
+ # @param consumer [::Rdkafka::Consumer]
265
+ # @param name [String, Symbol] expected topic name
266
+ # @param partition [Integer]
267
+ # @param offset [Integer, Time]
268
+ # @return [Integer] expected offset
269
+ def resolve_offset(consumer, name, partition, offset)
270
+ if offset.is_a?(Time)
271
+ tpl = ::Rdkafka::Consumer::TopicPartitionList.new
272
+ tpl.add_topic_and_partitions_with_offsets(
273
+ name, partition => offset
274
+ )
275
+
276
+ real_offsets = consumer.offsets_for_times(tpl)
277
+ detected_offset = real_offsets.to_h.dig(name, partition)
278
+
279
+ detected_offset&.offset || raise(Errors::InvalidTimeBasedOffsetError)
280
+ else
281
+ offset
282
+ end
283
+ end
284
+ end
285
+ end
286
+ end
data/lib/karafka/app.rb CHANGED
@@ -6,35 +6,57 @@ module Karafka
6
6
  extend Setup::Dsl
7
7
 
8
8
  class << self
9
- # Sets up all the internal components and bootstrap whole app
10
- # We need to know details about consumers in order to setup components,
11
- # that's why we don't setup them after std setup is done
12
- # @raise [Karafka::Errors::InvalidConfigurationError] raised when configuration
13
- # doesn't match with the config contract
14
- def boot!
15
- initialize!
16
- Setup::Config.validate!
17
- Setup::Config.setup_components
18
- initialized!
9
+ # @return [Karafka::Routing::Builder] consumers builder instance alias
10
+ def consumer_groups
11
+ config
12
+ .internal
13
+ .routing
14
+ .builder
19
15
  end
20
16
 
21
- # @return [Karafka::Routing::Builder] consumers builder instance
22
- def consumer_groups
23
- config.internal.routing_builder
17
+ # @return [Hash] active subscription groups grouped based on consumer group in a hash
18
+ def subscription_groups
19
+ # We first build all the subscription groups, so they all get the same position, despite
20
+ # later narrowing that. It allows us to maintain same position number for static members
21
+ # even when we want to run subset of consumer groups or subscription groups
22
+ #
23
+ # We then narrow this to active consumer groups from which we select active subscription
24
+ # groups.
25
+ consumer_groups
26
+ .map { |cg| [cg, cg.subscription_groups] }
27
+ .select { |cg, _| cg.active? }
28
+ .select { |_, sgs| sgs.delete_if { |sg| !sg.active? } }
29
+ .delete_if { |_, sgs| sgs.empty? }
30
+ .each { |_, sgs| sgs.each { |sg| sg.topics.delete_if { |top| !top.active? } } }
31
+ .each { |_, sgs| sgs.delete_if { |sg| sg.topics.empty? } }
32
+ .reject { |cg, _| cg.subscription_groups.empty? }
33
+ .to_h
24
34
  end
25
35
 
26
- # Triggers reload of all cached Karafka app components, so we can use in-process
27
- # in-development hot code reloading without Karafka process restart
28
- def reload
29
- Karafka::Persistence::Consumers.clear
30
- Karafka::Persistence::Topics.clear
31
- config.internal.routing_builder.reload
36
+ # Just a nicer name for the consumer groups
37
+ alias routes consumer_groups
38
+
39
+ # Allow for easier status management via `Karafka::App` by aliasing status methods here
40
+ Status::STATES.each do |state, transition|
41
+ class_eval <<~RUBY, __FILE__, __LINE__ + 1
42
+ def #{state}
43
+ App.config.internal.status.#{state}
44
+ end
45
+
46
+ def #{state}?
47
+ App.config.internal.status.#{state}?
48
+ end
49
+
50
+ def #{transition}
51
+ App.config.internal.status.#{transition}
52
+ end
53
+ RUBY
32
54
  end
33
55
 
34
- Status.instance_methods(false).each do |delegated|
35
- define_method(delegated) do
36
- App.config.internal.status.send(delegated)
37
- end
56
+ # @return [Boolean] true if we should be done in general with processing anything
57
+ # @note It is a meta status from the status object
58
+ def done?
59
+ App.config.internal.status.done?
38
60
  end
39
61
 
40
62
  # Methods that should be delegated to Karafka module
@@ -42,7 +64,9 @@ module Karafka
42
64
  root
43
65
  env
44
66
  logger
67
+ producer
45
68
  monitor
69
+ pro?
46
70
  ].each do |delegated|
47
71
  define_method(delegated) do
48
72
  Karafka.send(delegated)
@@ -4,54 +4,272 @@
4
4
  module Karafka
5
5
  # Base consumer from which all Karafka consumers should inherit
6
6
  class BaseConsumer
7
+ # Allow for consumer instance tagging for instrumentation
8
+ include ::Karafka::Core::Taggable
9
+
7
10
  extend Forwardable
8
11
 
9
- # Allows us to mark messages as consumed for non-automatic mode without having
10
- # to use consumer client directly. We do this that way, because most of the people should not
11
- # mess with the client instance directly (just in case)
12
- %i[
13
- mark_as_consumed
14
- mark_as_consumed!
15
- trigger_heartbeat
16
- trigger_heartbeat!
17
- ].each do |delegated_method_name|
18
- def_delegator :client, delegated_method_name
12
+ def_delegators :@coordinator, :topic, :partition
13
+
14
+ # @return [String] id of the current consumer
15
+ attr_reader :id
16
+ # @return [Karafka::Routing::Topic] topic to which a given consumer is subscribed
17
+ attr_accessor :messages
18
+ # @return [Karafka::Connection::Client] kafka connection client
19
+ attr_accessor :client
20
+ # @return [Karafka::Processing::Coordinator] coordinator
21
+ attr_accessor :coordinator
22
+ # @return [Waterdrop::Producer] producer instance
23
+ attr_accessor :producer
19
24
 
20
- private delegated_method_name
25
+ # Creates new consumer and assigns it an id
26
+ def initialize
27
+ @id = SecureRandom.hex(6)
28
+ @used = false
21
29
  end
22
30
 
23
- # @return [Karafka::Routing::Topic] topic to which a given consumer is subscribed
24
- attr_reader :topic
25
- # @return [Karafka::Params:ParamsBatch] current params batch
26
- attr_accessor :params_batch
31
+ # Can be used to run preparation code prior to the job being enqueued
32
+ #
33
+ # @private
34
+ # @note This should not be used by the end users as it is part of the lifecycle of things and
35
+ # not as a part of the public api. This should not perform any extensive operations as it is
36
+ # blocking and running in the listener thread.
37
+ def on_before_enqueue
38
+ @used = true
39
+ handle_before_enqueue
40
+ rescue StandardError => e
41
+ Karafka.monitor.instrument(
42
+ 'error.occurred',
43
+ error: e,
44
+ caller: self,
45
+ type: 'consumer.before_enqueue.error'
46
+ )
47
+ end
27
48
 
28
- # Assigns a topic to a consumer and builds up proper consumer functionalities
29
- # so that it can cooperate with the topic settings
30
- # @param topic [Karafka::Routing::Topic]
31
- def initialize(topic)
32
- @topic = topic
33
- Consumers::Includer.call(self)
49
+ # Can be used to run preparation code in the worker
50
+ #
51
+ # @private
52
+ # @note This should not be used by the end users as it is part of the lifecycle of things and
53
+ # not as part of the public api. This can act as a hook when creating non-blocking
54
+ # consumers and doing other advanced stuff
55
+ def on_before_consume
56
+ messages.metadata.processed_at = Time.now
57
+ messages.metadata.freeze
58
+
59
+ # We run this after the full metadata setup, so we can use all the messages information
60
+ # if needed
61
+ handle_before_consume
62
+ rescue StandardError => e
63
+ Karafka.monitor.instrument(
64
+ 'error.occurred',
65
+ error: e,
66
+ caller: self,
67
+ type: 'consumer.before_consume.error'
68
+ )
34
69
  end
35
70
 
36
71
  # Executes the default consumer flow.
37
- def call
38
- process
72
+ #
73
+ # @private
74
+ #
75
+ # @return [Boolean] true if there was no exception, otherwise false.
76
+ # @note We keep the seek offset tracking, and use it to compensate for async offset flushing
77
+ # that may not yet kick in when error occurs. That way we pause always on the last processed
78
+ # message.
79
+ def on_consume
80
+ handle_consume
81
+ rescue StandardError => e
82
+ Karafka.monitor.instrument(
83
+ 'error.occurred',
84
+ error: e,
85
+ caller: self,
86
+ seek_offset: coordinator.seek_offset,
87
+ type: 'consumer.consume.error'
88
+ )
39
89
  end
40
90
 
41
- private
91
+ # @private
92
+ # @note This should not be used by the end users as it is part of the lifecycle of things but
93
+ # not as part of the public api.
94
+ def on_after_consume
95
+ handle_after_consume
96
+ rescue StandardError => e
97
+ Karafka.monitor.instrument(
98
+ 'error.occurred',
99
+ error: e,
100
+ caller: self,
101
+ type: 'consumer.after_consume.error'
102
+ )
103
+ end
104
+
105
+ # Trigger method for running on idle runs without messages
106
+ #
107
+ # @private
108
+ def on_idle
109
+ handle_idle
110
+ rescue StandardError => e
111
+ Karafka.monitor.instrument(
112
+ 'error.occurred',
113
+ error: e,
114
+ caller: self,
115
+ type: 'consumer.idle.error'
116
+ )
117
+ end
42
118
 
43
- # @return [Karafka::Connection::Client] messages consuming client that can be used to
44
- # commit manually offset or pause / stop consumer based on the business logic
45
- def client
46
- Persistence::Client.read
119
+ # Trigger method for running on partition revocation.
120
+ #
121
+ # @private
122
+ def on_revoked
123
+ handle_revoked
124
+ rescue StandardError => e
125
+ Karafka.monitor.instrument(
126
+ 'error.occurred',
127
+ error: e,
128
+ caller: self,
129
+ type: 'consumer.revoked.error'
130
+ )
47
131
  end
48
132
 
133
+ # Trigger method for running on shutdown.
134
+ #
135
+ # @private
136
+ def on_shutdown
137
+ handle_shutdown
138
+ rescue StandardError => e
139
+ Karafka.monitor.instrument(
140
+ 'error.occurred',
141
+ error: e,
142
+ caller: self,
143
+ type: 'consumer.shutdown.error'
144
+ )
145
+ end
146
+
147
+ private
148
+
49
149
  # Method that will perform business logic and on data received from Kafka (it will consume
50
150
  # the data)
51
- # @note This method needs bo be implemented in a subclass. We stub it here as a failover if
151
+ # @note This method needs to be implemented in a subclass. We stub it here as a failover if
52
152
  # someone forgets about it or makes on with typo
53
153
  def consume
54
154
  raise NotImplementedError, 'Implement this in a subclass'
55
155
  end
156
+
157
+ # Method that will be executed when a given topic partition is revoked. You can use it for
158
+ # some teardown procedures (closing file handler, etc).
159
+ def revoked; end
160
+
161
+ # Method that will be executed when the process is shutting down. You can use it for
162
+ # some teardown procedures (closing file handler, etc).
163
+ def shutdown; end
164
+
165
+ # @return [Boolean] was this consumer in active use. Active use means running `#consume` at
166
+ # least once. Consumer may have to run `#revoked` or `#shutdown` despite not running
167
+ # `#consume` previously in delayed job cases and other cases that potentially involve running
168
+ # the `Jobs::Idle` for house-keeping
169
+ def used?
170
+ @used
171
+ end
172
+
173
+ # Pauses processing on a given offset for the current topic partition
174
+ #
175
+ # After given partition is resumed, it will continue processing from the given offset
176
+ # @param offset [Integer] offset from which we want to restart the processing
177
+ # @param timeout [Integer, nil] how long in milliseconds do we want to pause or nil to use the
178
+ # default exponential pausing strategy defined for retries
179
+ # @param manual_pause [Boolean] Flag to differentiate between user pause and system/strategy
180
+ # based pause. While they both pause in exactly the same way, the strategy application
181
+ # may need to differentiate between them.
182
+ def pause(offset, timeout = nil, manual_pause = true)
183
+ timeout ? coordinator.pause_tracker.pause(timeout) : coordinator.pause_tracker.pause
184
+
185
+ client.pause(
186
+ topic.name,
187
+ partition,
188
+ offset
189
+ )
190
+
191
+ # Indicate, that user took a manual action of pausing
192
+ coordinator.manual_pause if manual_pause
193
+
194
+ Karafka.monitor.instrument(
195
+ 'consumer.consuming.pause',
196
+ caller: self,
197
+ manual: manual_pause,
198
+ topic: topic.name,
199
+ partition: partition,
200
+ offset: offset,
201
+ timeout: coordinator.pause_tracker.current_timeout,
202
+ attempt: coordinator.pause_tracker.attempt
203
+ )
204
+ end
205
+
206
+ # Resumes processing of the current topic partition
207
+ def resume
208
+ return unless coordinator.pause_tracker.paused?
209
+
210
+ # This is sufficient to expire a partition pause, as with it will be resumed by the listener
211
+ # thread before the next poll.
212
+ coordinator.pause_tracker.expire
213
+ end
214
+
215
+ # Seeks in the context of current topic and partition
216
+ #
217
+ # @param offset [Integer, Time] offset where we want to seek or time of the offset where we
218
+ # want to seek.
219
+ # @param manual_seek [Boolean] Flag to differentiate between user seek and system/strategy
220
+ # based seek. User seek operations should take precedence over system actions, hence we need
221
+ # to know who invoked it.
222
+ # @note Please note, that if you are seeking to a time offset, getting the offset is blocking
223
+ def seek(offset, manual_seek = true)
224
+ coordinator.manual_seek if manual_seek
225
+
226
+ client.seek(
227
+ Karafka::Messages::Seek.new(
228
+ topic.name,
229
+ partition,
230
+ offset
231
+ )
232
+ )
233
+ end
234
+
235
+ # @return [Boolean] true if partition was revoked from the current consumer
236
+ # @note There are two "levels" on which we can know that partition was revoked. First one is
237
+ # when we loose the assignment involuntarily and second is when coordinator gets this info
238
+ # after we poll with the rebalance callbacks. The first check allows us to get this notion
239
+ # even before we poll but it gets reset when polling happens, hence we also need to switch
240
+ # the coordinator state after the revocation (but prior to running more jobs)
241
+ def revoked?
242
+ return true if coordinator.revoked?
243
+ return false unless client.assignment_lost?
244
+
245
+ coordinator.revoke
246
+
247
+ true
248
+ end
249
+
250
+ # @return [Boolean] are we retrying processing after an error. This can be used to provide a
251
+ # different flow after there is an error, for example for resources cleanup, small manual
252
+ # backoff or different instrumentation tracking.
253
+ def retrying?
254
+ coordinator.pause_tracker.attempt > 1
255
+ end
256
+
257
+ # Pauses the processing from the last offset to retry on given message
258
+ # @private
259
+ def retry_after_pause
260
+ pause(coordinator.seek_offset, nil, false)
261
+
262
+ # Instrumentation needs to run **after** `#pause` invocation because we rely on the states
263
+ # set by `#pause`
264
+ Karafka.monitor.instrument(
265
+ 'consumer.consuming.retry',
266
+ caller: self,
267
+ topic: topic.name,
268
+ partition: partition,
269
+ offset: coordinator.seek_offset,
270
+ timeout: coordinator.pause_tracker.current_timeout,
271
+ attempt: coordinator.pause_tracker.attempt
272
+ )
273
+ end
56
274
  end
57
275
  end