prevent devious scheduling from reporting negative size to kamon #624
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.
ConcurrentLinkedQueue.add is not atomic with respect to AtomicInteger.getAndIncrement. I think we should increment the size variable before we call
add
to address this race condition:Thread A is executing the
flush()
loop.Thread B calls
flushQ.add
, the channel is added, but the thread is interrupted before incrementingsize
.Thread A empties the queue and now
flushQ.size
is actually-1
.Thread A takes a 1ms nap while thread B remains suspended.
Thread A wakes up and enters flush() again, assigning
flushQ.estimateSize()
(which is still-1
) toqSize
Thread B finally resumes and corrects the
size
variable, but the spurious negative value has already been captured and will be reported to kamon.A negative could spike the y-axis scale if grafana or another process in the reporting chain is configured to expect unsigned values.