Skip to content

Switch to short IDs kept in memory - #5013

Closed
vito wants to merge 2 commits into
dagger:mainfrom
vito:short-ids
Closed

vito wants to merge 2 commits into
dagger:mainfrom
vito:short-ids

Conversation

@vito

@vito vito commented Apr 24, 2023 •

Copy link
Copy Markdown
Contributor

Currently ContainerID, FileID, and DirectoryID are gigantic strings because they are self-contained values that store the instructions for creating them.

I noticed during testing that a these can be pretty expensive to deal with. Dagger was burning a ton of memory on my laptop and even caused my OS to crash. I think the root cause of this was really #5012 but either way I don't think we necessarily wanted these to be giant values forever.

This PR maintains their content-addressed quality by replacing them with a digest.Digest which maps to an internal cache from digest.Digest to *Container, *Directory, and *File.

New caveats to consider:

  • IDs are no longer transferrable with zero-context as before. They're content-addressed, rather than being literally the content.
  • The mapping from ID to concrete value grows without bounds, so if you have a long-lived session running containers over time you will eventually run out of memory. We could improve this with an LRU cache or something, but it raises some uncomfortable questions: what if someone finally calls back to use an ID that has been reaped?
  • I initially went with xxh3 + base32 to generate these hashes, which is the same technique we use for container hostnames, but I switched to SHA256 digest.Digest just because that's more common everywhere else. Curious if anyone has an opinion on this.

I'm curious if that second point affects anyone. I don't think we're really focused on 'infinite sessions' right now so my hunch is nothing would break with this change, but it's worth picking apart a bit more.

vito added 2 commits April 24, 2023 11:43
These IDs can get comically large and it makes it difficult to display
them anywhere. Now we'll use the same hashing scheme used for service
hostnames to create much shorter IDs that are mapped to full objects
in-memory.

Signed-off-by: Alex Suraci <[email protected]>
This is mostly a "when in rome" decision, but I'm not 100% sold on it.
It made sense to use xxh3 and base32 for container hostnames owing to
size restrictions but it doesn't make as much sense everywhere else.

Signed-off-by: Alex Suraci <[email protected]>
@sipsma

sipsma commented Apr 24, 2023 •

Copy link
Copy Markdown
Contributor

I tried this a long time ago but backed out because it breaks the caching of extension outputs. Basically, if an extensions returns a Container/Directory/File, it does so by writing the id to a mount for the result. That way you can obtain the result of the extension from the cache without having to necessarily re-run it.

I think we can probably find other ways of dealing with that such as giving extensions some internal api for mapping hashed id->full id to let them still persist full id. So I'd be okay with this change in general but just want to mention that up front.

EDIT: Also, don't worry about implementing that now, we can fix extensions later, I just am thinking out loud here.

@sipsma

sipsma commented Apr 24, 2023

Copy link
Copy Markdown
Contributor

IDs are no longer transferrable with zero-context as before. They're content-addressed, rather than being literally the content.

This is what we've intended in the long run, but unfortunately in the mean time we have published some docs that rely on the IDs having a degree of re-usability, e.g. here: https://docs.dagger.io/cli/389936/run-pipelines-cli

I think for those examples at least we can just update them to wrap the whole script in a dagger run so it all happens in the context of a single session. Not sure off the top of my head if dagger query will also need updates to re-use the DAGGER_SESSION_* env vars when set, but if so that is likely a simple update too.

The mapping from ID to concrete value grows without bounds, so if you have a long-lived session running containers over time you will eventually run out of memory. We could improve this with an LRU cache or something, but it raises some uncomfortable questions: what if someone finally calls back to use an ID that has been reaped?

I think for almost all use cases this is totally fine; the memory overhead is essentially a factor of how many IDs you've used in the session so far, which isn't worse than the worst case today where all those IDs are being sent in memory as part of requests. Today it's a bit more spiky in that the memory goes down when the request isn't in flight, but I really doubt this will be a bottleneck. If we do run into issues, we can also find ways of decreasing the size of the unhashed IDs, even via something as dumb as just compressing them.

The one special use case I can think of where this may matter is the playground cc @marcosnils

I don't think we'd ever want to use an LRU cache, I don't think there's any way of dealing with the ID being kicked from it besides returning an error to the user, which wouldn't make any sense to them.

I initially went with xxh3 + base32 to generate these hashes, which is the same technique we use for container hostnames, but I switched to SHA256 digest.Digest just because that's more common everywhere else. Curious if anyone has an opinion on this.

I'm very ambivalent, but would probably air on the side of sha256 too just because it's more well understood and from the stdlib. And on the off-chance we for whatever reason actually want these IDs to be cryptographically secure someday, then we'll already have that on solid footing. I'm sure xxh3 is totally fine too though

@github-actions

github-actions Bot commented May 9, 2023

Copy link
Copy Markdown
Contributor

This PR is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@vito

vito commented May 9, 2023 •

Copy link
Copy Markdown
Contributor Author

Closing this for now. We can re-assess once the dust has settled around entrypoints/extensions.

The only reason I still wanted this was to have shorter IDs in the TUI with --debug but we don't actually need to switch to digest IDs internally for that: 9735fc4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants