Class: Kafka::TopicProducer
- Inherits:
-
Object
- Object
- Kafka::TopicProducer
- Defined in:
- lib/fluent/plugin/kafka_producer_ext.rb
Instance Method Summary collapse
- #abort_transaction ⇒ Object
- #assign_partitions! ⇒ Object
- #begin_transaction ⇒ Object
- #buffer_bytesize ⇒ Object
- #buffer_messages ⇒ Object
-
#buffer_size ⇒ Integer
Returns the number of messages currently held in the buffer.
-
#clear_buffer ⇒ nil
Deletes all buffered messages.
- #commit_transaction ⇒ Object
- #deliver_messages ⇒ Object
- #deliver_messages_with_retries ⇒ Object
- #init_transactions ⇒ Object
-
#initialize(topic, cluster:, transaction_manager:, logger:, instrumenter:, compressor:, ack_timeout:, required_acks:, max_retries:, retry_backoff:, max_buffer_size:, max_buffer_bytesize:, partitioner:) ⇒ TopicProducer
constructor
A new instance of TopicProducer.
- #produce(value, key: nil, partition: nil, partition_key: nil, headers: EMPTY_HEADER, create_time: Time.now) ⇒ Object
-
#shutdown ⇒ nil
Closes all connections to the brokers.
- #transaction ⇒ Object
Constructor Details
#initialize(topic, cluster:, transaction_manager:, logger:, instrumenter:, compressor:, ack_timeout:, required_acks:, max_retries:, retry_backoff:, max_buffer_size:, max_buffer_bytesize:, partitioner:) ⇒ TopicProducer
Returns a new instance of TopicProducer.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/fluent/plugin/kafka_producer_ext.rb', line 78 def initialize(topic, cluster:, transaction_manager:, logger:, instrumenter:, compressor:, ack_timeout:, required_acks:, max_retries:, retry_backoff:, max_buffer_size:, max_buffer_bytesize:, partitioner:) @cluster = cluster @transaction_manager = transaction_manager @logger = logger @instrumenter = instrumenter @required_acks = required_acks == :all ? -1 : required_acks @ack_timeout = ack_timeout @max_retries = max_retries @retry_backoff = retry_backoff @max_buffer_size = max_buffer_size @max_buffer_bytesize = max_buffer_bytesize @compressor = compressor @partitioner = partitioner @topic = topic @cluster.add_target_topics(Set.new([topic])) # A buffer organized by topic/partition. @buffer = MessageBuffer.new # Messages added by `#produce` but not yet assigned a partition. @pending_message_queue = PendingMessageQueue.new end |
Instance Method Details
#abort_transaction ⇒ Object
170 171 172 |
# File 'lib/fluent/plugin/kafka_producer_ext.rb', line 170 def abort_transaction @transaction_manager.abort_transaction end |
#assign_partitions! ⇒ Object
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/fluent/plugin/kafka_producer_ext.rb', line 246 def assign_partitions! = [] partition_count = @cluster.partitions_for(@topic).count @pending_message_queue.each do || partition = .partition begin if partition.nil? partition = @partitioner.call(partition_count, ) end @buffer.write( value: .value, key: .key, headers: .headers, topic: .topic, partition: partition, create_time: .create_time, ) rescue Kafka::Error => e << end end if .any? .group_by(&:topic).each do |topic, | @logger.error "Failed to assign partitions to #{.count} messages in #{topic}" end @cluster.mark_as_stale! end @pending_message_queue.replace() end |
#begin_transaction ⇒ Object
162 163 164 |
# File 'lib/fluent/plugin/kafka_producer_ext.rb', line 162 def begin_transaction @transaction_manager.begin_transaction end |
#buffer_bytesize ⇒ Object
138 139 140 |
# File 'lib/fluent/plugin/kafka_producer_ext.rb', line 138 def buffer_bytesize @pending_message_queue.bytesize + @buffer.bytesize end |
#buffer_messages ⇒ Object
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/fluent/plugin/kafka_producer_ext.rb', line 282 def = [] @pending_message_queue.each do || << end @buffer.each do |topic, partition, | .each do || << PendingMessage.new( value: .value, key: .key, headers: .headers, topic: topic, partition: partition, partition_key: nil, create_time: .create_time ) end end end |
#buffer_size ⇒ Integer
Returns the number of messages currently held in the buffer.
134 135 136 |
# File 'lib/fluent/plugin/kafka_producer_ext.rb', line 134 def buffer_size @pending_message_queue.size + @buffer.size end |
#clear_buffer ⇒ nil
Deletes all buffered messages.
145 146 147 148 |
# File 'lib/fluent/plugin/kafka_producer_ext.rb', line 145 def clear_buffer @buffer.clear @pending_message_queue.clear end |
#commit_transaction ⇒ Object
166 167 168 |
# File 'lib/fluent/plugin/kafka_producer_ext.rb', line 166 def commit_transaction @transaction_manager.commit_transaction end |
#deliver_messages ⇒ Object
124 125 126 127 128 129 |
# File 'lib/fluent/plugin/kafka_producer_ext.rb', line 124 def # There's no need to do anything if the buffer is empty. return if buffer_size == 0 end |
#deliver_messages_with_retries ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/fluent/plugin/kafka_producer_ext.rb', line 186 def attempt = 0 #@cluster.add_target_topics(@target_topics) operation = ProduceOperation.new( cluster: @cluster, transaction_manager: @transaction_manager, buffer: @buffer, required_acks: @required_acks, ack_timeout: @ack_timeout, compressor: @compressor, logger: @logger, instrumenter: @instrumenter, ) loop do attempt += 1 begin @cluster. rescue ConnectionError => e raise DeliveryFailed.new(e, ) end assign_partitions! operation.execute if @required_acks.zero? # No response is returned by the brokers, so we can't know which messages # have been successfully written. Our only option is to assume that they all # have. @buffer.clear end if buffer_size.zero? break elsif attempt <= @max_retries @logger.warn "Failed to send all messages; attempting retry #{attempt} of #{@max_retries} after #{@retry_backoff}s" sleep @retry_backoff else @logger.error "Failed to send all messages; keeping remaining messages in buffer" break end end unless @pending_message_queue.empty? # Mark the cluster as stale in order to force a cluster metadata refresh. @cluster.mark_as_stale! raise DeliveryFailed.new("Failed to assign partitions to #{@pending_message_queue.size} messages", ) end unless @buffer.empty? partitions = @buffer.map {|topic, partition, _| "#{topic}/#{partition}" }.join(", ") raise DeliveryFailed.new("Failed to send messages to #{partitions}", ) end end |
#init_transactions ⇒ Object
158 159 160 |
# File 'lib/fluent/plugin/kafka_producer_ext.rb', line 158 def init_transactions @transaction_manager.init_transactions end |
#produce(value, key: nil, partition: nil, partition_key: nil, headers: EMPTY_HEADER, create_time: Time.now) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/fluent/plugin/kafka_producer_ext.rb', line 102 def produce(value, key: nil, partition: nil, partition_key: nil, headers: EMPTY_HEADER, create_time: Time.now) = PendingMessage.new( value: value, key: key, headers: headers, topic: @topic, partition: partition, partition_key: partition_key, create_time: create_time ) # If the producer is in transactional mode, all the message production # must be used when the producer is currently in transaction if @transaction_manager.transactional? && !@transaction_manager.in_transaction? raise 'You must trigger begin_transaction before producing messages' end @pending_message_queue.write() nil end |
#shutdown ⇒ nil
Closes all connections to the brokers.
153 154 155 156 |
# File 'lib/fluent/plugin/kafka_producer_ext.rb', line 153 def shutdown @transaction_manager.close @cluster.disconnect end |
#transaction ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/fluent/plugin/kafka_producer_ext.rb', line 174 def transaction raise 'This method requires a block' unless block_given? begin_transaction yield commit_transaction rescue Kafka::Producer::AbortTransaction abort_transaction rescue abort_transaction raise end |