Skip to content

Commit

Permalink
buf_file: handle "Too many open files" error to keep buffer and metad…
Browse files Browse the repository at this point in the history
…ata pair

This is not incomplete but keep buffer and metadata pair phase
as much as possible.
  • Loading branch information
repeatedly committed Feb 15, 2017
1 parent 3e3ff24 commit 387295c
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions lib/fluent/plugin/buffer/file_chunk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,20 @@ def enqueued!

write_metadata(update: false) # re-write metadata w/ finalized records

file_rename(@chunk, @path, new_chunk_path, ->(new_io){ @chunk = new_io })
@path = new_chunk_path
begin
file_rename(@chunk, @path, new_chunk_path, ->(new_io) { @chunk = new_io })
rescue => e
raise "can't enqueue buffer file: path = #{@path}, error = #{e}"
end

begin
file_rename(@meta, @meta_path, new_meta_path, ->(new_io) { @meta = new_io })
rescue => e
file_rename(@chunk, new_chunk_path, @path, ->(new_io) { @chunk = new_io })
raise "can't enqueue buffer metadata: path = #{@meta_path}, error = #{e}"
end

file_rename(@meta, @meta_path, new_meta_path, ->(new_io){ @meta = new_io })
@path = new_chunk_path
@meta_path = new_meta_path

super
Expand Down Expand Up @@ -242,14 +252,24 @@ def file_rename(file, old_path, new_path, callback=nil)
def create_new_chunk(path, perm)
@path = self.class.generate_stage_chunk_path(path, @unique_id)
@meta_path = @path + '.meta'
@chunk = File.open(@path, 'wb+', perm)
@chunk.set_encoding(Encoding::ASCII_8BIT)
@chunk.sync = true
@chunk.binmode
@meta = File.open(@meta_path, 'wb', perm)
@meta.set_encoding(Encoding::ASCII_8BIT)
@meta.sync = true
@meta.binmode
begin
@chunk = File.open(@path, 'wb+', perm)
@chunk.set_encoding(Encoding::ASCII_8BIT)
@chunk.sync = true
@chunk.binmode
rescue => e
raise BufferOverflowError, "can't create buffer file for #{path}. Stop creating buffer files: error = #{e}"
end
begin
@meta = File.open(@meta_path, 'wb', perm)
@meta.set_encoding(Encoding::ASCII_8BIT)
@meta.sync = true
@meta.binmode
rescue => e
@chunk.close
File.unlink(@path)
raise BufferOverflowError, "can't create buffer metadata for #{path}. Stop creating buffer files: error = #{e}"
end

@state = :unstaged
@bytesize = 0
Expand Down

0 comments on commit 387295c

Please sign in to comment.