Skip to content

Client#execute_query always routes to leader, even when unnecessary #196

@michaelherold

Description

@michaelherold

Problem

When using the single_use specifier for execute_query, the Spanner gem creates a read-only transaction. However, execute_query is hard-coded to always route requests to the leader, which introduces unnecessary latency depending on the region of the leader and the querist.

I believe that we should be able to check if a read-only transaction is open when determining whether to route to the leader, like so:

          single_use_tx = single_use_transaction single_use
-          route_to_leader = LARHeaders.execute_query true
+          route_to_leader = LARHeaders.execute_query !single_use_tx

but am unsure if I have the semantics correct.

This, however, has the underlying effect of setting the x-goog-spanner-route-to-leader value to "false" instead of just not setting it.

Environment details

  • OS: Any
  • Ruby version: Any
  • Gem name and version: google-cloud-spanner = 2.28.0 and current main branch

Steps to reproduce

  1. Create a client
  2. Run execute_query(sql, single_use: { bounded_staleness: 10 })
  3. See the query route through the leader even when specifying a different placement

Code example

require "google/cloud/spanner"

puts "Starting"
emulator_host = ENV.fetch("SPANNER_EMULATOR_HOST", nil)
project_id = ENV.fetch("SPANNER_EMULATOR_PROJECT", "")
instance_id = ENV.fetch("SPANNER_EMULATOR_INSTANCE", "")
database_id = ENV.fetch("SPANNER_EMULATOR_DATABASE", "")

puts "Creating project"
project = Google::Cloud::Spanner.new(
  project_id: project_id,
  emulator_host: emulator_host,
)

puts "Creating client"
client = project.client(instance_id, database_id)

puts "Executing sql"
puts client.execute_sql("SELECT * FROM my_table", single_use: { bounded_staleness: 10 })

Full backtrace

There is no backtrace, the query runs just not at the location one would expect.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions