karafka 1.4.9 → 2.0.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (127) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/.github/FUNDING.yml +3 -0
  4. data/.github/workflows/ci.yml +78 -26
  5. data/.ruby-version +1 -1
  6. data/CHANGELOG.md +46 -0
  7. data/Gemfile +6 -0
  8. data/Gemfile.lock +39 -49
  9. data/LICENSE +14 -0
  10. data/LICENSE-COMM +89 -0
  11. data/LICENSE-LGPL +165 -0
  12. data/README.md +16 -48
  13. data/bin/benchmarks +85 -0
  14. data/bin/create_token +28 -0
  15. data/bin/integrations +160 -0
  16. data/bin/stress +13 -0
  17. data/certs/karafka-pro.pem +11 -0
  18. data/config/errors.yml +4 -38
  19. data/docker-compose.yml +11 -3
  20. data/karafka.gemspec +17 -17
  21. data/lib/active_job/consumer.rb +22 -0
  22. data/lib/active_job/karafka.rb +18 -0
  23. data/lib/active_job/queue_adapters/karafka_adapter.rb +29 -0
  24. data/lib/active_job/routing_extensions.rb +15 -0
  25. data/lib/karafka/app.rb +13 -20
  26. data/lib/karafka/base_consumer.rb +103 -34
  27. data/lib/karafka/cli/base.rb +4 -4
  28. data/lib/karafka/cli/info.rb +43 -8
  29. data/lib/karafka/cli/install.rb +3 -8
  30. data/lib/karafka/cli/server.rb +17 -30
  31. data/lib/karafka/cli.rb +4 -11
  32. data/lib/karafka/connection/client.rb +279 -93
  33. data/lib/karafka/connection/listener.rb +137 -38
  34. data/lib/karafka/connection/messages_buffer.rb +57 -0
  35. data/lib/karafka/connection/pauses_manager.rb +46 -0
  36. data/lib/karafka/connection/rebalance_manager.rb +62 -0
  37. data/lib/karafka/contracts/config.rb +25 -7
  38. data/lib/karafka/contracts/consumer_group.rb +0 -173
  39. data/lib/karafka/contracts/consumer_group_topic.rb +17 -7
  40. data/lib/karafka/contracts/server_cli_options.rb +1 -9
  41. data/lib/karafka/contracts.rb +1 -1
  42. data/lib/karafka/env.rb +46 -0
  43. data/lib/karafka/errors.rb +14 -18
  44. data/lib/karafka/helpers/multi_delegator.rb +2 -2
  45. data/lib/karafka/instrumentation/callbacks/error.rb +40 -0
  46. data/lib/karafka/instrumentation/callbacks/statistics.rb +42 -0
  47. data/lib/karafka/instrumentation/monitor.rb +14 -21
  48. data/lib/karafka/instrumentation/stdout_listener.rb +64 -91
  49. data/lib/karafka/instrumentation.rb +21 -0
  50. data/lib/karafka/licenser.rb +65 -0
  51. data/lib/karafka/{params → messages}/batch_metadata.rb +7 -13
  52. data/lib/karafka/messages/builders/batch_metadata.rb +30 -0
  53. data/lib/karafka/messages/builders/message.rb +38 -0
  54. data/lib/karafka/messages/builders/messages.rb +40 -0
  55. data/lib/karafka/{params/params.rb → messages/message.rb} +7 -12
  56. data/lib/karafka/messages/messages.rb +64 -0
  57. data/lib/karafka/{params → messages}/metadata.rb +4 -6
  58. data/lib/karafka/messages/seek.rb +9 -0
  59. data/lib/karafka/patches/rdkafka/consumer.rb +22 -0
  60. data/lib/karafka/processing/executor.rb +96 -0
  61. data/lib/karafka/processing/executors_buffer.rb +49 -0
  62. data/lib/karafka/processing/jobs/base.rb +18 -0
  63. data/lib/karafka/processing/jobs/consume.rb +28 -0
  64. data/lib/karafka/processing/jobs/revoked.rb +22 -0
  65. data/lib/karafka/processing/jobs/shutdown.rb +23 -0
  66. data/lib/karafka/processing/jobs_queue.rb +121 -0
  67. data/lib/karafka/processing/worker.rb +57 -0
  68. data/lib/karafka/processing/workers_batch.rb +22 -0
  69. data/lib/karafka/railtie.rb +65 -0
  70. data/lib/karafka/routing/builder.rb +15 -14
  71. data/lib/karafka/routing/consumer_group.rb +10 -18
  72. data/lib/karafka/routing/consumer_mapper.rb +1 -2
  73. data/lib/karafka/routing/router.rb +1 -1
  74. data/lib/karafka/routing/subscription_group.rb +53 -0
  75. data/lib/karafka/routing/subscription_groups_builder.rb +51 -0
  76. data/lib/karafka/routing/topic.rb +47 -25
  77. data/lib/karafka/runner.rb +59 -0
  78. data/lib/karafka/serialization/json/deserializer.rb +6 -15
  79. data/lib/karafka/server.rb +62 -25
  80. data/lib/karafka/setup/config.rb +86 -159
  81. data/lib/karafka/status.rb +13 -3
  82. data/lib/karafka/templates/example_consumer.rb.erb +16 -0
  83. data/lib/karafka/templates/karafka.rb.erb +14 -50
  84. data/lib/karafka/time_trackers/base.rb +19 -0
  85. data/lib/karafka/time_trackers/pause.rb +84 -0
  86. data/lib/karafka/time_trackers/poll.rb +65 -0
  87. data/lib/karafka/version.rb +1 -1
  88. data/lib/karafka.rb +30 -13
  89. data.tar.gz.sig +0 -0
  90. metadata +78 -108
  91. metadata.gz.sig +0 -0
  92. data/MIT-LICENCE +0 -18
  93. data/lib/karafka/assignment_strategies/round_robin.rb +0 -13
  94. data/lib/karafka/attributes_map.rb +0 -63
  95. data/lib/karafka/backends/inline.rb +0 -16
  96. data/lib/karafka/base_responder.rb +0 -226
  97. data/lib/karafka/cli/flow.rb +0 -48
  98. data/lib/karafka/cli/missingno.rb +0 -19
  99. data/lib/karafka/code_reloader.rb +0 -67
  100. data/lib/karafka/connection/api_adapter.rb +0 -158
  101. data/lib/karafka/connection/batch_delegator.rb +0 -55
  102. data/lib/karafka/connection/builder.rb +0 -23
  103. data/lib/karafka/connection/message_delegator.rb +0 -36
  104. data/lib/karafka/consumers/batch_metadata.rb +0 -10
  105. data/lib/karafka/consumers/callbacks.rb +0 -71
  106. data/lib/karafka/consumers/includer.rb +0 -64
  107. data/lib/karafka/consumers/responders.rb +0 -24
  108. data/lib/karafka/consumers/single_params.rb +0 -15
  109. data/lib/karafka/contracts/responder_usage.rb +0 -54
  110. data/lib/karafka/fetcher.rb +0 -42
  111. data/lib/karafka/helpers/class_matcher.rb +0 -88
  112. data/lib/karafka/helpers/config_retriever.rb +0 -46
  113. data/lib/karafka/helpers/inflector.rb +0 -26
  114. data/lib/karafka/params/builders/batch_metadata.rb +0 -30
  115. data/lib/karafka/params/builders/params.rb +0 -38
  116. data/lib/karafka/params/builders/params_batch.rb +0 -25
  117. data/lib/karafka/params/params_batch.rb +0 -60
  118. data/lib/karafka/patches/ruby_kafka.rb +0 -47
  119. data/lib/karafka/persistence/client.rb +0 -29
  120. data/lib/karafka/persistence/consumers.rb +0 -45
  121. data/lib/karafka/persistence/topics.rb +0 -48
  122. data/lib/karafka/responders/builder.rb +0 -36
  123. data/lib/karafka/responders/topic.rb +0 -55
  124. data/lib/karafka/routing/topic_mapper.rb +0 -53
  125. data/lib/karafka/serialization/json/serializer.rb +0 -31
  126. data/lib/karafka/setup/configurators/water_drop.rb +0 -36
  127. data/lib/karafka/templates/application_responder.rb.erb +0 -11
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Karafka
4
+ module Connection
5
+ # Partitions pauses management abstraction layer.
6
+ # It aggregates all the pauses for all the partitions that we're working with.
7
+ class PausesManager
8
+ # @return [Karafka::Connection::PausesManager] pauses manager
9
+ def initialize
10
+ @pauses = Hash.new do |h, k|
11
+ h[k] = {}
12
+ end
13
+ end
14
+
15
+ # Creates or fetches pause of a given topic partition.
16
+ #
17
+ # @param topic [String] topic name
18
+ # @param partition [Integer] partition number
19
+ # @return [Karafka::TimeTrackers::Pause] pause instance
20
+ def fetch(topic, partition)
21
+ @pauses[topic][partition] ||= TimeTrackers::Pause.new(
22
+ timeout: Karafka::App.config.pause_timeout,
23
+ max_timeout: Karafka::App.config.pause_max_timeout,
24
+ exponential_backoff: Karafka::App.config.pause_with_exponential_backoff
25
+ )
26
+ end
27
+
28
+ # Resumes processing of partitions for which pause time has ended.
29
+ #
30
+ # @yieldparam [String] topic name
31
+ # @yieldparam [Integer] partition number
32
+ def resume
33
+ @pauses.each do |topic, partitions|
34
+ partitions.each do |partition, pause|
35
+ next unless pause.paused?
36
+ next unless pause.expired?
37
+
38
+ pause.resume
39
+
40
+ yield(topic, partition)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Karafka
4
+ module Connection
5
+ # Manager for tracking changes in the partitions assignment.
6
+ #
7
+ # We need tracking of those to clean up consumers that will no longer process given partitions
8
+ # as they were taken away.
9
+ #
10
+ # @note Since this does not happen really often, we try to stick with same objects for the
11
+ # empty states most of the time, so we don't create many objects during the manager life
12
+ class RebalanceManager
13
+ # @return [RebalanceManager]
14
+ def initialize
15
+ @assigned = {}
16
+ @revoked = {}
17
+ end
18
+
19
+ # @return [Hash<String, Array<Integer>>] hash where the keys are the names of topics for
20
+ # which we've got new partitions assigned and array with ids of the partitions as the value
21
+ # @note Once assigned partitions are fetched, the state will be reset since the callbacks
22
+ # for new assigned partitions are set only during a state change
23
+ def assigned_partitions
24
+ return @assigned if @assigned.empty?
25
+
26
+ result = @assigned.dup
27
+ @assigned.clear
28
+ result
29
+ end
30
+
31
+ # @return [Hash<String, Array<Integer>>] hash where the keys are the names of topics for
32
+ # which we've lost partitions and array with ids of the partitions as the value
33
+ # @note Once revoked partitions are fetched, the state will be reset since the callbacks
34
+ # for new revoked partitions are set only during a state change
35
+ def revoked_partitions
36
+ return @revoked if @revoked.empty?
37
+
38
+ result = @revoked.dup
39
+ @revoked.clear
40
+ result
41
+ end
42
+
43
+ # Callback that kicks in inside of rdkafka, when new partitions are assigned.
44
+ #
45
+ # @private
46
+ # @param _ [Rdkafka::Consumer]
47
+ # @param partitions [Rdkafka::Consumer::TopicPartitionList]
48
+ def on_partitions_assigned(_, partitions)
49
+ @assigned = partitions.to_h.transform_values { |part| part.map(&:partition) }
50
+ end
51
+
52
+ # Callback that kicks in inside of rdkafka, when partitions are revoked.
53
+ #
54
+ # @private
55
+ # @param _ [Rdkafka::Consumer]
56
+ # @param partitions [Rdkafka::Consumer::TopicPartitionList]
57
+ def on_partitions_revoked(_, partitions)
58
+ @revoked = partitions.to_h.transform_values { |part| part.map(&:partition) }
59
+ end
60
+ end
61
+ end
62
+ end
@@ -2,19 +2,37 @@
2
2
 
3
3
  module Karafka
4
4
  module Contracts
5
- # Contract with validation rules for Karafka configuration details
5
+ # Contract with validation rules for Karafka configuration details.
6
+ #
6
7
  # @note There are many more configuration options inside of the
7
- # Karafka::Setup::Config model, but we don't validate them here as they are
8
+ # `Karafka::Setup::Config` model, but we don't validate them here as they are
8
9
  # validated per each route (topic + consumer_group) because they can be overwritten,
9
- # so we validate all of that once all the routes are defined and ready
10
+ # so we validate all of that once all the routes are defined and ready.
10
11
  class Config < Dry::Validation::Contract
12
+ config.messages.load_paths << File.join(Karafka.gem_root, 'config', 'errors.yml')
13
+
11
14
  params do
15
+ # License validity happens in the licenser. Here we do only the simple consistency checks
16
+ required(:license).schema do
17
+ required(:token) { bool? | str? }
18
+ required(:entity) { str? }
19
+ required(:expires_on) { date? }
20
+ end
21
+
12
22
  required(:client_id).filled(:str?, format?: Karafka::Contracts::TOPIC_REGEXP)
13
- required(:shutdown_timeout) { (int? & gt?(0)) }
14
- required(:consumer_mapper)
15
- required(:topic_mapper)
23
+ required(:concurrency) { int? & gt?(0) }
24
+ required(:consumer_mapper).filled
25
+ required(:consumer_persistence).filled(:bool?)
26
+ required(:pause_timeout) { int? & gt?(0) }
27
+ required(:pause_max_timeout) { int? & gt?(0) }
28
+ required(:pause_with_exponential_backoff).filled(:bool?)
29
+ required(:shutdown_timeout) { int? & gt?(0) }
30
+ end
16
31
 
17
- optional(:backend).filled
32
+ rule(:pause_timeout, :pause_max_timeout) do
33
+ if values[:pause_timeout].to_i > values[:pause_max_timeout].to_i
34
+ key(:pause_timeout).failure(:max_timeout_vs_pause_max_timeout)
35
+ end
18
36
  end
19
37
  end
20
38
  end
@@ -6,14 +6,6 @@ module Karafka
6
6
  class ConsumerGroup < Dry::Validation::Contract
7
7
  config.messages.load_paths << File.join(Karafka.gem_root, 'config', 'errors.yml')
8
8
 
9
- # Valid uri schemas of Kafka broker url
10
- # The ||= is due to the behavior of require_all that resolves dependencies
11
- # but sometimes loads things twice
12
- URI_SCHEMES ||= %w[kafka kafka+ssl plaintext ssl].freeze
13
-
14
- # Available sasl scram mechanism of authentication (plus nil)
15
- SASL_SCRAM_MECHANISMS ||= %w[sha256 sha512].freeze
16
-
17
9
  # Internal contract for sub-validating topics schema
18
10
  TOPIC_CONTRACT = ConsumerGroupTopic.new.freeze
19
11
 
@@ -22,58 +14,6 @@ module Karafka
22
14
  params do
23
15
  required(:id).filled(:str?, format?: Karafka::Contracts::TOPIC_REGEXP)
24
16
  required(:topics).value(:array, :filled?)
25
- required(:seed_brokers).value(:array, :filled?)
26
- required(:session_timeout).filled { int? | float? }
27
- required(:pause_timeout).maybe(%i[integer float]) { filled? > gteq?(0) }
28
- required(:pause_max_timeout).maybe(%i[integer float]) { filled? > gteq?(0) }
29
- required(:pause_exponential_backoff).filled(:bool?)
30
- required(:offset_commit_interval) { int? | float? }
31
- required(:offset_commit_threshold).filled(:int?)
32
- required(:offset_retention_time).maybe(:integer)
33
- required(:heartbeat_interval).filled { (int? | float?) & gteq?(0) }
34
- required(:fetcher_max_queue_size).filled(:int?, gt?: 0)
35
- required(:assignment_strategy).value(:any)
36
- required(:connect_timeout).filled { (int? | float?) & gt?(0) }
37
- required(:reconnect_timeout).filled { (int? | float?) & gteq?(0) }
38
- required(:socket_timeout).filled { (int? | float?) & gt?(0) }
39
- required(:min_bytes).filled(:int?, gt?: 0)
40
- required(:max_bytes).filled(:int?, gt?: 0)
41
- required(:max_wait_time).filled { (int? | float?) & gteq?(0) }
42
- required(:batch_fetching).filled(:bool?)
43
-
44
- %i[
45
- ssl_ca_cert
46
- ssl_ca_cert_file_path
47
- ssl_client_cert
48
- ssl_client_cert_key
49
- ssl_client_cert_chain
50
- ssl_client_cert_key_password
51
- sasl_gssapi_principal
52
- sasl_gssapi_keytab
53
- sasl_plain_authzid
54
- sasl_plain_username
55
- sasl_plain_password
56
- sasl_scram_username
57
- sasl_scram_password
58
- ].each do |encryption_attribute|
59
- optional(encryption_attribute).maybe(:str?)
60
- end
61
-
62
- optional(:ssl_verify_hostname).maybe(:bool?)
63
- optional(:ssl_ca_certs_from_system).maybe(:bool?)
64
- optional(:sasl_over_ssl).maybe(:bool?)
65
- optional(:sasl_oauth_token_provider).value(:any)
66
-
67
- # It's not with other encryptions as it has some more rules
68
- optional(:sasl_scram_mechanism)
69
- .maybe(:str?, included_in?: SASL_SCRAM_MECHANISMS)
70
- end
71
-
72
- # Uri rule to check if uri is in a Karafka acceptable format
73
- rule(:seed_brokers) do
74
- if value.is_a?(Array) && !value.all?(&method(:kafka_uri?))
75
- key.failure(:invalid_broker_schema)
76
- end
77
17
  end
78
18
 
79
19
  rule(:topics) do
@@ -93,119 +33,6 @@ module Karafka
93
33
  end
94
34
  end
95
35
  end
96
-
97
- rule(:assignment_strategy) do
98
- key.failure(:does_not_respond_to_call) unless value.respond_to?(:call)
99
- end
100
-
101
- rule(:ssl_client_cert, :ssl_client_cert_key) do
102
- if values[:ssl_client_cert] && !values[:ssl_client_cert_key]
103
- key(:ssl_client_cert_key).failure(:ssl_client_cert_with_ssl_client_cert_key)
104
- end
105
- end
106
-
107
- rule(:ssl_client_cert, :ssl_client_cert_key) do
108
- if values[:ssl_client_cert_key] && !values[:ssl_client_cert]
109
- key(:ssl_client_cert).failure(:ssl_client_cert_key_with_ssl_client_cert)
110
- end
111
- end
112
-
113
- rule(:ssl_client_cert, :ssl_client_cert_chain) do
114
- if values[:ssl_client_cert_chain] && !values[:ssl_client_cert]
115
- key(:ssl_client_cert).failure(:ssl_client_cert_chain_with_ssl_client_cert)
116
- end
117
- end
118
-
119
- rule(:ssl_client_cert_chain, :ssl_client_cert_key) do
120
- if values[:ssl_client_cert_chain] && !values[:ssl_client_cert]
121
- key(:ssl_client_cert).failure(:ssl_client_cert_chain_with_ssl_client_cert_key)
122
- end
123
- end
124
-
125
- rule(:ssl_client_cert_key_password, :ssl_client_cert_key) do
126
- if values[:ssl_client_cert_key_password] && !values[:ssl_client_cert_key]
127
- key(:ssl_client_cert_key).failure(:ssl_client_cert_key_password_with_ssl_client_cert_key)
128
- end
129
- end
130
-
131
- rule(:ssl_ca_cert) do
132
- key.failure(:invalid_certificate) if value && !valid_certificate?(value)
133
- end
134
-
135
- rule(:ssl_client_cert) do
136
- key.failure(:invalid_certificate) if value && !valid_certificate?(value)
137
- end
138
-
139
- rule(:ssl_ca_cert_file_path) do
140
- if value
141
- if File.exist?(value)
142
- key.failure(:invalid_certificate_from_path) unless valid_certificate?(File.read(value))
143
- else
144
- key.failure(:does_not_exist)
145
- end
146
- end
147
- end
148
-
149
- rule(:ssl_client_cert_key) do
150
- key.failure(:invalid_private_key) if value && !valid_private_key?(value)
151
- end
152
-
153
- rule(:ssl_client_cert_chain) do
154
- key.failure(:invalid_certificate) if value && !valid_certificate?(value)
155
- end
156
-
157
- rule(:sasl_oauth_token_provider) do
158
- key.failure(:does_not_respond_to_token) if value && !value.respond_to?(:token)
159
- end
160
-
161
- rule(:max_wait_time, :socket_timeout) do
162
- max_wait_time = values[:max_wait_time]
163
- socket_timeout = values[:socket_timeout]
164
-
165
- if socket_timeout.is_a?(Numeric) &&
166
- max_wait_time.is_a?(Numeric) &&
167
- max_wait_time > socket_timeout
168
-
169
- key(:max_wait_time).failure(:max_wait_time_limit)
170
- end
171
- end
172
-
173
- rule(:pause_timeout, :pause_max_timeout, :pause_exponential_backoff) do
174
- if values[:pause_exponential_backoff]
175
- if values[:pause_timeout].to_i > values[:pause_max_timeout].to_i
176
- key(:pause_max_timeout).failure(:max_timeout_size_for_exponential)
177
- end
178
- end
179
- end
180
-
181
- private
182
-
183
- # @param value [String] potential RSA key value
184
- # @return [Boolean] is the given string a valid RSA key
185
- def valid_private_key?(value)
186
- OpenSSL::PKey.read(value)
187
- true
188
- rescue OpenSSL::PKey::PKeyError
189
- false
190
- end
191
-
192
- # @param value [String] potential X509 cert value
193
- # @return [Boolean] is the given string a valid X509 cert
194
- def valid_certificate?(value)
195
- OpenSSL::X509::Certificate.new(value)
196
- true
197
- rescue OpenSSL::X509::CertificateError
198
- false
199
- end
200
-
201
- # @param value [String] potential kafka uri
202
- # @return [Boolean] true if it is a kafka uri, otherwise false
203
- def kafka_uri?(value)
204
- uri = URI.parse(value)
205
- URI_SCHEMES.include?(uri.scheme) && uri.port
206
- rescue URI::InvalidURIError
207
- false
208
- end
209
36
  end
210
37
  end
211
38
  end
@@ -2,17 +2,27 @@
2
2
 
3
3
  module Karafka
4
4
  module Contracts
5
- # Consumer group topic validation rules
5
+ # Consumer group topic validation rules.
6
6
  class ConsumerGroupTopic < Dry::Validation::Contract
7
+ config.messages.load_paths << File.join(Karafka.gem_root, 'config', 'errors.yml')
8
+
7
9
  params do
8
- required(:id).filled(:str?, format?: Karafka::Contracts::TOPIC_REGEXP)
9
- required(:name).filled(:str?, format?: Karafka::Contracts::TOPIC_REGEXP)
10
- required(:backend).filled(included_in?: %i[inline sidekiq])
11
10
  required(:consumer).filled
12
11
  required(:deserializer).filled
13
- required(:max_bytes_per_partition).filled(:int?, gteq?: 0)
14
- required(:start_from_beginning).filled(:bool?)
15
- required(:batch_consuming).filled(:bool?)
12
+ required(:id).filled(:str?, format?: Karafka::Contracts::TOPIC_REGEXP)
13
+ required(:kafka).filled
14
+ required(:max_messages) { int? & gteq?(1) }
15
+ required(:max_wait_time).filled { int? & gteq?(10) }
16
+ required(:manual_offset_management).filled(:bool?)
17
+ required(:name).filled(:str?, format?: Karafka::Contracts::TOPIC_REGEXP)
18
+ end
19
+
20
+ rule(:kafka) do
21
+ # This will trigger rdkafka validations that we catch and re-map the info and use dry
22
+ # compatible format
23
+ Rdkafka::Config.new(value).send(:native_config)
24
+ rescue Rdkafka::Config::ConfigError => e
25
+ key(:kafka).failure(e.message)
16
26
  end
17
27
  end
18
28
  end
@@ -2,22 +2,14 @@
2
2
 
3
3
  module Karafka
4
4
  module Contracts
5
- # Contract for validating correctness of the server cli command options
6
- # We validate some basics + the list of consumer_groups on which we want to use, to make
7
- # sure that all of them are defined, plus that a pidfile does not exist
5
+ # Contract for validating correctness of the server cli command options.
8
6
  class ServerCliOptions < Dry::Validation::Contract
9
7
  config.messages.load_paths << File.join(Karafka.gem_root, 'config', 'errors.yml')
10
8
 
11
9
  params do
12
- optional(:pid).filled(:str?)
13
- optional(:daemon).filled(:bool?)
14
10
  optional(:consumer_groups).value(:array, :filled?)
15
11
  end
16
12
 
17
- rule(:pid) do
18
- key(:pid).failure(:pid_already_exists) if value && File.exist?(value)
19
- end
20
-
21
13
  rule(:consumer_groups) do
22
14
  # If there were no consumer_groups declared in the server cli, it means that we will
23
15
  # run all of them and no need to validate them here at all
@@ -5,6 +5,6 @@ module Karafka
5
5
  module Contracts
6
6
  # Regexp for validating format of groups and topics
7
7
  # @note It is not nested inside of the contracts, as it is used by couple of them
8
- TOPIC_REGEXP = /\A(\w|-|\.)+\z/.freeze
8
+ TOPIC_REGEXP = /\A(\w|-|\.)+\z/
9
9
  end
10
10
  end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Karafka
4
+ # Env management class to get and set environment for Karafka
5
+ class Env < String
6
+ # Keys where we look for environment details for Karafka
7
+ LOOKUP_ENV_KEYS = %w[
8
+ KARAFKA_ENV
9
+ RACK_ENV
10
+ RAILS_ENV
11
+ ].freeze
12
+
13
+ # Default fallback env
14
+ DEFAULT_ENV = 'development'
15
+
16
+ private_constant :LOOKUP_ENV_KEYS, :DEFAULT_ENV
17
+
18
+ # @return [Karafka::Env] env object
19
+ # @note Will load appropriate environment automatically
20
+ def initialize
21
+ super('')
22
+
23
+ LOOKUP_ENV_KEYS
24
+ .map { |key| ENV[key] }
25
+ .compact
26
+ .first
27
+ .then { |env| env || DEFAULT_ENV }
28
+ .then { |env| replace(env) }
29
+ end
30
+
31
+ # @param method_name [String] method name
32
+ # @param include_private [Boolean] should we include private methods as well
33
+ # @return [Boolean] true if we respond to a given missing method, otherwise false
34
+ def respond_to_missing?(method_name, include_private = false)
35
+ (method_name[-1] == '?') || super
36
+ end
37
+
38
+ # Reacts to missing methods, from which some might be the env checks.
39
+ # If the method ends with '?' we assume, that it is an env check
40
+ # @param method_name [String] method name for missing or env name with question mark
41
+ # @param arguments [Array] any arguments that we pass to the method
42
+ def method_missing(method_name, *arguments)
43
+ method_name[-1] == '?' ? self == method_name[0..-2] : super
44
+ end
45
+ end
46
+ end
@@ -6,12 +6,6 @@ module Karafka
6
6
  # Base class for all the Karafka internal errors
7
7
  BaseError = Class.new(StandardError)
8
8
 
9
- # Should be raised when we have that that we cannot serialize
10
- SerializationError = Class.new(BaseError)
11
-
12
- # Should be raised when we tried to deserialize incoming data but we failed
13
- DeserializationError = Class.new(BaseError)
14
-
15
9
  # Raised when router receives topic name which does not correspond with any routes
16
10
  # This can only happen in a case when:
17
11
  # - you've received a message and we cannot match it with a consumer
@@ -24,28 +18,30 @@ module Karafka
24
18
  # @see https://github.com/karafka/karafka/issues/135
25
19
  NonMatchingRouteError = Class.new(BaseError)
26
20
 
27
- # Raised when we don't use or use responder not in the way it expected to based on the
28
- # topics usage definitions
29
- InvalidResponderUsageError = Class.new(BaseError)
30
-
31
- # Raised when options that we provide to the responder to respond aren't what the contract
32
- # requires
33
- InvalidResponderMessageOptionsError = Class.new(BaseError)
34
-
35
21
  # Raised when configuration doesn't match with validation contract
36
22
  InvalidConfigurationError = Class.new(BaseError)
37
23
 
38
24
  # Raised when we try to use Karafka CLI commands (except install) without a boot file
39
25
  MissingBootFileError = Class.new(BaseError)
40
26
 
41
- # Raised when we want to read a persisted thread messages consumer but it is unavailable
42
- # This should never happen and if it does, please contact us
43
- MissingClientError = Class.new(BaseError)
44
-
45
27
  # Raised when want to hook up to an event that is not registered and supported
46
28
  UnregisteredMonitorEventError = Class.new(BaseError)
47
29
 
48
30
  # Raised when we've waited enough for shutting down a non-responsive process
49
31
  ForcefulShutdownError = Class.new(BaseError)
32
+
33
+ # Raised when the jobs queue receives a job that should not be received as it would cause
34
+ # the processing to go out of sync. We should never process in parallel data from the same
35
+ # topic partition (unless virtual partitions apply)
36
+ JobsQueueSynchronizationError = Class.new(BaseError)
37
+
38
+ # Raised when given topic is not found while expected
39
+ TopicNotFoundError = Class.new(BaseError)
40
+
41
+ # This should never happen. Please open an issue if it does.
42
+ UnsupportedCaseError = Class.new(BaseError)
43
+
44
+ # Raised when the license token is not valid
45
+ InvalidLicenseTokenError = Class.new(BaseError)
50
46
  end
51
47
  end
@@ -1,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Karafka
4
- # Module containing classes and methods that provide some additional functionalities
4
+ # Module containing classes and methods that provide some additional helper functionalities.
5
5
  module Helpers
6
6
  # @note Taken from http://stackoverflow.com/questions/6407141
7
- # Multidelegator is used to delegate calls to multiple targets
7
+ # Multidelegator is used to delegate calls to multiple targets.
8
8
  class MultiDelegator
9
9
  # @param targets to which we want to delegate methods
10
10
  def initialize(*targets)
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Karafka
4
+ module Instrumentation
5
+ # Callbacks used to transport things from rdkafka
6
+ module Callbacks
7
+ # Callback that kicks in when consumer error occurs and is published in a background thread
8
+ class Error
9
+ # @param subscription_group_id [String] id of the current subscription group instance
10
+ # @param consumer_group_id [String] id of the current consumer group
11
+ # @param client_name [String] rdkafka client name
12
+ # @param monitor [WaterDrop::Instrumentation::Monitor] monitor we are using
13
+ def initialize(subscription_group_id, consumer_group_id, client_name, monitor)
14
+ @subscription_group_id = subscription_group_id
15
+ @consumer_group_id = consumer_group_id
16
+ @client_name = client_name
17
+ @monitor = monitor
18
+ end
19
+
20
+ # Runs the instrumentation monitor with error
21
+ # @param client_name [String] rdkafka client name
22
+ # @param error [Rdkafka::Error] error that occurred
23
+ # @note It will only instrument on errors of the client of our consumer
24
+ def call(client_name, error)
25
+ # Emit only errors related to our client
26
+ # Same as with statistics (mor explanation there)
27
+ return unless @client_name == client_name
28
+
29
+ @monitor.instrument(
30
+ 'error.occurred',
31
+ subscription_group_id: @subscription_group_id,
32
+ consumer_group_id: @consumer_group_id,
33
+ type: 'librdkafka.error',
34
+ error: error
35
+ )
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Karafka
4
+ module Instrumentation
5
+ module Callbacks
6
+ # Statistics callback handler
7
+ # @see `WaterDrop::Instrumentation::Callbacks::Statistics` for details on why we decorate
8
+ # those statistics
9
+ class Statistics
10
+ # @param subscription_group_id [String] id of the current subscription group
11
+ # @param consumer_group_id [String] id of the current consumer group
12
+ # @param client_name [String] rdkafka client name
13
+ # @param monitor [WaterDrop::Instrumentation::Monitor] monitor we are using
14
+ def initialize(subscription_group_id, consumer_group_id, client_name, monitor)
15
+ @subscription_group_id = subscription_group_id
16
+ @consumer_group_id = consumer_group_id
17
+ @client_name = client_name
18
+ @monitor = monitor
19
+ # We decorate both Karafka and WaterDrop statistics the same way
20
+ @statistics_decorator = ::WaterDrop::Instrumentation::Callbacks::StatisticsDecorator.new
21
+ end
22
+
23
+ # Emits decorated statistics to the monitor
24
+ # @param statistics [Hash] rdkafka statistics
25
+ def call(statistics)
26
+ # Emit only statistics related to our client
27
+ # rdkafka does not have per-instance statistics hook, thus we need to make sure that we
28
+ # emit only stats that are related to current producer. Otherwise we would emit all of
29
+ # all the time.
30
+ return unless @client_name == statistics['name']
31
+
32
+ @monitor.instrument(
33
+ 'statistics.emitted',
34
+ subscription_group_id: @subscription_group_id,
35
+ consumer_group_id: @consumer_group_id,
36
+ statistics: @statistics_decorator.call(statistics)
37
+ )
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -16,31 +16,24 @@ module Karafka
16
16
  # @note Depending on Karafka extensions and additional engines, this might not be the
17
17
  # complete list of all the events. Please use the #available_events on fully loaded
18
18
  # Karafka system to determine all of the events you can use.
19
- # Last 4 events are from WaterDrop but for convenience we use the same monitor for the
20
- # whole karafka ecosystem
21
19
  BASE_EVENTS = %w[
22
- params.params.deserialize
23
- params.params.deserialize.error
24
- connection.listener.before_fetch_loop
25
- connection.listener.fetch_loop
26
- connection.listener.fetch_loop.error
27
- connection.client.fetch_loop.error
28
- connection.batch_delegator.call
29
- connection.message_delegator.call
30
- fetcher.call.error
31
- backends.inline.process
32
- process.notice_signal
33
- consumers.responders.respond_with
34
- async_producer.call.error
35
- async_producer.call.retry
36
- sync_producer.call.error
37
- sync_producer.call.retry
38
- app.initializing
39
- app.initialized
40
20
  app.running
41
21
  app.stopping
42
- app.stopping.error
43
22
  app.stopped
23
+
24
+ consumer.consume
25
+ consumer.revoked
26
+ consumer.shutdown
27
+
28
+ process.notice_signal
29
+
30
+ connection.listener.before_fetch_loop
31
+ connection.listener.fetch_loop
32
+ connection.listener.fetch_loop.received
33
+
34
+ statistics.emitted
35
+
36
+ error.occurred
44
37
  ].freeze
45
38
 
46
39
  private_constant :BASE_EVENTS