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

Add disable affinity feature #167

Merged
merged 1 commit into from
Oct 25, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.grpc.CallOptions;
import io.grpc.ClientCall;
import io.grpc.ConnectivityState;
import io.grpc.Context;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.MethodDescriptor;
Expand Down Expand Up @@ -72,6 +73,10 @@ public class GcpManagedChannel extends ManagedChannel {
static final AtomicInteger channelPoolIndex = new AtomicInteger();
static final int DEFAULT_MAX_CHANNEL = 10;
static final int DEFAULT_MAX_STREAM = 100;
public static final Context.Key<Boolean> DISABLE_AFFINITY_CTX_KEY =
Context.keyWithDefault("DisableAffinity", false);
public static final CallOptions.Key<Boolean> DISABLE_AFFINITY_KEY =
CallOptions.Key.createWithDefault("DisableAffinity", false);

@GuardedBy("this")
private Integer bindingIndex = -1;
Expand Down Expand Up @@ -1224,7 +1229,10 @@ public String authority() {
public <ReqT, RespT> ClientCall<ReqT, RespT> newCall(
MethodDescriptor<ReqT, RespT> methodDescriptor, CallOptions callOptions) {
AffinityConfig affinity = methodToAffinity.get(methodDescriptor.getFullMethodName());
if (affinity == null) {
if (
affinity == null
|| callOptions.getOption(DISABLE_AFFINITY_KEY)
|| DISABLE_AFFINITY_CTX_KEY.get(Context.current())) {
return new GcpClientCall.SimpleGcpClientCall<>(
getChannelRef(null), methodDescriptor, callOptions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,99 @@ public void testExecuteStreamingSqlAsync() throws Exception {
deleteAsyncSessions(stub, respNames);
}

@Test
public void testExecuteStreamingSqlWithAffinityDisabledViaContext() throws Exception {
SpannerStub stub = getSpannerStub();
String sessionName = createAsyncSessions(stub).get(0);
AsyncResponseObserver<PartialResultSet> resp = new AsyncResponseObserver<>();
stub.executeStreamingSql(
ExecuteSqlRequest
.newBuilder()
.setSession(sessionName)
.setSql("select * FROM Users")
.build(),
resp);
// The ChannelRef which is bound with the current affinity key.
ChannelRef currentChannel =
gcpChannel.affinityKeyToChannelRef.get(sessionName);
// Verify the channel is in use.
assertEquals(1, currentChannel.getActiveStreamsCount());
PartialResultSet response = resp.get();
assertEquals(USERNAME, response.getValues(1).getStringValue());
assertEquals(0, currentChannel.getActiveStreamsCount());

Context ctx = Context.current().withValue(GcpManagedChannel.DISABLE_AFFINITY_CTX_KEY, true);
List<AsyncResponseObserver<PartialResultSet>> resps = new ArrayList<>();
for (int i = 0; i < MAX_CHANNEL; i++) {
// Execute the call in the context where affinity is disabled.
ctx.run(() -> {
AsyncResponseObserver<PartialResultSet> r = new AsyncResponseObserver<>();
resps.add(r);
stub.executeStreamingSql(
ExecuteSqlRequest.newBuilder().setSession(sessionName).setSql("select * FROM Users").build(),
r);
});
}
// Verify calls with disabled affinity are distributed accross all channels.
for (ChannelRef ch : gcpChannel.channelRefs) {
assertEquals(1, ch.getActiveStreamsCount());
}

for (AsyncResponseObserver<PartialResultSet> r : resps) {
response = r.get();
assertEquals(USERNAME, response.getValues(1).getStringValue());
assertEquals(0, currentChannel.getActiveStreamsCount());
}
}

@Test
public void testExecuteStreamingSqlWithAffinityDisabledViaCallOptions() throws Exception {
SpannerStub stub = getSpannerStub();
String sessionName = createAsyncSessions(stub).get(0);
AsyncResponseObserver<PartialResultSet> resp = new AsyncResponseObserver<>();
stub.executeStreamingSql(
ExecuteSqlRequest
.newBuilder()
.setSession(sessionName)
.setSql("select * FROM Users")
.build(),
resp);
// The ChannelRef which is bound with the current affinity key.
ChannelRef currentChannel =
gcpChannel.affinityKeyToChannelRef.get(sessionName);
// Verify the channel is in use.
assertEquals(1, currentChannel.getActiveStreamsCount());
PartialResultSet response = resp.get();
assertEquals(USERNAME, response.getValues(1).getStringValue());
assertEquals(0, currentChannel.getActiveStreamsCount());

List<AsyncResponseObserver<PartialResultSet>> resps = new ArrayList<>();
for (int i = 0; i < MAX_CHANNEL; i++) {
AsyncResponseObserver<PartialResultSet> r = new AsyncResponseObserver<>();
resps.add(r);
// Execute the call with affinity disabled via call option.
stub
.withOption(GcpManagedChannel.DISABLE_AFFINITY_KEY, true)
.executeStreamingSql(
ExecuteSqlRequest
.newBuilder()
.setSession(sessionName)
.setSql("select * FROM Users")
.build(),
r);
}
// Verify calls with disabled affinity are distributed accross all channels.
for (ChannelRef ch : gcpChannel.channelRefs) {
assertEquals(1, ch.getActiveStreamsCount());
}

for (AsyncResponseObserver<PartialResultSet> r : resps) {
response = r.get();
assertEquals(USERNAME, response.getValues(1).getStringValue());
assertEquals(0, currentChannel.getActiveStreamsCount());
}
}

@Test
public void testPartitionQueryAsync() throws Exception {
SpannerStub stub = getSpannerStub();
Expand Down