Skip to content

Commit 0d14d73

Browse files
committed
chore(spanner): address review comments
1 parent c50bb2e commit 0d14d73

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
lines changed

java-spanner/google-cloud-spanner/src/main/java/com/google/cloud/spanner/spi/v1/EwmaLatencyTracker.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import com.google.api.core.BetaApi;
2020
import com.google.api.core.InternalApi;
2121
import com.google.common.base.Preconditions;
22+
import java.time.Duration;
23+
import java.util.concurrent.TimeUnit;
2224
import javax.annotation.concurrent.GuardedBy;
2325

2426
/**
@@ -66,20 +68,28 @@ public double getScore() {
6668
}
6769

6870
@Override
69-
public void update(long latencyMillis) {
71+
public void update(Duration latency) {
72+
long latencyMicros;
73+
try {
74+
latencyMicros = TimeUnit.MICROSECONDS.convert(latency.toNanos(), TimeUnit.NANOSECONDS);
75+
} catch (ArithmeticException e) {
76+
// Duration is too large to fit in nanoseconds (292+ years).
77+
// Use Long.MAX_VALUE to give it the lowest possible priority.
78+
latencyMicros = Long.MAX_VALUE;
79+
}
7080
synchronized (lock) {
7181
if (!initialized) {
72-
score = latencyMillis;
82+
score = latencyMicros;
7383
initialized = true;
7484
} else {
75-
score = alpha * latencyMillis + (1 - alpha) * score;
85+
score = alpha * latencyMicros + (1 - alpha) * score;
7686
}
7787
}
7888
}
7989

8090
@Override
81-
public void recordError(long penaltyMillis) {
91+
public void recordError(Duration penalty) {
8292
// Treat the error as a sample with high latency (penalty)
83-
update(penaltyMillis);
93+
update(penalty);
8494
}
8595
}

java-spanner/google-cloud-spanner/src/main/java/com/google/cloud/spanner/spi/v1/LatencyTracker.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.api.core.BetaApi;
2020
import com.google.api.core.InternalApi;
21+
import java.time.Duration;
2122

2223
/**
2324
* Interface for tracking latency scores of Spanner servers.
@@ -39,14 +40,14 @@ public interface LatencyTracker {
3940
/**
4041
* Updates the latency score with a new observation.
4142
*
42-
* @param latencyMillis the observed latency in milliseconds.
43+
* @param latency the observed latency.
4344
*/
44-
void update(long latencyMillis);
45+
void update(Duration latency);
4546

4647
/**
4748
* Records an error and applies a latency penalty.
4849
*
49-
* @param penaltyMillis the penalty in milliseconds to apply.
50+
* @param penalty the penalty to apply.
5051
*/
51-
void recordError(long penaltyMillis);
52+
void recordError(Duration penalty);
5253
}

java-spanner/google-cloud-spanner/src/test/java/com/google/cloud/spanner/spi/v1/EwmaLatencyTrackerTest.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.junit.Assert.assertEquals;
2020
import static org.junit.Assert.assertThrows;
2121

22+
import java.time.Duration;
2223
import org.junit.Test;
2324
import org.junit.runner.RunWith;
2425
import org.junit.runners.JUnit4;
@@ -29,7 +30,7 @@ public class EwmaLatencyTrackerTest {
2930
@Test
3031
public void testInitialization() {
3132
EwmaLatencyTracker tracker = new EwmaLatencyTracker();
32-
tracker.update(100);
33+
tracker.update(Duration.ofNanos(100 * 1000));
3334
assertEquals(100.0, tracker.getScore(), 0.001);
3435
}
3536

@@ -39,26 +40,33 @@ public void testUninitializedScore() {
3940
assertEquals(Double.MAX_VALUE, tracker.getScore(), 0.001);
4041
}
4142

43+
@Test
44+
public void testOverflowScore() {
45+
EwmaLatencyTracker tracker = new EwmaLatencyTracker();
46+
tracker.update(Duration.ofSeconds(Long.MAX_VALUE));
47+
assertEquals((double) Long.MAX_VALUE, tracker.getScore(), 0.001);
48+
}
49+
4250
@Test
4351
public void testEwmaCalculation() {
4452
double alpha = 0.5;
4553
EwmaLatencyTracker tracker = new EwmaLatencyTracker(alpha);
4654

47-
tracker.update(100); // Initial score = 100
55+
tracker.update(Duration.ofNanos(100 * 1000)); // Initial score = 100
4856
assertEquals(100.0, tracker.getScore(), 0.001);
4957

50-
tracker.update(200); // Score = 0.5 * 200 + 0.5 * 100 = 150
58+
tracker.update(Duration.ofNanos(200 * 1000)); // Score = 0.5 * 200 + 0.5 * 100 = 150
5159
assertEquals(150.0, tracker.getScore(), 0.001);
5260

53-
tracker.update(300); // Score = 0.5 * 300 + 0.5 * 150 = 225
61+
tracker.update(Duration.ofNanos(300 * 1000)); // Score = 0.5 * 300 + 0.5 * 150 = 225
5462
assertEquals(225.0, tracker.getScore(), 0.001);
5563
}
5664

5765
@Test
5866
public void testDefaultAlpha() {
5967
EwmaLatencyTracker tracker = new EwmaLatencyTracker();
60-
tracker.update(100);
61-
tracker.update(200);
68+
tracker.update(Duration.ofNanos(100 * 1000));
69+
tracker.update(Duration.ofNanos(200 * 1000));
6270

6371
double expected =
6472
EwmaLatencyTracker.DEFAULT_ALPHA * 200 + (1 - EwmaLatencyTracker.DEFAULT_ALPHA) * 100;
@@ -68,9 +76,9 @@ public void testDefaultAlpha() {
6876
@Test
6977
public void testRecordError() {
7078
EwmaLatencyTracker tracker = new EwmaLatencyTracker(0.5);
71-
tracker.update(100);
79+
tracker.update(Duration.ofNanos(100 * 1000));
7280

73-
tracker.recordError(10000); // Score = 0.5 * 10000 + 0.5 * 100 = 5050
81+
tracker.recordError(Duration.ofNanos(10000 * 1000)); // Score = 0.5 * 10000 + 0.5 * 100 = 5050
7482
assertEquals(5050.0, tracker.getScore(), 0.001);
7583
}
7684

@@ -84,10 +92,10 @@ public void testInvalidAlpha() {
8492
@Test
8593
public void testAlphaOne() {
8694
EwmaLatencyTracker tracker = new EwmaLatencyTracker(1.0);
87-
tracker.update(100);
95+
tracker.update(Duration.ofNanos(100 * 1000));
8896
assertEquals(100.0, tracker.getScore(), 0.001);
8997

90-
tracker.update(200);
98+
tracker.update(Duration.ofNanos(200 * 1000));
9199
assertEquals(200.0, tracker.getScore(), 0.001);
92100
}
93101
}

0 commit comments

Comments
 (0)