Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@wagoodman
Copy link
Contributor

@wagoodman wagoodman commented Jun 27, 2021

This PR changes the tempfile generation to share the same parent directory for the same call to GetImage or GetImageFromSource. This change requires that cleanup functions are exposed on these GetImage* calls to cleanup temp dirs created by that particular call.

Today the temp file generation is globally handled with the Cleanup function, which (correctly) breaks when used as a test cleanup function, since it affects global state. Test cleanup calls now leverage the more narrowly-scoped cleanup function returned from GetImage* functions.

This PR also opts to leave the global Cleanup function in tact. Why? Callers (such as syft and grype) may need to interrupt the process and exit, but also make certain that any temp directories are cleaned up. This PR updates the file.TempDirGenerator tracks all temp generators created and temp directories created from a root object such that the root object can cleanup all temp directories from all nested generators. This allows callers to have the "clean everything up and exit" option for exceptional cases (today, syft needs this).

Specific changes:

  • Removes locking and individual dir creation tracking from file.TempDirGenerator
  • A single TempDirGenerator now keeps all created temp dirs within a shared parent dir.
  • Adds the ability to create additional TempDirGenerators from a TempDirGenerator, which will create a sibling temp dir with the same prefix as the parent prefix. All directories created from the child generator are persisted in the sibling temp dir from the parent generator.
  • TempDirGenerator.Cleanup calls cleanup on all child generators as well as removing the shared top directory among all created temp dirs for the generator.
  • adds image.Image.Cleanup() deletes the temp dir scoped for that particular image.

Partially addresses anchore/syft#416
Fixes #96

@wagoodman wagoodman added the enhancement New feature or request label Jun 27, 2021
@wagoodman wagoodman requested a review from a team June 27, 2021 03:16
@wagoodman wagoodman self-assigned this Jun 27, 2021
@wagoodman wagoodman force-pushed the upgrade-temp-dir-generation branch from 1f7246d to aa11ddc Compare June 27, 2021 03:16
@github-actions
Copy link

github-actions bot commented Jun 27, 2021

Benchmark Test Results

Benchmark results from the latest changes vs base branch
name        old time/op    new time/op    delta
pkg:github.com/anchore/stereoscope/pkg/file goos:linux goarch:amd64
TarIndex-2    48.5µs ± 4%    49.8µs ± 0%   ~     (p=0.343 n=4+4)

name        old alloc/op   new alloc/op   delta
pkg:github.com/anchore/stereoscope/pkg/file goos:linux goarch:amd64
TarIndex-2    8.38kB ± 0%    8.38kB ± 0%   ~     (p=1.000 n=4+4)

name        old allocs/op  new allocs/op  delta
pkg:github.com/anchore/stereoscope/pkg/file goos:linux goarch:amd64
TarIndex-2       205 ± 0%       205 ± 0%   ~     (all equal)

Copy link
Contributor

@luhring luhring left a comment

Choose a reason for hiding this comment

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

You've made some great improvements here! Having the image creation provide its own cleanup function is excellent because it lets image cleanup be handled directly alongside a caller's use of the image. This makes it much easier for consumers to manage the side effects had by image creation operations.

(Quick aside: One alternative approach to consider would be to have the image's cleanup function be a method on the image itself. This way, any consumer of the image is capable of cleaning up the image's backing data (similar to how database connection objects frequently have a conn.Close() method).

Primary concern: We need to avoid global state wherever possible, since it creates opportunities for bugs to arise in the future and it typically complicates the codebase. Fortunately, because of the cleanup functions being associated with image creation, we have an opportunity to associate an image's temp data with the image object directly. This would eliminate the need to maintain a global store of temp directories, a maintenance that has gotten a bit more complicated in the current draft.

Image handling would become more atomic, and a consumer of any given image wouldn't need to know about anything more global than the image itself.

@wagoodman
Copy link
Contributor Author

wagoodman commented Jun 28, 2021

@luhring The leveraging of global state is unfortunate, but also an existing behavior today. This PR has made a step in removing that global state (with the cleanup functions). I think we need to sync up on context on Monday as some perspectives have been over simplified.

@wagoodman wagoodman marked this pull request as draft June 28, 2021 14:16
@wagoodman
Copy link
Contributor Author

We're deferring this work for now

@wagoodman wagoodman closed this Jun 29, 2021
@wagoodman wagoodman reopened this Feb 9, 2022
@wagoodman wagoodman force-pushed the upgrade-temp-dir-generation branch from aa11ddc to 72a2782 Compare February 9, 2022 15:42
@wagoodman wagoodman marked this pull request as ready for review February 9, 2022 15:51
@wagoodman wagoodman changed the title Improve temp dir generation + clean up Add per-image scoped temp dir cleanup functions Feb 9, 2022
@wagoodman wagoodman requested a review from a team February 9, 2022 15:53
@wagoodman wagoodman force-pushed the upgrade-temp-dir-generation branch 2 times, most recently from e677dca to 980163d Compare February 9, 2022 16:04
Copy link
Contributor

@luhring luhring left a comment

Choose a reason for hiding this comment

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

Nice!

@wagoodman wagoodman force-pushed the upgrade-temp-dir-generation branch from 980163d to 2f2d423 Compare February 9, 2022 17:55
@wagoodman wagoodman merged commit 403dd70 into main Feb 9, 2022
@wagoodman wagoodman deleted the upgrade-temp-dir-generation branch February 9, 2022 18:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Concurrently analyzing containers can lead to race condition causing error

2 participants