Skip to content

Commit c4662b1

Browse files
authored
Add TimePoint and use it in Timeout (#1164)
This work is needed for JAVA-5076
1 parent 80a9cb4 commit c4662b1

File tree

10 files changed

+382
-119
lines changed

10 files changed

+382
-119
lines changed

driver-core/src/main/com/mongodb/internal/connection/ConcurrentPool.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import com.mongodb.MongoTimeoutException;
2424
import com.mongodb.annotations.ThreadSafe;
2525
import com.mongodb.internal.VisibleForTesting;
26+
import com.mongodb.internal.time.TimePoint;
27+
import com.mongodb.internal.time.Timeout;
2628
import com.mongodb.lang.Nullable;
2729

2830
import java.util.Deque;
@@ -142,7 +144,7 @@ public T get() {
142144
* Gets an object from the pool. Blocks until an object is available, or the specified {@code timeout} expires,
143145
* or the pool is {@linkplain #close() closed}/{@linkplain #pause(Supplier) paused}.
144146
*
145-
* @param timeout See {@link com.mongodb.internal.Timeout#startNow(long, TimeUnit)}.
147+
* @param timeout See {@link Timeout#started(long, TimeUnit, TimePoint)}.
146148
* @param timeUnit the time unit of the timeout
147149
* @return An object from the pool, or null if can't get one in the given waitTime
148150
* @throws MongoTimeoutException if the timeout has been exceeded
@@ -226,7 +228,7 @@ private T createNewAndReleasePermitIfFailure() {
226228
}
227229

228230
/**
229-
* @param timeout See {@link com.mongodb.internal.Timeout#startNow(long, TimeUnit)}.
231+
* @param timeout See {@link Timeout#started(long, TimeUnit, TimePoint)}.
230232
*/
231233
@VisibleForTesting(otherwise = PRIVATE)
232234
boolean acquirePermit(final long timeout, final TimeUnit timeUnit) {
@@ -386,7 +388,7 @@ boolean acquirePermitImmediateUnfair() {
386388
* This method also emulates the eager {@link InterruptedException} behavior of
387389
* {@link java.util.concurrent.Semaphore#tryAcquire(long, TimeUnit)}.
388390
*
389-
* @param timeout See {@link com.mongodb.internal.Timeout#startNow(long, TimeUnit)}.
391+
* @param timeout See {@link Timeout#started(long, TimeUnit, TimePoint)}.
390392
*/
391393
boolean acquirePermit(final long timeout, final TimeUnit unit) throws MongoInterruptedException {
392394
long remainingNanos = unit.toNanos(timeout);

driver-core/src/main/com/mongodb/internal/connection/ConnectionPool.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import com.mongodb.annotations.ThreadSafe;
2121
import com.mongodb.connection.ConnectionPoolSettings;
2222
import com.mongodb.internal.async.SingleResultCallback;
23+
import com.mongodb.internal.time.Timeout;
24+
import com.mongodb.internal.time.TimePoint;
2325
import org.bson.types.ObjectId;
2426
import com.mongodb.lang.Nullable;
2527

@@ -38,7 +40,7 @@ interface ConnectionPool extends Closeable {
3840

3941
/**
4042
* @param operationContext operation context
41-
* @param timeout See {@link com.mongodb.internal.Timeout#startNow(long, TimeUnit)}.
43+
* @param timeout See {@link Timeout#started(long, TimeUnit, TimePoint)}.
4244
* @throws MongoConnectionPoolClearedException If detects that the pool is {@linkplain #invalidate(Throwable) paused}.
4345
*/
4446
InternalConnection get(OperationContext operationContext, long timeout, TimeUnit timeUnit) throws MongoConnectionPoolClearedException;

driver-core/src/main/com/mongodb/internal/connection/DefaultConnectionPool.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
import com.mongodb.event.ConnectionPoolListener;
4444
import com.mongodb.event.ConnectionPoolReadyEvent;
4545
import com.mongodb.event.ConnectionReadyEvent;
46-
import com.mongodb.internal.Timeout;
46+
import com.mongodb.internal.time.TimePoint;
47+
import com.mongodb.internal.time.Timeout;
4748
import com.mongodb.internal.VisibleForTesting;
4849
import com.mongodb.internal.async.SingleResultCallback;
4950
import com.mongodb.internal.connection.SdamServerDescriptionManager.SdamIssue;
@@ -1123,7 +1124,7 @@ void signalClosedOrPaused() {
11231124
}
11241125

11251126
/**
1126-
* @param timeoutNanos See {@link Timeout#startNow(long)}.
1127+
* @param timeoutNanos See {@link Timeout#started(long, TimePoint)}.
11271128
* @return The remaining duration as per {@link Timeout#remainingOrInfinite(TimeUnit)} if waiting ended early either
11281129
* spuriously or because of receiving a signal.
11291130
*/
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.mongodb.internal.time;
17+
18+
import com.mongodb.annotations.Immutable;
19+
import com.mongodb.internal.VisibleForTesting;
20+
21+
import java.time.Clock;
22+
import java.time.Duration;
23+
24+
import static com.mongodb.internal.VisibleForTesting.AccessModifier.PRIVATE;
25+
26+
/**
27+
* A <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/doc-files/ValueBased.html">value-based</a> class
28+
* representing a point on a timeline. The origin of this timeline has no known relation to the
29+
* {@linkplain Clock#systemUTC() system clock}. The same timeline is used by all {@link TimePoint}s within the same process.
30+
* <p>
31+
* Methods operating on a pair of {@link TimePoint}s,
32+
* for example, {@link #durationSince(TimePoint)}, {@link #compareTo(TimePoint)},
33+
* or producing a point from another one, for example, {@link #add(Duration)},
34+
* work correctly only if the duration between the points is not greater than {@link Long#MAX_VALUE} nanoseconds,
35+
* which is more than 292 years.</p>
36+
* <p>
37+
* This class is not part of the public API and may be removed or changed at any time.</p>
38+
*/
39+
@Immutable
40+
public final class TimePoint implements Comparable<TimePoint> {
41+
private final long nanos;
42+
43+
private TimePoint(final long nanos) {
44+
this.nanos = nanos;
45+
}
46+
47+
/**
48+
* Returns the current {@link TimePoint}.
49+
*/
50+
public static TimePoint now() {
51+
return at(System.nanoTime());
52+
}
53+
54+
@VisibleForTesting(otherwise = PRIVATE)
55+
static TimePoint at(final long nanos) {
56+
return new TimePoint(nanos);
57+
}
58+
59+
/**
60+
* The {@link Duration} between this {@link TimePoint} and {@code t}.
61+
* A {@linkplain Duration#isNegative() negative} {@link Duration} means that
62+
* this {@link TimePoint} is {@linkplain #compareTo(TimePoint) before} {@code t}.
63+
*
64+
* @see #elapsed()
65+
*/
66+
public Duration durationSince(final TimePoint t) {
67+
return Duration.ofNanos(nanos - t.nanos);
68+
}
69+
70+
/**
71+
* The {@link Duration} between {@link TimePoint#now()} and this {@link TimePoint}.
72+
* This method is functionally equivalent to {@code TimePoint.now().durationSince(this)}.
73+
*
74+
* @see #durationSince(TimePoint)
75+
*/
76+
public Duration elapsed() {
77+
return Duration.ofNanos(System.nanoTime() - nanos);
78+
}
79+
80+
/**
81+
* Returns a {@link TimePoint} that is {@code duration} away from this one.
82+
*
83+
* @param duration A duration that may also be {@linkplain Duration#isNegative() negative}.
84+
*/
85+
public TimePoint add(final Duration duration) {
86+
long durationNanos = duration.toNanos();
87+
return TimePoint.at(nanos + durationNanos);
88+
}
89+
90+
/**
91+
* If this {@link TimePoint} is less/greater than {@code t}, then it is before/after {@code t}.
92+
* <p>
93+
* {@inheritDoc}</p>
94+
*/
95+
@Override
96+
public int compareTo(final TimePoint t) {
97+
return Long.signum(nanos - t.nanos);
98+
}
99+
100+
@Override
101+
public boolean equals(final Object o) {
102+
if (this == o) {
103+
return true;
104+
}
105+
if (o == null || getClass() != o.getClass()) {
106+
return false;
107+
}
108+
final TimePoint timePoint = (TimePoint) o;
109+
return nanos == timePoint.nanos;
110+
}
111+
112+
@Override
113+
public int hashCode() {
114+
return Long.hashCode(nanos);
115+
}
116+
117+
@Override
118+
public String toString() {
119+
return "TimePoint{"
120+
+ "nanos=" + nanos
121+
+ '}';
122+
}
123+
}

0 commit comments

Comments
 (0)