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

Use JSON instead of Yajl to improve performance of store/load files #4759

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
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
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
Loading