Class: Datadog::Core::Buffer::CRuby

Inherits:
Random
  • Object
show all
Defined in:
lib/datadog/core/buffer/cruby.rb

Overview

Buffer that stores objects, has a maximum size, and can be safely used concurrently with CRuby.

Because singular Array operations are thread-safe in CRuby, we can implement the buffer without an explicit lock, while making the compromise of allowing the buffer to go over its maximum limit under extreme circumstances.

On the following scenario:

  • 4.5 million spans/second.

  • Pushed into a single CRubyTraceBuffer from 1000 threads.

This implementation allocates less memory and is faster than ThreadSafe.

Direct Known Subclasses

Tracing::CRubyTraceBuffer

Constant Summary collapse

FIXNUM_MAX =

A very large number to allow us to effectively drop all items when invoking ‘slice!(i, FIXNUM_MAX)`.

(1 << 62) - 1

Instance Method Summary collapse

Methods inherited from Random

#close, #closed?, #concat, #empty?, #initialize, #length, #pop, #push

Constructor Details

This class inherits a constructor from Datadog::Core::Buffer::Random

Instance Method Details

#replace!(item) ⇒ Object

Add a new “item“ in the local queue. This method doesn’t block the execution even if the buffer is full. In that case, a random item is discarded.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/datadog/core/buffer/cruby.rb', line 32

def replace!(item)
  # Ensure buffer stays within +max_size+ items.
  # This can happen when there's concurrent modification
  # between a call the check in `full?` and the `add!` call in
  # `full? ? replace!(item) : add!(item)`.
  #
  # We can still have `@items.size > @max_size` for a short period of
  # time, but we will always try to correct it here.
  #
  # `slice!` is performed before `delete_at` & `<<` to avoid always
  # removing the item that was just inserted.
  #
  # DEV: `slice!` with two integer arguments is ~10% faster than
  # `slice!` with a {Range} argument.
  @items.slice!(@max_size, FIXNUM_MAX)

  # We should replace a random item with the new one
  replace_index = rand(@max_size)
  @items[replace_index] = item
end