Skip to content

Commit 8885dc2

Browse files
author
Tony Cui
committed
Modify scale factor and minimum refillRatio value. Add test annotation to publisher
1 parent dd288c7 commit 8885dc2

4 files changed

Lines changed: 45 additions & 7 deletions

File tree

java-pubsub/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/v1/HedgeSettings.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ public final class HedgeSettings {
3030
/** Default refill rate (tokens per successful request). */
3131
private static final float DEFAULT_REFILL_RATIO = 0.1f;
3232

33+
/** Minimum refill rate. */
34+
private static final float MIN_REFILL_RATIO = 0.001f;
35+
36+
/** Maximum refill rate. */
37+
private static final float MAX_REFILL_RATIO = 0.2f;
38+
3339
/** Hedging delay. */
3440
private final Duration hedgeDelay;
3541

@@ -118,14 +124,17 @@ public Builder setMaxTokens(final int maxTokens) {
118124
/**
119125
* Allows the token bucket refill rate to be configurable.
120126
*
121-
* @param refill the refill rate (tokens per successful request), must be 0 < RefillRatio <=
122-
* 0.2.
127+
* @param refillRatio the refill rate (tokens per successful request), must be 0.001 <=
128+
* RefillRatio <= 0.2.
123129
* @return this builder.
124130
*/
125131
public Builder setRefillRatio(final float refillRatio) {
126-
if (refillRatio <= 0.0f || refillRatio > 0.2f) {
132+
if (refillRatio < MIN_REFILL_RATIO || refillRatio > MAX_REFILL_RATIO) {
127133
throw new IllegalArgumentException(
128-
"refillRatio must be greater than 0.0 and less than or equal to 0.2");
134+
"refillRatio must be greater than or equal to "
135+
+ MIN_REFILL_RATIO
136+
+ " and less than or equal to "
137+
+ MAX_REFILL_RATIO);
129138
}
130139
this.refillRatio = refillRatio;
131140
return this;

java-pubsub/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/v1/Publisher.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import com.google.cloud.pubsub.v1.stub.GrpcPublisherStub;
4848
import com.google.cloud.pubsub.v1.stub.PublisherStub;
4949
import com.google.cloud.pubsub.v1.stub.PublisherStubSettings;
50+
import com.google.common.annotations.VisibleForTesting;
5051
import com.google.common.base.Preconditions;
5152
import com.google.common.collect.ImmutableMap;
5253
import com.google.common.collect.ImmutableSet;
@@ -151,10 +152,10 @@ public class Publisher implements PublisherInterface {
151152

152153
/**
153154
* Scale factor to represent decimal token values (e.g. 0.1 refill ratio) as integers inside the
154-
* AtomicInteger token bucket. A scale of 100 allows representing decimal ratios down to 0.01
155-
* (1%). For example, 1.0 logical token is represented as 100.
155+
* AtomicInteger token bucket. A scale of 1000 allows representing decimal ratios down to 0.001.
156+
* For example, 1.0 logical token is represented as 1000.
156157
*/
157-
private static final int HEDGE_TOKEN_SCALE = 100;
158+
private static final int HEDGE_TOKEN_SCALE = 1000;
158159

159160
private final AtomicInteger hedgeTokenBucket = new AtomicInteger();
160161
private int scaledMaxHedgeTokens;
@@ -303,6 +304,7 @@ public HedgeSettings getHedgeSettings() {
303304
return hedgeSettings;
304305
}
305306

307+
@VisibleForTesting
306308
Float getHedgeTokenBalance() {
307309
if (hedgeSettings == null) {
308310
return null;

java-pubsub/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/v1/HedgeSettingsTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,16 @@ public void testRefillTooLargeThrows() {
109109
assertThrows(
110110
IllegalArgumentException.class, () -> HedgeSettings.newBuilder().setRefillRatio(0.21f));
111111
}
112+
113+
@Test
114+
public void testRefillTooSmallThrows() {
115+
assertThrows(
116+
IllegalArgumentException.class, () -> HedgeSettings.newBuilder().setRefillRatio(0.0009f));
117+
}
118+
119+
@Test
120+
public void testMinimumRefillValid() {
121+
HedgeSettings settings = HedgeSettings.newBuilder().setRefillRatio(0.001f).build();
122+
assertEquals(0.001f, settings.getRefillRatio(), 0.0001f);
123+
}
112124
}

java-pubsub/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/v1/PublisherImplTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,6 +1475,21 @@ private void waitForRequests(FakePublisherServiceImpl service, int expectedCount
14751475
}
14761476
}
14771477

1478+
@Test
1479+
public void testTokenBucketRefillRate() throws Exception {
1480+
Publisher publisher = getPublisherWithHedge(Duration.ofMillis(100), 0.125f, 10);
1481+
// Starts at 0
1482+
assertThat(publisher.getHedgeTokenBalance()).isEqualTo(0.0f);
1483+
1484+
// Warm up 1 message (should succeed and refill by 0.125)
1485+
testPublisherServiceImpl.setAutoPublishResponse(true);
1486+
sendTestMessage(publisher, "refill-warmup").get();
1487+
1488+
// Balance should be exactly 0.125
1489+
assertThat(publisher.getHedgeTokenBalance()).isWithin(0.0001f).of(0.125f);
1490+
shutdownTestPublisher(publisher);
1491+
}
1492+
14781493
@Test
14791494
public void testHedgingNotTriggeredIfFast() throws Exception {
14801495
Publisher publisher = getPublisherWithHedge(Duration.ofMillis(100));

0 commit comments

Comments
 (0)