Class: Fluent::ForwardOutput

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

Defined Under Namespace

Classes: FailureDetector, HeartbeatHandler, HeartbeatRequestTimer, Node, NodeConfig, NoneHeartbeatNode

Constant Summary collapse

LISTEN_PORT =
24224

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.



90
91
92
# File 'lib/fluent/plugin/out_forward.rb', line 90

def nodes
  @nodes
end

Instance Method Details

#configure(conf) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/fluent/plugin/out_forward.rb', line 95

def configure(conf)
  super

  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

  conf.elements.each {|e|
    next if e.name != "server"

    host = e['host']
    port = e['port']
    port = port ? port.to_i : LISTEN_PORT

    weight = e['weight']
    weight = weight ? weight.to_i : 60

    standby = !!e['standby']

    name = e['name']
    unless name
      name = "#{host}:#{port}"
    end

    failure = FailureDetector.new(@heartbeat_interval, @hard_timeout, Time.now.to_i.to_f)

    node_conf = NodeConfig.new(name, host, port, weight, standby, failure,
      @phi_threshold, recover_sample_size, @expire_dns_cache, @phi_failure_detector, @dns_round_robin)

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

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

#runObject



178
179
180
181
182
183
# File 'lib/fluent/plugin/out_forward.rb', line 178

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

#shutdownObject



166
167
168
169
170
171
172
173
174
175
176
# File 'lib/fluent/plugin/out_forward.rb', line 166

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



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/fluent/plugin/out_forward.rb', line 141

def start
  super

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

  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



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/fluent/plugin/out_forward.rb', line 185

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
        send_data(node, 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