Skip to content

rls: Document RefCountedChildPolicyWrapperFactory as non-threadsafe #11124

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

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rls/src/main/java/io/grpc/rls/CachingRlsLbClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ final class CachingRlsLbClient {
private final RouteLookupServiceStub rlsStub;
private final RlsPicker rlsPicker;
private final ResolvedAddressFactory childLbResolvedAddressFactory;
@GuardedBy("lock")
private final RefCountedChildPolicyWrapperFactory refCountedChildPolicyWrapperFactory;
private final ChannelLogger logger;

Expand Down
15 changes: 5 additions & 10 deletions rls/src/main/java/io/grpc/rls/LbPolicyConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong;
import javax.annotation.Nullable;

/** Configuration for RLS load balancing policy. */
Expand Down Expand Up @@ -203,9 +202,8 @@ public String toString() {
}
}

/** Factory for {@link ChildPolicyWrapper}. */
/** Factory for {@link ChildPolicyWrapper}. Not thread-safe. */
static final class RefCountedChildPolicyWrapperFactory {
// GuardedBy CachingRlsLbClient.lock
@VisibleForTesting
final Map<String /* target */, RefCountedChildPolicyWrapper> childPolicyMap =
new HashMap<>();
Expand All @@ -227,7 +225,6 @@ public RefCountedChildPolicyWrapperFactory(
this.childLbStatusListener = checkNotNull(childLbStatusListener, "childLbStatusListener");
}

// GuardedBy CachingRlsLbClient.lock
ChildPolicyWrapper createOrGet(String target) {
// TODO(creamsoup) check if the target is valid or not
RefCountedChildPolicyWrapper pooledChildPolicyWrapper = childPolicyMap.get(target);
Expand All @@ -247,7 +244,6 @@ ChildPolicyWrapper createOrGet(String target) {
}
}

// GuardedBy CachingRlsLbClient.lock
List<ChildPolicyWrapper> createOrGet(List<String> targets) {
List<ChildPolicyWrapper> retVal = new ArrayList<>();
for (String target : targets) {
Expand All @@ -256,7 +252,6 @@ List<ChildPolicyWrapper> createOrGet(List<String> targets) {
return retVal;
}

// GuardedBy CachingRlsLbClient.lock
void release(ChildPolicyWrapper childPolicyWrapper) {
checkNotNull(childPolicyWrapper, "childPolicyWrapper");
String target = childPolicyWrapper.getTarget();
Expand Down Expand Up @@ -402,7 +397,7 @@ interface ChildLbStatusListener {
private static final class RefCountedChildPolicyWrapper
implements ObjectPool<ChildPolicyWrapper> {

private final AtomicLong refCnt = new AtomicLong();
private long refCnt;
@Nullable
private ChildPolicyWrapper childPolicyWrapper;

Expand All @@ -413,7 +408,7 @@ private RefCountedChildPolicyWrapper(ChildPolicyWrapper childPolicyWrapper) {
@Override
public ChildPolicyWrapper getObject() {
checkState(!isReleased(), "ChildPolicyWrapper is already released");
refCnt.getAndIncrement();
refCnt++;
return childPolicyWrapper;
}

Expand All @@ -426,7 +421,7 @@ public ChildPolicyWrapper returnObject(Object object) {
checkState(
childPolicyWrapper == object,
"returned object doesn't match the pooled childPolicyWrapper");
long newCnt = refCnt.decrementAndGet();
long newCnt = --refCnt;
checkState(newCnt != -1, "Cannot return never pooled childPolicyWrapper");
if (newCnt == 0) {
childPolicyWrapper.shutdown();
Expand All @@ -447,7 +442,7 @@ static RefCountedChildPolicyWrapper of(ChildPolicyWrapper childPolicyWrapper) {
public String toString() {
return MoreObjects.toStringHelper(this)
.add("object", childPolicyWrapper)
.add("refCnt", refCnt.get())
.add("refCnt", refCnt)
.toString();
}
}
Expand Down