class Mmtrix::Agent::Transaction::TransactionSampleBuffer
Constants
- NO_SAMPLES
- SINGLE_BUFFER_MAX
Attributes
Public Class Methods
# File lib/mmtrix/agent/transaction/transaction_sample_buffer.rb, line 14 def initialize @samples = [] end
Public Instance Methods
# File lib/mmtrix/agent/transaction/transaction_sample_buffer.rb, line 32 def allow_sample?(sample) true end
Capacity is the desired number of samples a buffer will hold. This can be user dictated via config if a feature wants.
This value will be forcibly capped by the max_capacity
# File lib/mmtrix/agent/transaction/transaction_sample_buffer.rb, line 64 def capacity raise NotImplementedError.new("TransactionSampleBuffer subclasses must provide a capacity override") end
# File lib/mmtrix/agent/transaction/transaction_sample_buffer.rb, line 18 def enabled? true end
# File lib/mmtrix/agent/transaction/transaction_sample_buffer.rb, line 56 def full? @samples.length >= max_capacity end
# File lib/mmtrix/agent/transaction/transaction_sample_buffer.rb, line 26 def harvest_samples @samples ensure reset! end
Apply hard upper limit to the capacity to prevent users from consuming too much memory buffering TT’s.
A typical buffer should NOT override this method (although we do for odd things like dev-mode)
# File lib/mmtrix/agent/transaction/transaction_sample_buffer.rb, line 73 def max_capacity capacity > SINGLE_BUFFER_MAX ? SINGLE_BUFFER_MAX : capacity end
# File lib/mmtrix/agent/transaction/transaction_sample_buffer.rb, line 22 def reset! @samples = [] end
# File lib/mmtrix/agent/transaction/transaction_sample_buffer.rb, line 36 def store(sample) return unless enabled? if allow_sample?(sample) add_sample(sample) truncate_samples_if_needed end end
# File lib/mmtrix/agent/transaction/transaction_sample_buffer.rb, line 44 def store_previous(previous_samples) return unless enabled? previous_samples.each do |sample| add_sample(sample) if allow_sample?(sample) end truncate_samples_if_needed end
Our default truncation strategy is to keep max_capacity
worth of the longest samples. Override this method for alternate behavior.
This doesn’t use the more convenient last and sort_by to avoid additional array allocations (and abundant alliteration)
# File lib/mmtrix/agent/transaction/transaction_sample_buffer.rb, line 83 def truncate_samples @samples.sort!{|a,b| a.duration <=> b.duration} @samples.slice!(0..-(max_capacity + 1)) end
# File lib/mmtrix/agent/transaction/transaction_sample_buffer.rb, line 52 def truncate_samples_if_needed truncate_samples if full? end
When pushing a scope different sample buffers potentially want to know about what’s happening to annotate the incoming nodes
# File lib/mmtrix/agent/transaction/transaction_sample_buffer.rb, line 90 def visit_node(*) # no-op end
Private Instance Methods
If a buffer needs to modify an added sample, override this method. Bounds checking, allowing samples and truncation belongs elsewhere.
# File lib/mmtrix/agent/transaction/transaction_sample_buffer.rb, line 98 def add_sample(sample) @samples << sample end