Skip to content

Commit

Permalink
HSEARCH-4069 Make tookTime returing an accurate value
Browse files Browse the repository at this point in the history
  • Loading branch information
fax4ever authored and yrodiere committed Nov 5, 2020
1 parent 33201bd commit dea9576
Showing 1 changed file with 15 additions and 6 deletions.
Expand Up @@ -62,7 +62,8 @@ public enum Type {
protected final Type type;
private final DynamicDeadline deadline;

private Long start;
private Long monotonicTimeEstimateStart;
private Long nanoTimeStart;

public TimeoutManager(TimingSource timingSource, Long timeoutValue, TimeUnit timeoutUnit, Type type) {
this.timingSource = timingSource;
Expand All @@ -79,11 +80,13 @@ public TimeoutManager(TimingSource timingSource, Long timeoutValue, TimeUnit tim
* we start counting from this method call (if needed)
*/
public void start() {
this.start = timingSource.monotonicTimeEstimate();
this.monotonicTimeEstimateStart = timingSource.monotonicTimeEstimate();
this.nanoTimeStart = timingSource.nanoTime();
}

public void stop() {
this.start = null;
this.monotonicTimeEstimateStart = null;
this.nanoTimeStart = null;
}

public TimingSource timingSource() {
Expand Down Expand Up @@ -111,7 +114,7 @@ public Deadline hardDeadlineOrNull() {
}

public long timeoutBaseline() {
return start;
return monotonicTimeEstimateStart;
}

/**
Expand All @@ -138,12 +141,18 @@ public boolean hasHardTimeout() {
return this.type == Type.EXCEPTION;
}

/**
* Returns the time passed from the start with high precision.
* This method may be performance expensive.
*
* @return high precision duration of took time.
*/
public Duration tookTime() {
return Duration.ofMillis( elapsedTimeInMilliseconds() );
return Duration.ofMillis( timingSource.nanoTime() - nanoTimeStart );
}

protected long elapsedTimeInMilliseconds() {
return timingSource.monotonicTimeEstimate() - start;
return timingSource.monotonicTimeEstimate() - monotonicTimeEstimateStart;
}

final class DynamicDeadline implements Deadline {
Expand Down

0 comments on commit dea9576

Please sign in to comment.