Class: Fluent::Plugin::FileBuffer

Inherits:
Buffer show all
Includes:
SystemConfig::Mixin
Defined in:
lib/fluent/plugin/buf_file.rb

Constant Summary collapse

DEFAULT_CHUNK_LIMIT_SIZE =

256MB

256 * 1024 * 1024
DEFAULT_TOTAL_LIMIT_SIZE =

64GB, same with v0.12 (TimeSlicedOutput + buf_file)

64 * 1024 * 1024 * 1024
DIR_PERMISSION =
0755
@@buffer_paths =
{}

Constants inherited from Buffer

Buffer::DEFAULT_CHUNK_FULL_THRESHOLD, Buffer::MINIMUM_APPEND_ATTEMPT_RECORDS

Constants included from Configurable

Configurable::CONFIG_TYPE_REGISTRY

Instance Attribute Summary

Attributes inherited from Buffer

#dequeued, #queue, #queue_size, #queued_num, #stage, #stage_size

Attributes inherited from Base

#under_plugin_development

Instance Method Summary collapse

Methods included from SystemConfig::Mixin

#system_config, #system_config_override

Methods inherited from Buffer

#add_metadata, #chunk_size_full?, #chunk_size_over?, #clear_queue!, #close, #dequeue_chunk, #enqueue_all, #enqueue_chunk, #enqueue_unstaged_chunk, #metadata, #metadata_list, #metadata_list_clear!, #new_metadata, #purge_chunk, #queued?, #queued_records, #storable?, #takeback_chunk, #terminate, #write, #write_once, #write_step_by_step

Methods included from UniqueId::Mixin

#dump_unique_id_hex, #generate_unique_id

Methods included from OwnedByMixin

#log, #owner, #owner=

Methods inherited from Base

#after_shutdown, #after_shutdown?, #after_start, #after_started?, #before_shutdown, #before_shutdown?, #close, #closed?, #configured?, #context_router, #context_router=, #fluentd_worker_id, #has_router?, #inspect, #plugin_root_dir, #shutdown, #shutdown?, #started?, #stop, #stopped?, #string_safe_encoding, #terminate, #terminated?

Methods included from Configurable

#config, #configure_proxy_generate, #configured_section_create, included, lookup_type, register_type

Constructor Details

#initializeFileBuffer

Returns a new instance of FileBuffer.



46
47
48
49
50
51
# File 'lib/fluent/plugin/buf_file.rb', line 46

def initialize
  super
  @symlink_path = nil
  @multi_workers_available = false
  @additional_resume_path = nil
end

Instance Method Details

#buffer_path_for_test?Boolean

Returns:

  • (Boolean)


117
118
119
120
121
122
123
124
125
126
127
# File 'lib/fluent/plugin/buf_file.rb', line 117

def buffer_path_for_test?
  caller_locations.each do |location|
    # Thread::Backtrace::Location#path returns base filename or absolute path.
    # #absolute_path returns absolute_path always.
    # https://bugs.ruby-lang.org/issues/12159
    if location.absolute_path =~ /\/test_[^\/]+\.rb$/ # location.path =~ /test_.+\.rb$/
      return true
    end
  end
  false
end

#configure(conf) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
# File 'lib/fluent/plugin/buf_file.rb', line 53

def configure(conf)
  super

  multi_workers_configured = owner.system_config.workers > 1 ? true : false

  using_plugin_root_dir = false
  unless @path
    if root_dir = owner.plugin_root_dir
      @path = File.join(root_dir, 'buffer')
      using_plugin_root_dir = true # plugin_root_dir path contains worker id
    else
      raise Fluent::ConfigError, "buffer path is not configured. specify 'path' in <buffer>"
    end
  end

  type_of_owner = Plugin.lookup_type_from_class(@_owner.class)
  if @@buffer_paths.has_key?(@path) && !buffer_path_for_test?
    type_using_this_path = @@buffer_paths[@path]
    raise ConfigError, "Other '#{type_using_this_path}' plugin already use same buffer path: type = #{type_of_owner}, buffer path = #{@path}"
  end

  @@buffer_paths[@path] = type_of_owner

  specified_directory_exists = File.exist?(@path) && File.directory?(@path)
  unexisting_path_for_directory = !File.exist?(@path) && !@path.include?('.*')

  if specified_directory_exists || unexisting_path_for_directory # directory
    if using_plugin_root_dir || !multi_workers_configured
      @path = File.join(@path, 'buffer.*.log')
    else
      @path = File.join(@path, "worker#{fluentd_worker_id}", 'buffer.*.log')
      if fluentd_worker_id == 0
        # worker 0 always checks unflushed buffer chunks to be resumed (might be created while non-multi-worker configuration)
        @additional_resume_path = File.join(File.expand_path("../../", @path), 'buffer.*.log')
      end
    end
    @multi_workers_available = true
  else # specified path is file path
    if File.basename(@path).include?('.*.')
      # valid file path
    elsif File.basename(@path).end_with?('.*')
      @path = @path + '.log'
    else
      # existing file will be ignored
      @path = @path + '.*.log'
    end
    @multi_workers_available = false
  end

  if @dir_permission
    @dir_permission = @dir_permission.to_i(8) if @dir_permission.is_a?(String)
  else
    @dir_permission = system_config.dir_permission || DIR_PERMISSION
  end
end

#generate_chunk(metadata) ⇒ Object



170
171
172
173
174
175
176
177
# File 'lib/fluent/plugin/buf_file.rb', line 170

def generate_chunk()
  # FileChunk generates real path with unique_id
  if @file_permission
    Fluent::Plugin::Buffer::FileChunk.new(, @path, :create, perm: @file_permission, compress: @compress)
  else
    Fluent::Plugin::Buffer::FileChunk.new(, @path, :create, compress: @compress)
  end
end

#multi_workers_ready?Boolean

This method is called only when multi worker is configured

Returns:

  • (Boolean)


110
111
112
113
114
115
# File 'lib/fluent/plugin/buf_file.rb', line 110

def multi_workers_ready?
  unless @multi_workers_available
    log.error "file buffer with multi workers should be configured to use directory 'path', or system root_dir and plugin id"
  end
  @multi_workers_available
end

#persistent?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/fluent/plugin/buf_file.rb', line 135

def persistent?
  true
end

#resumeObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/fluent/plugin/buf_file.rb', line 139

def resume
  stage = {}
  queue = []

  patterns = [@path]
  patterns.unshift @additional_resume_path if @additional_resume_path
  Dir.glob(patterns) do |path|
    next unless File.file?(path)

    m = () # this metadata will be overwritten by resuming .meta file content
                       # so it should not added into @metadata_list for now
    mode = Fluent::Plugin::Buffer::FileChunk.assume_chunk_state(path)
    if mode == :unknown
      log.debug "uknown state chunk found", path: path
      next
    end

    chunk = Fluent::Plugin::Buffer::FileChunk.new(m, path, mode) # file chunk resumes contents of metadata
    case chunk.state
    when :staged
      stage[chunk.] = chunk
    when :queued
      queue << chunk
    end
  end

  queue.sort_by!{ |chunk| chunk.modified_at }

  return stage, queue
end

#startObject



129
130
131
132
133
# File 'lib/fluent/plugin/buf_file.rb', line 129

def start
  FileUtils.mkdir_p File.dirname(@path), mode: @dir_permission

  super
end