Method: Fluent::Plugin::Output#start

Defined in:
lib/fluent/plugin/output.rb

#startObject



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'lib/fluent/plugin/output.rb', line 447

def start
  super

  if @buffering.nil?
    @buffering = prefer_buffered_processing
    if !@buffering && @buffer
      @buffer.terminate # it's not started, so terminate will be enough
      # At here, this plugin works as non-buffered plugin.
      # Un-assign @buffer not to show buffering metrics (e.g., in_monitor_agent)
      @buffer = nil
    end
  end

  if @buffering
    m = method(:emit_buffered)
    singleton_class.module_eval do
      define_method(:emit_events, m)
    end

    @custom_format = implement?(:custom_format)
    @enable_msgpack_streamer = @custom_format ? formatted_to_msgpack_binary : true
    @delayed_commit = if implement?(:buffered) && implement?(:delayed_commit)
                        prefer_delayed_commit
                      else
                        implement?(:delayed_commit)
                      end
    @delayed_commit_timeout = @buffer_config.delayed_commit_timeout
  else # !@buffering
    m = method(:emit_sync)
    singleton_class.module_eval do
      define_method(:emit_events, m)
    end
  end

  if @buffering && !@as_secondary
    @retry = nil
    @retry_mutex = Mutex.new

    @buffer.start

    @output_enqueue_thread = nil
    @output_enqueue_thread_running = true

    @output_flush_threads = []
    @output_flush_threads_mutex = Mutex.new
    @output_flush_threads_running = true

    # mainly for test: detect enqueue works as code below:
    #   @output.interrupt_flushes
    #   # emits
    #   @output.enqueue_thread_wait
    @output_flush_interrupted = false
    @output_enqueue_thread_mutex = Mutex.new
    @output_enqueue_thread_waiting = false

    @dequeued_chunks = []
    @dequeued_chunks_mutex = Mutex.new

    @output_flush_thread_current_position = 0
    @buffer_config.flush_thread_count.times do |i|
      thread_title = "flush_thread_#{i}".to_sym
      thread_state = FlushThreadState.new(nil, nil, Mutex.new, ConditionVariable.new)
      thread = thread_create(thread_title) do
        flush_thread_run(thread_state)
      end
      thread_state.thread = thread
      @output_flush_threads_mutex.synchronize do
        @output_flush_threads << thread_state
      end
    end

    if !@under_plugin_development && (@flush_mode == :interval || @chunk_key_time)
      @output_enqueue_thread = thread_create(:enqueue_thread, &method(:enqueue_thread_run))
    end
  end
  @secondary.start if @secondary
end