Skip to content

Commit

Permalink
Use JSON instead of Yajl to improve performance of store/load files
Browse files Browse the repository at this point in the history
Signed-off-by: Shizuo Fujita <[email protected]>
  • Loading branch information
Watson1978 committed Jan 6, 2025
1 parent b9fc438 commit b380e95
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/fluent/plugin/formatter_out_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

require 'fluent/plugin/formatter'
require 'fluent/time'
require 'yajl'
require 'json'

module Fluent
module Plugin
Expand Down Expand Up @@ -46,7 +46,7 @@ def format(tag, time, record)
header = ''
header << "#{@timef.format(time)}#{@delimiter}" if @output_time
header << "#{tag}#{@delimiter}" if @output_tag
"#{header}#{Yajl.dump(record)}#{@newline}"
"#{header}#{JSON.generate(record)}#{@newline}"
end
end
end
Expand Down
12 changes: 8 additions & 4 deletions lib/fluent/plugin/storage_local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
require 'fluent/plugin/storage'

require 'fileutils'
require 'yajl'
require 'json'

module Fluent
module Plugin
Expand Down Expand Up @@ -90,7 +90,7 @@ def configure(conf)
log.warn "detect empty plugin storage file during startup. Ignored: #{@path}"
return
end
data = Yajl::Parser.parse(data)
data = JSON.parse(data)
raise Fluent::ConfigError, "Invalid contents (not object) in plugin storage file: '#{@path}'" unless data.is_a?(Hash)
rescue => e
log.error "failed to read data from plugin storage file", path: @path, error: e
Expand All @@ -114,7 +114,7 @@ def load
return unless File.exist?(@path)
begin
json_string = File.open(@path, 'r:utf-8'){ |io| io.read }
json = Yajl::Parser.parse(json_string)
json = JSON.parse(json_string)
unless json.is_a?(Hash)
log.error "broken content for plugin storage (Hash required: ignored)", type: json.class
log.debug "broken content", content: json_string
Expand All @@ -130,7 +130,11 @@ def save
return if @on_memory
tmp_path = @path + '.tmp.' + Fluent::UniqueId.hex(Fluent::UniqueId.generate)
begin
json_string = Yajl::Encoder.encode(@store, pretty: @pretty_print)
if @pretty_print
json_string = JSON.pretty_generate(@store)
else
json_string = JSON.generate(@store)
end
File.open(tmp_path, 'w:utf-8', @mode) { |io| io.write json_string; io.fsync }
File.rename(tmp_path, @path)
rescue => e
Expand Down

0 comments on commit b380e95

Please sign in to comment.