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

feat: add support for leader aware routing #309

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 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
12 changes: 11 additions & 1 deletion google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,9 @@ def create_connect_args(self, url):

The given URL follows the style:
`spanner:///projects/{project-id}/instances/{instance-id}/databases/{database-id}`

If you want to disble route to leader add it to the url as following:
`spanner:///projects/{project-id}/instances/{instance-id}/databases/{database-id}?route_to_leader_enabled=False`
"""
match = re.match(
(
Expand All @@ -582,9 +585,16 @@ def create_connect_args(self, url):
url.database,
)
dist = pkg_resources.get_distribution("sqlalchemy-spanner")
options = {"user_agent": f"gl-{dist.project_name}/{dist.version}"}
options.update(url.query)
if "route_to_leader_enabled" in options:
if options["route_to_leader_enabled"].lower() == "false":
options["route_to_leader_enabled"] = False
else:
options["route_to_leader_enabled"] = True
return (
[match.group("instance"), match.group("database"), match.group("project")],
{"user_agent": f"gl-{dist.project_name}/{dist.version}"},
options,
)

@engine_to_connection
Expand Down
2 changes: 1 addition & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def compliance_test_14(session):
session.install("-e", ".[tracing]")
session.run("python", "create_test_database.py")

session.install("sqlalchemy>=1.4")
session.install("sqlalchemy>=1.4,<2.0")
asthamohta marked this conversation as resolved.
Show resolved Hide resolved

session.run(
"py.test",
Expand Down
17 changes: 17 additions & 0 deletions test/test_suite_13.py
Original file line number Diff line number Diff line change
Expand Up @@ -1593,6 +1593,23 @@ def test_user_agent(self):
)


class RouteToLeaderEnabledTest(SpannerSpecificTestBase):
"""
Check that SQLAlchemy dialect passes correct
route_to_leader_enabled to Client.
"""

def test_route_to_leader(self):
engine = create_engine(
"spanner:///projects/project-id/instances/instance-id/databases/database-id"
"?route_to_leader_enabled=False"
)
with engine.connect() as connection:
assert (
connection.connection.instance._client.route_to_leader_enabled is False
)


class ExecutionOptionsReadOnlyTest(fixtures.TestBase):
"""
Check that `execution_options()` method correctly
Expand Down
20 changes: 20 additions & 0 deletions test/test_suite_14.py
Original file line number Diff line number Diff line change
Expand Up @@ -1767,6 +1767,26 @@ def test_user_agent(self):
)


class RouteToLeaderEnabledTest(fixtures.TestBase):
"""
Check that SQLAlchemy dialect passes correct
route_to_leader_enabled to Client.
"""

def setUp(self):
self._engine = create_engine(
"spanner:///projects/appdev-soda-spanner-staging/instances/"
"sqlalchemy-dialect-test/databases/compliance-test"
"?route_to_leader_enabled=False"
)

def test_route_to_leader_enabled(self):
olavloite marked this conversation as resolved.
Show resolved Hide resolved
with self._engine.connect() as connection:
assert (
connection.connection.instance._client.route_to_leader_enabled is False
)


class SimpleUpdateDeleteTest(_SimpleUpdateDeleteTest):
"""
SPANNER OVERRIDE:
Expand Down