-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(pubsub): implement HedgeSettings and integrate with Publisher #13735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tonyyyycui
wants to merge
7
commits into
googleapis:main
Choose a base branch
from
tonyyyycui:publish-hedging-settings
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
61d0b60
Add hedgeSettings with configurable hedgeDelay
19afd68
Add HedgeTokenBucket class to allow for token operations
49426a9
Add publisher integration with HedgeSettings
22417d1
Restore original @Ignore annotations and code in PublisherImplTest
91706a2
style(pubsub): fix checkstyle violations in HedgeSettings
bd76ede
style(pubsub): fix formatting and checkstyle violations in HedgeSetti…
61aef08
Merge branch 'main' into publish-hedging-settings
tonyyyycui File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
java-pubsub/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/v1/HedgeSettings.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed 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 | ||
| * | ||
| * https://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 com.google.cloud.pubsub.v1; | ||
|
|
||
| import com.google.common.base.Preconditions; | ||
| import java.time.Duration; | ||
|
|
||
| /** Settings for configuring publish hedging. */ | ||
| public final class HedgeSettings { | ||
| /** Default hedging delay. */ | ||
| private static final Duration DEFAULT_DELAY = Duration.ofMillis(100); | ||
|
|
||
| /** Default maximum number of tokens in the bucket. */ | ||
| private static final int DEFAULT_MAX_TOKENS = 100; | ||
|
|
||
| /** Default refill rate (tokens per successful request). */ | ||
| private static final float DEFAULT_REFILL = 0.1f; | ||
|
|
||
| /** Hedging delay. */ | ||
| private final Duration hedgeDelay; | ||
|
|
||
| /** Maximum tokens. */ | ||
| private final int maxTokens; | ||
|
|
||
| /** Refill rate. */ | ||
| private final float refill; | ||
|
|
||
| private HedgeSettings(final Builder builder) { | ||
| this.hedgeDelay = builder.hedgeDelay; | ||
| this.maxTokens = builder.maxTokens; | ||
| this.refill = builder.refill; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the configured hedging delay. | ||
| * | ||
| * @return the hedging delay. | ||
| */ | ||
| public Duration getHedgeDelay() { | ||
| return hedgeDelay; | ||
| } | ||
|
|
||
| int getMaxTokens() { | ||
| return maxTokens; | ||
| } | ||
|
|
||
| float getRefill() { | ||
| return refill; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a new builder for {@code HedgeSettings}. | ||
| * | ||
| * @return a new builder. | ||
| */ | ||
| public static Builder newBuilder() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| /** Builder for {@code HedgeSettings}. */ | ||
| public static final class Builder { | ||
| /** Hedging delay. */ | ||
| private Duration hedgeDelay = DEFAULT_DELAY; | ||
|
|
||
| /** Maximum tokens. */ | ||
| private int maxTokens = DEFAULT_MAX_TOKENS; | ||
|
|
||
| /** Refill rate. */ | ||
| private float refill = DEFAULT_REFILL; | ||
|
|
||
| private Builder() { | ||
| super(); | ||
| } | ||
|
|
||
| /** | ||
| * Allows hedging delay to be configurable. | ||
| * | ||
| * @param delay the hedging delay, must be positive. | ||
| * @return this builder. | ||
| */ | ||
| public Builder setHedgeDelay(final Duration delay) { | ||
| Preconditions.checkNotNull(delay); | ||
| if (delay.isNegative() || delay.isZero()) { | ||
| throw new IllegalArgumentException("delay must be positive"); | ||
| } | ||
| this.hedgeDelay = delay; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Builds an instance of {@code HedgeSettings}. | ||
| * | ||
| * @return the built {@code HedgeSettings} instance. | ||
| */ | ||
| public HedgeSettings build() { | ||
| return new HedgeSettings(this); | ||
| } | ||
| } | ||
| } |
65 changes: 65 additions & 0 deletions
65
...pubsub/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/v1/HedgeTokenBucket.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed 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 | ||
| * | ||
| * https://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 com.google.cloud.pubsub.v1; | ||
|
|
||
| import com.google.common.annotations.VisibleForTesting; | ||
| import javax.annotation.concurrent.GuardedBy; | ||
|
|
||
| /** Token bucket for limiting hedged requests. Thread-safe. */ | ||
| final class HedgeTokenBucket { | ||
| private final int maxTokens; | ||
| private final float refillAmount; | ||
|
|
||
| @GuardedBy("this") | ||
| private float tokens; | ||
|
|
||
| HedgeTokenBucket(HedgeSettings settings) { | ||
| this.maxTokens = settings.getMaxTokens(); | ||
| this.refillAmount = settings.getRefill(); | ||
| this.tokens = maxTokens; | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| HedgeTokenBucket(int maxTokens, float refillAmount) { | ||
| this.maxTokens = maxTokens; | ||
| this.refillAmount = refillAmount; | ||
| this.tokens = maxTokens; | ||
| } | ||
|
|
||
| /** | ||
| * Attempts to acquire a token for a hedged request. | ||
| * | ||
| * @return {@code true} if a token was acquired, {@code false} otherwise. | ||
| */ | ||
| synchronized boolean tryAcquire() { | ||
| if (tokens >= 1.0f) { | ||
| tokens -= 1.0f; | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** Refills the bucket by the configured refill amount, capped at max tokens. */ | ||
| synchronized void refill() { | ||
| tokens = Math.min(maxTokens, tokens + refillAmount); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| synchronized float getTokens() { | ||
| return tokens; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...ubsub/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/v1/HedgeSettingsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed 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 com.google.cloud.pubsub.v1; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertNotNull; | ||
| import static org.junit.Assert.assertThrows; | ||
|
|
||
| import java.time.Duration; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.JUnit4; | ||
|
|
||
| @RunWith(JUnit4.class) | ||
| public class HedgeSettingsTest { | ||
|
|
||
| @Test | ||
| public void testDefaultSettings() { | ||
| HedgeSettings settings = HedgeSettings.newBuilder().build(); | ||
| assertNotNull(settings); | ||
| assertEquals(Duration.ofMillis(100), settings.getHedgeDelay()); | ||
| assertEquals(100, settings.getMaxTokens()); | ||
| assertEquals(0.1f, settings.getRefill(), 0.0001f); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCustomDelay() { | ||
| Duration customDelay = Duration.ofMillis(50); | ||
| HedgeSettings settings = HedgeSettings.newBuilder().setHedgeDelay(customDelay).build(); | ||
| assertNotNull(settings); | ||
| assertEquals(customDelay, settings.getHedgeDelay()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNegativeDelayThrows() { | ||
| assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> HedgeSettings.newBuilder().setHedgeDelay(Duration.ofMillis(-10))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testZeroDelayThrows() { | ||
| assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> HedgeSettings.newBuilder().setHedgeDelay(Duration.ZERO)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNullDelayThrows() { | ||
| assertThrows(NullPointerException.class, () -> HedgeSettings.newBuilder().setHedgeDelay(null)); | ||
| } | ||
| } |
71 changes: 71 additions & 0 deletions
71
...ub/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/v1/HedgeTokenBucketTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed 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 com.google.cloud.pubsub.v1; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.JUnit4; | ||
|
|
||
| @RunWith(JUnit4.class) | ||
| public class HedgeTokenBucketTest { | ||
|
|
||
| @Test | ||
| public void testInitialState() { | ||
| HedgeTokenBucket bucket = new HedgeTokenBucket(10, 0.5f); | ||
| assertEquals(10.0f, bucket.getTokens(), 0.0001f); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAcquire() { | ||
| HedgeTokenBucket bucket = new HedgeTokenBucket(10, 0.5f); | ||
| assertTrue(bucket.tryAcquire()); | ||
| assertEquals(9.0f, bucket.getTokens(), 0.0001f); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAcquireUntilEmpty() { | ||
| HedgeTokenBucket bucket = new HedgeTokenBucket(2, 0.5f); | ||
| assertTrue(bucket.tryAcquire()); | ||
| assertTrue(bucket.tryAcquire()); | ||
| assertFalse(bucket.tryAcquire()); | ||
| assertEquals(0.0f, bucket.getTokens(), 0.0001f); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRefill() { | ||
| HedgeTokenBucket bucket = new HedgeTokenBucket(2, 0.5f); | ||
| assertTrue(bucket.tryAcquire()); // 1.0 left | ||
| assertTrue(bucket.tryAcquire()); // 0.0 left | ||
|
|
||
| bucket.refill(); // 0.5 tokens | ||
| assertFalse(bucket.tryAcquire()); // needs 1.0 | ||
|
|
||
| bucket.refill(); // 1.0 tokens | ||
| assertTrue(bucket.tryAcquire()); // succeeds, 0.0 left | ||
| } | ||
|
|
||
| @Test | ||
| public void testRefillCap() { | ||
| HedgeTokenBucket bucket = new HedgeTokenBucket(2, 0.5f); | ||
| bucket.refill(); // already at max (2) | ||
| assertEquals(2.0f, bucket.getTokens(), 0.0001f); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In performance-sensitive code like the Pub/Sub
Publisher, usingsynchronizedmethods on the critical path can introduce significant lock contention and performance overhead. To protect shared state while ensuring thread safety and visibility, prefer using explicit locks (such asReentrantLock) over thesynchronizedkeyword.References