git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2 3/3] refs: add configuration to enable flushing of refs

From
Patrick Steinhardt <ps@pks.im>
Date
Nov 11, 2021, 12:06 UTC
Message-ID
<YY0HWCgg2e/cAc5y@ncase>
In-Reply-To
<211110.86v910gi9a.gmgdl@evledraar.gmail.com>
On Wed, Nov 10, 2021 at 03:49:02PM +0100, Ævar Arnfjörð Bjarmason wrote:
Show 63 quoted lines
> 
> On Wed, Nov 10 2021, Patrick Steinhardt wrote:
> 
> > [...]
> > Fix this by introducing a new configuration "core.fsyncRefFiles". This
> > config matches behaviour of "core.fsyncObjectFiles" in that it provides
> > three different modes:
> >
> >     - "off" disables calling fsync on ref files. This is the default
> >       behaviour previous to this change and remains the default after
> >       this change.
> >
> >     - "on" enables calling fsync on ref files, where each reference is
> >       flushed to disk before it is being committed. This is the safest
> >       setting, but may incur significant performance overhead.
> >
> >     - "batch" will flush the page cache of each file as it is written to
> >       ensure its data is persisted. After all refs have been written,
> >       the directories which host refs are flushed.
> >
> > With this change in place and when "core.fsyncRefFiles" is set to either
> > "on" or "batch", this kind of corruption shouldn't happen anymore.
> >
> > [1]: https://www.kernel.org/doc/Documentation/filesystems/ext4.txt
> 
> With the understanding that my grokking of this approach is still
> somewhere between "uh, that works?" and "wow, voodoo FS magic!". ....
> 
> I haven't looked at these changes in much daiter, or Neeraj's recent
> related changes but...
> 
> > +core.fsyncRefFiles::
> > +	A value indicating the level of effort Git will expend in trying to make
> > +	refs added to the repo durable in the event of an unclean system
> > +	shutdown. This setting currently only controls loose refs in the object
> > +	store, so updates to packed refs may not be equally durable. Takes the
> > +	same parameters as `core.fsyncObjectFiles`.
> > +
> 
> ...my understanding of it is basically a way of going back to what Linus
> pointed out way back in aafe9fbaf4f (Add config option to enable
> 'fsync()' of object files, 2008-06-18).
> 
> I.e. we've got files x and y. POSIX sayeth we'd need to fsync them all
> and the directory entry, but on some FS's it would be sufficient to
> fsync() just y if they're created in that order. It'll imply an fsync of
> both x and y, is that accurate?
> 
> If not you can probably discard the rest, but forging on:
> 
> Why do we then need to fsync() a "z" file in get_object_directory()
> (i.e. .git/objects) then? That's a new "z", wasn't flushing "y" enough?
> 
> Or if you've written .git/objects/x and .git/refs/y I can imagine
> wanting to create and sync a .git/z if the FS's semantics are to then
> flush all remaining updates from that tree up, but here it's
> .git/objects, not .git. That also seems to contract this above:
> 
> >       ensure its data is persisted. After all refs have been written,
> >       the directories which host refs are flushed.
> 
> I.e. that would be .git/refs (let's ignore .git/HEAD and the like for
> now), not .git/objects or .git?

Yeah, .git/refs. Note that we need to flush refs in two locations though given that there are common refs shared across worktrees, and then there are worktree-specific refs. These may not even share the filesystem, so there's not much we can do about this. We could play games with the fsid, but I'm not sure it's worth it.

Show 9 quoted lines
> And again, forging on but more generally [continued below]...
> 
> > +	if (!strcmp(var, "core.fsyncreffiles")) {
> 
> UX side: now we've got a core.fsyncRefFiles and
> core.fsyncWhateverItWasCalled in Neeraj series. Let's make sure those
> work together like say "fsck.*" and "fetch.fsck.*" do, i.e. you'd be
> able to configure this once for objects and refs, or in two variables,
> one for objects, one for refs...

Honestly, I'd prefer to just have a global variable with per-subsystem overrides so that you can say "Please batch-sync all files by default, but I really don't care for files in the worktree". Kind of goes into the same direction as your RFC.

[snip]
Show 39 quoted lines
> > @@ -2665,7 +2719,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,
> >  		files_downcast(ref_store, REF_STORE_WRITE,
> >  			       "ref_transaction_prepare");
> >  	size_t i;
> > -	int ret = 0;
> > +	int ret = 0, sync_flags = 0;
> >  	struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
> >  	char *head_ref = NULL;
> >  	int head_type;
> > @@ -2777,8 +2831,14 @@ static int files_transaction_prepare(struct ref_store *ref_store,
> >  					&update->new_oid, NULL,
> >  					NULL);
> >  		}
> > +
> > +		sync_flags |= sync_loose_refs_flags(update->refname);
> >  	}
> >  
> > +	ret = sync_loose_refs(refs, sync_flags, err);
> > +	if (ret)
> > +		goto cleanup;
> > +
> >  	if (packed_transaction) {
> >  		if (packed_refs_lock(refs->packed_ref_store, 0, err)) {
> >  			ret = TRANSACTION_GENERIC_ERROR;
> 
> ...[continued from above]: Again, per my potentially wrong understanding
> of syncing a "x" and "y" via an fsync of a subsequent "z" that's
> adjacent on the FS to those two.
> 
> Isn't this setting us up for a really bad interaction between this
> series and Neeraj's work? Well "bad" as in "bad for performance".
> 
> I.e. you'll turn on "use the batch thing for objects and refs" and we'll
> do two fsyncs, one for the object update, and one for refs. The common
> case is that we'll have both in play.
> 
> So shouldn't this go to a higher level for both so we only create a "z"
> .git/sync-it-now-please.txt or whatever once we do all pending updates
> on the .git/ directory?

Good question. Taking a different stance would be to say "I don't ever want to write a ref referring to an object which isn't yet guaranteed to be persistent". In that case, writing and syncing objects first and then updating refs would be the correct thing to do and wouldn't need coordination on a higher level.

Show 10 quoted lines
> I can also imagine that we'd want that at an even higher level, e.g. for
> "git pull" surely we'd want it not for refs or objects, but in
> builtin/pull.c somewhere because we'll be updating the .git/index after
> we do both refs and objects, and you'd want to fsync at the very end,
> no?
> 
> None of the above should mean we can't pursue a more narrow approach for
> now. I'm just:
> 
>  1) Seeing if I understand what we're trying to do here, maybe not.

Personally, my most important bullet point is that I want to get rid of the corrupt refs we see in production every now and then. As Peff pointed out in another email, it _might_ be sufficient to just flush out the page cache for those without synchronizing any metadata at all. But none of us seems to be sure that this really works the way we think it does.

>  2) Encouraging you two to think about a holistic way to configure some
>     logical conclusion to this topic at large, so we won't end up with
>     core.fsyncConfigFiles, core.fsyncObjectFiles, core.fsyncIndexFile,
>     core.fsyncRefFiles, core.fsyncTheCrapRebaseWritesOutFiles etc. :)

Yeah, I agree with you on this point. We definitely don't want to end up with twenty different knobs to sync objects, refs, packed-refs, configs, reflogs et al, and we're currently steering into that direction. Having a central knob core.fsync which directs defaults for all subsystems would be very much preferable if you ask me, where specific subsystems can then be changed as the user sees fit.

What I think is important though is that we should have the right defaults in place. It doesn't help to say "fsync is slow" as an excuse to not do them at all by default, where the result is that users get a corrupt repository.

Patrick
Previous: Patrick SteinhardtNext: Neeraj Singh
Message 30 of 34 in “refs: sync loose refs to disk before committing them”
  1. refs: sync loose refs to disk before committing themPatrick Steinhardt, Nov 4, 2021
  2. Ævar Arnfjörð BjarmasonNov 4, 2021
  3. Patrick SteinhardtNov 4, 2021
  4. Junio C HamanoNov 4, 2021
  5. Neeraj SinghNov 4, 2021
  6. Junio C HamanoNov 5, 2021
  7. Jeff KingNov 5, 2021
  8. Ævar Arnfjörð BjarmasonNov 5, 2021
  9. Jeff KingNov 5, 2021
  10. Jeff KingNov 5, 2021
  11. Jeff KingNov 5, 2021
  12. Johannes SchindelinNov 5, 2021
  13. Patrick SteinhardtNov 5, 2021
  14. Jeff KingNov 5, 2021
  15. Patrick SteinhardtNov 9, 2021
  16. Jeff KingNov 10, 2021
  17. Patrick SteinhardtNov 10, 2021
  18. 0/3 refs: sync loose refs to disk before committing themPatrick Steinhardt, Nov 10, 2021
  19. 1/3 wrapper: handle EINTR in `git_fsync()`Patrick Steinhardt, Nov 10, 2021
  20. Johannes SchindelinNov 10, 2021
  21. Ævar Arnfjörð BjarmasonNov 10, 2021
  22. 2/3 wrapper: provide function to sync directoriesPatrick Steinhardt, Nov 10, 2021
  23. Ævar Arnfjörð BjarmasonNov 10, 2021
  24. 3/3 refs: add configuration to enable flushing of refsPatrick Steinhardt, Nov 10, 2021
  25. Ævar Arnfjörð BjarmasonNov 10, 2021
  26. Neeraj SinghNov 10, 2021
  27. Ævar Arnfjörð BjarmasonNov 10, 2021
  28. Neeraj SinghNov 11, 2021
  29. Patrick SteinhardtNov 11, 2021
  30. Patrick SteinhardtNov 11, 2021
  31. Neeraj SinghNov 11, 2021
  32. Johannes SchindelinNov 10, 2021
  33. Jeff KingNov 10, 2021
  34. Patrick SteinhardtNov 11, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.