Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ public class SpannerOptions extends ServiceOptions<Spanner, SpannerOptions> {
"https://www.googleapis.com/auth/spanner.admin",
"https://www.googleapis.com/auth/spanner.data");
private static final int MAX_CHANNELS = 256;
private static final int DEFAULT_CHANNELS = 4;
// Set the default number of channels to GRPC_GCP_ENABLED_DEFAULT_CHANNELS when gRPC-GCP extension
// is enabled,
// to make sure there are sufficient channels available to move the sessions to a different
// channel if a network connection in a particular channel fails.
@VisibleForTesting static final int GRPC_GCP_ENABLED_DEFAULT_CHANNELS = 8;
private final TransportChannelProvider channelProvider;

@SuppressWarnings("rawtypes")
Expand Down Expand Up @@ -669,8 +675,7 @@ public static class Builder

private GrpcInterceptorProvider interceptorProvider;

/** By default, we create 4 channels per {@link SpannerOptions} */
private int numChannels = 4;
private Integer numChannels;

private String transportChannelExecutorThreadNameFormat = "Cloud-Spanner-TransportChannel-%d";

Expand Down Expand Up @@ -1122,8 +1127,7 @@ public Builder setHost(String host) {

/** Enables gRPC-GCP extension with the default settings. */
public Builder enableGrpcGcpExtension() {
this.grpcGcpExtensionEnabled = true;
return this;
return this.enableGrpcGcpExtension(null);
}

/**
Expand All @@ -1133,6 +1137,9 @@ public Builder enableGrpcGcpExtension() {
public Builder enableGrpcGcpExtension(GcpManagedChannelOptions options) {
this.grpcGcpExtensionEnabled = true;
this.grpcGcpOptions = options;
if (this.numChannels == null) {
this.numChannels = GRPC_GCP_ENABLED_DEFAULT_CHANNELS;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove this and instead assign the default number of channels in the build method for both GCP enabled and disabled. (See also below)

return this;
}

Expand Down Expand Up @@ -1166,6 +1173,10 @@ public SpannerOptions build() {
// As we are using plain text, we should never send any credentials.
this.setCredentials(NoCredentials.getInstance());
}
if (this.numChannels == null) {
this.numChannels = DEFAULT_CHANNELS;
Copy link
Collaborator

Choose a reason for hiding this comment

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

this.num

Suggested change
this.numChannels = DEFAULT_CHANNELS;
this.numChannels = this.grpcGcpExtensionEnabled ? GRPC_GCP_ENABLED_DEFAULT_CHANNELS : DEFAULT_CHANNELS;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion @olavloite. This makes it more readable than the previous one.
I have made the changes in the recent commit.

}

return new SpannerOptions(this);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -918,4 +918,38 @@ public void testCustomAsyncExecutorProvider() {
.build();
assertSame(service, options.getAsyncExecutorProvider().getExecutor());
}

@Test
public void testDefaultNumChannelsWithGrpcGcpExtensionEnabled() {
SpannerOptions options =
SpannerOptions.newBuilder().setProjectId("test-project").enableGrpcGcpExtension().build();

assertEquals(options.getNumChannels(), SpannerOptions.GRPC_GCP_ENABLED_DEFAULT_CHANNELS);
}

@Test
public void testNumChannelsWithGrpcGcpExtensionEnabled() {
// Set number of channels explicitly, before enabling gRPC-GCP channel pool in SpannerOptions
// builder.
int numChannels = 5;
SpannerOptions options1 =
SpannerOptions.newBuilder()
.setProjectId("test-project")
.setNumChannels(numChannels)
.enableGrpcGcpExtension()
.build();

assertEquals(options1.getNumChannels(), numChannels);

// Set number of channels explicitly, after enabling gRPC-GCP channel pool in SpannerOptions
// builder.
SpannerOptions options2 =
SpannerOptions.newBuilder()
.setProjectId("test-project")
.enableGrpcGcpExtension()
.setNumChannels(numChannels)
.build();

assertEquals(options2.getNumChannels(), numChannels);
}
}