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

Re: [PATCH v2 04/12] refs: implement simple transactions for the packed-refs file

From
Michael Haggerty <mhagger@alum.mit.edu>
Date
Jun 20, 2013, 07:49 UTC
Message-ID
<51C2B41F.2050708@alum.mit.edu>
In-Reply-To
<7vfvwdzz6k.fsf@alter.siamese.dyndns.org>
On 06/19/2013 09:18 PM, Junio C Hamano wrote:
Show 57 quoted lines
> Michael Haggerty <mhagger@alum.mit.edu> writes:
> 
>> Handle simple transactions for the packed-refs file at the
>> packed_ref_cache level via new functions lock_packed_refs(),
>> commit_packed_refs(), and rollback_packed_refs().
>>
>> Only allow the packed ref cache to be modified (via add_packed_ref())
>> while the packed refs file is locked.
>>
>> Change clone to add the new references within a transaction.
>>
>> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
>> ---
>>  builtin/clone.c |  7 ++++-
>>  refs.c          | 83 ++++++++++++++++++++++++++++++++++++++++++++++-----------
>>  refs.h          | 27 +++++++++++++++++--
>>  3 files changed, 98 insertions(+), 19 deletions(-)
>>
>> diff --git a/builtin/clone.c b/builtin/clone.c
>> index 66bff57..b0c000a 100644
>> --- a/builtin/clone.c
>> +++ b/builtin/clone.c
>> @@ -489,17 +489,22 @@ static struct ref *wanted_peer_refs(const struct ref *refs,
>>  	return local_refs;
>>  }
>>  
>> +static struct lock_file packed_refs_lock;
>> +
>>  static void write_remote_refs(const struct ref *local_refs)
>>  {
>>  	const struct ref *r;
>>  
>> +	lock_packed_refs(&packed_refs_lock, LOCK_DIE_ON_ERROR);
>> +
>>  	for (r = local_refs; r; r = r->next) {
>>  		if (!r->peer_ref)
>>  			continue;
>>  		add_packed_ref(r->peer_ref->name, r->old_sha1);
>>  	}
>>  
>> -	pack_refs(PACK_REFS_ALL);
>> +	if (commit_packed_refs())
>> +		die_errno("unable to overwrite old ref-pack file");
>>  }
> 
> The calling convention used here looks somewhat strange.  You allow
> callers to specify which lock-file structure is used when locking,
> but when you are done, commit_packed_refs() does not take any
> parameter.
> 
> lock_packed_refs() make the singleton in-core packed-ref-cache be
> aware of which lock it is under, so commit_packed_refs() does not
> need to be told (the singleton already knows what lockfile is in
> effect), so I am not saying the code is broken, though.
> 
> Does the caller need to even have an access to this lock_file
> instance?
No it doesn't.  My reasoning was as follows:

lock_file instances can never be freed, because they are added to a linked list in the atexit handler but never removed. Therefore they have to be allocated statically (or allocated dynamically then leaked).

[I just noticed that lock_ref_sha1_basic() leaks a struct lock_file every time that it is called.]

I wanted to leave the way open to allow other packed refs caches to be locked. But since all packed refs caches are allocated dynamically, the lock_file instance cannot be part of struct packed_ref_cache. So I thought that the easiest approach is to rely on the caller, who presumably can know that only a finite number of locks are in use at once, to supply a usable lock_file instance.

But currently only the main packed ref cache can be locked, so it would be possible for lock_packed_refs() to use the static packlock instance for locking. I will change it to do so.

If/when we add support for locking other packed ref caches, we can change the API to use a caller-supplied lock_file. Or even better, implement a way to remove a lock_file instance from the atexit list; then lock_packed_refs() could use dynamically-allocated lock_file instances without having to leak them.

v3 to come.
Michael
-- 
Michael Haggerty
mhagger@alum.mit.edu
http://softwareswirl.blogspot.com/
Previous: Junio C HamanoNext: Jeff King
Message 7 of 24 in “Fix some reference-related races”
  1. 00/12 Fix some reference-related racesMichael Haggerty, Jun 19, 2013
  2. 01/12 repack_without_ref(): split list curation and entry writingMichael Haggerty, Jun 19, 2013
  3. 02/12 pack_refs(): split creation of packed refs and entry writingMichael Haggerty, Jun 19, 2013
  4. 03/12 refs: wrap the packed refs cache in a level of indirectionMichael Haggerty, Jun 19, 2013
  5. 04/12 refs: implement simple transactions for the packed-refs fileMichael Haggerty, Jun 19, 2013
  6. Junio C HamanoJun 19, 2013
  7. Michael HaggertyJun 20, 2013
  8. Jeff KingJun 20, 2013
  9. Michael HaggertyJun 20, 2013
  10. Jeff KingJun 20, 2013
  11. Junio C HamanoJun 20, 2013
  12. Michael HaggertyJun 20, 2013
  13. Junio C HamanoJun 20, 2013
  14. 05/12 refs: manage lifetime of packed refs cache via reference countingMichael Haggerty, Jun 19, 2013
  15. 06/12 do_for_each_entry(): increment the packed refs cache refcountMichael Haggerty, Jun 19, 2013
  16. 07/12 packed_ref_cache: increment refcount when lockedMichael Haggerty, Jun 19, 2013
  17. 08/12 Extract a struct stat_data from cache_entryMichael Haggerty, Jun 19, 2013
  18. 09/12 add a stat_validity structMichael Haggerty, Jun 19, 2013
  19. 10/12 get_packed_ref_cache: reload packed-refs file when it changesMichael Haggerty, Jun 19, 2013
  20. 11/12 for_each_ref: load all loose refs before packed refsMichael Haggerty, Jun 19, 2013
  21. 12/12 refs: do not invalidate the packed-refs cache unnecessarilyMichael Haggerty, Jun 19, 2013
  22. Jeff KingJun 19, 2013
  23. Michael HaggertyJun 20, 2013
  24. Jeff KingJun 20, 2013

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.