-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support the JDK's ThreadLocalRandom #1052
Conversation
562aeff
to
2f129eb
Compare
I dug out an old dusty JRE6 build (Travis doesn't support it anymore), and the tests passed locally. |
@dropwizard/dropwizard-metrics Could someone please review this? |
@@ -95,7 +95,7 @@ public void update(long value, long timestamp) { | |||
try { | |||
final double itemWeight = weight(timestamp - startTime); | |||
final WeightedSample sample = new WeightedSample(value, itemWeight); | |||
final double priority = itemWeight / ThreadLocalRandom.current().nextDouble(); | |||
final double priority = itemWeight / ThreadLocalRandomFactory.current().nextDouble(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternate approach is to switch to Java 7, but that would break Java 6 - compatibility, which I think it's not desirable for a maintenance release .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, we could do that for 3.2
2f129eb
to
284ff93
Compare
284ff93
to
7154ab4
Compare
* By default it tries to use the JDK's implementation and fallbacks to the internal | ||
* one if the JDK doesn't provide any. | ||
*/ | ||
class ThreadLocalRandomFactory { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be ThreadLocalRandomProxy
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a also a good name.
7154ab4
to
752d244
Compare
*/ | ||
private static class JdkDelegate { | ||
|
||
private static Random get() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets rename this to current
, just to be consistent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
752d244
to
406d280
Compare
This looks great. The only thing I was thinking about was trying to avoid the branch per call by making the JdkDelegate an instance class, creating a new delegate class for our backfill ThreadLocalRandom, and an interface that defines just the |
Add a proxy which provides a ThreadLocalRandom implementation depending on its availability in runtime. If the JDK provides ThreadLocalRandom, use it. Otherwise, fallback to the internal implementation.
406d280
to
4487e8b
Compare
That's fantastic! Thank you! You've tested this in JDK 6? I'm curious because just by looking at it, I'm not sure whether NoClassDefFound error would be thrown during the instantiation of JdkProvider, or if it would be thrown while calling |
Yes, I've run the suite in JDK6. If I add a
I suppose the exception is thrown when the |
Merge away! |
When is this fix release into maven repository? |
I think we could make a release this week. Seems like nothing remains in the queue for 3.1.3. |
* Support for publishing metrics of BigInteger and BigDecimal type * Update third-party.rst New reporter - metrics-munin-reporter * Add links to more third party libs * Update third-party to include metrics-circonus * Fix spelling in core.rst * #814 always close socket (cherry picked from commit 2762ce5) * Revert "Add log4j2 xml-based config support" This reverts commit 6667014. * Revert "Added log4j2 InstrumentedAppender xml support to docs" This reverts commit 26c4cd2. * Revert "Fix javadoc link" This reverts commit 28e6556. * Revert "Add CPU profiling support to the AdminServlet (fixes #927)" This reverts commit bbb52c1. * Revert "Support for publishing metrics of BigInteger and BigDecimal type" This reverts commit 7cfc23c. * Suppress all kinds of Exceptions raised by report() (#1049) (#1056) * Support the JDK's ThreadLocalRandom (#1052) Add a proxy which provides a ThreadLocalRandom implementation depending on its availability in runtime. If the JDK provides ThreadLocalRandom, use it. Otherwise, fallback to the internal implementation. * Support JDK's LongAdder (#1055) * Support the JDK's LongAdder Add an ability to use the JDK's implementation if it's available in the runtime. Use a proxy for creating LongAdders which tries to use the JDK's implementation and fallbacks to the internal one if the JDK doesn't provide any. Unfortunatelly, `LongAdder`s don't have a common interface, therefore we introduce `LongAdderAdapter` as a common interface for `LongAdder`s. It will be used in other components as an interface, but a concrete implementation of it will be created by `LongAdderProxy`. This pattern allows to us to still run code in the JRE, but enjoy the benefits of the stock `LongAdder` in modern JDKs (less bugs, better performance). * Run tests only on JDK8 We need a compile dependency on LongAdders which is not available in JDK7. * Retry DNS lookups (#1064) Do retry DNS lookups, which are just done in the constructor of InetSocketAddress. Reason might be that in some environments DNS is very dynamic and thus a name might be sometimes resolveable and sometimes not. The way this is currently implemented the InetSocketAddress is always cached, which in itself caches the return value of the last DNS lookup, even if that failed. * Backport rescale bug (#1046) * Added ExponentiallyDecayingReservoir test illustrating the rescale race condition. (cherry picked from commit d9af3c1) * Fixed the race condition in ExponentiallyDecayingReservoir's rescale method. (cherry picked from commit b022f04)
Ryan, as you wondered : "I'm not sure whether NoClassDefFound error would be thrown during the instantiation of JdkProvider, or if it would be thrown while calling JdkProvider.current", in fact, testing 3.1.4, on Weblogic 10.3 and Oracle JDK 6, the exception is thrown only when JdkProvider.current is called, not when JdkProvider constructor is called, so 3.1.4 is KO. Curiously, on Tomcat 7 and the same Oracle JDK 6, the exception is thrown when the JdkProvider constructor is called. I made a pull request for my tested workaround (#1129). |
Add a factory which provides a ThreadLocalRandom implementation depending on its availability in runtime. If the JDK provides ThreadLocalRandom, use it. Otherwise, fallback to the internal implementation.