Class: Fluent::Plugin::ForwardOutput::Node
- Inherits:
-
Object
- Object
- Fluent::Plugin::ForwardOutput::Node
- Extended by:
- Forwardable
- Defined in:
- lib/fluent/plugin/out_forward.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#failure ⇒ Object
readonly
for test.
-
#sockaddr ⇒ Object
readonly
used by on_udp_heartbeat_response_recv.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#usock ⇒ Object
Returns the value of attribute usock.
Instance Method Summary collapse
- #available? ⇒ Boolean
- #disable! ⇒ Object
- #establish_connection(sock, ri) ⇒ Object
- #heartbeat(detect = true) ⇒ Object
-
#initialize(sender, server, failure:, connection_manager:, ack_handler:) ⇒ Node
constructor
A new instance of Node.
- #resolved_host ⇒ Object
- #send_data(tag, chunk) ⇒ Object
- #send_data_actual(sock, tag, chunk) ⇒ Object
-
#send_heartbeat ⇒ Boolean
FORWARD_TCP_HEARTBEAT_DATA = FORWARD_HEADER + ''.to_msgpack + [].to_msgpack.
- #standby? ⇒ Boolean
- #tick ⇒ Object
- #validate_host_resolution! ⇒ Object
- #verify_connection ⇒ Object
Constructor Details
#initialize(sender, server, failure:, connection_manager:, ack_handler:) ⇒ Node
Returns a new instance of Node.
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 |
# File 'lib/fluent/plugin/out_forward.rb', line 565 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&.self_hostname, shared_key: server.shared_key || 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
#failure ⇒ Object (readonly)
for test
609 610 611 |
# File 'lib/fluent/plugin/out_forward.rb', line 609 def failure @failure end |
#sockaddr ⇒ Object (readonly)
used by on_udp_heartbeat_response_recv
608 609 610 |
# File 'lib/fluent/plugin/out_forward.rb', line 608 def sockaddr @sockaddr end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
607 608 609 |
# File 'lib/fluent/plugin/out_forward.rb', line 607 def state @state end |
#usock ⇒ Object
Returns the value of attribute usock.
605 606 607 |
# File 'lib/fluent/plugin/out_forward.rb', line 605 def usock @usock end |
Instance Method Details
#available? ⇒ Boolean
615 616 617 |
# File 'lib/fluent/plugin/out_forward.rb', line 615 def available? @available end |
#disable! ⇒ Object
619 620 621 |
# File 'lib/fluent/plugin/out_forward.rb', line 619 def disable! @available = false end |
#establish_connection(sock, ri) ⇒ Object
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 |
# File 'lib/fluent/plugin/out_forward.rb', line 633 def establish_connection(sock, ri) start_time = Fluent::Clock.now timeout = @sender.hard_timeout while ri.state != :established # Check for timeout to prevent infinite loop if Fluent::Clock.now - start_time > timeout @log.warn "handshake timeout after #{timeout}s", host: @host, port: @port disable! break end 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
813 814 815 816 817 818 819 820 821 822 823 |
# File 'lib/fluent/plugin/out_forward.rb', line 813 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_host ⇒ Object
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 |
# File 'lib/fluent/plugin/out_forward.rb', line 754 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
705 706 707 708 709 710 711 712 713 714 |
# File 'lib/fluent/plugin/out_forward.rb', line 705 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
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 |
# File 'lib/fluent/plugin/out_forward.rb', line 683 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_heartbeat ⇒ Boolean
FORWARD_TCP_HEARTBEAT_DATA = FORWARD_HEADER + ''.to_msgpack + [].to_msgpack
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 |
# File 'lib/fluent/plugin/out_forward.rb', line 719 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_heartbeat_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
623 624 625 |
# File 'lib/fluent/plugin/out_forward.rb', line 623 def standby? @standby end |
#tick ⇒ Object
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 |
# File 'lib/fluent/plugin/out_forward.rb', line 783 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
611 612 613 |
# File 'lib/fluent/plugin/out_forward.rb', line 611 def validate_host_resolution! resolved_host end |
#verify_connection ⇒ Object
627 628 629 630 631 |
# File 'lib/fluent/plugin/out_forward.rb', line 627 def verify_connection connect do |sock, ri| ensure_established_connection(sock, ri) end end |