-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
xds: implement a global map for holding circuit breaker request counters #7588
Merged
voidzcy
merged 5 commits into
grpc:master
from
voidzcy:impl/shared_circuit_breaking_call_counters
Nov 13, 2020
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d3a4576
Introduce an interface for providing per-clusuter request counters.
voidzcy bd222c5
Implement a global map for holding atomic counters used for aggregati…
voidzcy 4d62f73
Put supress warning close to usage.
voidzcy 4bf5aa1
Removed unused import.
voidzcy db6c43d
Fix typo.
voidzcy 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
This file contains 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
100 changes: 100 additions & 0 deletions
100
xds/src/main/java/io/grpc/xds/SharedCallCounterMap.java
This file contains 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,100 @@ | ||
/* | ||
* Copyright 2020 The gRPC Authors | ||
* | ||
* 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 io.grpc.xds; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import io.grpc.xds.EdsLoadBalancer2.CallCounterProvider; | ||
import java.lang.ref.ReferenceQueue; | ||
import java.lang.ref.WeakReference; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
import javax.annotation.Nullable; | ||
import javax.annotation.concurrent.ThreadSafe; | ||
|
||
/** | ||
* The global map for holding circuit breaker atomic counters. | ||
*/ | ||
@ThreadSafe | ||
final class SharedCallCounterMap implements CallCounterProvider { | ||
|
||
private final ReferenceQueue<AtomicLong> refQueue = new ReferenceQueue<>(); | ||
private final Map<String, Map<String, CounterReference>> counters; | ||
|
||
private SharedCallCounterMap() { | ||
this(new HashMap<String, Map<String, CounterReference>>()); | ||
} | ||
|
||
@VisibleForTesting | ||
SharedCallCounterMap(Map<String, Map<String, CounterReference>> counters) { | ||
this.counters = checkNotNull(counters, "counters"); | ||
} | ||
|
||
static SharedCallCounterMap getInstance() { | ||
return SharedCallCounterMapHolder.instance; | ||
} | ||
|
||
@Override | ||
public synchronized AtomicLong getOrCreate(String cluster, @Nullable String edsServiceName) { | ||
Map<String, CounterReference> clusterCounters = counters.get(cluster); | ||
if (clusterCounters == null) { | ||
clusterCounters = new HashMap<>(); | ||
counters.put(cluster, clusterCounters); | ||
} | ||
CounterReference ref = clusterCounters.get(edsServiceName); | ||
AtomicLong counter; | ||
if (ref == null || (counter = ref.get()) == null) { | ||
counter = new AtomicLong(); | ||
ref = new CounterReference(counter, refQueue, cluster, edsServiceName); | ||
clusterCounters.put(edsServiceName, ref); | ||
} | ||
cleanQueue(); | ||
return counter; | ||
} | ||
|
||
@VisibleForTesting | ||
void cleanQueue() { | ||
CounterReference ref; | ||
while ((ref = (CounterReference) refQueue.poll()) != null) { | ||
Map<String, CounterReference> clusterCounter = counters.get(ref.cluster); | ||
clusterCounter.remove(ref.edsServiceName); | ||
if (clusterCounter.isEmpty()) { | ||
counters.remove(ref.cluster); | ||
} | ||
} | ||
} | ||
|
||
@VisibleForTesting | ||
static final class CounterReference extends WeakReference<AtomicLong> { | ||
private final String cluster; | ||
@Nullable | ||
private final String edsServiceName; | ||
|
||
CounterReference(AtomicLong counter, ReferenceQueue<AtomicLong> refQueue, String cluster, | ||
@Nullable String edsServiceName) { | ||
super(counter, refQueue); | ||
this.cluster = cluster; | ||
this.edsServiceName = edsServiceName; | ||
} | ||
} | ||
|
||
private static final class SharedCallCounterMapHolder { | ||
private static final SharedCallCounterMap instance = new SharedCallCounterMap(); | ||
} | ||
} |
This file contains 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
59 changes: 59 additions & 0 deletions
59
xds/src/test/java/io/grpc/xds/SharedCallCounterMapTest.java
This file contains 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,59 @@ | ||
/* | ||
* Copyright 2019 The gRPC Authors | ||
* | ||
* 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 io.grpc.xds; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.common.testing.GcFinalization; | ||
import io.grpc.xds.SharedCallCounterMap.CounterReference; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
/** | ||
* Tests for {@link SharedCallCounterMap}. | ||
*/ | ||
@RunWith(JUnit4.class) | ||
public class SharedCallCounterMapTest { | ||
|
||
private static final String CLUSTER = "cluster-foo.googleapis.com"; | ||
private static final String EDS_SERVICE_NAME = null; | ||
|
||
private final Map<String, Map<String, CounterReference>> counters = new HashMap<>(); | ||
private final SharedCallCounterMap map = new SharedCallCounterMap(counters); | ||
|
||
@Test | ||
public void sharedCounterInstance() { | ||
AtomicLong counter1 = map.getOrCreate(CLUSTER, EDS_SERVICE_NAME); | ||
AtomicLong counter2 = map.getOrCreate(CLUSTER, EDS_SERVICE_NAME); | ||
assertThat(counter2).isSameInstanceAs(counter1); | ||
} | ||
|
||
@Test | ||
public void autoCleanUp() { | ||
@SuppressWarnings("UnusedVariable") | ||
AtomicLong counter = map.getOrCreate(CLUSTER, EDS_SERVICE_NAME); | ||
CounterReference ref = counters.get(CLUSTER).get(EDS_SERVICE_NAME); | ||
counter = null; | ||
GcFinalization.awaitClear(ref); | ||
map.cleanQueue(); | ||
assertThat(counters).isEmpty(); | ||
} | ||
} |
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.
What about avoiding assignment inside if-condition?
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.
Why choose something that is more verbose? 😄
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.
Because assignment inside if-condition is less readable.
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.
Well, that should not be considered as a readability issue, that's what the language feature is, just like all the one liners in Python.