Class: Fluent::Rdkafka2Output

Inherits:
Output
  • Object
show all
Includes:
KafkaPluginUtil::SSLSettings, KafkaPluginUtil::SaslSettings
Defined in:
lib/fluent/plugin/out_rdkafka2.rb

Defined Under Namespace

Classes: EnqueueRate

Constant Summary

Constants included from KafkaPluginUtil::SSLSettings

KafkaPluginUtil::SSLSettings::DummyFormatter

Instance Method Summary collapse

Methods included from KafkaPluginUtil::SaslSettings

included

Methods included from KafkaPluginUtil::SSLSettings

included, #pickup_ssl_endpoint, #read_ssl_file

Constructor Details

#initializeRdkafka2Output

Returns a new instance of Rdkafka2Output.



164
165
166
167
168
169
170
171
172
173
# File 'lib/fluent/plugin/out_rdkafka2.rb', line 164

def initialize
  super

  @producers = nil
  @producers_mutex = nil
  @shared_producer = nil
  @enqueue_rate = nil
  @writing_threads_mutex = Mutex.new
  @writing_threads = Set.new
end

Instance Method Details

#build_configObject



243
244
245
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
281
282
283
284
285
286
287
288
289
# File 'lib/fluent/plugin/out_rdkafka2.rb', line 243

def build_config
  config = {:"bootstrap.servers" => @brokers}

  if @ssl_ca_cert && @ssl_ca_cert[0]
    ssl = true
    config[:"ssl.ca.location"] = @ssl_ca_cert[0]
    config[:"ssl.certificate.location"] = @ssl_client_cert if @ssl_client_cert
    config[:"ssl.key.location"] = @ssl_client_cert_key if @ssl_client_cert_key
    config[:"ssl.key.password"] = @ssl_client_cert_key_password if @ssl_client_cert_key_password
  end

  if @principal
    sasl = true
    config[:"sasl.mechanisms"] = "GSSAPI"
    config[:"sasl.kerberos.principal"] = @principal
    config[:"sasl.kerberos.service.name"] = @service_name if @service_name
    config[:"sasl.kerberos.keytab"] = @keytab if @keytab
  end

  if ssl && sasl
    security_protocol = "SASL_SSL"
  elsif ssl && !sasl
    security_protocol = "SSL"
  elsif !ssl && sasl
    security_protocol = "SASL_PLAINTEXT"
  else
    security_protocol = "PLAINTEXT"
  end
  config[:"security.protocol"] = security_protocol

  config[:"compression.codec"] = @compression_codec if @compression_codec
  config[:"message.send.max.retries"] = @max_send_retries if @max_send_retries
  config[:"request.required.acks"] = @required_acks if @required_acks
  config[:"request.timeout.ms"] = @ack_timeout * 1000 if @ack_timeout
  config[:"queue.buffering.max.ms"] = @rdkafka_buffering_max_ms if @rdkafka_buffering_max_ms
  config[:"queue.buffering.max.messages"] = @rdkafka_buffering_max_messages if @rdkafka_buffering_max_messages
  config[:"message.max.bytes"] = @rdkafka_message_max_bytes if @rdkafka_message_max_bytes
  config[:"batch.num.messages"] = @rdkafka_message_max_num if @rdkafka_message_max_num
  config[:"sasl.username"] = @username if @username
  config[:"sasl.password"] = @password if @password

  @rdkafka_options.each { |k, v|
    config[k.to_sym] = v
  }

  config
end

#close_producer(producer) ⇒ Object



341
342
343
344
345
# File 'lib/fluent/plugin/out_rdkafka2.rb', line 341

def close_producer(producer)
  unless producer.close(10)
    log.warn("Queue is forcefully closed after 10 seconds wait")
  end
end

#configure(conf) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
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
# File 'lib/fluent/plugin/out_rdkafka2.rb', line 175

def configure(conf)
  super
  log.instance_eval {
    def add(level, message = nil)
      if message.nil?
        if block_given?
          message = yield
        else
          return
        end
      end

      # Follow rdkakfa's log level. See also rdkafka-ruby's bindings.rb: https://github.com/appsignal/rdkafka-ruby/blob/e5c7261e3f2637554a5c12b924be297d7dca1328/lib/rdkafka/bindings.rb#L117
      case level
      when Logger::FATAL
        self.fatal(message)
      when Logger::ERROR
        self.error(message)
      when Logger::WARN
        self.warn(message)
      when Logger::INFO
        self.info(message)
      when Logger::DEBUG
        self.debug(message)
      else
        self.trace(message)
      end
    end
  }
  Rdkafka::Config.logger = log
  config = build_config
  @rdkafka = Rdkafka::Config.new(config)

  if @default_topic.nil?
    if @chunk_keys.include?(@topic_key) && !@chunk_key_tag
      log.warn "Use '#{@topic_key}' field of event record for topic but no fallback. Recommend to set default_topic or set 'tag' in buffer chunk keys like <buffer #{@topic_key},tag>"
    end
  else
    if @chunk_key_tag
      log.warn "default_topic is set. Fluentd's event tag is not used for topic"
    end
  end

  formatter_conf = conf.elements('format').first
  unless formatter_conf
    raise Fluent::ConfigError, "<format> section is required."
  end
  unless formatter_conf["@type"]
    raise Fluent::ConfigError, "format/@type is required."
  end
  @formatter_proc = setup_formatter(formatter_conf)
  @topic_key_sym = @topic_key.to_sym

  @headers_from_record_accessors = {}
  @headers_from_record.each do |key, value|
    @headers_from_record_accessors[key] = record_accessor_create(value)
  end

  @exclude_field_accessors = @exclude_fields.map do |field|
    record_accessor_create(field)
  end

  @enqueue_rate = EnqueueRate.new(@max_enqueue_bytes_per_second) unless @max_enqueue_bytes_per_second.nil?

  @record_field_accessor = nil
  @record_field_accessor = record_accessor_create(@record_key) unless @record_key.nil?
end

#enqueue_with_retry(producer, topic, record_buf, message_key, partition, headers, time) ⇒ Object



440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/fluent/plugin/out_rdkafka2.rb', line 440

def enqueue_with_retry(producer, topic, record_buf, message_key, partition, headers, time)
  attempt = 0
  loop do
    begin
      @enqueue_rate.raise_if_limit_exceeded(record_buf.bytesize) if @enqueue_rate
      return producer.produce(topic: topic, payload: record_buf, key: message_key, partition: partition, headers: headers, timestamp: @use_event_time ? Time.at(time) : nil)
    rescue EnqueueRate::LimitExceeded => e
      @enqueue_rate.revert if @enqueue_rate
      duration = e.next_retry_clock - Fluent::Clock.now
      sleep(duration) if duration > 0.0
    rescue Exception => e
      @enqueue_rate.revert if @enqueue_rate
      if e.respond_to?(:code) && e.code == :queue_full
        if attempt <= @max_enqueue_retries
          log.warn "Failed to enqueue message; attempting retry #{attempt} of #{@max_enqueue_retries} after #{@enqueue_retry_backoff}s"
          sleep @enqueue_retry_backoff
          attempt += 1
        else
          raise "Failed to enqueue message although tried retry #{@max_enqueue_retries} times"
        end
      else
        raise e
      end
    end
  end
end

#get_producerObject



347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/fluent/plugin/out_rdkafka2.rb', line 347

def get_producer
  if @share_producer
    @shared_producer
  else
    @producers_mutex.synchronize {
      producer = @producers[Thread.current.object_id]
      unless producer
        producer = @rdkafka.producer
        @producers[Thread.current.object_id] = producer
      end
      producer
    }
  end
end

#multi_workers_ready?Boolean

Returns:

  • (Boolean)


302
303
304
# File 'lib/fluent/plugin/out_rdkafka2.rb', line 302

def multi_workers_ready?
  true
end

#setup_formatter(conf) ⇒ Object



362
363
364
365
366
367
368
369
370
371
372
# File 'lib/fluent/plugin/out_rdkafka2.rb', line 362

def setup_formatter(conf)
  type = conf['@type']
  case type
  when 'ltsv'
    require 'ltsv'
    Proc.new { |tag, time, record| LTSV.dump(record) }
  else
    @formatter = formatter_create(usage: 'rdkafka-plugin', conf: conf)
    @formatter.method(:format)
  end
end

#shutdownObject



316
317
318
319
320
# File 'lib/fluent/plugin/out_rdkafka2.rb', line 316

def shutdown
  super
  wait_writing_threads
  shutdown_producers
end

#shutdown_producersObject



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/fluent/plugin/out_rdkafka2.rb', line 322

def shutdown_producers
  if @share_producer
    close_producer(@shared_producer)
    @shared_producer = nil
  else
    @producers_mutex.synchronize {
      shutdown_threads = @producers.map { |key, producer|
        th = Thread.new {
          close_producer(producer)
        }
        th.abort_on_exception = true
        th
      }
      shutdown_threads.each { |th| th.join }
      @producers = {}
    }
  end
end

#startObject



291
292
293
294
295
296
297
298
299
300
# File 'lib/fluent/plugin/out_rdkafka2.rb', line 291

def start
  if @share_producer
    @shared_producer = @rdkafka.producer
  else
    @producers = {}
    @producers_mutex = Mutex.new
  end

  super
end

#wait_writing_threadsObject



306
307
308
309
310
311
312
313
314
# File 'lib/fluent/plugin/out_rdkafka2.rb', line 306

def wait_writing_threads
  done = false
  until done do
    @writing_threads_mutex.synchronize do
      done = true if @writing_threads.empty?
    end
    sleep(1) unless done
  end
end

#write(chunk) ⇒ Object



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/fluent/plugin/out_rdkafka2.rb', line 374

def write(chunk)
  @writing_threads_mutex.synchronize { @writing_threads.add(Thread.current) }
  tag = chunk..tag
  topic = if @topic
            extract_placeholders(@topic, chunk)
          else
            (chunk..variables && chunk..variables[@topic_key_sym]) || @default_topic || tag
          end

  handlers = []

  headers = @headers.clone

  begin
    producer = get_producer
    chunk.msgpack_each { |time, record|
      begin
        record = inject_values_to_record(tag, time, record)
        record.delete(@topic_key) if @exclude_topic_key
        partition = (@exclude_partition ? record.delete(@partition_key) : record[@partition_key]) || @default_partition
        message_key = (@exclude_message_key ? record.delete(@message_key_key) : record[@message_key_key]) || @default_message_key

        @headers_from_record_accessors.each do |key, header_accessor|
          headers[key] = header_accessor.call(record)
        end

        unless @exclude_fields.empty?
          @exclude_field_accessors.each do |exclude_field_acessor|
            exclude_field_acessor.delete(record)
          end
        end

        record = @record_field_accessor.call(record) unless @record_field_accessor.nil?
        record_buf = @formatter_proc.call(tag, time, record)
        record_buf_bytes = record_buf.bytesize
        if @max_send_limit_bytes && record_buf_bytes > @max_send_limit_bytes
          log.warn "record size exceeds max_send_limit_bytes. Skip event:", :time => time, :record_size => record_buf_bytes
          log.debug "Skipped event:", :record => record
          next
        end
      rescue StandardError => e
        log.warn "unexpected error during format record. Skip broken event:", :error => e.to_s, :error_class => e.class.to_s, :time => time, :record => record
        next
      end

      handler = enqueue_with_retry(producer, topic, record_buf, message_key, partition, headers, time)
      if @rdkafka_delivery_handle_poll_timeout != 0
        handlers << handler
      end
    }
    handlers.each { |handler|
      handler.wait(max_wait_timeout: @rdkafka_delivery_handle_poll_timeout)
    }
  end
rescue Exception => e
  if @discard_kafka_delivery_failed
    log.warn "Delivery failed. Discard events:", :error => e.to_s, :error_class => e.class.to_s, :tag => tag
  else
    log.warn "Send exception occurred: #{e} at #{e.backtrace.first}"
    # Raise exception to retry sendind messages
    raise e
  end
ensure
  @writing_threads_mutex.synchronize { @writing_threads.delete(Thread.current) }
end