Class: Fluent::Plugin::ForwardOutput::Node
- Inherits:
-
Object
- Object
- Fluent::Plugin::ForwardOutput::Node
- Defined in:
- lib/fluent/plugin/out_forward.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#available ⇒ Object
readonly
for test.
-
#failure ⇒ Object
readonly
for test.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#sockaddr ⇒ Object
readonly
used by on_heartbeat.
-
#standby ⇒ Object
readonly
Returns the value of attribute standby.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#usock ⇒ Object
Returns the value of attribute usock.
-
#weight ⇒ Object
readonly
Returns the value of attribute weight.
Instance Method Summary collapse
- #available? ⇒ Boolean
- #check_helo(message) ⇒ Object
- #check_pong(message) ⇒ Object
- #disable! ⇒ Object
- #establish_connection(sock) ⇒ Object
- #generate_ping ⇒ Object
- #generate_salt ⇒ Object
- #heartbeat(detect = true) ⇒ Object
-
#initialize(sender, server, failure:) ⇒ Node
constructor
A new instance of Node.
- #on_read(sock, data) ⇒ Object
- #resolved_host ⇒ Object
- #send_data(tag, chunk) ⇒ Object
- #send_data_actual(sock, tag, chunk) ⇒ Object
-
#send_heartbeat ⇒ Object
FORWARD_TCP_HEARTBEAT_DATA = FORWARD_HEADER + ”.to_msgpack + [].to_msgpack.
- #standby? ⇒ Boolean
- #tick ⇒ Object
- #validate_host_resolution! ⇒ Object
Constructor Details
#initialize(sender, server, failure:) ⇒ Node
Returns a new instance of Node.
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 |
# File 'lib/fluent/plugin/out_forward.rb', line 505 def initialize(sender, server, failure:) @sender = sender @log = sender.log @compress = sender.compress @name = server.name @host = server.host @port = server.port @weight = server.weight @standby = server.standby @failure = failure @available = true @state = nil # @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 @username = server.username @password = server.password @shared_key = server.shared_key || (sender.security && sender.security.shared_key) || "" @shared_key_salt = generate_salt @shared_key_nonce = "" @unpacker = Fluent::Engine.msgpack_unpacker @resolved_host = nil @resolved_time = 0 @resolved_once = false end |
Instance Attribute Details
#available ⇒ Object (readonly)
for test
546 547 548 |
# File 'lib/fluent/plugin/out_forward.rb', line 546 def available @available end |
#failure ⇒ Object (readonly)
for test
546 547 548 |
# File 'lib/fluent/plugin/out_forward.rb', line 546 def failure @failure end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
544 545 546 |
# File 'lib/fluent/plugin/out_forward.rb', line 544 def host @host end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
544 545 546 |
# File 'lib/fluent/plugin/out_forward.rb', line 544 def name @name end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
544 545 546 |
# File 'lib/fluent/plugin/out_forward.rb', line 544 def port @port end |
#sockaddr ⇒ Object (readonly)
used by on_heartbeat
545 546 547 |
# File 'lib/fluent/plugin/out_forward.rb', line 545 def sockaddr @sockaddr end |
#standby ⇒ Object (readonly)
Returns the value of attribute standby.
544 545 546 |
# File 'lib/fluent/plugin/out_forward.rb', line 544 def standby @standby end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
544 545 546 |
# File 'lib/fluent/plugin/out_forward.rb', line 544 def state @state end |
#usock ⇒ Object
Returns the value of attribute usock.
542 543 544 |
# File 'lib/fluent/plugin/out_forward.rb', line 542 def usock @usock end |
#weight ⇒ Object (readonly)
Returns the value of attribute weight.
544 545 546 |
# File 'lib/fluent/plugin/out_forward.rb', line 544 def weight @weight end |
Instance Method Details
#available? ⇒ Boolean
552 553 554 |
# File 'lib/fluent/plugin/out_forward.rb', line 552 def available? @available end |
#check_helo(message) ⇒ Object
750 751 752 753 754 755 756 757 758 759 760 761 |
# File 'lib/fluent/plugin/out_forward.rb', line 750 def check_helo() @log.debug "checking helo" # ['HELO', options(hash)] unless .size == 2 && [0] == 'HELO' return false end opts = [1] || {} # make shared_key_check failed (instead of error) if protocol version mismatch exist @shared_key_nonce = opts['nonce'] || '' @authentication = opts['auth'] || '' true end |
#check_pong(message) ⇒ Object
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 |
# File 'lib/fluent/plugin/out_forward.rb', line 782 def check_pong() @log.debug "checking pong" # ['PONG', bool(authentication result), 'reason if authentication failed', # self_hostname, sha512\_hex(salt + self_hostname + nonce + sharedkey)] unless .size == 5 && [0] == 'PONG' return false, 'invalid format for PONG message' end _pong, auth_result, reason, hostname, shared_key_hexdigest = unless auth_result return false, 'authentication failed: ' + reason end if hostname == @sender.security.self_hostname return false, 'same hostname between input and output: invalid configuration' end clientside = Digest::SHA512.new.update(@shared_key_salt).update(hostname).update(@shared_key_nonce).update(@shared_key).hexdigest unless shared_key_hexdigest == clientside return false, 'shared key mismatch' end return true, nil end |
#disable! ⇒ Object
556 557 558 |
# File 'lib/fluent/plugin/out_forward.rb', line 556 def disable! @available = false end |
#establish_connection(sock) ⇒ Object
564 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 |
# File 'lib/fluent/plugin/out_forward.rb', line 564 def establish_connection(sock) while available? && @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 @unpacker.feed_each(buf) do |data| on_read(sock, data) 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. # http://docs.ruby-lang.org/en/2.3.0/IO.html#method-i-read_nonblock sleep @sender.read_interval unless @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 end end end |
#generate_ping ⇒ Object
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 763 def generate_ping @log.debug "generating ping" # ['PING', self_hostname, sharedkey\_salt, sha512\_hex(sharedkey\_salt + self_hostname + nonce + shared_key), # username || '', sha512\_hex(auth\_salt + username + password) || ''] shared_key_hexdigest = Digest::SHA512.new.update(@shared_key_salt) .update(@sender.security.self_hostname) .update(@shared_key_nonce) .update(@shared_key) .hexdigest ping = ['PING', @sender.security.self_hostname, @shared_key_salt, shared_key_hexdigest] if !@authentication.empty? password_hexdigest = Digest::SHA512.new.update(@authentication).update(@username).update(@password).hexdigest ping.push(@username, password_hexdigest) else ping.push('','') end ping end |
#generate_salt ⇒ Object
746 747 748 |
# File 'lib/fluent/plugin/out_forward.rb', line 746 def generate_salt SecureRandom.hex(16) end |
#heartbeat(detect = true) ⇒ Object
734 735 736 737 738 739 740 741 742 743 744 |
# File 'lib/fluent/plugin/out_forward.rb', line 734 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 |
#on_read(sock, data) ⇒ Object
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 |
# File 'lib/fluent/plugin/out_forward.rb', line 807 def on_read(sock, data) @log.trace __callee__ case @state when :helo unless check_helo(data) @log.warn "received invalid helo message from #{@name}" disable! # shutdown return end sock.write(generate_ping.to_msgpack) @state = :pingpong when :pingpong succeeded, reason = check_pong(data) unless succeeded @log.warn "connection refused to #{@name}: #{reason}" disable! # shutdown return end @state = :established @log.debug "connection established", host: @host, port: @port else raise "BUG: unknown session state: #{@state}" end end |
#resolved_host ⇒ Object
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 |
# File 'lib/fluent/plugin/out_forward.rb', line 675 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::Engine.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
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 |
# File 'lib/fluent/plugin/out_forward.rb', line 625 def send_data(tag, chunk) sock = @sender.create_transfer_socket(resolved_host, port, @hostname) begin send_data_actual(sock, tag, chunk) rescue sock.close rescue nil raise end if @sender.require_ack_response return sock # to read ACK from socket end sock.close_write rescue nil sock.close rescue nil heartbeat(false) nil end |
#send_data_actual(sock, tag, chunk) ⇒ Object
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 |
# File 'lib/fluent/plugin/out_forward.rb', line 594 def send_data_actual(sock, tag, chunk) @state = @sender.security ? :helo : :established if @state != :established establish_connection(sock) end unless available? raise ConnectionClosedError, "failed to establish connection with node #{@name}" end option = { 'size' => chunk.size, 'compressed' => @compress } option['chunk'] = Base64.encode64(chunk.unique_id) if @sender.require_ack_response # 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 ⇒ Object
FORWARD_TCP_HEARTBEAT_DATA = FORWARD_HEADER + ”.to_msgpack + [].to_msgpack
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 |
# File 'lib/fluent/plugin/out_forward.rb', line 645 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 end raise end case @sender.heartbeat_type when :transport @sender.create_transfer_socket(dest_addr, port, @hostname) do |sock| ## 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 heartbeat(true) end when :udp @usock.send "\0", 0, Socket.pack_sockaddr_in(@port, resolved_host) 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
560 561 562 |
# File 'lib/fluent/plugin/out_forward.rb', line 560 def standby? @standby end |
#tick ⇒ Object
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 |
# File 'lib/fluent/plugin/out_forward.rb', line 704 def tick now = Time.now.to_f if !@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 @available = false @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 @available = false @resolved_host = nil # expire cached host @failure.clear return true end end false end |
#validate_host_resolution! ⇒ Object
548 549 550 |
# File 'lib/fluent/plugin/out_forward.rb', line 548 def validate_host_resolution! resolved_host end |