Registry Mirror
Skip to content

Conversation

@carlosthe19916

@carlosthe19916 carlosthe19916 commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes: #7940

Addresses: https://redhat.atlassian.net/browse/PULP-2150

Problem

KEDA scales pulp-workers from OpenTelemetry waiting_tasks (emitted by Redis worker record_waiting_tasks_metric). Today that is roughly (WAITING + RUNNING) − workers, which ignores resource locks. Under exclusive contention, queue depth ≫ useful parallelism → over-scale → DB pool saturation → crash loops / orphan Redis locks (incident 2026-07-24).

Fix direction: the metric

def record_waiting_tasks_metric(self):
must reflect how many tasks can usefully run in parallel (resource-aware demand), not raw unfinished count.

Solution

  • First commit: creates the test to verify the metric count is the expected one -> expected to fail
  • Second commit: refactors the logic for counting the metrics -> tests expected to pass
  • Third commit: add more tests

📜 Checklist

  • Commits are cleanly separated with meaningful messages (simple features and bug fixes should be squashed to one commit)
  • A changelog entry or entries has been added for any significant changes
  • Follows the Pulp policy on AI Usage
  • (For new features) - User documentation and test coverage has been added

See: Pull Request Walkthrough

@carlosthe19916
carlosthe19916 marked this pull request as ready for review July 30, 2026 12:15
@carlosthe19916
carlosthe19916 force-pushed the fix/PULP-2150 branch 2 times, most recently from 5eb4933 to be2ef80 Compare July 31, 2026 06:32
return int(process.stdout.decode().strip())


def test_waiting_tasks_metric_resource_contention(dispatch_task, pulpcore_bindings):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The pulpcore-manager shell subprocess isn't needed here. Since the functional tests already run inside a Django-initialized environment, you can import and call the function directly:

from pulpcore.tasking.redis_worker import count_waiting_tasks_for_metric

def _read_waiting_tasks_metric_value(num_workers=1):
    return count_waiting_tasks_for_metric(num_workers)

This avoids spawning a full Django process six times across the test suite and makes failures easier to debug (you get a real traceback instead of stderr from a subprocess).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, the last commit is importing directly count_waiting_tasks_for_metric


@pytest.mark.long_running
@pytest.mark.parallel
def test_worker_cleanup_on_missing_worker(dispatch_task, monitor_task, pulpcore_bindings):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Missing test case: shared-shared concurrency. The three tests here cover exclusive-exclusive, two independent exclusive lanes, and exclusive-blocks-shared — but none verify that multiple tasks needing only shared access to the same resource are all counted as parallel lanes.

That's the core correctness property of shared locks. If someone accidentally added shared resources to taken_exclusive in the algorithm, only this test would catch it.

Something like: dispatch N tasks all using shared_resources=[same_resource] (no exclusive holder), and assert the metric rises by N, not 1.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, the last test is adding this scenario. Thank for pointing that out!

@carlosthe19916

Copy link
Copy Markdown
Contributor Author

@dkliban I addressed your comments! Thanks for the review!

@gerrod3 gerrod3 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I also think this needs a changelog

Comment thread pulpcore/tasking/redis_worker.py Outdated

Counts how many WAITING/RUNNING tasks older than five seconds can run at the
same time given exclusive/shared resource reservations, then subtracts
``num_workers``. This value is published as the OpenTelemetry ``waiting_tasks``

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Use single ticks for comments and changelogs.

Comment on lines +143 to +144
if conflicts:
continue

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Tasks are FIFO. So if Task 1 takes resource A & B and Task 2 requires B & C and Task 3 requires C, then both Task 2 & 3 will be blocked even though resource C hasn't been taken yet.

So update the taken_exclusive and taken_shared for each task no matter the conflict.

Comment thread pulpcore/tasking/redis_worker.py Outdated
taken_exclusive.update(exclusive_resources)
taken_shared.update(shared_resources)

return parallel_count - num_workers

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does this need to do the minus num_workers? Why can't the calling function do this math?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, good point. I'll make the caller do the math

Comment thread pulpcore/tasking/redis_worker.py Outdated
Counts how many WAITING/RUNNING tasks older than five seconds can run at the
same time given exclusive/shared resource reservations, then subtracts
``num_workers``. This value is published as the OpenTelemetry ``waiting_tasks``
gauge used by KEDA.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What is KEDA? If it is operator related, not sure it should be in the pulpcore comment docs.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You're right. I am removing that terminology. I am replacing it by OpenTelemetry as this metric will serve OpenTelemetry

Note: KEDA = Kubernetes Event-Driven Autoscaling

Comment on lines +113 to +119
Setup: hold one exclusive resource with a long-running sleep, then enqueue many
more sleeps that need the same exclusive resource (they stay waiting). After the
metric's age cutoff, the published demand for this contention must rise by one
(a single runnable lane), not by one plus every waiter.

That is the signal KEDA should scale on: workers help only when work is
runnable, not when it is blocked on a held lock.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not sure we need this long comment before the test. The test should self-describe what it is doing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I made sure the long comments are gone

Comment on lines +267 to +269
def test_waiting_tasks_metric_exclusive_blocks_shared_waiters(
dispatch_task, pulpcore_bindings, _read_waiting_tasks_metric_value
):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not sure this needs another test. We could just move it inside one of the previous test and have some of the queued tasks use shared_resources.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

@carlosthe19916

Copy link
Copy Markdown
Contributor Author

@gerrod3 thanks for your review. I think I addressed all your comments.
I was tempted to squash all my commits in a single commit but I wanted to hold on until you do second round of review.

Thanks again!

@gerrod3 gerrod3 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good, squash then lgtm.

Signed-off-by: Carlos Feria <2582866+carlosthe19916@users.noreply.github.com>
@carlosthe19916

Copy link
Copy Markdown
Contributor Author

Looks good, squash then lgtm.

@gerrod3 commits squashed! Thanks a lot!

@gerrod3
gerrod3 merged commit ef82525 into pulp:main Aug 4, 2026
13 of 14 checks passed
@patchback

patchback Bot commented Aug 4, 2026 •

Copy link
Copy Markdown

Backport to 3.115: 💚 backport PR created

✅ Backport PR branch: patchback/backports/3.115/ef82525a5ad17dd646a84637b563ce3888f71c70/pr-7938

Backported as #7943

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

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve RedisWorker auto-scaling with resource-aware task metrics

3 participants