Class: Fluent::EventLimitedFileBuffer

Inherits:
FileBuffer
  • Object
show all
Defined in:
lib/fluent/plugin/buf_event_limited.rb

Instance Method Summary collapse

Instance Method Details

#emit(key, data, chain) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/fluent/plugin/buf_event_limited.rb', line 117

def emit(key, data, chain)
  data = MessagePackFormattedBufferData.new(data)
  key = key.to_s
  flush_trigger = false

  synchronize do
    # Get the current open chunk
    chunk = (@map[key] ||= new_chunk(key))

    data.each_slice(chunk_sizes(chunk.remaining_capacity)) do |data, size|
      chain.next
      chunk.write(data, size)
      chunk, queue_size = rotate_chunk!(chunk, key)
      flush_trigger ||= (queue_size == 0)
    end
  end

  return flush_trigger
end

#new_chunk(key) ⇒ Object



137
138
139
140
141
142
143
# File 'lib/fluent/plugin/buf_event_limited.rb', line 137

def new_chunk(key)
  encoded_key = encode_key(key)
  path, tsuffix = make_path(encoded_key, 'b')
  unique_id = tsuffix_to_unique_id(tsuffix)

  chunk_factory(key, path, unique_id, 'a+')
end

#resumeObject



83
84
85
86
87
88
89
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
# File 'lib/fluent/plugin/buf_event_limited.rb', line 83

def resume
  maps = []
  queues = []

  Dir.glob("#{@buffer_path_prefix}*#{@buffer_path_suffix}") do |path|
    identifier_part = chunk_identifier_in_path(path)
    next unless (m = PATH_MATCH.match(identifier_part))

    key = decode_key(m[1])
    bq = m[2]
    tsuffix = m[3]
    timestamp = m[3].to_i(16)
    unique_id = tsuffix_to_unique_id(tsuffix)

    case bq
    when 'b'
      maps << [timestamp, chunk_factory(key, path, unique_id, 'a+')]
    when 'q'
      queues << [timestamp, chunk_factory(key, path, unique_id, 'r')]
    end
  end

  map = {}
  maps
    .sort_by { |(timestamp, chunk)| timestamp }
    .each    { |(timestamp, chunk)| map[chunk.key] = chunk }

  queue = queues
    .sort_by { |(timestamp, _chunk)| timestamp }
    .map     { |(_timestamp, chunk)| chunk }

  return queue, map
end