Class: Fluent::ForwardOutput

Inherits:
ObjectBufferedOutput
  • Object
show all
Defined in:
lib/fluent/plugin/out_forward.rb

Defined Under Namespace

Classes: FailureDetector, HeartbeatHandler, HeartbeatRequestTimer, Node, NoneHeartbeatNode

Constant Summary collapse

LISTEN_PORT =
24224
FORWARD_HEADER =

MessagePack FixArray length is 3

[0x93].pack('C').freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeForwardOutput

Returns a new instance of ForwardOutput.



44
45
46
47
48
49
50
51
# File 'lib/fluent/plugin/out_forward.rb', line 44

def initialize
  super
  require 'fluent/plugin/socket_util'
  @nodes = []  #=> [Node]
  @loop = nil
  @thread = nil
  @finished = false
end

Instance Attribute Details

#nodesObject (readonly)

Returns the value of attribute nodes.



112
113
114
# File 'lib/fluent/plugin/out_forward.rb', line 112

def nodes
  @nodes
end

#read_intervalObject (readonly)

Returns the value of attribute read_interval.



117
118
119
# File 'lib/fluent/plugin/out_forward.rb', line 117

def read_interval
  @read_interval
end

#recover_sample_sizeObject (readonly)

Returns the value of attribute recover_sample_size.



117
118
119
# File 'lib/fluent/plugin/out_forward.rb', line 117

def recover_sample_size
  @recover_sample_size
end

Instance Method Details

#configure(conf) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/fluent/plugin/out_forward.rb', line 119

def configure(conf)
  super

  @read_interval = @read_interval_msec / 1000.0
  @recover_sample_size = @recover_wait / @heartbeat_interval

  if @dns_round_robin
    if @heartbeat_type == :udp
      raise ConfigError, "forward output heartbeat type must be 'tcp' or 'none' to use dns_round_robin option"
    end
  end

  @servers.each do |server|
    failure = FailureDetector.new(@heartbeat_interval, @hard_timeout, Time.now.to_i.to_f)
    name = server.name || "#{server.host}:#{server.port}"

    log.info "adding forwarding server '#{name}'", host: server.host, port: server.port, weight: server.weight, plugin_id: plugin_id
    if @heartbeat_type == :none
      @nodes << NoneHeartbeatNode.new(self, server, failure: failure)
    else
      @nodes << Node.new(self, server, failure: failure)
    end
  end

  if @compress == :gzip && @buffer.compress == :text
    @buffer.compress = :gzip
  elsif @compress == :text && @buffer.compress == :gzip
    log.info "buffer is compressed.  If you also want to save the bandwidth of a network, Add `compress` configuration in <match>"
  end

  if @nodes.empty?
    raise ConfigError, "forward output plugin requires at least one <server> is required"
  end

  raise Fluent::ConfigError, "ack_response_timeout must be a positive integer" if @ack_response_timeout < 1
end

#forward_headerObject



231
232
233
# File 'lib/fluent/plugin/out_forward.rb', line 231

def forward_header
  FORWARD_HEADER
end

#runObject



194
195
196
197
198
199
# File 'lib/fluent/plugin/out_forward.rb', line 194

def run
  @loop.run if @loop
rescue
  log.error "unexpected error", error: $!.to_s
  log.error_backtrace
end

#shutdownObject



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/fluent/plugin/out_forward.rb', line 182

def shutdown
  @finished = true
  if @loop
    @loop.watchers.each {|w| w.detach }
    @loop.stop
  end
  @thread.join if @thread
  @usock.close if @usock

  super
end

#startObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/fluent/plugin/out_forward.rb', line 156

def start
  super

  @rand_seed = Random.new.seed
  rebuild_weight_array
  @rr = 0
  @usock = nil

  unless @heartbeat_type == :none
    @loop = Coolio::Loop.new

    if @heartbeat_type == :udp
      # assuming all hosts use udp
      @usock = SocketUtil.create_udp_socket(@nodes.first.host)
      @usock.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK)
      @hb = HeartbeatHandler.new(@usock, method(:on_heartbeat))
      @loop.attach(@hb)
    end

    @timer = HeartbeatRequestTimer.new(@heartbeat_interval, method(:on_timer))
    @loop.attach(@timer)

    @thread = Thread.new(&method(:run))
  end
end

#write_objects(tag, chunk) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/fluent/plugin/out_forward.rb', line 201

def write_objects(tag, chunk)
  return if chunk.empty?

  error = nil

  wlen = @weight_array.length
  wlen.times do
    @rr = (@rr + 1) % wlen
    node = @weight_array[@rr]

    if node.available?
      begin
        node.send_data(tag, chunk)
        return
      rescue
        # for load balancing during detecting crashed servers
        error = $!  # use the latest error
      end
    end
  end

  if error
    raise error
  else
    raise "no nodes are available"  # TODO message
  end
end