karafka 1.4.12 → 2.2.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (359) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/.github/FUNDING.yml +1 -0
  4. data/.github/ISSUE_TEMPLATE/bug_report.md +10 -9
  5. data/.github/workflows/ci.yml +169 -31
  6. data/.rspec +4 -0
  7. data/.ruby-version +1 -1
  8. data/CHANGELOG.md +716 -607
  9. data/CONTRIBUTING.md +10 -19
  10. data/Gemfile +7 -0
  11. data/Gemfile.lock +69 -92
  12. data/LICENSE +17 -0
  13. data/LICENSE-COMM +89 -0
  14. data/LICENSE-LGPL +165 -0
  15. data/README.md +48 -47
  16. data/bin/benchmarks +99 -0
  17. data/bin/create_token +22 -0
  18. data/bin/integrations +310 -0
  19. data/bin/karafka +5 -14
  20. data/bin/record_rss +50 -0
  21. data/bin/rspecs +6 -0
  22. data/bin/scenario +29 -0
  23. data/bin/stress_many +13 -0
  24. data/bin/stress_one +13 -0
  25. data/bin/verify_license_integrity +37 -0
  26. data/bin/wait_for_kafka +24 -0
  27. data/certs/cert_chain.pem +26 -0
  28. data/certs/karafka-pro.pem +11 -0
  29. data/config/locales/errors.yml +97 -0
  30. data/config/locales/pro_errors.yml +59 -0
  31. data/docker-compose.yml +19 -11
  32. data/karafka.gemspec +26 -22
  33. data/lib/active_job/karafka.rb +17 -0
  34. data/lib/active_job/queue_adapters/karafka_adapter.rb +32 -0
  35. data/lib/karafka/active_job/consumer.rb +49 -0
  36. data/lib/karafka/active_job/current_attributes/loading.rb +36 -0
  37. data/lib/karafka/active_job/current_attributes/persistence.rb +28 -0
  38. data/lib/karafka/active_job/current_attributes.rb +42 -0
  39. data/lib/karafka/active_job/dispatcher.rb +69 -0
  40. data/lib/karafka/active_job/job_extensions.rb +34 -0
  41. data/lib/karafka/active_job/job_options_contract.rb +32 -0
  42. data/lib/karafka/admin.rb +313 -0
  43. data/lib/karafka/app.rb +47 -23
  44. data/lib/karafka/base_consumer.rb +260 -29
  45. data/lib/karafka/cli/base.rb +67 -36
  46. data/lib/karafka/cli/console.rb +18 -12
  47. data/lib/karafka/cli/help.rb +24 -0
  48. data/lib/karafka/cli/info.rb +47 -12
  49. data/lib/karafka/cli/install.rb +23 -14
  50. data/lib/karafka/cli/server.rb +101 -44
  51. data/lib/karafka/cli/topics.rb +146 -0
  52. data/lib/karafka/cli.rb +24 -27
  53. data/lib/karafka/connection/client.rb +553 -90
  54. data/lib/karafka/connection/consumer_group_coordinator.rb +48 -0
  55. data/lib/karafka/connection/listener.rb +294 -38
  56. data/lib/karafka/connection/listeners_batch.rb +40 -0
  57. data/lib/karafka/connection/messages_buffer.rb +84 -0
  58. data/lib/karafka/connection/pauses_manager.rb +46 -0
  59. data/lib/karafka/connection/proxy.rb +98 -0
  60. data/lib/karafka/connection/raw_messages_buffer.rb +101 -0
  61. data/lib/karafka/connection/rebalance_manager.rb +105 -0
  62. data/lib/karafka/contracts/base.rb +17 -0
  63. data/lib/karafka/contracts/config.rb +130 -11
  64. data/lib/karafka/contracts/consumer_group.rb +32 -187
  65. data/lib/karafka/contracts/server_cli_options.rb +80 -19
  66. data/lib/karafka/contracts/topic.rb +65 -0
  67. data/lib/karafka/contracts.rb +1 -1
  68. data/lib/karafka/embedded.rb +36 -0
  69. data/lib/karafka/env.rb +46 -0
  70. data/lib/karafka/errors.rb +37 -21
  71. data/lib/karafka/helpers/async.rb +33 -0
  72. data/lib/karafka/helpers/colorize.rb +26 -0
  73. data/lib/karafka/helpers/multi_delegator.rb +2 -2
  74. data/lib/karafka/instrumentation/callbacks/error.rb +39 -0
  75. data/lib/karafka/instrumentation/callbacks/rebalance.rb +64 -0
  76. data/lib/karafka/instrumentation/callbacks/statistics.rb +51 -0
  77. data/lib/karafka/instrumentation/logger_listener.rb +303 -0
  78. data/lib/karafka/instrumentation/monitor.rb +13 -61
  79. data/lib/karafka/instrumentation/notifications.rb +79 -0
  80. data/lib/karafka/instrumentation/proctitle_listener.rb +7 -16
  81. data/lib/karafka/instrumentation/vendors/appsignal/base.rb +30 -0
  82. data/lib/karafka/instrumentation/vendors/appsignal/client.rb +122 -0
  83. data/lib/karafka/instrumentation/vendors/appsignal/dashboard.json +222 -0
  84. data/lib/karafka/instrumentation/vendors/appsignal/errors_listener.rb +30 -0
  85. data/lib/karafka/instrumentation/vendors/appsignal/metrics_listener.rb +331 -0
  86. data/lib/karafka/instrumentation/vendors/datadog/dashboard.json +1 -0
  87. data/lib/karafka/instrumentation/vendors/datadog/logger_listener.rb +155 -0
  88. data/lib/karafka/instrumentation/vendors/datadog/metrics_listener.rb +264 -0
  89. data/lib/karafka/instrumentation/vendors/kubernetes/liveness_listener.rb +176 -0
  90. data/lib/karafka/licenser.rb +78 -0
  91. data/lib/karafka/messages/batch_metadata.rb +52 -0
  92. data/lib/karafka/messages/builders/batch_metadata.rb +60 -0
  93. data/lib/karafka/messages/builders/message.rb +40 -0
  94. data/lib/karafka/messages/builders/messages.rb +36 -0
  95. data/lib/karafka/{params/params.rb → messages/message.rb} +20 -13
  96. data/lib/karafka/messages/messages.rb +71 -0
  97. data/lib/karafka/{params → messages}/metadata.rb +4 -6
  98. data/lib/karafka/messages/parser.rb +14 -0
  99. data/lib/karafka/messages/seek.rb +12 -0
  100. data/lib/karafka/patches/rdkafka/bindings.rb +122 -0
  101. data/lib/karafka/patches/rdkafka/opaque.rb +36 -0
  102. data/lib/karafka/pro/active_job/consumer.rb +47 -0
  103. data/lib/karafka/pro/active_job/dispatcher.rb +86 -0
  104. data/lib/karafka/pro/active_job/job_options_contract.rb +45 -0
  105. data/lib/karafka/pro/cleaner/errors.rb +27 -0
  106. data/lib/karafka/pro/cleaner/messages/message.rb +46 -0
  107. data/lib/karafka/pro/cleaner/messages/messages.rb +42 -0
  108. data/lib/karafka/pro/cleaner.rb +41 -0
  109. data/lib/karafka/pro/contracts/base.rb +23 -0
  110. data/lib/karafka/pro/contracts/server_cli_options.rb +111 -0
  111. data/lib/karafka/pro/encryption/cipher.rb +58 -0
  112. data/lib/karafka/pro/encryption/contracts/config.rb +79 -0
  113. data/lib/karafka/pro/encryption/errors.rb +27 -0
  114. data/lib/karafka/pro/encryption/messages/middleware.rb +46 -0
  115. data/lib/karafka/pro/encryption/messages/parser.rb +56 -0
  116. data/lib/karafka/pro/encryption/setup/config.rb +48 -0
  117. data/lib/karafka/pro/encryption.rb +47 -0
  118. data/lib/karafka/pro/iterator/expander.rb +95 -0
  119. data/lib/karafka/pro/iterator/tpl_builder.rb +155 -0
  120. data/lib/karafka/pro/iterator.rb +170 -0
  121. data/lib/karafka/pro/loader.rb +106 -0
  122. data/lib/karafka/pro/performance_tracker.rb +84 -0
  123. data/lib/karafka/pro/processing/collapser.rb +62 -0
  124. data/lib/karafka/pro/processing/coordinator.rb +147 -0
  125. data/lib/karafka/pro/processing/filters/base.rb +61 -0
  126. data/lib/karafka/pro/processing/filters/delayer.rb +70 -0
  127. data/lib/karafka/pro/processing/filters/expirer.rb +51 -0
  128. data/lib/karafka/pro/processing/filters/inline_insights_delayer.rb +78 -0
  129. data/lib/karafka/pro/processing/filters/throttler.rb +84 -0
  130. data/lib/karafka/pro/processing/filters/virtual_limiter.rb +52 -0
  131. data/lib/karafka/pro/processing/filters_applier.rb +105 -0
  132. data/lib/karafka/pro/processing/jobs/consume_non_blocking.rb +39 -0
  133. data/lib/karafka/pro/processing/jobs/revoked_non_blocking.rb +37 -0
  134. data/lib/karafka/pro/processing/jobs_builder.rb +50 -0
  135. data/lib/karafka/pro/processing/partitioner.rb +69 -0
  136. data/lib/karafka/pro/processing/scheduler.rb +75 -0
  137. data/lib/karafka/pro/processing/strategies/aj/dlq_ftr_lrj_mom.rb +70 -0
  138. data/lib/karafka/pro/processing/strategies/aj/dlq_ftr_lrj_mom_vp.rb +76 -0
  139. data/lib/karafka/pro/processing/strategies/aj/dlq_ftr_mom.rb +72 -0
  140. data/lib/karafka/pro/processing/strategies/aj/dlq_ftr_mom_vp.rb +76 -0
  141. data/lib/karafka/pro/processing/strategies/aj/dlq_lrj_mom.rb +66 -0
  142. data/lib/karafka/pro/processing/strategies/aj/dlq_lrj_mom_vp.rb +70 -0
  143. data/lib/karafka/pro/processing/strategies/aj/dlq_mom.rb +64 -0
  144. data/lib/karafka/pro/processing/strategies/aj/dlq_mom_vp.rb +69 -0
  145. data/lib/karafka/pro/processing/strategies/aj/ftr_lrj_mom.rb +38 -0
  146. data/lib/karafka/pro/processing/strategies/aj/ftr_lrj_mom_vp.rb +66 -0
  147. data/lib/karafka/pro/processing/strategies/aj/ftr_mom.rb +38 -0
  148. data/lib/karafka/pro/processing/strategies/aj/ftr_mom_vp.rb +58 -0
  149. data/lib/karafka/pro/processing/strategies/aj/lrj_mom.rb +37 -0
  150. data/lib/karafka/pro/processing/strategies/aj/lrj_mom_vp.rb +82 -0
  151. data/lib/karafka/pro/processing/strategies/aj/mom.rb +36 -0
  152. data/lib/karafka/pro/processing/strategies/aj/mom_vp.rb +52 -0
  153. data/lib/karafka/pro/processing/strategies/base.rb +26 -0
  154. data/lib/karafka/pro/processing/strategies/default.rb +105 -0
  155. data/lib/karafka/pro/processing/strategies/dlq/default.rb +137 -0
  156. data/lib/karafka/pro/processing/strategies/dlq/ftr.rb +61 -0
  157. data/lib/karafka/pro/processing/strategies/dlq/ftr_lrj.rb +75 -0
  158. data/lib/karafka/pro/processing/strategies/dlq/ftr_lrj_mom.rb +71 -0
  159. data/lib/karafka/pro/processing/strategies/dlq/ftr_lrj_mom_vp.rb +43 -0
  160. data/lib/karafka/pro/processing/strategies/dlq/ftr_lrj_vp.rb +41 -0
  161. data/lib/karafka/pro/processing/strategies/dlq/ftr_mom.rb +69 -0
  162. data/lib/karafka/pro/processing/strategies/dlq/ftr_mom_vp.rb +41 -0
  163. data/lib/karafka/pro/processing/strategies/dlq/ftr_vp.rb +40 -0
  164. data/lib/karafka/pro/processing/strategies/dlq/lrj.rb +64 -0
  165. data/lib/karafka/pro/processing/strategies/dlq/lrj_mom.rb +65 -0
  166. data/lib/karafka/pro/processing/strategies/dlq/lrj_mom_vp.rb +36 -0
  167. data/lib/karafka/pro/processing/strategies/dlq/lrj_vp.rb +39 -0
  168. data/lib/karafka/pro/processing/strategies/dlq/mom.rb +68 -0
  169. data/lib/karafka/pro/processing/strategies/dlq/mom_vp.rb +37 -0
  170. data/lib/karafka/pro/processing/strategies/dlq/vp.rb +40 -0
  171. data/lib/karafka/pro/processing/strategies/ftr/default.rb +111 -0
  172. data/lib/karafka/pro/processing/strategies/ftr/vp.rb +40 -0
  173. data/lib/karafka/pro/processing/strategies/lrj/default.rb +85 -0
  174. data/lib/karafka/pro/processing/strategies/lrj/ftr.rb +69 -0
  175. data/lib/karafka/pro/processing/strategies/lrj/ftr_mom.rb +67 -0
  176. data/lib/karafka/pro/processing/strategies/lrj/ftr_mom_vp.rb +40 -0
  177. data/lib/karafka/pro/processing/strategies/lrj/ftr_vp.rb +39 -0
  178. data/lib/karafka/pro/processing/strategies/lrj/mom.rb +77 -0
  179. data/lib/karafka/pro/processing/strategies/lrj/mom_vp.rb +38 -0
  180. data/lib/karafka/pro/processing/strategies/lrj/vp.rb +36 -0
  181. data/lib/karafka/pro/processing/strategies/mom/default.rb +46 -0
  182. data/lib/karafka/pro/processing/strategies/mom/ftr.rb +53 -0
  183. data/lib/karafka/pro/processing/strategies/mom/ftr_vp.rb +37 -0
  184. data/lib/karafka/pro/processing/strategies/mom/vp.rb +35 -0
  185. data/lib/karafka/pro/processing/strategies/vp/default.rb +124 -0
  186. data/lib/karafka/pro/processing/strategies.rb +22 -0
  187. data/lib/karafka/pro/processing/strategy_selector.rb +84 -0
  188. data/lib/karafka/pro/processing/virtual_offset_manager.rb +147 -0
  189. data/lib/karafka/pro/routing/features/active_job/builder.rb +45 -0
  190. data/lib/karafka/pro/routing/features/active_job.rb +26 -0
  191. data/lib/karafka/pro/routing/features/base.rb +24 -0
  192. data/lib/karafka/pro/routing/features/dead_letter_queue/contracts/topic.rb +53 -0
  193. data/lib/karafka/pro/routing/features/dead_letter_queue.rb +27 -0
  194. data/lib/karafka/pro/routing/features/delaying/config.rb +27 -0
  195. data/lib/karafka/pro/routing/features/delaying/contracts/topic.rb +41 -0
  196. data/lib/karafka/pro/routing/features/delaying/topic.rb +59 -0
  197. data/lib/karafka/pro/routing/features/delaying.rb +29 -0
  198. data/lib/karafka/pro/routing/features/expiring/config.rb +27 -0
  199. data/lib/karafka/pro/routing/features/expiring/contracts/topic.rb +41 -0
  200. data/lib/karafka/pro/routing/features/expiring/topic.rb +59 -0
  201. data/lib/karafka/pro/routing/features/expiring.rb +27 -0
  202. data/lib/karafka/pro/routing/features/filtering/config.rb +40 -0
  203. data/lib/karafka/pro/routing/features/filtering/contracts/topic.rb +44 -0
  204. data/lib/karafka/pro/routing/features/filtering/topic.rb +51 -0
  205. data/lib/karafka/pro/routing/features/filtering.rb +27 -0
  206. data/lib/karafka/pro/routing/features/inline_insights/config.rb +32 -0
  207. data/lib/karafka/pro/routing/features/inline_insights/contracts/topic.rb +41 -0
  208. data/lib/karafka/pro/routing/features/inline_insights/topic.rb +52 -0
  209. data/lib/karafka/pro/routing/features/inline_insights.rb +26 -0
  210. data/lib/karafka/pro/routing/features/long_running_job/config.rb +28 -0
  211. data/lib/karafka/pro/routing/features/long_running_job/contracts/topic.rb +40 -0
  212. data/lib/karafka/pro/routing/features/long_running_job/topic.rb +42 -0
  213. data/lib/karafka/pro/routing/features/long_running_job.rb +28 -0
  214. data/lib/karafka/pro/routing/features/patterns/builder.rb +38 -0
  215. data/lib/karafka/pro/routing/features/patterns/config.rb +54 -0
  216. data/lib/karafka/pro/routing/features/patterns/consumer_group.rb +72 -0
  217. data/lib/karafka/pro/routing/features/patterns/contracts/consumer_group.rb +62 -0
  218. data/lib/karafka/pro/routing/features/patterns/contracts/pattern.rb +46 -0
  219. data/lib/karafka/pro/routing/features/patterns/contracts/topic.rb +41 -0
  220. data/lib/karafka/pro/routing/features/patterns/detector.rb +71 -0
  221. data/lib/karafka/pro/routing/features/patterns/pattern.rb +95 -0
  222. data/lib/karafka/pro/routing/features/patterns/patterns.rb +35 -0
  223. data/lib/karafka/pro/routing/features/patterns/topic.rb +50 -0
  224. data/lib/karafka/pro/routing/features/patterns/topics.rb +53 -0
  225. data/lib/karafka/pro/routing/features/patterns.rb +33 -0
  226. data/lib/karafka/pro/routing/features/pausing/contracts/topic.rb +51 -0
  227. data/lib/karafka/pro/routing/features/pausing/topic.rb +44 -0
  228. data/lib/karafka/pro/routing/features/pausing.rb +25 -0
  229. data/lib/karafka/pro/routing/features/throttling/config.rb +32 -0
  230. data/lib/karafka/pro/routing/features/throttling/contracts/topic.rb +44 -0
  231. data/lib/karafka/pro/routing/features/throttling/topic.rb +69 -0
  232. data/lib/karafka/pro/routing/features/throttling.rb +30 -0
  233. data/lib/karafka/pro/routing/features/virtual_partitions/config.rb +30 -0
  234. data/lib/karafka/pro/routing/features/virtual_partitions/contracts/topic.rb +55 -0
  235. data/lib/karafka/pro/routing/features/virtual_partitions/topic.rb +56 -0
  236. data/lib/karafka/pro/routing/features/virtual_partitions.rb +27 -0
  237. data/lib/karafka/pro.rb +13 -0
  238. data/lib/karafka/process.rb +24 -8
  239. data/lib/karafka/processing/coordinator.rb +181 -0
  240. data/lib/karafka/processing/coordinators_buffer.rb +62 -0
  241. data/lib/karafka/processing/executor.rb +155 -0
  242. data/lib/karafka/processing/executors_buffer.rb +72 -0
  243. data/lib/karafka/processing/expansions_selector.rb +22 -0
  244. data/lib/karafka/processing/inline_insights/consumer.rb +41 -0
  245. data/lib/karafka/processing/inline_insights/listener.rb +19 -0
  246. data/lib/karafka/processing/inline_insights/tracker.rb +128 -0
  247. data/lib/karafka/processing/jobs/base.rb +55 -0
  248. data/lib/karafka/processing/jobs/consume.rb +45 -0
  249. data/lib/karafka/processing/jobs/idle.rb +24 -0
  250. data/lib/karafka/processing/jobs/revoked.rb +22 -0
  251. data/lib/karafka/processing/jobs/shutdown.rb +23 -0
  252. data/lib/karafka/processing/jobs_builder.rb +28 -0
  253. data/lib/karafka/processing/jobs_queue.rb +150 -0
  254. data/lib/karafka/processing/partitioner.rb +24 -0
  255. data/lib/karafka/processing/result.rb +42 -0
  256. data/lib/karafka/processing/scheduler.rb +22 -0
  257. data/lib/karafka/processing/strategies/aj_dlq_mom.rb +44 -0
  258. data/lib/karafka/processing/strategies/aj_mom.rb +21 -0
  259. data/lib/karafka/processing/strategies/base.rb +52 -0
  260. data/lib/karafka/processing/strategies/default.rb +158 -0
  261. data/lib/karafka/processing/strategies/dlq.rb +88 -0
  262. data/lib/karafka/processing/strategies/dlq_mom.rb +49 -0
  263. data/lib/karafka/processing/strategies/mom.rb +29 -0
  264. data/lib/karafka/processing/strategy_selector.rb +47 -0
  265. data/lib/karafka/processing/worker.rb +93 -0
  266. data/lib/karafka/processing/workers_batch.rb +27 -0
  267. data/lib/karafka/railtie.rb +141 -0
  268. data/lib/karafka/routing/activity_manager.rb +84 -0
  269. data/lib/karafka/routing/builder.rb +45 -19
  270. data/lib/karafka/routing/consumer_group.rb +56 -20
  271. data/lib/karafka/routing/consumer_mapper.rb +1 -12
  272. data/lib/karafka/routing/features/active_job/builder.rb +33 -0
  273. data/lib/karafka/routing/features/active_job/config.rb +15 -0
  274. data/lib/karafka/routing/features/active_job/contracts/topic.rb +44 -0
  275. data/lib/karafka/routing/features/active_job/proxy.rb +14 -0
  276. data/lib/karafka/routing/features/active_job/topic.rb +33 -0
  277. data/lib/karafka/routing/features/active_job.rb +13 -0
  278. data/lib/karafka/routing/features/base/expander.rb +59 -0
  279. data/lib/karafka/routing/features/base.rb +71 -0
  280. data/lib/karafka/routing/features/dead_letter_queue/config.rb +19 -0
  281. data/lib/karafka/routing/features/dead_letter_queue/contracts/topic.rb +46 -0
  282. data/lib/karafka/routing/features/dead_letter_queue/topic.rb +41 -0
  283. data/lib/karafka/routing/features/dead_letter_queue.rb +16 -0
  284. data/lib/karafka/routing/features/declaratives/config.rb +18 -0
  285. data/lib/karafka/routing/features/declaratives/contracts/topic.rb +33 -0
  286. data/lib/karafka/routing/features/declaratives/topic.rb +44 -0
  287. data/lib/karafka/routing/features/declaratives.rb +14 -0
  288. data/lib/karafka/routing/features/inline_insights/config.rb +15 -0
  289. data/lib/karafka/routing/features/inline_insights/contracts/topic.rb +27 -0
  290. data/lib/karafka/routing/features/inline_insights/topic.rb +31 -0
  291. data/lib/karafka/routing/features/inline_insights.rb +40 -0
  292. data/lib/karafka/routing/features/manual_offset_management/config.rb +15 -0
  293. data/lib/karafka/routing/features/manual_offset_management/contracts/topic.rb +27 -0
  294. data/lib/karafka/routing/features/manual_offset_management/topic.rb +35 -0
  295. data/lib/karafka/routing/features/manual_offset_management.rb +18 -0
  296. data/lib/karafka/routing/proxy.rb +22 -21
  297. data/lib/karafka/routing/router.rb +24 -10
  298. data/lib/karafka/routing/subscription_group.rb +110 -0
  299. data/lib/karafka/routing/subscription_groups_builder.rb +65 -0
  300. data/lib/karafka/routing/topic.rb +87 -24
  301. data/lib/karafka/routing/topics.rb +46 -0
  302. data/lib/karafka/runner.rb +52 -0
  303. data/lib/karafka/serialization/json/deserializer.rb +7 -15
  304. data/lib/karafka/server.rb +113 -37
  305. data/lib/karafka/setup/attributes_map.rb +348 -0
  306. data/lib/karafka/setup/config.rb +256 -175
  307. data/lib/karafka/status.rb +54 -7
  308. data/lib/karafka/templates/example_consumer.rb.erb +16 -0
  309. data/lib/karafka/templates/karafka.rb.erb +33 -55
  310. data/lib/karafka/time_trackers/base.rb +14 -0
  311. data/lib/karafka/time_trackers/pause.rb +122 -0
  312. data/lib/karafka/time_trackers/poll.rb +69 -0
  313. data/lib/karafka/version.rb +1 -1
  314. data/lib/karafka.rb +91 -17
  315. data/renovate.json +9 -0
  316. data.tar.gz.sig +0 -0
  317. metadata +330 -168
  318. metadata.gz.sig +0 -0
  319. data/MIT-LICENCE +0 -18
  320. data/certs/mensfeld.pem +0 -25
  321. data/config/errors.yml +0 -41
  322. data/lib/karafka/assignment_strategies/round_robin.rb +0 -13
  323. data/lib/karafka/attributes_map.rb +0 -63
  324. data/lib/karafka/backends/inline.rb +0 -16
  325. data/lib/karafka/base_responder.rb +0 -226
  326. data/lib/karafka/cli/flow.rb +0 -48
  327. data/lib/karafka/cli/missingno.rb +0 -19
  328. data/lib/karafka/code_reloader.rb +0 -67
  329. data/lib/karafka/connection/api_adapter.rb +0 -158
  330. data/lib/karafka/connection/batch_delegator.rb +0 -55
  331. data/lib/karafka/connection/builder.rb +0 -23
  332. data/lib/karafka/connection/message_delegator.rb +0 -36
  333. data/lib/karafka/consumers/batch_metadata.rb +0 -10
  334. data/lib/karafka/consumers/callbacks.rb +0 -71
  335. data/lib/karafka/consumers/includer.rb +0 -64
  336. data/lib/karafka/consumers/responders.rb +0 -24
  337. data/lib/karafka/consumers/single_params.rb +0 -15
  338. data/lib/karafka/contracts/consumer_group_topic.rb +0 -19
  339. data/lib/karafka/contracts/responder_usage.rb +0 -54
  340. data/lib/karafka/fetcher.rb +0 -42
  341. data/lib/karafka/helpers/class_matcher.rb +0 -88
  342. data/lib/karafka/helpers/config_retriever.rb +0 -46
  343. data/lib/karafka/helpers/inflector.rb +0 -26
  344. data/lib/karafka/instrumentation/stdout_listener.rb +0 -140
  345. data/lib/karafka/params/batch_metadata.rb +0 -26
  346. data/lib/karafka/params/builders/batch_metadata.rb +0 -30
  347. data/lib/karafka/params/builders/params.rb +0 -38
  348. data/lib/karafka/params/builders/params_batch.rb +0 -25
  349. data/lib/karafka/params/params_batch.rb +0 -60
  350. data/lib/karafka/patches/ruby_kafka.rb +0 -47
  351. data/lib/karafka/persistence/client.rb +0 -29
  352. data/lib/karafka/persistence/consumers.rb +0 -45
  353. data/lib/karafka/persistence/topics.rb +0 -48
  354. data/lib/karafka/responders/builder.rb +0 -36
  355. data/lib/karafka/responders/topic.rb +0 -55
  356. data/lib/karafka/routing/topic_mapper.rb +0 -53
  357. data/lib/karafka/serialization/json/serializer.rb +0 -31
  358. data/lib/karafka/setup/configurators/water_drop.rb +0 -36
  359. data/lib/karafka/templates/application_responder.rb.erb +0 -11
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This Karafka component is a Pro component under a commercial license.
4
+ # This Karafka component is NOT licensed under LGPL.
5
+ #
6
+ # All of the commercial components are present in the lib/karafka/pro directory of this
7
+ # repository and their usage requires commercial license agreement.
8
+ #
9
+ # Karafka has also commercial-friendly license, commercial support and commercial components.
10
+ #
11
+ # By sending a pull request to the pro components, you are agreeing to transfer the copyright of
12
+ # your code to Maciej Mensfeld.
13
+
14
+ module Karafka
15
+ module Pro
16
+ # Out of the box encryption engine for both Karafka and WaterDrop
17
+ # It uses asymmetric encryption via RSA. We use asymmetric so we can have producers that won't
18
+ # have ability (when private key not added) to decrypt messages.
19
+ module Encryption
20
+ class << self
21
+ # Sets up additional config scope, validations and other things
22
+ #
23
+ # @param config [Karafka::Core::Configurable::Node] root node config
24
+ def pre_setup(config)
25
+ # Expand the config with this feature specific stuff
26
+ config.instance_eval do
27
+ setting(:encryption, default: Setup::Config.config)
28
+ end
29
+ end
30
+
31
+ # @param config [Karafka::Core::Configurable::Node] root node config
32
+ def post_setup(config)
33
+ Encryption::Contracts::Config.new.validate!(config.to_h)
34
+
35
+ # Don't inject extra components if encryption is not active
36
+ return unless config.encryption.active
37
+
38
+ # This parser is encryption aware
39
+ config.internal.messages.parser = Messages::Parser.new
40
+
41
+ # Encryption for WaterDrop
42
+ config.producer.middleware.append(Messages::Middleware.new)
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This Karafka component is a Pro component under a commercial license.
4
+ # This Karafka component is NOT licensed under LGPL.
5
+ #
6
+ # All of the commercial components are present in the lib/karafka/pro directory of this
7
+ # repository and their usage requires commercial license agreement.
8
+ #
9
+ # Karafka has also commercial-friendly license, commercial support and commercial components.
10
+ #
11
+ # By sending a pull request to the pro components, you are agreeing to transfer the copyright of
12
+ # your code to Maciej Mensfeld.
13
+
14
+ module Karafka
15
+ module Pro
16
+ class Iterator
17
+ # There are various ways you can provide topics information for iterating.
18
+ #
19
+ # This mapper normalizes this data, resolves offsets and maps the time based offsets into
20
+ # appropriate once
21
+ #
22
+ # Following formats are accepted:
23
+ #
24
+ # - 'topic1' - just a string with one topic name
25
+ # - ['topic1', 'topic2'] - just the names
26
+ # - { 'topic1' => -100 } - names with negative lookup offset
27
+ # - { 'topic1' => { 0 => 5 } } - names with exact partitions offsets
28
+ # - { 'topic1' => { 0 => -5 }, 'topic2' => { 1 => 5 } } - with per partition negative offsets
29
+ # - { 'topic1' => 100 } - means we run all partitions from the offset 100
30
+ # - { 'topic1' => Time.now - 60 } - we run all partitions from the message from 60s ago
31
+ # - { 'topic1' => { 1 => Time.now - 60 } } - partition1 from message 60s ago
32
+ #
33
+ class Expander
34
+ # Expands topics to which we want to subscribe with partitions information in case this
35
+ # info is not provided.
36
+ #
37
+ # @param topics [Array, Hash, String] topics definitions
38
+ # @return [Hash] expanded and normalized requested topics and partitions data
39
+ def call(topics)
40
+ expanded = Hash.new { |h, k| h[k] = {} }
41
+
42
+ normalize_format(topics).map do |topic, details|
43
+ if details.is_a?(Hash)
44
+ details.each do |partition, offset|
45
+ expanded[topic][partition] = offset
46
+ end
47
+ else
48
+ partition_count(topic).times do |partition|
49
+ # If no offsets are provided, we just start from zero
50
+ expanded[topic][partition] = details || 0
51
+ end
52
+ end
53
+ end
54
+
55
+ expanded
56
+ end
57
+
58
+ private
59
+
60
+ # Input can be provided in multiple formats. Here we normalize it to one (hash).
61
+ #
62
+ # @param topics [Array, Hash, String] requested topics
63
+ # @return [Hash] normalized hash with topics data
64
+ def normalize_format(topics)
65
+ # Simplification for the single topic case
66
+ topics = [topics] if topics.is_a?(String)
67
+
68
+ # If we've got just array with topics, we need to convert that into a representation
69
+ # that we can expand with offsets
70
+ topics = topics.map { |name| [name, false] }.to_h if topics.is_a?(Array)
71
+ # We remap by creating new hash, just in case the hash came as the argument for this
72
+ # expanded. We do not want to modify user provided hash
73
+ topics.transform_keys(&:to_s)
74
+ end
75
+
76
+ # List of topics with their partition information for expansion
77
+ # We cache it so we do not have to run consecutive requests to obtain data about multiple
78
+ # topics
79
+ def topics
80
+ @topics ||= Admin.cluster_info.topics
81
+ end
82
+
83
+ # @param name [String] topic name
84
+ # @return [Integer] number of partitions of the topic we want to iterate over
85
+ def partition_count(name)
86
+ topics
87
+ .find { |topic| topic.fetch(:topic_name) == name }
88
+ .tap { |topic| topic || raise(Errors::TopicNotFoundError, name) }
89
+ .fetch(:partitions)
90
+ .count
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,155 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This Karafka component is a Pro component under a commercial license.
4
+ # This Karafka component is NOT licensed under LGPL.
5
+ #
6
+ # All of the commercial components are present in the lib/karafka/pro directory of this
7
+ # repository and their usage requires commercial license agreement.
8
+ #
9
+ # Karafka has also commercial-friendly license, commercial support and commercial components.
10
+ #
11
+ # By sending a pull request to the pro components, you are agreeing to transfer the copyright of
12
+ # your code to Maciej Mensfeld.
13
+
14
+ module Karafka
15
+ module Pro
16
+ class Iterator
17
+ # Because we have various formats in which we can provide the offsets, before we can
18
+ # subscribe to them, there needs to be a bit of normalization.
19
+ #
20
+ # For some of the cases, we need to go to Kafka and get the real offsets or watermarks.
21
+ #
22
+ # This builder resolves that and builds a tpl to which we can safely subscribe the way
23
+ # we want it.
24
+ class TplBuilder
25
+ # @param consumer [::Rdkafka::Consumer] consumer instance needed to talk with Kafka
26
+ # @param expanded_topics [Hash] hash with expanded and normalized topics data
27
+ def initialize(consumer, expanded_topics)
28
+ @consumer = Connection::Proxy.new(consumer)
29
+ @expanded_topics = expanded_topics
30
+ @mapped_topics = Hash.new { |h, k| h[k] = {} }
31
+ end
32
+
33
+ # @return [Rdkafka::Consumer::TopicPartitionList] final tpl we can use to subscribe
34
+ def call
35
+ resolve_partitions_without_offsets
36
+ resolve_partitions_with_exact_offsets
37
+ resolve_partitions_with_negative_offsets
38
+ resolve_partitions_with_time_offsets
39
+
40
+ # Final tpl with all the data
41
+ tpl = Rdkafka::Consumer::TopicPartitionList.new
42
+
43
+ @mapped_topics.each do |name, partitions|
44
+ tpl.add_topic_and_partitions_with_offsets(name, partitions)
45
+ end
46
+
47
+ tpl
48
+ end
49
+
50
+ private
51
+
52
+ # First we expand on those partitions that do not have offsets defined.
53
+ # When we operate in case like this, we just start from beginning
54
+ def resolve_partitions_without_offsets
55
+ @expanded_topics.each do |name, partitions|
56
+ # We can here only about the case where we have partitions without offsets
57
+ next unless partitions.is_a?(Array) || partitions.is_a?(Range)
58
+
59
+ # When no offsets defined, we just start from zero
60
+ @mapped_topics[name] = partitions.map { |partition| [partition, 0] }.to_h
61
+ end
62
+ end
63
+
64
+ # If we get exact numeric offsets, we can just start from them without any extra work
65
+ def resolve_partitions_with_exact_offsets
66
+ @expanded_topics.each do |name, partitions|
67
+ next unless partitions.is_a?(Hash)
68
+
69
+ partitions.each do |partition, offset|
70
+ # Skip negative and time based offsets
71
+ next unless offset.is_a?(Integer) && offset >= 0
72
+
73
+ # Exact offsets can be used as they are
74
+ # No need for extra operations
75
+ @mapped_topics[name][partition] = offset
76
+ end
77
+ end
78
+ end
79
+
80
+ # If the offsets are negative, it means we want to fetch N last messages and we need to
81
+ # figure out the appropriate offsets
82
+ #
83
+ # We do it by getting the watermark offsets and just calculating it. This means that for
84
+ # heavily compacted topics, this may return less than the desired number but it is a
85
+ # limitation that is documented.
86
+ def resolve_partitions_with_negative_offsets
87
+ @expanded_topics.each do |name, partitions|
88
+ next unless partitions.is_a?(Hash)
89
+
90
+ partitions.each do |partition, offset|
91
+ # Care only about numerical offsets
92
+ #
93
+ # For time based we already resolve them via librdkafka lookup API
94
+ next unless offset.is_a?(Integer)
95
+
96
+ low_offset, high_offset = @consumer.query_watermark_offsets(name, partition)
97
+
98
+ # Care only about negative offsets (last n messages)
99
+ #
100
+ # We reject the above results but we **NEED** to run the `#query_watermark_offsets`
101
+ # for each topic partition nonetheless. Without this, librdkafka fetches a lot more
102
+ # metadata about each topic and each partition and this takes much more time than
103
+ # just getting watermarks. If we do not run watermark, at least an extra second
104
+ # is added at the beginning of iterator flow
105
+ #
106
+ # This may not be significant when this runs in the background but in case of
107
+ # using iterator in thins like Puma, it heavily impacts the end user experience
108
+ next unless offset.negative?
109
+
110
+ # We add because this offset is negative
111
+ @mapped_topics[name][partition] = [high_offset + offset, low_offset].max
112
+ end
113
+ end
114
+ end
115
+
116
+ # For time based offsets we first need to aggregate them and request the proper offsets.
117
+ # We want to get all times in one go for all tpls defined with times, so we accumulate
118
+ # them here and we will make one sync request to kafka for all.
119
+ def resolve_partitions_with_time_offsets
120
+ time_tpl = Rdkafka::Consumer::TopicPartitionList.new
121
+
122
+ # First we need to collect the time based once
123
+ @expanded_topics.each do |name, partitions|
124
+ next unless partitions.is_a?(Hash)
125
+
126
+ time_based = {}
127
+
128
+ partitions.each do |partition, offset|
129
+ next unless offset.is_a?(Time)
130
+
131
+ time_based[partition] = offset
132
+ end
133
+
134
+ next if time_based.empty?
135
+
136
+ time_tpl.add_topic_and_partitions_with_offsets(name, time_based)
137
+ end
138
+
139
+ # If there were no time-based, no need to query Kafka
140
+ return if time_tpl.empty?
141
+
142
+ real_offsets = @consumer.offsets_for_times(time_tpl)
143
+
144
+ real_offsets.to_h.each do |name, results|
145
+ results.each do |result|
146
+ raise(Errors::InvalidTimeBasedOffsetError) unless result
147
+
148
+ @mapped_topics[name][result.partition] = result.offset
149
+ end
150
+ end
151
+ end
152
+ end
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,170 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This Karafka component is a Pro component under a commercial license.
4
+ # This Karafka component is NOT licensed under LGPL.
5
+ #
6
+ # All of the commercial components are present in the lib/karafka/pro directory of this
7
+ # repository and their usage requires commercial license agreement.
8
+ #
9
+ # Karafka has also commercial-friendly license, commercial support and commercial components.
10
+ #
11
+ # By sending a pull request to the pro components, you are agreeing to transfer the copyright of
12
+ # your code to Maciej Mensfeld.
13
+
14
+ module Karafka
15
+ module Pro
16
+ # Topic iterator allows you to iterate over topic/partition data and perform lookups for
17
+ # information that you need.
18
+ #
19
+ # It supports early stops on finding the requested data and allows for seeking till
20
+ # the end. It also allows for signaling, when a given message should be last out of certain
21
+ # partition, but we still want to continue iterating in other messages.
22
+ #
23
+ # It does **not** create a consumer group and does not have any offset management.
24
+ class Iterator
25
+ # Local partition reference for librdkafka
26
+ Partition = Struct.new(:partition, :offset)
27
+
28
+ private_constant :Partition
29
+
30
+ # A simple API allowing to iterate over topic/partition data, without having to subscribe
31
+ # and deal with rebalances. This API allows for multi-partition streaming and is optimized
32
+ # for data lookups. It allows for explicit stopping iteration over any partition during
33
+ # the iteration process, allowing for optimized lookups.
34
+ #
35
+ # @param topics [Array<String>, Hash] list of strings if we want to subscribe to multiple
36
+ # topics and all of their partitions or a hash where keys are the topics and values are
37
+ # hashes with partitions and their initial offsets.
38
+ # @param settings [Hash] extra settings for the consumer. Please keep in mind, that if
39
+ # overwritten, you may want to include `auto.offset.reset` to match your case.
40
+ # @param yield_nil [Boolean] should we yield also `nil` values when poll returns nothing.
41
+ # Useful in particular for long-living iterators.
42
+ # @param max_wait_time [Integer] max wait in ms when iterator did not receive any messages
43
+ #
44
+ # @note It is worth keeping in mind, that this API also needs to operate within
45
+ # `max.poll.interval.ms` limitations on each iteration
46
+ #
47
+ # @note In case of a never-ending iterator, you need to set `enable.partition.eof` to `false`
48
+ # so we don't stop polling data even when reaching the end (end on a given moment)
49
+ def initialize(
50
+ topics,
51
+ settings: { 'auto.offset.reset': 'beginning' },
52
+ yield_nil: false,
53
+ max_wait_time: 200
54
+ )
55
+ @topics_with_partitions = Expander.new.call(topics)
56
+
57
+ @routing_topics = @topics_with_partitions.map do |name, _|
58
+ [name, ::Karafka::Routing::Router.find_or_initialize_by_name(name)]
59
+ end.to_h
60
+
61
+ @total_partitions = @topics_with_partitions.map(&:last).sum(&:count)
62
+
63
+ @stopped_partitions = 0
64
+
65
+ @settings = settings
66
+ @yield_nil = yield_nil
67
+ @max_wait_time = max_wait_time
68
+ end
69
+
70
+ # Iterates over requested topic partitions and yields the results with the iterator itself
71
+ # Iterator instance is yielded because one can run `stop_partition` to stop iterating over
72
+ # part of data. It is useful for scenarios where we are looking for some information in all
73
+ # the partitions but once we found it, given partition data is no longer needed and would
74
+ # only eat up resources.
75
+ def each
76
+ Admin.with_consumer(@settings) do |consumer|
77
+ tpl = TplBuilder.new(consumer, @topics_with_partitions).call
78
+ consumer.assign(tpl)
79
+
80
+ # We need this for self-referenced APIs like pausing
81
+ @current_consumer = consumer
82
+
83
+ # Stream data until we reach the end of all the partitions or until the end user
84
+ # indicates that they are done
85
+ until done?
86
+ message = poll
87
+
88
+ # Skip nils if not explicitly required
89
+ next if message.nil? && !@yield_nil
90
+
91
+ if message
92
+ @current_message = build_message(message)
93
+
94
+ yield(@current_message, self)
95
+ else
96
+ yield(nil, self)
97
+ end
98
+ end
99
+
100
+ @current_message = nil
101
+ @current_consumer = nil
102
+ end
103
+
104
+ # Reset so we can use the same iterator again if needed
105
+ @stopped_partitions = 0
106
+ end
107
+
108
+ # Stops the partition we're currently yielded into
109
+ def stop_current_partition
110
+ stop_partition(
111
+ @current_message.topic,
112
+ @current_message.partition
113
+ )
114
+ end
115
+
116
+ # Stops processing of a given partition
117
+ # We expect the partition to be provided because of a scenario, where there is a
118
+ # multi-partition iteration and we want to stop a different partition that the one that
119
+ # is currently yielded.
120
+ #
121
+ # We pause it forever and no longer work with it.
122
+ #
123
+ # @param name [String] topic name of which partition we want to stop
124
+ # @param partition [Integer] partition we want to stop processing
125
+ def stop_partition(name, partition)
126
+ @stopped_partitions += 1
127
+
128
+ @current_consumer.pause(
129
+ Rdkafka::Consumer::TopicPartitionList.new(
130
+ name => [Partition.new(partition, 0)]
131
+ )
132
+ )
133
+ end
134
+
135
+ private
136
+
137
+ # @return [Rdkafka::Consumer::Message, nil] message or nil if nothing to do
138
+ def poll
139
+ @current_consumer.poll(@max_wait_time)
140
+ rescue Rdkafka::RdkafkaError => e
141
+ # End of partition
142
+ if e.code == :partition_eof
143
+ @stopped_partitions += 1
144
+
145
+ retry
146
+ end
147
+
148
+ raise e
149
+ end
150
+
151
+ # Converts raw rdkafka message into Karafka message
152
+ #
153
+ # @param message [Rdkafka::Consumer::Message] raw rdkafka message
154
+ # @return [::Karafka::Messages::Message]
155
+ def build_message(message)
156
+ Messages::Builders::Message.call(
157
+ message,
158
+ @routing_topics.fetch(message.topic),
159
+ Time.now
160
+ )
161
+ end
162
+
163
+ # Do we have all the data we wanted or did every topic partition has reached eof.
164
+ # @return [Boolean]
165
+ def done?
166
+ @stopped_partitions >= @total_partitions
167
+ end
168
+ end
169
+ end
170
+ end
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This Karafka component is a Pro component under a commercial license.
4
+ # This Karafka component is NOT licensed under LGPL.
5
+ #
6
+ # All of the commercial components are present in the lib/karafka/pro directory of this
7
+ # repository and their usage requires commercial license agreement.
8
+ #
9
+ # Karafka has also commercial-friendly license, commercial support and commercial components.
10
+ #
11
+ # By sending a pull request to the pro components, you are agreeing to transfer the copyright of
12
+ # your code to Maciej Mensfeld.
13
+
14
+ module Karafka
15
+ module Pro
16
+ # Loader requires and loads all the pro components only when they are needed
17
+ class Loader
18
+ # There seems to be a conflict in between using two Zeitwerk instances and it makes lookups
19
+ # for nested namespaces instead of creating them.
20
+ # We require those not to deal with this and then all works as expected
21
+ FORCE_LOADED = %w[
22
+ active_job/dispatcher
23
+ processing/jobs/consume_non_blocking
24
+ processing/strategies/base
25
+ routing/features/base
26
+ encryption
27
+ encryption/cipher
28
+ encryption/setup/config
29
+ encryption/contracts/config
30
+ encryption/messages/parser
31
+ ].freeze
32
+
33
+ # Zeitwerk pro loader
34
+ # We need to have one per process, that's why it's set as a constant
35
+ PRO_LOADER = Zeitwerk::Loader.new
36
+
37
+ private_constant :PRO_LOADER
38
+
39
+ class << self
40
+ # Requires all the components without using them anywhere
41
+ def require_all
42
+ FORCE_LOADED.each { |file| require_relative(file) }
43
+
44
+ PRO_LOADER.push_dir(Karafka.core_root.join('pro'), namespace: Karafka::Pro)
45
+ PRO_LOADER.setup
46
+ PRO_LOADER.eager_load
47
+ end
48
+
49
+ # Loads all the pro components and configures them wherever it is expected
50
+ # @param config [Karafka::Core::Configurable::Node] app config that we can alter with pro
51
+ # components
52
+ def pre_setup_all(config)
53
+ features.each { |feature| feature.pre_setup(config) }
54
+
55
+ reconfigure(config)
56
+
57
+ load_topic_features
58
+ end
59
+
60
+ # Runs post setup features configuration operations
61
+ #
62
+ # @param config [Karafka::Core::Configurable::Node]
63
+ def post_setup_all(config)
64
+ features.each { |feature| feature.post_setup(config) }
65
+ end
66
+
67
+ private
68
+
69
+ # @return [Array<Module>] extra non-routing related pro features and routing components
70
+ # that need to have some special configuration stuff injected into config, etc
71
+ def features
72
+ [
73
+ Encryption,
74
+ Cleaner
75
+ ]
76
+ end
77
+
78
+ # Sets proper config options to use pro components
79
+ # @param config [::Karafka::Core::Configurable::Node] root config node
80
+ def reconfigure(config)
81
+ icfg = config.internal
82
+
83
+ icfg.cli.contract = Contracts::ServerCliOptions.new
84
+
85
+ icfg.processing.coordinator_class = Processing::Coordinator
86
+ icfg.processing.partitioner_class = Processing::Partitioner
87
+ icfg.processing.scheduler = Processing::Scheduler.new
88
+ icfg.processing.jobs_builder = Processing::JobsBuilder.new
89
+ icfg.processing.strategy_selector = Processing::StrategySelector.new
90
+
91
+ icfg.active_job.consumer_class = ActiveJob::Consumer
92
+ icfg.active_job.dispatcher = ActiveJob::Dispatcher.new
93
+ icfg.active_job.job_options_contract = ActiveJob::JobOptionsContract.new
94
+
95
+ config.monitor.subscribe(PerformanceTracker.instance)
96
+ end
97
+
98
+ # Loads the Pro features of Karafka
99
+ # @note Object space lookup is not the fastest but we do it once during boot, so it's ok
100
+ def load_topic_features
101
+ ::Karafka::Pro::Routing::Features::Base.load_all
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This Karafka component is a Pro component under a commercial license.
4
+ # This Karafka component is NOT licensed under LGPL.
5
+ #
6
+ # All of the commercial components are present in the lib/karafka/pro directory of this
7
+ # repository and their usage requires commercial license agreement.
8
+ #
9
+ # Karafka has also commercial-friendly license, commercial support and commercial components.
10
+ #
11
+ # By sending a pull request to the pro components, you are agreeing to transfer the copyright of
12
+ # your code to Maciej Mensfeld.
13
+
14
+ module Karafka
15
+ module Pro
16
+ # Tracker used to keep track of performance metrics
17
+ # It provides insights that can be used to optimize processing flow
18
+ class PerformanceTracker
19
+ include Singleton
20
+
21
+ # How many samples do we collect per topic partition
22
+ SAMPLES_COUNT = 200
23
+
24
+ private_constant :SAMPLES_COUNT
25
+
26
+ # Builds up nested concurrent hash for data tracking
27
+ def initialize
28
+ @processing_times = Concurrent::Map.new do |topics_hash, topic|
29
+ topics_hash.compute_if_absent(topic) do
30
+ Concurrent::Map.new do |partitions_hash, partition|
31
+ # This array does not have to be concurrent because we always access single
32
+ # partition data via instrumentation that operates in a single thread via consumer
33
+ partitions_hash.compute_if_absent(partition) { [] }
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ # @param topic [String]
40
+ # @param partition [Integer]
41
+ # @return [Float] p95 processing time of a single message from a single topic partition
42
+ def processing_time_p95(topic, partition)
43
+ values = @processing_times[topic][partition]
44
+
45
+ return 0 if values.empty?
46
+ return values.first if values.size == 1
47
+
48
+ percentile(0.95, values)
49
+ end
50
+
51
+ # @private
52
+ # @param event [Karafka::Core::Monitoring::Event] event details
53
+ # Tracks time taken to process a single message of a given topic partition
54
+ def on_consumer_consumed(event)
55
+ consumer = event[:caller]
56
+ messages = consumer.messages
57
+ topic = messages.metadata.topic
58
+ partition = messages.metadata.partition
59
+
60
+ samples = @processing_times[topic][partition]
61
+ samples << event[:time] / messages.count
62
+
63
+ return unless samples.size > SAMPLES_COUNT
64
+
65
+ samples.shift
66
+ end
67
+
68
+ private
69
+
70
+ # Computers the requested percentile out of provided values
71
+ # @param percentile [Float]
72
+ # @param values [Array<String>] all the values based on which we should
73
+ # @return [Float] computed percentile
74
+ def percentile(percentile, values)
75
+ values_sorted = values.sort
76
+
77
+ floor = (percentile * (values_sorted.length - 1) + 1).floor - 1
78
+ mod = (percentile * (values_sorted.length - 1) + 1).modulo(1)
79
+
80
+ values_sorted[floor] + (mod * (values_sorted[floor + 1] - values_sorted[floor]))
81
+ end
82
+ end
83
+ end
84
+ end