Make update refs more atomic
v3 cover Make update refs more atomic
refs.c:ref_transaction_commit() intermingles doing updates and checks with actually applying changes to the refs in loops that abort on error. This is done one ref at a time and means that if an error is detected that will fail the operation partway through the list of refs to update we will end up with some changes applied to disk and others not.
Without having transaction support from the filesystem, it is hard to make an update that involves multiple refs to guarantee atomicity, but we can do a somewhat better than we currently do.
These patches change the update and delete functions to use a three call pattern of
1, lock
2, update, or flag for deletion
3, apply on disk (rename() or unlink())
When a transaction is commited we first do all the locking, preparations and most of the error checking before we actually start applying any changes to the filesystem store.
This means that more of the error cases that will fail the commit will trigger before we start doing any changes to the actual files.
This should make the changes of refs in refs_transaction_commit slightly more atomic.
Version 3:
* Rebased onto mhagger/ref-transactions.
* Removed the patch to do update/delete from a single loop.
Version 2:
Updates and fixes based on Junio's feedback.
* Fix the subject line for patches so they comply with the project standard.
* Redo the update/delete loops so that we maintain the correct order of operations. Perform all updates first, then perform the deletes.
* Add an additional patch that allows us to do the update/delete in the correct order from within a single loop by first sorting the refs so that deletes are after all non-deletes.
Ronnie Sahlberg (3):
refs.c: split writing and commiting a ref into two separate functions refs.c: split delete_ref_loose() into a separate flag-for-deletion and
commit phase
refs.c: change update_refs to run the commit loops once all work is
finished
branch.c | 10 ++++- builtin/commit.c | 5 +++ builtin/fetch.c | 7 +++- builtin/receive-pack.c | 4 ++ builtin/replace.c | 6 ++- builtin/tag.c | 6 ++- fast-import.c | 7 +++- refs.c | 99 ++++++++++++++++++++++++++++++++++---------------- refs.h | 6 +++ sequencer.c | 4 ++ walker.c | 4 ++ 11 files changed, 120 insertions(+), 38 deletions(-)
Signature · 1 line
1.9.1.505.g6fcb662
PATCH v3 1/3 refs.c: split writing and commiting a ref into two separate functions
Change the function write_ref_sha1() to just write the ref but not commit the ref or the lockfile.
Add a new function commit_ref_lock() that will commit the change done by a previous write_ref_sha1().
Update all callers of write_ref_sha1() to call commit_ref_lock().
The new pattern for updating a ref is now :
lock = lock_ref_sha1_basic() (or varient of)write_ref_sha1(lock)unlock_ref(lock) | commit_ref_lock(lock)
Once write_ref_sha1() returns, the new ref has been written and the lock file has been closed.
At that stage we can then either call unlock_ref() which will abort the update and delete the lock file withouth applying it, or call commit_ref_lock() which will rename the lock file onto the ref file.
--- branch.c | 10 ++++++++-- builtin/commit.c | 5 +++++ builtin/fetch.c | 7 ++++++- builtin/receive-pack.c | 4 ++++ builtin/replace.c | 6 +++++- builtin/tag.c | 6 +++++- fast-import.c | 7 ++++++- refs.c | 41 +++++++++++++++++++++++++++++++---------- refs.h | 4 ++++ sequencer.c | 4 ++++ walker.c | 4 ++++ 11 files changed, 82 insertions(+), 16 deletions(-)
branch.c +8 -2
@@ -304,9 +304,15 @@ void create_branch(const char *head,if (real_ref && track)setup_tracking (ref.buf +11 , real_ref, track, quiet); -if (!dont_change_ref) -if (write_ref_sha1 (lock, sha1, msg) <0 ) +if (!dont_change_ref) { +if (write_ref_sha1 (lock, sha1, msg) <0 ) { +unlock_ref (lock);die_errno (_ ("Failed to write ref" )); + } +if (commit_ref_lock (lock) <0 ) { +die_errno (_ ("Failed to commit ref" )); + } + }strbuf_release (&ref);free (real_ref);
builtin/commit.c +5 -0
@@ -1686,9 +1686,14 @@ int cmd_commit(int argc, const char **argv, const char *prefix)die (_ ("cannot lock HEAD ref" )); }if (write_ref_sha1 (ref_lock, sha1, sb.buf) <0 ) { +unlock_ref (ref_lock);rollback_index_files ();die (_ ("cannot update HEAD ref" )); } +if (commit_ref_lock (ref_lock) <0 ) { +rollback_index_files (); +die (_ ("cannot commit HEAD ref" )); + }unlink (git_path ("CHERRY_PICK_HEAD" ));unlink (git_path ("REVERT_HEAD" ));
builtin/fetch.c +6 -1
@@ -388,7 +388,12 @@ static int s_update_ref(const char *action,if (!lock)return errno ==ENOTDIR ?STORE_REF_ERROR_DF_CONFLICT :STORE_REF_ERROR_OTHER ; -if (write_ref_sha1 (lock, ref->new_sha1, msg) <0 ) +if (write_ref_sha1 (lock, ref->new_sha1, msg) <0 ) { +unlock_ref (lock); +return errno ==ENOTDIR ?STORE_REF_ERROR_DF_CONFLICT : +STORE_REF_ERROR_OTHER ; + } +if (commit_ref_lock (lock) <0 )return errno ==ENOTDIR ?STORE_REF_ERROR_DF_CONFLICT :STORE_REF_ERROR_OTHER ;return 0 ;
builtin/receive-pack.c +4 -0
@@ -587,8 +587,12 @@ static const char *update(struct command *cmd, struct shallow_info *si)return "failed to lock" ; }if (write_ref_sha1 (lock, new_sha1,"push" )) { +unlock_ref (lock);return "failed to write" ;/* error() already called */ } +if (commit_ref_lock (lock)) +return "failed to commit" ;/* error() already called */ +return NULL ;/* good */ } }
builtin/replace.c +5 -1
@@ -160,8 +160,12 @@ static int replace_object(const char *object_ref, const char *replace_ref, lock =lock_any_ref_for_update (ref, prev,0 ,NULL );if (!lock)die ("%s: cannot lock the ref" , ref); -if (write_ref_sha1 (lock, repl,NULL ) <0 ) +if (write_ref_sha1 (lock, repl,NULL ) <0 ) { +unlock_ref (lock);die ("%s: cannot update the ref" , ref); + } +if (commit_ref_lock (lock) <0 ) +die ("%s: cannot commit the ref" , ref);return 0 ; }
builtin/tag.c +5 -1
@@ -644,8 +644,12 @@ int cmd_tag(int argc, const char **argv, const char *prefix) lock =lock_any_ref_for_update (ref.buf, prev,0 ,NULL );if (!lock)die (_ ("%s: cannot lock the ref" ), ref.buf); -if (write_ref_sha1 (lock, object,NULL ) <0 ) +if (write_ref_sha1 (lock, object,NULL ) <0 ) { +unlock_ref (lock);die (_ ("%s: cannot update the ref" ), ref.buf); + } +if (commit_ref_lock (lock) <0 ) +die (_ ("%s: cannot commit the ref" ), ref.buf);if (force && !is_null_sha1 (prev) &&hashcmp (prev, object))printf (_ ("Updated tag '%s' (was %s)\n" ), tag,find_unique_abbrev (prev,DEFAULT_ABBREV ));
fast-import.c +6 -1
@@ -1706,8 +1706,13 @@ static int update_branch(struct branch *b)return -1 ; } } -if (write_ref_sha1 (lock, b->sha1, msg) <0 ) +if (write_ref_sha1 (lock, b->sha1, msg) <0 ) { +unlock_ref (lock);return error ("Unable to update %s" , b->name); + } +if (commit_ref_lock (lock) <0 ) { +return error ("Unable to commit %s" , b->name); + }return 0 ; }
refs.c +31 -10
113 lines not on this page · raw message
refs.h +4 -0
@@ -8,6 +8,7 @@ struct ref_lock {unsigned char old_sha1[20 ];int lock_fd;int force_write; +int skipped_write; };struct ref_transaction ;
@@ -153,6 +154,9 @@ extern void unlock_ref(struct ref_lock *lock);/** Writes sha1 into the ref specified by the lock. **/ extern int write_ref_sha1 (struct ref_lock *lock,const unsigned char *sha1,const char *msg); +/** Commit any changes done to the ref specified by the lock. **/ +extern int commit_ref_lock (struct ref_lock *lock); +/** Setup reflog before using. **/ int log_ref_setup (const char *refname,char *logfile,int bufsize);
sequencer.c +4 -0
@@ -283,6 +283,10 @@ static int fast_forward_to(const unsigned char *to, const unsigned char *from, 0, NULL);strbuf_addf (&sb,"%s: fast-forward" ,action_name (opts)); ret =write_ref_sha1 (ref_lock, to, sb.buf); +if (ret) +unlock_ref (ref_lock); +else + ret |=commit_ref_lock (ref_lock);strbuf_release (&sb);return ret; }
walker.c +4 -0
@@ -295,6 +295,10 @@ int walker_fetch(struct walker *walker, int targets, char **target,if (!write_ref || !write_ref[i])continue ; ret =write_ref_sha1 (lock[i], &sha1[20 * i], msg ? msg :"fetch (unknown)" ); +if (ret) +unlock_ref (lock[i]); +else + ret |=commit_ref_lock (lock[i]); lock[i] =NULL ;if (ret) goto unlock_and_fail;
Signature · 1 line
1.9.1.505.g4f1e74f
PATCH v3 2/3 refs.c: split delete_ref_loose() into a separate flag-for-deletion and commit phase
In reply to Ronnie SahlbergChange delete_ref_loose()) to just flag that a ref is to be deleted but do not actually unlink the files.
Change commit_ref_lock() so that it will unlink refs that are flagged for deletion.
Change all callers of delete_ref_loose() to explicitely call commit_ref_lock() to commit the deletion.
The new pattern for deleting loose refs thus become:
lock = lock_ref_sha1_basic() (or varient of)delete_ref_loose(lock)unlock_ref(lock) | commit_ref_lock(lock)
--- refs.c | 31 ++++++++++++++++++++----------- refs.h | 2 ++ 2 files changed, 22 insertions(+), 11 deletions(-)
refs.c +20 -11
@@ -2484,16 +2484,9 @@ static int repack_without_ref(const char *refname)static int delete_ref_loose (struct ref_lock *lock,int flag) { -if (!(flag &REF_ISPACKED ) || flag &REF_ISSYMREF ) { -/* loose */ -int err, i =strlen (lock->lk->filename) -5 ;/* .lock */ - - lock->lk->filename[i] =0 ; - err =unlink_or_warn (lock->lk->filename); - lock->lk->filename[i] ='.' ; -if (err && errno !=ENOENT ) -return 1 ; - } + lock->delete_ref =1 ; + lock->delete_flag = flag; +return 0 ; }
@@ -2515,7 +2508,7 @@ int delete_ref(const char *refname, const unsigned char *sha1, int delopt)unlink_or_warn (git_path ("logs/%s" , lock->ref_name));clear_loose_ref_cache (&ref_cache); -unlock_ref (lock); + ret |=commit_ref_lock (lock);return ret; }
@@ -2868,6 +2861,20 @@ int write_ref_sha1(struct ref_lock *lock,int commit_ref_lock (struct ref_lock *lock) { +if (lock->delete_ref) { +int flag = lock->delete_flag; + +if (!(flag &REF_ISPACKED ) || flag &REF_ISSYMREF ) { +/* loose */ +int err, i =strlen (lock->lk->filename) -5 ;/* .lock */ + + lock->lk->filename[i] =0 ; + err =unlink_or_warn (lock->lk->filename); + lock->lk->filename[i] ='.' ; +if (err && errno !=ENOENT ) +return 1 ; + } + }else if (!lock->skipped_write &&commit_ref (lock)) {error ("Couldn't set %s" , lock->ref_name);unlock_ref (lock);
@@ -3487,6 +3494,8 @@ int ref_transaction_commit(struct ref_transaction *transaction,if (update->lock) { delnames[delnum++] = update->refname; ret |=delete_ref_loose (update->lock, update->type); + ret |=commit_ref_lock (update->lock); + update->lock =NULL ; } }
refs.h +2 -0
@@ -9,6 +9,8 @@ struct ref_lock {int lock_fd;int force_write;int skipped_write; +int delete_ref; +int delete_flag; };struct ref_transaction ;
Signature · 1 line
1.9.1.505.g4f1e74f
Re: [PATCH v3 2/3] refs.c: split delete_ref_loose() into a separate flag-for-deletion and commit phase
Quoted 7 lines · Ronnie Sahlberg
int
commit_ref_lock(struct ref_lock *lock)
{refs.c +5
-0+if (lock->delete_ref) { +int flag = lock->delete_flag; + +if (!(flag &REF_ISPACKED ) || flag &REF_ISSYMREF ) { +/* loose */
refs.c int write_ref_sha1(struct ref_lock *lock, · line 2878
Quoted 10 lines
refs.c +9
-0+int err, i =strlen (lock->lk->filename) -5 ;/* .lock */ + + lock->lk->filename[i] =0 ; + err =unlink_or_warn (lock->lk->filename); + lock->lk->filename[i] ='.' ; +if (err && errno !=ENOENT ) +return 1 ; + } + }else if (!lock->skipped_write &&commit_ref (lock)) {
} else if (...) {Also the previous patch indents the above "if" line in the context with spaces; please use a tab.
PATCH v3 3/3 refs.c: change ref_transaction_commit to run the commit loops once all work is finished
In reply to Ronnie SahlbergDuring a transaction commit we will both update and delete refs. Since both update and delete now use the same pattern
lock = lock_ref_sha1_basic() (or varient of)
write_ref_sha1(lock)/delete_ref_loose(lock)
unlock_ref(lock) | commit_ref_lock(lock)
we can now simplify ref_transaction_commit to have one loop that locks all involved refs.
A second loop that writes or flags for deletion, but does not commit, all the refs.
And a final third loop that commits all the refs once all the work and preparations are complete.
This makes updating/deleting multiple refs more atomic since we will not start the commit phase until all the preparations have completed successfully.
--- refs.c | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-)
refs.c +22 -17
@@ -3468,42 +3468,47 @@ int ref_transaction_commit(struct ref_transaction *transaction, } } -/* Perform updates first so live commits remain referenced */ +/* Prepare all the updates/deletes */ for (i =0 ; i < n; i++) {struct ref_update *update = updates[i]; -if (!is_null_sha1 (update->new_sha1)) { +if (!is_null_sha1 (update->new_sha1)) ret =update_ref_write (msg, update->refname, update->new_sha1, update->lock, onerr); -if (ret) -unlock_ref (update->lock); -else -commit_ref_lock (update->lock); - update->lock =NULL ; -if (ret) - goto cleanup; +else { + delnames[delnum++] = update->refname; + ret =delete_ref_loose (update->lock, update->type); } +if (ret) + goto cleanup; } -/* Perform deletes now that updates are safely completed */ + ret |=repack_without_refs (delnames, delnum); +for (i =0 ; i < delnum; i++) +unlink_or_warn (git_path ("logs/%s" , delnames[i])); +clear_loose_ref_cache (&ref_cache); + +/* Perform updates first so live commits remain referenced */ +for (i =0 ; i < n; i++) { +struct ref_update *update = updates[i]; + +if (update->lock && !update->lock->delete_ref) { + ret |=commit_ref_lock (update->lock); + update->lock =NULL ; + } + } +/* And finally perform all deletes */ for (i =0 ; i < n; i++) {struct ref_update *update = updates[i];if (update->lock) { - delnames[delnum++] = update->refname; - ret |=delete_ref_loose (update->lock, update->type); ret |=commit_ref_lock (update->lock); update->lock =NULL ; } } - ret |=repack_without_refs (delnames, delnum); -for (i =0 ; i < delnum; i++) -unlink_or_warn (git_path ("logs/%s" , delnames[i])); -clear_loose_ref_cache (&ref_cache); - cleanup:for (i =0 ; i < n; i++)if (updates[i]->lock)
Signature · 1 line
1.9.1.505.g4f1e74f
Quoted 21 lines · Ronnie Sahlberg
refs.c:ref_transaction_commit() intermingles doing updates and checks with actually applying changes to the refs in loops that abort on error. This is done one ref at a time and means that if an error is detected that will fail the operation partway through the list of refs to update we will end up with some changes applied to disk and others not.
Without having transaction support from the filesystem, it is hard to make an update that involves multiple refs to guarantee atomicity, but we can do a somewhat better than we currently do.
These patches change the update and delete functions to use a three call pattern of
1, lock
2, update, or flag for deletion
3, apply on disk (rename()orunlink())When a transaction is commited we first do all the locking, preparations and most of the error checking before we actually start applying any changes to the filesystem store.
Quoted 6 lines
This means that more of the error cases that will fail the commit will trigger before we start doing any changes to the actual files.
This should make the changes of refs in
refs_transaction_commitslightly more atomic.
Hmph. At least 5801 9350 and 9300 seem to fail with these three queued on top of mh/ref-transaction series.