Skip to content

Commit 7aeec7b

Browse files
committed
PYTHON-1508 Remove stmtId from transaction commands
The statement identifier is no longer required by the server.
1 parent 876fa50 commit 7aeec7b

17 files changed

+91
-180
lines changed

pymongo/bulk.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,6 @@ def _execute_command(self, generator, write_concern, session,
277277
self.collection.codec_options, bwc)
278278
if not to_send:
279279
raise InvalidOperation("cannot do an empty bulk write")
280-
if session:
281-
session._advance_statement_id(len(to_send))
282280
result = bwc.write_command(request_id, msg, to_send)
283281
client._receive_cluster_time(result, session)
284282
results.append((run.idx_offset, result))

pymongo/client_session.py

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ class _Transaction(object):
208208
"""Internal class to hold transaction information in a ClientSession."""
209209
def __init__(self, opts):
210210
self.opts = opts
211+
self.sent_command = False
211212

212213

213214
class ClientSession(object):
@@ -310,7 +311,7 @@ def start_transaction(self, read_concern=None, write_concern=None):
310311

311312
self._transaction = _Transaction(TransactionOptions(
312313
read_concern=read_concern, write_concern=write_concern))
313-
self._server_session.statement_id = 0
314+
self._server_session._transaction_id += 1
314315
return _TransactionContext(self)
315316

316317
def commit_transaction(self):
@@ -337,9 +338,8 @@ def _finish_transaction(self, command_name):
337338
raise InvalidOperation("No transaction started")
338339

339340
try:
340-
if self._server_session.statement_id == 0:
341+
if not self._transaction.sent_command:
341342
# Not really started.
342-
self._server_session._transaction_id += 1
343343
return
344344

345345
# TODO: retryable. And it's weird to pass parse_write_concern_error
@@ -350,7 +350,6 @@ def _finish_transaction(self, command_name):
350350
write_concern=self._transaction.opts.write_concern,
351351
parse_write_concern_error=True)
352352
finally:
353-
self._server_session.reset_transaction()
354353
self._transaction = None
355354

356355
def _advance_cluster_time(self, cluster_time):
@@ -439,9 +438,9 @@ def _apply_to(self, command, is_retryable, read_preference):
439438
'read preference in a transaction must be primary, not: '
440439
'%r' % (read_preference,))
441440

442-
if self._server_session.statement_id == 0:
443-
# First statement begins a new transaction.
444-
self._server_session._transaction_id += 1
441+
if not self._transaction.sent_command:
442+
# First command begins a new transaction.
443+
self._transaction.sent_command = True
445444
command['startTransaction'] = True
446445

447446
if self._transaction.opts.read_concern:
@@ -457,15 +456,8 @@ def _apply_to(self, command, is_retryable, read_preference):
457456
command['readConcern'] = rc
458457

459458
command['txnNumber'] = self._server_session.transaction_id
460-
command['stmtId'] = self._server_session.statement_id
461459
command['autocommit'] = False
462460

463-
self._server_session.statement_id += 1
464-
465-
def _advance_statement_id(self, n):
466-
self._check_ended()
467-
self._server_session.advance_statement_id(n)
468-
469461
def _retry_transaction_id(self):
470462
self._check_ended()
471463
self._server_session.retry_transaction_id()
@@ -477,26 +469,18 @@ def __init__(self):
477469
self.session_id = {'id': Binary(uuid.uuid4().bytes, 4)}
478470
self.last_use = monotonic.time()
479471
self._transaction_id = 0
480-
self.statement_id = 0
481472

482473
def timed_out(self, session_timeout_minutes):
483474
idle_seconds = monotonic.time() - self.last_use
484475

485476
# Timed out if we have less than a minute to live.
486477
return idle_seconds > (session_timeout_minutes - 1) * 60
487478

488-
def advance_statement_id(self, n):
489-
# Every command advances the statement id by 1 already.
490-
self.statement_id += (n - 1)
491-
492479
@property
493480
def transaction_id(self):
494481
"""Positive 64-bit integer."""
495482
return Int64(self._transaction_id)
496483

497-
def reset_transaction(self):
498-
self.statement_id = 0
499-
500484
def retry_transaction_id(self):
501485
self._transaction_id -= 1
502486

test/transactions/abort.json

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
"txnNumber": {
7070
"$numberLong": "1"
7171
},
72-
"stmtId": 0,
7372
"startTransaction": true,
7473
"autocommit": false,
7574
"writeConcern": null
@@ -86,7 +85,6 @@
8685
"txnNumber": {
8786
"$numberLong": "1"
8887
},
89-
"stmtId": 1,
9088
"startTransaction": null,
9189
"autocommit": false,
9290
"writeConcern": null
@@ -112,7 +110,6 @@
112110
"txnNumber": {
113111
"$numberLong": "2"
114112
},
115-
"stmtId": 0,
116113
"startTransaction": true,
117114
"autocommit": false,
118115
"writeConcern": null
@@ -129,7 +126,6 @@
129126
"txnNumber": {
130127
"$numberLong": "2"
131128
},
132-
"stmtId": 1,
133129
"startTransaction": null,
134130
"autocommit": false,
135131
"writeConcern": null
@@ -183,7 +179,6 @@
183179
"txnNumber": {
184180
"$numberLong": "1"
185181
},
186-
"stmtId": 0,
187182
"startTransaction": true,
188183
"autocommit": false,
189184
"writeConcern": null
@@ -200,7 +195,6 @@
200195
"txnNumber": {
201196
"$numberLong": "1"
202197
},
203-
"stmtId": 1,
204198
"startTransaction": null,
205199
"autocommit": false,
206200
"writeConcern": null
@@ -269,7 +263,6 @@
269263
"txnNumber": {
270264
"$numberLong": "1"
271265
},
272-
"stmtId": 0,
273266
"startTransaction": true,
274267
"autocommit": false,
275268
"writeConcern": null
@@ -286,7 +279,6 @@
286279
"txnNumber": {
287280
"$numberLong": "1"
288281
},
289-
"stmtId": 1,
290282
"startTransaction": null,
291283
"autocommit": false,
292284
"writeConcern": null
@@ -375,7 +367,6 @@
375367
"txnNumber": {
376368
"$numberLong": "1"
377369
},
378-
"stmtId": 0,
379370
"startTransaction": true,
380371
"autocommit": false,
381372
"writeConcern": null
@@ -392,7 +383,6 @@
392383
"txnNumber": {
393384
"$numberLong": "1"
394385
},
395-
"stmtId": 1,
396386
"startTransaction": null,
397387
"autocommit": false,
398388
"writeConcern": null
@@ -471,7 +461,6 @@
471461
"txnNumber": {
472462
"$numberLong": "1"
473463
},
474-
"stmtId": 0,
475464
"startTransaction": true,
476465
"autocommit": false,
477466
"writeConcern": null
@@ -495,7 +484,6 @@
495484
"txnNumber": {
496485
"$numberLong": "1"
497486
},
498-
"stmtId": 1,
499487
"startTransaction": null,
500488
"autocommit": false,
501489
"writeConcern": null
@@ -519,7 +507,6 @@
519507
"txnNumber": {
520508
"$numberLong": "1"
521509
},
522-
"stmtId": 2,
523510
"startTransaction": null,
524511
"autocommit": false,
525512
"writeConcern": null
@@ -536,7 +523,6 @@
536523
"txnNumber": {
537524
"$numberLong": "1"
538525
},
539-
"stmtId": 3,
540526
"startTransaction": null,
541527
"autocommit": false,
542528
"writeConcern": null
@@ -551,6 +537,45 @@
551537
"data": []
552538
}
553539
}
540+
},
541+
{
542+
"description": "abort does not apply writeConcern",
543+
"operations": [
544+
{
545+
"name": "startTransaction",
546+
"arguments": {
547+
"session": "session0",
548+
"options": {
549+
"writeConcern": {
550+
"w": 10
551+
}
552+
}
553+
}
554+
},
555+
{
556+
"name": "insertOne",
557+
"arguments": {
558+
"document": {
559+
"_id": 1
560+
},
561+
"session": "session0"
562+
},
563+
"result": {
564+
"insertedId": 1
565+
}
566+
},
567+
{
568+
"name": "abortTransaction",
569+
"arguments": {
570+
"session": "session0"
571+
}
572+
}
573+
],
574+
"outcome": {
575+
"collection": {
576+
"data": []
577+
}
578+
}
554579
}
555580
]
556581
}

0 commit comments

Comments
 (0)