natefinch / lumberjack

lumberjack is a log rolling package for Go
MIT License
4.76k stars 585 forks source link

Make compressed log rotation atomic #155

Open chancez opened 2 years ago

chancez commented 2 years ago

If another process is watching for *.gz files then it's possible to begin reading the archive before it has been completely created, resulting in corruption if the other process is copying the archive to another location (for example: archival to s3).

To resolve this, we can use a different suffix when writing the file so that other programs do not read it while it's being created. Once the archive has been completely created, we atomically rename it to the desired file name with the *.gz extension, ensuring external programs only ever see the finished archive.

Signed-off-by: Chance Zibolski chance.zibolski@gmail.com

pchaigno commented 2 years ago

@natefinch :wave: Did you get a chance to look into this?

natefinch commented 2 years ago

I mean, I guess it's ok? but then what if code is looking for .tmp files?

What problem is this solving? What program do you have that is looking for *.gz files and what does it do?

chancez commented 2 years ago

@natefinch I considered making the tmp suffix configurable. Basically, in cilium we are using lumberjack for log rotation, and have a daemon watching for *.gz files and is uploading them to s3 for archival. What's happening without this is that the daemon is seeing the files get created, and it's uploading the files before they've been completely written to, so we've got corrupted .gz files in s3.

Also, I need to push up another fix to this PR, I forgot to Flush the writer before calling Sync on the underlying file. (Done)

pchaigno commented 2 years ago

Hi @natefinch :wave: Does the above look good?

pchaigno commented 2 years ago

@natefinch Did you get a chance to look into this? Is there anything else we'd need to change?

kaworu commented 11 months ago

@natefinch could you take a quick look at this PR please? :pray: