Skip to content

fix(spanner): honor GOOGLE_CLOUD_UNIVERSE_DOMAIN when universe domain is unset - #15142

Open
sakthivelmanii wants to merge 1 commit into
mainfrom
fix/spanner-universe-domain-env
Open

sakthivelmanii wants to merge 1 commit into
mainfrom
fix/spanner-universe-domain-env

Conversation

@sakthivelmanii

@sakthivelmanii sakthivelmanii commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Problem Statement

  • When no universe domain was set explicitly, the Spanner client coerced it to googleapis.com for the data, instance admin, and database admin stubs and for the default host, so GOOGLE_CLOUD_UNIVERSE_DOMAIN was ignored.
  • The universeDomain connection property defaulted to googleapis.com, so ConnectionOptions and pooled Spanner clients also ignored GOOGLE_CLOUD_UNIVERSE_DOMAIN.
  • Built-in metrics received the unresolved universe domain rather than the one used by the Spanner stubs.

Solution

  • Add SpannerOptions.SpannerEnvironment#getUniverseDomain(), backed by GOOGLE_CLOUD_UNIVERSE_DOMAIN.
  • Resolve the universe domain with the precedence: explicit setting, then environment, then googleapis.com, and use that value for the stubs, the default host, and built-in metrics.
  • The universeDomain connection property is now unset by default, so ConnectionOptions#getUniverseDomain() returns null when it is not configured.
  • An unset or empty GOOGLE_CLOUD_UNIVERSE_DOMAIN still falls back to googleapis.com.
  • An invalid GOOGLE_CLOUD_UNIVERSE_DOMAIN fails with IllegalArgumentException at SpannerOptions.Builder#build() (including when a host or emulator host is set) and with INVALID_ARGUMENT in ConnectionOptions#getChannelProvider().

@sakthivelmanii
sakthivelmanii requested review from a team as code owners November 22, 2026 17:50

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request updates the Spanner client to support resolving the universe domain from the GOOGLE_CLOUD_UNIVERSE_DOMAIN environment variable. It refactors universe domain resolution and validation logic in SpannerOptions and ConnectionOptions to ensure consistent precedence (explicit configuration > environment variable > default "googleapis.com") and delegation to GAX stub builders. Additionally, comprehensive integration and unit tests using forked JVM processes have been added to verify these behaviors. I have no feedback to provide as there are no review comments.

@olavloite olavloite left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A few issues to address before merging:

1. Use SpannerEnvironment instead of System.getenv(...) and forked-JVM probes

SpannerOptions already provides SpannerEnvironment (SpannerOptions.useEnvironment(...) / SpannerOptions.useDefaultEnvironment()) so environment-variable configuration can be tested in-process (like getMonitoringHost() and getOptimizerVersion()).

Instead of calling System.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") directly and using forked JVMs to test GAX's EndpointContext:

  1. Add default @Nullable String getUniverseDomain() { return null; } to SpannerOptions.SpannerEnvironment, returning System.getenv(GOOGLE_CLOUD_UNIVERSE_DOMAIN_ENV_VAR) in SpannerEnvironmentImpl.
  2. Read environment.getUniverseDomain() in SpannerOptions.resolveUniverseDomain(...).
  3. Pass getResolvedUniverseDomain() directly to spannerStubSettingsBuilder, instanceAdminStubSettingsBuilder, and databaseAdminStubSettingsBuilder, and remove resolveStubUniverseDomain(...).
  4. Replace the forked-JVM probe tests in SpannerOptionsTest and ConnectionOptionsTest with in-process tests using SpannerOptions.useEnvironment(...) (resetting via SpannerOptions.useDefaultEnvironment() in finally/@After).

2. ConnectionOptions and SpannerPool still ignore GOOGLE_CLOUD_UNIVERSE_DOMAIN by default

ConnectionProperties.UNIVERSE_DOMAIN defaults to "googleapis.com" rather than null. When a connection URI omits universeDomain:

  • ConnectionOptions.getUniverseDomain() returns "googleapis.com".
  • ConnectionOptions.getDefaultHost() passes "googleapis.com" to SpannerOptions.resolveUniverseDomain(...), skipping the env fallback.
  • SpannerPool passes "googleapis.com" to SpannerOptions.Builder#setUniverseDomain(...), overriding the env var.

ChannelProviderUniverseDomainProbe only worked because it explicitly called .setConnectionPropertyValue(ConnectionProperties.UNIVERSE_DOMAIN, null).

To fix this:

  • Change the default of ConnectionProperties.UNIVERSE_DOMAIN from "googleapis.com" to null (and update ConnectionOptionsTest.testUniverseDomain(), which currently expects spannerOptions.getUniverseDomain() to be "googleapis.com" when omitted, plus regenerate the connection properties markdown docs if needed).
  • Add tests using a plain connection URI without universeDomain (and without setting the property to null) verifying both ConnectionOptions#getChannelProvider() and SpannerPool.

3. Pass the resolved universe domain to BuiltInMetricsProvider

SpannerOptions still passes getUniverseDomain() (null or "" when unset/cleared) to builtInMetricsProvider in getBuiltInOpenTelemetry(), enablegRPCMetrics(...), and createCloudMonitoringMetricsTracerFactory(). If GOOGLE_CLOUD_UNIVERSE_DOMAIN="", SpannerCloudMonitoringExporter skips setUniverseDomain(...) and MetricServiceSettings resolves monitoring.:443. Passing getResolvedUniverseDomain() keeps monitoring consistent with the Spanner stubs.

4. Minor items

  • Explicit validation: Validation of GOOGLE_CLOUD_UNIVERSE_DOMAIN when host or emulatorHost is set currently only happens because ServiceOptions calls MoreObjects.firstNonNull(builder.host, getDefaultHost()) eagerly. Call getResolvedUniverseDomain() (or validateUniverseDomain(...)) explicitly in SpannerOptions instead of relying on that side effect.
  • Nullness annotations: Use org.jspecify.annotations.Nullable instead of adding javax.annotation.Nullable in ConnectionOptionsTest (and on new methods in SpannerOptions).

@olavloite

Copy link
Copy Markdown
Contributor

Also, long term, this should be fixed in gax + google-cloud-core, so we need less workaround code in the Spanner client.

… is unset

## Problem Statement

- When no universe domain was set explicitly, the Spanner client coerced it to `googleapis.com` for the data, instance admin, and database admin stubs and for the default host, so `GOOGLE_CLOUD_UNIVERSE_DOMAIN` was ignored.
- The `universeDomain` connection property defaulted to `googleapis.com`, so `ConnectionOptions` and pooled Spanner clients also ignored `GOOGLE_CLOUD_UNIVERSE_DOMAIN`.
- Built-in metrics received the unresolved universe domain rather than the one used by the Spanner stubs.

## Solution

- Add `SpannerOptions.SpannerEnvironment#getUniverseDomain()`, backed by `GOOGLE_CLOUD_UNIVERSE_DOMAIN`.
- Resolve the universe domain with the precedence: explicit setting, then environment, then `googleapis.com`, and use that value for the stubs, the default host, and built-in metrics.
- The `universeDomain` connection property is now unset by default, so `ConnectionOptions#getUniverseDomain()` returns `null` when it is not configured.
- An unset or empty `GOOGLE_CLOUD_UNIVERSE_DOMAIN` still falls back to `googleapis.com`.
- An invalid `GOOGLE_CLOUD_UNIVERSE_DOMAIN` fails with `IllegalArgumentException` at `SpannerOptions.Builder#build()` (including when a host or emulator host is set) and with `INVALID_ARGUMENT` in `ConnectionOptions#getChannelProvider()`.
@sakthivelmanii
sakthivelmanii force-pushed the fix/spanner-universe-domain-env branch from e7a73af to 498e49c Compare November 24, 2026 10:25
@sakthivelmanii

Copy link
Copy Markdown
Contributor Author

@olavloite Addressed all comments — moved the env lookup into SpannerEnvironment, defaulted universeDomain in ConnectionProperties to null, passed the resolved universe domain to BuiltInMetricsProvider, and switched the env tests to run in-process. Ready for another look!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants