Skip to content

feat: add Directory.asGit - #9730

Merged
jedevc merged 8 commits into
dagger:mainfrom
jedevc:git-as-directory
Mar 10, 2025
Merged

jedevc merged 8 commits into
dagger:mainfrom
jedevc:git-as-directory

Conversation

@jedevc

@jedevc jedevc commented Feb 27, 2025 •

Copy link
Copy Markdown
Contributor

Revives #9098.

This PR follows up on the work in #9395, by adding a new Directory.asGit API. This allows for importing a directory and performing git operations on it. This was really hard to do efficiently at all without some sort of buildkit caching integration.

Note - we still need to do some nasty shuffling around in a temporary directory - however, I have an idea for how to clean this up later, just need to tinker around.

@jedevc
jedevc force-pushed the git-as-directory branch 7 times, most recently from 9f18b64 to 4a5a8ad Compare March 3, 2025 14:53
@jedevc
jedevc marked this pull request as ready for review March 3, 2025 14:53
@jedevc
jedevc requested review from a team as code owners March 3, 2025 14:53
@jedevc
jedevc requested a review from sipsma March 3, 2025 15:05
@jedevc
jedevc force-pushed the git-as-directory branch 6 times, most recently from 3f6d2c6 to bf6559d Compare March 5, 2025 14:31

@sipsma sipsma left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super cool! only significant comment is around the ref releasing

Comment thread core/directory.go

LLB *pb.Definition
LLB *pb.Definition
Result bkcache.ImmutableRef // only valid when returned by dagop

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might have not gotten to this part of the code yet but do we ever release these (this one and the one in File)? If not then the object will get cached until the session is closed and then the ref will leak

@jedevc jedevc Mar 6, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 From my understanding, we only need to Release non-returned Refs (e.g. the cloned git repository in the git source implementation), but any returned Refs (e.g. the checked out git repo in the same) aren't. Which makes sense, once it's returned, buildkit is then responsible for cleaning up the ref, if you return a released ref, you're sure to get odd results.

The only scenario we'd need to release this is in the case of an error - but I don't think there's a possibility for an error between returning this, and immediately loading it into FSDagOp.Exec where it's returned. So I don't think Releaseing is necessary here? We already handle all the error cases in the function bodies where we create and set Result.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

buildkit is then responsible for cleaning up the ref

Is it? There's obviously way too many layers of abstraction we have to deal with here so I may be forgetting something but I can't think of where buildkit would release e.g. the Result we set in Container.asTarball.

Actually, if that is true and buildkit is free to release the result even though we have an open ref to it, that would mean it could prune it and then we could be trying to use a ref that doesn't exist anymore.

if you return a released ref, you're sure to get odd results.

Right, what I'm saying is that when an object with a Result is removed from dagql cache, we should release the Result at that time. So the ref stays open until we don't have it in memory anymore. It's essentially manual memory management, but in Go 🎉😭


Again apologies if I'm totally missing something here, the dots just aren't connecting yet

@jedevc jedevc Mar 6, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I think I get where the confusion is potentially. The Result shouldn't propagate through the entire dagql graph - or through the cache at all. It should be impure so it never enters the dagql cache (but since that's removed, not sure how I do that now).

The Result is only read from the Load in the DagOp - but then the rest of the graph sees a "normal" Directory, with an LLB op that is that DagOp.

So actually I think this is a bit wrong now:

// Return an ID that can be used to force execution of the current ID as a DagOp.
// It works by mixing in a constant value into the current ID's digest.
func currentIDForDagOp(ctx context.Context) *call.ID {
currentID := dagql.CurrentID(ctx)
return currentID.WithDigest(dagql.HashFrom(
currentID.Digest().String(),
runDagOpDigestMixin,
))
}

I think we want it to not store anything to the dagql cache - since it could return a Result, which isn't valid outside of that single Exec.

Does that make sense?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Result shouldn't propagate through the entire dagql graph - or through the cache at all. It should be impure so it never enters the dagql cache (but since that's removed, not sure how I do that now).

Okay that explains more yeah, it's just not apparent at first because of the spaghetti around the self-invoking ops (which isn't a criticism at all, it's just the hoops we have to jump through w/ buildkit until we get to the other side of this whole effort).

As a last resort we can add DoNotCache to IDs (only difference w/ previous Impure is that it wouldn't "propagate" and instead just behave the same as fields marked DoNotCache).

But I'd like to avoid that mainly since I don't want to add more complexity to IDs just for the sake of this temporary bridge. At least, I can't think of any reason we'd ever need, in the long-run, self-invoking APIs where the inner result cannot be cached due to reasons like this.

Other more preferable options I can think of:

  1. Add support for a callback that runs when a CachedResult in the dagql cache is Released, building on top of the stuff added in cache result wrapping and context cancel management in dagql cache #9774
    • That feels likely to be something we'd have more use for in the long-run in addition to solving this short term problem
  2. Figure out a way to get DoNotCache settable from custom cache key funcs, dealing with what I mentioned here replace impurity with mix of custom cache keys + field spec settings #9719 (comment)
    • Then we can look at the dagop context and decide whether to cache or not
    • My only hesitation is that this seems like something better to during the migration of dagql cache to sqlite (and all the associated features that'll entail), but we can figure something out earlier than that too

I'm happy to try to help out with this since it's obviously getting way out of scope of what you were doing. I'll try 1 quick and see what it looks like, might not be that bad.

@sipsma sipsma Mar 7, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think I got that working here: 6165ea7

It's actually just 50% copypasta from #9682 plus the addition of OnRelease methods to File/Directory, worked out pretty nicely.

Did some manual testing of cache ref leaks and looks good.

Let me know what you think

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neaaaat 🎉 thanks, that's really so much better 😆

Comment thread core/git.go Resolved
Comment thread core/util.go Resolved
@jedevc
jedevc force-pushed the git-as-directory branch from bf6559d to 1247d75 Compare March 6, 2025 11:17
@sipsma
sipsma force-pushed the git-as-directory branch 2 times, most recently from 5fad493 to d8e18d7 Compare March 7, 2025 22:01
@sipsma

sipsma commented Mar 7, 2025

Copy link
Copy Markdown
Contributor

@jedevc after my commit for #9730 (comment) I saw a flake in TestCall/TestGitMod, which I assumed at first was from my commit.

It turns out it was happening before my commit too, but I figured it out and fixed here: d8e18d7

It was really tricky, but basically

  1. our SDK calls are already cached in buildkit across sessions and they use a content-based digest
  2. these test cases run against modules that are bit-by-bit source identical, but from different repos that have different auth requirements
  3. the module source SDK calls thus could hit cache from clients that don't exist anymore and that used auth which isn't available anymore
  4. that resulted in a secret transfer error

The patch just incorporates the git endpoint url into the content-based cache key for those sources. This means we get less caching but only for the very very specific scenario of "two repos with bit-by-bit identical module source", but I think that's exceptionally rare anywhere outside our integ tests, so not a big deal imo.

Comment thread core/dagop.go
case dagql.Instance[*Directory]:
if inst.Self.Result != nil {
ref := worker.NewWorkerRefResult(inst.Self.Result, opt.Worker)
ref := worker.NewWorkerRefResult(inst.Self.Result.Clone(), opt.Worker)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah neat, yup, I think this is what I was confused by - we need to Clone here, since otherwise we would be potentially double released refs.

LGTM 🎉

jedevc added 2 commits March 10, 2025 12:24
These are exclusively used in modulesource.go, so we should move them to
there.

Signed-off-by: Justin Chadwell <[email protected]>
jedevc and others added 6 commits March 10, 2025 12:24
Signed-off-by: Justin Chadwell <[email protected]>
This allows returning `File`s and `Directory`s more easily, using a
wrapped immutable cache ref - this avoids needing our nasty hack to
import from the host file system: we can just write directly to our
cache ref.

To get this working with `Container.asTarball` though, we needed a
little reworking, so that's it's possible to get a reasonable filename
for the tarball. Potentially this doesn't work if the filename is
*completely* unknown until running the function? So, the
`DirectoryDagOp` becomes a generic `FSDagOp` able to handle both
`Directory`s and `File`s.

Signed-off-by: Justin Chadwell <[email protected]>
Signed-off-by: Justin Chadwell <[email protected]>
@jedevc
jedevc force-pushed the git-as-directory branch from d8e18d7 to 3fc5d19 Compare March 10, 2025 12:24
@jedevc

jedevc commented Mar 10, 2025

Copy link
Copy Markdown
Contributor Author

Oop, noticed that somewhere some change was adding nil llb dependencies - fixed that, think it was an oversight.

Should be good now 🎉 @sipsma the changes you've made look good to me, just need an approval now, and should be good to merge whenever!

@jedevc
jedevc merged commit e2be4ea into dagger:main Mar 10, 2025
@jedevc
jedevc deleted the git-as-directory branch March 10, 2025 16:59
Comment thread core/modulesource.go
Comment on lines +286 to +295
// For git sources, we currently need to mix in the actual git endpoint
// in order to ensure that we don't get cache hits on repositories that
// have the same content but different auth. If we get a cache hit on
// a repo we don't have auth for subsequent operations can break.
// Given that scenarios in which two different git repos have the exact
// same bit-by-bit module source are expected to be rare, this shouldn't
// be a big compromise.
if src.Git != nil {
inputs = append(inputs, src.Git.CloneRef)
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, @sipsma I've seen a similar flake happening: https://v3.dagger.cloud/dagger/traces/10d54381586861dbb1835770e95c9215?span=55f4d468d718aff1

image

In the above, the bitbucket clone fails because we don't have access to the gitlab credentials. It feels like the buildkit edge merging equivalent of what you just fixed here?

I'm guessing that what's happening is that we're edge merging these two git sources because technically they have the same commit, so can be merged. This would be fine, I think, but, I think we also have double caching of Git.tree for remotes - it's getting cached at the dagop level, and then again at the git source level. There are two ops here, which is making things confusing, and definitely isn't helping.

The right fix here will be to have this as a dagql interface I think, so that one of the implementations can get wrapped, and the other not. But adding core interfaces is a bit tricky, so gonna try and have a bit more of a think on how to fix this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My best guess at a fix is in #9840. I think that should resolve this, but even if it doesn't, it should hopefully make a bit clearer to see what's actually going on.

vikram-dagger pushed a commit to vikram-dagger/dagger that referenced this pull request Mar 17, 2025
* chore: move matchVersion helpers to modulesource.go

These are exclusively used in modulesource.go, so we should move them to
there.

Signed-off-by: Justin Chadwell <[email protected]>

* tests: refactor GitTest to avoid testutil.Query

Signed-off-by: Justin Chadwell <[email protected]>

* feat: add Directory.asGit

Signed-off-by: Justin Chadwell <[email protected]>

* feat: refactor dagop usage

This allows returning `File`s and `Directory`s more easily, using a
wrapped immutable cache ref - this avoids needing our nasty hack to
import from the host file system: we can just write directly to our
cache ref.

To get this working with `Container.asTarball` though, we needed a
little reworking, so that's it's possible to get a reasonable filename
for the tarball. Potentially this doesn't work if the filename is
*completely* unknown until running the function? So, the
`DirectoryDagOp` becomes a generic `FSDagOp` able to handle both
`Directory`s and `File`s.

Signed-off-by: Justin Chadwell <[email protected]>

* chore: suggestions from review

Signed-off-by: Justin Chadwell <[email protected]>

* add support for result release callbacks, use on dir/file

Signed-off-by: Erik Sipsma <[email protected]>

* fix sdk call cache hits on repos w/ different auth

Signed-off-by: Erik Sipsma <[email protected]>

* fix: extractLLBDependencies with nil data

Signed-off-by: Justin Chadwell <[email protected]>

---------

Signed-off-by: Justin Chadwell <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Co-authored-by: Erik Sipsma <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants