Skip to content

Commit 3a1611e

Browse files
committed
Reduce edit surface for better precision
1 parent 9242220 commit 3a1611e

File tree

3 files changed

+52
-52
lines changed

3 files changed

+52
-52
lines changed

google/cloud/spanner_v1/pool.py

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
from google.cloud.spanner_v1._opentelemetry_tracing import (
2929
add_span_event,
3030
get_current_span,
31-
trace_call,
3231
)
3332
from warnings import warn
3433

@@ -206,51 +205,49 @@ def bind(self, database):
206205
)
207206
self._database_role = self._database_role or self._database.database_role
208207

209-
with trace_call("CloudSpanner.BatchCreateSessions", self) as span:
210-
requested_session_count = self.size - self._sessions.qsize()
211-
span_event_attributes = {"kind": type(self).__name__}
212-
request = BatchCreateSessionsRequest(
213-
database=database.name,
214-
session_count=requested_session_count,
215-
session_template=Session(creator_role=self.database_role),
216-
)
217-
218-
if requested_session_count > 0:
219-
add_span_event(
220-
span,
221-
f"Requesting {requested_session_count} sessions",
222-
span_event_attributes,
223-
)
208+
requested_session_count = self.size - self._sessions.qsize()
209+
request = BatchCreateSessionsRequest(
210+
database=database.name,
211+
session_count=requested_session_count,
212+
session_template=Session(creator_role=self.database_role),
213+
)
224214

225-
if self._sessions.full():
226-
add_span_event(
227-
span, "Session pool is already full", span_event_attributes
228-
)
229-
return
215+
span = get_current_span()
216+
span_event_attributes = {"kind": type(self).__name__}
217+
if requested_session_count > 0:
218+
add_span_event(
219+
span,
220+
f"Requesting {requested_session_count} sessions",
221+
span_event_attributes,
222+
)
230223

231-
returned_session_count = 0
232-
while not self._sessions.full():
233-
request.session_count = requested_session_count - self._sessions.qsize()
234-
add_span_event(
235-
span,
236-
f"Creating {request.session_count} sessions",
237-
span_event_attributes,
238-
)
239-
resp = api.batch_create_sessions(
240-
request=request,
241-
metadata=metadata,
242-
)
243-
for session_pb in resp.session:
244-
session = self._new_session()
245-
session._session_id = session_pb.name.split("/")[-1]
246-
self._sessions.put(session)
247-
returned_session_count += 1
224+
if self._sessions.full():
225+
add_span_event(span, "Session pool is already full", span_event_attributes)
226+
return
248227

228+
returned_session_count = 0
229+
while not self._sessions.full():
230+
request.session_count = requested_session_count - self._sessions.qsize()
249231
add_span_event(
250232
span,
251-
f"Requested for {requested_session_count} sessions, returned {returned_session_count}",
233+
f"Creating {request.session_count} sessions",
252234
span_event_attributes,
253235
)
236+
resp = api.batch_create_sessions(
237+
request=request,
238+
metadata=metadata,
239+
)
240+
for session_pb in resp.session:
241+
session = self._new_session()
242+
session._session_id = session_pb.name.split("/")[-1]
243+
self._sessions.put(session)
244+
returned_session_count += 1
245+
246+
add_span_event(
247+
span,
248+
f"Requested for {requested_session_count} sessions, returned {returned_session_count}",
249+
span_event_attributes,
250+
)
254251

255252
def get(self, timeout=None):
256253
"""Check a session out from the pool.

google/cloud/spanner_v1/transaction.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,9 @@ def commit(
243243
:raises ValueError: if there are no mutations to commit.
244244
"""
245245
self._check_state()
246-
not_began = self._transaction_id is None and len(self._mutations) == 0
247-
if not_began:
246+
if self._transaction_id is None and len(self._mutations) > 0:
247+
self.begin()
248+
elif self._transaction_id is None and len(self._mutations) == 0:
248249
raise ValueError("Transaction is not begun")
249250

250251
database = self._session._database

tests/unit/test_pool.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -229,27 +229,29 @@ def test_spans_bind_get(self):
229229
]
230230
self.assertSpanEvents("pool.Get", wantEventNames, span)
231231

232-
# Check for the overall spans.
232+
# Check for the overall spans too.
233233
self.assertSpanAttributes(
234-
"CloudSpanner.BatchCreateSessions",
234+
"pool.Get",
235235
attributes=TestFixedSizePool.BASE_ATTRIBUTES,
236236
)
237237

238238
wantEventNames = [
239-
"Requesting 4 sessions",
240-
"Creating 4 sessions",
241-
"Creating 2 sessions",
242-
"Requested for 4 sessions, returned 4",
239+
"Acquiring session",
240+
"Waiting for a session to become available",
241+
"Acquired session",
243242
]
244-
self.assertSpanEvents("CloudSpanner.BatchCreateSessions", wantEventNames)
243+
self.assertSpanEvents("pool.Get", wantEventNames)
245244

246-
def test_spans_get_create_sessions(self):
245+
def test_spans_pool_bind(self):
247246
pool = self._make_one(size=1)
248247
database = _Database("name")
249248
SESSIONS = []
250249
database._sessions.extend(SESSIONS)
250+
fauxSession = mock.Mock()
251+
setattr(fauxSession, "_database", database)
251252
try:
252-
pool.bind(database)
253+
with trace_call("testBind", fauxSession):
254+
pool.bind(database)
253255
except Exception:
254256
pass
255257

@@ -259,11 +261,11 @@ def test_spans_get_create_sessions(self):
259261
"exception",
260262
"exception",
261263
]
262-
self.assertSpanEvents("CloudSpanner.BatchCreateSessions", wantEventNames)
264+
self.assertSpanEvents("testBind", wantEventNames)
263265

264266
# Check for the overall spans.
265267
self.assertSpanAttributes(
266-
"CloudSpanner.BatchCreateSessions",
268+
"testBind",
267269
status=StatusCode.ERROR,
268270
attributes=TestFixedSizePool.BASE_ATTRIBUTES,
269271
)

0 commit comments

Comments
 (0)