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

Feature indirect host counting #15802

Open
wants to merge 19 commits into
base: devel
Choose a base branch
from

Conversation

AlanCoding
Copy link
Member

SUMMARY

At the moment, this feature is delivering a table IndirectManagedNodeAudit. It is generated after a job runs, and has a cleanup policy and things like that. This will be processed later on, ping @Ladas , to generate analytics, like a count of the "real" hosts that were interacted with.

ISSUE TYPE
  • New or Enhanced Feature
COMPONENT NAME
  • API

@github-actions github-actions bot added component:api dependencies Pull requests that update a dependency file labels Jan 31, 2025
Copy link

codecov bot commented Jan 31, 2025

Codecov Report

Attention: Patch coverage is 78.16377% with 88 lines in your changes missing coverage. Please review.

Project coverage is 75.32%. Comparing base (4487f2a) to head (cbe6d4c).

✅ All tests successful. No failed tests found.

❌ Your patch check has failed because the patch coverage (78.16%) is below the target coverage (100.00%). You can increase the patch coverage or adjust the target coverage.

awx/main/tasks/host_indirect.py Outdated Show resolved Hide resolved

# From this jq result (specific to a single Ansible module), get index information about this host record
if not data.get('canonical_facts'):
if not facts_missing_logged:
Copy link
Contributor

Choose a reason for hiding this comment

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

Is not this the opposite? Shouldn't it be:

Suggested change
if not facts_missing_logged:
if facts_missing_logged:

Copy link
Member Author

Choose a reason for hiding this comment

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

There is something wrong here, another great catch.

But let me explain more of the intent - this will process a lot of events. It's extremely possible that the event_query.yml file, which comes from the collection, is malformed. It could have any number of things wrong with it, but that error will repeat for every single event processed against that query. But also, if a job has 1M events, we don't necessarily want to exit the entire processing loop because event 928 had an error. So, if this type of error happens, it is only logged once. The variable is to say "we already logged this once", and if we have logged it once already, we will not log again.

try:
hashable_facts = get_hashable_form(canonical_facts)
except UnhashableFacts:
if not unhashable_facts_logged:
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here?

Suggested change
if not unhashable_facts_logged:
if unhashable_facts_logged:

except UnhashableFacts:
if not unhashable_facts_logged:
logger.info(f'Could not hash canonical_facts {canonical_facts}, skipping')
unhashable_facts_logged = True
Copy link
Contributor

Choose a reason for hiding this comment

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

Not following why this is changed :(

awx/main/tasks/host_indirect.py Outdated Show resolved Hide resolved
save_indirect_host_entries(job.id, wait_for_events=True)
job_ct += 1
if job_ct:
logger.info(f'Restarted event processing for {job_ct} jobs')
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this info level? I would put this on debug and/or mention it is related to indirect host counting. As a system administrator I don't think I want to see information about how data is collected internally in my logs.

Copy link
Member Author

Choose a reason for hiding this comment

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

Well it's an hourly schedule. And yeah, default logging is INFO level. Maybe this is just me, but as long as it's a single-line log, I would like to see some kind of log reporting for any schedule that's hourly or more infrequent.

compiled_jq_expressions = {} # Cache for compiled jq expressions
facts_missing_logged = False
unhashable_facts_logged = False
for event in job.job_events.filter(task__in=job_event_queries.keys()).iterator():
Copy link
Contributor

@pb82 pb82 Feb 5, 2025

Choose a reason for hiding this comment

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

@AlanCoding I have tested this with the VMWare collection, but it looks like none of the job events match that query: the task name is either empty or doesn't match the module name.

for data in compiled_jq.input(event.event_data['res']).all():

# From this jq result (specific to a single Ansible module), get index information about this host record
if not data.get('canonical_facts'):
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder should we use the json schema here to check the response is valid or is it overkill?

In my schema proposal I am also listy device_type under facts to be mandatory, so we can sort out which groups of devices we have in https://github.com/ansible/handbook/pull/181/files#diff-aa1240cabb636a0facdb972a621d8c8f3f46ace92838bd9a6ecfb55a5d9aabd9R197

# Gate running this task on the job having all events processed, not just EOF or playbook_on_stats
current_events = 0
for _ in range(10):
current_events = job.job_events.count()
Copy link
Contributor

Choose a reason for hiding this comment

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

we had a conversation about count effectivity. Count will have to scan all events of a job, that can take a lot of time. (O(n) complexity)

Rather use timestamp comparison, just getting max(timestamp) of job's events, which if covered by index is a O(1) lookup.

Copy link
Contributor

@Ladas Ladas Feb 6, 2025

Choose a reason for hiding this comment

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

that is if this translates to select count(*) from job_events where job_id=job.job_id AND job_created=job.job_created

if you could confirm this is the query that runs

Copy link
Contributor

Choose a reason for hiding this comment

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

since the job_events is partitioned. We need to make sure we use tje job_created in every query, to go into certain condition (which should be covered the Django ORM I believe)

We'd need to do a quick check what kind of index we'd need to do, to perform index scan on max(timestamp)

Copy link
Member Author

Choose a reason for hiding this comment

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

We are careful to use job.job_events specifically so that it'll be filtered by job_created, and thus, that partition.

This isn't quite what we want, and I have #15811 up, but I believe there are other issues with the test suite that needs to be fixed before that will pass. That would reduce the max from 10 queries to 1.

We could skip doing this altogether in the post_run_hook, if push comes to shove. Then we'll have to wait an hour or something for every job to be processed.

Rather use timestamp comparison, just getting max(timestamp) of job's events, which if covered by index is a O(1) lookup

I don't know if that can solve any problem we have. We only ever get here if the playbook_on_stats event is processed. But this covers a much more niche case where the final event (or EOF) is processed while mid-run events are still not yet saved. Events are not processed linearly. If they were, this polling loop could be deleted outright.

AlanCoding and others added 17 commits February 14, 2025 14:47
…on (#15774)

* Add jq dependency

* Add file in progress

* Add license for jq

* Write test and get it passing
* Callback plugin method from cmeyers adapted to global collection list

Get tests passing

Mild rebranding

Put behind feature flag, flip true in dev

Add noqa flag

* Add missing wait_for_events
* Minor import changes to collection processing in callback plugin

* Move agreed location of event_query file
…#15784)

* Rename the indirect host counting test file

* Combine artifacts saving logic
…15796)

* Document, implement, and test remaining indirect host audit fields

* Fix hashing
…15798)

* Wait for all event processing to finish, add fallback task

* Add flag check to periodic task
* By default, do not count indirect hosts

* Fix copy paste goof

* Fix linter issue from base branch
#15805)

prevent multiple tasks from processing the same job events, prevent periodic task from spawning another task per job
#15815)

* fix: rely on resolved_action instead of task, adapt to proposed query structure

* tests: update indirect host tests

* update remaining queries to new format

* update live test
* Remove polling loop for job finishing event processing

* Make awx/main/tests/live dramatically faster (#15780)
@pb82 pb82 force-pushed the feature_indirect-host-counting branch from f83a7c1 to cbe6d4c Compare February 14, 2025 13:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component:api dependencies Pull requests that update a dependency file
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants