1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
|
/**
* Implements the sessions api for the shell.
*
* Roughly follows the driver sessions spec:
* https://github.com/mongodb/specifications/blob/master/source/sessions/driver-sessions.rst#abstract
*/
var {
DriverSession, SessionOptions, _DummyDriverSession, _DelegatingDriverSession,
} = (function() {
"use strict";
const kShellDefaultShouldRetryWrites =
typeof _shouldRetryWrites === "function" ? _shouldRetryWrites() : false;
function isNonNullObject(obj) {
return typeof obj === "object" && obj !== null;
}
function SessionOptions(rawOptions = {}) {
if (!(this instanceof SessionOptions)) {
return new SessionOptions(rawOptions);
}
let _readPreference = rawOptions.readPreference;
let _readConcern = rawOptions.readConcern;
let _writeConcern = rawOptions.writeConcern;
// Causal consistency is implicitly enabled when a session is explicitly started.
const _causalConsistency =
rawOptions.hasOwnProperty("causalConsistency") ? rawOptions.causalConsistency : true;
// If the user specified --retryWrites to the mongo shell, then we enable retryable
// writes automatically.
const _retryWrites = rawOptions.hasOwnProperty("retryWrites")
? rawOptions.retryWrites
: kShellDefaultShouldRetryWrites;
this.getReadPreference = function getReadPreference() {
return _readPreference;
};
this.setReadPreference = function setReadPreference(readPreference) {
_readPreference = readPreference;
};
this.getReadConcern = function getReadConcern() {
return _readConcern;
};
this.setReadConcern = function setReadConcern(readConcern) {
_readConcern = readConcern;
};
this.getWriteConcern = function getWriteConcern() {
return _writeConcern;
};
this.setWriteConcern = function setWriteConcern(writeConcern) {
if (!(writeConcern instanceof WriteConcern)) {
writeConcern = new WriteConcern(writeConcern);
}
_writeConcern = writeConcern;
};
this.isCausalConsistency = function isCausalConsistency() {
return _causalConsistency;
};
this.shouldRetryWrites = function shouldRetryWrites() {
return _retryWrites;
};
this.shellPrint = function _shellPrint() {
return this.toString();
};
this.tojson = function _tojson(...args) {
return tojson(rawOptions, ...args);
};
this.toString = function toString() {
return "SessionOptions(" + this.tojson() + ")";
};
}
function SessionAwareClient(client) {
const kWireVersionSupportingCausalConsistency = 6;
const kWireVersionSupportingLogicalSession = 6;
const kWireVersionSupportingRetryableWrites = 6;
this.getReadPreference = function getReadPreference(driverSession) {
const sessionOptions = driverSession.getOptions();
if (sessionOptions.getReadPreference() !== undefined) {
return sessionOptions.getReadPreference();
}
return client.getReadPref();
};
this.getReadConcern = function getReadConcern(driverSession) {
const sessionOptions = driverSession.getOptions();
if (sessionOptions.getReadConcern() !== undefined) {
return sessionOptions.getReadConcern();
}
if (client.getReadConcern() !== undefined) {
return {level: client.getReadConcern()};
}
return null;
};
this.getWriteConcern = function getWriteConcern(driverSession) {
const sessionOptions = driverSession.getOptions();
if (sessionOptions.getWriteConcern() !== undefined) {
return sessionOptions.getWriteConcern();
}
return client.getWriteConcern();
};
function serverSupports(wireVersion) {
return client.getMinWireVersion() <= wireVersion &&
wireVersion <= client.getMaxWireVersion();
}
// TODO: Update this whitelist, or convert it to a blacklist depending on the outcome of
// SERVER-31743.
const kCommandsThatSupportReadConcern = new Set([
"aggregate",
"count",
"distinct",
"explain",
"find",
"geoNear",
"geoSearch",
"group",
"mapReduce",
"mapreduce",
"parallelCollectionScan",
]);
function canUseReadConcern(cmdObj) {
let cmdName = Object.keys(cmdObj)[0];
// If the command is in a wrapped form, then we look for the actual command name inside
// the query/$query object.
let cmdObjUnwrapped = cmdObj;
if (cmdName === "query" || cmdName === "$query") {
cmdObjUnwrapped = cmdObj[cmdName];
cmdName = Object.keys(cmdObjUnwrapped)[0];
}
if (!kCommandsThatSupportReadConcern.has(cmdName)) {
return false;
}
if (cmdName === "explain") {
return kCommandsThatSupportReadConcern.has(Object.keys(cmdObjUnwrapped.explain)[0]);
}
return true;
}
function gossipClusterTime(cmdObj, clusterTime) {
cmdObj = Object.assign({}, cmdObj);
const cmdName = Object.keys(cmdObj)[0];
// If the command is in a wrapped form, then we look for the actual command object
// inside the query/$query object.
let cmdObjUnwrapped = cmdObj;
if (cmdName === "query" || cmdName === "$query") {
cmdObj[cmdName] = Object.assign({}, cmdObj[cmdName]);
cmdObjUnwrapped = cmdObj[cmdName];
}
if (!cmdObjUnwrapped.hasOwnProperty("$clusterTime")) {
cmdObjUnwrapped.$clusterTime = clusterTime;
}
return cmdObj;
}
function injectAfterClusterTime(cmdObj, operationTime) {
cmdObj = Object.assign({}, cmdObj);
const cmdName = Object.keys(cmdObj)[0];
// If the command is in a wrapped form, then we look for the actual command object
// inside the query/$query object.
let cmdObjUnwrapped = cmdObj;
if (cmdName === "query" || cmdName === "$query") {
cmdObj[cmdName] = Object.assign({}, cmdObj[cmdName]);
cmdObjUnwrapped = cmdObj[cmdName];
}
cmdObjUnwrapped.readConcern = Object.assign({}, cmdObjUnwrapped.readConcern);
const readConcern = cmdObjUnwrapped.readConcern;
if (!readConcern.hasOwnProperty("afterClusterTime")) {
readConcern.afterClusterTime = operationTime;
}
return cmdObj;
}
function prepareCommandRequest(driverSession, cmdObj) {
if (serverSupports(kWireVersionSupportingLogicalSession)) {
cmdObj = driverSession._serverSession.injectSessionId(cmdObj);
}
if (serverSupports(kWireVersionSupportingCausalConsistency) &&
(client.isReplicaSetMember() || client.isMongos()) &&
!jsTest.options().skipGossipingClusterTime) {
// The `clientClusterTime` is the highest clusterTime observed by any connection
// within this mongo shell.
const clientClusterTime = client.getClusterTime();
// The `sessionClusterTime` is the highest clusterTime tracked by the
// `driverSession` session and may lag behind `clientClusterTime` if operations on
// other sessions or connections are advancing the clusterTime.
const sessionClusterTime = driverSession.getClusterTime();
// We gossip the greater of the client's clusterTime and the session's clusterTime.
// If this is the first command being sent on this connection and/or session, then
// it's possible that either clusterTime hasn't been initialized yet. Additionally,
// if the user specified a malformed clusterTime as part of initialClusterTime, then
// we want the server to be the one to reject it and therefore write our comparisons
// using bsonWoCompare() accordingly.
if (isNonNullObject(clientClusterTime) || isNonNullObject(sessionClusterTime)) {
let clusterTimeToGossip;
if (!isNonNullObject(sessionClusterTime)) {
clusterTimeToGossip = clientClusterTime;
} else if (!isNonNullObject(clientClusterTime)) {
clusterTimeToGossip = sessionClusterTime;
} else {
clusterTimeToGossip =
(bsonWoCompare({_: clientClusterTime.clusterTime},
{_: sessionClusterTime.clusterTime}) >= 0)
? clientClusterTime
: sessionClusterTime;
}
cmdObj = gossipClusterTime(cmdObj, clusterTimeToGossip);
}
}
// TODO SERVER-31868: A user should get back an error if they attempt to advance the
// DriverSession's operationTime manually when talking to a stand-alone mongod. Removing
// the `(client.isReplicaSetMember() || client.isMongos())` condition will also involve
// calling resetOperationTime_forTesting() in JavaScript tests that start different
// cluster types.
if (serverSupports(kWireVersionSupportingCausalConsistency) &&
(client.isReplicaSetMember() || client.isMongos()) &&
(driverSession.getOptions().isCausalConsistency() ||
client.isCausalConsistency()) &&
canUseReadConcern(cmdObj)) {
// `driverSession.getOperationTime()` is the smallest time needed for performing a
// causally consistent read using the current session. Note that
// `client.getClusterTime()` is no smaller than the operation time and would
// therefore only be less efficient to wait until.
const operationTime = driverSession.getOperationTime();
if (operationTime !== undefined) {
cmdObj = injectAfterClusterTime(cmdObj, driverSession.getOperationTime());
}
}
// Retryable writes code should execute only we are not in an active transaction.
if (jsTest.options().alwaysInjectTransactionNumber &&
serverSupports(kWireVersionSupportingRetryableWrites) &&
driverSession.getOptions().shouldRetryWrites() &&
driverSession._serverSession.canRetryWrites(cmdObj)) {
cmdObj = driverSession._serverSession.assignTransactionNumber(cmdObj);
}
// If startTransaction was called on the session, attach txn number and readConcern.
// TODO: SERVER-34170 guard this code with a wire version check.
if (driverSession._serverSession.isInActiveTransaction()) {
cmdObj = driverSession._serverSession.assignTxnInfo(cmdObj);
}
return cmdObj;
}
function processCommandResponse(driverSession, res) {
if (res.hasOwnProperty("operationTime")) {
driverSession.advanceOperationTime(res.operationTime);
}
if (res.hasOwnProperty("$clusterTime")) {
driverSession.advanceClusterTime(res.$clusterTime);
client.advanceClusterTime(res.$clusterTime);
}
}
/**
* Returns true if the error code is retryable, assuming the command is idempotent.
*/
function isRetryableCode(code) {
return ErrorCodes.isNetworkError(code) || ErrorCodes.isNotMasterError(code) ||
// The driver's spec does not allow retrying on writeConcern errors, so only do so
// when testing retryable writes.
(jsTest.options().alwaysInjectTransactionNumber &&
ErrorCodes.isWriteConcernError(code));
}
/**
* Returns the error code from a write response that should be used in the check for
* retryability.
*/
function getEffectiveWriteErrorCode(res) {
let code;
if (res instanceof WriteResult) {
if (res.hasWriteError()) {
code = res.getWriteError().code;
} else if (res.hasWriteConcernError()) {
code = res.getWriteConcernError().code;
}
} else if (res instanceof BulkWriteResult) {
if (res.hasWriteErrors()) {
code = res.getWriteErrorAt(0).code;
} else if (res.hasWriteConcernError()) {
code = res.getWriteConcernError().code;
}
} else {
if (res.writeError) {
code = res.writeError.code;
} else if (res.writeErrors) {
code = res.writeErrors[0].code;
} else if (res.writeConcernError) {
code = res.writeConcernError.code;
}
}
return code;
}
function runClientFunctionWithRetries(
driverSession, cmdObj, clientFunction, clientFunctionArguments) {
let cmdName = Object.keys(cmdObj)[0];
// If the command is in a wrapped form, then we look for the actual command object
// inside the query/$query object.
if (cmdName === "query" || cmdName === "$query") {
cmdObj = cmdObj[cmdName];
cmdName = Object.keys(cmdObj)[0];
}
// TODO SERVER-33921: Revisit how the mongo shell decides whether it should retry a
// command or not.
const sessionOptions = driverSession.getOptions();
let numRetries =
(sessionOptions.shouldRetryWrites() && cmdObj.hasOwnProperty("txnNumber") &&
!jsTest.options().skipRetryOnNetworkError &&
!driverSession._serverSession.isInActiveTransaction())
? 1
: 0;
if (numRetries > 0 && jsTest.options().overrideRetryAttempts) {
numRetries = jsTest.options().overrideRetryAttempts;
}
do {
try {
let res = clientFunction.apply(client, clientFunctionArguments);
if (numRetries > 0) {
if (!res.ok && isRetryableCode(res.code)) {
// Don't decrement retries, because the command returned before the
// connection was closed, so a subsequent attempt will receive a network
// error (or NotMaster error) and need to retry.
if (jsTest.options().logRetryAttempts) {
print("=-=-=-= Retrying failed response with retryable code: " +
res.code + ", for command: " + cmdName +
", retries remaining: " + numRetries);
}
continue;
}
let code = getEffectiveWriteErrorCode(res);
if (isRetryableCode(code)) {
// Don't decrement retries, because the command returned before the
// connection was closed, so a subsequent attempt will receive a network
// error (or NotMaster error) and need to retry.
if (jsTest.options().logRetryAttempts) {
print("=-=-=-= Retrying write with retryable write error code: " +
code + ", for command: " + cmdName + ", retries remaining: " +
numRetries);
}
continue;
}
}
return res;
} catch (e) {
if (!isNetworkError(e) || numRetries === 0) {
throw e;
}
// We run an "isMaster" command explicitly to force the underlying DBClient to
// reconnect to the server.
const res = client.adminCommand({isMaster: 1});
if (res.ok !== 1) {
throw e;
}
// It's possible that the server we're connected with after re-establishing our
// connection doesn't support retryable writes. If that happens, then we just
// return the original network error back to the user.
const serverSupportsRetryableWrites = res.hasOwnProperty("minWireVersion") &&
res.hasOwnProperty("maxWireVersion") &&
res.minWireVersion <= kWireVersionSupportingRetryableWrites &&
kWireVersionSupportingRetryableWrites <= res.maxWireVersion;
if (!serverSupportsRetryableWrites) {
throw e;
}
}
--numRetries;
if (jsTest.options().logRetryAttempts) {
print("=-=-=-= Retrying on network error for command: " + cmdName +
", retries remaining: " + numRetries);
}
} while (numRetries >= 0);
}
this.runCommand = function runCommand(driverSession, dbName, cmdObj, options) {
cmdObj = prepareCommandRequest(driverSession, cmdObj);
const res = runClientFunctionWithRetries(
driverSession, cmdObj, client.runCommand, [dbName, cmdObj, options]);
processCommandResponse(driverSession, res);
return res;
};
this.runCommandWithMetadata = function runCommandWithMetadata(
driverSession, dbName, metadata, cmdObj) {
cmdObj = prepareCommandRequest(driverSession, cmdObj);
const res = runClientFunctionWithRetries(
driverSession, cmdObj, client.runCommandWithMetadata, [dbName, metadata, cmdObj]);
processCommandResponse(driverSession, res);
return res;
};
}
function TransactionOptions(rawOptions = {}) {
if (!(this instanceof TransactionOptions)) {
return new TransactionOptions(rawOptions);
}
let _readConcern = rawOptions.readConcern;
let _writeConcern = rawOptions.writeConcern;
this.setTxnReadConcern = function setTxnReadConcern(value) {
_readConcern = value;
};
this.getTxnReadConcern = function getTxnReadConcern() {
return _readConcern;
};
this.setTxnWriteConcern = function setTxnWriteConcern(value) {
_writeConcern = value;
};
this.getTxnWriteConcern = function getTxnWriteConcern() {
return _writeConcern;
};
}
// The server session maintains the state of a transaction, a monotonically increasing txn
// number, and a transaction's read/write concerns.
function ServerSession(client) {
// The default txnState is `inactive` until we call startTransaction.
let _txnState = ServerSession.TransactionStates.kInactive;
let _txnOptions;
// Keep track of the first statement of a transaction, which is the only statement
// in which a user can specify a readConcern and writeConcern.
// This will eventually turn into a stmtId field.
let _firstStatement = false;
// _txnNumber starts at -1 because when we increment it, the first transaction
// and retryable write will both have a txnNumber of 0.
let _txnNumber = -1;
let _lastUsed = new Date();
this.client = new SessionAwareClient(client);
this.handle = client._startSession();
this.isInActiveTransaction = function isInActiveTransaction() {
return _txnState === ServerSession.TransactionStates.kActive;
};
this.getLastUsed = function getLastUsed() {
return _lastUsed;
};
this.getTxnOptions = function getTxnOptions() {
return _txnOptions;
};
function updateLastUsed() {
_lastUsed = new Date();
}
this.injectSessionId = function injectSessionId(cmdObj) {
cmdObj = Object.assign({}, cmdObj);
const cmdName = Object.keys(cmdObj)[0];
// If the command is in a wrapped form, then we look for the actual command object
// inside the query/$query object.
let cmdObjUnwrapped = cmdObj;
if (cmdName === "query" || cmdName === "$query") {
cmdObj[cmdName] = Object.assign({}, cmdObj[cmdName]);
cmdObjUnwrapped = cmdObj[cmdName];
}
if (!cmdObjUnwrapped.hasOwnProperty("lsid")) {
cmdObjUnwrapped.lsid = this.handle.getId();
// We consider the session to still be in use by the client any time the session id
// is injected into the command object as part of making a request.
updateLastUsed();
}
return cmdObj;
};
this.assignTransactionNumber = function assignTransactionNumber(cmdObj) {
cmdObj = Object.assign({}, cmdObj);
const cmdName = Object.keys(cmdObj)[0];
// If the command is in a wrapped form, then we look for the actual command object
// inside the query/$query object.
let cmdObjUnwrapped = cmdObj;
if (cmdName === "query" || cmdName === "$query") {
cmdObj[cmdName] = Object.assign({}, cmdObj[cmdName]);
cmdObjUnwrapped = cmdObj[cmdName];
}
if (!cmdObjUnwrapped.hasOwnProperty("txnNumber")) {
// Since there's no native support for adding NumberLong instances and getting back
// another NumberLong instance, converting from a 64-bit floating-point value to a
// 64-bit integer value will overflow at 2**53.
_txnNumber++;
cmdObjUnwrapped.txnNumber = new NumberLong(_txnNumber);
}
return cmdObj;
};
this.canRetryWrites = function canRetryWrites(cmdObj) {
let cmdName = Object.keys(cmdObj)[0];
// If the command is in a wrapped form, then we look for the actual command name inside
// the query/$query object.
if (cmdName === "query" || cmdName === "$query") {
cmdObj = cmdObj[cmdName];
cmdName = Object.keys(cmdObj)[0];
}
if (isNonNullObject(cmdObj.writeConcern)) {
const writeConcern = cmdObj.writeConcern;
// We use bsonWoCompare() in order to handle cases where the "w" field is specified
// as a NumberInt() or NumberLong() instance.
if (writeConcern.hasOwnProperty("w") &&
bsonWoCompare({_: writeConcern.w}, {_: 0}) === 0) {
// Unacknowledged writes cannot be retried.
return false;
}
}
if (cmdName === "insert") {
if (!Array.isArray(cmdObj.documents)) {
// The command object is malformed, so we'll just leave it as-is and let the
// server reject it.
return false;
}
// Both single-statement operations (e.g. insertOne()) and multi-statement
// operations (e.g. insertMany()) can be retried regardless of whether they are
// executed in order by the server.
return true;
} else if (cmdName === "update") {
if (!Array.isArray(cmdObj.updates)) {
// The command object is malformed, so we'll just leave it as-is and let the
// server reject it.
return false;
}
const hasMultiUpdate = cmdObj.updates.some(updateOp => updateOp.multi);
if (hasMultiUpdate) {
// Operations that modify multiple documents (e.g. updateMany()) cannot be
// retried.
return false;
}
// Both single-statement operations (e.g. updateOne()) and multi-statement
// operations (e.g. bulkWrite()) can be retried regardless of whether they are
// executed in order by the server.
return true;
} else if (cmdName === "delete") {
if (!Array.isArray(cmdObj.deletes)) {
// The command object is malformed, so we'll just leave it as-is and let the
// server reject it.
return false;
}
// We use bsonWoCompare() in order to handle cases where the limit is specified as a
// NumberInt() or NumberLong() instance.
const hasMultiDelete = cmdObj.deletes.some(
deleteOp => bsonWoCompare({_: deleteOp.limit}, {_: 0}) === 0);
if (hasMultiDelete) {
// Operations that modify multiple documents (e.g. deleteMany()) cannot be
// retried.
return false;
}
// Both single-statement operations (e.g. deleteOne()) and multi-statement
// operations (e.g. bulkWrite()) can be retried regardless of whether they are
// executed in order by the server.
return true;
} else if (cmdName === "findAndModify" || cmdName === "findandmodify") {
// Operations that modify a single document (e.g. findOneAndUpdate()) can be
// retried.
return true;
}
return false;
};
this.assignTxnInfo = function assignTxnInfo(cmdObj) {
cmdObj = Object.assign({}, cmdObj);
const cmdName = Object.keys(cmdObj)[0];
// If the command is in a wrapped form, then we look for the actual command object
// inside the query/$query object.
let cmdObjUnwrapped = cmdObj;
if (cmdName === "query" || cmdName === "$query") {
cmdObj[cmdName] = Object.assign({}, cmdObj[cmdName]);
cmdObjUnwrapped = cmdObj[cmdName];
}
if (!cmdObjUnwrapped.hasOwnProperty("txnNumber")) {
// Since there's no native support for adding NumberLong instances and getting back
// another NumberLong instance, converting from a 64-bit floating-point value to a
// 64-bit integer value will overflow at 2**53.
cmdObjUnwrapped.txnNumber = new NumberLong(_txnNumber);
}
// readConcern and autocommit can only be specified on the first statement in a
// transaction.
if (_firstStatement) {
// TODO: As a part of SERVER-34052, we might also need to specify
// `cmdObjUnwrapped.startTransaction = 1` on the first statement of a multi
// statement txn.
cmdObjUnwrapped.autocommit = false;
if (_txnOptions.getTxnReadConcern() !== undefined) {
cmdObjUnwrapped.readConcern = _txnOptions.getTxnReadConcern();
}
if (_txnOptions.getTxnWriteConcern() !== undefined) {
cmdObjUnwrapped.writeConcern = _txnOptions.getTxnWriteConcern();
}
_firstStatement = false;
}
return cmdObj;
};
this.startTransaction = function startTransaction(txnOptsObj) {
if (_txnState === ServerSession.TransactionStates.kActive) {
throw new Error("There is already an active transaction on this session.");
}
_txnOptions = new TransactionOptions(txnOptsObj);
_txnState = ServerSession.TransactionStates.kActive;
_firstStatement = true;
_txnNumber++;
};
this.commitTransaction = function commitTransaction(driverSession) {
// run commitTxn command
return endTransaction("commitTransaction", driverSession);
};
this.abortTransaction = function abortTransaction(driverSession) {
// run abortTxn command
return endTransaction("abortTransaction", driverSession);
};
const endTransaction = (commandName, driverSession) => {
const cmd = {[commandName]: 1, txnNumber: NumberLong(_txnNumber)};
// run command against the admin database.
const res = this.client.runCommand(driverSession, "admin", cmd, 0);
_txnState = ServerSession.TransactionStates.kInactive;
return res;
};
}
// TransactionStates represents the state of the current transaction. The default state
// is `inactive` until startTransaction is called and changes the state to `active`.
// Calling a successful abort or commitTransaction will change the state to `inactive`.
ServerSession.TransactionStates = {
kActive: 'active',
kInactive: 'inactive',
};
function makeDriverSessionConstructor(implMethods, defaultOptions = {}) {
return function(client, options = defaultOptions) {
let _options = options;
let _hasEnded = false;
let _operationTime;
let _clusterTime;
if (!(_options instanceof SessionOptions)) {
_options = new SessionOptions(_options);
}
this._serverSession = implMethods.createServerSession(client);
this.getClient = function getClient() {
return client;
};
this._getSessionAwareClient = function _getSessionAwareClient() {
return this._serverSession.client;
};
this.getOptions = function getOptions() {
return _options;
};
this.getSessionId = function getSessionId() {
if (!this._serverSession.hasOwnProperty("handle")) {
return null;
}
return this._serverSession.handle.getId();
};
this.getOperationTime = function getOperationTime() {
return _operationTime;
};
this.advanceOperationTime = function advanceOperationTime(operationTime) {
if (!isNonNullObject(_operationTime) ||
bsonWoCompare({_: operationTime}, {_: _operationTime}) > 0) {
_operationTime = operationTime;
}
};
this.resetOperationTime_forTesting = function resetOperationTime_forTesting() {
_operationTime = undefined;
};
this.getClusterTime = function getClusterTime() {
return _clusterTime;
};
this.advanceClusterTime = function advanceClusterTime(clusterTime) {
if (!isNonNullObject(_clusterTime) ||
bsonWoCompare({_: clusterTime.clusterTime}, {_: _clusterTime.clusterTime}) >
0) {
_clusterTime = clusterTime;
}
};
this.resetClusterTime_forTesting = function resetClusterTime_forTesting() {
_clusterTime = undefined;
};
this.getDatabase = function getDatabase(dbName) {
const db = client.getDB(dbName);
db._session = this;
return db;
};
this.hasEnded = function hasEnded() {
return _hasEnded;
};
this.endSession = function endSession() {
if (this._hasEnded) {
return;
}
this._hasEnded = true;
implMethods.endSession(this._serverSession);
};
this.shellPrint = function() {
return this.toString();
};
this.tojson = function _tojson(...args) {
return tojson(this.getSessionId(), ...args);
};
this.toString = function toString() {
const sessionId = this.getSessionId();
if (sessionId === null) {
return "dummy session";
}
return "session " + tojson(sessionId);
};
this.startTransaction = function startTransaction(txnOptsObj = {}) {
this._serverSession.startTransaction(txnOptsObj);
};
this.commitTransaction = function commitTransaction() {
assert.commandWorked(this._serverSession.commitTransaction(this));
};
this.abortTransaction = function abortTransaction() {
assert.commandWorked(this._serverSession.abortTransaction(this));
};
};
}
const DriverSession = makeDriverSessionConstructor({
createServerSession: function createServerSession(client) {
return new ServerSession(client);
},
endSession: function endSession(serverSession) {
serverSession.handle.end();
},
});
function DelegatingDriverSession(client, originalSession) {
const sessionAwareClient = new SessionAwareClient(client);
this.getClient = function() {
return client;
};
this._getSessionAwareClient = function() {
return sessionAwareClient;
};
this.getDatabase = function(dbName) {
const db = client.getDB(dbName);
db._session = this;
return db;
};
return new Proxy(this, {
get: function get(target, property, receiver) {
// If the property is defined on the DelegatingDriverSession instance itself, then
// return it. Otherwise, get the value of the property from the `originalSession`
// instance.
if (target.hasOwnProperty(property)) {
return target[property];
}
return originalSession[property];
},
});
}
// The default session on the Mongo connection object should report that causal consistency
// isn't enabled when interrogating the SessionOptions since it must be enabled on the Mongo
// connection object.
//
// The default session on the Mongo connection object should also report that retryable
// writes isn't enabled when interrogating the SessionOptions since `DummyDriverSession` won't
// ever assign a transaction number.
const DummyDriverSession =
makeDriverSessionConstructor( // Force clang-format to break this line.
{
createServerSession: function createServerSession(client) {
return {
client: new SessionAwareClient(client),
injectSessionId: function injectSessionId(cmdObj) {
return cmdObj;
},
assignTransactionNumber: function assignTransactionNumber(cmdObj) {
return cmdObj;
},
canRetryWrites: function canRetryWrites(cmdObj) {
return false;
},
assignTxnInfo: function assignTxnInfo(cmdObj) {
return cmdObj;
},
isInActiveTransaction: function isInActiveTransaction() {
return false;
},
getTxnOptions: function getTxnOptions() {
return {};
},
startTransaction: function startTransaction() {
throw new Error("Must call startSession() on the Mongo connection " +
"object before starting a transaction.");
},
commitTransaction: function commitTransaction() {
throw new Error("Must call startSession() on the Mongo connection " +
"object before committing a transaction.");
},
abortTransaction: function abortTransaction() {
throw new Error("Must call startSession() on the Mongo connection " +
"object before aborting a transaction.");
},
};
},
endSession: function endSession(serverSession) {},
},
{causalConsistency: false, retryWrites: false});
// We don't actually put anything on DriverSession.prototype, but this way
// `session instanceof DriverSession` will work for DummyDriverSession instances.
DummyDriverSession.prototype = Object.create(DriverSession.prototype);
DummyDriverSession.prototype.constructor = DriverSession;
return {
DriverSession: DriverSession,
SessionOptions: SessionOptions,
_DummyDriverSession: DummyDriverSession,
_DelegatingDriverSession: DelegatingDriverSession,
};
})();
|