karafka 1.4.10 → 2.0.0.alpha2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (137) 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 +79 -26
  5. data/.ruby-version +1 -1
  6. data/CHANGELOG.md +46 -0
  7. data/CONTRIBUTING.md +6 -6
  8. data/Gemfile +6 -0
  9. data/Gemfile.lock +45 -53
  10. data/LICENSE +17 -0
  11. data/LICENSE-COMM +89 -0
  12. data/LICENSE-LGPL +165 -0
  13. data/README.md +16 -48
  14. data/bin/benchmarks +85 -0
  15. data/bin/create_token +28 -0
  16. data/bin/integrations +160 -0
  17. data/bin/karafka +4 -0
  18. data/bin/stress +13 -0
  19. data/certs/karafka-pro.pem +11 -0
  20. data/config/errors.yml +5 -38
  21. data/docker-compose.yml +12 -3
  22. data/karafka.gemspec +14 -14
  23. data/lib/active_job/karafka.rb +20 -0
  24. data/lib/active_job/queue_adapters/karafka_adapter.rb +26 -0
  25. data/lib/karafka/active_job/consumer.rb +24 -0
  26. data/lib/karafka/active_job/dispatcher.rb +38 -0
  27. data/lib/karafka/active_job/job_extensions.rb +34 -0
  28. data/lib/karafka/active_job/job_options_contract.rb +15 -0
  29. data/lib/karafka/active_job/routing_extensions.rb +18 -0
  30. data/lib/karafka/app.rb +14 -20
  31. data/lib/karafka/base_consumer.rb +103 -34
  32. data/lib/karafka/cli/base.rb +4 -4
  33. data/lib/karafka/cli/info.rb +44 -9
  34. data/lib/karafka/cli/install.rb +3 -8
  35. data/lib/karafka/cli/server.rb +16 -43
  36. data/lib/karafka/cli.rb +4 -11
  37. data/lib/karafka/connection/client.rb +279 -93
  38. data/lib/karafka/connection/listener.rb +137 -38
  39. data/lib/karafka/connection/messages_buffer.rb +57 -0
  40. data/lib/karafka/connection/pauses_manager.rb +46 -0
  41. data/lib/karafka/connection/rebalance_manager.rb +62 -0
  42. data/lib/karafka/contracts/base.rb +23 -0
  43. data/lib/karafka/contracts/config.rb +44 -8
  44. data/lib/karafka/contracts/consumer_group.rb +1 -176
  45. data/lib/karafka/contracts/consumer_group_topic.rb +16 -8
  46. data/lib/karafka/contracts/server_cli_options.rb +2 -12
  47. data/lib/karafka/contracts.rb +1 -1
  48. data/lib/karafka/env.rb +46 -0
  49. data/lib/karafka/errors.rb +18 -18
  50. data/lib/karafka/helpers/multi_delegator.rb +2 -2
  51. data/lib/karafka/instrumentation/callbacks/error.rb +40 -0
  52. data/lib/karafka/instrumentation/callbacks/statistics.rb +42 -0
  53. data/lib/karafka/instrumentation/monitor.rb +14 -21
  54. data/lib/karafka/instrumentation/stdout_listener.rb +67 -91
  55. data/lib/karafka/instrumentation.rb +21 -0
  56. data/lib/karafka/licenser.rb +76 -0
  57. data/lib/karafka/{params → messages}/batch_metadata.rb +9 -13
  58. data/lib/karafka/messages/builders/batch_metadata.rb +52 -0
  59. data/lib/karafka/messages/builders/message.rb +38 -0
  60. data/lib/karafka/messages/builders/messages.rb +40 -0
  61. data/lib/karafka/{params/params.rb → messages/message.rb} +7 -12
  62. data/lib/karafka/messages/messages.rb +64 -0
  63. data/lib/karafka/{params → messages}/metadata.rb +4 -6
  64. data/lib/karafka/messages/seek.rb +9 -0
  65. data/lib/karafka/patches/rdkafka/consumer.rb +22 -0
  66. data/lib/karafka/pro/active_job/dispatcher.rb +58 -0
  67. data/lib/karafka/pro/active_job/job_options_contract.rb +27 -0
  68. data/lib/karafka/pro/loader.rb +29 -0
  69. data/lib/karafka/pro.rb +13 -0
  70. data/lib/karafka/processing/executor.rb +96 -0
  71. data/lib/karafka/processing/executors_buffer.rb +49 -0
  72. data/lib/karafka/processing/jobs/base.rb +18 -0
  73. data/lib/karafka/processing/jobs/consume.rb +28 -0
  74. data/lib/karafka/processing/jobs/revoked.rb +22 -0
  75. data/lib/karafka/processing/jobs/shutdown.rb +23 -0
  76. data/lib/karafka/processing/jobs_queue.rb +121 -0
  77. data/lib/karafka/processing/worker.rb +57 -0
  78. data/lib/karafka/processing/workers_batch.rb +22 -0
  79. data/lib/karafka/railtie.rb +75 -0
  80. data/lib/karafka/routing/builder.rb +15 -24
  81. data/lib/karafka/routing/consumer_group.rb +10 -18
  82. data/lib/karafka/routing/consumer_mapper.rb +1 -2
  83. data/lib/karafka/routing/router.rb +1 -1
  84. data/lib/karafka/routing/subscription_group.rb +53 -0
  85. data/lib/karafka/routing/subscription_groups_builder.rb +51 -0
  86. data/lib/karafka/routing/topic.rb +47 -25
  87. data/lib/karafka/runner.rb +59 -0
  88. data/lib/karafka/serialization/json/deserializer.rb +6 -15
  89. data/lib/karafka/server.rb +62 -25
  90. data/lib/karafka/setup/config.rb +98 -171
  91. data/lib/karafka/status.rb +13 -3
  92. data/lib/karafka/templates/example_consumer.rb.erb +16 -0
  93. data/lib/karafka/templates/karafka.rb.erb +14 -50
  94. data/lib/karafka/time_trackers/base.rb +19 -0
  95. data/lib/karafka/time_trackers/pause.rb +84 -0
  96. data/lib/karafka/time_trackers/poll.rb +65 -0
  97. data/lib/karafka/version.rb +1 -1
  98. data/lib/karafka.rb +35 -13
  99. data.tar.gz.sig +0 -0
  100. metadata +82 -104
  101. metadata.gz.sig +0 -0
  102. data/MIT-LICENCE +0 -18
  103. data/lib/karafka/assignment_strategies/round_robin.rb +0 -13
  104. data/lib/karafka/attributes_map.rb +0 -63
  105. data/lib/karafka/backends/inline.rb +0 -16
  106. data/lib/karafka/base_responder.rb +0 -226
  107. data/lib/karafka/cli/flow.rb +0 -48
  108. data/lib/karafka/cli/missingno.rb +0 -19
  109. data/lib/karafka/code_reloader.rb +0 -67
  110. data/lib/karafka/connection/api_adapter.rb +0 -158
  111. data/lib/karafka/connection/batch_delegator.rb +0 -55
  112. data/lib/karafka/connection/builder.rb +0 -23
  113. data/lib/karafka/connection/message_delegator.rb +0 -36
  114. data/lib/karafka/consumers/batch_metadata.rb +0 -10
  115. data/lib/karafka/consumers/callbacks.rb +0 -71
  116. data/lib/karafka/consumers/includer.rb +0 -64
  117. data/lib/karafka/consumers/responders.rb +0 -24
  118. data/lib/karafka/consumers/single_params.rb +0 -15
  119. data/lib/karafka/contracts/responder_usage.rb +0 -54
  120. data/lib/karafka/fetcher.rb +0 -42
  121. data/lib/karafka/helpers/class_matcher.rb +0 -88
  122. data/lib/karafka/helpers/config_retriever.rb +0 -46
  123. data/lib/karafka/helpers/inflector.rb +0 -26
  124. data/lib/karafka/params/builders/batch_metadata.rb +0 -30
  125. data/lib/karafka/params/builders/params.rb +0 -38
  126. data/lib/karafka/params/builders/params_batch.rb +0 -25
  127. data/lib/karafka/params/params_batch.rb +0 -60
  128. data/lib/karafka/patches/ruby_kafka.rb +0 -47
  129. data/lib/karafka/persistence/client.rb +0 -29
  130. data/lib/karafka/persistence/consumers.rb +0 -45
  131. data/lib/karafka/persistence/topics.rb +0 -48
  132. data/lib/karafka/responders/builder.rb +0 -36
  133. data/lib/karafka/responders/topic.rb +0 -55
  134. data/lib/karafka/routing/topic_mapper.rb +0 -53
  135. data/lib/karafka/serialization/json/serializer.rb +0 -31
  136. data/lib/karafka/setup/configurators/water_drop.rb +0 -36
  137. data/lib/karafka/templates/application_responder.rb.erb +0 -11
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Karafka
4
+ module Connection
5
+ # Buffer for messages.
6
+ # When message is added to this buffer, it gets assigned to an array with other messages from
7
+ # the same topic and partition.
8
+ #
9
+ # @note This buffer is NOT threadsafe.
10
+ class MessagesBuffer
11
+ attr_reader :size
12
+
13
+ # @return [Karafka::Connection::MessagesBuffer] buffer instance
14
+ def initialize
15
+ @size = 0
16
+ @groups = Hash.new do |topic_groups, topic|
17
+ topic_groups[topic] = Hash.new do |partition_groups, partition|
18
+ partition_groups[partition] = []
19
+ end
20
+ end
21
+ end
22
+
23
+ # Iterates over aggregated data providing messages per topic partition.
24
+ #
25
+ # @yieldparam [String] topic name
26
+ # @yieldparam [Integer] partition number
27
+ # @yieldparam [Array<Rdkafka::Consumer::Message>] topic partition aggregated results
28
+ def each
29
+ @groups.each do |topic, partitions|
30
+ partitions.each do |partition, messages|
31
+ yield(topic, partition, messages)
32
+ end
33
+ end
34
+ end
35
+
36
+ # Adds a message to the buffer.
37
+ #
38
+ # @param message [Rdkafka::Consumer::Message] raw rdkafka message
39
+ # @return [Array<Rdkafka::Consumer::Message>] given partition topic sub-buffer array
40
+ def <<(message)
41
+ @size += 1
42
+ @groups[message.topic][message.partition] << message
43
+ end
44
+
45
+ # Removes all the data from the buffer.
46
+ #
47
+ # @note We do not clear the whole groups hash but rather we clear the partition hashes, so
48
+ # we save ourselves some objects allocations. We cannot clear the underlying arrays as they
49
+ # may be used in other threads for data processing, thus if we would clear it, we could
50
+ # potentially clear a raw messages array for a job that is in the jobs queue.
51
+ def clear
52
+ @size = 0
53
+ @groups.each_value(&:clear)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -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
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Karafka
4
+ module Contracts
5
+ # Base contract for all Karafka contracts
6
+ class Base < Dry::Validation::Contract
7
+ config.messages.load_paths << File.join(Karafka.gem_root, 'config', 'errors.yml')
8
+
9
+ # @param data [Hash] data for validation
10
+ # @return [Boolean] true if all good
11
+ # @raise [Errors::InvalidConfigurationError] invalid configuration error
12
+ # @note We use contracts only in the config validation context, so no need to add support
13
+ # for multiple error classes. It will be added when it will be needed.
14
+ def validate!(data)
15
+ result = call(data)
16
+
17
+ return true if result.success?
18
+
19
+ raise Errors::InvalidConfigurationError, result.errors.to_h
20
+ end
21
+ end
22
+ end
23
+ end
@@ -2,19 +2,55 @@
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
- class Config < Dry::Validation::Contract
10
+ # so we validate all of that once all the routes are defined and ready.
11
+ class Config < Base
11
12
  params do
13
+ # License validity happens in the licenser. Here we do only the simple consistency checks
14
+ required(:license).schema do
15
+ required(:token) { bool? | str? }
16
+ required(:entity) { str? }
17
+ required(:expires_on) { date? }
18
+ end
19
+
12
20
  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)
21
+ required(:concurrency) { int? & gt?(0) }
22
+ required(:consumer_mapper).filled
23
+ required(:consumer_persistence).filled(:bool?)
24
+ required(:pause_timeout) { int? & gt?(0) }
25
+ required(:pause_max_timeout) { int? & gt?(0) }
26
+ required(:pause_with_exponential_backoff).filled(:bool?)
27
+ required(:shutdown_timeout) { int? & gt?(0) }
28
+ required(:kafka).filled(:hash)
29
+
30
+ # We validate internals just to be sure, that they are present and working
31
+ required(:internal).schema do
32
+ required(:routing_builder)
33
+ required(:status)
34
+ required(:process)
35
+ required(:subscription_groups_builder)
36
+ end
37
+ end
38
+
39
+ # rdkafka requires all the keys to be strings, so we ensure that
40
+ rule(:kafka) do
41
+ next unless value.is_a?(Hash)
42
+
43
+ value.each_key do |key|
44
+ next if key.is_a?(Symbol)
45
+
46
+ key(:"kafka.#{key}").failure(:kafka_key_must_be_a_symbol)
47
+ end
48
+ end
16
49
 
17
- optional(:backend).filled
50
+ rule(:pause_timeout, :pause_max_timeout) do
51
+ if values[:pause_timeout].to_i > values[:pause_max_timeout].to_i
52
+ key(:pause_timeout).failure(:max_timeout_vs_pause_max_timeout)
53
+ end
18
54
  end
19
55
  end
20
56
  end
@@ -3,17 +3,7 @@
3
3
  module Karafka
4
4
  module Contracts
5
5
  # Contract for single full route (consumer group + topics) validation.
6
- class ConsumerGroup < Dry::Validation::Contract
7
- config.messages.load_paths << File.join(Karafka.gem_root, 'config', 'errors.yml')
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
-
6
+ class ConsumerGroup < Base
17
7
  # Internal contract for sub-validating topics schema
18
8
  TOPIC_CONTRACT = ConsumerGroupTopic.new.freeze
19
9
 
@@ -22,58 +12,6 @@ module Karafka
22
12
  params do
23
13
  required(:id).filled(:str?, format?: Karafka::Contracts::TOPIC_REGEXP)
24
14
  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
15
  end
78
16
 
79
17
  rule(:topics) do
@@ -93,119 +31,6 @@ module Karafka
93
31
  end
94
32
  end
95
33
  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
34
  end
210
35
  end
211
36
  end
@@ -2,17 +2,25 @@
2
2
 
3
3
  module Karafka
4
4
  module Contracts
5
- # Consumer group topic validation rules
6
- class ConsumerGroupTopic < Dry::Validation::Contract
5
+ # Consumer group topic validation rules.
6
+ class ConsumerGroupTopic < Base
7
7
  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
8
  required(:consumer).filled
12
9
  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?)
10
+ required(:id).filled(:str?, format?: Karafka::Contracts::TOPIC_REGEXP)
11
+ required(:kafka).filled
12
+ required(:max_messages) { int? & gteq?(1) }
13
+ required(:max_wait_time).filled { int? & gteq?(10) }
14
+ required(:manual_offset_management).filled(:bool?)
15
+ required(:name).filled(:str?, format?: Karafka::Contracts::TOPIC_REGEXP)
16
+ end
17
+
18
+ rule(:kafka) do
19
+ # This will trigger rdkafka validations that we catch and re-map the info and use dry
20
+ # compatible format
21
+ Rdkafka::Config.new(value).send(:native_config)
22
+ rescue Rdkafka::Config::ConfigError => e
23
+ key(:kafka).failure(e.message)
16
24
  end
17
25
  end
18
26
  end
@@ -2,22 +2,12 @@
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
8
- class ServerCliOptions < Dry::Validation::Contract
9
- config.messages.load_paths << File.join(Karafka.gem_root, 'config', 'errors.yml')
10
-
5
+ # Contract for validating correctness of the server cli command options.
6
+ class ServerCliOptions < Base
11
7
  params do
12
- optional(:pid).filled(:str?)
13
- optional(:daemon).filled(:bool?)
14
8
  optional(:consumer_groups).value(:array, :filled?)
15
9
  end
16
10
 
17
- rule(:pid) do
18
- key(:pid).failure(:pid_already_exists) if value && File.exist?(value)
19
- end
20
-
21
11
  rule(:consumer_groups) do
22
12
  # If there were no consumer_groups declared in the server cli, it means that we will
23
13
  # 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,34 @@ 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)
46
+
47
+ # Used to instrument this error into the error notifications
48
+ # We do not raise it so we won't crash deployed systems
49
+ ExpiredLicenseTokenError = Class.new(BaseError)
50
50
  end
51
51
  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