gc: ignore old gc.log files
PATCH v5 1/1 gc: ignore old gc.log files
A server can end up in a state where there are lots of unreferenced loose objects (say, because many users are doing a bunch of rebasing and pushing their rebased branches). Running "git gc --auto" in this state would cause a gc.log file to be created, preventing future auto gcs, causing pack files to pile up. Since many git operations are O(n) in the number of pack files, this would lead to poor performance.
Git should never get itself into a state where it refuses to do any maintenance, just because at some point some piece of the maintenance didn't make progress.
Teach Git to ignore gc.log files which are older than (by default) one day old, which can be tweaked via the gc.logExpiry configuration variable. That way, these pack files will get cleaned up, if necessary, at least once per day. And operators who find a need for more-frequent gcs can adjust gc.logExpiry to meet their needs.
There is also some cleanup: a successful manual gc, or a warning-free auto gc with an old log file, will remove any old gc.log files.
It might still happen that manual intervention is required (e.g. because the repo is corrupt), but at the very least it won't be because Git is too dumb to try again.
--- Documentation/config.txt | 6 +++++ builtin/gc.c | 59 ++++++++++++++++++++++++++++++++++++++++++------ t/t6500-gc.sh | 15 ++++++++++++ 3 files changed, 73 insertions(+), 7 deletions(-)
Documentation/config.txt +6 -0
@@ -1402,6 +1402,12 @@ gc.autoDetach:: Make `git gc --auto` return immediately and run in background if the system supports it. Default is true. +gc.logExpiry:: + If the file gc.log exists, then `git gc --auto` won't run + unless that file is more than 'gc.logExpiry' old. Default is + "1.day". See `gc.pruneExpire` for more ways to specify its + value. + gc.packRefs:: Running `git pack-refs` in a repository renders it unclonable by Git versions prior to 1.5.1.2 over dumb
builtin/gc.c +52 -7
105 lines not on this page · raw message
t/t6500-gc.sh +15 -0
@@ -67,5 +67,20 @@ test_expect_success 'auto gc with too many loose objects does not attempt to cre test_line_count = 2 new # There is one new pack and its .idx ' +test_expect_success 'background auto gc does not run if gc.log is present and recent but does if it is old' ' + test_commit foo && + test_commit bar && + git repack && + test_config gc.autopacklimit 1 && + test_config gc.autodetach true && + echo fleem >.git/gc.log && + test_must_fail git gc --auto 2>err && + test_i18ngrep "^error:" err && + test_config gc.logexpiry 5.days && + test-chmtime =-345600 .git/gc.log && + test_must_fail git gc --auto && + test_config gc.logexpiry 2.days && + git gc --auto +' test_done
Signature · 1 line
2.11.GIT
Quoted 5 lines
builtin/gc.c +0
-1@@ -76,10 +78,30 @@ static void git_config_date_string(const char *key, const char **output)static void process_log_file (void ) {struct stat st; -if (!fstat (get_lock_file_fd (&log_lock), &st) && st.st_size)
builtin/gc.c static void git_config_date_string(const char *key, const char **output) · line 90
Quoted 10 lines
builtin/gc.c +10
-0+if (fstat (get_lock_file_fd (&log_lock), &st)) { +/* +* Perhaps there was an i/o error or another +* unlikely situation. Try to make a note of +* this in gc.log along with any existing +* messages. +*/ +FILE *fp; +int saved_errno = errno; + fp =fdopen (log_lock.tempfile.fd,"a" );
We usually use xfdopen() to handle (unlikely) errors rather than segfaulting. But I think you'd actually want fdopen_lock_file(), which attaches the fd to the tempfile for flushing and cleanup purposes.
That said, I'm not sure I understand why you're opening a new stdio filehandle. We know that stderr already points to our logfile (that's how content gets there in the first place). If there's a problem with the file or the descriptor, opening a new filehandle around the same descriptor won't help.
Speaking of stderr, I wonder if this function should be calling fflush(stderr) before looking at the fstat result. There could be contents buffered there that haven't been written out yet (not from child processes, but perhaps ones written in this process itself). Probably unlikely in practice, since stderr is typically unbuffered by default.
-Peff
Quoted 34 lines
-----Original Message-----
From: Jeff King [mailto:[email protected]]
Sent: Friday, February 10, 2017 4:15 PM
To: David Turner <[email protected]>
Cc: [email protected]; [email protected]; Junio C Hamano <[email protected]>
Subject: Re: [PATCH v5] gc: ignore old gc.log filesQuoted 4 lines
builtin/gc.c +0
-1@@ -76,10 +78,30 @@ static void git_config_date_string(const char *key, const char **output) static void process_log_file(void) {struct stat st; -if (!fstat (get_lock_file_fd (&log_lock), &st) && st.st_size)builtin/gc.c static void git_config_date_string(const char · line 89
Quoted 10 lines
builtin/gc.c +10
-0+if (fstat (get_lock_file_fd (&log_lock), &st)) { +/* +* Perhaps there was an i/o error or another +* unlikely situation. Try to make a note of +* this in gc.log along with any existing +* messages. +*/ +FILE *fp; +int saved_errno = errno; + fp =fdopen (log_lock.tempfile.fd,"a" );We usually use
xfdopen()to handle (unlikely) errors rather than segfaulting. But I think you'd actually wantfdopen_lock_file(), which attaches the fd to the tempfile for flushing and cleanup purposes.That said, I'm not sure I understand why you're opening a new stdio filehandle. We know that stderr already points to our logfile (that's how content gets there in the first place). If there's a problem with the file or the descriptor, opening a new filehandle around the same descriptor won't help.
Quoted 5 lines
Speaking of stderr, I wonder if this function should be calling fflush(stderr) before looking at the fstat result. There could be contents buffered there that haven't been written out yet (not from child processes, but perhaps ones written in this process itself).
Probably unlikely in practice, since stderr is typically unbuffered by default.
Process_log_file_at_exit calls fflush. Will fix the other.
Quoted 7 lines · David Turner
Quoted 5 lines
Speaking of stderr, I wonder if this function should be calling fflush(stderr) before looking at the fstat result. There could be contents buffered there that haven't been written out yet (not from child processes, but perhaps ones written in this process itself).
Probably unlikely in practice, since stderr is typically unbuffered by default.
Process_log_file_at_exitcalls fflush. Will fix the other.
Ah, good. That makes sense, since we might deadlock if we do it in a signal handler. Perhaps that is a reason not to use stderr here again (though if we want to be that careful, a new fdopen() call is also a bad idea, as we can deadlock over the malloc() lock; you'd have to snprintf to a small buffer and dump it with write()).
-Peff