From 9a7cda6c533564c286b132b8006d8b8378ad20b3 Mon Sep 17 00:00:00 2001 From: Fujimoto Seiji Date: Mon, 19 Apr 2021 10:33:38 +0900 Subject: [PATCH] storage_local: change LocalStorage.save() to be atomic 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 --- lib/fluent/plugin/storage_local.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fluent/plugin/storage_local.rb b/lib/fluent/plugin/storage_local.rb index 1ae0c6cd54..d17022fe6b 100644 --- a/lib/fluent/plugin/storage_local.rb +++ b/lib/fluent/plugin/storage_local.rb @@ -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 }