Skip to content

Commit a42fec1

Browse files
committed
Replace old NanoTime object with Timer
1 parent 4d599ba commit a42fec1

File tree

6 files changed

+59
-63
lines changed

6 files changed

+59
-63
lines changed

core/src/main/java/org/apache/accumulo/core/clientImpl/TabletServerBatchWriter.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@
7777
import org.apache.accumulo.core.tabletserver.thrift.NoSuchScanIDException;
7878
import org.apache.accumulo.core.trace.TraceUtil;
7979
import org.apache.accumulo.core.util.Retry;
80+
import org.apache.accumulo.core.util.Timer;
8081
import org.apache.accumulo.core.util.threads.ThreadPools;
8182
import org.apache.accumulo.core.util.threads.Threads;
82-
import org.apache.accumulo.core.util.time.NanoTime;
8383
import org.apache.thrift.TApplicationException;
8484
import org.apache.thrift.TException;
8585
import org.apache.thrift.TServiceClient;
@@ -1096,7 +1096,7 @@ private void cancelSession() throws InterruptedException, ThriftSecurityExceptio
10961096

10971097
final HostAndPort parsedServer = HostAndPort.fromString(location);
10981098

1099-
NanoTime startTime = NanoTime.now();
1099+
Timer timer = Timer.startNew();
11001100

11011101
boolean useCloseUpdate = false;
11021102

@@ -1170,7 +1170,7 @@ private void cancelSession() throws InterruptedException, ThriftSecurityExceptio
11701170
}
11711171

11721172
// if a timeout is set on the batch writer, then do not retry longer than the timeout
1173-
if (startTime.elapsed().toMillis() > timeout) {
1173+
if (timer.hasElapsed(timeout, MILLISECONDS)) {
11741174
log.debug("Giving up on canceling session {} {} and timing out.", location, usid);
11751175
throw new TimedOutException(Set.of(location));
11761176
}

core/src/main/java/org/apache/accumulo/core/fate/zookeeper/DistributedReadWriteLock.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import static java.util.concurrent.TimeUnit.DAYS;
2323
import static java.util.concurrent.TimeUnit.MILLISECONDS;
2424

25-
import java.time.Duration;
2625
import java.util.Arrays;
2726
import java.util.Iterator;
2827
import java.util.Map.Entry;
@@ -31,8 +30,8 @@
3130
import java.util.concurrent.locks.Condition;
3231
import java.util.concurrent.locks.Lock;
3332

33+
import org.apache.accumulo.core.util.Timer;
3434
import org.apache.accumulo.core.util.UtilWaitThread;
35-
import org.apache.accumulo.core.util.time.NanoTime;
3635
import org.slf4j.Logger;
3736
import org.slf4j.LoggerFactory;
3837

@@ -182,9 +181,8 @@ public boolean tryLock() {
182181

183182
@Override
184183
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
185-
Duration returnTime = Duration.of(time, unit.toChronoUnit());
186-
NanoTime start = NanoTime.now();
187-
while (start.elapsed().compareTo(returnTime) < 0) {
184+
Timer timer = Timer.startNew();
185+
while (timer.hasElapsed(time, unit)) {
188186
if (tryLock()) {
189187
return true;
190188
}

core/src/main/java/org/apache/accumulo/core/util/Retry.java

+19-25
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import java.time.Duration;
2424

25-
import org.apache.accumulo.core.util.time.NanoTime;
2625
import org.slf4j.Logger;
2726

2827
import com.google.common.annotations.VisibleForTesting;
@@ -42,9 +41,8 @@ public class Retry {
4241
private Duration currentWait;
4342
private Duration initialWait;
4443

45-
private boolean hasNeverLogged;
4644
private boolean hasLoggedWarn = false;
47-
private NanoTime lastRetryLog;
45+
private Timer lastRetryLogTimer;
4846
private double currentBackOffFactor;
4947
private boolean doTimeJitter = true;
5048

@@ -64,8 +62,7 @@ private Retry(long maxRetries, Duration startWait, Duration waitIncrement, Durat
6462
this.currentWait = startWait;
6563
this.initialWait = startWait;
6664
this.logInterval = logInterval;
67-
this.hasNeverLogged = true;
68-
this.lastRetryLog = null;
65+
this.lastRetryLogTimer = null;
6966
this.backOffFactor = backOffFactor;
7067
this.currentBackOffFactor = this.backOffFactor;
7168

@@ -202,16 +199,14 @@ protected void sleep(Duration wait) throws InterruptedException {
202199

203200
public void logRetry(Logger log, String message, Throwable t) {
204201
// log the first time as debug, and then after every logInterval as a warning
205-
NanoTime now = NanoTime.now();
206-
if (hasNeverLogged) {
202+
if (lastRetryLogTimer == null) {
207203
if (log.isDebugEnabled()) {
208204
log.debug(getMessage(message, t));
209205
}
210-
hasNeverLogged = false;
211-
lastRetryLog = now;
212-
} else if (now.subtract(lastRetryLog).compareTo(logInterval) > 0) {
206+
lastRetryLogTimer = Timer.startNew();
207+
} else if (lastRetryLogTimer.hasElapsed(logInterval)) {
213208
log.warn(getMessage(message), t);
214-
lastRetryLog = now;
209+
lastRetryLogTimer.restart();
215210
hasLoggedWarn = true;
216211
} else {
217212
if (log.isTraceEnabled()) {
@@ -222,16 +217,14 @@ public void logRetry(Logger log, String message, Throwable t) {
222217

223218
public void logRetry(Logger log, String message) {
224219
// log the first time as debug, and then after every logInterval as a warning
225-
NanoTime now = NanoTime.now();
226-
if (hasNeverLogged) {
220+
if (lastRetryLogTimer == null) {
227221
if (log.isDebugEnabled()) {
228222
log.debug(getMessage(message));
229223
}
230-
hasNeverLogged = false;
231-
lastRetryLog = now;
232-
} else if (now.subtract(lastRetryLog).compareTo(logInterval) > 0) {
224+
lastRetryLogTimer = Timer.startNew();
225+
} else if (lastRetryLogTimer.hasElapsed(logInterval)) {
233226
log.warn(getMessage(message));
234-
lastRetryLog = now;
227+
lastRetryLogTimer.restart();
235228
hasLoggedWarn = true;
236229
} else {
237230
if (log.isTraceEnabled()) {
@@ -251,14 +244,15 @@ private String getMessage(String message, Throwable t) {
251244
}
252245

253246
public void logCompletion(Logger log, String operationDescription) {
254-
if (!hasNeverLogged) {
255-
var message = operationDescription + " completed after " + (retriesDone + 1)
256-
+ " retries and is no longer retrying.";
257-
if (hasLoggedWarn) {
258-
log.info(message);
259-
} else {
260-
log.debug(message);
261-
}
247+
if (lastRetryLogTimer == null) { // have never logged a retry
248+
return;
249+
}
250+
var message = operationDescription + " completed after " + (retriesDone + 1)
251+
+ " retries and is no longer retrying.";
252+
if (hasLoggedWarn) {
253+
log.info(message);
254+
} else {
255+
log.debug(message);
262256
}
263257
}
264258

server/base/src/main/java/org/apache/accumulo/server/mem/LowMemoryDetector.java

+13-11
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
*/
1919
package org.apache.accumulo.server.mem;
2020

21+
import static java.util.concurrent.TimeUnit.MILLISECONDS;
22+
2123
import java.lang.management.GarbageCollectorMXBean;
2224
import java.lang.management.ManagementFactory;
25+
import java.time.Duration;
2326
import java.util.HashMap;
2427
import java.util.List;
2528
import java.util.concurrent.locks.Lock;
@@ -29,7 +32,7 @@
2932
import org.apache.accumulo.core.conf.AccumuloConfiguration;
3033
import org.apache.accumulo.core.conf.Property;
3134
import org.apache.accumulo.core.util.Halt;
32-
import org.apache.accumulo.core.util.time.NanoTime;
35+
import org.apache.accumulo.core.util.Timer;
3336
import org.apache.accumulo.server.ServerContext;
3437
import org.slf4j.Logger;
3538
import org.slf4j.LoggerFactory;
@@ -51,7 +54,7 @@ public enum DetectionScope {
5154

5255
private long lastMemorySize = 0;
5356
private int lowMemCount = 0;
54-
private NanoTime lastMemoryCheckTime = null;
57+
private Timer lastMemoryCheckTime = null;
5558
private final Lock memCheckTimeLock = new ReentrantLock();
5659
private volatile boolean runningLowOnMemory = false;
5760

@@ -104,7 +107,7 @@ public void logGCInfo(AccumuloConfiguration conf) {
104107

105108
memCheckTimeLock.lock();
106109
try {
107-
final NanoTime now = NanoTime.now();
110+
final Timer currentTimer = Timer.startNew();
108111

109112
List<GarbageCollectorMXBean> gcmBeans = ManagementFactory.getGarbageCollectorMXBeans();
110113

@@ -173,25 +176,24 @@ public void logGCInfo(AccumuloConfiguration conf) {
173176
LOG.debug(sb.toString());
174177
}
175178

176-
final long keepAliveTimeout = conf.getTimeInMillis(Property.INSTANCE_ZK_TIMEOUT);
177-
if (lastMemoryCheckTime != null && lastMemoryCheckTime.compareTo(now) < 0) {
178-
final long diff = now.subtract(lastMemoryCheckTime).toMillis();
179-
if (diff > keepAliveTimeout + 1000) {
179+
final Duration keepAliveTimeout = conf.getDuration(Property.INSTANCE_ZK_TIMEOUT);
180+
if (lastMemoryCheckTime != null && lastMemoryCheckTime.hasElapsed(keepAliveTimeout)) {
181+
if (currentTimer.hasElapsed(keepAliveTimeout.plus(Duration.ofSeconds(1)))) {
180182
LOG.warn(String.format(
181183
"GC pause checker not called in a timely"
182184
+ " fashion. Expected every %.1f seconds but was %.1f seconds since last check",
183-
keepAliveTimeout / 1000., diff / 1000.));
185+
keepAliveTimeout.toMillis() / 1000., currentTimer.elapsed(MILLISECONDS) / 1000.));
184186
}
185-
lastMemoryCheckTime = now;
187+
lastMemoryCheckTime = currentTimer;
186188
return;
187189
}
188190

189-
if (maxIncreaseInCollectionTime > keepAliveTimeout) {
191+
if (maxIncreaseInCollectionTime > keepAliveTimeout.toMillis()) {
190192
Halt.halt("Garbage collection may be interfering with lock keep-alive. Halting.", -1);
191193
}
192194

193195
lastMemorySize = freeMemory;
194-
lastMemoryCheckTime = now;
196+
lastMemoryCheckTime = currentTimer;
195197
} finally {
196198
memCheckTimeLock.unlock();
197199
}

server/base/src/main/java/org/apache/accumulo/server/rpc/TimedProcessor.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
*/
1919
package org.apache.accumulo.server.rpc;
2020

21+
import static java.util.concurrent.TimeUnit.MILLISECONDS;
22+
2123
import org.apache.accumulo.core.metrics.MetricsInfo;
22-
import org.apache.accumulo.core.util.time.NanoTime;
24+
import org.apache.accumulo.core.util.Timer;
2325
import org.apache.accumulo.server.metrics.ThriftMetrics;
2426
import org.apache.thrift.TException;
2527
import org.apache.thrift.TProcessor;
@@ -32,25 +34,25 @@ public class TimedProcessor implements TProcessor {
3234

3335
private final TProcessor other;
3436
private final ThriftMetrics thriftMetrics;
35-
private NanoTime idleStart;
37+
private final Timer idleTimer;
3638

3739
public TimedProcessor(final TProcessor next, final MetricsInfo metricsInfo) {
3840
this.other = next;
3941
thriftMetrics = new ThriftMetrics();
4042
metricsInfo.addMetricsProducers(thriftMetrics);
41-
idleStart = NanoTime.now();
43+
idleTimer = Timer.startNew();
4244
}
4345

4446
@Override
4547
public void process(TProtocol in, TProtocol out) throws TException {
46-
NanoTime processStart = NanoTime.now();
47-
thriftMetrics.addIdle(processStart.subtract(idleStart).toMillis());
48+
thriftMetrics.addIdle(idleTimer.elapsed(MILLISECONDS));
49+
Timer processTimer = Timer.startNew();
4850
try {
4951
other.process(in, out);
5052
} finally {
51-
// set idle to now, calc time in process
52-
idleStart = NanoTime.now();
53-
thriftMetrics.addExecute(idleStart.subtract(processStart).toMillis());
53+
// calc time in process, restart idle timer
54+
thriftMetrics.addExecute(processTimer.elapsed(MILLISECONDS));
55+
idleTimer.restart();
5456
}
5557
}
5658
}

server/tserver/src/main/java/org/apache/accumulo/tserver/tablet/Tablet.java

+11-11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import static com.google.common.util.concurrent.Uninterruptibles.sleepUninterruptibly;
2222
import static java.nio.charset.StandardCharsets.UTF_8;
23+
import static java.util.concurrent.TimeUnit.MINUTES;
24+
import static java.util.concurrent.TimeUnit.SECONDS;
2325
import static java.util.stream.Collectors.toList;
2426

2527
import java.io.ByteArrayInputStream;
@@ -92,7 +94,7 @@
9294
import org.apache.accumulo.core.tabletserver.thrift.TabletStats;
9395
import org.apache.accumulo.core.trace.TraceUtil;
9496
import org.apache.accumulo.core.util.Pair;
95-
import org.apache.accumulo.core.util.time.NanoTime;
97+
import org.apache.accumulo.core.util.Timer;
9698
import org.apache.accumulo.core.volume.Volume;
9799
import org.apache.accumulo.server.ServerContext;
98100
import org.apache.accumulo.server.compaction.CompactionStats;
@@ -1032,7 +1034,7 @@ synchronized void completeClose(boolean saveState, boolean completeClose) throws
10321034
return currentlyUnreserved;
10331035
});
10341036

1035-
long lastLogTime = System.nanoTime();
1037+
Timer lastLogTimer = Timer.startNew();
10361038

10371039
// wait for reads and writes to complete
10381040
while (writesInProgress > 0 || !runningScans.isEmpty()) {
@@ -1044,12 +1046,12 @@ synchronized void completeClose(boolean saveState, boolean completeClose) throws
10441046
return currentlyUnreserved;
10451047
});
10461048

1047-
if (log.isDebugEnabled() && System.nanoTime() - lastLogTime > TimeUnit.SECONDS.toNanos(60)) {
1049+
if (log.isDebugEnabled() && lastLogTimer.hasElapsed(1, MINUTES)) {
10481050
for (ScanDataSource activeScan : runningScans) {
10491051
log.debug("Waiting on scan in completeClose {} {}", extent, activeScan);
10501052
}
10511053

1052-
lastLogTime = System.nanoTime();
1054+
lastLogTimer.restart();
10531055
}
10541056

10551057
try {
@@ -1770,7 +1772,7 @@ public void importDataFiles(long tid, Map<ReferencedTabletFile,DataFileInfo> fil
17701772

17711773
// Clients timeout and will think that this operation failed.
17721774
// Don't do it if we spent too long waiting for the lock
1773-
NanoTime now = NanoTime.now();
1775+
Timer lockWaitTimer = Timer.startNew();
17741776
synchronized (this) {
17751777
if (isClosed()) {
17761778
throw new IOException("tablet " + extent + " is closed");
@@ -1789,9 +1791,8 @@ public void importDataFiles(long tid, Map<ReferencedTabletFile,DataFileInfo> fil
17891791
throw new IllegalStateException(e);
17901792
}
17911793

1792-
Duration lockWait = now.elapsed();
1793-
if (lockWait.compareTo(rpcTimeout) > 0) {
1794-
throw new IOException("Timeout waiting " + lockWait.toSeconds()
1794+
if (lockWaitTimer.hasElapsed(rpcTimeout)) {
1795+
throw new IOException("Timeout waiting " + lockWaitTimer.elapsed(SECONDS)
17951796
+ " seconds to get tablet lock for " + extent + " " + tid);
17961797
}
17971798
}
@@ -1801,9 +1802,8 @@ public void importDataFiles(long tid, Map<ReferencedTabletFile,DataFileInfo> fil
18011802
throw new IOException("tablet " + extent + " is closed");
18021803
}
18031804

1804-
Duration lockWait = now.elapsed();
1805-
if (lockWait.compareTo(rpcTimeout) > 0) {
1806-
throw new IOException("Timeout waiting " + lockWait.toSeconds()
1805+
if (lockWaitTimer.hasElapsed(rpcTimeout)) {
1806+
throw new IOException("Timeout waiting " + lockWaitTimer.elapsed(SECONDS)
18071807
+ " seconds to get tablet lock for " + extent + " " + tid);
18081808
}
18091809

0 commit comments

Comments
 (0)