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



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/logstash/util/wrapped_acked_queue.rb', line 112

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



203
204
205
206
# File 'lib/logstash/util/wrapped_acked_queue.rb', line 203

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



208
209
210
211
# File 'lib/logstash/util/wrapped_acked_queue.rb', line 208

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



197
198
199
200
201
# File 'lib/logstash/util/wrapped_acked_queue.rb', line 197

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



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

def close
  @queue.close
end

#close_batch(batch) ⇒ Object



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

def close_batch(batch)
  @mutex.synchronize do
    batch.close
    @inflight_batches.delete(Thread.current)
    stop_clock
  end
end

#current_inflight_batchObject



156
157
158
# File 'lib/logstash/util/wrapped_acked_queue.rb', line 156

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

#define_initial_metrics_values(namespaced_metric) ⇒ Object



143
144
145
146
147
148
# File 'lib/logstash/util/wrapped_acked_queue.rb', line 143

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

#inflight_batchesObject



150
151
152
153
154
# File 'lib/logstash/util/wrapped_acked_queue.rb', line 150

def inflight_batches
  @mutex.synchronize do
    yield(@inflight_batches)
  end
end

#set_batch_dimensions(batch_size, wait_for) ⇒ Object



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

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

#set_current_thread_inflight_batch(batch) ⇒ Object



173
174
175
# File 'lib/logstash/util/wrapped_acked_queue.rb', line 173

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

#set_events_metric(metric) ⇒ Object



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

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

#set_pipeline_metric(metric) ⇒ Object



138
139
140
141
# File 'lib/logstash/util/wrapped_acked_queue.rb', line 138

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

#start_clockObject



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

def start_clock
  @inflight_clocks[Thread.current] = [
  @event_metric.time(:duration_in_millis),
  @pipeline_metric.time(:duration_in_millis)
  ]
end

#stop_clockObject



192
193
194
195
# File 'lib/logstash/util/wrapped_acked_queue.rb', line 192

def stop_clock
  @inflight_clocks[Thread.current].each(&:stop)
  @inflight_clocks.delete(Thread.current)
end

#take_batchObject



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

def take_batch
  if @queue.closed?
    raise QueueClosedError.new("Attempt to take a batch from a closed AckedQueue")
  end
  @mutex.synchronize do
    batch = ReadBatch.new(@queue, @batch_size, @wait_for)
    add_starting_metrics(batch)
    set_current_thread_inflight_batch(batch)
    start_clock
    batch
  end
end