Skip to content
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: Fix WeakReference bug in SharedCallCounterMap #8466

Merged
merged 6 commits into from Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 17 additions & 8 deletions xds/src/main/java/io/grpc/xds/SharedCallCounterMap.java
Expand Up @@ -52,19 +52,28 @@ static SharedCallCounterMap getInstance() {

@Override
public synchronized AtomicLong getOrCreate(String cluster, @Nullable String edsServiceName) {
AtomicLong counter = null;
Map<String, CounterReference> clusterCounters = counters.get(cluster);
if (clusterCounters != null) {
CounterReference ref = clusterCounters.get(edsServiceName);
if (ref != null) {
counter = ref.get();
}
}
// In the case of ref.get() == null, must call cleanQueue() prior to creating a new map entry,
// otherwise the new entry will be deleted by any later cleanQueue().
ejona86 marked this conversation as resolved.
Show resolved Hide resolved
cleanQueue();
if (counter != null) {
return counter;
}
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();
counter = new AtomicLong();
CounterReference ref = new CounterReference(counter, refQueue, cluster, edsServiceName);
clusterCounters.put(edsServiceName, ref);
return counter;
}

Expand Down
18 changes: 18 additions & 0 deletions xds/src/test/java/io/grpc/xds/SharedCallCounterMapTest.java
Expand Up @@ -62,4 +62,22 @@ public boolean isDone() {
map.cleanQueue();
assertThat(counters).isEmpty();
}

@Test
public void gcAndRecreate() {
@SuppressWarnings("UnusedVariable") // assign to null for GC only
AtomicLong counter = map.getOrCreate(CLUSTER, EDS_SERVICE_NAME);
final CounterReference ref = counters.get(CLUSTER).get(EDS_SERVICE_NAME);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, you can call ref.clear() and ref.enqueue() manually instead of relying on GC.

assertThat(counter.get()).isEqualTo(0);
counter = null;
GcFinalization.awaitDone(new FinalizationPredicate() {
@Override
public boolean isDone() {
return ref.isEnqueued();
}
});
map.getOrCreate(CLUSTER, EDS_SERVICE_NAME);
assertThat(counters.get(CLUSTER)).isNotNull();
assertThat(counters.get(CLUSTER).get(EDS_SERVICE_NAME)).isNotNull();
}
}