valkey icon indicating copy to clipboard operation
valkey copied to clipboard

Keep the log fd, don't re-open logfile in every logs

Open enjoy-binbin opened this issue 1 year ago • 6 comments

Currently in serverLog, we fopen and fclose the logfile in every call, if the log prints too much, it will waste performance. Also frequent open may cause blocking in some caes, like doing a big rename in the same dir (like rename the bgsave temp RDB).

In this PR, we keep the log fd, and only re-open it when dir changed. This also improve performance, using benchmark DEBUG log shows that it improves the performance by 3 times.

enjoy-binbin avatar Aug 14 '24 05:08 enjoy-binbin

Codecov Report

Attention: Patch coverage is 90.32258% with 3 lines in your changes missing coverage. Please review.

Project coverage is 70.35%. Comparing base (370bdb3) to head (3c37799). Report is 288 commits behind head on unstable.

Files with missing lines Patch % Lines
src/config.c 60.00% 2 Missing :warning:
src/server.c 95.83% 1 Missing :warning:
Additional details and impacted files
@@            Coverage Diff            @@
##           unstable     #906   +/-   ##
=========================================
  Coverage     70.34%   70.35%           
=========================================
  Files           112      112           
  Lines         61490    61492    +2     
=========================================
+ Hits          43258    43265    +7     
+ Misses        18232    18227    -5     
Files with missing lines Coverage Δ
src/debug.c 54.37% <100.00%> (+0.39%) :arrow_up:
src/server.h 100.00% <ø> (ø)
src/server.c 88.56% <95.83%> (+0.02%) :arrow_up:
src/config.c 78.93% <60.00%> (+0.23%) :arrow_up:

... and 9 files with indirect coverage changes

codecov[bot] avatar Aug 14 '24 05:08 codecov[bot]

@madolson @zuiderkwast @PingXie do you guys want to takes a look with this? i would like to include it in the RC3

enjoy-binbin avatar Aug 27 '24 05:08 enjoy-binbin

This is a breaking change IIRC, if there are existing mechanisms for log rotation we need to send a signal to the process to close and re-open the log. We discussed this at length in Redis, but never committed to it.

madolson avatar Aug 27 '24 16:08 madolson

Good point @madolson. I think we can't do this change without a more advanced solution then.

We can use ionotify on linux and kqueue on BSD/MacOS to watch the file and be notified when it is renamed. Then we close and reopen it. Fallback to the old behaviour if no watch method is available.

zuiderkwast avatar Aug 27 '24 20:08 zuiderkwast

ok, i see the point, indeed in this case, we need to find a way to re-open the file.

enjoy-binbin avatar Aug 28 '24 03:08 enjoy-binbin

This is a breaking change IIRC, if there are existing mechanisms for log rotation we need to send a signal to the process to close and re-open the log. We discussed this at length in Redis, but never committed to it.

That is great information. I think it would be very helpful if we could leave this context in serverLogRaw.

PingXie avatar Aug 28 '24 03:08 PingXie