Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg: add support to consume snapshots #202

Merged
merged 1 commit into from
Dec 16, 2020

Conversation

ffromani
Copy link
Collaborator

@ffromani ffromani commented Dec 14, 2020

Needed for #66 (see #66 (comment) and following)

Add support to allow ghw read from snapshots (created with ghw-snapshot) using
environment variables or programmatically.
This is meant to be used mostly in the testsuite, but can be useful for
users in general.

Added environment variables (and counterpart in golang pkgs):

  • GHW_SNAPSHOT_PATH let users specify a snapshot
    that ghw will automatically consume.
  • GHW_SNAPSHOT_ROOT let users specify the directory
    on which the snapshot should be unpacked.
  • GHW_SNAPSHOT_EXCLUSIVE is relevant iff GHW_SNAPSHOT_ROOT is given.
    Tells ghw that the directory is meant only to contain the given snapshot,
    thus ghw will not attempt to unpack it
    (and will go ahead silently!) unless the directory is empty.
  • GHW_SNAPSHOT_PRESERVE if set, ghw will not
    clean up the unpacked snapshot once done, leaving it into the system.

Signed-off-by: Francesco Romani fromani@redhat.com

@ffromani
Copy link
Collaborator Author

@jaypipes PRs will follow soon against ghw-snapshot to capture more data from sysfs and, and after that I'll post also a PR with the initial set of snapshot blobs.

Copy link
Owner

@jaypipes jaypipes left a comment

Choose a reason for hiding this comment

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

@fromanirh you rock brother! :)

I left some nits/suggestions inline but this looks awesome. Really appreciate your effort here!

Best,
-jay

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
pkg/option/option.go Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
pkg/context/context.go Show resolved Hide resolved
pkg/option/option.go Outdated Show resolved Hide resolved
pkg/snapshot/unpack.go Show resolved Hide resolved
@ffromani
Copy link
Collaborator Author

Thanks for the quick review @jaypipes ! I'm happy you like the direction. I'll be more than happy to address your comments and fix the docs in the next uploads.

@ffromani
Copy link
Collaborator Author

update: I'd love to add a couple unit test before to the next upload to address review comments. This will take a little while more.

@ffromani ffromani closed this Dec 15, 2020
@ffromani ffromani reopened this Dec 15, 2020
@ffromani
Copy link
Collaborator Author

(clicked wrong button again)

@ffromani
Copy link
Collaborator Author

ffromani commented Dec 15, 2020

@jaypipes hey, just FYI I'm intentionally keeping the commits separated to make review (hopefully) easier, if you want me to squash them no problem at all, just let me know and I'll do that right away.

Copy link
Owner

@jaypipes jaypipes left a comment

Choose a reason for hiding this comment

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

Hi again @fromanirh, sorry I wasn't clear in my previous review! It was the GHW_SNAPSHOT_KEEP that I was suggesting to change to GHW_SNAPSHOT_PRESERVE :)

Also, recommend git rebasing your patches in this PR into a single commit if you don't mind?

Best,
-jay

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
pkg/context/context.go Outdated Show resolved Hide resolved
@ffromani
Copy link
Collaborator Author

ffromani commented Dec 15, 2020

Hi again @fromanirh, sorry I wasn't clear in my previous review! It was the GHW_SNAPSHOT_KEEP that I was suggesting to change to GHW_SNAPSHOT_PRESERVE :)

Also, recommend git rebasing your patches in this PR into a single commit if you don't mind?

Best,
-jay

No worries, no problem at all. I'll fix ,squash and push again. Thanks for your quick reviews!

Add support to allow `ghw` read from snapshots (created with `ghw-snapshot`) using
environment variables or programmatically.
This is meant to be used mostly in the testsuite, but can be useful for
users in general.

Added environment variables (and counterpart in golang pkgs):
- `GHW_SNAPSHOT_PATH` let users specify a snapshot
   that `ghw` will automatically consume.
- `GHW_SNAPSHOT_ROOT` let users specify the directory
   on which the snapshot should be unpacked.
- `GHW_SNAPSHOT_EXCLUSIVE` is relevant iff `GHW_SNAPSHOT_ROOT` is given.
   Tells `ghw` that the directory is meant only to contain the given snapshot,
   thus `ghw` will *not* attempt to unpack it
   (and will go ahead silently!) unless the directory is empty.
- `GHW_SNAPSHOT_PRESERVE` if set, `ghw` will *not*
   clean up the unpacked snapshot once done, leaving it into the system.

Signed-off-by: Francesco Romani <fromani@redhat.com>
Copy link
Owner

@jaypipes jaypipes left a comment

Choose a reason for hiding this comment

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

Fantastic work, @fromanirh, thank you so much! :)

@jaypipes jaypipes merged commit 5d04579 into jaypipes:master Dec 16, 2020
@ffromani
Copy link
Collaborator Author

Fantastic work, @fromanirh, thank you so much! :)

My pleasure! thank you for the quick and insightful reviews!

@ffromani ffromani deleted the consume-snapshots branch December 17, 2020 07:54
@jaypipes jaypipes added this to the v0.7 milestone Jan 12, 2021
ffromani added a commit to ffromani/ghw that referenced this pull request Apr 11, 2021
The PR jaypipes#236 fixes a snapshot
directory leakage issue, but also highlight a context
lifecycle/ownership issue.

In general in ghw each package creates its own context, including
the cases on which a package is consumed by another, for example
`topology` and `pci`. However, in this case, it would make more sense
to reuse the context from the parent package, because the two packages
are tightly coupled.

Before the introduction of the the transparent snapshot support
(jaypipes#202), the above mechanism worked,
because each context, each time, was consuming over and over again the
same parameters (e.g. environment variables); besides some resource
waste, this had no negative effect.

When introducing snapshots in the picture, repeatedly unpacking the
same snapshot to consume the same data is much more wasteful.
So it makes sense to introduce explicitly the concept of dependent
context.

We add a functio to create a context subordinate to another.
The subordinate context will effectively borrow all the resources from
the parent one. The parent is in charge to perform any setup/teardown
(e.g. for snapshot, to learn which chroot should be used, if at all)
and so forth.
Making this dependency explicit allow the client code to manage
correctly the lifecycle of the topmost context, hence of all the
resources.

Signed-off-by: Francesco Romani <fromani@redhat.com>
ffromani added a commit to ffromani/ghw that referenced this pull request Apr 12, 2021
The PR jaypipes#236 fixes a snapshot
directory leakage issue, but also highlight a context
lifecycle/ownership issue.

In general in ghw each package creates its own context, including
the cases on which a package is consumed by another, for example
`topology` and `pci`. However, in this case, it would make more sense
to reuse the context from the parent package, because the two packages
are tightly coupled.

Before the introduction of the the transparent snapshot support
(jaypipes#202), the above mechanism worked,
because each context, each time, was consuming over and over again the
same parameters (e.g. environment variables); besides some resource
waste, this had no negative effect.

When introducing snapshots in the picture, repeatedly unpacking the
same snapshot to consume the same data is much more wasteful.
So it makes sense to introduce explicitly the concept of dependent
context.

We add a functio to create a context subordinate to another.
The subordinate context will effectively borrow all the resources from
the parent one. The parent is in charge to perform any setup/teardown
(e.g. for snapshot, to learn which chroot should be used, if at all)
and so forth.
Making this dependency explicit allow the client code to manage
correctly the lifecycle of the topmost context, hence of all the
resources.

Signed-off-by: Francesco Romani <fromani@redhat.com>
ffromani added a commit to ffromani/ghw that referenced this pull request Apr 12, 2021
The PR jaypipes#236 fixes a snapshot
directory leakage issue, but also highlight a context
lifecycle/ownership issue.

In general in ghw each package creates its own context, including
the cases on which a package is consumed by another, for example
`topology` and `pci`. However, in this case, it would make more sense
to reuse the context from the parent package, because the two packages
are tightly coupled.

Before the introduction of the the transparent snapshot support
(jaypipes#202), the above mechanism worked,
because each context, each time, was consuming over and over again the
same parameters (e.g. environment variables); besides some resource
waste, this had no negative effect.

When introducing snapshots in the picture, repeatedly unpacking the
same snapshot to consume the same data is much more wasteful.
So it makes sense to introduce explicitly the concept of dependent
context.

We add a functio to create a context subordinate to another.
The subordinate context will effectively borrow all the resources from
the parent one. The parent is in charge to perform any setup/teardown
(e.g. for snapshot, to learn which chroot should be used, if at all)
and so forth.
Making this dependency explicit allow the client code to manage
correctly the lifecycle of the topmost context, hence of all the
resources.

Signed-off-by: Francesco Romani <fromani@redhat.com>
ffromani added a commit to ffromani/ghw that referenced this pull request Apr 25, 2021
The PR jaypipes#236 fixes a snapshot
directory leakage issue, but also highlight a context
lifecycle/ownership issue.

In general in ghw each package creates its own context, including
the cases on which a package is consumed by another, for example
`topology` and `pci`. However, in this case, it would make more sense
to reuse the context from the parent package, because the two packages
are tightly coupled.

Before the introduction of the the transparent snapshot support
(jaypipes#202), the above mechanism worked,
because each context, each time, was consuming over and over again the
same parameters (e.g. environment variables); besides some resource
waste, this had no negative effect.

When introducing snapshots in the picture, repeatedly unpacking the
same snapshot to consume the same data is much more wasteful.
So it makes sense to introduce explicitly the concept of dependent
context.

We add a functio to create a context subordinate to another.
The subordinate context will effectively borrow all the resources from
the parent one. The parent is in charge to perform any setup/teardown
(e.g. for snapshot, to learn which chroot should be used, if at all)
and so forth.
Making this dependency explicit allow the client code to manage
correctly the lifecycle of the topmost context, hence of all the
resources.

Signed-off-by: Francesco Romani <fromani@redhat.com>
ffromani added a commit to ffromani/ghw that referenced this pull request May 13, 2021
The PR jaypipes#236 fixes a snapshot
directory leakage issue, but also highlight a context
lifecycle/ownership issue.

In general in ghw each package creates its own context, including
the cases on which a package is consumed by another, for example
`topology` and `pci`. However, in this case, it would make more sense
to reuse the context from the parent package, because the two packages
are tightly coupled.

Before the introduction of the the transparent snapshot support
(jaypipes#202), the above mechanism worked,
because each context, each time, was consuming over and over again the
same parameters (e.g. environment variables); besides some resource
waste, this had no negative effect.

When introducing snapshots in the picture, repeatedly unpacking the
same snapshot to consume the same data is much more wasteful.
So it makes sense now to introduce explicitly the concept of dependent
context.

We add a function to create a context subordinate to another.
The subordinate context will effectively borrow all the resources from
the parent one. The parent is in charge to perform any setup/teardown
(e.g. for snapshot, to learn which chroot should be used, if at all)
and so forth.

Making this dependency explicit allow the client code to manage
correctly the lifecycle of the topmost context, hence of all the
resources.

Signed-off-by: Francesco Romani <fromani@redhat.com>
ffromani added a commit to ffromani/ghw that referenced this pull request May 14, 2021
The PR jaypipes#236 fixes a snapshot
directory leakage issue, but also highlight a context
lifecycle/ownership issue.

In general in ghw each package creates its own context, including
the cases on which a package is consumed by another, for example
`topology` and `pci`. However, in this case, it would make more sense
to reuse the context from the parent package, because the two packages
are tightly coupled.

Before the introduction of the the transparent snapshot support
(jaypipes#202), the above mechanism worked,
because each context, each time, was consuming over and over again the
same parameters (e.g. environment variables); besides some resource
waste, this had no negative effect.

When introducing snapshots in the picture, repeatedly unpacking the
same snapshot to consume the same data is much more wasteful.
So it makes sense now to introduce explicitly the concept of dependent
context.

We add a function to create a context subordinate to another.
The subordinate context will effectively borrow all the resources from
the parent one. The parent is in charge to perform any setup/teardown
(e.g. for snapshot, to learn which chroot should be used, if at all)
and so forth.

Making this dependency explicit allow the client code to manage
correctly the lifecycle of the topmost context, hence of all the
resources.

Signed-off-by: Francesco Romani <fromani@redhat.com>
ffromani added a commit to ffromani/ghw that referenced this pull request May 26, 2021
The PR jaypipes#236 fixes a snapshot
directory leakage issue, but also highlight a context
lifecycle/ownership issue.

In general in ghw each package creates its own context, including
the cases on which a package is consumed by another, for example
`topology` and `pci`. However, in this case, it would make more sense
to reuse the context from the parent package, because the two packages
are tightly coupled.

Before the introduction of the the transparent snapshot support
(jaypipes#202), the above mechanism worked,
because each context, each time, was consuming over and over again the
same parameters (e.g. environment variables); besides some resource
waste, this had no negative effect.

When introducing snapshots in the picture, repeatedly unpacking the
same snapshot to consume the same data is much more wasteful.
So it makes sense now to introduce explicitly the concept of dependent
context.

To move forward and make the ownership more explicit, we add an
internal reference count in context. This is used only to track the
context setup/teardown process.

When contexts are reused, we always have a parent module -which creates
and owns the context- which passes its context to a subordinate module,
because the parent wants to consume the service offered by the
subordinate. The subordinate (possibly further nested, or more than one)
should never attempt Setup() and Teardown(). These are responsabilities
of the parent, outer module, which owns the context.

It's impractical to conditionally call Setup() and Teardown(), so the
functions internally use and check the reference count to do the right
thing automatically.

The following are are idempotent (and safe and quickly return success)
- calling multiple times Setup() on a ready context
- calling multiple times Teardown() on a un-ready context

Signed-off-by: Francesco Romani <fromani@redhat.com>
ffromani added a commit to ffromani/ghw that referenced this pull request May 26, 2021
The PR jaypipes#236 fixes a snapshot
directory leakage issue, but also highlight a context
lifecycle/ownership issue.

In general in ghw each package creates its own context, including
the cases on which a package is consumed by another, for example
`topology` and `pci`. However, in this case, it would make more sense
to reuse the context from the parent package, because the two packages
are tightly coupled.

Before the introduction of the the transparent snapshot support
(jaypipes#202), the above mechanism worked,
because each context, each time, was consuming over and over again the
same parameters (e.g. environment variables); besides some resource
waste, this had no negative effect.

When introducing snapshots in the picture, repeatedly unpacking the
same snapshot to consume the same data is much more wasteful.
So it makes sense now to introduce explicitly the concept of dependent
context.

To move forward and make the ownership more explicit, we add an
internal reference count in context. This is used only to track the
context setup/teardown process.

When contexts are reused, we always have a parent module -which creates
and owns the context- which passes its context to a subordinate module,
because the parent wants to consume the service offered by the
subordinate. The subordinate (possibly further nested, or more than one)
should never attempt Setup() and Teardown(). These are responsabilities
of the parent, outer module, which owns the context.

It's impractical to conditionally call Setup() and Teardown(), so the
functions internally use and check the reference count to do the right
thing automatically.

The following are are idempotent (and safe and quickly return success)
- calling multiple times Setup() on a ready context
- calling multiple times Teardown() on a un-ready context

Signed-off-by: Francesco Romani <fromani@redhat.com>
ffromani added a commit to ffromani/ghw that referenced this pull request Jun 25, 2021
The PR jaypipes#236 fixes a snapshot
directory leakage issue, but also highlight a context
lifecycle/ownership issue.

In general in ghw each package creates its own context, including
the cases on which a package is consumed by another, for example
`topology` and `pci`. However, in this case, it would make more sense
to reuse the context from the parent package, because the two packages
are tightly coupled.

Before the introduction of the the transparent snapshot support
(jaypipes#202), the above mechanism worked,
because each context, each time, was consuming over and over again the
same parameters (e.g. environment variables); besides some resource
waste, this had no negative effect.

When introducing snapshots in the picture, repeatedly unpacking the
same snapshot to consume the same data is much more wasteful.
So it makes sense now to introduce explicitly the concept of dependent
context.

To move forward and make the ownership more explicit, we add an
internal reference count in context. This is used only to track the
context setup/teardown process.

When contexts are reused, we always have a parent module -which creates
and owns the context- which passes its context to a subordinate module,
because the parent wants to consume the service offered by the
subordinate. The subordinate (possibly further nested, or more than one)
should never attempt Setup() and Teardown(). These are responsabilities
of the parent, outer module, which owns the context.

It's impractical to conditionally call Setup() and Teardown(), so the
functions internally use and check the reference count to do the right
thing automatically.

The following are are idempotent (and safe and quickly return success)
- calling multiple times Setup() on a ready context
- calling multiple times Teardown() on a un-ready context

Signed-off-by: Francesco Romani <fromani@redhat.com>
ffromani added a commit to ffromani/ghw that referenced this pull request Sep 21, 2021
The PR jaypipes#236 fixes a snapshot
directory leakage issue, but also highlight a context
lifecycle/ownership issue.

In general in ghw each package creates its own context, including
the cases on which a package is consumed by another, for example
`topology` and `pci`. However, in this case, it would make more sense
to reuse the context from the parent package, because the two packages
are tightly coupled.

Before the introduction of the the transparent snapshot support
(jaypipes#202), the above mechanism worked,
because each context, each time, was consuming over and over again the
same parameters (e.g. environment variables); besides some resource
waste, this had no negative effect.

When introducing snapshots in the picture, repeatedly unpacking the
same snapshot to consume the same data is much more wasteful.
So it makes sense now to introduce explicitly the concept of dependent
context.

To move forward and make the ownership more explicit, we add an
internal reference count in context. This is used only to track the
context setup/teardown process.

When contexts are reused, we always have a parent module -which creates
and owns the context- which passes its context to a subordinate module,
because the parent wants to consume the service offered by the
subordinate. The subordinate (possibly further nested, or more than one)
should never attempt Setup() and Teardown(). These are responsabilities
of the parent, outer module, which owns the context.

It's impractical to conditionally call Setup() and Teardown(), so the
functions internally use and check the reference count to do the right
thing automatically.

The following are are idempotent (and safe and quickly return success)
- calling multiple times Setup() on a ready context
- calling multiple times Teardown() on a un-ready context

Signed-off-by: Francesco Romani <fromani@redhat.com>
ffromani added a commit to ffromani/ghw that referenced this pull request Sep 21, 2021
The PR jaypipes#236 fixes a snapshot
directory leakage issue, but also highlight a context
lifecycle/ownership issue.

In general in ghw each package creates its own context, including
the cases on which a package is consumed by another, for example
`topology` and `pci`. However, in this case, it would make more sense
to reuse the context from the parent package, because the two packages
are tightly coupled.

Before the introduction of the the transparent snapshot support
(jaypipes#202), the above mechanism worked,
because each context, each time, was consuming over and over again the
same parameters (e.g. environment variables); besides some resource
waste, this had no negative effect.

When introducing snapshots in the picture, repeatedly unpacking the
same snapshot to consume the same data is much more wasteful.
So it makes sense now to introduce explicitly the concept of dependent
context.

To move forward and make the ownership more explicit, we make the
context management more explict. We refactor the Do method of
context.Context as a top level function so we can pass it through
in all the NewWithContext functions. With these changes:

- the top-level packages, which owns the context, will keep working
  as usual with just a bit of extra verbosity - it will need to pass
  around `context.Do` which as implicit before
- the auxiliary packages, which do NOT own the context and thus should
  not act upon it, will be created by the top-level packages using
  `NewWithContext` with the new `context.Bypass` helper, which is
  a NOP from the context setup/teardown perspective.

Signed-off-by: Francesco Romani <fromani@redhat.com>
ffromani added a commit to ffromani/ghw that referenced this pull request Sep 23, 2021
The PR jaypipes#236 fixes a snapshot
directory leakage issue, but also highlight a context
lifecycle/ownership issue.

In general in ghw each package creates its own context, including
the cases on which a package is consumed by another, for example
`topology` and `pci`. However, in this case, it would make more sense
to reuse the context from the parent package, because the two packages
are tightly coupled.

Before the introduction of the the transparent snapshot support
(jaypipes#202), the above mechanism worked,
because each context, each time, was consuming over and over again the
same parameters (e.g. environment variables); besides some resource
waste, this had no negative effect.

When introducing snapshots in the picture, repeatedly unpacking the
same snapshot to consume the same data is much more wasteful.
So it makes sense now to introduce explicitly the concept of dependent
context.

To move forward and make the ownership more explicit, we add a
new function `NewWithContext` in all the subpackages.
The function `NewWithContext` will assume the context it gets
as argument is ready to be consumed and it will use as-is, attempting
no setup or teardown in any circumstances.

The usage rules are pretty simple:
1. In your client code, you most likely just need to use `New` as usual.
2. When consuming a package from another (e.g. `toppology` from `pci`),
   always use the `NewWithContext` function to get a handle for
   the auxiliary package, passin through the context from the top-level
   package.
3. Within a `NewWithContext` function, ally calls to other
   `NewWithContext` functions should be made to preserve the properties.

Signed-off-by: Francesco Romani <fromani@redhat.com>
ffromani added a commit to ffromani/ghw that referenced this pull request Sep 23, 2021
The PR jaypipes#236 fixes a snapshot
directory leakage issue, but also highlight a context
lifecycle/ownership issue.

In general in ghw each package creates its own context, including
the cases on which a package is consumed by another, for example
`topology` and `pci`. However, in this case, it would make more sense
to reuse the context from the parent package, because the two packages
are tightly coupled.

Before the introduction of the the transparent snapshot support
(jaypipes#202), the above mechanism worked,
because each context, each time, was consuming over and over again the
same parameters (e.g. environment variables); besides some resource
waste, this had no negative effect.

When introducing snapshots in the picture, repeatedly unpacking the
same snapshot to consume the same data is much more wasteful.
So it makes sense now to introduce explicitly the concept of dependent
context.

To move forward and make the ownership more explicit, we add a
new function `NewWithContext` in all the subpackages.
The function `NewWithContext` will assume the context it gets
as argument is ready to be consumed and it will use as-is, attempting
no setup or teardown in any circumstances.

The usage rules are pretty simple:
1. In your client code, you most likely just need to use `New` as usual.
2. When consuming a package from another (e.g. `toppology` from `pci`),
   always use the `NewWithContext` function to get a handle for
   the auxiliary package, passin through the context from the top-level
   package.
3. Within a `NewWithContext` function, ally calls to other
   `NewWithContext` functions should be made to preserve the properties.

Signed-off-by: Francesco Romani <fromani@redhat.com>
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