rdkafka 0.13.0 → 0.31.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +388 -111
  3. data/{LICENSE → MIT-LICENSE} +2 -1
  4. data/README.md +90 -43
  5. data/Rakefile +21 -23
  6. data/bin/verify_kafka_warnings +44 -0
  7. data/dist/librdkafka-2.15.1.tar.gz +0 -0
  8. data/dist/patches/rdkafka_global_init.patch +15 -0
  9. data/docker-compose-ssl.yml +39 -0
  10. data/docker-compose.yml +22 -17
  11. data/ext/Rakefile +97 -30
  12. data/lib/rdkafka/abstract_handle.rb +113 -34
  13. data/lib/rdkafka/admin/acl_binding_result.rb +51 -0
  14. data/lib/rdkafka/admin/alter_consumer_group_offsets_handle.rb +31 -0
  15. data/lib/rdkafka/admin/alter_consumer_group_offsets_report.rb +50 -0
  16. data/lib/rdkafka/admin/config_binding_result.rb +30 -0
  17. data/lib/rdkafka/admin/config_resource_binding_result.rb +19 -0
  18. data/lib/rdkafka/admin/create_acl_handle.rb +32 -0
  19. data/lib/rdkafka/admin/create_acl_report.rb +26 -0
  20. data/lib/rdkafka/admin/create_partitions_handle.rb +28 -0
  21. data/lib/rdkafka/admin/create_partitions_report.rb +7 -0
  22. data/lib/rdkafka/admin/create_topic_handle.rb +8 -7
  23. data/lib/rdkafka/admin/create_topic_report.rb +4 -1
  24. data/lib/rdkafka/admin/delete_acl_handle.rb +31 -0
  25. data/lib/rdkafka/admin/delete_acl_report.rb +25 -0
  26. data/lib/rdkafka/admin/delete_consumer_group_offsets_handle.rb +31 -0
  27. data/lib/rdkafka/admin/delete_consumer_group_offsets_report.rb +50 -0
  28. data/lib/rdkafka/admin/delete_groups_handle.rb +31 -0
  29. data/lib/rdkafka/admin/delete_groups_report.rb +27 -0
  30. data/lib/rdkafka/admin/delete_records_handle.rb +31 -0
  31. data/lib/rdkafka/admin/delete_records_report.rb +25 -0
  32. data/lib/rdkafka/admin/delete_topic_handle.rb +8 -7
  33. data/lib/rdkafka/admin/delete_topic_report.rb +4 -1
  34. data/lib/rdkafka/admin/describe_acl_handle.rb +31 -0
  35. data/lib/rdkafka/admin/describe_acl_report.rb +27 -0
  36. data/lib/rdkafka/admin/describe_cluster_handle.rb +31 -0
  37. data/lib/rdkafka/admin/describe_cluster_report.rb +48 -0
  38. data/lib/rdkafka/admin/describe_configs_handle.rb +30 -0
  39. data/lib/rdkafka/admin/describe_configs_report.rb +56 -0
  40. data/lib/rdkafka/admin/describe_consumer_groups_handle.rb +31 -0
  41. data/lib/rdkafka/admin/describe_consumer_groups_report.rb +151 -0
  42. data/lib/rdkafka/admin/describe_result_parsing.rb +47 -0
  43. data/lib/rdkafka/admin/describe_topics_handle.rb +31 -0
  44. data/lib/rdkafka/admin/describe_topics_report.rb +118 -0
  45. data/lib/rdkafka/admin/incremental_alter_configs_handle.rb +30 -0
  46. data/lib/rdkafka/admin/incremental_alter_configs_report.rb +56 -0
  47. data/lib/rdkafka/admin/list_consumer_groups_handle.rb +31 -0
  48. data/lib/rdkafka/admin/list_consumer_groups_report.rb +83 -0
  49. data/lib/rdkafka/admin/list_offsets_handle.rb +31 -0
  50. data/lib/rdkafka/admin/list_offsets_report.rb +54 -0
  51. data/lib/rdkafka/admin.rb +1381 -38
  52. data/lib/rdkafka/bindings/admin/acls.rb +85 -0
  53. data/lib/rdkafka/bindings/admin/configs.rb +37 -0
  54. data/lib/rdkafka/bindings/admin/list_offsets.rb +26 -0
  55. data/lib/rdkafka/bindings/admin.rb +197 -0
  56. data/lib/rdkafka/bindings/callbacks.rb +100 -0
  57. data/lib/rdkafka/bindings/configuration.rb +50 -0
  58. data/lib/rdkafka/bindings/consumer.rb +83 -0
  59. data/lib/rdkafka/bindings/errors.rb +15 -0
  60. data/lib/rdkafka/bindings/handle.rb +16 -0
  61. data/lib/rdkafka/bindings/messages.rb +28 -0
  62. data/lib/rdkafka/bindings/metadata.rb +14 -0
  63. data/lib/rdkafka/bindings/polling.rb +17 -0
  64. data/lib/rdkafka/bindings/producer.rb +77 -0
  65. data/lib/rdkafka/bindings/structs.rb +62 -0
  66. data/lib/rdkafka/bindings.rb +69 -294
  67. data/lib/rdkafka/callbacks/alter_consumer_group_offsets_handler.rb +65 -0
  68. data/lib/rdkafka/callbacks/base_handler.rb +62 -0
  69. data/lib/rdkafka/callbacks/create_acl_handler.rb +37 -0
  70. data/lib/rdkafka/callbacks/create_partitions_handler.rb +37 -0
  71. data/lib/rdkafka/callbacks/create_topic_handler.rb +37 -0
  72. data/lib/rdkafka/callbacks/delete_acl_handler.rb +42 -0
  73. data/lib/rdkafka/callbacks/delete_consumer_group_offsets_handler.rb +65 -0
  74. data/lib/rdkafka/callbacks/delete_groups_handler.rb +37 -0
  75. data/lib/rdkafka/callbacks/delete_records_handler.rb +38 -0
  76. data/lib/rdkafka/callbacks/delete_topic_handler.rb +37 -0
  77. data/lib/rdkafka/callbacks/describe_acl_handler.rb +35 -0
  78. data/lib/rdkafka/callbacks/describe_cluster_handler.rb +36 -0
  79. data/lib/rdkafka/callbacks/describe_configs_handler.rb +42 -0
  80. data/lib/rdkafka/callbacks/describe_consumer_groups_handler.rb +36 -0
  81. data/lib/rdkafka/callbacks/describe_topics_handler.rb +36 -0
  82. data/lib/rdkafka/callbacks/incremental_alter_configs_handler.rb +42 -0
  83. data/lib/rdkafka/callbacks/list_consumer_groups_handler.rb +39 -0
  84. data/lib/rdkafka/callbacks/list_offsets_handler.rb +42 -0
  85. data/lib/rdkafka/callbacks.rb +273 -52
  86. data/lib/rdkafka/clients.rb +81 -0
  87. data/lib/rdkafka/config.rb +333 -91
  88. data/lib/rdkafka/consumer/allocator.rb +38 -0
  89. data/lib/rdkafka/consumer/headers.rb +39 -25
  90. data/lib/rdkafka/consumer/message.rb +18 -20
  91. data/lib/rdkafka/consumer/partition.rb +15 -4
  92. data/lib/rdkafka/consumer/topic_partition_list.rb +82 -48
  93. data/lib/rdkafka/consumer.rb +765 -192
  94. data/lib/rdkafka/defaults.rb +123 -0
  95. data/lib/rdkafka/error.rb +40 -14
  96. data/lib/rdkafka/helpers/list_offsets.rb +127 -0
  97. data/lib/rdkafka/helpers/metadata.rb +29 -0
  98. data/lib/rdkafka/helpers/oauth.rb +95 -0
  99. data/lib/rdkafka/helpers/time.rb +19 -0
  100. data/lib/rdkafka/metadata.rb +138 -26
  101. data/lib/rdkafka/native_kafka.rb +142 -22
  102. data/lib/rdkafka/producer/delivery_handle.rb +27 -7
  103. data/lib/rdkafka/producer/delivery_report.rb +71 -4
  104. data/lib/rdkafka/producer/partitions_count_cache.rb +212 -0
  105. data/lib/rdkafka/producer.rb +422 -81
  106. data/lib/rdkafka/version.rb +6 -3
  107. data/lib/rdkafka.rb +73 -1
  108. data/package-lock.json +331 -0
  109. data/package.json +9 -0
  110. data/rdkafka.gemspec +78 -25
  111. data/renovate.json +111 -0
  112. metadata +114 -134
  113. data/.gitignore +0 -8
  114. data/.rspec +0 -1
  115. data/.semaphore/semaphore.yml +0 -27
  116. data/.yardopts +0 -2
  117. data/Gemfile +0 -5
  118. data/Guardfile +0 -19
  119. data/ext/README.md +0 -18
  120. data/spec/rdkafka/abstract_handle_spec.rb +0 -115
  121. data/spec/rdkafka/admin/create_topic_handle_spec.rb +0 -54
  122. data/spec/rdkafka/admin/create_topic_report_spec.rb +0 -18
  123. data/spec/rdkafka/admin/delete_topic_handle_spec.rb +0 -54
  124. data/spec/rdkafka/admin/delete_topic_report_spec.rb +0 -18
  125. data/spec/rdkafka/admin_spec.rb +0 -204
  126. data/spec/rdkafka/bindings_spec.rb +0 -136
  127. data/spec/rdkafka/callbacks_spec.rb +0 -22
  128. data/spec/rdkafka/config_spec.rb +0 -197
  129. data/spec/rdkafka/consumer/headers_spec.rb +0 -62
  130. data/spec/rdkafka/consumer/message_spec.rb +0 -141
  131. data/spec/rdkafka/consumer/partition_spec.rb +0 -59
  132. data/spec/rdkafka/consumer/topic_partition_list_spec.rb +0 -225
  133. data/spec/rdkafka/consumer_spec.rb +0 -1106
  134. data/spec/rdkafka/error_spec.rb +0 -91
  135. data/spec/rdkafka/metadata_spec.rb +0 -80
  136. data/spec/rdkafka/native_kafka_spec.rb +0 -124
  137. data/spec/rdkafka/producer/delivery_handle_spec.rb +0 -47
  138. data/spec/rdkafka/producer/delivery_report_spec.rb +0 -23
  139. data/spec/rdkafka/producer_spec.rb +0 -557
  140. data/spec/spec_helper.rb +0 -155
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rdkafka
4
+ module Bindings
5
+ # Metadata
6
+
7
+ attach_function :rd_kafka_name, [:pointer], :string
8
+ attach_function :rd_kafka_memberid, [:pointer], :pointer, blocking: true
9
+ attach_function :rd_kafka_clusterid, [:pointer, :int], :pointer, blocking: true
10
+ attach_function :rd_kafka_mem_free, [:pointer, :pointer], :void
11
+ attach_function :rd_kafka_metadata, [:pointer, :int, :pointer, :pointer, :int], :int, blocking: true
12
+ attach_function :rd_kafka_metadata_destroy, [:pointer], :void, blocking: true
13
+ end
14
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rdkafka
4
+ module Bindings
5
+ # Polling
6
+
7
+ attach_function :rd_kafka_flush, [:pointer, :int], :int, blocking: true
8
+ attach_function :rd_kafka_poll, [:pointer, :int], :int, blocking: true
9
+ attach_function :rd_kafka_outq_len, [:pointer], :int, blocking: true
10
+
11
+ # Non-blocking poll variants (do not release GVL)
12
+ # These are more efficient for poll(0) calls in fiber schedulers where GVL
13
+ # release/reacquire overhead is wasteful since we don't expect to wait.
14
+ # Uses the same underlying C function but with blocking: false to skip GVL release.
15
+ attach_function :rd_kafka_poll_nb, :rd_kafka_poll, [:pointer, :int], :int, blocking: false
16
+ end
17
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rdkafka
4
+ module Bindings
5
+ # Producer
6
+ RD_KAFKA_VTYPE_END = 0
7
+ RD_KAFKA_VTYPE_TOPIC = 1
8
+ RD_KAFKA_VTYPE_RKT = 2
9
+ RD_KAFKA_VTYPE_PARTITION = 3
10
+ RD_KAFKA_VTYPE_VALUE = 4
11
+ RD_KAFKA_VTYPE_KEY = 5
12
+ RD_KAFKA_VTYPE_OPAQUE = 6
13
+ RD_KAFKA_VTYPE_MSGFLAGS = 7
14
+ RD_KAFKA_VTYPE_TIMESTAMP = 8
15
+ RD_KAFKA_VTYPE_HEADER = 9
16
+ RD_KAFKA_VTYPE_HEADERS = 10
17
+ RD_KAFKA_PURGE_F_QUEUE = 1
18
+ RD_KAFKA_PURGE_F_INFLIGHT = 2
19
+
20
+ RD_KAFKA_MSG_F_COPY = 0x2
21
+
22
+ attach_function :rd_kafka_producev, [:pointer, :varargs], :int, blocking: true
23
+ attach_function :rd_kafka_purge, [:pointer, :int], :int, blocking: true
24
+ callback :delivery_cb, [:pointer, :pointer, :pointer], :void
25
+ attach_function :rd_kafka_conf_set_dr_msg_cb, [:pointer, :delivery_cb], :void
26
+
27
+ # Hash mapping partitioner names to their FFI function symbols
28
+ # @return [Hash{String => Symbol}]
29
+ PARTITIONERS = %w[random consistent consistent_random murmur2 murmur2_random fnv1a fnv1a_random].each_with_object({}) do |name, hsh|
30
+ method_name = :"rd_kafka_msg_partitioner_#{name}"
31
+ attach_function method_name, [:pointer, :pointer, :size_t, :int32, :pointer, :pointer], :int32
32
+ hsh[name] = method_name
33
+ end
34
+
35
+ # Calculates the partition for a message based on the partitioner
36
+ #
37
+ # @param topic_ptr [FFI::Pointer] pointer to the topic handle
38
+ # @param str [String] the partition key string
39
+ # @param partition_count [Integer, nil] number of partitions
40
+ # @param partitioner [String] name of the partitioner to use
41
+ # @return [Integer] partition number or RD_KAFKA_PARTITION_UA if unassigned
42
+ # @raise [Rdkafka::Config::ConfigError] when an unknown partitioner is specified
43
+ def self.partitioner(topic_ptr, str, partition_count, partitioner = "consistent_random")
44
+ # Return RD_KAFKA_PARTITION_UA(unassigned partition) when partition count is nil/zero.
45
+ return RD_KAFKA_PARTITION_UA unless partition_count&.nonzero?
46
+
47
+ str_ptr = str.empty? ? FFI::MemoryPointer::NULL : FFI::MemoryPointer.from_string(str)
48
+ method_name = PARTITIONERS.fetch(partitioner) do
49
+ raise Rdkafka::Config::ConfigError.new("Unknown partitioner: #{partitioner}")
50
+ end
51
+
52
+ public_send(method_name, topic_ptr, str_ptr, partition_key_length(str), partition_count, nil, nil)
53
+ end
54
+
55
+ # Partition key length as the character count (legacy default).
56
+ #
57
+ # @param str [String] the partition key string
58
+ # @return [Integer]
59
+ def self.partition_key_size(str)
60
+ str.size
61
+ end
62
+
63
+ # Partition key length as the byte count, which is what librdkafka hashes (UTF-8 bytes copied
64
+ # into the key pointer) and what other Kafka clients use.
65
+ #
66
+ # @param str [String] the partition key string
67
+ # @return [Integer]
68
+ def self.partition_key_bytesize(str)
69
+ str.bytesize
70
+ end
71
+
72
+ # Length of the partition key passed to librdkafka. Aliased to one of the methods above by
73
+ # `Rdkafka::Config.partitioner_key_uses_bytesize=`, so the per-message path does not check the
74
+ # setting on every call.
75
+ singleton_class.alias_method :partition_key_length, :partition_key_size
76
+ end
77
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rdkafka
4
+ module Bindings
5
+ # FFI struct representing a Kafka message (rd_kafka_message_t)
6
+ class Message < FFI::Struct
7
+ layout :err, :int,
8
+ :rkt, :pointer,
9
+ :partition, :int32,
10
+ :payload, :pointer,
11
+ :len, :size_t,
12
+ :key, :pointer,
13
+ :key_len, :size_t,
14
+ :offset, :int64,
15
+ :_private, :pointer
16
+ end
17
+
18
+ # FFI struct representing a topic partition (rd_kafka_topic_partition_t)
19
+ class TopicPartition < FFI::Struct
20
+ layout :topic, :string,
21
+ :partition, :int32,
22
+ :offset, :int64,
23
+ :metadata, :pointer,
24
+ :metadata_size, :size_t,
25
+ :opaque, :pointer,
26
+ :err, :int,
27
+ :_private, :pointer
28
+ end
29
+
30
+ # FFI struct representing a topic partition list (rd_kafka_topic_partition_list_t)
31
+ class TopicPartitionList < FFI::Struct
32
+ layout :cnt, :int,
33
+ :size, :int,
34
+ :elems, :pointer
35
+ end
36
+
37
+ # FFI struct representing a config resource (rd_kafka_ConfigResource_t)
38
+ # Structs for management of configurations. Each configuration is attached to a resource
39
+ # and one resource can have many configuration details. Each resource will also have
40
+ # separate errors results if obtaining configuration was not possible for any reason
41
+ class ConfigResource < FFI::Struct
42
+ layout :type, :int,
43
+ :name, :string
44
+ end
45
+
46
+ # FFI struct for error description (rd_kafka_err_desc)
47
+ class NativeErrorDesc < FFI::Struct
48
+ layout :code, :int,
49
+ :name, :pointer,
50
+ :desc, :pointer
51
+ end
52
+
53
+ # FFI struct for native error (rd_kafka_error_t)
54
+ class NativeError < FFI::Struct
55
+ layout :code, :int32,
56
+ :errstr, :pointer,
57
+ :fatal, :u_int8_t,
58
+ :retriable, :u_int8_t,
59
+ :txn_requires_abort, :u_int8_t
60
+ end
61
+ end
62
+ end
@@ -1,325 +1,100 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "ffi"
4
- require "json"
5
- require "logger"
6
-
7
3
  module Rdkafka
8
4
  # @private
5
+ #
6
+ # @note
7
+ # There are two types of responses related to errors:
8
+ # - rd_kafka_error_t - a C object that we need to remap into an error or null when no error
9
+ # - rd_kafka_resp_err_t - response error code (numeric) that we can use directly
10
+ #
11
+ # It is critical to ensure, that we handle them correctly. The result type should be:
12
+ # - rd_kafka_error_t - :pointer
13
+ # - rd_kafka_resp_err_t - :int
9
14
  module Bindings
10
15
  extend FFI::Library
11
16
 
17
+ # Returns the library extension based on the host OS
18
+ # @return [String] 'dylib' on macOS, 'so' on other systems
12
19
  def self.lib_extension
13
- if RbConfig::CONFIG['host_os'] =~ /darwin/
14
- 'dylib'
20
+ if /darwin/.match?(RbConfig::CONFIG["host_os"])
21
+ "dylib"
15
22
  else
16
- 'so'
17
- end
18
- end
19
-
20
- ffi_lib File.join(__dir__, "../../ext/librdkafka.#{lib_extension}")
21
-
22
- RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS = -175
23
- RD_KAFKA_RESP_ERR__REVOKE_PARTITIONS = -174
24
- RD_KAFKA_RESP_ERR__NOENT = -156
25
- RD_KAFKA_RESP_ERR_NO_ERROR = 0
26
-
27
- RD_KAFKA_OFFSET_END = -1
28
- RD_KAFKA_OFFSET_BEGINNING = -2
29
- RD_KAFKA_OFFSET_STORED = -1000
30
- RD_KAFKA_OFFSET_INVALID = -1001
31
-
32
- class SizePtr < FFI::Struct
33
- layout :value, :size_t
34
- end
35
-
36
- # Polling
37
-
38
- attach_function :rd_kafka_flush, [:pointer, :int], :void, blocking: true
39
- attach_function :rd_kafka_poll, [:pointer, :int], :void, blocking: true
40
- attach_function :rd_kafka_outq_len, [:pointer], :int, blocking: true
41
-
42
- # Metadata
43
-
44
- attach_function :rd_kafka_memberid, [:pointer], :string, blocking: true
45
- attach_function :rd_kafka_clusterid, [:pointer], :string, blocking: true
46
- attach_function :rd_kafka_metadata, [:pointer, :int, :pointer, :pointer, :int], :int, blocking: true
47
- attach_function :rd_kafka_metadata_destroy, [:pointer], :void, blocking: true
48
-
49
- # Message struct
50
-
51
- class Message < FFI::Struct
52
- layout :err, :int,
53
- :rkt, :pointer,
54
- :partition, :int32,
55
- :payload, :pointer,
56
- :len, :size_t,
57
- :key, :pointer,
58
- :key_len, :size_t,
59
- :offset, :int64,
60
- :_private, :pointer
61
- end
62
-
63
- attach_function :rd_kafka_message_destroy, [:pointer], :void
64
- attach_function :rd_kafka_message_timestamp, [:pointer, :pointer], :int64
65
- attach_function :rd_kafka_topic_new, [:pointer, :string, :pointer], :pointer
66
- attach_function :rd_kafka_topic_destroy, [:pointer], :pointer
67
- attach_function :rd_kafka_topic_name, [:pointer], :string
68
-
69
- # TopicPartition ad TopicPartitionList structs
70
-
71
- class TopicPartition < FFI::Struct
72
- layout :topic, :string,
73
- :partition, :int32,
74
- :offset, :int64,
75
- :metadata, :pointer,
76
- :metadata_size, :size_t,
77
- :opaque, :pointer,
78
- :err, :int,
79
- :_private, :pointer
80
- end
81
-
82
- class TopicPartitionList < FFI::Struct
83
- layout :cnt, :int,
84
- :size, :int,
85
- :elems, :pointer
86
- end
87
-
88
- attach_function :rd_kafka_topic_partition_list_new, [:int32], :pointer
89
- attach_function :rd_kafka_topic_partition_list_add, [:pointer, :string, :int32], :void
90
- attach_function :rd_kafka_topic_partition_list_set_offset, [:pointer, :string, :int32, :int64], :void
91
- attach_function :rd_kafka_topic_partition_list_destroy, [:pointer], :void
92
- attach_function :rd_kafka_topic_partition_list_copy, [:pointer], :pointer
93
-
94
- # Errors
95
-
96
- attach_function :rd_kafka_err2name, [:int], :string
97
- attach_function :rd_kafka_err2str, [:int], :string
98
-
99
- # Configuration
100
-
101
- enum :kafka_config_response, [
102
- :config_unknown, -2,
103
- :config_invalid, -1,
104
- :config_ok, 0
105
- ]
106
-
107
- attach_function :rd_kafka_conf_new, [], :pointer
108
- attach_function :rd_kafka_conf_set, [:pointer, :string, :string, :pointer, :int], :kafka_config_response
109
- callback :log_cb, [:pointer, :int, :string, :string], :void
110
- attach_function :rd_kafka_conf_set_log_cb, [:pointer, :log_cb], :void
111
- attach_function :rd_kafka_conf_set_opaque, [:pointer, :pointer], :void
112
- callback :stats_cb, [:pointer, :string, :int, :pointer], :int
113
- attach_function :rd_kafka_conf_set_stats_cb, [:pointer, :stats_cb], :void
114
- callback :error_cb, [:pointer, :int, :string, :pointer], :void
115
- attach_function :rd_kafka_conf_set_error_cb, [:pointer, :error_cb], :void
116
- attach_function :rd_kafka_rebalance_protocol, [:pointer], :string
117
-
118
- # Log queue
119
- attach_function :rd_kafka_set_log_queue, [:pointer, :pointer], :void
120
- attach_function :rd_kafka_queue_get_main, [:pointer], :pointer
121
-
122
- LogCallback = FFI::Function.new(
123
- :void, [:pointer, :int, :string, :string]
124
- ) do |_client_ptr, level, _level_string, line|
125
- severity = case level
126
- when 0 || 1 || 2
127
- Logger::FATAL
128
- when 3
129
- Logger::ERROR
130
- when 4
131
- Logger::WARN
132
- when 5 || 6
133
- Logger::INFO
134
- when 7
135
- Logger::DEBUG
136
- else
137
- Logger::UNKNOWN
138
- end
139
- Rdkafka::Config.log_queue << [severity, "rdkafka: #{line}"]
140
- end
141
-
142
- StatsCallback = FFI::Function.new(
143
- :int, [:pointer, :string, :int, :pointer]
144
- ) do |_client_ptr, json, _json_len, _opaque|
145
- # Pass the stats hash to callback in config
146
- if Rdkafka::Config.statistics_callback
147
- stats = JSON.parse(json)
148
- Rdkafka::Config.statistics_callback.call(stats)
149
- end
150
-
151
- # Return 0 so librdkafka frees the json string
152
- 0
153
- end
154
-
155
- ErrorCallback = FFI::Function.new(
156
- :void, [:pointer, :int, :string, :pointer]
157
- ) do |_client_prr, err_code, reason, _opaque|
158
- if Rdkafka::Config.error_callback
159
- error = Rdkafka::RdkafkaError.new(err_code, broker_message: reason)
160
- Rdkafka::Config.error_callback.call(error)
23
+ "so"
161
24
  end
162
25
  end
163
26
 
164
- # Handle
27
+ # Wrap ffi_lib to provide better error messages for glibc compatibility issues
28
+ begin
29
+ ffi_lib File.join(__dir__, "../../ext/librdkafka.#{lib_extension}")
30
+ rescue LoadError => e
31
+ error_message = e.message
165
32
 
166
- enum :kafka_type, [
167
- :rd_kafka_producer,
168
- :rd_kafka_consumer
169
- ]
33
+ # Check if this is a glibc version mismatch error
34
+ if /GLIBC_[\d.]+['"` ]?\s*not found/i.match?(error_message)
35
+ glibc_version = error_message[/GLIBC_([\d.]+)/, 1] || "unknown"
170
36
 
171
- attach_function :rd_kafka_new, [:kafka_type, :pointer, :pointer, :int], :pointer
37
+ raise Rdkafka::LibraryLoadError, <<~ERROR_MSG.strip
38
+ Failed to load librdkafka due to glibc compatibility issue.
172
39
 
173
- attach_function :rd_kafka_destroy, [:pointer], :void
40
+ The precompiled librdkafka binary requires glibc version #{glibc_version} or higher,
41
+ but your system has an older version installed.
174
42
 
175
- # Consumer
43
+ To resolve this issue, you have two options:
176
44
 
177
- attach_function :rd_kafka_subscribe, [:pointer, :pointer], :int, blocking: true
178
- attach_function :rd_kafka_unsubscribe, [:pointer], :int, blocking: true
179
- attach_function :rd_kafka_subscription, [:pointer, :pointer], :int, blocking: true
180
- attach_function :rd_kafka_assign, [:pointer, :pointer], :int, blocking: true
181
- attach_function :rd_kafka_incremental_assign, [:pointer, :pointer], :int, blocking: true
182
- attach_function :rd_kafka_incremental_unassign, [:pointer, :pointer], :int, blocking: true
183
- attach_function :rd_kafka_assignment, [:pointer, :pointer], :int, blocking: true
184
- attach_function :rd_kafka_committed, [:pointer, :pointer, :int], :int, blocking: true
185
- attach_function :rd_kafka_commit, [:pointer, :pointer, :bool], :int, blocking: true
186
- attach_function :rd_kafka_poll_set_consumer, [:pointer], :void, blocking: true
187
- attach_function :rd_kafka_consumer_poll, [:pointer, :int], :pointer, blocking: true
188
- attach_function :rd_kafka_consumer_close, [:pointer], :void, blocking: true
189
- attach_function :rd_kafka_offset_store, [:pointer, :int32, :int64], :int, blocking: true
190
- attach_function :rd_kafka_pause_partitions, [:pointer, :pointer], :int, blocking: true
191
- attach_function :rd_kafka_resume_partitions, [:pointer, :pointer], :int, blocking: true
192
- attach_function :rd_kafka_seek, [:pointer, :int32, :int64, :int], :int, blocking: true
45
+ 1. Upgrade your system to a supported platform (recommended)
193
46
 
194
- # Headers
195
- attach_function :rd_kafka_header_get_all, [:pointer, :size_t, :pointer, :pointer, SizePtr], :int
196
- attach_function :rd_kafka_message_headers, [:pointer, :pointer], :int
47
+ 2. Force compilation from source by reinstalling without the precompiled binary:
48
+ gem install rdkafka --platform=ruby
197
49
 
198
- # Rebalance
50
+ Or if using Bundler, add to your Gemfile:
51
+ gem 'rdkafka', force_ruby_platform: true
199
52
 
200
- callback :rebalance_cb_function, [:pointer, :int, :pointer, :pointer], :void
201
- attach_function :rd_kafka_conf_set_rebalance_cb, [:pointer, :rebalance_cb_function], :void, blocking: true
202
-
203
- RebalanceCallback = FFI::Function.new(
204
- :void, [:pointer, :int, :pointer, :pointer]
205
- ) do |client_ptr, code, partitions_ptr, opaque_ptr|
206
- case code
207
- when RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS
208
- if Rdkafka::Bindings.rd_kafka_rebalance_protocol(client_ptr) == "COOPERATIVE"
209
- Rdkafka::Bindings.rd_kafka_incremental_assign(client_ptr, partitions_ptr)
210
- else
211
- Rdkafka::Bindings.rd_kafka_assign(client_ptr, partitions_ptr)
212
- end
213
- else # RD_KAFKA_RESP_ERR__REVOKE_PARTITIONS or errors
214
- if Rdkafka::Bindings.rd_kafka_rebalance_protocol(client_ptr) == "COOPERATIVE"
215
- Rdkafka::Bindings.rd_kafka_incremental_unassign(client_ptr, partitions_ptr)
216
- else
217
- Rdkafka::Bindings.rd_kafka_assign(client_ptr, FFI::Pointer::NULL)
218
- end
219
- end
220
-
221
- opaque = Rdkafka::Config.opaques[opaque_ptr.to_i]
222
- return unless opaque
223
-
224
- tpl = Rdkafka::Consumer::TopicPartitionList.from_native_tpl(partitions_ptr).freeze
225
- begin
226
- case code
227
- when RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS
228
- opaque.call_on_partitions_assigned(tpl)
229
- when RD_KAFKA_RESP_ERR__REVOKE_PARTITIONS
230
- opaque.call_on_partitions_revoked(tpl)
231
- end
232
- rescue Exception => err
233
- Rdkafka::Config.logger.error("Unhandled exception: #{err.class} - #{err.message}")
53
+ Original error: #{error_message}
54
+ ERROR_MSG
55
+ else
56
+ # Re-raise the original error if it's not a glibc issue
57
+ raise
234
58
  end
235
59
  end
236
60
 
237
- # Stats
238
-
239
- attach_function :rd_kafka_query_watermark_offsets, [:pointer, :string, :int, :pointer, :pointer, :int], :int
240
-
241
- # Producer
242
-
243
- RD_KAFKA_VTYPE_END = 0
244
- RD_KAFKA_VTYPE_TOPIC = 1
245
- RD_KAFKA_VTYPE_RKT = 2
246
- RD_KAFKA_VTYPE_PARTITION = 3
247
- RD_KAFKA_VTYPE_VALUE = 4
248
- RD_KAFKA_VTYPE_KEY = 5
249
- RD_KAFKA_VTYPE_OPAQUE = 6
250
- RD_KAFKA_VTYPE_MSGFLAGS = 7
251
- RD_KAFKA_VTYPE_TIMESTAMP = 8
252
- RD_KAFKA_VTYPE_HEADER = 9
253
- RD_KAFKA_VTYPE_HEADERS = 10
61
+ RD_KAFKA_RESP_ERR__TIMED_OUT = -185
62
+ RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS = -175
63
+ RD_KAFKA_RESP_ERR__REVOKE_PARTITIONS = -174
64
+ RD_KAFKA_RESP_ERR__STATE = -172
65
+ RD_KAFKA_RESP_ERR__NOENT = -156
66
+ RD_KAFKA_RESP_ERR_NO_ERROR = 0
254
67
 
255
- RD_KAFKA_MSG_F_COPY = 0x2
68
+ RD_KAFKA_OFFSET_END = -1
69
+ RD_KAFKA_OFFSET_BEGINNING = -2
70
+ RD_KAFKA_OFFSET_STORED = -1000
71
+ RD_KAFKA_OFFSET_INVALID = -1001
256
72
 
257
- attach_function :rd_kafka_producev, [:pointer, :varargs], :int, blocking: true
258
- callback :delivery_cb, [:pointer, :pointer, :pointer], :void
259
- attach_function :rd_kafka_conf_set_dr_msg_cb, [:pointer, :delivery_cb], :void
73
+ RD_KAFKA_PARTITION_UA = -1
74
+ RD_KAFKA_PARTITION_UA_STR = RD_KAFKA_PARTITION_UA.to_s.freeze
260
75
 
261
- # Partitioner
262
- PARTITIONERS = %w(random consistent consistent_random murmur2 murmur2_random fnv1a fnv1a_random).each_with_object({}) do |name, hsh|
263
- method_name = "rd_kafka_msg_partitioner_#{name}".to_sym
264
- attach_function method_name, [:pointer, :pointer, :size_t, :int32, :pointer, :pointer], :int32
265
- hsh[name] = method_name
266
- end
76
+ EMPTY_HASH = {}.freeze
267
77
 
268
- def self.partitioner(str, partition_count, partitioner_name = "consistent_random")
269
- # Return RD_KAFKA_PARTITION_UA(unassigned partition) when partition count is nil/zero.
270
- return -1 unless partition_count&.nonzero?
271
-
272
- str_ptr = str.empty? ? FFI::MemoryPointer::NULL : FFI::MemoryPointer.from_string(str)
273
- method_name = PARTITIONERS.fetch(partitioner_name) do
274
- raise Rdkafka::Config::ConfigError.new("Unknown partitioner: #{partitioner_name}")
275
- end
276
- public_send(method_name, nil, str_ptr, str.size > 0 ? str.size : 1, partition_count, nil, nil)
78
+ # FFI struct for size_t pointer wrapper
79
+ class SizePtr < FFI::Struct
80
+ layout :value, :size_t
277
81
  end
278
82
 
279
- # Create Topics
280
-
281
- RD_KAFKA_ADMIN_OP_CREATETOPICS = 1 # rd_kafka_admin_op_t
282
- RD_KAFKA_EVENT_CREATETOPICS_RESULT = 100 # rd_kafka_event_type_t
283
-
284
- attach_function :rd_kafka_CreateTopics, [:pointer, :pointer, :size_t, :pointer, :pointer], :void, blocking: true
285
- attach_function :rd_kafka_NewTopic_new, [:pointer, :size_t, :size_t, :pointer, :size_t], :pointer, blocking: true
286
- attach_function :rd_kafka_NewTopic_set_config, [:pointer, :string, :string], :int32, blocking: true
287
- attach_function :rd_kafka_NewTopic_destroy, [:pointer], :void, blocking: true
288
- attach_function :rd_kafka_event_CreateTopics_result, [:pointer], :pointer, blocking: true
289
- attach_function :rd_kafka_CreateTopics_result_topics, [:pointer, :pointer], :pointer, blocking: true
290
-
291
- # Delete Topics
292
-
293
- RD_KAFKA_ADMIN_OP_DELETETOPICS = 2 # rd_kafka_admin_op_t
294
- RD_KAFKA_EVENT_DELETETOPICS_RESULT = 101 # rd_kafka_event_type_t
295
-
296
- attach_function :rd_kafka_DeleteTopics, [:pointer, :pointer, :size_t, :pointer, :pointer], :int32, blocking: true
297
- attach_function :rd_kafka_DeleteTopic_new, [:pointer], :pointer, blocking: true
298
- attach_function :rd_kafka_DeleteTopic_destroy, [:pointer], :void, blocking: true
299
- attach_function :rd_kafka_event_DeleteTopics_result, [:pointer], :pointer, blocking: true
300
- attach_function :rd_kafka_DeleteTopics_result_topics, [:pointer, :pointer], :pointer, blocking: true
301
-
302
- # Background Queue and Callback
303
-
304
- attach_function :rd_kafka_queue_get_background, [:pointer], :pointer
305
- attach_function :rd_kafka_conf_set_background_event_cb, [:pointer, :pointer], :void
306
- attach_function :rd_kafka_queue_destroy, [:pointer], :void
307
-
308
- # Admin Options
309
-
310
- attach_function :rd_kafka_AdminOptions_new, [:pointer, :int32], :pointer
311
- attach_function :rd_kafka_AdminOptions_set_opaque, [:pointer, :pointer], :void
312
- attach_function :rd_kafka_AdminOptions_destroy, [:pointer], :void
313
-
314
- # Extracting data from event types
315
-
316
- attach_function :rd_kafka_event_type, [:pointer], :int32
317
- attach_function :rd_kafka_event_opaque, [:pointer], :pointer
318
-
319
- # Extracting data from topic results
320
-
321
- attach_function :rd_kafka_topic_result_error, [:pointer], :int32
322
- attach_function :rd_kafka_topic_result_error_string, [:pointer], :pointer
323
- attach_function :rd_kafka_topic_result_name, [:pointer], :pointer
83
+ # This function comes from our patch on top of librdkafka. It allows os to load all the
84
+ # librdkafka components without initializing the client
85
+ # See: https://github.com/confluentinc/librdkafka/issues/4590
86
+ attach_function :rd_kafka_global_init, [], :void
324
87
  end
325
88
  end
89
+
90
+ require_relative "bindings/structs"
91
+ require_relative "bindings/polling"
92
+ require_relative "bindings/metadata"
93
+ require_relative "bindings/messages"
94
+ require_relative "bindings/errors"
95
+ require_relative "bindings/configuration"
96
+ require_relative "bindings/callbacks"
97
+ require_relative "bindings/handle"
98
+ require_relative "bindings/consumer"
99
+ require_relative "bindings/producer"
100
+ require_relative "bindings/admin"
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rdkafka
4
+ module Callbacks
5
+ # Handles `RD_KAFKA_EVENT_ALTERCONSUMERGROUPOFFSETS_RESULT` events
6
+ # @private
7
+ class AlterConsumerGroupOffsetsHandler < BaseHandler
8
+ class << self
9
+ # Resolves the alter consumer group offsets handle from its result event
10
+ # @param event_ptr [FFI::Pointer] pointer to the event
11
+ # @return [void]
12
+ def call(event_ptr)
13
+ result = Rdkafka::Bindings.rd_kafka_event_AlterConsumerGroupOffsets_result(event_ptr)
14
+
15
+ pointer_to_size_t = FFI::MemoryPointer.new(:size_t)
16
+ group_result_array = Rdkafka::Bindings.rd_kafka_AlterConsumerGroupOffsets_result_groups(result, pointer_to_size_t)
17
+ group_results = GroupResult.create_group_results_from_array(pointer_to_size_t.read_int, group_result_array)
18
+ handle_ptr = Rdkafka::Bindings.rd_kafka_event_opaque(event_ptr)
19
+
20
+ if (handle = Rdkafka::Admin::AlterConsumerGroupOffsetsHandle.remove(handle_ptr.address))
21
+ unless resolve_operation_error(event_ptr, handle)
22
+ group_result = group_results[0]
23
+
24
+ # librdkafka only supports one group per invocation, so a missing group result
25
+ # means the broker returned nothing to act on rather than a partial success.
26
+ if group_result.nil?
27
+ handle[:response] = Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR
28
+ handle.result = Rdkafka::Admin::AlterConsumerGroupOffsetsReport.new(
29
+ FFI::Pointer::NULL,
30
+ FFI::Pointer::NULL
31
+ )
32
+ else
33
+ report = Rdkafka::Admin::AlterConsumerGroupOffsetsReport.new(
34
+ group_result.error_string,
35
+ group_result.result_name,
36
+ Rdkafka::Bindings.rd_kafka_group_result_partitions(group_result_array.read_pointer)
37
+ )
38
+ handle.result = report
39
+
40
+ # Kafka reports a rejected offset change per partition, leaving the group level
41
+ # error unset - a group with live members comes back with the group error NULL
42
+ # and `unknown_member_id` on each partition. Resolving on the group error alone
43
+ # would hand back a report that looks like success, so any failed partition has
44
+ # to fail the handle too.
45
+ failed = report.partitions.find { |partition| partition[:error] }
46
+
47
+ if !group_result.result_error.zero?
48
+ handle[:response] = group_result.result_error
49
+ handle.broker_message = report.error_string
50
+ elsif failed
51
+ handle[:response] = failed[:error].rdkafka_response
52
+ handle.broker_message = "Failed for partition #{failed[:partition]} of '#{failed[:topic]}'"
53
+ else
54
+ handle[:response] = Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR
55
+ end
56
+ end
57
+
58
+ handle.unlock
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end