Skip to content

feat: add support for leader aware routing #309

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

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,10 @@ def create_connect_args(self, url):
or `spanner:///projects/{project-id}/instances/{instance-id}`. For the latter,
database operations will be not be possible and if required a new engine with
database-id set will need to be created.
If you want to disble route to leader, pass options as following:
engine = create_engine(
"spanner+spanner:///projects/project-id/instances/instance-id/databases/database-id",
connect_args={'route_to_leader_enabled': False})
"""
match = re.match(
(
Expand Down
18 changes: 18 additions & 0 deletions test/test_suite_13.py
Original file line number Diff line number Diff line change
Expand Up @@ -1654,6 +1654,24 @@ 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",
connect_args={"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 @@ -1922,6 +1922,26 @@ def test_user_agent(self):
)


class RouteToLeaderEnabledTest(fixtures.TestBase):
"""
SPANNER TEST:

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",
connect_args={"route_to_leader_enabled": False},
)
with engine.connect() as connection:
assert (
connection.connection.instance._client.route_to_leader_enabled is False
)


class SimpleUpdateDeleteTest(_SimpleUpdateDeleteTest):
"""
SPANNER OVERRIDE:
Expand Down
41 changes: 41 additions & 0 deletions test/test_suite_20.py
Original file line number Diff line number Diff line change
Expand Up @@ -3113,6 +3113,47 @@ def test_create_engine_wo_database(self):
assert connection.connection.database is None


class RouteToLeaderEnabledTest(fixtures.TestBase):
"""
SPANNER TEST:

Check that SQLAlchemy dialect passes correct
route_to_leader_enabled to Client.
"""

def test_route_to_leader_disabled(self):
engine = create_engine(
"spanner:///projects/project-id/instances/instance-id/"
+ "databases/database-id",
connect_args={"route_to_leader_enabled": False},
)
with engine.connect() as connection:
assert (
connection.connection.instance._client.route_to_leader_enabled is False
)

def test_route_to_leader_enabled(self):
engine = create_engine(
"spanner:///projects/project-id/instances/instance-id/"
+ "databases/database-id",
connect_args={"route_to_leader_enabled": True},
)
with engine.connect() as connection:
assert (
connection.connection.instance._client.route_to_leader_enabled is True
)

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


class ReturningTest(fixtures.TestBase):
def setUp(self):
self._engine = create_engine(get_db_url())
Expand Down
Loading