Class SpannerClients

java.lang.Object
io.github.flink.gcp.connector.spanner.SpannerClients

@Internal public final class SpannerClients extends Object
Builds the Spanner service handle both directions of this connector open.

One helper rather than one copy per direction, because the emulator-versus-credentials branch is exactly the code that reads green on a developer machine and red in CI: a handle built without the emulator host reaches for application default credentials, which a workstation has and a build agent does not.

Nothing else about the connection is configured. Spanner's own setEmulatorHost switches the channel to plaintext and the credentials to none in one call, so this connector never goes through the shared plaintext-channel helpers the other connectors need.

The same call puts the client in its emulator mode, where it records no client metrics into a provider (SpannerOptions.isEmulatorEnabled(), 6.124.0), so an emulator job reports its rpcMethod series at zero. The emulator branch also turns the built-in Cloud Monitoring export off. In emulator mode the client never starts it anyway, and saying so explicitly keeps the test switch below from starting the process-wide exporter against an emulator.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final boolean
    Whether a client takes part in its built-in Cloud Monitoring export when nothing chooses; one place, as DEFAULT_EXPORT_CLIENT_METRICS_TO_FLINK is.
    static final boolean
    Whether a component exports its client's own metrics to Flink when nothing chooses: every builder starts from it, and a Table component without a builder resolves an unset option to it, so the default lives in one place (ADR-0139).
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    close(com.google.cloud.spanner.Spanner spanner, ClientMetrics clientMetrics)
    Closes a handle, then the client metrics its client recorded into, whatever the handle's close did.
    static com.google.cloud.spanner.Spanner
    open(DatabaseDestination database, com.google.cloud.spanner.SpannerOptions settings)
    Opens a handle from settings already assembled by the owning runtime component.
    static com.google.cloud.spanner.Spanner
    open(DatabaseDestination database, com.google.cloud.spanner.SpannerOptions settings, ClientMetrics clientMetrics)
    Opens a handle whose client records into client metrics the caller hands over with it, closing the client metrics if the handle cannot be created: nothing else would.
    static com.google.cloud.spanner.Spanner
    open(String target, com.google.cloud.spanner.SpannerOptions settings)
    Opens a handle from settings, naming what it reaches in the failure message.
    static com.google.cloud.spanner.SpannerOptions
    settings(DatabaseDestination database, EmulatorEndpoint emulatorEndpoint)
    Builds the client settings for a database.
    static com.google.cloud.spanner.SpannerOptions
    settings(DatabaseDestination database, EmulatorEndpoint emulatorEndpoint, com.google.auth.Credentials credentialsOverride)
    Builds client settings with an optional runtime-loaded credential override.
    static com.google.cloud.spanner.SpannerOptions
    settings(DatabaseDestination database, EmulatorEndpoint emulatorEndpoint, com.google.auth.Credentials credentialsOverride, ClientMetrics clientMetrics, boolean exportClientMetricsToCloudMonitoring)
    Builds client settings for a runtime component, with its client-metrics choices.
    static com.google.cloud.spanner.SpannerOptions
    settings(String project, EmulatorEndpoint emulatorEndpoint, com.google.auth.Credentials credentialsOverride)
    Builds client settings for a project, for a caller that reaches more than one database, as the catalog does: the handle carries only the project, and each database is named per call.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_EXPORT_CLIENT_METRICS_TO_CLOUD_MONITORING

      public static final boolean DEFAULT_EXPORT_CLIENT_METRICS_TO_CLOUD_MONITORING
      Whether a client takes part in its built-in Cloud Monitoring export when nothing chooses; one place, as DEFAULT_EXPORT_CLIENT_METRICS_TO_FLINK is.
      See Also:
  • Method Details

    • settings

      public static com.google.cloud.spanner.SpannerOptions settings(DatabaseDestination database, @Nullable EmulatorEndpoint emulatorEndpoint)
      Builds the client settings for a database.

      Separate from open(DatabaseDestination, SpannerOptions) so that the mapping is testable without opening a channel.

      Parameters:
      database - the database the handle will reach
      emulatorEndpoint - the emulator to reach, or null for the real service
      Returns:
      the settings
    • settings

      public static com.google.cloud.spanner.SpannerOptions settings(DatabaseDestination database, @Nullable EmulatorEndpoint emulatorEndpoint, @Nullable com.google.auth.Credentials credentialsOverride)
      Builds client settings with an optional runtime-loaded credential override.
      Parameters:
      database - the database the handle will reach
      emulatorEndpoint - the emulator to reach, or null for the real service
      credentialsOverride - credentials loaded by the runtime component, or null for ADC
      Returns:
      the settings
    • settings

      public static com.google.cloud.spanner.SpannerOptions settings(String project, @Nullable EmulatorEndpoint emulatorEndpoint, @Nullable com.google.auth.Credentials credentialsOverride)
      Builds client settings for a project, for a caller that reaches more than one database, as the catalog does: the handle carries only the project, and each database is named per call.
      Parameters:
      project - the project the handle will reach
      emulatorEndpoint - the emulator to reach, or null for the real service
      credentialsOverride - credentials loaded by the runtime component, or null for ADC
      Returns:
      the settings
    • settings

      public static com.google.cloud.spanner.SpannerOptions settings(DatabaseDestination database, @Nullable EmulatorEndpoint emulatorEndpoint, @Nullable com.google.auth.Credentials credentialsOverride, @Nullable ClientMetrics clientMetrics, boolean exportClientMetricsToCloudMonitoring)
      Builds client settings for a runtime component, with its client-metrics choices.
      Parameters:
      database - the database the handle will reach
      emulatorEndpoint - the emulator to reach, or null for the real service
      credentialsOverride - credentials loaded by the runtime component, or null for ADC
      clientMetrics - the client metrics the client records into, or null to export nothing to Flink
      exportClientMetricsToCloudMonitoring - whether the client keeps its built-in Cloud Monitoring export
      Returns:
      the settings
    • open

      public static com.google.cloud.spanner.Spanner open(DatabaseDestination database, com.google.cloud.spanner.SpannerOptions settings) throws IOException
      Opens a handle from settings already assembled by the owning runtime component.
      Parameters:
      database - the database to reach, for the failure message
      settings - the settings to open the handle from
      Returns:
      the handle, which the caller owns and must close
      Throws:
      IOException - if the handle cannot be created
    • open

      public static com.google.cloud.spanner.Spanner open(DatabaseDestination database, com.google.cloud.spanner.SpannerOptions settings, @Nullable ClientMetrics clientMetrics) throws IOException
      Opens a handle whose client records into client metrics the caller hands over with it, closing the client metrics if the handle cannot be created: nothing else would.
      Parameters:
      database - the database to reach, for the failure message
      settings - the settings to open the handle from, carrying the client metrics
      clientMetrics - the client metrics the settings carry, or null
      Returns:
      the handle, which the caller owns and closes with close(Spanner, ClientMetrics)
      Throws:
      IOException - if the handle cannot be created
    • close

      public static void close(com.google.cloud.spanner.Spanner spanner, @Nullable ClientMetrics clientMetrics)
      Closes a handle, then the client metrics its client recorded into, whatever the handle's close did. In this order because closed client metrics drop what they are given, so a client still closing would lose whatever it recorded meanwhile.
      Parameters:
      spanner - the handle to close
      clientMetrics - the client metrics to close after it, or null
    • open

      public static com.google.cloud.spanner.Spanner open(String target, com.google.cloud.spanner.SpannerOptions settings) throws IOException
      Opens a handle from settings, naming what it reaches in the failure message.
      Parameters:
      target - what the handle reaches, for the failure message
      settings - the settings to open the handle from
      Returns:
      the handle, which the caller owns and must close
      Throws:
      IOException - if the handle cannot be created