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

chore: add directpath_enabled attribute #3380

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public class BuiltInMetricsConstant {
DIRECT_PATH_ENABLED_KEY,
DIRECT_PATH_USED_KEY);

public static boolean DIRECT_PATH_ENABLED;
static Aggregation AGGREGATION_WITH_MILLIS_HISTOGRAM =
Aggregation.explicitBucketHistogram(
ImmutableList.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public void addAttributes(Map<String, String> attributes) {
for (ApiTracer child : children) {
if (child instanceof MetricsTracer) {
MetricsTracer metricsTracer = (MetricsTracer) child;
attributes.forEach((key, value) -> metricsTracer.addAttributes(key, value));
metricsTracer.addAttributes(attributes);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.google.api.gax.grpc.GrpcCallContext;
import com.google.api.gax.grpc.GrpcCallSettings;
import com.google.api.gax.grpc.GrpcStubCallableFactory;
import com.google.api.gax.grpc.GrpcTransportChannel;
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
import com.google.api.gax.longrunning.OperationFuture;
import com.google.api.gax.retrying.ResultRetryAlgorithm;
Expand Down Expand Up @@ -63,6 +64,7 @@
import com.google.cloud.grpc.GrpcTransportOptions;
import com.google.cloud.spanner.AdminRequestsPerMinuteExceededException;
import com.google.cloud.spanner.BackupId;
import com.google.cloud.spanner.BuiltInMetricsConstant;
import com.google.cloud.spanner.ErrorCode;
import com.google.cloud.spanner.Restore;
import com.google.cloud.spanner.SpannerException;
Expand Down Expand Up @@ -396,18 +398,22 @@ public GapicSpannerRpc(final SpannerOptions options) {
final String emulatorHost = System.getenv("SPANNER_EMULATOR_HOST");

try {
this.spannerStub =
GrpcSpannerStub.create(
options
.getSpannerStubSettings()
.toBuilder()
.setTransportChannelProvider(channelProvider)
.setCredentialsProvider(credentialsProvider)
.setStreamWatchdogProvider(watchdogProvider)
.setTracerFactory(
options.getApiTracerFactory(
/* isAdminClient = */ false, isEmulatorEnabled(options, emulatorHost)))
.build());
SpannerStubSettings spannerStubSettings =
options
.getSpannerStubSettings()
.toBuilder()
.setTransportChannelProvider(channelProvider)
.setCredentialsProvider(credentialsProvider)
.setStreamWatchdogProvider(watchdogProvider)
.setTracerFactory(
options.getApiTracerFactory(
/* isAdminClient = */ false, isEmulatorEnabled(options, emulatorHost)))
.build();
ClientContext clientContext = ClientContext.create(spannerStubSettings);
this.spannerStub = GrpcSpannerStub.create(spannerStubSettings, clientContext);
Copy link
Collaborator

Choose a reason for hiding this comment

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

See below: This calls a method in a generated file, and this method is manually added to that generated file. That will be overwritten the next time the code generation is run, so this won't work.

BuiltInMetricsConstant.DIRECT_PATH_ENABLED =
Copy link
Collaborator

Choose a reason for hiding this comment

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

A user could create multiple Spanner instances. Then this code will be invoked multiple times. There is no guarantee that isDirectPath() will return the same value each time, as it depends on (AFAIK) the credentials that are used. DIRECT_PATH_ENABLED is a public static variable that should preferably only be set once (or changed to an instance field of the SpannerImpl class or one of the other underlying classes)

((GrpcTransportChannel) clientContext.getTransportChannel()).isDirectPath()
&& isAttemptDirectPathXds;
this.readRetrySettings =
options.getSpannerStubSettings().streamingReadSettings().getRetrySettings();
this.readRetryableCodes =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ private Map<String, String> getBuiltInMetricAttributes(String key, DatabaseName
attributes.put(BuiltInMetricsConstant.DATABASE_KEY.getKey(), databaseName.getDatabase());
attributes.put(
BuiltInMetricsConstant.INSTANCE_ID_KEY.getKey(), databaseName.getInstance());
attributes.put(
BuiltInMetricsConstant.DIRECT_PATH_ENABLED_KEY.getKey(),
String.valueOf(BuiltInMetricsConstant.DIRECT_PATH_ENABLED));
return attributes;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,11 @@ public static final GrpcSpannerStub create(ClientContext clientContext) throws I
return new GrpcSpannerStub(SpannerStubSettings.newBuilder().build(), clientContext);
}

public static final GrpcSpannerStub create(
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a generated file, so you can't manually update it. (That is: you can update it, but the next round of code generation will overwrite your changes)

SpannerStubSettings settings, ClientContext clientContext) throws IOException {
return new GrpcSpannerStub(settings, clientContext);
}

public static final GrpcSpannerStub create(
ClientContext clientContext, GrpcStubCallableFactory callableFactory) throws IOException {
return new GrpcSpannerStub(
Expand Down
Loading