Class: Prometheus::Client::Helper::MmapedFile

Inherits:
FastMmapedFileRs
  • Object
show all
Includes:
EntryParser
Defined in:
lib/prometheus/client/helper/mmaped_file.rb

Constant Summary

Constants included from EntryParser

EntryParser::ENCODED_LENGTH_BYTES, EntryParser::MINIMUM_SIZE, EntryParser::START_POSITION, EntryParser::VALUE_BYTES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EntryParser

#empty?, #entries, #multiprocess_mode, #parsed_entries, #parts, #pid, #to_metrics, #type, #used

Constructor Details

#initialize(filepath) ⇒ MmapedFile

Returns a new instance of MmapedFile.



20
21
22
23
24
25
26
27
28
29
# File 'lib/prometheus/client/helper/mmaped_file.rb', line 20

def initialize(filepath)
  @filepath = filepath

  File.open(filepath, 'a+b') do |file|
    file.truncate(initial_mmap_file_size) if file.size < MINIMUM_SIZE
    @size = file.size
  end

  super(filepath)
end

Instance Attribute Details

#filepathObject (readonly)

Returns the value of attribute filepath.



18
19
20
# File 'lib/prometheus/client/helper/mmaped_file.rb', line 18

def filepath
  @filepath
end

#sizeObject (readonly)

Returns the value of attribute size.



18
19
20
# File 'lib/prometheus/client/helper/mmaped_file.rb', line 18

def size
  @size
end

Class Method Details

.ensure_exclusive_file(file_prefix = 'mmaped_file') ⇒ Object



49
50
51
52
53
54
# File 'lib/prometheus/client/helper/mmaped_file.rb', line 49

def ensure_exclusive_file(file_prefix = 'mmaped_file')
  (0..Float::INFINITY).lazy
    .map { |f_num| "#{file_prefix}_#{Prometheus::Client.pid}-#{f_num}.db" }
    .map { |filename| File.join(Prometheus::Client.configuration.multiprocess_files_dir, filename) }
    .find { |path| Helper::FileLocker.lock_to_process(path) }
end

.open(filepath) ⇒ Object



45
46
47
# File 'lib/prometheus/client/helper/mmaped_file.rb', line 45

def open(filepath)
  MmapedFile.new(filepath)
end

.open_exclusive_file(file_prefix = 'mmaped_file') ⇒ Object



56
57
58
59
# File 'lib/prometheus/client/helper/mmaped_file.rb', line 56

def open_exclusive_file(file_prefix = 'mmaped_file')
  filename = Helper::MmapedFile.ensure_exclusive_file(file_prefix)
  open(filename)
end

Instance Method Details

#closeObject



31
32
33
34
# File 'lib/prometheus/client/helper/mmaped_file.rb', line 31

def close
  munmap
  FileLocker.unlock(filepath)
end