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

proxmox inventory: proposal for #9710 (caching) #9760

Merged
merged 4 commits into from
Feb 17, 2025

Conversation

iqt4
Copy link
Contributor

@iqt4 iqt4 commented Feb 16, 2025

SUMMARY

Updated cache logic according to Anible's dynamic inventory description

Fixes #9710

bugfixes:

  • proxmox inventory plugin did not update inventory correctly after meta refresh_inventory.
ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

proxmox inventory

ADDITIONAL INFORMATION

Details see #9710


@ansibullbot

This comment was marked as outdated.

@ansibullbot
Copy link
Collaborator

@ansibullbot ansibullbot added bug This issue/PR relates to a bug ci_verified Push fixes to PR branch to re-run CI inventory inventory plugin needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR new_contributor Help guide this first time contributor plugins plugin (any type) labels Feb 16, 2025
@felixfontein felixfontein changed the title Proposal for #9710 proxmox inventory: proposal for #9710 (caching) Feb 16, 2025
@felixfontein felixfontein added check-before-release PR will be looked at again shortly before release and merged if possible. backport-10 Automatically create a backport for the stable-10 branch labels Feb 16, 2025
Copy link
Collaborator

@felixfontein felixfontein left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution! I've added some comments below, I'd change the flow slightly so that the cache is still used if both update_cache and use_cache are true.

Also, can you please add a changelog fragment? Thanks.

@ansibullbot ansibullbot removed the ci_verified Push fixes to PR branch to re-run CI label Feb 16, 2025
@ansibullbot

This comment was marked as outdated.

@ansibullbot ansibullbot added the ci_verified Push fixes to PR branch to re-run CI label Feb 16, 2025
@ansibullbot ansibullbot removed the ci_verified Push fixes to PR branch to re-run CI label Feb 16, 2025
@ansibullbot ansibullbot removed the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR label Feb 16, 2025
@felixfontein felixfontein added the backport-9 Automatically create a backport for the stable-9 branch label Feb 16, 2025
Copy link
Collaborator

@felixfontein felixfontein left a comment

Choose a reason for hiding this comment

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

Looks good from my side. @Thulium-Drake if it's ok for you as well I'll merge.

self._cache[self.cache_key][url] = data

return make_unsafe(self._cache[self.cache_key][url])
self._results[url] = data
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure about this - the _get_json() method is used multiple times during the execution of "higher" level _populate(). This looks similar to a "global" variable that is used to go behind the logic of the program. data is already being returned by the method (and it is marked unsafe as it should). It is completely out of the scope of what this method is supposed to do.

IMO this is a very cheap solution to the short term problem and an expensive problem in the long term.

Copy link
Collaborator

Choose a reason for hiding this comment

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

How does this differ from what the code was doing before? Except that it was using self._cache[self.cache_key][url] instead of self._results[url].

Also yes, the cache is a global variable and that's how the cache should work, shouldn't it? How else should it cache?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I would argue that it was already a poor design (IMO) before :-) Literally ALL urls passed to _get_json() are being stored and that includes more than just the URLs containing inventory data.

That being said, I believe this method should not be concerned with storing its results in the "global" cache. Some other method on a higher level should be doing that.

One other point worth of mention is the make_unsafe(). I am not familiar with the semantics of this and I can only make inferences based on the name, but if this data is unsafe in anyway, why is a raw copy of it being stored unchecked in the cache for global consumption? Shouldn't it be marked unsafe as well?

I won't be able to produce a better design at short notice, but I would definitely like to take a look at that later on.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, that's a different story :) The thing is that that's harder to change, since it is a breaking change (in the sense that existing caches will stop working and we need to make sure that the plugin won't produce garbage out when finding a cache from an older version, or alternatively an older version finding a cache from a newer version). This is likely also something we should only merge for a new major release, just in case :)

@Thulium-Drake
Copy link
Contributor

As a general note, this could mean that the module this module is originally based on also needs some love to fix cache handling. As I (5 years ago already 😮) copied that module as a basis to build this one.

I've opened an issue on the foreman repo, maybe @evgeni has a good idea about @russoz 's points :-)

@Thulium-Drake
Copy link
Contributor

I did some digging in the changes made to the source module since the fork, these commits might be of interest/inspiration (by the sole virtue of having something to do with caching):

@iqt4
Copy link
Contributor Author

iqt4 commented Feb 17, 2025

Thank you for your remarks.

I agree, the solution may be called very cheap ;) but it is an effective bugfix without rewriting the plugin.

The reference design forsees the steps:

  1. attempt to read from cache
  2. if not possible, read from source
  3. update cache if required
  4. populate inventory object

This plugin bundled steps 1-3 "below" the _populate() function. The proposed bugfix moves the cache update (3) after _populate().

A further unbundling of reading (cached) data from source and populating the inventory object requires some redesign.
The challenge is that the urls are dynamic depending on the status of the proxmox host, e.g.

if status_key not in properties or not properties[status_key] == 'running':
return
ret = self._get_json(f"{self.proxmox_url}/api2/json/nodes/{node}/lxc/{vmid}/interfaces", ignore_errors=[501])

I.e. a logic is required to a) dynamically read the (cached) data from source and b) dynamically populate the inventory object

ps: the foreman plugin still suffers the same issue

@felixfontein felixfontein merged commit d696bb7 into ansible-collections:main Feb 17, 2025
138 checks passed
Copy link

patchback bot commented Feb 17, 2025

Backport to stable-9: 💚 backport PR created

✅ Backport PR branch: patchback/backports/stable-9/d696bb7b8992bfc6535c5e51dd37a00b9b4e22df/pr-9760

Backported as #9769

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

patchback bot pushed a commit that referenced this pull request Feb 17, 2025
* Proposal for #9710

* Fixed comments

* Fixed trailing whitespace

* Fixed changelog fragment

(cherry picked from commit d696bb7)
Copy link

patchback bot commented Feb 17, 2025

Backport to stable-10: 💚 backport PR created

✅ Backport PR branch: patchback/backports/stable-10/d696bb7b8992bfc6535c5e51dd37a00b9b4e22df/pr-9760

Backported as #9770

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

patchback bot pushed a commit that referenced this pull request Feb 17, 2025
* Proposal for #9710

* Fixed comments

* Fixed trailing whitespace

* Fixed changelog fragment

(cherry picked from commit d696bb7)
@felixfontein felixfontein removed the check-before-release PR will be looked at again shortly before release and merged if possible. label Feb 17, 2025
@felixfontein
Copy link
Collaborator

@iqt4 thanks for fixing this!
@Thulium-Drake @russoz thanks for reviewing!

@russoz
Copy link
Collaborator

russoz commented Feb 17, 2025

Thanks @Thulium-Drake @iqt4 @felixfontein for the comments. As I wrote before I am interested in looking into that but when time allows it, which won't be soon.

felixfontein pushed a commit that referenced this pull request Feb 17, 2025
…#9710 (caching) (#9769)

proxmox inventory: proposal for #9710 (caching) (#9760)

* Proposal for #9710

* Fixed comments

* Fixed trailing whitespace

* Fixed changelog fragment

(cherry picked from commit d696bb7)

Co-authored-by: Dirk S. <[email protected]>
felixfontein pushed a commit that referenced this pull request Feb 17, 2025
#9710 (caching) (#9770)

proxmox inventory: proposal for #9710 (caching) (#9760)

* Proposal for #9710

* Fixed comments

* Fixed trailing whitespace

* Fixed changelog fragment

(cherry picked from commit d696bb7)

Co-authored-by: Dirk S. <[email protected]>
@iqt4 iqt4 deleted the Promox-inventory branch February 17, 2025 19:22
rt-vnx pushed a commit to rt-vnx/community.general that referenced this pull request Feb 20, 2025
…nsible-collections#9760)

* Proposal for ansible-collections#9710

* Fixed comments

* Fixed trailing whitespace

* Fixed changelog fragment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-9 Automatically create a backport for the stable-9 branch backport-10 Automatically create a backport for the stable-10 branch bug This issue/PR relates to a bug has_issue inventory inventory plugin new_contributor Help guide this first time contributor plugins plugin (any type)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

proxmox inventory: refresh_inventory not working with cache enabled
5 participants