-
Notifications
You must be signed in to change notification settings - Fork 325
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
Use java.time.Instant to get micros accurate timestamps #261
Use java.time.Instant to get micros accurate timestamps #261
Conversation
fall back to System.currentTimeMillis() for Java 7
Codecov Report
@@ Coverage Diff @@
## master #261 +/- ##
============================================
+ Coverage 72.96% 73.04% +0.07%
Complexity 1134 1134
============================================
Files 121 122 +1
Lines 4221 4240 +19
Branches 422 422
============================================
+ Hits 3080 3097 +17
- Misses 943 945 +2
Partials 198 198
Continue to review full report at Codecov.
|
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 it is better to avoid referencing Java 8 types and opt for reflection instead, otherwise you are dependent on JVM implementation of class linkage.
import java.time.Clock; | ||
import java.time.Instant; | ||
|
||
public interface SystemClock { |
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.
Let's mark with a TODO or something that will let us quickly find all places we can cut unnecessary code when dropping support to 1.7.
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.
We just have to look out for the usages of the @IgnoreJRERequirement
annotation. The animal sniffer maven plugin would not let us verify non-Java 7 code which is not annotated with this annotation.
final long epochMillis = System.currentTimeMillis(); | ||
epochTickClock.init(epochMillis, 0); | ||
final long epochMicros = SystemClock.ForJava8CapableVM.INSTANCE.getEpochMicros(); | ||
epochTickClock.init(epochMicros, 0); |
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.
Unit-test JRE 7 code as well (regardless of the actual JRE running the tests)
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
enum ForJava8CapableVM implements SystemClock { | ||
INSTANCE; | ||
|
||
private static final Clock clock = Clock.systemUTC(); |
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.
Could be risky - this class cannot be fully loaded (pass linkage phase) on JRE 7. This means that ForCurrentVM
cannot be fully linked as well as it depends on it. You may not experience any problem using a lazy-linkage JVM, one that allows only linking parts of the class's constant pool when required, but I am not sure it is safe to assume that will be the case in all JVMs as I don't think the lazy-linkage is part of the JVM spec.
Using reflection referencing a Method
or MethodHandle
would be a safer approach
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.
You are completely right - that would be safer. But we don't support such a VM so I would not want to pay the cost of the increased complexity and potentially higher overhead yet. We can't claim to support a particular VM if we don't have an integration test for it. If we are to support such a VM in the future at that time, we might even have already dropped Java 7 support so that we can just use Instant
right away.
Yeah, only read through the conversation today, so removed the comment on "thinking it is too early to use Java 8 stuff" :) |
fall back to System.currentTimeMillis() for Java 7