From: Ronnie Sahlberg Subject: [PATCH v10 27/44] walker.c: use ref transaction for ref updates Date: Fri, 16 May 2014 10:37:15 -0700 Message-ID: <1400261852-31303-28-git-send-email-sahlberg@google.com> References: <1400261852-31303-1-git-send-email-sahlberg@google.com> Cc: mhagger@alum.mit.edu, Ronnie Sahlberg To: git@vger.kernel.org X-From: git-owner@vger.kernel.org Fri May 16 19:38:57 2014 Return-path: Envelope-to: gcvg-git-2@plane.gmane.org Received: from vger.kernel.org ([209.132.180.67]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1WlM5w-0000p5-Fi for gcvg-git-2@plane.gmane.org; Fri, 16 May 2014 19:38:56 +0200 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757723AbaEPRiv (ORCPT ); Fri, 16 May 2014 13:38:51 -0400 Received: from mail-oa0-f73.google.com ([209.85.219.73]:49841 "EHLO mail-oa0-f73.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757970AbaEPRhj (ORCPT ); Fri, 16 May 2014 13:37:39 -0400 Received: by mail-oa0-f73.google.com with SMTP id i4so587875oah.2 for ; Fri, 16 May 2014 10:37:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=YCzCqQAooYhIJGaeb/MYiZZGb63JyYvJozaowgLTwWc=; b=Gyg32DFDN1dNFdLnqzc3eZztvHKdluLP5Z6P+JmbAdg2XaTEt/cpvqiZSPQ1eV7rMq ZtKYQIHMP61R5wOj1QdjD6AA5U/bpBSp6U25ENDO7lqbj489AZCnPU4GBA6azH9PQJJj kJnj/szNHzBDVoKn0AFwb9xWyKScKYKOZw4yTl9DZyPCXwYniHs3LdGZmudB3zMq/RRk 7+XBulhfarlD39yUI1/OtdHTRBJPPxODVISpjcABU1HHh8JpcGsk/0Z6L4Mfl31PHW0l nv8S/HYRAPPH0JrMZlFTe6oOm5WxndOkpURZUEe4aFH+ieRnhv3UVSKAHcPtd9pyEP79 G2LA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=YCzCqQAooYhIJGaeb/MYiZZGb63JyYvJozaowgLTwWc=; b=ZAXQCRJpangKEe1uFgLioeCMkQlDJG3cy2YbfIqqrGzpxL79igE/CQ/Si5gjkqdbyM QwYwKB5/IF2b4gImi2lJDeNpD4T4ygId42dpG1j3slf6YRzqmCq2w9cGP6ZmZrhBWWiF 0CV3BeRjJHZFrsz4J2auILJvIO7Ab35jK5wED//zucpES7Zw+/eNZa2Z3uda1Ty62W5D JOZJBKmzh9MUN2BdI8vDWD+STBE3iifNZ8dKf/ecajEMcn8if67XqewA1olCKtHjDO9+ K9ZWXT/NKXQ5ULbeR6NtMfKuLl4j1zff5ErgKfI+CLxVVDJ2pCv6YkJNE0w8Xhvd+qxv 1vRQ== X-Gm-Message-State: ALoCoQlunymVgZ/86YnKiNIuV/uGITWWU0F6fyy9G3AHNKQy5LDKTcV4ZaAvFxFUH+2XBc9GlJyJ X-Received: by 10.182.104.74 with SMTP id gc10mr8965724obb.40.1400261858133; Fri, 16 May 2014 10:37:38 -0700 (PDT) Received: from corp2gmr1-1.hot.corp.google.com (corp2gmr1-1.hot.corp.google.com [172.24.189.92]) by gmr-mx.google.com with ESMTPS id h18si443263yhj.0.2014.05.16.10.37.38 for (version=TLSv1.1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 16 May 2014 10:37:38 -0700 (PDT) Received: from sahlberg1.mtv.corp.google.com (sahlberg1.mtv.corp.google.com [172.27.69.52]) by corp2gmr1-1.hot.corp.google.com (Postfix) with ESMTP id D278D31C273; Fri, 16 May 2014 10:37:37 -0700 (PDT) Received: by sahlberg1.mtv.corp.google.com (Postfix, from userid 177442) id 9A30DE115F; Fri, 16 May 2014 10:37:37 -0700 (PDT) X-Mailer: git-send-email 2.0.0.rc3.510.g20c254b In-Reply-To: <1400261852-31303-1-git-send-email-sahlberg@google.com> Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Archived-At: Switch to using ref transactions in walker_fetch(). As part of the refactoring to use ref transactions we also fix a potential memory leak where in the original code if write_ref_sha1() would fail we would end up returning from the function without free()ing the msg string. This changes the locking slightly for walker_fetch. Previously the code would lock all refs before writing them but now we do not lock the refs until the commit stage. There is thus a very short window where changes could be done locally during the fetch which would be overwritten when the fetch completes and commits its transaction. But this window should be reasonably short. Even if this race does trigger, since both the old code and the new code just overwrites the refs to the new values without checking or comparing them with the previous value, this is not too dissimilar to a similar scenario where you first do a ref change locally and then later do a fetch that overwrites the local change. With this in mind I do not see the change in locking semantics to be critical. Note that this function is only called when fetching from a remote HTTP repository onto the local (most of the time single-user) repository which likely means that the type of collissions that the previous locking would protect against and cause the fetch to fail for to be even more rare. Signed-off-by: Ronnie Sahlberg --- walker.c | 51 ++++++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/walker.c b/walker.c index 1dd86b8..cbe5686 100644 --- a/walker.c +++ b/walker.c @@ -251,24 +251,18 @@ void walker_targets_free(int targets, char **target, const char **write_ref) int walker_fetch(struct walker *walker, int targets, char **target, const char **write_ref, const char *write_ref_log_details) { - struct ref_lock **lock = xcalloc(targets, sizeof(struct ref_lock *)); + char ref_name[PATH_MAX]; + struct strbuf err = STRBUF_INIT; + struct ref_transaction *transaction; unsigned char *sha1 = xmalloc(targets * 20); - char *msg; - int ret; + char *msg = NULL; int i; save_commit_buffer = 0; - for (i = 0; i < targets; i++) { - if (!write_ref || !write_ref[i]) - continue; - - lock[i] = lock_ref_sha1(write_ref[i], NULL); - if (!lock[i]) { - error("Can't lock ref %s", write_ref[i]); - goto unlock_and_fail; - } - } + transaction = ref_transaction_begin(); + if (!transaction) + return -1; if (!walker->get_recover) for_each_ref(mark_complete, NULL); @@ -276,14 +270,14 @@ int walker_fetch(struct walker *walker, int targets, char **target, for (i = 0; i < targets; i++) { if (interpret_target(walker, target[i], &sha1[20 * i])) { error("Could not interpret response from server '%s' as something to pull", target[i]); - goto unlock_and_fail; + goto rollback_and_fail; } if (process(walker, lookup_unknown_object(&sha1[20 * i]))) - goto unlock_and_fail; + goto rollback_and_fail; } if (loop(walker)) - goto unlock_and_fail; + goto rollback_and_fail; if (write_ref_log_details) { msg = xmalloc(strlen(write_ref_log_details) + 12); @@ -294,19 +288,26 @@ int walker_fetch(struct walker *walker, int targets, char **target, for (i = 0; i < targets; i++) { if (!write_ref || !write_ref[i]) continue; - ret = write_ref_sha1(lock[i], &sha1[20 * i], msg ? msg : "fetch (unknown)"); - lock[i] = NULL; - if (ret) - goto unlock_and_fail; + sprintf(ref_name, "refs/%s", write_ref[i]); + if (ref_transaction_update(transaction, ref_name, + &sha1[20 * i], NULL, + 0, 0, &err)) + goto rollback_and_fail; + } + + if (ref_transaction_commit(transaction, msg ? msg : "fetch (unknown)", + &err)) { + error("%s", err.buf); + goto rollback_and_fail; } - free(msg); + free(msg); return 0; -unlock_and_fail: - for (i = 0; i < targets; i++) - if (lock[i]) - unlock_ref(lock[i]); +rollback_and_fail: + free(msg); + strbuf_release(&err); + ref_transaction_free(transaction); return -1; } -- 2.0.0.rc3.510.g20c254b