Skip to content

Commit

Permalink
storage_local: change LocalStorage.save() to be atomic
Browse files Browse the repository at this point in the history
Let's use unique names for temporary files like this:

    kubernetes.json.tmp.5c04a66a9731459acb723c5ee7d8d279

instead of:

    kubernetes.json.tmp

This makes LocalStorage.save() practically atomic, and hence allows
Fluentd not to corrupt data even in a race condition.

Signed-off-by: Fujimoto Seiji <[email protected]>
  • Loading branch information
fujimotos committed Apr 19, 2021
1 parent 72267d8 commit 9a7cda6
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lib/fluent/plugin/storage_local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def load

def save
return if @on_memory
tmp_path = @path + '.tmp'
tmp_path = @path + '.tmp.' + Fluent::UniqueId.hex(Fluent::UniqueId.generate)
begin
json_string = Yajl::Encoder.encode(@store, pretty: @pretty_print)
File.open(tmp_path, 'w:utf-8', @mode) { |io| io.write json_string; io.fsync }
Expand Down

0 comments on commit 9a7cda6

Please sign in to comment.