threads / patch / 51796

patchcache-tree: do not lazy-fetch merge tree

Subject: [PATCH] cache-tree: do not lazy-fetch merge tree

## tl;dr

12 messages between Sep 3, 2019 and Sep 10, 2019. Diffs are folded; open one to read it.

replies: 11people: 5as markdown or json

Jonathan Tan· Sep 3, 2019, 19:42 UTC · lore

When cherry-picking (for example), new trees may be constructed. During this process, Git checks whether these trees exist. However, in a partial clone, this causes a lazy fetch to occur, which is both unnecessary (because Git has already constructed this tree as part of the cherry-picking process) and likely to fail (because the remote probably doesn't have this tree).

Do not lazy fetch in this situation.
Signed-off-by: Jonathan Tan <jonathantanmy@google.com>
---
Another partial clone bug.
This raises the issue that failed fetches are currently fatal - if they
weren't fatal, this cherry-pick would have worked (except with some
delay as the fetch is attempted, and with a warning message about the
fetch failing). My personal inclination right now is to leave things as
it is (fatal failed fetches), but I'm open to other opinions.
---
 cache-tree.c             |  2 +-
 t/t0410-partial-clone.sh | 14 ++++++++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)
Show changes to 2 files +15 −1

cache-tree.c, t/t0410-partial-clone.sh

diff --git a/cache-tree.c b/cache-tree.c
index c22161f987..9e596893bc 100644
--- a/cache-tree.c
+++ b/cache-tree.c
@@ -407,7 +407,7 @@ static int update_one(struct cache_tree *it,
 	if (repair) {
 		struct object_id oid;
 		hash_object_file(buffer.buf, buffer.len, tree_type, &oid);
-		if (has_object_file(&oid))
+		if (has_object_file_with_flags(&oid, OBJECT_INFO_SKIP_FETCH_OBJECT))
 			oidcpy(&it->oid, &oid);
 		else
 			to_invalidate = 1;
diff --git a/t/t0410-partial-clone.sh b/t/t0410-partial-clone.sh
index 6415063980..3e434b6a81 100755
--- a/t/t0410-partial-clone.sh
+++ b/t/t0410-partial-clone.sh
@@ -492,6 +492,20 @@ test_expect_success 'gc stops traversal when a missing but promised object is re
 	! grep "$TREE_HASH" out
 '
 
+test_expect_success 'do not fetch when checking existence of tree we construct ourselves' '
+	rm -rf repo &&
+	test_create_repo repo &&
+	test_commit -C repo base &&
+	test_commit -C repo side1 &&
+	git -C repo checkout base &&
+	test_commit -C repo side2 &&
+
+	git -C repo config core.repositoryformatversion 1 &&
+	git -C repo config extensions.partialclone "arbitrary string" &&
+
+	git -C repo cherry-pick side1
+'
+
 . "$TEST_DIRECTORY"/lib-httpd.sh
 start_httpd
 
-- 
2.23.0.187.g17f5b7556c-goog
Derrick Stolee· Sep 4, 2019, 01:37 UTC · re: Jonathan Tan · lore

Re: [PATCH] cache-tree: do not lazy-fetch merge tree

On 9/3/2019 3:42 PM, Jonathan Tan wrote:
Show 6 quoted lines
> When cherry-picking (for example), new trees may be constructed. During
> this process, Git checks whether these trees exist. However, in a
> partial clone, this causes a lazy fetch to occur, which is both
> unnecessary (because Git has already constructed this tree as part of
> the cherry-picking process) and likely to fail (because the remote
> probably doesn't have this tree).

If we have constructed the object already, then why do we not see it and avoid fetching it? This must be a slightly strange timing issue with objects being flushed to disk or added to the object cache.

One approach is to find all of these has_object_file() calls that should really be one with OBJECT_INFO_SKIP_FETCH_OBJECT. Another would be to find out why has_object_file() isn't seeing the object we constructed.

> Do not lazy fetch in this situation.
I agree that the patch has this effect.
 
Show 49 quoted lines
> Signed-off-by: Jonathan Tan <jonathantanmy@google.com>
> ---
> Another partial clone bug.
> 
> This raises the issue that failed fetches are currently fatal - if they
> weren't fatal, this cherry-pick would have worked (except with some
> delay as the fetch is attempted, and with a warning message about the
> fetch failing). My personal inclination right now is to leave things as
> it is (fatal failed fetches), but I'm open to other opinions.
> ---
>  cache-tree.c             |  2 +-
>  t/t0410-partial-clone.sh | 14 ++++++++++++++
>  2 files changed, 15 insertions(+), 1 deletion(-)
> 
> diff --git a/cache-tree.c b/cache-tree.c
> index c22161f987..9e596893bc 100644
> --- a/cache-tree.c
> +++ b/cache-tree.c
> @@ -407,7 +407,7 @@ static int update_one(struct cache_tree *it,
>  	if (repair) {
>  		struct object_id oid;
>  		hash_object_file(buffer.buf, buffer.len, tree_type, &oid);
> -		if (has_object_file(&oid))
> +		if (has_object_file_with_flags(&oid, OBJECT_INFO_SKIP_FETCH_OBJECT))
>  			oidcpy(&it->oid, &oid);
>  		else
>  			to_invalidate = 1;
> diff --git a/t/t0410-partial-clone.sh b/t/t0410-partial-clone.sh
> index 6415063980..3e434b6a81 100755
> --- a/t/t0410-partial-clone.sh
> +++ b/t/t0410-partial-clone.sh
> @@ -492,6 +492,20 @@ test_expect_success 'gc stops traversal when a missing but promised object is re
>  	! grep "$TREE_HASH" out
>  '
>  
> +test_expect_success 'do not fetch when checking existence of tree we construct ourselves' '
> +	rm -rf repo &&
> +	test_create_repo repo &&
> +	test_commit -C repo base &&
> +	test_commit -C repo side1 &&
> +	git -C repo checkout base &&
> +	test_commit -C repo side2 &&
> +
> +	git -C repo config core.repositoryformatversion 1 &&
> +	git -C repo config extensions.partialclone "arbitrary string" &&
> +
> +	git -C repo cherry-pick side1
> +'
> +
I appreciate this test!

Thanks, -Stolee

Jonathan Tan· Sep 4, 2019, 22:35 UTC · re: Derrick Stolee · lore

Re: [PATCH] cache-tree: do not lazy-fetch merge tree

Show 11 quoted lines
> On 9/3/2019 3:42 PM, Jonathan Tan wrote:
> > When cherry-picking (for example), new trees may be constructed. During
> > this process, Git checks whether these trees exist. However, in a
> > partial clone, this causes a lazy fetch to occur, which is both
> > unnecessary (because Git has already constructed this tree as part of
> > the cherry-picking process) and likely to fail (because the remote
> > probably doesn't have this tree).
> 
> If we have constructed the object already, then why do we not see it
> and avoid fetching it? This must be a slightly strange timing issue
> with objects being flushed to disk or added to the object cache.

Thanks for taking a look at this! The answer is that I wasn't precise when I said "already constructed" - I meant that it is in a struct strbuf. It hasn't been written to disk yet, so has_object_file() does not see it.

> One approach is to find all of these has_object_file() calls that should
> really be one with OBJECT_INFO_SKIP_FETCH_OBJECT. Another would be to
> find out why has_object_file() isn't seeing the object we constructed.

By the former, do you mean that we should look at the other has_object_file() calls? I looked at the others in cache-tree.c and I think the one in this patch is the only one that is called on an OID generated from hash_object_file(). (And I answered the latter in the above paragraph.)

To avoid confusion, maybe this commit message is better:

When cherry-picking (for example), new trees may be constructed. During this process, Git constructs the new tree in a struct strbuf, computes the OID of the new tree, and checks if the new OID already exists on disk. However, in a partial clone, the disk check causes a lazy fetch to occur, which is both unnecessary (because we have the tree in the struct strbuf) and likely to fail (because the remote probably doesn't have this tree).

Junio C Hamano· Sep 4, 2019, 23:35 UTC · re: Jonathan Tan · lore

Re: [PATCH] cache-tree: do not lazy-fetch merge tree

Jonathan Tan <jonathantanmy@google.com> writes:
Show 7 quoted lines
> When cherry-picking (for example), new trees may be constructed. During
> this process, Git constructs the new tree in a struct strbuf, computes
> the OID of the new tree, and checks if the new OID already exists on
> disk. However, in a partial clone, the disk check causes a lazy fetch to
> occur, which is both unnecessary (because we have the tree in the struct
> strbuf) and likely to fail (because the remote probably doesn't have
> this tree).

FWIW, this logic dates back to aecf567c ("cache-tree: create/update cache-tree on checkout", 2014-07-05), when "checkout" learned to perform opportunistic revalidation of cache-tree data structure, without writing into the object store. If we were lazily checked out, and created a blob locally that happens to match the original we did not fetch in a directory this piece of code is hashing, the resulting hash _may_ name a tree that the other side has that we did not fetch, so taking the "to_invalidate = 1" side would make the resulting cache-tree less optimal, but because the design choice being made here is to take that hit in order to avoid network cost, as long as that is documented properly (iow, "probably doesn't have" is not an issue; even if they have it, you do not want to fetch and make the cache-tree entry valid), it is OK.

Jonathan Tan· Sep 9, 2019, 19:01 UTC · re: Jonathan Tan · lore

[PATCH v2] cache-tree: do not lazy-fetch merge tree

When cherry-picking (for example), new trees may be constructed. During this process, Git constructs the new tree in a struct strbuf, computes the OID of the new tree, and checks if the new OID already exists on disk. However, in a partial clone, the disk check causes a lazy fetch to occur, which is both unnecessary (because we have the tree in the struct strbuf) and likely to fail (because the remote probably doesn't have this tree).

Do not lazy fetch in this situation.
Signed-off-by: Jonathan Tan <jonathantanmy@google.com>
---
As requested in What's Cooking [1], here's a patch with an updated
commit message. Otherwise, the patch is exactly the same.
[1] https://public-inbox.org/git/xmqqd0gcm2zm.fsf@gitster-ct.c.googlers.com/
---
 cache-tree.c             |  2 +-
 t/t0410-partial-clone.sh | 14 ++++++++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)
Show changes to 2 files +15 −1

cache-tree.c, t/t0410-partial-clone.sh

diff --git a/cache-tree.c b/cache-tree.c
index c22161f987..9e596893bc 100644
--- a/cache-tree.c
+++ b/cache-tree.c
@@ -407,7 +407,7 @@ static int update_one(struct cache_tree *it,
 	if (repair) {
 		struct object_id oid;
 		hash_object_file(buffer.buf, buffer.len, tree_type, &oid);
-		if (has_object_file(&oid))
+		if (has_object_file_with_flags(&oid, OBJECT_INFO_SKIP_FETCH_OBJECT))
 			oidcpy(&it->oid, &oid);
 		else
 			to_invalidate = 1;
diff --git a/t/t0410-partial-clone.sh b/t/t0410-partial-clone.sh
index 6415063980..3e434b6a81 100755
--- a/t/t0410-partial-clone.sh
+++ b/t/t0410-partial-clone.sh
@@ -492,6 +492,20 @@ test_expect_success 'gc stops traversal when a missing but promised object is re
 	! grep "$TREE_HASH" out
 '
 
+test_expect_success 'do not fetch when checking existence of tree we construct ourselves' '
+	rm -rf repo &&
+	test_create_repo repo &&
+	test_commit -C repo base &&
+	test_commit -C repo side1 &&
+	git -C repo checkout base &&
+	test_commit -C repo side2 &&
+
+	git -C repo config core.repositoryformatversion 1 &&
+	git -C repo config extensions.partialclone "arbitrary string" &&
+
+	git -C repo cherry-pick side1
+'
+
 . "$TEST_DIRECTORY"/lib-httpd.sh
 start_httpd
 
-- 
2.23.0.162.g0b9fbb3734-goog
Junio C Hamano· Sep 9, 2019, 19:55 UTC · re: Jonathan Tan · lore

Re: [PATCH v2] cache-tree: do not lazy-fetch merge tree

Jonathan Tan <jonathantanmy@google.com> writes:
Show 7 quoted lines
> When cherry-picking (for example), new trees may be constructed. During
> this process, Git constructs the new tree in a struct strbuf, computes
> the OID of the new tree, and checks if the new OID already exists on
> disk. However, in a partial clone, the disk check causes a lazy fetch to
> occur, which is both unnecessary (because we have the tree in the struct
> strbuf) and likely to fail (because the remote probably doesn't have
> this tree).

I somehow smell that the above misses the point of the check in the first place, though. The reason why we are computing the tree object's name and seeing if we have it locally on disk is to decide if we want to record it in the cache tree, *without* writing the tree out to our object store, no?

It is not just unnecessary but also against the goal of the codepath to lazily download it, even if the tree is available remotely. And it is irrelevant that there are cases the remote does not have it---we have no need to mention that we must be prepared to see the lazy fetch to fail. Even when they do have one, we do not want to fetch it and write to our object store.

Isn't that what is going on? I thought I dug up the original that introduced the has_object_file() call to this codepath to make sure we understand why we make the check (and I expected the person who is proposing this change to do the same and record the finding in the proposed log message).

I am running out of time today, and will revisit later this week (I'll be down for at least two days starting tomorrow, by the way).

Thanks.
Junio C Hamano· Sep 9, 2019, 21:05 UTC · re: Junio C Hamano · lore

Re: [PATCH v2] cache-tree: do not lazy-fetch merge tree

Junio C Hamano <gitster@pobox.com> writes:
Show 8 quoted lines
> Isn't that what is going on?  I thought I dug up the original that
> introduced the has_object_file() call to this codepath to make sure
> we understand why we make the check (and I expected the person who
> is proposing this change to do the same and record the finding in
> the proposed log message).
>
> I am running out of time today, and will revisit later this week
> (I'll be down for at least two days starting tomorrow, by the way).
Here is what I came up with.
    The cache-tree datastructure is used to speed up the comparison
    between the HEAD and the index, and when the index is updated by
    a cherry-pick (for example), a tree object that would represent
    the paths in the index in a directory is constructed in-core, to
    see if such a tree object exists already in the object store.
    When the lazy-fetch mechanism was introduced, we converted this
    "does the tree exist?" check into an "if it does not, and if we
    lazily cloned, see if the remote has it" call by mistake.  Since
    the whole point of this check is to repair the cache-tree by
    recording an already existing tree object opportunistically, we
    shouldn't even try to fetch one from the remote.
    Pass the OBJECT_INFO_SKIP_FETCH_OBJECT flag to make sure we only
    check for existence in the local object store without triggering the
    lazy fetch mechanism.
Jeff King· Sep 9, 2019, 22:21 UTC · re: Junio C Hamano · lore

Re: [PATCH v2] cache-tree: do not lazy-fetch merge tree

On Mon, Sep 09, 2019 at 02:05:53PM -0700, Junio C Hamano wrote:
Show 29 quoted lines
> Junio C Hamano <gitster@pobox.com> writes:
> 
> > Isn't that what is going on?  I thought I dug up the original that
> > introduced the has_object_file() call to this codepath to make sure
> > we understand why we make the check (and I expected the person who
> > is proposing this change to do the same and record the finding in
> > the proposed log message).
> >
> > I am running out of time today, and will revisit later this week
> > (I'll be down for at least two days starting tomorrow, by the way).
> 
> Here is what I came up with.
> 
>     The cache-tree datastructure is used to speed up the comparison
>     between the HEAD and the index, and when the index is updated by
>     a cherry-pick (for example), a tree object that would represent
>     the paths in the index in a directory is constructed in-core, to
>     see if such a tree object exists already in the object store.
> 
>     When the lazy-fetch mechanism was introduced, we converted this
>     "does the tree exist?" check into an "if it does not, and if we
>     lazily cloned, see if the remote has it" call by mistake.  Since
>     the whole point of this check is to repair the cache-tree by
>     recording an already existing tree object opportunistically, we
>     shouldn't even try to fetch one from the remote.
> 
>     Pass the OBJECT_INFO_SKIP_FETCH_OBJECT flag to make sure we only
>     check for existence in the local object store without triggering the
>     lazy fetch mechanism.
As a third-party observer, that explanation makes sense to me.

I wondered also if this means we should be using OBJECT_INFO_QUICK. I.e., do we expect to see a "miss" here often, forcing us to re-scan the packed directory?

Reading dd0c34c46b (cache-tree: protect against "git prune"., 2006-04-24), I think the answer is "no".

-Peff
Junio C Hamano· Sep 10, 2019, 01:09 UTC · re: Jeff King · lore

Re: [PATCH v2] cache-tree: do not lazy-fetch merge tree

Jeff King <peff@peff.net> writes:
> I wondered also if this means we should be using OBJECT_INFO_QUICK.
> I.e., do we expect to see a "miss" here often, forcing us to re-scan the
> packed directory?

As a performance optimization hack, it is OK if we did not notice that the tree object, which corresponds to what is currently prepared for a directory in the index, does exist in the object store. It is not worth rescanning the packs to "protect" against races, I think, in the "repair" codepath.

When the user actually wants to write the index out as a tree, we would write it out as a loose object (or omit doing so if we know there are already copies), but because it is not a crime to create a duplicate loose object when we already have a packed copy, I do not think we need to rescan in that context, either. But I do not think the codepath Jonathan's patch touches is about that operation.

Jonathan Tan· Sep 10, 2019, 18:15 UTC · re: Junio C Hamano · lore

Re: [PATCH v2] cache-tree: do not lazy-fetch merge tree

Show 29 quoted lines
> Junio C Hamano <gitster@pobox.com> writes:
> 
> > Isn't that what is going on?  I thought I dug up the original that
> > introduced the has_object_file() call to this codepath to make sure
> > we understand why we make the check (and I expected the person who
> > is proposing this change to do the same and record the finding in
> > the proposed log message).
> >
> > I am running out of time today, and will revisit later this week
> > (I'll be down for at least two days starting tomorrow, by the way).
> 
> Here is what I came up with.
> 
>     The cache-tree datastructure is used to speed up the comparison
>     between the HEAD and the index, and when the index is updated by
>     a cherry-pick (for example), a tree object that would represent
>     the paths in the index in a directory is constructed in-core, to
>     see if such a tree object exists already in the object store.
> 
>     When the lazy-fetch mechanism was introduced, we converted this
>     "does the tree exist?" check into an "if it does not, and if we
>     lazily cloned, see if the remote has it" call by mistake.  Since
>     the whole point of this check is to repair the cache-tree by
>     recording an already existing tree object opportunistically, we
>     shouldn't even try to fetch one from the remote.
> 
>     Pass the OBJECT_INFO_SKIP_FETCH_OBJECT flag to make sure we only
>     check for existence in the local object store without triggering the
>     lazy fetch mechanism.

This commit message looks good to me. Thanks for writing the commit message - I thought that the justification in the commit message I wrote would be sufficient, but it makes sense to look into why the check was done.

SZEDER Gábor· Sep 10, 2019, 12:49 UTC · re: Jonathan Tan · lore

Re: [PATCH v2] cache-tree: do not lazy-fetch merge tree

On Mon, Sep 09, 2019 at 12:01:30PM -0700, Jonathan Tan wrote:
Show 22 quoted lines
> diff --git a/t/t0410-partial-clone.sh b/t/t0410-partial-clone.sh
> index 6415063980..3e434b6a81 100755
> --- a/t/t0410-partial-clone.sh
> +++ b/t/t0410-partial-clone.sh
> @@ -492,6 +492,20 @@ test_expect_success 'gc stops traversal when a missing but promised object is re
>  	! grep "$TREE_HASH" out
>  '
>  
> +test_expect_success 'do not fetch when checking existence of tree we construct ourselves' '
> +	rm -rf repo &&
> +	test_create_repo repo &&
> +	test_commit -C repo base &&
> +	test_commit -C repo side1 &&
> +	git -C repo checkout base &&
> +	test_commit -C repo side2 &&
> +
> +	git -C repo config core.repositoryformatversion 1 &&
> +	git -C repo config extensions.partialclone "arbitrary string" &&
> +
> +	git -C repo cherry-pick side1
> +'
> +

Sidenote, just curious: did you originally intend to add this test before the test script sources 'lib-httpd.sh', or you were about to append it at the end as usual, but then noticed the warning comment telling you not to do so?

>  . "$TEST_DIRECTORY"/lib-httpd.sh
>  start_httpd
Jonathan Tan· Sep 10, 2019, 18:19 UTC · re: SZEDER Gábor · lore

Re: [PATCH v2] cache-tree: do not lazy-fetch merge tree

> Sidenote, just curious: did you originally intend to add this test
> before the test script sources 'lib-httpd.sh', or you were about to
> append it at the end as usual, but then noticed the warning comment
> telling you not to do so?

Honestly, I don't remember. I do try to put tests near similar tests, so I might have seen that we had HTTP tests at the bottom and non-HTTP tests at the top, but in this case, I don't remember if I had that thought before putting this test where it is now.

← back to recent threads