Skip to content
Open
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 @@ -1172,4 +1172,11 @@
<className>com/google/cloud/spanner/DatabaseClient</className>
<method>java.lang.String getDatabaseDefaultIsolationLevelValue()</method>
</difference>

<!-- Added universe domain environment option -->
<difference>
<differenceType>7012</differenceType>
<className>com/google/cloud/spanner/SpannerOptions$SpannerEnvironment</className>
<method>java.lang.String getUniverseDomain()</method>
</difference>
</differences>
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
import java.util.logging.Logger;
import javax.annotation.Nullable;

final class BuiltInMetricsProvider {
class BuiltInMetricsProvider {

static BuiltInMetricsProvider INSTANCE = new BuiltInMetricsProvider();

Expand All @@ -78,7 +78,9 @@ final class BuiltInMetricsProvider {
private boolean mismatchedProjectIdLogged;
private Thread shutdownHook;

private BuiltInMetricsProvider() {}
// Package-private and non-final so tests can record the arguments passed by SpannerOptions.
@VisibleForTesting
BuiltInMetricsProvider() {}

synchronized OpenTelemetry getOrCreateOpenTelemetry(
String projectId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
import java.util.logging.Logger;
import java.util.regex.Pattern;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.jspecify.annotations.Nullable;

/** Options for the Cloud Spanner service. */
public class SpannerOptions extends ServiceOptions<Spanner, SpannerOptions> {
Expand All @@ -136,6 +136,7 @@ public class SpannerOptions extends ServiceOptions<Spanner, SpannerOptions> {
private static final String API_SHORT_NAME = "Spanner";
private static final String SPANNER_SERVICE_NAME = "spanner";
private static final String GOOGLE_DEFAULT_UNIVERSE = "googleapis.com";
private static final String GOOGLE_CLOUD_UNIVERSE_DOMAIN_ENV_VAR = "GOOGLE_CLOUD_UNIVERSE_DOMAIN";
private static final int MAX_DNS_HOSTNAME_LENGTH = 253;
private static final Pattern VALID_UNIVERSE_DOMAIN_PATTERN =
Pattern.compile(
Expand Down Expand Up @@ -323,7 +324,10 @@ static GcpChannelPoolOptions mergeWithDefaultChannelPoolOptions(
private final boolean trackTransactionStarter;
private final boolean enableGrpcGcpOtelMetrics;
private final boolean enableChannelPoolBuiltInMetrics;
private final BuiltInMetricsProvider builtInMetricsProvider = BuiltInMetricsProvider.INSTANCE;

// Non-final so tests can observe the arguments passed to the process-wide metrics provider.
@VisibleForTesting
BuiltInMetricsProvider builtInMetricsProvider = BuiltInMetricsProvider.INSTANCE;

/**
* These are the default {@link QueryOptions} defined by the user on this {@link SpannerOptions}.
Expand Down Expand Up @@ -460,8 +464,7 @@ public interface CallContextConfigurator {
* ApiCallContext}. If <code>null</code> is returned, no changes to the default {@link
* ApiCallContext} will be made.
*/
@Nullable
<ReqT, RespT> ApiCallContext configure(
@Nullable <ReqT, RespT> ApiCallContext configure(
ApiCallContext context, ReqT request, MethodDescriptor<ReqT, RespT> method);
}

Expand Down Expand Up @@ -990,18 +993,20 @@ protected SpannerOptions(Builder builder) {
databaseRole = builder.databaseRole;
sessionLabels = builder.sessionLabels;
try {
String resolvedUniversalDomain = getResolvedUniverseDomain();
// Resolve and validate the universe domain explicitly so validation does not rely solely on
// ServiceOptions eagerly evaluating getDefaultHost().
String resolvedUniverseDomain = getResolvedUniverseDomain();
spannerStubSettings =
builder.spannerStubSettingsBuilder.setUniverseDomain(resolvedUniversalDomain).build();
builder.spannerStubSettingsBuilder.setUniverseDomain(resolvedUniverseDomain).build();
instanceAdminStubSettings =
builder
.instanceAdminStubSettingsBuilder
.setUniverseDomain(resolvedUniversalDomain)
.setUniverseDomain(resolvedUniverseDomain)
.build();
databaseAdminStubSettings =
builder
.databaseAdminStubSettingsBuilder
.setUniverseDomain(resolvedUniversalDomain)
.setUniverseDomain(resolvedUniverseDomain)
.build();
} catch (IOException e) {
throw SpannerExceptionFactory.newSpannerException(e);
Expand Down Expand Up @@ -1118,7 +1123,8 @@ protected SpannerOptions(Builder builder) {
* inject URL components (such as userinfo, port, path, query, or fragment) into constructed
* endpoint URLs.
*/
private static void validateUniverseDomain(@Nullable String universeDomain) {
@InternalApi
public static void validateUniverseDomain(@Nullable String universeDomain) {
if (Strings.isNullOrEmpty(universeDomain)) {
return;
}
Expand All @@ -1129,15 +1135,35 @@ private static void validateUniverseDomain(@Nullable String universeDomain) {
universeDomain);
}

private String getResolvedUniverseDomain() {
String universeDomain = getUniverseDomain();
/**
* Resolves the effective universe domain using the precedence: explicit configuration, then
* {@link SpannerEnvironment#getUniverseDomain()} ({@code GOOGLE_CLOUD_UNIVERSE_DOMAIN}), then
* {@code "googleapis.com"}. Validates any non-empty domain against RFC 1123 hostname constraints.
*/
@InternalApi
public static String resolveUniverseDomain(@Nullable String configuredUniverseDomain) {
return resolveUniverseDomain(configuredUniverseDomain, environment.getUniverseDomain());
}

@VisibleForTesting
static String resolveUniverseDomain(
@Nullable String configuredUniverseDomain, @Nullable String envUniverseDomain) {
String universeDomain =
!Strings.isNullOrEmpty(configuredUniverseDomain)
? configuredUniverseDomain
: envUniverseDomain;
if (Strings.isNullOrEmpty(universeDomain)) {
return GOOGLE_DEFAULT_UNIVERSE;
}
validateUniverseDomain(universeDomain);
return universeDomain;
}

@VisibleForTesting
String getResolvedUniverseDomain() {
return resolveUniverseDomain(getUniverseDomain());
}

/** Returns the default {@link RequestOptions.ClientContext} for this {@link SpannerOptions}. */
public RequestOptions.ClientContext getClientContext() {
return clientContext;
Expand Down Expand Up @@ -1229,6 +1255,14 @@ default String getMonitoringHost() {
default GoogleCredentials getDefaultSpannerOmniCredentials() {
return null;
}

/**
* Returns the universe domain configured in the environment (for example via {@code
* GOOGLE_CLOUD_UNIVERSE_DOMAIN}), or {@code null} if not set.
*/
default @Nullable String getUniverseDomain() {
return null;
}
}

static final String DEFAULT_SPANNER_OMNI_CREDENTIALS = "SPANNER_OMNI_AUTH_TOKEN";
Expand Down Expand Up @@ -1358,6 +1392,11 @@ public String getMonitoringHost() {
public GoogleCredentials getDefaultSpannerOmniCredentials() {
return getOAuthTokenFromFile(System.getenv(DEFAULT_SPANNER_OMNI_CREDENTIALS));
}

@Override
public @Nullable String getUniverseDomain() {
return System.getenv(GOOGLE_CLOUD_UNIVERSE_DOMAIN_ENV_VAR);
}
}

/** Builder for {@link SpannerOptions} instances. */
Expand Down Expand Up @@ -3025,7 +3064,7 @@ public ApiTracerFactory getApiTracerFactory() {
@InternalApi
public OpenTelemetry getBuiltInOpenTelemetry() {
return this.builtInMetricsProvider.getOrCreateOpenTelemetry(
this.getProjectId(), getCredentials(), this.monitoringHost, getUniverseDomain());
this.getProjectId(), getCredentials(), this.monitoringHost, getResolvedUniverseDomain());
}

/**
Expand Down Expand Up @@ -3065,7 +3104,7 @@ public void enablegRPCMetrics(
this.getProjectId(),
getCredentials(),
this.monitoringHost,
getUniverseDomain());
getResolvedUniverseDomain());
}
if (usesCustomMetricsProvider()) {
OpenTelemetry customOpenTelemetry =
Expand Down Expand Up @@ -3164,7 +3203,10 @@ private ApiTracerFactory createCustomMetricsTracerFactory() {
private ApiTracerFactory createCloudMonitoringMetricsTracerFactory() {
OpenTelemetry openTelemetry =
this.builtInMetricsProvider.getOrCreateOpenTelemetry(
this.getProjectId(), getCredentials(), this.monitoringHost, getUniverseDomain());
this.getProjectId(),
getCredentials(),
this.monitoringHost,
getResolvedUniverseDomain());

return openTelemetry != null
? new BuiltInMetricsTracerFactory(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,6 @@ public class ConnectionOptions {
private static final String PLAIN_TEXT_PROTOCOL = "http:";
private static final String HOST_PROTOCOL = "https:";
private static final String SPANNER_SERVICE_NAME = "spanner";
private static final int MAX_DNS_HOSTNAME_LENGTH = 253;
private static final Pattern VALID_UNIVERSE_DOMAIN_PATTERN =
Pattern.compile(
"^[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?"
+ "(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$");
private static final String SPANNER_EMULATOR_HOST_ENV_VAR = "SPANNER_EMULATOR_HOST";
private static final String DEFAULT_EMULATOR_HOST = "http://localhost:9010";

Expand Down Expand Up @@ -765,7 +760,7 @@ private ConnectionOptions(Builder builder) {

// Create the initial connection state from the parsed properties in the connection URL.
this.initialConnectionState = new ConnectionState(connectionPropertyValues);
validateUniverseDomain(getUniverseDomain());
SpannerOptions.validateUniverseDomain(getUniverseDomain());

checkGuardedProperty(
getInitialConnectionPropertyValue(ENCODED_CREDENTIALS),
Expand Down Expand Up @@ -1190,33 +1185,13 @@ public Integer getDcpConcurrentStreamsLowWatermark() {
return getInitialConnectionPropertyValue(DCP_CONCURRENT_STREAMS_LOW_WATERMARK);
}

/**
* Validates that a non-empty universe domain is a valid RFC 1123 DNS domain name so it cannot
* inject URL components (such as userinfo, port, path, query, or fragment) into constructed
* endpoint URLs.
*/
private static void validateUniverseDomain(@Nullable String universeDomain) {
if (Strings.isNullOrEmpty(universeDomain)) {
return;
}
Preconditions.checkArgument(
SPANNER_SERVICE_NAME.length() + 1 + universeDomain.length() <= MAX_DNS_HOSTNAME_LENGTH
&& VALID_UNIVERSE_DOMAIN_PATTERN.matcher(universeDomain).matches(),
"Invalid universe domain: %s",
universeDomain);
}

/**
* Returns the default host URL derived from the configured universe domain, falling back to
* {@link Credentials#GOOGLE_DEFAULT_UNIVERSE} when null or empty.
* {@code GOOGLE_CLOUD_UNIVERSE_DOMAIN} and then {@link Credentials#GOOGLE_DEFAULT_UNIVERSE} when
* null or empty.
*/
private String getDefaultHost() {
String universeDomain = getUniverseDomain();
if (Strings.isNullOrEmpty(universeDomain)) {
universeDomain = Credentials.GOOGLE_DEFAULT_UNIVERSE;
} else {
validateUniverseDomain(universeDomain);
}
String universeDomain = SpannerOptions.resolveUniverseDomain(getUniverseDomain());
return String.format("%s//%s.%s", HOST_PROTOCOL, SPANNER_SERVICE_NAME, universeDomain);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public class ConnectionProperties {
"universeDomain",
"Configure the connection to try to connect to Spanner using "
+ "a different partner Google Universe than GDU (googleapis.com).",
"googleapis.com",
null,
StringValueConverter.INSTANCE,
Context.STARTUP);
static final ConnectionProperty<Boolean> USE_AUTO_SAVEPOINTS_FOR_EMULATOR =
Expand Down
Loading
Loading