Skip to content

Spawn a task in oximeter to actually do the collection - #7097

Merged
bnaecker merged 5 commits into
mainfrom
spawn-perform-collection
Nov 22, 2024
Merged

bnaecker merged 5 commits into
mainfrom
spawn-perform-collection

Conversation

@bnaecker

Copy link
Copy Markdown
Collaborator

oximeter currently starts up a task for each producer. When their collection interval expires, that task directly makes an HTTP request to the producer to collect its data, in-line in the same task. This can cause problems, especially if there are concurrent updates to the producer's address information. The main oximeter HTTP server will send a message to that task in that case, asking it to update its producer information, but that task might be off in lalaland making an HTTP request. This is particularly bad since that HTTP request may never complete -- the producer's address might be updated, so the old one is defunct, possibly forever!

This commit addresses this by spawning a new task to actually run the collection itself. This keeps the first-level task alive and responsive, such that it can abort any previously-spawned collections if there are updates. We also replace any existing task if a collection was explicitly requested, but we ignore existing collections (rather than spawning a new one) if the timer expires while one is already running.

This resolves test flakes described in #6901.

`oximeter` currently starts up a task for each producer. When their
collection interval expires, that task directly makes an HTTP request to
the producer to collect its data, _in-line_ in the same task. This can
cause problems, especially if there are concurrent updates to the
producer's address information. The main `oximeter` HTTP server will
send a message to that task in that case, asking it to update its
producer information, but that task might be off in lalaland making an
HTTP request. This is particularly bad since that HTTP request may never
complete -- the producer's address might be updated, so the old one is
defunct, possibly forever!

This commit addresses this by spawning a new task to actually run the
collection itself. This keeps the first-level task alive and responsive,
such that it can abort any previously-spawned collections if there are
updates. We _also_ replace any existing task if a collection was
explicitly requested, but we ignore existing collections (rather than
spawning a new one) if the timer expires while one is already running.

This resolves test flakes described in #6901.
@bnaecker
bnaecker requested a review from jgallagher November 18, 2024 23:47
@bnaecker

bnaecker commented Nov 19, 2024 •

Copy link
Copy Markdown
Collaborator Author

Some of the self-stat counter tests are failing, e.g., here. These have always been a bit flaky, so I'm not that surprised that adding another layer of spawning has made them more so. I've set up a small run-loop locally, which just runs these failing tests in a loop until they fail. I can get them both to fall over within about a minute, usually around 60 runs.

I'm going to fix them in this PR. The whole point of this is to remove a test flake, so that'd be pretty annoying to introduce more flakes :) I've taken a page from @jgallagher's book, and created a small test Dropshot server in place of the httpmock-based server the tests use on main. That gives much more precise control over the what the servers do in each handler, and also makes it much easier to get ground-truth around the number of times the server has responded in various ways. Doing that for the test_self_stat_collection_test, I've not hit any flakes with the run loop after about 10 minutes, so at least an order of magnitude more runs than previously. I'll let it keep running while I fix the other tests, and report back on this PR.

Comment thread oximeter/collector/src/agent.rs Resolved
Comment thread oximeter/collector/src/agent.rs Resolved
@bnaecker

Copy link
Copy Markdown
Collaborator Author

@jgallagher and I chatted at length about this. I'm going to push some big changes to this branch, but the summary is:

  • spawn another long-running task to do the collections
  • that task waits for either a timer-triggered or explicit collection request, both are treated the same. These are queued by the first task, with a pretty small queue-size limit (1, even).
  • once that task receives a request for work, it starts a select! loop that: waits for the work / collection to complete or timeout, and waits for updates to the producer endpoint information. The latter is forwarded in a watch channel from the first task, since we only care about the latest value here and don't care if anyone is explicitly listening.
  • If this task gets an update while a collection is running, it cancels and recreates the collection with the updated information. This is to avoid returning an annoying error that says, "I have all the information I need to retry your request, but I'm going to make you do it instead."

- Remove the previous, hacky manual `Future` that represented a spawned
  collection task. Instead, spawn a new long-running task that we
  communicate with to do the collections, which can be interrupted with
  an update to the producer endpoint if needed.
- Separate queues for timer-based and forced / triggered collections.
  Each has a queue depth of 1 currently, but we handle full queues
  differently. For force-collections, we return an error back to the
  caller in this case -- the assumption is that this shouldn't happen,
  since it's always used in test code, and we want to fail the
  collection entirely. For timer-based collections, we bump a new
  failure counter and log an error, and then let the queued collections
  operate as usual.
- Rename `Oximeter::force_collect()` to `Oximeter::try_force_collect()`
  and make it fallible to reflect the possibility of a queue-full error

@jgallagher jgallagher 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.

LGTM, thanks! This was quite a bit of work. I'm a little worried we've still missed some edge case, but there's a lot of logging in here so if so, hopefully it's easy to identify.

Comment thread oximeter/collector/src/agent.rs Resolved
Comment thread oximeter/collector/src/agent.rs Resolved
Comment thread oximeter/collector/src/agent.rs Resolved
Comment thread oximeter/collector/src/agent.rs Resolved
@bnaecker

Copy link
Copy Markdown
Collaborator Author

Thanks for the review @jgallagher!

I'm a little worried we've still missed some edge case

I've been waffling on adding a lot more tests here. The upshot of separating the two tasks out is that it should be easier to test them in isolation. Do you think it's worthwhile to do that? I don't want to bloat the PR, but now that we've seen a few pretty nasty bugs in this file, it seems like a good investment.

- Send error if we fail to send a force-collection request because the
  channel is closed
- Rework joining all the force-collection futures to ensure they all run
- Copy producer endpoint rather than clone
@bnaecker
bnaecker merged commit df13cc7 into main Nov 22, 2024
@bnaecker
bnaecker deleted the spawn-perform-collection branch November 22, 2024 01:05
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