From fe75ca3b4d6ddcf3929193c256f96dec5e004d30 Mon Sep 17 00:00:00 2001 From: Shizuo Fujita Date: Mon, 6 Jan 2025 17:52:05 +0900 Subject: [PATCH] Use JSON instead of Yajl to improve performance of store/load files Signed-off-by: Shizuo Fujita --- lib/fluent/plugin/formatter_out_file.rb | 4 ++-- lib/fluent/plugin/storage_local.rb | 12 ++++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/fluent/plugin/formatter_out_file.rb b/lib/fluent/plugin/formatter_out_file.rb index 364303000a..76bc41fc31 100644 --- a/lib/fluent/plugin/formatter_out_file.rb +++ b/lib/fluent/plugin/formatter_out_file.rb @@ -16,7 +16,7 @@ require 'fluent/plugin/formatter' require 'fluent/time' -require 'yajl' +require 'json' module Fluent module Plugin @@ -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 diff --git a/lib/fluent/plugin/storage_local.rb b/lib/fluent/plugin/storage_local.rb index b9e159f671..3cd8addc12 100644 --- a/lib/fluent/plugin/storage_local.rb +++ b/lib/fluent/plugin/storage_local.rb @@ -19,7 +19,7 @@ require 'fluent/plugin/storage' require 'fileutils' -require 'yajl' +require 'json' module Fluent module Plugin @@ -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 @@ -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 @@ -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