Class: LogStash::JavaPipeline

Inherits:
JavaBasePipeline
  • Object
show all
Includes:
Util::Loggable
Defined in:
lib/logstash/java_pipeline.rb

Constant Summary collapse

MAX_INFLIGHT_WARN_THRESHOLD =
10_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pipeline_config, namespaced_metric = nil, agent = nil) ⇒ JavaPipeline

Returns a new instance of JavaPipeline.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/logstash/java_pipeline.rb', line 21

def initialize(pipeline_config, namespaced_metric = nil, agent = nil)
  @logger = self.logger
  super pipeline_config, namespaced_metric, @logger, agent
  open_queue

  @worker_threads = []

  @drain_queue =  settings.get_value("queue.drain") || settings.get("queue.type") == "memory"

  @events_filtered = java.util.concurrent.atomic.LongAdder.new
  @events_consumed = java.util.concurrent.atomic.LongAdder.new

  @input_threads = []
  # @ready requires thread safety since it is typically polled from outside the pipeline thread
  @ready = Concurrent::AtomicBoolean.new(false)
  @running = Concurrent::AtomicBoolean.new(false)
  @flushing = java.util.concurrent.atomic.AtomicBoolean.new(false)
  @flushRequested = java.util.concurrent.atomic.AtomicBoolean.new(false)
  @shutdownRequested = java.util.concurrent.atomic.AtomicBoolean.new(false)
  @outputs_registered = Concurrent::AtomicBoolean.new(false)

  # @finished_execution signals that the pipeline thread has finished its execution
  # regardless of any exceptions; it will always be true when the thread completes
  @finished_execution = Concurrent::AtomicBoolean.new(false)

  # @finished_run signals that the run methods called in the pipeline thread was completed
  # without errors and it will NOT be set if the run method exits from an exception; this
  # is by design and necessary for the wait_until_started semantic
  @finished_run = Concurrent::AtomicBoolean.new(false)

  @thread = nil
end

Instance Attribute Details

#events_consumedObject (readonly)

Returns the value of attribute events_consumed.



12
13
14
# File 'lib/logstash/java_pipeline.rb', line 12

def events_consumed
  @events_consumed
end

#events_filteredObject (readonly)

Returns the value of attribute events_filtered.



12
13
14
# File 'lib/logstash/java_pipeline.rb', line 12

def events_filtered
  @events_filtered
end

#started_atObject (readonly)

Returns the value of attribute started_at.



12
13
14
# File 'lib/logstash/java_pipeline.rb', line 12

def started_at
  @started_at
end

#threadObject (readonly)

Returns the value of attribute thread.



12
13
14
# File 'lib/logstash/java_pipeline.rb', line 12

def thread
  @thread
end

#worker_threadsObject (readonly)

Returns the value of attribute worker_threads.



12
13
14
# File 'lib/logstash/java_pipeline.rb', line 12

def worker_threads
  @worker_threads
end

Instance Method Details

#clear_pipeline_metricsObject



414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/logstash/java_pipeline.rb', line 414

def clear_pipeline_metrics
  # TODO(ph): I think the metric should also proxy that call correctly to the collector
  # this will simplify everything since the null metric would simply just do a noop
  collector = metric.collector

  unless collector.nil?
    # selectively reset metrics we don't wish to keep after reloading
    # these include metrics about the plugins and number of processed events
    # we want to keep other metrics like reload counts and error messages
    collector.clear("stats/pipelines/#{pipeline_id}/plugins")
    collector.clear("stats/pipelines/#{pipeline_id}/events")
  end
end

#filter(event, &block) ⇒ Object

for backward compatibility in devutils for the rspec helpers, this method is not used anymore and just here to not break TestPipeline that inherits this class.



373
374
# File 'lib/logstash/java_pipeline.rb', line 373

def filter(event, &block)
end

#filters?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/logstash/java_pipeline.rb', line 86

def filters?
  filters.any?
end

#finished_execution?Boolean

def initialize

Returns:

  • (Boolean)


54
55
56
# File 'lib/logstash/java_pipeline.rb', line 54

def finished_execution?
  @finished_execution.true?
end

#flush_filters(options = {}, &block) ⇒ Object

for backward compatibility in devutils for the rspec helpers, this method is not used anymore and just here to not break TestPipeline that inherits this class.



378
379
# File 'lib/logstash/java_pipeline.rb', line 378

def flush_filters(options = {}, &block)
end

#inputworker(plugin) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/logstash/java_pipeline.rb', line 294

def inputworker(plugin)
  Util::set_thread_name("[#{pipeline_id}]<#{plugin.class.config_name}")
  begin
    plugin.run(wrapped_write_client(plugin.id.to_sym))
  rescue => e
    if plugin.stop?
      @logger.debug("Input plugin raised exception during shutdown, ignoring it.",
                    default_logging_keys(:plugin => plugin.class.config_name, :exception => e.message, :backtrace => e.backtrace))
      return
    end

    # otherwise, report error and restart
    @logger.error(I18n.t("logstash.pipeline.worker-error-debug",
                          default_logging_keys(
                            :plugin => plugin.inspect,
                            :error => e.message,
                            :exception => e.class,
                            :stacktrace => e.backtrace.join("\n"))))

    # Assuming the failure that caused this exception is transient,
    # let's sleep for a bit and execute #run again
    sleep(1)
    retry
  ensure
    plugin.do_close
  end
end

#inspectObject

Sometimes we log stuff that will dump the pipeline which may contain sensitive information (like the raw syntax tree which can contain passwords) We want to hide most of what’s in here



431
432
433
434
435
436
437
438
439
# File 'lib/logstash/java_pipeline.rb', line 431

def inspect
  {
    :pipeline_id => pipeline_id,
    :settings => settings.inspect,
    :ready => @ready,
    :running => @running,
    :flushing => @flushing
  }
end

#plugin_threads_infoObject



400
401
402
403
404
# File 'lib/logstash/java_pipeline.rb', line 400

def plugin_threads_info
  input_threads = @input_threads.select {|t| t.class == Thread && t.alive? }
  worker_threads = @worker_threads.select {|t| t.alive? }
  (input_threads + worker_threads).map {|t| Util.thread_info(t) }
end

#ready?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/logstash/java_pipeline.rb', line 58

def ready?
  @ready.value
end

#register_plugins(plugins) ⇒ Object

register_plugins calls #register_plugin on the plugins list and upon exception will call Plugin#do_close on all registered plugins

Parameters:

  • plugins (Array[Plugin])

    the list of plugins to register



188
189
190
191
192
193
194
195
196
197
# File 'lib/logstash/java_pipeline.rb', line 188

def register_plugins(plugins)
  registered = []
  plugins.each do |plugin|
    plugin.register
    registered << plugin
  end
rescue => e
  registered.each(&:do_close)
  raise e
end

#runObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/logstash/java_pipeline.rb', line 140

def run
  @started_at = Time.now
  @thread = Thread.current
  Util.set_thread_name("[#{pipeline_id}]-pipeline-manager")

  start_workers

  @logger.info("Pipeline started", "pipeline.id" => pipeline_id)

  # Block until all inputs have stopped
  # Generally this happens if SIGINT is sent and `shutdown` is called from an external thread

  transition_to_running
  start_flusher # Launches a non-blocking thread for flush events
  wait_inputs
  transition_to_stopped

  @logger.debug("Input plugins stopped! Will shutdown filter/output workers.", default_logging_keys)

  shutdown_flusher
  shutdown_workers

  close

  @logger.debug("Pipeline has been shutdown", default_logging_keys)

  # exit code
  return 0
end

#running?Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/logstash/java_pipeline.rb', line 178

def running?
  @running.true?
end

#safe_pipeline_worker_countObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/logstash/java_pipeline.rb', line 62

def safe_pipeline_worker_count
  default = settings.get_default("pipeline.workers")
  pipeline_workers = settings.get("pipeline.workers") #override from args "-w 8" or config
  safe_filters, unsafe_filters = filters.partition(&:threadsafe?)
  plugins = unsafe_filters.collect { |f| f.config_name }

  return pipeline_workers if unsafe_filters.empty?

  if settings.set?("pipeline.workers")
    if pipeline_workers > 1
      @logger.warn("Warning: Manual override - there are filters that might not work with multiple worker threads", default_logging_keys(:worker_threads => pipeline_workers, :filters => plugins))
    end
  else
    # user did not specify a worker thread count
    # warn if the default is multiple
    if default > 1
      @logger.warn("Defaulting pipeline worker threads to 1 because there are some filters that might not work with multiple worker threads",
                   default_logging_keys(:count_was => default, :filters => plugins))
      return 1 # can't allow the default value to propagate if there are unsafe filters
    end
  end
  pipeline_workers
end

#shutdown(&before_stop) ⇒ Object

initiate the pipeline shutdown sequence this method is intended to be called from outside the pipeline thread

Parameters:

  • before_stop (Proc)

    code block called before performing stop operation on input plugins



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/logstash/java_pipeline.rb', line 325

def shutdown(&before_stop)
  # shutdown can only start once the pipeline has completed its startup.
  # avoid potential race condition between the startup sequence and this
  # shutdown method which can be called from another thread at any time
  sleep(0.1) while !ready?

  # TODO: should we also check against calling shutdown multiple times concurrently?

  before_stop.call if block_given?

  stop_inputs

  # We make this call blocking, so we know for sure when the method return the shutdown is
  # stopped
  wait_for_workers
  clear_pipeline_metrics
  @logger.info("Pipeline terminated", "pipeline.id" => pipeline_id)
end

#shutdown_flusherObject



388
389
390
# File 'lib/logstash/java_pipeline.rb', line 388

def shutdown_flusher
  @flusher_thread.close
end

#shutdown_workersObject

After ‘shutdown` is called from an external thread this is called from the main thread to tell the worker threads to stop and then block until they’ve fully stopped This also stops all filter and output plugins



359
360
361
362
363
364
365
366
367
368
369
# File 'lib/logstash/java_pipeline.rb', line 359

def shutdown_workers
  @shutdownRequested.set(true)

  @worker_threads.each do |t|
    @logger.debug("Shutdown waiting for worker thread" , default_logging_keys(:thread => t.inspect))
    t.join
  end

  filters.each(&:do_close)
  outputs.each(&:do_close)
end

#stalling_threads_infoObject



406
407
408
409
410
411
412
# File 'lib/logstash/java_pipeline.rb', line 406

def stalling_threads_info
  plugin_threads_info
    .reject {|t| t["blocked_on"] } # known benign blocking statuses
    .each {|t| t.delete("backtrace") }
    .each {|t| t.delete("blocked_on") }
    .each {|t| t.delete("status") }
end

#startObject



90
91
92
93
94
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
# File 'lib/logstash/java_pipeline.rb', line 90

def start
  # Since we start lets assume that the metric namespace is cleared
  # this is useful in the context of pipeline reloading
  collect_stats
  collect_dlq_stats

  @logger.debug("Starting pipeline", default_logging_keys)

  @finished_execution.make_false
  @finished_run.make_false

  @thread = Thread.new do
    begin
      LogStash::Util.set_thread_name("pipeline.#{pipeline_id}")
      run
      @finished_run.make_true
    rescue => e
      close
      logger.error("Pipeline aborted due to error", default_logging_keys(:exception => e, :backtrace => e.backtrace))
    ensure
      @finished_execution.make_true
    end
  end

  status = wait_until_started

  if status
    logger.debug("Pipeline started successfully", default_logging_keys(:pipeline_id => pipeline_id))
  end

  status
end

#start_flusherObject



381
382
383
384
385
386
# File 'lib/logstash/java_pipeline.rb', line 381

def start_flusher
  # Invariant to help detect improper initialization
  raise "Attempted to start flusher on a stopped pipeline!" if stopped?
  @flusher_thread = org.logstash.execution.PeriodicFlush.new(@flushRequested, @flushing)
  @flusher_thread.start
end

#start_input(plugin) ⇒ Object



286
287
288
289
290
291
292
# File 'lib/logstash/java_pipeline.rb', line 286

def start_input(plugin)
  if plugin.class == LogStash::JavaInputDelegator
    @input_threads << plugin.start
  else
    @input_threads << Thread.new { inputworker(plugin) }
  end
end

#start_inputsObject



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/logstash/java_pipeline.rb', line 268

def start_inputs
  moreinputs = []
  inputs.each do |input|
    if input.threadable && input.threads > 1
      (input.threads - 1).times do |i|
        moreinputs << input.clone
      end
    end
  end
  moreinputs.each {|i| inputs << i}

  # first make sure we can register all input plugins
  register_plugins(inputs)

  # then after all input plugins are successfully registered, start them
  inputs.each { |input| start_input(input) }
end

#start_workersObject



199
200
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/logstash/java_pipeline.rb', line 199

def start_workers
  @worker_threads.clear # In case we're restarting the pipeline
  @outputs_registered.make_false
  begin
    maybe_setup_out_plugins

    pipeline_workers = safe_pipeline_worker_count
    batch_size = settings.get("pipeline.batch.size")
    batch_delay = settings.get("pipeline.batch.delay")

    max_inflight = batch_size * pipeline_workers

    config_metric = metric.namespace([:stats, :pipelines, pipeline_id.to_s.to_sym, :config])
    config_metric.gauge(:workers, pipeline_workers)
    config_metric.gauge(:batch_size, batch_size)
    config_metric.gauge(:batch_delay, batch_delay)
    config_metric.gauge(:config_reload_automatic, settings.get("config.reload.automatic"))
    config_metric.gauge(:config_reload_interval, settings.get("config.reload.interval"))
    config_metric.gauge(:dead_letter_queue_enabled, dlq_enabled?)
    config_metric.gauge(:dead_letter_queue_path, dlq_writer.get_path.to_absolute_path.to_s) if dlq_enabled?


    @logger.info("Starting pipeline", default_logging_keys(
      "pipeline.workers" => pipeline_workers,
      "pipeline.batch.size" => batch_size,
      "pipeline.batch.delay" => batch_delay,
      "pipeline.max_inflight" => max_inflight))
    if max_inflight > MAX_INFLIGHT_WARN_THRESHOLD
      @logger.warn("CAUTION: Recommended inflight events max exceeded! Logstash will run with up to #{max_inflight} events in memory in your current configuration. If your message sizes are large this may cause instability with the default heap size. Please consider setting a non-standard heap size, changing the batch size (currently #{batch_size}), or changing the number of pipeline workers (currently #{pipeline_workers})", default_logging_keys)
    end

    filter_queue_client.set_batch_dimensions(batch_size, batch_delay)

    pipeline_workers.times do |t|
      thread = Thread.new do
        Util.set_thread_name("[#{pipeline_id}]>worker#{t}")
        org.logstash.execution.WorkerLoop.new(
            lir_execution, filter_queue_client, @events_filtered, @events_consumed,
            @flushRequested, @flushing, @shutdownRequested, @drain_queue).run
      end
      @worker_threads << thread
    end

    # inputs should be started last, after all workers
    begin
      start_inputs
    rescue => e
      # if there is any exception in starting inputs, make sure we shutdown workers.
      # exception will already by logged in start_inputs
      shutdown_workers
      raise e
    end
  ensure
    # it is important to guarantee @ready to be true after the startup sequence has been completed
    # to potentially unblock the shutdown method which may be waiting on @ready to proceed
    @ready.make_true
  end
end

#stop_inputsObject



350
351
352
353
354
# File 'lib/logstash/java_pipeline.rb', line 350

def stop_inputs
  @logger.debug("Closing inputs", default_logging_keys)
  inputs.each(&:do_stop)
  @logger.debug("Closed inputs", default_logging_keys)
end

#stopped?Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/logstash/java_pipeline.rb', line 182

def stopped?
  @running.false?
end

#transition_to_runningObject

def run



170
171
172
# File 'lib/logstash/java_pipeline.rb', line 170

def transition_to_running
  @running.make_true
end

#transition_to_stoppedObject



174
175
176
# File 'lib/logstash/java_pipeline.rb', line 174

def transition_to_stopped
  @running.make_false
end

#uptimeFixnum

Calculate the uptime in milliseconds

Returns:

  • (Fixnum)

    Uptime in milliseconds, 0 if the pipeline is not started



395
396
397
398
# File 'lib/logstash/java_pipeline.rb', line 395

def uptime
  return 0 if started_at.nil?
  ((Time.now.to_f - started_at.to_f) * 1000.0).to_i
end

#wait_for_workersObject

def shutdown



344
345
346
347
348
# File 'lib/logstash/java_pipeline.rb', line 344

def wait_for_workers
  @logger.debug("Closing inputs", default_logging_keys)
  @worker_threads.map(&:join)
  @logger.debug("Worker closed", default_logging_keys)
end

#wait_inputsObject



258
259
260
261
262
263
264
265
266
# File 'lib/logstash/java_pipeline.rb', line 258

def wait_inputs
  @input_threads.each do |thread|
    if thread.class == Java::JavaObject
      thread.to_java.join
    else
      thread.join
    end
  end
end

#wait_until_startedObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/logstash/java_pipeline.rb', line 123

def wait_until_started
  while true do
    if @finished_run.true?
      # it completed run without exception
      return true
    elsif thread.nil? || !thread.alive?
      # some exception occurred and the thread is dead
      return false
    elsif running?
      # fully initialized and running
      return true
    else
      sleep 0.01
    end
  end
end