Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce memory bloating in FileMappedDict when reading metrics. #160

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions lib/prometheus/client/data_stores/direct_file_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,7 @@ def initialize(filename, readonly = false)

if @used > 0
# File already has data. Read the existing values
with_file_lock do
read_all_values.each do |key, _, pos|
@positions[key] = pos
end
end
with_file_lock { populate_positions }
else
# File is empty. Init the `used` counter, if we're in write mode
if !readonly
Expand All @@ -230,10 +226,14 @@ def initialize(filename, readonly = false)
end
end

# Yield (key, value, pos). No locking is performed.
# Return a list of key-value pairs
def all_values
with_file_lock do
read_all_values.map { |k, v, p| [k, v] }
@positions.map do |key, pos|
@f.seek(pos)
value = @f.read(8).unpack('d')[0]
[key, value]
end
end
end

Expand Down Expand Up @@ -309,22 +309,18 @@ def init_value(key)
@positions[key] = @used - 8
end

# Yield (key, value, pos). No locking is performed.
def read_all_values
# Read position of all keys. No locking is performed.
def populate_positions
@f.seek(8)
values = []
while @f.pos < @used
padded_len = @f.read(4).unpack('l')[0]
encoded = @f.read(padded_len).unpack("A#{padded_len}")[0]
value = @f.read(8).unpack('d')[0]
values << [encoded.strip, value, @f.pos - 8]
key = @f.read(padded_len).unpack("A#{padded_len}")[0].strip
@positions[key] = @f.pos
@f.seek(8, :CUR)
end
values
end
end
end
end
end
end