Class: Fluent::Plugin::ForwardOutput::Node

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/fluent/plugin/out_forward.rb

Direct Known Subclasses

NoneHeartbeatNode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sender, server, failure:, connection_manager:, ack_handler:) ⇒ Node

Returns a new instance of Node.



544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# File 'lib/fluent/plugin/out_forward.rb', line 544

def initialize(sender, server, failure:, connection_manager:, ack_handler:)
  @sender = sender
  @log = sender.log
  @compress = sender.compress
  @server = server

  @name = server.name
  @host = server.host
  @port = server.port
  @weight = server.weight
  @standby = server.standby
  @failure = failure
  @available = true

  # @hostname is used for certificate verification & TLS SNI
  host_is_hostname = !(IPAddr.new(@host) rescue false)
  @hostname = case
              when host_is_hostname then @host
              when @name then @name
              else nil
              end

  @usock = nil

  @handshake = HandshakeProtocol.new(
    log: @log,
    hostname: sender.security && sender.security.self_hostname,
    shared_key: server.shared_key || (sender.security && sender.security.shared_key) || '',
    password: server.password || '',
    username: server.username || '',
  )

  @resolved_host = nil
  @resolved_time = 0
  @resolved_once = false

  @connection_manager = connection_manager
  @ack_handler = ack_handler
end

Instance Attribute Details

#failureObject (readonly)

for test



588
589
590
# File 'lib/fluent/plugin/out_forward.rb', line 588

def failure
  @failure
end

#sockaddrObject (readonly)

used by on_udp_heatbeat_response_recv



587
588
589
# File 'lib/fluent/plugin/out_forward.rb', line 587

def sockaddr
  @sockaddr
end

#stateObject (readonly)

Returns the value of attribute state.



586
587
588
# File 'lib/fluent/plugin/out_forward.rb', line 586

def state
  @state
end

#usockObject

Returns the value of attribute usock.



584
585
586
# File 'lib/fluent/plugin/out_forward.rb', line 584

def usock
  @usock
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


594
595
596
# File 'lib/fluent/plugin/out_forward.rb', line 594

def available?
  @available
end

#disable!Object



598
599
600
# File 'lib/fluent/plugin/out_forward.rb', line 598

def disable!
  @available = false
end

#establish_connection(sock, ri) ⇒ Object



612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
# File 'lib/fluent/plugin/out_forward.rb', line 612

def establish_connection(sock, ri)
  while ri.state != :established
    begin
      # TODO: On Ruby 2.2 or earlier, read_nonblock doesn't work expectedly.
      # We need rewrite around here using new socket/server plugin helper.
      buf = sock.read_nonblock(@sender.read_length)
      if buf.empty?
        sleep @sender.read_interval
        next
      end
      Fluent::MessagePackFactory.msgpack_unpacker.feed_each(buf) do |data|
        if @handshake.invoke(sock, ri, data) == :established
          @log.debug "connection established", host: @host, port: @port
        end
      end
    rescue IO::WaitReadable
      # If the exception is Errno::EWOULDBLOCK or Errno::EAGAIN, it is extended by IO::WaitReadable.
      # So IO::WaitReadable can be used to rescue the exceptions for retrying read_nonblock.
      # https//docs.ruby-lang.org/en/2.3.0/IO.html#method-i-read_nonblock
      sleep @sender.read_interval unless ri.state == :established
    rescue SystemCallError => e
      @log.warn "disconnected by error", host: @host, port: @port, error: e
      disable!
      break
    rescue EOFError
      @log.warn "disconnected", host: @host, port: @port
      disable!
      break
    rescue HeloError => e
      @log.warn "received invalid helo message from #{@name}"
      disable!
      break
    rescue PingpongError => e
      @log.warn "connection refused to #{@name || @host}: #{e.message}"
      disable!
      break
    end
  end
end

#heartbeat(detect = true) ⇒ Object



782
783
784
785
786
787
788
789
790
791
792
# File 'lib/fluent/plugin/out_forward.rb', line 782

def heartbeat(detect=true)
  now = Time.now.to_f
  @failure.add(now)
  if detect && !available? && @failure.sample_size > @sender.recover_sample_size
    @available = true
    @log.warn "recovered forwarding server '#{@name}'", host: @host, port: @port
    true
  else
    nil
  end
end

#resolved_hostObject



723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
# File 'lib/fluent/plugin/out_forward.rb', line 723

def resolved_host
  case @sender.expire_dns_cache
  when 0
    # cache is disabled
    resolve_dns!

  when nil
    # persistent cache
    @resolved_host ||= resolve_dns!

  else
    now = Fluent::EventTime.now
    rh = @resolved_host
    if !rh || now - @resolved_time >= @sender.expire_dns_cache
      rh = @resolved_host = resolve_dns!
      @resolved_time = now
    end
    rh
  end
end

#send_data(tag, chunk) ⇒ Object



674
675
676
677
678
679
680
681
682
683
# File 'lib/fluent/plugin/out_forward.rb', line 674

def send_data(tag, chunk)
  ack = @ack_handler && @ack_handler.create_ack(chunk.unique_id, self)
  connect(nil, ack: ack) do |sock, ri|
    ensure_established_connection(sock, ri)
    send_data_actual(sock, tag, chunk)
  end

  heartbeat(false)
  nil
end

#send_data_actual(sock, tag, chunk) ⇒ Object



652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
# File 'lib/fluent/plugin/out_forward.rb', line 652

def send_data_actual(sock, tag, chunk)
  option = { 'size' => chunk.size, 'compressed' => @compress }
  option['chunk'] = Base64.encode64(chunk.unique_id) if @ack_handler

  # https://github.com/fluent/fluentd/wiki/Forward-Protocol-Specification-v1#packedforward-mode
  # out_forward always uses str32 type for entries.
  # str16 can store only 64kbytes, and it should be much smaller than buffer chunk size.

  tag = tag.dup.force_encoding(Encoding::UTF_8)

  sock.write @sender.forward_header                    # array, size=3
  sock.write tag.to_msgpack                            # 1. tag: String (str)
  chunk.open(compressed: @compress) do |chunk_io|
    entries = [0xdb, chunk_io.size].pack('CN')
    sock.write entries.force_encoding(Encoding::UTF_8) # 2. entries: String (str32)
    IO.copy_stream(chunk_io, sock)                     #    writeRawBody(packed_es)
  end
  sock.write option.to_msgpack                         # 3. option: Hash(map)

  # TODO: use bin32 for non-utf8 content(entries) when old msgpack-ruby (0.5.x or earlier) not supported
end

#send_heartbeatBoolean

FORWARD_TCP_HEARTBEAT_DATA = FORWARD_HEADER + ”.to_msgpack + [].to_msgpack

Returns:

  • (Boolean)

    return true if it needs to rebuild nodes



688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
# File 'lib/fluent/plugin/out_forward.rb', line 688

def send_heartbeat
  begin
    dest_addr = resolved_host
    @resolved_once = true
  rescue ::SocketError => e
    if !@resolved_once && @sender.ignore_network_errors_at_startup
      @log.warn "failed to resolve node name in heartbeating", server: @name || @host, error: e
      return false
    end
    raise
  end

  case @sender.heartbeat_type
  when :transport
    connect(dest_addr) do |sock, ri|
      ensure_established_connection(sock, ri)

      ## don't send any data to not cause a compatibility problem
      # sock.write FORWARD_TCP_HEARTBEAT_DATA

      # successful tcp connection establishment is considered as valid heartbeat.
      # When heartbeat is succeeded after detached, return true. It rebuilds weight array.
      heartbeat(true)
    end
  when :udp
    @usock.send "\0", 0, Socket.pack_sockaddr_in(@port, dest_addr)
    # response is going to receive at on_udp_heatbeat_response_recv
    false
  when :none # :none doesn't use this class
    raise "BUG: heartbeat_type none must not use Node"
  else
    raise "BUG: unknown heartbeat_type '#{@sender.heartbeat_type}'"
  end
end

#standby?Boolean

Returns:

  • (Boolean)


602
603
604
# File 'lib/fluent/plugin/out_forward.rb', line 602

def standby?
  @standby
end

#tickObject



752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
# File 'lib/fluent/plugin/out_forward.rb', line 752

def tick
  now = Time.now.to_f
  unless available?
    if @failure.hard_timeout?(now)
      @failure.clear
    end
    return nil
  end

  if @failure.hard_timeout?(now)
    @log.warn "detached forwarding server '#{@name}'", host: @host, port: @port, hard_timeout: true
    disable!
    @resolved_host = nil  # expire cached host
    @failure.clear
    return true
  end

  if @sender.phi_failure_detector
    phi = @failure.phi(now)
    if phi > @sender.phi_threshold
      @log.warn "detached forwarding server '#{@name}'", host: @host, port: @port, phi: phi, phi_threshold: @sender.phi_threshold
      disable!
      @resolved_host = nil  # expire cached host
      @failure.clear
      return true
    end
  end
  false
end

#validate_host_resolution!Object



590
591
592
# File 'lib/fluent/plugin/out_forward.rb', line 590

def validate_host_resolution!
  resolved_host
end

#verify_connectionObject



606
607
608
609
610
# File 'lib/fluent/plugin/out_forward.rb', line 606

def verify_connection
  connect do |sock, ri|
    ensure_established_connection(sock, ri)
  end
end