|
| 1 | +local ffi = require('ffi') |
| 2 | +local box = require('box') |
| 3 | +local fiber = require('fiber') |
| 4 | +local librdkafka = require('kafka.librdkafka') |
| 5 | + |
| 6 | +local ConsumerConfig = {} |
| 7 | + |
| 8 | +ConsumerConfig.__index = ConsumerConfig |
| 9 | + |
| 10 | +function ConsumerConfig.create(brokers_list) |
| 11 | + assert(brokers_list ~= nil) |
| 12 | + |
| 13 | + local config = { |
| 14 | + _brokers_list = brokers_list, |
| 15 | + _options = {}, |
| 16 | + } |
| 17 | + setmetatable(config, ConsumerConfig) |
| 18 | + return config |
| 19 | +end |
| 20 | + |
| 21 | +function ConsumerConfig:get_brokers_list() |
| 22 | + return self._brokers_list |
| 23 | +end |
| 24 | + |
| 25 | +function ConsumerConfig:set_option(name, value) |
| 26 | + self._options[name] = value |
| 27 | +end |
| 28 | + |
| 29 | +function ConsumerConfig:get_options() |
| 30 | + return self._options |
| 31 | +end |
| 32 | + |
| 33 | +local ConsumerMessage = {} |
| 34 | + |
| 35 | +ConsumerMessage.__index = ConsumerMessage |
| 36 | + |
| 37 | +function ConsumerMessage.create(rd_message) |
| 38 | + local msg = { |
| 39 | + _rd_message = rd_message, |
| 40 | + _value = nil, |
| 41 | + _topic = nil, |
| 42 | + _partition = nil, |
| 43 | + _offset = nil, |
| 44 | + } |
| 45 | + ffi.gc(msg, function(...) |
| 46 | + librdkafka.rd_kafka_message_destroy(...) |
| 47 | + end) |
| 48 | + setmetatable(msg, ConsumerMessage) |
| 49 | + return msg |
| 50 | +end |
| 51 | + |
| 52 | +function ConsumerMessage:value() |
| 53 | + if self._value == nil then |
| 54 | + self._value = ffi.string(self._rd_message.payload) |
| 55 | + end |
| 56 | + return self._value |
| 57 | +end |
| 58 | + |
| 59 | +function ConsumerMessage:topic() |
| 60 | + if self._topic == nil then |
| 61 | + self._topic = ffi.string(librdkafka.rd_kafka_topic_name(self._rd_message.rkt)) |
| 62 | + end |
| 63 | + return self._topic |
| 64 | +end |
| 65 | + |
| 66 | +function ConsumerMessage:partition() |
| 67 | + if self._partition == nil then |
| 68 | + self._partition = 1 |
| 69 | + end |
| 70 | + return self._partition |
| 71 | +end |
| 72 | + |
| 73 | +function ConsumerMessage:offset() |
| 74 | + if self._offset == nil then |
| 75 | + self._offset = 1 |
| 76 | + end |
| 77 | + return self._offset |
| 78 | +end |
| 79 | + |
| 80 | +local Consumer = {} |
| 81 | + |
| 82 | +Consumer.__index = Consumer |
| 83 | + |
| 84 | +function Consumer.create(config) |
| 85 | + assert(config ~= nil) |
| 86 | + |
| 87 | + local consumer = { |
| 88 | + config = config, |
| 89 | + _rd_consumer = {}, |
| 90 | + _output_ch = nil, |
| 91 | + } |
| 92 | + setmetatable(consumer, Consumer) |
| 93 | + return consumer |
| 94 | +end |
| 95 | + |
| 96 | +function Consumer:_get_consumer_rd_config() |
| 97 | + local rd_config = librdkafka.rd_kafka_conf_new() |
| 98 | + |
| 99 | +-- FIXME: почему мы здесь получаем segfault, а в продьюсере с таким же кодом все ок? |
| 100 | +-- ffi.gc(rd_config, function (rd_config) |
| 101 | +-- librdkafka.rd_kafka_conf_destroy(rd_config) |
| 102 | +-- end) |
| 103 | + |
| 104 | + local ERRLEN = 256 |
| 105 | + for key, value in pairs(self.config:get_options()) do |
| 106 | + local errbuf = ffi.new("char[?]", ERRLEN) -- cdata objects are garbage collected |
| 107 | + if librdkafka.rd_kafka_conf_set(rd_config, key, tostring(value), errbuf, ERRLEN) ~= librdkafka.RD_KAFKA_CONF_OK then |
| 108 | + return nil, ffi.string(errbuf) |
| 109 | + end |
| 110 | + end |
| 111 | + |
| 112 | + return rd_config, nil |
| 113 | +end |
| 114 | + |
| 115 | +function Consumer:_poll() |
| 116 | + while true do |
| 117 | + local rd_message = librdkafka.rd_kafka_consumer_poll(self._rd_consumer, 1) |
| 118 | + if rd_message.err == librdkafka.RD_KAFKA_RESP_ERR_NO_ERROR then |
| 119 | + self._output_ch:put(ConsumerMessage.create(rd_message)) |
| 120 | + else |
| 121 | + fiber.yield() |
| 122 | + end |
| 123 | + end |
| 124 | +end |
| 125 | + |
| 126 | +jit.off(Consumer._poll) |
| 127 | + |
| 128 | +function Consumer:start() |
| 129 | + local rd_config, err = self:_get_consumer_rd_config() |
| 130 | + if err ~= nil then |
| 131 | + return err |
| 132 | + end |
| 133 | + |
| 134 | + local ERRLEN = 256 |
| 135 | + local errbuf = ffi.new("char[?]", ERRLEN) -- cdata objects are garbage collected |
| 136 | + local rd_consumer = librdkafka.rd_kafka_new(librdkafka.RD_KAFKA_CONSUMER, rd_config, errbuf, ERRLEN) |
| 137 | + |
| 138 | + if rd_consumer == nil then |
| 139 | + return ffi.string(errbuf) |
| 140 | + end |
| 141 | + |
| 142 | + for _, broker in ipairs(self.config:get_brokers_list()) do |
| 143 | + librdkafka.rd_kafka_brokers_add(rd_consumer, broker) |
| 144 | + end |
| 145 | + |
| 146 | + self._rd_consumer = rd_consumer |
| 147 | + |
| 148 | + self._output_ch = fiber.channel(100) |
| 149 | + |
| 150 | + self._poll_fiber = fiber.create(function() |
| 151 | + self:_poll() |
| 152 | + end) |
| 153 | +end |
| 154 | + |
| 155 | +function Consumer:stop(timeout_ms) |
| 156 | + if self._rd_consumer == nil then |
| 157 | + return "'stop' method must be called only after consumer was started " |
| 158 | + end |
| 159 | + |
| 160 | + if timeout_ms == nil then |
| 161 | + timeout_ms = 1000 |
| 162 | + end |
| 163 | + |
| 164 | + self._poll_fiber:cancel() |
| 165 | + self._output_ch:close() |
| 166 | + |
| 167 | + -- FIXME: handle this error |
| 168 | + local err = librdkafka.rd_kafka_consumer_close(self._rd_consumer) |
| 169 | + |
| 170 | + librdkafka.rd_kafka_destroy(self._rd_consumer) |
| 171 | + librdkafka.rd_kafka_wait_destroyed(timeout_ms) |
| 172 | + self._rd_consumer = nil |
| 173 | + |
| 174 | + return nil |
| 175 | +end |
| 176 | + |
| 177 | +function Consumer:subscribe(topics) |
| 178 | + if self._rd_consumer == nil then |
| 179 | + return "'add_topic' method must be called only after consumer was started " |
| 180 | + end |
| 181 | + |
| 182 | + local list = librdkafka.rd_kafka_topic_partition_list_new(#topics) |
| 183 | + for _, topic in ipairs(topics) do |
| 184 | + librdkafka.rd_kafka_topic_partition_list_add(list, topic, 0) |
| 185 | + end |
| 186 | + |
| 187 | + local err = nil |
| 188 | + local err_no = librdkafka.rd_kafka_subscribe(self._rd_consumer, list) |
| 189 | + if err_no ~= librdkafka.RD_KAFKA_RESP_ERR_NO_ERROR then |
| 190 | + err = ffi.string(librdkafka.rd_kafka_err2str(err_no)) |
| 191 | + end |
| 192 | + |
| 193 | + librdkafka.rd_kafka_topic_partition_list_destroy(list) |
| 194 | + |
| 195 | + return err |
| 196 | +end |
| 197 | + |
| 198 | +function Consumer:output() |
| 199 | + if self._rd_consumer == nil then |
| 200 | + return nil, "'output' method must be called only after consumer was started " |
| 201 | + end |
| 202 | + |
| 203 | + return self._output_ch, nil |
| 204 | +end |
| 205 | + |
| 206 | +function Consumer:commit_async(message) |
| 207 | + if self._rd_consumer == nil then |
| 208 | + return "'commit' method must be called only after consumer was started " |
| 209 | + end |
| 210 | + |
| 211 | + local err_no = librdkafka.rd_kafka_commit_message(self._rd_consumer, message._rd_message, 1) |
| 212 | + if err_no ~= librdkafka.RD_KAFKA_RESP_ERR_NO_ERROR then |
| 213 | + return ffi.string(librdkafka.rd_kafka_err2str(err_no)) |
| 214 | + end |
| 215 | + |
| 216 | + return nil |
| 217 | +end |
| 218 | + |
| 219 | +return { |
| 220 | + ConsumerConfig = ConsumerConfig, |
| 221 | + Consumer = Consumer, |
| 222 | +} |
0 commit comments