Class: LogStash::Util::WrappedAckedQueue::ReadClient

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/util/wrapped_acked_queue.rb

Instance Method Summary collapse

Constructor Details

#initialize(queue, batch_size = 125, wait_for = 250) ⇒ ReadClient

We generally only want one thread at a time able to access pop/take/poll operations from this queue. We also depend on this to be able to block consumers while we snapshot in-flight buffers



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/logstash/util/wrapped_acked_queue.rb', line 93

def initialize(queue, batch_size = 125, wait_for = 250)
  @queue = queue
  @mutex = Mutex.new
  # Note that @inflight_batches as a central mechanism for tracking inflight
  # batches will fail if we have multiple read clients in the pipeline.
  @inflight_batches = {}
  # allow the worker thread to report the execution time of the filter + output
  @inflight_clocks = {}
  @batch_size = batch_size
  @wait_for = wait_for
end

Instance Method Details

#add_filtered_metrics(batch) ⇒ Object



225
226
227
228
# File 'lib/logstash/util/wrapped_acked_queue.rb', line 225

def add_filtered_metrics(batch)
  @event_metric.increment(:filtered, batch.filtered_size)
  @pipeline_metric.increment(:filtered, batch.filtered_size)
end

#add_output_metrics(batch) ⇒ Object



230
231
232
233
# File 'lib/logstash/util/wrapped_acked_queue.rb', line 230

def add_output_metrics(batch)
  @event_metric.increment(:out, batch.filtered_size)
  @pipeline_metric.increment(:out, batch.filtered_size)
end

#add_starting_metrics(batch) ⇒ Object



219
220
221
222
223
# File 'lib/logstash/util/wrapped_acked_queue.rb', line 219

def add_starting_metrics(batch)
  return if @event_metric.nil? || @pipeline_metric.nil?
  @event_metric.increment(:in, batch.starting_size)
  @pipeline_metric.increment(:in, batch.starting_size)
end

#closeObject



105
106
107
# File 'lib/logstash/util/wrapped_acked_queue.rb', line 105

def close
  @queue.close
end

#close_batch(batch) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
# File 'lib/logstash/util/wrapped_acked_queue.rb', line 189

def close_batch(batch)
  @mutex.lock
  begin
    batch.close
    # there seems to be concurrency issues with metrics, keep it in the mutex
    @inflight_batches.delete(Thread.current)
    stop_clock(batch)
  ensure
    @mutex.unlock
  end
end

#current_inflight_batchObject



148
149
150
# File 'lib/logstash/util/wrapped_acked_queue.rb', line 148

def current_inflight_batch
  @inflight_batches.fetch(Thread.current, [])
end

#define_initial_metrics_values(namespaced_metric) ⇒ Object



133
134
135
136
137
# File 'lib/logstash/util/wrapped_acked_queue.rb', line 133

def define_initial_metrics_values(namespaced_metric)
  namespaced_metric.report_time(:duration_in_millis, 0)
  namespaced_metric.increment(:filtered, 0)
  namespaced_metric.increment(:out, 0)
end

#empty?Boolean

Returns:

  • (Boolean)


109
110
111
112
113
114
115
116
# File 'lib/logstash/util/wrapped_acked_queue.rb', line 109

def empty?
  @mutex.lock
  begin
    @queue.is_empty?
  ensure
    @mutex.unlock
  end
end

#inflight_batchesObject



139
140
141
142
143
144
145
146
# File 'lib/logstash/util/wrapped_acked_queue.rb', line 139

def inflight_batches
  @mutex.lock
  begin
    yield(@inflight_batches)
  ensure
    @mutex.unlock
  end
end

#new_batchReadBatch

create a new empty batch

Returns:



154
155
156
# File 'lib/logstash/util/wrapped_acked_queue.rb', line 154

def new_batch
  ReadBatch.new(@queue, @batch_size, @wait_for)
end

#read_batchObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/logstash/util/wrapped_acked_queue.rb', line 158

def read_batch
  if @queue.closed?
    raise QueueClosedError.new("Attempt to take a batch from a closed AckedQueue")
  end

  batch = new_batch
  @mutex.lock
  begin
    batch.read_next
  ensure
    @mutex.unlock
  end
  start_metrics(batch)
  batch
end

#set_batch_dimensions(batch_size, wait_for) ⇒ Object



118
119
120
121
# File 'lib/logstash/util/wrapped_acked_queue.rb', line 118

def set_batch_dimensions(batch_size, wait_for)
  @batch_size = batch_size
  @wait_for = wait_for
end

#set_current_thread_inflight_batch(batch) ⇒ Object



185
186
187
# File 'lib/logstash/util/wrapped_acked_queue.rb', line 185

def set_current_thread_inflight_batch(batch)
  @inflight_batches[Thread.current] = batch
end

#set_events_metric(metric) ⇒ Object



123
124
125
126
# File 'lib/logstash/util/wrapped_acked_queue.rb', line 123

def set_events_metric(metric)
  @event_metric = metric
  define_initial_metrics_values(@event_metric)
end

#set_pipeline_metric(metric) ⇒ Object



128
129
130
131
# File 'lib/logstash/util/wrapped_acked_queue.rb', line 128

def set_pipeline_metric(metric)
  @pipeline_metric = metric
  define_initial_metrics_values(@pipeline_metric)
end

#start_clockObject



201
202
203
# File 'lib/logstash/util/wrapped_acked_queue.rb', line 201

def start_clock
  @inflight_clocks[Thread.current] = java.lang.System.current_time_millis
end

#start_metrics(batch) ⇒ Object



174
175
176
177
178
179
180
181
182
183
# File 'lib/logstash/util/wrapped_acked_queue.rb', line 174

def start_metrics(batch)
  @mutex.lock
  begin
    # there seems to be concurrency issues with metrics, keep it in the mutex
    set_current_thread_inflight_batch(batch)
    start_clock
  ensure
    @mutex.unlock
  end
end

#stop_clock(batch) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/logstash/util/wrapped_acked_queue.rb', line 205

def stop_clock(batch)
  unless @inflight_clocks[Thread.current].nil?
    if batch.size > 0
      # only stop (which also records) the metrics if the batch is non-empty.
      # start_clock is now called at empty batch creation and an empty batch could
      # stay empty all the way down to the close_batch call.
      time_taken = java.lang.System.current_time_millis - @inflight_clocks[Thread.current]
      @event_metric.report_time(:duration_in_millis, time_taken)
      @pipeline_metric.report_time(:duration_in_millis, time_taken)
    end
    @inflight_clocks.delete(Thread.current)
  end
end