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

[PATCH 10/15] refs.c: allow multiple reflog updates during a single transaction

From
Ronnie Sahlberg <sahlberg@google.com>
Date
Oct 21, 2014, 19:24 UTC
Message-ID
<1413919462-3458-11-git-send-email-sahlberg@google.com>
In-Reply-To
<1413919462-3458-1-git-send-email-sahlberg@google.com>
commit 5fd1fa7d8520cf03e94fa7d0e9aa8685de0ef63f upstream.

Allow to make multiple reflog updates to the same ref during a transaction. This means we only need to lock the reflog once, during the first update that touches the reflog, and that all further updates can just write the reflog entry since the reflog is already locked.

This allows us to write code such as:
t = transaction_begin()
transaction_reflog_update(t, "foo", REFLOG_TRUNCATE, NULL);
loop-over-something...
   transaction_reflog_update(t, "foo", 0, <message>);
transaction_commit(t)

where we first truncate the reflog and then build the new content one line at a time.

While this technically looks like O(n2) behavior it not that bad. We only do this loop for transactions that cover a single ref during reflog expire. This means that the linear search inside transaction_update_reflog() will find the match on the very first entry thus making it O(1) and not O(n) or our usecases. Thus the whole expire becomes O(n) instead of O(n2). If in the future we start doing this for many refs in one single transaction we might want to optimize this. But there is no need to complexify the code and optimize for future usecases that might never materialize at this stage.

Change-Id: Ibe703310e33a4db07e5bcc704310211268b788fc
Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
 refs.c | 46 +++++++++++++++++++++++++++++++++++++---------
 1 file changed, 37 insertions(+), 9 deletions(-)
diff --git a/refs.c b/refs.c
index f14b76e..f7e947f 100644
--- a/refs.c
+++ b/refs.c
@@ -31,6 +31,12 @@ static unsigned char refname_disposition[256] = {
  */
 #define REF_ISPRUNING	0x0100
 /*
+ * Only the first reflog update needs to lock the reflog file. Further updates
+ * just use the lock taken by the first update.
+ */
+#define UPDATE_REFLOG_NOLOCK 0x0200
+
+/*
  * Try to read one refname component from the front of refname.
  * Return the length of the component found, or -1 if the component is
  * not legal.  It is legal if it is something reasonable to have under
@@ -3521,7 +3527,7 @@ enum transaction_update_type {
 	UPDATE_LOG = 1
 };
 
-/**
+/*
  * Information needed for a single ref update.  Set new_sha1 to the
  * new value or to zero to delete the ref.  To check the old value
  * while locking the ref, set have_old to 1 and set old_sha1 to the
@@ -3531,7 +3537,7 @@ struct ref_update {
 	enum transaction_update_type update_type;
 	unsigned char new_sha1[20];
 	unsigned char old_sha1[20];
-	int flags; /* REF_NODEREF? */
+	int flags; /* REF_NODEREF? or private flags */
 	int have_old; /* 1 if old_sha1 is valid, 0 otherwise */
 	struct ref_lock *lock;
 	int type;
@@ -3539,8 +3545,9 @@ struct ref_update {
 
 	/* used by reflog updates */
 	int reflog_fd;
-	struct lock_file reflog_lock;
+	struct lock_file *reflog_lock;
 	char *committer;
+	struct ref_update *orig_update; /* For UPDATE_REFLOG_NOLOCK */
 
 	const char refname[FLEX_ARRAY];
 };
@@ -3619,11 +3626,26 @@ int transaction_update_reflog(struct transaction *transaction,
 			      struct strbuf *err)
 {
 	struct ref_update *update;
+	int i;
 
 	if (transaction->state != TRANSACTION_OPEN)
 		die("BUG: update_reflog called for transaction that is not open");
 
 	update = add_update(transaction, refname, UPDATE_LOG);
+	update->flags = flags;
+	for (i = 0; i < transaction->nr - 1; i++) {
+		if (transaction->updates[i]->update_type != UPDATE_LOG)
+			continue;
+		if (!strcmp(transaction->updates[i]->refname,
+			    update->refname)) {
+			update->flags |= UPDATE_REFLOG_NOLOCK;
+			update->orig_update = transaction->updates[i];
+			break;
+		}
+	}
+	if (!(update->flags & UPDATE_REFLOG_NOLOCK))
+		update->reflog_lock = xcalloc(1, sizeof(struct lock_file));
+
 	hashcpy(update->new_sha1, new_sha1);
 	hashcpy(update->old_sha1, old_sha1);
 	update->reflog_fd = -1;
@@ -3639,7 +3661,6 @@ int transaction_update_reflog(struct transaction *transaction,
 	}
 	if (msg)
 		update->msg = xstrdup(msg);
-	update->flags = flags;
 
 	return 0;
 }
@@ -3822,10 +3843,15 @@ int transaction_commit(struct transaction *transaction,
 
 		if (update->update_type != UPDATE_LOG)
 			continue;
+		if (update->flags & UPDATE_REFLOG_NOLOCK) {
+			update->reflog_fd = update->orig_update->reflog_fd;
+			update->reflog_lock = update->orig_update->reflog_lock;
+			continue;
+		}
 		update->reflog_fd = hold_lock_file_for_append(
-					&update->reflog_lock,
+					update->reflog_lock,
 					git_path("logs/%s", update->refname),
-					0);
+					LOCK_NO_DEREF);
 		if (update->reflog_fd < 0) {
 			const char *str = "Cannot lock reflog for '%s'. %s";
 
@@ -3891,7 +3917,7 @@ int transaction_commit(struct transaction *transaction,
 				ftruncate(update->reflog_fd, 0)) {
 				error("Could not truncate reflog: %s. %s",
 				      update->refname, strerror(errno));
-				rollback_lock_file(&update->reflog_lock);
+				rollback_lock_file(update->reflog_lock);
 				update->reflog_fd = -1;
 				continue;
 			}
@@ -3901,7 +3927,7 @@ int transaction_commit(struct transaction *transaction,
 				     update->committer, update->msg)) {
 			error("Could write to reflog: %s. %s",
 			      update->refname, strerror(errno));
-			rollback_lock_file(&update->reflog_lock);
+			rollback_lock_file(update->reflog_lock);
 			update->reflog_fd = -1;
 		}
 	}
@@ -3912,9 +3938,11 @@ int transaction_commit(struct transaction *transaction,
 
 		if (update->update_type != UPDATE_LOG)
 			continue;
+		if (update->flags & UPDATE_REFLOG_NOLOCK)
+			continue;
 		if (update->reflog_fd == -1)
 			continue;
-		if (commit_lock_file(&update->reflog_lock)) {
+		if (commit_lock_file(update->reflog_lock)) {
 			error("Could not commit reflog: %s. %s",
 			      update->refname, strerror(errno));
 			update->reflog_fd = -1;
-- 
2.1.0.rc2.206.gedb03e5
Previous: Ronnie SahlbergNext: Junio C Hamano
Message 21 of 28 in “ref-transactions-reflog”
  1. 00/15 ref-transactions-reflogRonnie Sahlberg, Oct 21, 2014
  2. 01/15 refs.c make ref_transaction_create a wrapper to ref_transaction_updateRonnie Sahlberg, Oct 21, 2014
  3. Junio C HamanoOct 23, 2014
  4. Ronnie SahlbergOct 23, 2014
  5. Junio C HamanoOct 23, 2014
  6. Junio C HamanoOct 28, 2014
  7. 02/15 refs.c: make ref_transaction_delete a wrapper for ref_transaction_updateRonnie Sahlberg, Oct 21, 2014
  8. Junio C HamanoOct 23, 2014
  9. 03/15 refs.c: rename the transaction functionsRonnie Sahlberg, Oct 21, 2014
  10. 04/15 refs.c: add a new update_type field to ref_updateRonnie Sahlberg, Oct 21, 2014
  11. Junio C HamanoOct 23, 2014
  12. 05/15 refs.c: add a function to append a reflog entry to a fdRonnie Sahlberg, Oct 21, 2014
  13. 06/15 copy.c: make copy_fd preserve meaningful errnoRonnie Sahlberg, Oct 21, 2014
  14. Junio C HamanoOct 23, 2014
  15. Ronnie SahlbergOct 23, 2014
  16. 07/15 refs.c: add a transaction function to append a reflog entryRonnie Sahlberg, Oct 21, 2014
  17. 08/15 refs.c: add a flag to allow reflog updates to truncate the logRonnie Sahlberg, Oct 21, 2014
  18. 09/15 refs.c: only write reflog update if msg is non-NULLRonnie Sahlberg, Oct 21, 2014
  19. Junio C HamanoOct 23, 2014
  20. Ronnie SahlbergOct 28, 2014
  21. 10/15 refs.c: allow multiple reflog updates during a single transactionRonnie Sahlberg, Oct 21, 2014
  22. Junio C HamanoOct 23, 2014
  23. Ronnie SahlbergOct 28, 2014
  24. 11/15 reflog.c: use a reflog transaction when writing during expireRonnie Sahlberg, Oct 21, 2014
  25. 12/15 refs.c: rename log_ref_setup to create_reflogRonnie Sahlberg, Oct 21, 2014
  26. 13/15 refs.c: make unlock_ref/close_ref/commit_ref staticRonnie Sahlberg, Oct 21, 2014
  27. 14/15 refs.c: remove lock_any_ref_for_updateRonnie Sahlberg, Oct 21, 2014
  28. 15/15 refs.c: allow deleting refs with a broken sha1Ronnie Sahlberg, Oct 21, 2014

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.