Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ef2159c

Browse filesBrowse files
authored
feat: support request priorities (#834)
1 parent 444db7f commit ef2159c
Copy full SHA for ef2159c

File tree

Expand file treeCollapse file tree

2 files changed

+45
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+45
-0
lines changed

‎google/cloud/spanner_dbapi/connection.py

Copy file name to clipboardExpand all lines: google/cloud/spanner_dbapi/connection.py
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from google.api_core.exceptions import Aborted
2121
from google.api_core.gapic_v1.client_info import ClientInfo
2222
from google.cloud import spanner_v1 as spanner
23+
from google.cloud.spanner_v1 import RequestOptions
2324
from google.cloud.spanner_v1.session import _get_retry_delay
2425
from google.cloud.spanner_v1.snapshot import Snapshot
2526

@@ -103,6 +104,7 @@ def __init__(self, instance, database, read_only=False):
103104
self._own_pool = True
104105
self._read_only = read_only
105106
self._staleness = None
107+
self.request_priority = None
106108

107109
@property
108110
def autocommit(self):
@@ -442,11 +444,18 @@ def run_statement(self, statement, retried=False):
442444
ResultsChecksum() if retried else statement.checksum,
443445
)
444446

447+
if self.request_priority is not None:
448+
req_opts = RequestOptions(priority=self.request_priority)
449+
self.request_priority = None
450+
else:
451+
req_opts = None
452+
445453
return (
446454
transaction.execute_sql(
447455
statement.sql,
448456
statement.params,
449457
param_types=statement.param_types,
458+
request_options=req_opts,
450459
),
451460
ResultsChecksum() if retried else statement.checksum,
452461
)

‎tests/unit/spanner_dbapi/test_connection.py

Copy file name to clipboardExpand all lines: tests/unit/spanner_dbapi/test_connection.py
+36Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,42 @@ def test_staleness_single_use_readonly_autocommit(self):
883883

884884
connection.database.snapshot.assert_called_with(read_timestamp=timestamp)
885885

886+
def test_request_priority(self):
887+
from google.cloud.spanner_dbapi.checksum import ResultsChecksum
888+
from google.cloud.spanner_dbapi.cursor import Statement
889+
from google.cloud.spanner_v1 import RequestOptions
890+
891+
sql = "SELECT 1"
892+
params = []
893+
param_types = {}
894+
priority = 2
895+
896+
connection = self._make_connection()
897+
connection._transaction = mock.Mock(committed=False, rolled_back=False)
898+
connection._transaction.execute_sql = mock.Mock()
899+
900+
connection.request_priority = priority
901+
902+
req_opts = RequestOptions(priority=priority)
903+
904+
connection.run_statement(
905+
Statement(sql, params, param_types, ResultsChecksum(), False)
906+
)
907+
908+
connection._transaction.execute_sql.assert_called_with(
909+
sql, params, param_types=param_types, request_options=req_opts
910+
)
911+
assert connection.request_priority is None
912+
913+
# check that priority is applied for only one request
914+
connection.run_statement(
915+
Statement(sql, params, param_types, ResultsChecksum(), False)
916+
)
917+
918+
connection._transaction.execute_sql.assert_called_with(
919+
sql, params, param_types=param_types, request_options=None
920+
)
921+
886922

887923
def exit_ctx_func(self, exc_type, exc_value, traceback):
888924
"""Context __exit__ method mock."""

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.