Tracing: wait on an event instead of polling in the batch worker - #4695
Closed
rajarshidattapy wants to merge 1 commit into
Closed
Tracing: wait on an event instead of polling in the batch worker#4695rajarshidattapy wants to merge 1 commit into
rajarshidattapy wants to merge 1 commit into
Conversation
BatchTraceProcessor's worker thread slept 200 ms at a time and re-checked its conditions, so an otherwise idle process woke five times a second for the rest of its lifetime - with the default 5 s schedule_delay, 24 of every 25 wakeups did nothing. time.sleep() is also not interruptible, so shutdown() waited out the remainder of the interval before draining. The worker now blocks on a wake event until its next scheduled export. Producers set it once the queue reaches the export trigger size and shutdown() sets it to break out immediately, so the queue-size trigger and shutdown stay as responsive as before while an idle process wakes once per schedule_delay.
Member
|
Please refer to #4688 (comment) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4688.
Problem
BatchTraceProcessor's export thread polled in a fixed 200 ms loop:The thread starts lazily on the first trace/span and never stops, so for the rest of the process lifetime it woke five times a second and did nothing. With the default
schedule_delay = 5.0, 24 of every 25 wakeups were pure overhead — constant background CPU in profiles of an idle agent service, and timer wakeups on battery-powered hosts.time.sleep()is also not interruptible by the shutdown event, soshutdown()waited out the rest of the interval before the final drain even with an empty queue.Fix
The worker now blocks until it actually has something to do. It waits on a
threading.Eventwith a timeout of "time until the next scheduled export", so an idle process wakes once perschedule_delayinstead of 25 times.Both of the things that previously needed polling to be noticed now signal the event explicitly:
on_trace_start/on_span_endset the event once the queue reaches_export_trigger_size, so the immediate-export path is as responsive as before (more so: it no longer waits for the next poll tick);shutdown()sets it alongside_shutdown_event, so the worker leaves the loop and drains immediately.This is the second shape offered in the issue rather than the
min(0.2, ...)one-liner: capping the wait at 200 ms would have fixed the shutdown latency but left the idle wakeups — the main complaint — in place. It keeps the existing drain logic untouched, unlike blocking onqueue.Queue.get.There is no lost-wakeup window. A
set()that lands while the worker is exporting, or between itswait()returning and itsclear(), is covered by the queue-size check at the top of the next iteration — the condition that caused theset()is re-read there, not assumed.As the issue notes,
BackendSpanExporter._sleep_before_retryin this same file already waits on its shutdown event, so this brings the worker in line with it.Tests
test_batch_trace_processor_queue_trigger_wakes_the_idle_worker— withschedule_delay=60, a sub-trigger queue leaves the worker parked and reaching the trigger wakes it. Hangs to failure if the producer-side signal is dropped.test_batch_trace_processor_shutdown_wakes_the_idle_worker—shutdown()on a parked worker joins promptly and still drains the queued span, instead of timing out and warning.test_batch_trace_processor_schedule_uses_monotonic_clockdrove_runthrough itstime.sleepcalls, so it now drives it through the wake event'swaitinstead. ItsControlledTime.sleepraises, which makes the test fail if polling is ever reintroduced into the loop.Each was verified to fail against a deliberately broken version of the corresponding change.