Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Use a pool of rand.Source to reduce lock contention #357

Merged
merged 1 commit into from
Dec 12, 2018

Conversation

objectiser
Copy link
Contributor

@objectiser objectiser commented Dec 12, 2018

when creating span ids

Signed-off-by: Gary Brown [email protected]

Which problem is this PR solving?

Resolves #353 - possibly :)

Short description of the changes

Instead of using a lock around the random number generation, a pool of rand.Sources is maintained.

@ghost ghost assigned objectiser Dec 12, 2018
@ghost ghost added the review label Dec 12, 2018
@objectiser
Copy link
Contributor Author

Running a test creating 5 million spans, using the existing and new approach, shows that generally they take the same time (8 to 9 seconds) - however on occasions the existing approach resulted in runs of 18 seconds.

Checking the mutex profiling showed far less time spent - on the pool, rather than the mutex around random number generation.

@yurishkuro If the implementation looks ok, is it worth asking the Istio team to try it out before we consider merging?

@codecov
Copy link

codecov bot commented Dec 12, 2018

Codecov Report

Merging #357 into master will increase coverage by 0.02%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #357      +/-   ##
==========================================
+ Coverage   87.19%   87.22%   +0.02%     
==========================================
  Files          54       54              
  Lines        2999     3005       +6     
==========================================
+ Hits         2615     2621       +6     
  Misses        272      272              
  Partials      112      112
Impacted Files Coverage Δ
tracer.go 90.09% <100%> (+0.27%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 95576ea...4b92ed8. Read the comment docs.

@yurishkuro yurishkuro changed the title Use a pool of rand.Source to reduce lock contention when creating spa… Use a pool of rand.Source to reduce lock contention Dec 12, 2018
seedGenerator := utils.NewRand(time.Now().UnixNano())
pool := sync.Pool{
New: func() interface{} {
return rand.NewSource(seedGenerator.Int63())
Copy link
Member

Choose a reason for hiding this comment

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

could we bump this to use int64? I think it's now available from the stdlib.

@yurishkuro yurishkuro merged commit a9d03b8 into jaegertracing:master Dec 12, 2018
@ghost ghost removed the review label Dec 12, 2018
@yurishkuro
Copy link
Member

sorry, already merged - def. worth asking them to try, but overall seems like a worthy improvement.

@objectiser objectiser deleted the contention branch December 12, 2018 18:22
seedGenerator := utils.NewRand(time.Now().UnixNano())
pool := sync.Pool{
New: func() interface{} {
return rand.NewSource(seedGenerator.Int63())
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry to see this late, but I'm not sure I fully understand the logic here. Perhaps I'm missing something...

If the underlying issue is lock contention, how does moving to a sync.Pool solve the issue? Get() still involves a mutex, correct? And inside of that, we are still calling Int63() on a locked source, correct?

It feels like we want something that is lock-free. Have I missed something obvious?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True there is a mutex on the pool, but the pool is returning a rand.Source which is not a locked resource. So there is no lock being held while the random numbers are being generated.

Copy link
Contributor

Choose a reason for hiding this comment

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

Do we believe that getting a source is quicker than generating the numbers?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

From some basic tests, as described here #357 (comment), the benefit was more related to consistent results.

Not sure if @mandarjog has had a chance to try the latest version in the same test scenario to see whether it performed better?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants