class Fluent::Plugin::ForwardOutput::Node
Attributes
failure[R]
sockaddr[R]
state[R]
usock[RW]
Public Class Methods
new(sender, server, failure:, connection_manager:, ack_handler:)
click to toggle source
@param connection_manager [Fluent::Plugin::ForwardOutput::ConnectionManager] @param ack_handler [Fluent::Plugin::ForwardOutput::AckHandler]
# 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
Public Instance Methods
available?()
click to toggle source
# File lib/fluent/plugin/out_forward.rb, line 594 def available? @available end
disable!()
click to toggle source
# File lib/fluent/plugin/out_forward.rb, line 598 def disable! @available = false end
establish_connection(sock, ri)
click to toggle source
# 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)
click to toggle source
# 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_host()
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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_heartbeat()
click to toggle source
FORWARD_TCP_HEARTBEAT_DATA = FORWARD_HEADER + ''.to_msgpack + [].to_msgpack
@return [Boolean] return true if it needs to rebuild nodes
# 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?()
click to toggle source
# File lib/fluent/plugin/out_forward.rb, line 602 def standby? @standby end
tick()
click to toggle source
# 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!()
click to toggle source
# File lib/fluent/plugin/out_forward.rb, line 590 def validate_host_resolution! resolved_host end
verify_connection()
click to toggle source
# File lib/fluent/plugin/out_forward.rb, line 606 def verify_connection connect do |sock, ri| ensure_established_connection(sock, ri) end end
Private Instance Methods
connect(host = nil, ack: false, &block)
click to toggle source
# File lib/fluent/plugin/out_forward.rb, line 806 def connect(host = nil, ack: false, &block) @connection_manager.connect(host: host || resolved_host, port: port, hostname: @hostname, ack: ack, &block) end
ensure_established_connection(sock, request_info)
click to toggle source
# File lib/fluent/plugin/out_forward.rb, line 796 def ensure_established_connection(sock, request_info) if request_info.state != :established establish_connection(sock, request_info) if request_info.state != :established raise ConnectionClosedError, "failed to establish connection with node #{@name}" end end end
resolve_dns!()
click to toggle source
# File lib/fluent/plugin/out_forward.rb, line 744 def resolve_dns! addrinfo_list = Socket.getaddrinfo(@host, @port, nil, Socket::SOCK_STREAM) addrinfo = @sender.dns_round_robin ? addrinfo_list.sample : addrinfo_list.first @sockaddr = Socket.pack_sockaddr_in(addrinfo[1], addrinfo[3]) # used by on_udp_heatbeat_response_recv addrinfo[3] end