This is an automated email from the ASF dual-hosted git repository.
reschke pushed a commit to branch OAK-11618
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
The following commit(s) were added to refs/heads/OAK-11618 by this push:
new ca997243ee OAK-11618: Remove usage of Guava Stopwatch - work in
progress
ca997243ee is described below
commit ca997243eed31f23fb11372529e2299697e844c2
Author: Julian Reschke <[email protected]>
AuthorDate: Mon Apr 7 13:23:16 2025 +0100
OAK-11618: Remove usage of Guava Stopwatch - work in progress
---
.../org/apache/jackrabbit/oak/stats/Stopwatch.java | 54 +++++++++-------------
.../org/apache/jackrabbit/oak/stats/ClockTest.java | 10 ----
.../jackrabbit/oak/stats/NonTickingTestClock.java | 34 --------------
.../plugins/index/TrackingCorruptIndexHandler.java | 4 +-
.../oak/plugins/index/FormattingUtilsTest.java | 24 ++++++++--
.../oak/plugins/index/search/BadIndexTracker.java | 5 +-
6 files changed, 47 insertions(+), 84 deletions(-)
diff --git
a/oak-core-spi/src/main/java/org/apache/jackrabbit/oak/stats/Stopwatch.java
b/oak-core-spi/src/main/java/org/apache/jackrabbit/oak/stats/Stopwatch.java
index 6b1c6183f3..b8a5cfaa4f 100644
--- a/oak-core-spi/src/main/java/org/apache/jackrabbit/oak/stats/Stopwatch.java
+++ b/oak-core-spi/src/main/java/org/apache/jackrabbit/oak/stats/Stopwatch.java
@@ -25,51 +25,43 @@ import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
/**
- * A stop watch based on either a {@link java.time.Clock} or a {@link
java.security.Provider} of milliseconds,
- * measuring elapsed time in milliseconds.
+ * A stop watch based on a {@link Supplier} of nanoseconds.
* <p>
* Inspired by Guava's.
*/
public class Stopwatch {
- private long starttime;
+ private long startTime;
private long accumulated;
private boolean running;
private final Supplier<Long> ticker;
- private Stopwatch(Supplier<Long> ticker, boolean running) {
+ private Stopwatch(Supplier<Long> ticker) {
this.ticker = ticker;
this.accumulated = 0L;
- this.starttime = ticker.get();
- this.running = running;
+ this.startTime = ticker.get();
+ this.running = false;
}
/**
* @return a running stop watch, using {@link System#nanoTime()}.
*/
public static Stopwatch createStarted() {
- return new Stopwatch(() -> tickerNanoAsMs(), true);
- }
-
- /**
- * @return a running stop watch, using the supplied clock.
- */
- public static Stopwatch createStarted(java.time.Clock clock) {
- return new Stopwatch(() -> clock.millis(), true);
+ return new Stopwatch(Stopwatch::tick).start();
}
/**
* @return a running stop watch, using the supplied provider.
*/
public static Stopwatch createStarted(Supplier<Long> ticker) {
- return new Stopwatch(ticker, true);
+ return new Stopwatch(ticker).start();
}
/**
* @return a non-running stop watch, using {@link System#nanoTime()}.
*/
public static Stopwatch createUnstarted() {
- return new Stopwatch(() -> tickerNanoAsMs(), false);
+ return new Stopwatch(Stopwatch::tick);
}
/**
@@ -77,20 +69,20 @@ public class Stopwatch {
* @return the stop watch
*/
public Stopwatch start() {
- Validate.checkState(!this.running, "Stopwatch already started.");
- this.starttime = this.ticker.get();
+ Validate.checkState(!this.running, "Stopwatch already running.");
+ this.startTime = this.ticker.get();
this.running = true;
return this;
}
/**
- * Stops the stop watch, will fail when stopped.
+ * Stops the stop watch, will fail when not running.
* @return the stop watch
*/
public Stopwatch stop() {
Validate.checkState(this.running, "Stopwatch not running.");
- this.accumulated += elapsedMillis();
- this.starttime = 0L;
+ this.accumulated += elapsedNanos();
+ this.startTime = 0L;
this.running = false;
return this;
}
@@ -101,7 +93,7 @@ public class Stopwatch {
*/
public Stopwatch reset() {
this.accumulated = 0L;
- this.starttime = 0;
+ this.startTime = 0;
this.running = false;
return this;
}
@@ -119,7 +111,7 @@ public class Stopwatch {
* @return elapsed time in the specified unit
*/
public long elapsed(TimeUnit timeunit) {
- return timeunit.convert(elapsedMillis(), TimeUnit.MILLISECONDS);
+ return timeunit.convert(elapsedNanos(), TimeUnit.NANOSECONDS);
}
/**
@@ -127,22 +119,22 @@ public class Stopwatch {
* @return elapsed time
*/
public Duration elapsed() {
- return Duration.ofMillis(elapsedMillis());
+ return Duration.ofMillis(elapsedNanos());
}
@Override
public String toString() {
- return java.time.Duration.ofMillis(elapsedMillis()).toString();
+ return java.time.Duration.ofNanos(elapsedNanos()).toString();
}
- private long elapsedMillis() {
- long delta = this.running ? this.ticker.get() - this.starttime : 0;
+ // private parts
+
+ private long elapsedNanos() {
+ long delta = this.running ? this.ticker.get() - this.startTime : 0;
return this.accumulated + delta;
}
- private static long NS_PER_MS = TimeUnit.MILLISECONDS.toNanos(1);
-
- private static long tickerNanoAsMs() {
- return System.nanoTime() / NS_PER_MS;
+ private static long tick() {
+ return System.nanoTime();
}
}
diff --git
a/oak-core-spi/src/test/java/org/apache/jackrabbit/oak/stats/ClockTest.java
b/oak-core-spi/src/test/java/org/apache/jackrabbit/oak/stats/ClockTest.java
index 0b6dce9d6a..943cfbaf76 100644
--- a/oak-core-spi/src/test/java/org/apache/jackrabbit/oak/stats/ClockTest.java
+++ b/oak-core-spi/src/test/java/org/apache/jackrabbit/oak/stats/ClockTest.java
@@ -137,16 +137,6 @@ public class ClockTest {
assertEquals(c2.getZone(), c.getZone());
}
- @Test
- public void testNonTicking() {
- NonTickingTestClock ntc = new NonTickingTestClock();
- assertEquals(0, ntc.millis());
- ntc.setTime(1000);
- assertEquals(1000, ntc.millis());
- ntc.setTime(500);
- assertEquals(500, ntc.millis());
- }
-
private void testClockDrift(Clock clock) throws InterruptedException {
long drift = clock.getTime() - System.currentTimeMillis();
diff --git
a/oak-core-spi/src/test/java/org/apache/jackrabbit/oak/stats/NonTickingTestClock.java
b/oak-core-spi/src/test/java/org/apache/jackrabbit/oak/stats/NonTickingTestClock.java
deleted file mode 100644
index a5379be4df..0000000000
---
a/oak-core-spi/src/test/java/org/apache/jackrabbit/oak/stats/NonTickingTestClock.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.jackrabbit.oak.stats;
-
-/**
- * Simple non-ticking clock that can be set (for use in test cases).
- */
-public class NonTickingTestClock extends Clock {
-
- long time = 0;
-
- @Override
- public long getTime() {
- return this.time;
- }
-
- public void setTime(long time) {
- this.time = time;
- }
-}
diff --git
a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/TrackingCorruptIndexHandler.java
b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/TrackingCorruptIndexHandler.java
index 448a36213e..326e4fa9de 100644
---
a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/TrackingCorruptIndexHandler.java
+++
b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/TrackingCorruptIndexHandler.java
@@ -25,6 +25,7 @@ import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
+import java.util.function.Supplier;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.openmbean.CompositeType;
@@ -36,7 +37,6 @@ import javax.management.openmbean.TabularDataSupport;
import javax.management.openmbean.TabularType;
import org.apache.jackrabbit.guava.common.base.Throwables;
-import org.apache.jackrabbit.guava.common.base.Ticker;
import org.apache.jackrabbit.oak.stats.Clock;
import org.apache.jackrabbit.oak.stats.MeterStats;
import org.apache.jackrabbit.oak.stats.Stopwatch;
@@ -168,7 +168,7 @@ public class TrackingCorruptIndexHandler implements
CorruptIndexHandler {
private final String asyncName;
private final String path;
private final long lastIndexerCycleCount = indexerCycleCount;
- private final Stopwatch watch = Stopwatch.createStarted(clock);
+ private final Stopwatch watch = Stopwatch.createStarted(() ->
TimeUnit.MICROSECONDS.toNanos(clock.millis()));
private String exception = "";
private int failureCount;
diff --git
a/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/FormattingUtilsTest.java
b/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/FormattingUtilsTest.java
index a4b84f6b81..56c5777b1e 100644
---
a/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/FormattingUtilsTest.java
+++
b/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/FormattingUtilsTest.java
@@ -16,18 +16,18 @@
*/
package org.apache.jackrabbit.oak.plugins.index;
-import org.apache.jackrabbit.oak.stats.NonTickingTestClock;
import org.apache.jackrabbit.oak.stats.Stopwatch;
import org.junit.Test;
import java.util.concurrent.TimeUnit;
+import java.util.function.Supplier;
import static org.junit.Assert.assertEquals;
public class FormattingUtilsTest {
- private final NonTickingTestClock clock = new NonTickingTestClock();
- private final Stopwatch sw = Stopwatch.createStarted(clock);
+ private final NonTickingTimeSupplier ticker = new NonTickingTimeSupplier();
+ private final Stopwatch sw = Stopwatch.createStarted(ticker);
@Test
public void formatToSeconds() {
@@ -48,7 +48,7 @@ public class FormattingUtilsTest {
}
private void testFormatToSeconds(String expected, long millis) {
- clock.setTime(millis);
+ ticker.setTimeMillis(millis);
assertEquals(expected, FormattingUtils.formatToSeconds(sw));
}
@@ -71,7 +71,7 @@ public class FormattingUtilsTest {
}
private void testFormatToMillis(String expected, long millis) {
- clock.setTime(millis);
+ ticker.setTimeMillis(millis);
assertEquals(expected, FormattingUtils.formatToMillis(sw));
}
@@ -92,4 +92,18 @@ public class FormattingUtilsTest {
assertEquals(100.0, FormattingUtils.safeComputeAverage(100, 1), 0.001);
assertEquals(33.333, FormattingUtils.safeComputeAverage(100, 3),
0.001);
}
+
+ private static class NonTickingTimeSupplier implements Supplier<Long> {
+
+ private long time;
+
+ public void setTimeMillis(long time) {
+ this.time = TimeUnit.MILLISECONDS.toNanos(time);
+ }
+
+ @Override
+ public Long get() {
+ return this.time;
+ }
+ }
}
\ No newline at end of file
diff --git
a/oak-search/src/main/java/org/apache/jackrabbit/oak/plugins/index/search/BadIndexTracker.java
b/oak-search/src/main/java/org/apache/jackrabbit/oak/plugins/index/search/BadIndexTracker.java
index 9c0d484525..4004f09d88 100644
---
a/oak-search/src/main/java/org/apache/jackrabbit/oak/plugins/index/search/BadIndexTracker.java
+++
b/oak-search/src/main/java/org/apache/jackrabbit/oak/plugins/index/search/BadIndexTracker.java
@@ -24,6 +24,7 @@ import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
+import org.apache.jackrabbit.guava.common.base.Supplier;
import org.apache.jackrabbit.guava.common.base.Throwables;
import org.apache.jackrabbit.oak.stats.Stopwatch;
import org.slf4j.Logger;
@@ -155,8 +156,8 @@ public class BadIndexTracker {
final int lastIndexerCycleCount = indexerCycleCount;
private final long createdTime = clock.millis();
private final boolean persistedIndex;
- private final Stopwatch created = Stopwatch.createStarted(clock);
- private final Stopwatch watch = Stopwatch.createStarted(clock);
+ private final Stopwatch created = Stopwatch.createStarted(() ->
TimeUnit.MILLISECONDS.toNanos(clock.millis()));
+ private final Stopwatch watch = Stopwatch.createStarted(() ->
TimeUnit.MILLISECONDS.toNanos(clock.millis()));
private String exception;
private int accessCount;
private int failedAccessCount;