Skip to content

Commit c305f14

Browse files
committed
cache Query/GetMore.as_command, send stmtId with commit/abortTransaction
fix comparison of insertedIds in test_transactions.py
1 parent 54ba1b3 commit c305f14

File tree

10 files changed

+62
-30
lines changed

10 files changed

+62
-30
lines changed

pymongo/client_session.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ def _finish_transaction(self, command_name):
231231
self._client.admin.command(
232232
command_name,
233233
txnNumber=self._server_session.transaction_id,
234+
stmtId=self._server_session.statement_id,
234235
session=self,
235236
write_concern=self._current_transaction_opts.write_concern,
236237
parse_write_concern_error=True)
@@ -314,11 +315,13 @@ def _apply_to(self, command, is_retryable):
314315
command['autocommit'] = False
315316

316317
command['txnNumber'] = self._server_session.transaction_id
318+
317319
# TODO: Allow stmtId for find/getMore, SERVER-33213.
318320
name = next(iter(command))
319321
if name not in ('find', 'getMore'):
320322
command['stmtId'] = self._server_session.statement_id
321-
self._server_session.statement_id += 1
323+
324+
self._server_session.statement_id += 1
322325

323326
def _advance_statement_id(self, n):
324327
self._check_ended()

pymongo/message.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ class _Query(object):
222222
__slots__ = ('flags', 'db', 'coll', 'ntoskip', 'spec',
223223
'fields', 'codec_options', 'read_preference', 'limit',
224224
'batch_size', 'name', 'read_concern', 'collation',
225-
'session', 'client')
225+
'session', 'client', '__as_command')
226226

227227
def __init__(self, flags, db, coll, ntoskip, spec, fields,
228228
codec_options, read_preference, limit,
@@ -242,6 +242,7 @@ def __init__(self, flags, db, coll, ntoskip, spec, fields,
242242
self.session = session
243243
self.client = client
244244
self.name = 'find'
245+
self.__as_command = None
245246

246247
def use_command(self, sock_info, exhaust):
247248
use_find_cmd = False
@@ -265,10 +266,13 @@ def use_command(self, sock_info, exhaust):
265266
return use_find_cmd
266267

267268
def as_command(self, sock_info):
268-
"""Return a find command document for this query.
269+
"""Return a find command document for this query."""
270+
# We use the command twice: on the wire and for command monitoring.
271+
# Generate it once, for speed and to avoid repeating side-effects
272+
# like incrementing the session's statement id.
273+
if self.__as_command is not None:
274+
return self.__as_command
269275

270-
Should be called *after* get_message.
271-
"""
272276
explain = '$explain' in self.spec
273277
cmd = _gen_find_command(
274278
self.coll, self.spec, self.fields, self.ntoskip,
@@ -288,7 +292,8 @@ def as_command(self, sock_info):
288292
'readConcern', {})[
289293
'afterClusterTime'] = session.operation_time
290294
sock_info.send_cluster_time(cmd, session, self.client)
291-
return cmd, self.db
295+
self.__as_command = cmd, self.db
296+
return self.__as_command
292297

293298
def get_message(self, set_slave_ok, sock_info, use_cmd=False):
294299
"""Get a query message, possibly setting the slaveOk bit."""
@@ -328,7 +333,7 @@ class _GetMore(object):
328333
"""A getmore operation."""
329334

330335
__slots__ = ('db', 'coll', 'ntoreturn', 'cursor_id', 'max_await_time_ms',
331-
'codec_options', 'session', 'client')
336+
'codec_options', 'session', 'client', '__as_command')
332337

333338
name = 'getMore'
334339

@@ -342,21 +347,27 @@ def __init__(self, db, coll, ntoreturn, cursor_id, codec_options, session,
342347
self.session = session
343348
self.client = client
344349
self.max_await_time_ms = max_await_time_ms
350+
self.__as_command = None
345351

346352
def use_command(self, sock_info, exhaust):
347353
sock_info.validate_session(self.client, self.session)
348354
return sock_info.max_wire_version >= 4 and not exhaust
349355

350356
def as_command(self, sock_info):
351357
"""Return a getMore command document for this query."""
358+
# See _Query.as_command for an explanation of this caching.
359+
if self.__as_command is not None:
360+
return self.__as_command
361+
352362
cmd = _gen_get_more_command(self.cursor_id, self.coll,
353363
self.ntoreturn,
354364
self.max_await_time_ms)
355365

356366
if self.session:
357367
self.session._apply_to(cmd, False)
358368
sock_info.send_cluster_time(cmd, self.session, self.client)
359-
return cmd, self.db
369+
self.__as_command = cmd, self.db
370+
return self.__as_command
360371

361372
def get_message(self, dummy0, sock_info, use_cmd=False):
362373
"""Get a getmore message."""

test/test_transactions.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ def check_result(self, expected_result, result):
7575
for res in expected_result:
7676
prop = camel_to_snake(res)
7777
# SPEC-869: Only BulkWriteResult has upserted_count.
78-
if (prop == "upserted_count" and
79-
not isinstance(result, BulkWriteResult)):
78+
if (prop == "upserted_count"
79+
and not isinstance(result, BulkWriteResult)):
8080
if result.upserted_id is not None:
8181
upserted_count = 1
8282
else:
@@ -85,22 +85,27 @@ def check_result(self, expected_result, result):
8585
elif prop == "inserted_ids":
8686
# BulkWriteResult does not have inserted_ids.
8787
if isinstance(result, BulkWriteResult):
88-
self.assertEqual(len(expected_result[res]), result.inserted_count)
88+
self.assertEqual(len(expected_result[res]),
89+
result.inserted_count)
8990
else:
9091
# InsertManyResult may be compared to [id1] from the
9192
# crud spec or {"0": id1} from the retryable write spec.
9293
ids = expected_result[res]
9394
if isinstance(ids, dict):
9495
ids = [ids[str(i)] for i in range(len(ids))]
95-
self.assertEqual(ids, result.inserted_ids)
96+
self.assertEqual(ids,
97+
result.inserted_ids)
9698
elif prop == "upserted_ids":
9799
# Convert indexes from strings to integers.
98100
ids = expected_result[res]
99101
expected_ids = {}
100102
for str_index in ids:
101103
expected_ids[int(str_index)] = ids[str_index]
102-
self.assertEqual(expected_ids != result.upserted_ids)
103-
self.assertEqual(getattr(result, prop), expected_result[res])
104+
self.assertEqual(expected_ids,
105+
result.upserted_ids)
106+
else:
107+
self.assertEqual(getattr(result, prop),
108+
expected_result[res])
104109
return True
105110
else:
106111
self.assertEqual(result, expected_result)

test/transactions/abort.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
"abortTransaction": 1,
8282
"lsid": "session0",
8383
"txnNumber": 1,
84+
"stmtId": 1,
8485
"writeConcern": null
8586
},
8687
"command_name": "abortTransaction",
@@ -113,6 +114,7 @@
113114
"abortTransaction": 1,
114115
"lsid": "session0",
115116
"txnNumber": 2,
117+
"stmtId": 1,
116118
"writeConcern": null
117119
},
118120
"command_name": "abortTransaction",
@@ -176,6 +178,7 @@
176178
"abortTransaction": 1,
177179
"lsid": "session0",
178180
"txnNumber": 1,
181+
"stmtId": 1,
179182
"writeConcern": null
180183
},
181184
"command_name": "abortTransaction",
@@ -254,6 +257,7 @@
254257
"abortTransaction": 1,
255258
"lsid": "session0",
256259
"txnNumber": 1,
260+
"stmtId": 1,
257261
"writeConcern": null
258262
},
259263
"command_name": "abortTransaction",
@@ -265,6 +269,7 @@
265269
"abortTransaction": 1,
266270
"lsid": "session0",
267271
"txnNumber": 1,
272+
"stmtId": 1,
268273
"writeConcern": null
269274
},
270275
"command_name": "abortTransaction",
@@ -362,6 +367,7 @@
362367
"abortTransaction": 1,
363368
"lsid": "session0",
364369
"txnNumber": 1,
370+
"stmtId": 1,
365371
"writeConcern": null
366372
},
367373
"command_name": "abortTransaction",

test/transactions/auto-start.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
"commitTransaction": 1,
8686
"lsid": "session0",
8787
"txnNumber": 1,
88+
"stmtId": 1,
8889
"writeConcern": null
8990
},
9091
"command_name": "commitTransaction",
@@ -137,6 +138,7 @@
137138
"commitTransaction": 1,
138139
"lsid": "session0",
139140
"txnNumber": 2,
141+
"stmtId": 2,
140142
"writeConcern": null
141143
},
142144
"command_name": "commitTransaction",
@@ -223,6 +225,7 @@
223225
"commitTransaction": 1,
224226
"lsid": "session0",
225227
"txnNumber": 1,
228+
"stmtId": 1,
226229
"writeConcern": null
227230
},
228231
"command_name": "commitTransaction",
@@ -313,6 +316,7 @@
313316
"abortTransaction": 1,
314317
"lsid": "session0",
315318
"txnNumber": 1,
319+
"stmtId": 1,
316320
"writeConcern": null
317321
},
318322
"command_name": "abortTransaction",
@@ -345,6 +349,7 @@
345349
"commitTransaction": 1,
346350
"lsid": "session0",
347351
"txnNumber": 2,
352+
"stmtId": 1,
348353
"writeConcern": null
349354
},
350355
"command_name": "commitTransaction",

test/transactions/commit.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
"commitTransaction": 1,
8181
"lsid": "session0",
8282
"txnNumber": 1,
83+
"stmtId": 1,
8384
"writeConcern": null
8485
},
8586
"command_name": "commitTransaction",
@@ -112,6 +113,7 @@
112113
"commitTransaction": 1,
113114
"lsid": "session0",
114115
"txnNumber": 2,
116+
"stmtId": 1,
115117
"writeConcern": null
116118
},
117119
"command_name": "commitTransaction",
@@ -196,6 +198,7 @@
196198
"commitTransaction": 1,
197199
"lsid": "session0",
198200
"txnNumber": 1,
201+
"stmtId": 1,
199202
"writeConcern": null
200203
},
201204
"command_name": "commitTransaction",

test/transactions/reads.json

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,6 @@
2525
"arguments": {
2626
"session": "session0"
2727
}
28-
},
29-
{
30-
"command_started_event": {
31-
"command": {
32-
"commitTransaction": 1,
33-
"lsid": "session0",
34-
"txnNumber": 1,
35-
"writeConcern": null
36-
},
37-
"command_name": "commitTransaction",
38-
"database_name": "admin"
39-
}
4028
}
4129
],
4230
"tests": [
@@ -122,6 +110,7 @@
122110
"commitTransaction": 1,
123111
"lsid": "session0",
124112
"txnNumber": 1,
113+
"stmtId": 2,
125114
"writeConcern": null
126115
},
127116
"command_name": "commitTransaction",
@@ -270,6 +259,7 @@
270259
"commitTransaction": 1,
271260
"lsid": "session0",
272261
"txnNumber": 1,
262+
"stmtId": 4,
273263
"writeConcern": null
274264
},
275265
"command_name": "commitTransaction",
@@ -358,6 +348,7 @@
358348
"commitTransaction": 1,
359349
"lsid": "session0",
360350
"txnNumber": 1,
351+
"stmtId": 2,
361352
"writeConcern": null
362353
},
363354
"command_name": "commitTransaction",

test/transactions/snapshot-reads.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@
219219
"commitTransaction": 1,
220220
"lsid": "session0",
221221
"txnNumber": 1,
222+
"stmtId": 3,
222223
"writeConcern": null
223224
},
224225
"command_name": "commitTransaction",
@@ -313,6 +314,7 @@
313314
"commitTransaction": 1,
314315
"lsid": "session0",
315316
"txnNumber": 2,
317+
"stmtId": 3,
316318
"writeConcern": null
317319
},
318320
"command_name": "commitTransaction",
@@ -799,6 +801,7 @@
799801
"commitTransaction": 1,
800802
"lsid": "session0",
801803
"txnNumber": 1,
804+
"stmtId": 3,
802805
"writeConcern": null
803806
},
804807
"command_name": "commitTransaction",
@@ -893,6 +896,7 @@
893896
"commitTransaction": 1,
894897
"lsid": "session0",
895898
"txnNumber": 2,
899+
"stmtId": 3,
896900
"writeConcern": null
897901
},
898902
"command_name": "commitTransaction",

test/transactions/statement-ids.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@
3636
"session": "session0"
3737
},
3838
"result": {
39-
"insertedIds": [
40-
2,
41-
3
42-
]
39+
"insertedIds": {
40+
"0": 2,
41+
"1": 3
42+
}
4343
}
4444
},
4545
{
@@ -153,6 +153,7 @@
153153
"commitTransaction": 1,
154154
"lsid": "session0",
155155
"txnNumber": 1,
156+
"stmtId": 4,
156157
"writeConcern": null
157158
},
158159
"command_name": "commitTransaction",
@@ -185,6 +186,7 @@
185186
"commitTransaction": 1,
186187
"lsid": "session0",
187188
"txnNumber": 2,
189+
"stmtId": 1,
188190
"writeConcern": null
189191
},
190192
"command_name": "commitTransaction",

test/transactions/write-concern.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"commitTransaction": 1,
5959
"lsid": "session0",
6060
"txnNumber": 1,
61+
"stmtId": 1,
6162
"writeConcern": {
6263
"w": "majority"
6364
}
@@ -131,6 +132,7 @@
131132
"commitTransaction": 1,
132133
"lsid": "session0",
133134
"txnNumber": 1,
135+
"stmtId": 1,
134136
"writeConcern": null
135137
},
136138
"command_name": "commitTransaction",

0 commit comments

Comments
 (0)